* Merge from mainline
[official-gcc.git] / gcc / dwarf2out.c
blob3d5688cd7cff91fae6a5493dc7504fc352d15506
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 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 2, 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 COPYING. If not, write to the Free
22 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
23 02110-1301, USA. */
25 /* TODO: Emit .debug_line header even when there are no functions, since
26 the file numbers are used by .debug_info. Alternately, leave
27 out locations for types and decls.
28 Avoid talking about ctors and op= for PODs.
29 Factor out common prologue sequences into multiple CIEs. */
31 /* The first part of this file deals with the DWARF 2 frame unwind
32 information, which is also used by the GCC efficient exception handling
33 mechanism. The second part, controlled only by an #ifdef
34 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
35 information. */
37 #include "config.h"
38 #include "system.h"
39 #include "coretypes.h"
40 #include "tm.h"
41 #include "tree.h"
42 #include "version.h"
43 #include "flags.h"
44 #include "real.h"
45 #include "rtl.h"
46 #include "hard-reg-set.h"
47 #include "regs.h"
48 #include "insn-config.h"
49 #include "reload.h"
50 #include "function.h"
51 #include "output.h"
52 #include "expr.h"
53 #include "libfuncs.h"
54 #include "except.h"
55 #include "dwarf2.h"
56 #include "dwarf2out.h"
57 #include "dwarf2asm.h"
58 #include "toplev.h"
59 #include "varray.h"
60 #include "ggc.h"
61 #include "md5.h"
62 #include "tm_p.h"
63 #include "diagnostic.h"
64 #include "debug.h"
65 #include "target.h"
66 #include "langhooks.h"
67 #include "hashtab.h"
68 #include "cgraph.h"
69 #include "input.h"
71 #ifdef DWARF2_DEBUGGING_INFO
72 static void dwarf2out_source_line (unsigned int, const char *);
73 #endif
75 /* DWARF2 Abbreviation Glossary:
76 CFA = Canonical Frame Address
77 a fixed address on the stack which identifies a call frame.
78 We define it to be the value of SP just before the call insn.
79 The CFA register and offset, which may change during the course
80 of the function, are used to calculate its value at runtime.
81 CFI = Call Frame Instruction
82 an instruction for the DWARF2 abstract machine
83 CIE = Common Information Entry
84 information describing information common to one or more FDEs
85 DIE = Debugging Information Entry
86 FDE = Frame Description Entry
87 information describing the stack call frame, in particular,
88 how to restore registers
90 DW_CFA_... = DWARF2 CFA call frame instruction
91 DW_TAG_... = DWARF2 DIE tag */
93 #ifndef DWARF2_FRAME_INFO
94 # ifdef DWARF2_DEBUGGING_INFO
95 # define DWARF2_FRAME_INFO \
96 (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
97 # else
98 # define DWARF2_FRAME_INFO 0
99 # endif
100 #endif
102 /* Map register numbers held in the call frame info that gcc has
103 collected using DWARF_FRAME_REGNUM to those that should be output in
104 .debug_frame and .eh_frame. */
105 #ifndef DWARF2_FRAME_REG_OUT
106 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
107 #endif
109 /* Decide whether we want to emit frame unwind information for the current
110 translation unit. */
113 dwarf2out_do_frame (void)
115 /* We want to emit correct CFA location expressions or lists, so we
116 have to return true if we're going to output debug info, even if
117 we're not going to output frame or unwind info. */
118 return (write_symbols == DWARF2_DEBUG
119 || write_symbols == VMS_AND_DWARF2_DEBUG
120 || DWARF2_FRAME_INFO
121 #ifdef DWARF2_UNWIND_INFO
122 || (DWARF2_UNWIND_INFO
123 && (flag_unwind_tables
124 || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)))
125 #endif
129 /* The size of the target's pointer type. */
130 #ifndef PTR_SIZE
131 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
132 #endif
134 /* Array of RTXes referenced by the debugging information, which therefore
135 must be kept around forever. */
136 static GTY(()) VEC(rtx,gc) *used_rtx_array;
138 /* A pointer to the base of a list of incomplete types which might be
139 completed at some later time. incomplete_types_list needs to be a
140 VEC(tree,gc) because we want to tell the garbage collector about
141 it. */
142 static GTY(()) VEC(tree,gc) *incomplete_types;
144 /* A pointer to the base of a table of references to declaration
145 scopes. This table is a display which tracks the nesting
146 of declaration scopes at the current scope and containing
147 scopes. This table is used to find the proper place to
148 define type declaration DIE's. */
149 static GTY(()) VEC(tree,gc) *decl_scope_table;
151 /* Pointers to various DWARF2 sections. */
152 static GTY(()) section *debug_info_section;
153 static GTY(()) section *debug_abbrev_section;
154 static GTY(()) section *debug_aranges_section;
155 static GTY(()) section *debug_macinfo_section;
156 static GTY(()) section *debug_line_section;
157 static GTY(()) section *debug_loc_section;
158 static GTY(()) section *debug_pubnames_section;
159 static GTY(()) section *debug_str_section;
160 static GTY(()) section *debug_ranges_section;
161 static GTY(()) section *debug_frame_section;
163 /* How to start an assembler comment. */
164 #ifndef ASM_COMMENT_START
165 #define ASM_COMMENT_START ";#"
166 #endif
168 typedef struct dw_cfi_struct *dw_cfi_ref;
169 typedef struct dw_fde_struct *dw_fde_ref;
170 typedef union dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
172 /* Call frames are described using a sequence of Call Frame
173 Information instructions. The register number, offset
174 and address fields are provided as possible operands;
175 their use is selected by the opcode field. */
177 enum dw_cfi_oprnd_type {
178 dw_cfi_oprnd_unused,
179 dw_cfi_oprnd_reg_num,
180 dw_cfi_oprnd_offset,
181 dw_cfi_oprnd_addr,
182 dw_cfi_oprnd_loc
185 typedef union dw_cfi_oprnd_struct GTY(())
187 unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
188 HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
189 const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
190 struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
192 dw_cfi_oprnd;
194 typedef struct dw_cfi_struct GTY(())
196 dw_cfi_ref dw_cfi_next;
197 enum dwarf_call_frame_info dw_cfi_opc;
198 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
199 dw_cfi_oprnd1;
200 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
201 dw_cfi_oprnd2;
203 dw_cfi_node;
205 /* This is how we define the location of the CFA. We use to handle it
206 as REG + OFFSET all the time, but now it can be more complex.
207 It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
208 Instead of passing around REG and OFFSET, we pass a copy
209 of this structure. */
210 typedef struct cfa_loc GTY(())
212 HOST_WIDE_INT offset;
213 HOST_WIDE_INT base_offset;
214 unsigned int reg;
215 int indirect; /* 1 if CFA is accessed via a dereference. */
216 } dw_cfa_location;
218 /* All call frame descriptions (FDE's) in the GCC generated DWARF
219 refer to a single Common Information Entry (CIE), defined at
220 the beginning of the .debug_frame section. This use of a single
221 CIE obviates the need to keep track of multiple CIE's
222 in the DWARF generation routines below. */
224 typedef struct dw_fde_struct GTY(())
226 tree decl;
227 const char *dw_fde_begin;
228 const char *dw_fde_current_label;
229 const char *dw_fde_end;
230 const char *dw_fde_hot_section_label;
231 const char *dw_fde_hot_section_end_label;
232 const char *dw_fde_unlikely_section_label;
233 const char *dw_fde_unlikely_section_end_label;
234 bool dw_fde_switched_sections;
235 dw_cfi_ref dw_fde_cfi;
236 unsigned funcdef_number;
237 unsigned all_throwers_are_sibcalls : 1;
238 unsigned nothrow : 1;
239 unsigned uses_eh_lsda : 1;
241 dw_fde_node;
243 /* Maximum size (in bytes) of an artificially generated label. */
244 #define MAX_ARTIFICIAL_LABEL_BYTES 30
246 /* The size of addresses as they appear in the Dwarf 2 data.
247 Some architectures use word addresses to refer to code locations,
248 but Dwarf 2 info always uses byte addresses. On such machines,
249 Dwarf 2 addresses need to be larger than the architecture's
250 pointers. */
251 #ifndef DWARF2_ADDR_SIZE
252 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
253 #endif
255 /* The size in bytes of a DWARF field indicating an offset or length
256 relative to a debug info section, specified to be 4 bytes in the
257 DWARF-2 specification. The SGI/MIPS ABI defines it to be the same
258 as PTR_SIZE. */
260 #ifndef DWARF_OFFSET_SIZE
261 #define DWARF_OFFSET_SIZE 4
262 #endif
264 /* According to the (draft) DWARF 3 specification, the initial length
265 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
266 bytes are 0xffffffff, followed by the length stored in the next 8
267 bytes.
269 However, the SGI/MIPS ABI uses an initial length which is equal to
270 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
272 #ifndef DWARF_INITIAL_LENGTH_SIZE
273 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
274 #endif
276 #define DWARF_VERSION 2
278 /* Round SIZE up to the nearest BOUNDARY. */
279 #define DWARF_ROUND(SIZE,BOUNDARY) \
280 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
282 /* Offsets recorded in opcodes are a multiple of this alignment factor. */
283 #ifndef DWARF_CIE_DATA_ALIGNMENT
284 #ifdef STACK_GROWS_DOWNWARD
285 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
286 #else
287 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
288 #endif
289 #endif
291 /* CIE identifier. */
292 #if HOST_BITS_PER_WIDE_INT >= 64
293 #define DWARF_CIE_ID \
294 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
295 #else
296 #define DWARF_CIE_ID DW_CIE_ID
297 #endif
299 /* A pointer to the base of a table that contains frame description
300 information for each routine. */
301 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
303 /* Number of elements currently allocated for fde_table. */
304 static GTY(()) unsigned fde_table_allocated;
306 /* Number of elements in fde_table currently in use. */
307 static GTY(()) unsigned fde_table_in_use;
309 /* Size (in elements) of increments by which we may expand the
310 fde_table. */
311 #define FDE_TABLE_INCREMENT 256
313 /* A list of call frame insns for the CIE. */
314 static GTY(()) dw_cfi_ref cie_cfi_head;
316 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
317 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
318 attribute that accelerates the lookup of the FDE associated
319 with the subprogram. This variable holds the table index of the FDE
320 associated with the current function (body) definition. */
321 static unsigned current_funcdef_fde;
322 #endif
324 struct indirect_string_node GTY(())
326 const char *str;
327 unsigned int refcount;
328 unsigned int form;
329 char *label;
332 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
334 static GTY(()) int dw2_string_counter;
335 static GTY(()) unsigned long dwarf2out_cfi_label_num;
337 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
339 /* Forward declarations for functions defined in this file. */
341 static char *stripattributes (const char *);
342 static const char *dwarf_cfi_name (unsigned);
343 static dw_cfi_ref new_cfi (void);
344 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
345 static void add_fde_cfi (const char *, dw_cfi_ref);
346 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *);
347 static void lookup_cfa (dw_cfa_location *);
348 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
349 static void initial_return_save (rtx);
350 static HOST_WIDE_INT stack_adjust_offset (rtx);
351 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
352 static void output_call_frame_info (int);
353 static void dwarf2out_stack_adjust (rtx, bool);
354 static void flush_queued_reg_saves (void);
355 static bool clobbers_queued_reg_save (rtx);
356 static void dwarf2out_frame_debug_expr (rtx, const char *);
358 /* Support for complex CFA locations. */
359 static void output_cfa_loc (dw_cfi_ref);
360 static void get_cfa_from_loc_descr (dw_cfa_location *,
361 struct dw_loc_descr_struct *);
362 static struct dw_loc_descr_struct *build_cfa_loc
363 (dw_cfa_location *, HOST_WIDE_INT);
364 static void def_cfa_1 (const char *, dw_cfa_location *);
366 /* How to start an assembler comment. */
367 #ifndef ASM_COMMENT_START
368 #define ASM_COMMENT_START ";#"
369 #endif
371 /* Data and reference forms for relocatable data. */
372 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
373 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
375 #ifndef DEBUG_FRAME_SECTION
376 #define DEBUG_FRAME_SECTION ".debug_frame"
377 #endif
379 #ifndef FUNC_BEGIN_LABEL
380 #define FUNC_BEGIN_LABEL "LFB"
381 #endif
383 #ifndef FUNC_END_LABEL
384 #define FUNC_END_LABEL "LFE"
385 #endif
387 #ifndef FRAME_BEGIN_LABEL
388 #define FRAME_BEGIN_LABEL "Lframe"
389 #endif
390 #define CIE_AFTER_SIZE_LABEL "LSCIE"
391 #define CIE_END_LABEL "LECIE"
392 #define FDE_LABEL "LSFDE"
393 #define FDE_AFTER_SIZE_LABEL "LASFDE"
394 #define FDE_END_LABEL "LEFDE"
395 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
396 #define LINE_NUMBER_END_LABEL "LELT"
397 #define LN_PROLOG_AS_LABEL "LASLTP"
398 #define LN_PROLOG_END_LABEL "LELTP"
399 #define DIE_LABEL_PREFIX "DW"
401 /* The DWARF 2 CFA column which tracks the return address. Normally this
402 is the column for PC, or the first column after all of the hard
403 registers. */
404 #ifndef DWARF_FRAME_RETURN_COLUMN
405 #ifdef PC_REGNUM
406 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGNUM (PC_REGNUM)
407 #else
408 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGISTERS
409 #endif
410 #endif
412 /* The mapping from gcc register number to DWARF 2 CFA column number. By
413 default, we just provide columns for all registers. */
414 #ifndef DWARF_FRAME_REGNUM
415 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
416 #endif
418 /* Hook used by __throw. */
421 expand_builtin_dwarf_sp_column (void)
423 unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
424 return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
427 /* Return a pointer to a copy of the section string name S with all
428 attributes stripped off, and an asterisk prepended (for assemble_name). */
430 static inline char *
431 stripattributes (const char *s)
433 char *stripped = XNEWVEC (char, strlen (s) + 2);
434 char *p = stripped;
436 *p++ = '*';
438 while (*s && *s != ',')
439 *p++ = *s++;
441 *p = '\0';
442 return stripped;
445 /* Generate code to initialize the register size table. */
447 void
448 expand_builtin_init_dwarf_reg_sizes (tree address)
450 unsigned int i;
451 enum machine_mode mode = TYPE_MODE (char_type_node);
452 rtx addr = expand_normal (address);
453 rtx mem = gen_rtx_MEM (BLKmode, addr);
454 bool wrote_return_column = false;
456 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
458 int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
460 if (rnum < DWARF_FRAME_REGISTERS)
462 HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
463 enum machine_mode save_mode = reg_raw_mode[i];
464 HOST_WIDE_INT size;
466 if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
467 save_mode = choose_hard_reg_mode (i, 1, true);
468 if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
470 if (save_mode == VOIDmode)
471 continue;
472 wrote_return_column = true;
474 size = GET_MODE_SIZE (save_mode);
475 if (offset < 0)
476 continue;
478 emit_move_insn (adjust_address (mem, mode, offset),
479 gen_int_mode (size, mode));
483 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
484 gcc_assert (wrote_return_column);
485 i = DWARF_ALT_FRAME_RETURN_COLUMN;
486 wrote_return_column = false;
487 #else
488 i = DWARF_FRAME_RETURN_COLUMN;
489 #endif
491 if (! wrote_return_column)
493 enum machine_mode save_mode = Pmode;
494 HOST_WIDE_INT offset = i * GET_MODE_SIZE (mode);
495 HOST_WIDE_INT size = GET_MODE_SIZE (save_mode);
496 emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
500 /* Convert a DWARF call frame info. operation to its string name */
502 static const char *
503 dwarf_cfi_name (unsigned int cfi_opc)
505 switch (cfi_opc)
507 case DW_CFA_advance_loc:
508 return "DW_CFA_advance_loc";
509 case DW_CFA_offset:
510 return "DW_CFA_offset";
511 case DW_CFA_restore:
512 return "DW_CFA_restore";
513 case DW_CFA_nop:
514 return "DW_CFA_nop";
515 case DW_CFA_set_loc:
516 return "DW_CFA_set_loc";
517 case DW_CFA_advance_loc1:
518 return "DW_CFA_advance_loc1";
519 case DW_CFA_advance_loc2:
520 return "DW_CFA_advance_loc2";
521 case DW_CFA_advance_loc4:
522 return "DW_CFA_advance_loc4";
523 case DW_CFA_offset_extended:
524 return "DW_CFA_offset_extended";
525 case DW_CFA_restore_extended:
526 return "DW_CFA_restore_extended";
527 case DW_CFA_undefined:
528 return "DW_CFA_undefined";
529 case DW_CFA_same_value:
530 return "DW_CFA_same_value";
531 case DW_CFA_register:
532 return "DW_CFA_register";
533 case DW_CFA_remember_state:
534 return "DW_CFA_remember_state";
535 case DW_CFA_restore_state:
536 return "DW_CFA_restore_state";
537 case DW_CFA_def_cfa:
538 return "DW_CFA_def_cfa";
539 case DW_CFA_def_cfa_register:
540 return "DW_CFA_def_cfa_register";
541 case DW_CFA_def_cfa_offset:
542 return "DW_CFA_def_cfa_offset";
544 /* DWARF 3 */
545 case DW_CFA_def_cfa_expression:
546 return "DW_CFA_def_cfa_expression";
547 case DW_CFA_expression:
548 return "DW_CFA_expression";
549 case DW_CFA_offset_extended_sf:
550 return "DW_CFA_offset_extended_sf";
551 case DW_CFA_def_cfa_sf:
552 return "DW_CFA_def_cfa_sf";
553 case DW_CFA_def_cfa_offset_sf:
554 return "DW_CFA_def_cfa_offset_sf";
556 /* SGI/MIPS specific */
557 case DW_CFA_MIPS_advance_loc8:
558 return "DW_CFA_MIPS_advance_loc8";
560 /* GNU extensions */
561 case DW_CFA_GNU_window_save:
562 return "DW_CFA_GNU_window_save";
563 case DW_CFA_GNU_args_size:
564 return "DW_CFA_GNU_args_size";
565 case DW_CFA_GNU_negative_offset_extended:
566 return "DW_CFA_GNU_negative_offset_extended";
568 default:
569 return "DW_CFA_<unknown>";
573 /* Return a pointer to a newly allocated Call Frame Instruction. */
575 static inline dw_cfi_ref
576 new_cfi (void)
578 dw_cfi_ref cfi = ggc_alloc (sizeof (dw_cfi_node));
580 cfi->dw_cfi_next = NULL;
581 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
582 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
584 return cfi;
587 /* Add a Call Frame Instruction to list of instructions. */
589 static inline void
590 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
592 dw_cfi_ref *p;
594 /* Find the end of the chain. */
595 for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
598 *p = cfi;
601 /* Generate a new label for the CFI info to refer to. */
603 char *
604 dwarf2out_cfi_label (void)
606 static char label[20];
608 ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
609 ASM_OUTPUT_LABEL (asm_out_file, label);
610 return label;
613 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
614 or to the CIE if LABEL is NULL. */
616 static void
617 add_fde_cfi (const char *label, dw_cfi_ref cfi)
619 if (label)
621 dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
623 if (*label == 0)
624 label = dwarf2out_cfi_label ();
626 if (fde->dw_fde_current_label == NULL
627 || strcmp (label, fde->dw_fde_current_label) != 0)
629 dw_cfi_ref xcfi;
631 fde->dw_fde_current_label = label = xstrdup (label);
633 /* Set the location counter to the new label. */
634 xcfi = new_cfi ();
635 xcfi->dw_cfi_opc = DW_CFA_advance_loc4;
636 xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
637 add_cfi (&fde->dw_fde_cfi, xcfi);
640 add_cfi (&fde->dw_fde_cfi, cfi);
643 else
644 add_cfi (&cie_cfi_head, cfi);
647 /* Subroutine of lookup_cfa. */
649 static void
650 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc)
652 switch (cfi->dw_cfi_opc)
654 case DW_CFA_def_cfa_offset:
655 loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
656 break;
657 case DW_CFA_def_cfa_offset_sf:
658 loc->offset
659 = cfi->dw_cfi_oprnd1.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
660 break;
661 case DW_CFA_def_cfa_register:
662 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
663 break;
664 case DW_CFA_def_cfa:
665 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
666 loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
667 break;
668 case DW_CFA_def_cfa_sf:
669 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
670 loc->offset
671 = cfi->dw_cfi_oprnd2.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
672 break;
673 case DW_CFA_def_cfa_expression:
674 get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
675 break;
676 default:
677 break;
681 /* Find the previous value for the CFA. */
683 static void
684 lookup_cfa (dw_cfa_location *loc)
686 dw_cfi_ref cfi;
688 loc->reg = INVALID_REGNUM;
689 loc->offset = 0;
690 loc->indirect = 0;
691 loc->base_offset = 0;
693 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
694 lookup_cfa_1 (cfi, loc);
696 if (fde_table_in_use)
698 dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
699 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
700 lookup_cfa_1 (cfi, loc);
704 /* The current rule for calculating the DWARF2 canonical frame address. */
705 static dw_cfa_location cfa;
707 /* The register used for saving registers to the stack, and its offset
708 from the CFA. */
709 static dw_cfa_location cfa_store;
711 /* The running total of the size of arguments pushed onto the stack. */
712 static HOST_WIDE_INT args_size;
714 /* The last args_size we actually output. */
715 static HOST_WIDE_INT old_args_size;
717 /* Entry point to update the canonical frame address (CFA).
718 LABEL is passed to add_fde_cfi. The value of CFA is now to be
719 calculated from REG+OFFSET. */
721 void
722 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
724 dw_cfa_location loc;
725 loc.indirect = 0;
726 loc.base_offset = 0;
727 loc.reg = reg;
728 loc.offset = offset;
729 def_cfa_1 (label, &loc);
732 /* Determine if two dw_cfa_location structures define the same data. */
734 static bool
735 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
737 return (loc1->reg == loc2->reg
738 && loc1->offset == loc2->offset
739 && loc1->indirect == loc2->indirect
740 && (loc1->indirect == 0
741 || loc1->base_offset == loc2->base_offset));
744 /* This routine does the actual work. The CFA is now calculated from
745 the dw_cfa_location structure. */
747 static void
748 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
750 dw_cfi_ref cfi;
751 dw_cfa_location old_cfa, loc;
753 cfa = *loc_p;
754 loc = *loc_p;
756 if (cfa_store.reg == loc.reg && loc.indirect == 0)
757 cfa_store.offset = loc.offset;
759 loc.reg = DWARF_FRAME_REGNUM (loc.reg);
760 lookup_cfa (&old_cfa);
762 /* If nothing changed, no need to issue any call frame instructions. */
763 if (cfa_equal_p (&loc, &old_cfa))
764 return;
766 cfi = new_cfi ();
768 if (loc.reg == old_cfa.reg && !loc.indirect)
770 /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
771 the CFA register did not change but the offset did. */
772 if (loc.offset < 0)
774 HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
775 gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
777 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
778 cfi->dw_cfi_oprnd1.dw_cfi_offset = f_offset;
780 else
782 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
783 cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
787 #ifndef MIPS_DEBUGGING_INFO /* SGI dbx thinks this means no offset. */
788 else if (loc.offset == old_cfa.offset
789 && old_cfa.reg != INVALID_REGNUM
790 && !loc.indirect)
792 /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
793 indicating the CFA register has changed to <register> but the
794 offset has not changed. */
795 cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
796 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
798 #endif
800 else if (loc.indirect == 0)
802 /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
803 indicating the CFA register has changed to <register> with
804 the specified offset. */
805 if (loc.offset < 0)
807 HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
808 gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
810 cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
811 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
812 cfi->dw_cfi_oprnd2.dw_cfi_offset = f_offset;
814 else
816 cfi->dw_cfi_opc = DW_CFA_def_cfa;
817 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
818 cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
821 else
823 /* Construct a DW_CFA_def_cfa_expression instruction to
824 calculate the CFA using a full location expression since no
825 register-offset pair is available. */
826 struct dw_loc_descr_struct *loc_list;
828 cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
829 loc_list = build_cfa_loc (&loc, 0);
830 cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
833 add_fde_cfi (label, cfi);
836 /* Add the CFI for saving a register. REG is the CFA column number.
837 LABEL is passed to add_fde_cfi.
838 If SREG is -1, the register is saved at OFFSET from the CFA;
839 otherwise it is saved in SREG. */
841 static void
842 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
844 dw_cfi_ref cfi = new_cfi ();
846 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
848 if (sreg == INVALID_REGNUM)
850 if (reg & ~0x3f)
851 /* The register number won't fit in 6 bits, so we have to use
852 the long form. */
853 cfi->dw_cfi_opc = DW_CFA_offset_extended;
854 else
855 cfi->dw_cfi_opc = DW_CFA_offset;
857 #ifdef ENABLE_CHECKING
859 /* If we get an offset that is not a multiple of
860 DWARF_CIE_DATA_ALIGNMENT, there is either a bug in the
861 definition of DWARF_CIE_DATA_ALIGNMENT, or a bug in the machine
862 description. */
863 HOST_WIDE_INT check_offset = offset / DWARF_CIE_DATA_ALIGNMENT;
865 gcc_assert (check_offset * DWARF_CIE_DATA_ALIGNMENT == offset);
867 #endif
868 offset /= DWARF_CIE_DATA_ALIGNMENT;
869 if (offset < 0)
870 cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
872 cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
874 else if (sreg == reg)
875 cfi->dw_cfi_opc = DW_CFA_same_value;
876 else
878 cfi->dw_cfi_opc = DW_CFA_register;
879 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
882 add_fde_cfi (label, cfi);
885 /* Add the CFI for saving a register window. LABEL is passed to reg_save.
886 This CFI tells the unwinder that it needs to restore the window registers
887 from the previous frame's window save area.
889 ??? Perhaps we should note in the CIE where windows are saved (instead of
890 assuming 0(cfa)) and what registers are in the window. */
892 void
893 dwarf2out_window_save (const char *label)
895 dw_cfi_ref cfi = new_cfi ();
897 cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
898 add_fde_cfi (label, cfi);
901 /* Add a CFI to update the running total of the size of arguments
902 pushed onto the stack. */
904 void
905 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
907 dw_cfi_ref cfi;
909 if (size == old_args_size)
910 return;
912 old_args_size = size;
914 cfi = new_cfi ();
915 cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
916 cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
917 add_fde_cfi (label, cfi);
920 /* Entry point for saving a register to the stack. REG is the GCC register
921 number. LABEL and OFFSET are passed to reg_save. */
923 void
924 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
926 reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
929 /* Entry point for saving the return address in the stack.
930 LABEL and OFFSET are passed to reg_save. */
932 void
933 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
935 reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
938 /* Entry point for saving the return address in a register.
939 LABEL and SREG are passed to reg_save. */
941 void
942 dwarf2out_return_reg (const char *label, unsigned int sreg)
944 reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
947 /* Record the initial position of the return address. RTL is
948 INCOMING_RETURN_ADDR_RTX. */
950 static void
951 initial_return_save (rtx rtl)
953 unsigned int reg = INVALID_REGNUM;
954 HOST_WIDE_INT offset = 0;
956 switch (GET_CODE (rtl))
958 case REG:
959 /* RA is in a register. */
960 reg = DWARF_FRAME_REGNUM (REGNO (rtl));
961 break;
963 case MEM:
964 /* RA is on the stack. */
965 rtl = XEXP (rtl, 0);
966 switch (GET_CODE (rtl))
968 case REG:
969 gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
970 offset = 0;
971 break;
973 case PLUS:
974 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
975 offset = INTVAL (XEXP (rtl, 1));
976 break;
978 case MINUS:
979 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
980 offset = -INTVAL (XEXP (rtl, 1));
981 break;
983 default:
984 gcc_unreachable ();
987 break;
989 case PLUS:
990 /* The return address is at some offset from any value we can
991 actually load. For instance, on the SPARC it is in %i7+8. Just
992 ignore the offset for now; it doesn't matter for unwinding frames. */
993 gcc_assert (GET_CODE (XEXP (rtl, 1)) == CONST_INT);
994 initial_return_save (XEXP (rtl, 0));
995 return;
997 default:
998 gcc_unreachable ();
1001 if (reg != DWARF_FRAME_RETURN_COLUMN)
1002 reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1005 /* Given a SET, calculate the amount of stack adjustment it
1006 contains. */
1008 static HOST_WIDE_INT
1009 stack_adjust_offset (rtx pattern)
1011 rtx src = SET_SRC (pattern);
1012 rtx dest = SET_DEST (pattern);
1013 HOST_WIDE_INT offset = 0;
1014 enum rtx_code code;
1016 if (dest == stack_pointer_rtx)
1018 /* (set (reg sp) (plus (reg sp) (const_int))) */
1019 code = GET_CODE (src);
1020 if (! (code == PLUS || code == MINUS)
1021 || XEXP (src, 0) != stack_pointer_rtx
1022 || GET_CODE (XEXP (src, 1)) != CONST_INT)
1023 return 0;
1025 offset = INTVAL (XEXP (src, 1));
1026 if (code == PLUS)
1027 offset = -offset;
1029 else if (MEM_P (dest))
1031 /* (set (mem (pre_dec (reg sp))) (foo)) */
1032 src = XEXP (dest, 0);
1033 code = GET_CODE (src);
1035 switch (code)
1037 case PRE_MODIFY:
1038 case POST_MODIFY:
1039 if (XEXP (src, 0) == stack_pointer_rtx)
1041 rtx val = XEXP (XEXP (src, 1), 1);
1042 /* We handle only adjustments by constant amount. */
1043 gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1044 && GET_CODE (val) == CONST_INT);
1045 offset = -INTVAL (val);
1046 break;
1048 return 0;
1050 case PRE_DEC:
1051 case POST_DEC:
1052 if (XEXP (src, 0) == stack_pointer_rtx)
1054 offset = GET_MODE_SIZE (GET_MODE (dest));
1055 break;
1057 return 0;
1059 case PRE_INC:
1060 case POST_INC:
1061 if (XEXP (src, 0) == stack_pointer_rtx)
1063 offset = -GET_MODE_SIZE (GET_MODE (dest));
1064 break;
1066 return 0;
1068 default:
1069 return 0;
1072 else
1073 return 0;
1075 return offset;
1078 /* Check INSN to see if it looks like a push or a stack adjustment, and
1079 make a note of it if it does. EH uses this information to find out how
1080 much extra space it needs to pop off the stack. */
1082 static void
1083 dwarf2out_stack_adjust (rtx insn, bool after_p ATTRIBUTE_UNUSED)
1085 HOST_WIDE_INT offset;
1086 const char *label;
1087 int i;
1089 /* Don't handle epilogues at all. Certainly it would be wrong to do so
1090 with this function. Proper support would require all frame-related
1091 insns to be marked, and to be able to handle saving state around
1092 epilogues textually in the middle of the function. */
1093 if (prologue_epilogue_contains (insn) || sibcall_epilogue_contains (insn))
1094 return;
1096 if (BARRIER_P (insn))
1098 /* When we see a BARRIER, we know to reset args_size to 0. Usually
1099 the compiler will have already emitted a stack adjustment, but
1100 doesn't bother for calls to noreturn functions. */
1101 #ifdef STACK_GROWS_DOWNWARD
1102 offset = -args_size;
1103 #else
1104 offset = args_size;
1105 #endif
1107 else if (GET_CODE (PATTERN (insn)) == SET)
1108 offset = stack_adjust_offset (PATTERN (insn));
1109 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1110 || GET_CODE (PATTERN (insn)) == SEQUENCE)
1112 /* There may be stack adjustments inside compound insns. Search
1113 for them. */
1114 for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1115 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1116 offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i));
1118 else if (GET_CODE (insn) == CALL_INSN)
1119 offset = 0;
1120 else
1121 return;
1123 /* We handle this separately because we want stack adjustments in a
1124 CALL_INSN to be handled. */;
1125 if (GET_CODE (insn) == CALL_INSN)
1127 /* If only calls can throw, adjust args_size only at call sites. */
1128 if (!flag_asynchronous_unwind_tables)
1129 dwarf2out_args_size ("", args_size);
1132 if (offset == 0)
1133 return;
1135 if (cfa.reg == STACK_POINTER_REGNUM)
1136 cfa.offset += offset;
1138 #ifndef STACK_GROWS_DOWNWARD
1139 offset = -offset;
1140 #endif
1142 args_size += offset;
1143 if (args_size < 0)
1144 args_size = 0;
1146 /* If only calls can throw and we have a frame pointer, we'll save
1147 up adjustments until we see the CALL_INSN. We used to return
1148 early and derive args_size from NARGS in the CALL_INSN itself,
1149 but that doesn't compute the right value if we have nested call
1150 expansions, e.g., stack adjustments for a call have already been
1151 emitted, and then we issue another call to compute an argument
1152 for the enclosing call (i.e., bar (foo ())). */
1153 if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1154 return;
1156 label = dwarf2out_cfi_label ();
1157 def_cfa_1 (label, &cfa);
1158 if (flag_asynchronous_unwind_tables)
1159 dwarf2out_args_size (label, args_size);
1162 #endif
1164 /* We delay emitting a register save until either (a) we reach the end
1165 of the prologue or (b) the register is clobbered. This clusters
1166 register saves so that there are fewer pc advances. */
1168 struct queued_reg_save GTY(())
1170 struct queued_reg_save *next;
1171 rtx reg;
1172 HOST_WIDE_INT cfa_offset;
1173 rtx saved_reg;
1176 static GTY(()) struct queued_reg_save *queued_reg_saves;
1178 /* The caller's ORIG_REG is saved in SAVED_IN_REG. */
1179 struct reg_saved_in_data GTY(()) {
1180 rtx orig_reg;
1181 rtx saved_in_reg;
1184 /* A list of registers saved in other registers.
1185 The list intentionally has a small maximum capacity of 4; if your
1186 port needs more than that, you might consider implementing a
1187 more efficient data structure. */
1188 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1189 static GTY(()) size_t num_regs_saved_in_regs;
1191 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1192 static const char *last_reg_save_label;
1194 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1195 SREG, or if SREG is NULL then it is saved at OFFSET to the CFA. */
1197 static void
1198 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1200 struct queued_reg_save *q;
1202 /* Duplicates waste space, but it's also necessary to remove them
1203 for correctness, since the queue gets output in reverse
1204 order. */
1205 for (q = queued_reg_saves; q != NULL; q = q->next)
1206 if (REGNO (q->reg) == REGNO (reg))
1207 break;
1209 if (q == NULL)
1211 q = ggc_alloc (sizeof (*q));
1212 q->next = queued_reg_saves;
1213 queued_reg_saves = q;
1216 q->reg = reg;
1217 q->cfa_offset = offset;
1218 q->saved_reg = sreg;
1220 last_reg_save_label = label;
1223 /* Output all the entries in QUEUED_REG_SAVES. */
1225 static void
1226 flush_queued_reg_saves (void)
1228 struct queued_reg_save *q;
1230 for (q = queued_reg_saves; q; q = q->next)
1232 size_t i;
1233 unsigned int reg, sreg;
1235 for (i = 0; i < num_regs_saved_in_regs; i++)
1236 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1237 break;
1238 if (q->saved_reg && i == num_regs_saved_in_regs)
1240 gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1241 num_regs_saved_in_regs++;
1243 if (i != num_regs_saved_in_regs)
1245 regs_saved_in_regs[i].orig_reg = q->reg;
1246 regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1249 reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1250 if (q->saved_reg)
1251 sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1252 else
1253 sreg = INVALID_REGNUM;
1254 reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1257 queued_reg_saves = NULL;
1258 last_reg_save_label = NULL;
1261 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1262 location for? Or, does it clobber a register which we've previously
1263 said that some other register is saved in, and for which we now
1264 have a new location for? */
1266 static bool
1267 clobbers_queued_reg_save (rtx insn)
1269 struct queued_reg_save *q;
1271 for (q = queued_reg_saves; q; q = q->next)
1273 size_t i;
1274 if (modified_in_p (q->reg, insn))
1275 return true;
1276 for (i = 0; i < num_regs_saved_in_regs; i++)
1277 if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1278 && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1279 return true;
1282 return false;
1285 /* What register, if any, is currently saved in REG? */
1287 static rtx
1288 reg_saved_in (rtx reg)
1290 unsigned int regn = REGNO (reg);
1291 size_t i;
1292 struct queued_reg_save *q;
1294 for (q = queued_reg_saves; q; q = q->next)
1295 if (q->saved_reg && regn == REGNO (q->saved_reg))
1296 return q->reg;
1298 for (i = 0; i < num_regs_saved_in_regs; i++)
1299 if (regs_saved_in_regs[i].saved_in_reg
1300 && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1301 return regs_saved_in_regs[i].orig_reg;
1303 return NULL_RTX;
1307 /* A temporary register holding an integral value used in adjusting SP
1308 or setting up the store_reg. The "offset" field holds the integer
1309 value, not an offset. */
1310 static dw_cfa_location cfa_temp;
1312 /* Record call frame debugging information for an expression EXPR,
1313 which either sets SP or FP (adjusting how we calculate the frame
1314 address) or saves a register to the stack or another register.
1315 LABEL indicates the address of EXPR.
1317 This function encodes a state machine mapping rtxes to actions on
1318 cfa, cfa_store, and cfa_temp.reg. We describe these rules so
1319 users need not read the source code.
1321 The High-Level Picture
1323 Changes in the register we use to calculate the CFA: Currently we
1324 assume that if you copy the CFA register into another register, we
1325 should take the other one as the new CFA register; this seems to
1326 work pretty well. If it's wrong for some target, it's simple
1327 enough not to set RTX_FRAME_RELATED_P on the insn in question.
1329 Changes in the register we use for saving registers to the stack:
1330 This is usually SP, but not always. Again, we deduce that if you
1331 copy SP into another register (and SP is not the CFA register),
1332 then the new register is the one we will be using for register
1333 saves. This also seems to work.
1335 Register saves: There's not much guesswork about this one; if
1336 RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1337 register save, and the register used to calculate the destination
1338 had better be the one we think we're using for this purpose.
1339 It's also assumed that a copy from a call-saved register to another
1340 register is saving that register if RTX_FRAME_RELATED_P is set on
1341 that instruction. If the copy is from a call-saved register to
1342 the *same* register, that means that the register is now the same
1343 value as in the caller.
1345 Except: If the register being saved is the CFA register, and the
1346 offset is nonzero, we are saving the CFA, so we assume we have to
1347 use DW_CFA_def_cfa_expression. If the offset is 0, we assume that
1348 the intent is to save the value of SP from the previous frame.
1350 In addition, if a register has previously been saved to a different
1351 register,
1353 Invariants / Summaries of Rules
1355 cfa current rule for calculating the CFA. It usually
1356 consists of a register and an offset.
1357 cfa_store register used by prologue code to save things to the stack
1358 cfa_store.offset is the offset from the value of
1359 cfa_store.reg to the actual CFA
1360 cfa_temp register holding an integral value. cfa_temp.offset
1361 stores the value, which will be used to adjust the
1362 stack pointer. cfa_temp is also used like cfa_store,
1363 to track stores to the stack via fp or a temp reg.
1365 Rules 1- 4: Setting a register's value to cfa.reg or an expression
1366 with cfa.reg as the first operand changes the cfa.reg and its
1367 cfa.offset. Rule 1 and 4 also set cfa_temp.reg and
1368 cfa_temp.offset.
1370 Rules 6- 9: Set a non-cfa.reg register value to a constant or an
1371 expression yielding a constant. This sets cfa_temp.reg
1372 and cfa_temp.offset.
1374 Rule 5: Create a new register cfa_store used to save items to the
1375 stack.
1377 Rules 10-14: Save a register to the stack. Define offset as the
1378 difference of the original location and cfa_store's
1379 location (or cfa_temp's location if cfa_temp is used).
1381 The Rules
1383 "{a,b}" indicates a choice of a xor b.
1384 "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1386 Rule 1:
1387 (set <reg1> <reg2>:cfa.reg)
1388 effects: cfa.reg = <reg1>
1389 cfa.offset unchanged
1390 cfa_temp.reg = <reg1>
1391 cfa_temp.offset = cfa.offset
1393 Rule 2:
1394 (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1395 {<const_int>,<reg>:cfa_temp.reg}))
1396 effects: cfa.reg = sp if fp used
1397 cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
1398 cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1399 if cfa_store.reg==sp
1401 Rule 3:
1402 (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
1403 effects: cfa.reg = fp
1404 cfa_offset += +/- <const_int>
1406 Rule 4:
1407 (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
1408 constraints: <reg1> != fp
1409 <reg1> != sp
1410 effects: cfa.reg = <reg1>
1411 cfa_temp.reg = <reg1>
1412 cfa_temp.offset = cfa.offset
1414 Rule 5:
1415 (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1416 constraints: <reg1> != fp
1417 <reg1> != sp
1418 effects: cfa_store.reg = <reg1>
1419 cfa_store.offset = cfa.offset - cfa_temp.offset
1421 Rule 6:
1422 (set <reg> <const_int>)
1423 effects: cfa_temp.reg = <reg>
1424 cfa_temp.offset = <const_int>
1426 Rule 7:
1427 (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1428 effects: cfa_temp.reg = <reg1>
1429 cfa_temp.offset |= <const_int>
1431 Rule 8:
1432 (set <reg> (high <exp>))
1433 effects: none
1435 Rule 9:
1436 (set <reg> (lo_sum <exp> <const_int>))
1437 effects: cfa_temp.reg = <reg>
1438 cfa_temp.offset = <const_int>
1440 Rule 10:
1441 (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1442 effects: cfa_store.offset -= <const_int>
1443 cfa.offset = cfa_store.offset if cfa.reg == sp
1444 cfa.reg = sp
1445 cfa.base_offset = -cfa_store.offset
1447 Rule 11:
1448 (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1449 effects: cfa_store.offset += -/+ mode_size(mem)
1450 cfa.offset = cfa_store.offset if cfa.reg == sp
1451 cfa.reg = sp
1452 cfa.base_offset = -cfa_store.offset
1454 Rule 12:
1455 (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1457 <reg2>)
1458 effects: cfa.reg = <reg1>
1459 cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
1461 Rule 13:
1462 (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1463 effects: cfa.reg = <reg1>
1464 cfa.base_offset = -{cfa_store,cfa_temp}.offset
1466 Rule 14:
1467 (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
1468 effects: cfa.reg = <reg1>
1469 cfa.base_offset = -cfa_temp.offset
1470 cfa_temp.offset -= mode_size(mem)
1472   Rule 15:
1473   (set <reg> {unspec, unspec_volatile})
1474   effects: target-dependent */
1476 static void
1477 dwarf2out_frame_debug_expr (rtx expr, const char *label)
1479 rtx src, dest;
1480 HOST_WIDE_INT offset;
1482 /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1483 the PARALLEL independently. The first element is always processed if
1484 it is a SET. This is for backward compatibility. Other elements
1485 are processed only if they are SETs and the RTX_FRAME_RELATED_P
1486 flag is set in them. */
1487 if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
1489 int par_index;
1490 int limit = XVECLEN (expr, 0);
1492 for (par_index = 0; par_index < limit; par_index++)
1493 if (GET_CODE (XVECEXP (expr, 0, par_index)) == SET
1494 && (RTX_FRAME_RELATED_P (XVECEXP (expr, 0, par_index))
1495 || par_index == 0))
1496 dwarf2out_frame_debug_expr (XVECEXP (expr, 0, par_index), label);
1498 return;
1501 gcc_assert (GET_CODE (expr) == SET);
1503 src = SET_SRC (expr);
1504 dest = SET_DEST (expr);
1506 if (REG_P (src))
1508 rtx rsi = reg_saved_in (src);
1509 if (rsi)
1510 src = rsi;
1513 switch (GET_CODE (dest))
1515 case REG:
1516 switch (GET_CODE (src))
1518 /* Setting FP from SP. */
1519 case REG:
1520 if (cfa.reg == (unsigned) REGNO (src))
1522 /* Rule 1 */
1523 /* Update the CFA rule wrt SP or FP. Make sure src is
1524 relative to the current CFA register.
1526 We used to require that dest be either SP or FP, but the
1527 ARM copies SP to a temporary register, and from there to
1528 FP. So we just rely on the backends to only set
1529 RTX_FRAME_RELATED_P on appropriate insns. */
1530 cfa.reg = REGNO (dest);
1531 cfa_temp.reg = cfa.reg;
1532 cfa_temp.offset = cfa.offset;
1534 else
1536 /* Saving a register in a register. */
1537 gcc_assert (!fixed_regs [REGNO (dest)]
1538 /* For the SPARC and its register window. */
1539 || (DWARF_FRAME_REGNUM (REGNO (src))
1540 == DWARF_FRAME_RETURN_COLUMN));
1541 queue_reg_save (label, src, dest, 0);
1543 break;
1545 case PLUS:
1546 case MINUS:
1547 case LO_SUM:
1548 if (dest == stack_pointer_rtx)
1550 /* Rule 2 */
1551 /* Adjusting SP. */
1552 switch (GET_CODE (XEXP (src, 1)))
1554 case CONST_INT:
1555 offset = INTVAL (XEXP (src, 1));
1556 break;
1557 case REG:
1558 gcc_assert ((unsigned) REGNO (XEXP (src, 1))
1559 == cfa_temp.reg);
1560 offset = cfa_temp.offset;
1561 break;
1562 default:
1563 gcc_unreachable ();
1566 if (XEXP (src, 0) == hard_frame_pointer_rtx)
1568 /* Restoring SP from FP in the epilogue. */
1569 gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
1570 cfa.reg = STACK_POINTER_REGNUM;
1572 else if (GET_CODE (src) == LO_SUM)
1573 /* Assume we've set the source reg of the LO_SUM from sp. */
1575 else
1576 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
1578 if (GET_CODE (src) != MINUS)
1579 offset = -offset;
1580 if (cfa.reg == STACK_POINTER_REGNUM)
1581 cfa.offset += offset;
1582 if (cfa_store.reg == STACK_POINTER_REGNUM)
1583 cfa_store.offset += offset;
1585 else if (dest == hard_frame_pointer_rtx)
1587 /* Rule 3 */
1588 /* Either setting the FP from an offset of the SP,
1589 or adjusting the FP */
1590 gcc_assert (frame_pointer_needed);
1592 gcc_assert (REG_P (XEXP (src, 0))
1593 && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
1594 && GET_CODE (XEXP (src, 1)) == CONST_INT);
1595 offset = INTVAL (XEXP (src, 1));
1596 if (GET_CODE (src) != MINUS)
1597 offset = -offset;
1598 cfa.offset += offset;
1599 cfa.reg = HARD_FRAME_POINTER_REGNUM;
1601 else
1603 gcc_assert (GET_CODE (src) != MINUS);
1605 /* Rule 4 */
1606 if (REG_P (XEXP (src, 0))
1607 && REGNO (XEXP (src, 0)) == cfa.reg
1608 && GET_CODE (XEXP (src, 1)) == CONST_INT)
1610 /* Setting a temporary CFA register that will be copied
1611 into the FP later on. */
1612 offset = - INTVAL (XEXP (src, 1));
1613 cfa.offset += offset;
1614 cfa.reg = REGNO (dest);
1615 /* Or used to save regs to the stack. */
1616 cfa_temp.reg = cfa.reg;
1617 cfa_temp.offset = cfa.offset;
1620 /* Rule 5 */
1621 else if (REG_P (XEXP (src, 0))
1622 && REGNO (XEXP (src, 0)) == cfa_temp.reg
1623 && XEXP (src, 1) == stack_pointer_rtx)
1625 /* Setting a scratch register that we will use instead
1626 of SP for saving registers to the stack. */
1627 gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
1628 cfa_store.reg = REGNO (dest);
1629 cfa_store.offset = cfa.offset - cfa_temp.offset;
1632 /* Rule 9 */
1633 else if (GET_CODE (src) == LO_SUM
1634 && GET_CODE (XEXP (src, 1)) == CONST_INT)
1636 cfa_temp.reg = REGNO (dest);
1637 cfa_temp.offset = INTVAL (XEXP (src, 1));
1639 else
1640 gcc_unreachable ();
1642 break;
1644 /* Rule 6 */
1645 case CONST_INT:
1646 cfa_temp.reg = REGNO (dest);
1647 cfa_temp.offset = INTVAL (src);
1648 break;
1650 /* Rule 7 */
1651 case IOR:
1652 gcc_assert (REG_P (XEXP (src, 0))
1653 && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
1654 && GET_CODE (XEXP (src, 1)) == CONST_INT);
1656 if ((unsigned) REGNO (dest) != cfa_temp.reg)
1657 cfa_temp.reg = REGNO (dest);
1658 cfa_temp.offset |= INTVAL (XEXP (src, 1));
1659 break;
1661 /* Skip over HIGH, assuming it will be followed by a LO_SUM,
1662 which will fill in all of the bits. */
1663 /* Rule 8 */
1664 case HIGH:
1665 break;
1667 /* Rule 15 */
1668 case UNSPEC:
1669 case UNSPEC_VOLATILE:
1670 gcc_assert (targetm.dwarf_handle_frame_unspec);
1671 targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
1672 break;
1674 default:
1675 gcc_unreachable ();
1678 def_cfa_1 (label, &cfa);
1679 break;
1681 case MEM:
1682 gcc_assert (REG_P (src));
1684 /* Saving a register to the stack. Make sure dest is relative to the
1685 CFA register. */
1686 switch (GET_CODE (XEXP (dest, 0)))
1688 /* Rule 10 */
1689 /* With a push. */
1690 case PRE_MODIFY:
1691 /* We can't handle variable size modifications. */
1692 gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
1693 == CONST_INT);
1694 offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
1696 gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1697 && cfa_store.reg == STACK_POINTER_REGNUM);
1699 cfa_store.offset += offset;
1700 if (cfa.reg == STACK_POINTER_REGNUM)
1701 cfa.offset = cfa_store.offset;
1703 offset = -cfa_store.offset;
1704 break;
1706 /* Rule 11 */
1707 case PRE_INC:
1708 case PRE_DEC:
1709 offset = GET_MODE_SIZE (GET_MODE (dest));
1710 if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
1711 offset = -offset;
1713 gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1714 && cfa_store.reg == STACK_POINTER_REGNUM);
1716 cfa_store.offset += offset;
1717 if (cfa.reg == STACK_POINTER_REGNUM)
1718 cfa.offset = cfa_store.offset;
1720 offset = -cfa_store.offset;
1721 break;
1723 /* Rule 12 */
1724 /* With an offset. */
1725 case PLUS:
1726 case MINUS:
1727 case LO_SUM:
1729 int regno;
1731 gcc_assert (GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT
1732 && REG_P (XEXP (XEXP (dest, 0), 0)));
1733 offset = INTVAL (XEXP (XEXP (dest, 0), 1));
1734 if (GET_CODE (XEXP (dest, 0)) == MINUS)
1735 offset = -offset;
1737 regno = REGNO (XEXP (XEXP (dest, 0), 0));
1739 if (cfa_store.reg == (unsigned) regno)
1740 offset -= cfa_store.offset;
1741 else
1743 gcc_assert (cfa_temp.reg == (unsigned) regno);
1744 offset -= cfa_temp.offset;
1747 break;
1749 /* Rule 13 */
1750 /* Without an offset. */
1751 case REG:
1753 int regno = REGNO (XEXP (dest, 0));
1755 if (cfa_store.reg == (unsigned) regno)
1756 offset = -cfa_store.offset;
1757 else
1759 gcc_assert (cfa_temp.reg == (unsigned) regno);
1760 offset = -cfa_temp.offset;
1763 break;
1765 /* Rule 14 */
1766 case POST_INC:
1767 gcc_assert (cfa_temp.reg
1768 == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
1769 offset = -cfa_temp.offset;
1770 cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
1771 break;
1773 default:
1774 gcc_unreachable ();
1777 if (REGNO (src) != STACK_POINTER_REGNUM
1778 && REGNO (src) != HARD_FRAME_POINTER_REGNUM
1779 && (unsigned) REGNO (src) == cfa.reg)
1781 /* We're storing the current CFA reg into the stack. */
1783 if (cfa.offset == 0)
1785 /* If the source register is exactly the CFA, assume
1786 we're saving SP like any other register; this happens
1787 on the ARM. */
1788 def_cfa_1 (label, &cfa);
1789 queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
1790 break;
1792 else
1794 /* Otherwise, we'll need to look in the stack to
1795 calculate the CFA. */
1796 rtx x = XEXP (dest, 0);
1798 if (!REG_P (x))
1799 x = XEXP (x, 0);
1800 gcc_assert (REG_P (x));
1802 cfa.reg = REGNO (x);
1803 cfa.base_offset = offset;
1804 cfa.indirect = 1;
1805 def_cfa_1 (label, &cfa);
1806 break;
1810 def_cfa_1 (label, &cfa);
1811 queue_reg_save (label, src, NULL_RTX, offset);
1812 break;
1814 default:
1815 gcc_unreachable ();
1819 /* Record call frame debugging information for INSN, which either
1820 sets SP or FP (adjusting how we calculate the frame address) or saves a
1821 register to the stack. If INSN is NULL_RTX, initialize our state.
1823 If AFTER_P is false, we're being called before the insn is emitted,
1824 otherwise after. Call instructions get invoked twice. */
1826 void
1827 dwarf2out_frame_debug (rtx insn, bool after_p)
1829 const char *label;
1830 rtx src;
1832 if (insn == NULL_RTX)
1834 size_t i;
1836 /* Flush any queued register saves. */
1837 flush_queued_reg_saves ();
1839 /* Set up state for generating call frame debug info. */
1840 lookup_cfa (&cfa);
1841 gcc_assert (cfa.reg
1842 == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
1844 cfa.reg = STACK_POINTER_REGNUM;
1845 cfa_store = cfa;
1846 cfa_temp.reg = -1;
1847 cfa_temp.offset = 0;
1849 for (i = 0; i < num_regs_saved_in_regs; i++)
1851 regs_saved_in_regs[i].orig_reg = NULL_RTX;
1852 regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
1854 num_regs_saved_in_regs = 0;
1855 return;
1858 if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
1859 flush_queued_reg_saves ();
1861 if (! RTX_FRAME_RELATED_P (insn))
1863 if (!ACCUMULATE_OUTGOING_ARGS)
1864 dwarf2out_stack_adjust (insn, after_p);
1865 return;
1868 label = dwarf2out_cfi_label ();
1869 src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1870 if (src)
1871 insn = XEXP (src, 0);
1872 else
1873 insn = PATTERN (insn);
1875 dwarf2out_frame_debug_expr (insn, label);
1878 #endif
1880 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
1881 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
1882 (enum dwarf_call_frame_info cfi);
1884 static enum dw_cfi_oprnd_type
1885 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
1887 switch (cfi)
1889 case DW_CFA_nop:
1890 case DW_CFA_GNU_window_save:
1891 return dw_cfi_oprnd_unused;
1893 case DW_CFA_set_loc:
1894 case DW_CFA_advance_loc1:
1895 case DW_CFA_advance_loc2:
1896 case DW_CFA_advance_loc4:
1897 case DW_CFA_MIPS_advance_loc8:
1898 return dw_cfi_oprnd_addr;
1900 case DW_CFA_offset:
1901 case DW_CFA_offset_extended:
1902 case DW_CFA_def_cfa:
1903 case DW_CFA_offset_extended_sf:
1904 case DW_CFA_def_cfa_sf:
1905 case DW_CFA_restore_extended:
1906 case DW_CFA_undefined:
1907 case DW_CFA_same_value:
1908 case DW_CFA_def_cfa_register:
1909 case DW_CFA_register:
1910 return dw_cfi_oprnd_reg_num;
1912 case DW_CFA_def_cfa_offset:
1913 case DW_CFA_GNU_args_size:
1914 case DW_CFA_def_cfa_offset_sf:
1915 return dw_cfi_oprnd_offset;
1917 case DW_CFA_def_cfa_expression:
1918 case DW_CFA_expression:
1919 return dw_cfi_oprnd_loc;
1921 default:
1922 gcc_unreachable ();
1926 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
1927 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
1928 (enum dwarf_call_frame_info cfi);
1930 static enum dw_cfi_oprnd_type
1931 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
1933 switch (cfi)
1935 case DW_CFA_def_cfa:
1936 case DW_CFA_def_cfa_sf:
1937 case DW_CFA_offset:
1938 case DW_CFA_offset_extended_sf:
1939 case DW_CFA_offset_extended:
1940 return dw_cfi_oprnd_offset;
1942 case DW_CFA_register:
1943 return dw_cfi_oprnd_reg_num;
1945 default:
1946 return dw_cfi_oprnd_unused;
1950 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1952 /* Switch to eh_frame_section. If we don't have an eh_frame_section,
1953 switch to the data section instead, and write out a synthetic label
1954 for collect2. */
1956 static void
1957 switch_to_eh_frame_section (void)
1959 tree label;
1961 #ifdef EH_FRAME_SECTION_NAME
1962 if (eh_frame_section == 0)
1964 int flags;
1966 if (EH_TABLES_CAN_BE_READ_ONLY)
1968 int fde_encoding;
1969 int per_encoding;
1970 int lsda_encoding;
1972 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
1973 /*global=*/0);
1974 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
1975 /*global=*/1);
1976 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
1977 /*global=*/0);
1978 flags = ((! flag_pic
1979 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
1980 && (fde_encoding & 0x70) != DW_EH_PE_aligned
1981 && (per_encoding & 0x70) != DW_EH_PE_absptr
1982 && (per_encoding & 0x70) != DW_EH_PE_aligned
1983 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
1984 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
1985 ? 0 : SECTION_WRITE);
1987 else
1988 flags = SECTION_WRITE;
1989 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
1991 #endif
1993 if (eh_frame_section)
1994 switch_to_section (eh_frame_section);
1995 else
1997 /* We have no special eh_frame section. Put the information in
1998 the data section and emit special labels to guide collect2. */
1999 switch_to_section (data_section);
2000 label = get_file_function_name ('F');
2001 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2002 targetm.asm_out.globalize_label (asm_out_file,
2003 IDENTIFIER_POINTER (label));
2004 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
2008 /* Output a Call Frame Information opcode and its operand(s). */
2010 static void
2011 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
2013 unsigned long r;
2014 if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
2015 dw2_asm_output_data (1, (cfi->dw_cfi_opc
2016 | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
2017 "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
2018 cfi->dw_cfi_oprnd1.dw_cfi_offset);
2019 else if (cfi->dw_cfi_opc == DW_CFA_offset)
2021 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2022 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2023 "DW_CFA_offset, column 0x%lx", r);
2024 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2026 else if (cfi->dw_cfi_opc == DW_CFA_restore)
2028 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2029 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2030 "DW_CFA_restore, column 0x%lx", r);
2032 else
2034 dw2_asm_output_data (1, cfi->dw_cfi_opc,
2035 "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
2037 switch (cfi->dw_cfi_opc)
2039 case DW_CFA_set_loc:
2040 if (for_eh)
2041 dw2_asm_output_encoded_addr_rtx (
2042 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
2043 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
2044 false, NULL);
2045 else
2046 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2047 cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
2048 break;
2050 case DW_CFA_advance_loc1:
2051 dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2052 fde->dw_fde_current_label, NULL);
2053 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2054 break;
2056 case DW_CFA_advance_loc2:
2057 dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2058 fde->dw_fde_current_label, NULL);
2059 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2060 break;
2062 case DW_CFA_advance_loc4:
2063 dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2064 fde->dw_fde_current_label, NULL);
2065 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2066 break;
2068 case DW_CFA_MIPS_advance_loc8:
2069 dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2070 fde->dw_fde_current_label, NULL);
2071 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2072 break;
2074 case DW_CFA_offset_extended:
2075 case DW_CFA_def_cfa:
2076 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2077 dw2_asm_output_data_uleb128 (r, NULL);
2078 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2079 break;
2081 case DW_CFA_offset_extended_sf:
2082 case DW_CFA_def_cfa_sf:
2083 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2084 dw2_asm_output_data_uleb128 (r, NULL);
2085 dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2086 break;
2088 case DW_CFA_restore_extended:
2089 case DW_CFA_undefined:
2090 case DW_CFA_same_value:
2091 case DW_CFA_def_cfa_register:
2092 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2093 dw2_asm_output_data_uleb128 (r, NULL);
2094 break;
2096 case DW_CFA_register:
2097 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2098 dw2_asm_output_data_uleb128 (r, NULL);
2099 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
2100 dw2_asm_output_data_uleb128 (r, NULL);
2101 break;
2103 case DW_CFA_def_cfa_offset:
2104 case DW_CFA_GNU_args_size:
2105 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2106 break;
2108 case DW_CFA_def_cfa_offset_sf:
2109 dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2110 break;
2112 case DW_CFA_GNU_window_save:
2113 break;
2115 case DW_CFA_def_cfa_expression:
2116 case DW_CFA_expression:
2117 output_cfa_loc (cfi);
2118 break;
2120 case DW_CFA_GNU_negative_offset_extended:
2121 /* Obsoleted by DW_CFA_offset_extended_sf. */
2122 gcc_unreachable ();
2124 default:
2125 break;
2130 /* Output the call frame information used to record information
2131 that relates to calculating the frame pointer, and records the
2132 location of saved registers. */
2134 static void
2135 output_call_frame_info (int for_eh)
2137 unsigned int i;
2138 dw_fde_ref fde;
2139 dw_cfi_ref cfi;
2140 char l1[20], l2[20], section_start_label[20];
2141 bool any_lsda_needed = false;
2142 char augmentation[6];
2143 int augmentation_size;
2144 int fde_encoding = DW_EH_PE_absptr;
2145 int per_encoding = DW_EH_PE_absptr;
2146 int lsda_encoding = DW_EH_PE_absptr;
2147 int return_reg;
2149 /* Don't emit a CIE if there won't be any FDEs. */
2150 if (fde_table_in_use == 0)
2151 return;
2153 /* If we make FDEs linkonce, we may have to emit an empty label for
2154 an FDE that wouldn't otherwise be emitted. We want to avoid
2155 having an FDE kept around when the function it refers to is
2156 discarded. Example where this matters: a primary function
2157 template in C++ requires EH information, but an explicit
2158 specialization doesn't. */
2159 if (TARGET_USES_WEAK_UNWIND_INFO
2160 && ! flag_asynchronous_unwind_tables
2161 && for_eh)
2162 for (i = 0; i < fde_table_in_use; i++)
2163 if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
2164 && !fde_table[i].uses_eh_lsda
2165 && ! DECL_WEAK (fde_table[i].decl))
2166 targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
2167 for_eh, /* empty */ 1);
2169 /* If we don't have any functions we'll want to unwind out of, don't
2170 emit any EH unwind information. Note that if exceptions aren't
2171 enabled, we won't have collected nothrow information, and if we
2172 asked for asynchronous tables, we always want this info. */
2173 if (for_eh)
2175 bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
2177 for (i = 0; i < fde_table_in_use; i++)
2178 if (fde_table[i].uses_eh_lsda)
2179 any_eh_needed = any_lsda_needed = true;
2180 else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2181 any_eh_needed = true;
2182 else if (! fde_table[i].nothrow
2183 && ! fde_table[i].all_throwers_are_sibcalls)
2184 any_eh_needed = true;
2186 if (! any_eh_needed)
2187 return;
2190 /* We're going to be generating comments, so turn on app. */
2191 if (flag_debug_asm)
2192 app_enable ();
2194 if (for_eh)
2195 switch_to_eh_frame_section ();
2196 else
2197 switch_to_section (debug_frame_section);
2199 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
2200 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
2202 /* Output the CIE. */
2203 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
2204 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
2205 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2206 dw2_asm_output_data (4, 0xffffffff,
2207 "Initial length escape value indicating 64-bit DWARF extension");
2208 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2209 "Length of Common Information Entry");
2210 ASM_OUTPUT_LABEL (asm_out_file, l1);
2212 /* Now that the CIE pointer is PC-relative for EH,
2213 use 0 to identify the CIE. */
2214 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
2215 (for_eh ? 0 : DWARF_CIE_ID),
2216 "CIE Identifier Tag");
2218 dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
2220 augmentation[0] = 0;
2221 augmentation_size = 0;
2222 if (for_eh)
2224 char *p;
2226 /* Augmentation:
2227 z Indicates that a uleb128 is present to size the
2228 augmentation section.
2229 L Indicates the encoding (and thus presence) of
2230 an LSDA pointer in the FDE augmentation.
2231 R Indicates a non-default pointer encoding for
2232 FDE code pointers.
2233 P Indicates the presence of an encoding + language
2234 personality routine in the CIE augmentation. */
2236 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
2237 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
2238 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
2240 p = augmentation + 1;
2241 if (eh_personality_libfunc)
2243 *p++ = 'P';
2244 augmentation_size += 1 + size_of_encoded_value (per_encoding);
2246 if (any_lsda_needed)
2248 *p++ = 'L';
2249 augmentation_size += 1;
2251 if (fde_encoding != DW_EH_PE_absptr)
2253 *p++ = 'R';
2254 augmentation_size += 1;
2256 if (p > augmentation + 1)
2258 augmentation[0] = 'z';
2259 *p = '\0';
2262 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
2263 if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
2265 int offset = ( 4 /* Length */
2266 + 4 /* CIE Id */
2267 + 1 /* CIE version */
2268 + strlen (augmentation) + 1 /* Augmentation */
2269 + size_of_uleb128 (1) /* Code alignment */
2270 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
2271 + 1 /* RA column */
2272 + 1 /* Augmentation size */
2273 + 1 /* Personality encoding */ );
2274 int pad = -offset & (PTR_SIZE - 1);
2276 augmentation_size += pad;
2278 /* Augmentations should be small, so there's scarce need to
2279 iterate for a solution. Die if we exceed one uleb128 byte. */
2280 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
2284 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
2285 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
2286 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
2287 "CIE Data Alignment Factor");
2289 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
2290 if (DW_CIE_VERSION == 1)
2291 dw2_asm_output_data (1, return_reg, "CIE RA Column");
2292 else
2293 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
2295 if (augmentation[0])
2297 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
2298 if (eh_personality_libfunc)
2300 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
2301 eh_data_format_name (per_encoding));
2302 dw2_asm_output_encoded_addr_rtx (per_encoding,
2303 eh_personality_libfunc,
2304 true, NULL);
2307 if (any_lsda_needed)
2308 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
2309 eh_data_format_name (lsda_encoding));
2311 if (fde_encoding != DW_EH_PE_absptr)
2312 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
2313 eh_data_format_name (fde_encoding));
2316 for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
2317 output_cfi (cfi, NULL, for_eh);
2319 /* Pad the CIE out to an address sized boundary. */
2320 ASM_OUTPUT_ALIGN (asm_out_file,
2321 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
2322 ASM_OUTPUT_LABEL (asm_out_file, l2);
2324 /* Loop through all of the FDE's. */
2325 for (i = 0; i < fde_table_in_use; i++)
2327 fde = &fde_table[i];
2329 /* Don't emit EH unwind info for leaf functions that don't need it. */
2330 if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
2331 && (fde->nothrow || fde->all_throwers_are_sibcalls)
2332 && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2333 && !fde->uses_eh_lsda)
2334 continue;
2336 targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
2337 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
2338 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
2339 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
2340 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2341 dw2_asm_output_data (4, 0xffffffff,
2342 "Initial length escape value indicating 64-bit DWARF extension");
2343 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2344 "FDE Length");
2345 ASM_OUTPUT_LABEL (asm_out_file, l1);
2347 if (for_eh)
2348 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
2349 else
2350 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
2351 debug_frame_section, "FDE CIE offset");
2353 if (for_eh)
2355 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
2356 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
2357 dw2_asm_output_encoded_addr_rtx (fde_encoding,
2358 sym_ref,
2359 false,
2360 "FDE initial location");
2361 if (fde->dw_fde_switched_sections)
2363 rtx sym_ref2 = gen_rtx_SYMBOL_REF (Pmode,
2364 fde->dw_fde_unlikely_section_label);
2365 rtx sym_ref3= gen_rtx_SYMBOL_REF (Pmode,
2366 fde->dw_fde_hot_section_label);
2367 SYMBOL_REF_FLAGS (sym_ref2) |= SYMBOL_FLAG_LOCAL;
2368 SYMBOL_REF_FLAGS (sym_ref3) |= SYMBOL_FLAG_LOCAL;
2369 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref3, false,
2370 "FDE initial location");
2371 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2372 fde->dw_fde_hot_section_end_label,
2373 fde->dw_fde_hot_section_label,
2374 "FDE address range");
2375 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref2, false,
2376 "FDE initial location");
2377 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2378 fde->dw_fde_unlikely_section_end_label,
2379 fde->dw_fde_unlikely_section_label,
2380 "FDE address range");
2382 else
2383 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2384 fde->dw_fde_end, fde->dw_fde_begin,
2385 "FDE address range");
2387 else
2389 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
2390 "FDE initial location");
2391 if (fde->dw_fde_switched_sections)
2393 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2394 fde->dw_fde_hot_section_label,
2395 "FDE initial location");
2396 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2397 fde->dw_fde_hot_section_end_label,
2398 fde->dw_fde_hot_section_label,
2399 "FDE address range");
2400 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2401 fde->dw_fde_unlikely_section_label,
2402 "FDE initial location");
2403 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2404 fde->dw_fde_unlikely_section_end_label,
2405 fde->dw_fde_unlikely_section_label,
2406 "FDE address range");
2408 else
2409 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2410 fde->dw_fde_end, fde->dw_fde_begin,
2411 "FDE address range");
2414 if (augmentation[0])
2416 if (any_lsda_needed)
2418 int size = size_of_encoded_value (lsda_encoding);
2420 if (lsda_encoding == DW_EH_PE_aligned)
2422 int offset = ( 4 /* Length */
2423 + 4 /* CIE offset */
2424 + 2 * size_of_encoded_value (fde_encoding)
2425 + 1 /* Augmentation size */ );
2426 int pad = -offset & (PTR_SIZE - 1);
2428 size += pad;
2429 gcc_assert (size_of_uleb128 (size) == 1);
2432 dw2_asm_output_data_uleb128 (size, "Augmentation size");
2434 if (fde->uses_eh_lsda)
2436 ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
2437 fde->funcdef_number);
2438 dw2_asm_output_encoded_addr_rtx (
2439 lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
2440 false, "Language Specific Data Area");
2442 else
2444 if (lsda_encoding == DW_EH_PE_aligned)
2445 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2446 dw2_asm_output_data
2447 (size_of_encoded_value (lsda_encoding), 0,
2448 "Language Specific Data Area (none)");
2451 else
2452 dw2_asm_output_data_uleb128 (0, "Augmentation size");
2455 /* Loop through the Call Frame Instructions associated with
2456 this FDE. */
2457 fde->dw_fde_current_label = fde->dw_fde_begin;
2458 for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
2459 output_cfi (cfi, fde, for_eh);
2461 /* Pad the FDE out to an address sized boundary. */
2462 ASM_OUTPUT_ALIGN (asm_out_file,
2463 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
2464 ASM_OUTPUT_LABEL (asm_out_file, l2);
2467 if (for_eh && targetm.terminate_dw2_eh_frame_info)
2468 dw2_asm_output_data (4, 0, "End of Table");
2469 #ifdef MIPS_DEBUGGING_INFO
2470 /* Work around Irix 6 assembler bug whereby labels at the end of a section
2471 get a value of 0. Putting .align 0 after the label fixes it. */
2472 ASM_OUTPUT_ALIGN (asm_out_file, 0);
2473 #endif
2475 /* Turn off app to make assembly quicker. */
2476 if (flag_debug_asm)
2477 app_disable ();
2480 /* Output a marker (i.e. a label) for the beginning of a function, before
2481 the prologue. */
2483 void
2484 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
2485 const char *file ATTRIBUTE_UNUSED)
2487 char label[MAX_ARTIFICIAL_LABEL_BYTES];
2488 char * dup_label;
2489 dw_fde_ref fde;
2491 current_function_func_begin_label = NULL;
2493 #ifdef TARGET_UNWIND_INFO
2494 /* ??? current_function_func_begin_label is also used by except.c
2495 for call-site information. We must emit this label if it might
2496 be used. */
2497 if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
2498 && ! dwarf2out_do_frame ())
2499 return;
2500 #else
2501 if (! dwarf2out_do_frame ())
2502 return;
2503 #endif
2505 switch_to_section (function_section (current_function_decl));
2506 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
2507 current_function_funcdef_no);
2508 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
2509 current_function_funcdef_no);
2510 dup_label = xstrdup (label);
2511 current_function_func_begin_label = dup_label;
2513 #ifdef TARGET_UNWIND_INFO
2514 /* We can elide the fde allocation if we're not emitting debug info. */
2515 if (! dwarf2out_do_frame ())
2516 return;
2517 #endif
2519 /* Expand the fde table if necessary. */
2520 if (fde_table_in_use == fde_table_allocated)
2522 fde_table_allocated += FDE_TABLE_INCREMENT;
2523 fde_table = ggc_realloc (fde_table,
2524 fde_table_allocated * sizeof (dw_fde_node));
2525 memset (fde_table + fde_table_in_use, 0,
2526 FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2529 /* Record the FDE associated with this function. */
2530 current_funcdef_fde = fde_table_in_use;
2532 /* Add the new FDE at the end of the fde_table. */
2533 fde = &fde_table[fde_table_in_use++];
2534 fde->decl = current_function_decl;
2535 fde->dw_fde_begin = dup_label;
2536 fde->dw_fde_current_label = NULL;
2537 fde->dw_fde_hot_section_label = NULL;
2538 fde->dw_fde_hot_section_end_label = NULL;
2539 fde->dw_fde_unlikely_section_label = NULL;
2540 fde->dw_fde_unlikely_section_end_label = NULL;
2541 fde->dw_fde_switched_sections = false;
2542 fde->dw_fde_end = NULL;
2543 fde->dw_fde_cfi = NULL;
2544 fde->funcdef_number = current_function_funcdef_no;
2545 fde->nothrow = TREE_NOTHROW (current_function_decl);
2546 fde->uses_eh_lsda = cfun->uses_eh_lsda;
2547 fde->all_throwers_are_sibcalls = cfun->all_throwers_are_sibcalls;
2549 args_size = old_args_size = 0;
2551 /* We only want to output line number information for the genuine dwarf2
2552 prologue case, not the eh frame case. */
2553 #ifdef DWARF2_DEBUGGING_INFO
2554 if (file)
2555 dwarf2out_source_line (line, file);
2556 #endif
2559 /* Output a marker (i.e. a label) for the absolute end of the generated code
2560 for a function definition. This gets called *after* the epilogue code has
2561 been generated. */
2563 void
2564 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
2565 const char *file ATTRIBUTE_UNUSED)
2567 dw_fde_ref fde;
2568 char label[MAX_ARTIFICIAL_LABEL_BYTES];
2570 /* Output a label to mark the endpoint of the code generated for this
2571 function. */
2572 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
2573 current_function_funcdef_no);
2574 ASM_OUTPUT_LABEL (asm_out_file, label);
2575 fde = &fde_table[fde_table_in_use - 1];
2576 fde->dw_fde_end = xstrdup (label);
2579 void
2580 dwarf2out_frame_init (void)
2582 /* Allocate the initial hunk of the fde_table. */
2583 fde_table = ggc_alloc_cleared (FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2584 fde_table_allocated = FDE_TABLE_INCREMENT;
2585 fde_table_in_use = 0;
2587 /* Generate the CFA instructions common to all FDE's. Do it now for the
2588 sake of lookup_cfa. */
2590 /* On entry, the Canonical Frame Address is at SP. */
2591 dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
2593 #ifdef DWARF2_UNWIND_INFO
2594 if (DWARF2_UNWIND_INFO)
2595 initial_return_save (INCOMING_RETURN_ADDR_RTX);
2596 #endif
2599 void
2600 dwarf2out_frame_finish (void)
2602 /* Output call frame information. */
2603 if (DWARF2_FRAME_INFO)
2604 output_call_frame_info (0);
2606 #ifndef TARGET_UNWIND_INFO
2607 /* Output another copy for the unwinder. */
2608 if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
2609 output_call_frame_info (1);
2610 #endif
2612 #endif
2614 /* And now, the subset of the debugging information support code necessary
2615 for emitting location expressions. */
2617 /* We need some way to distinguish DW_OP_addr with a direct symbol
2618 relocation from DW_OP_addr with a dtp-relative symbol relocation. */
2619 #define INTERNAL_DW_OP_tls_addr (0x100 + DW_OP_addr)
2622 typedef struct dw_val_struct *dw_val_ref;
2623 typedef struct die_struct *dw_die_ref;
2624 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
2625 typedef struct dw_loc_list_struct *dw_loc_list_ref;
2627 /* Each DIE may have a series of attribute/value pairs. Values
2628 can take on several forms. The forms that are used in this
2629 implementation are listed below. */
2631 enum dw_val_class
2633 dw_val_class_addr,
2634 dw_val_class_offset,
2635 dw_val_class_loc,
2636 dw_val_class_loc_list,
2637 dw_val_class_range_list,
2638 dw_val_class_const,
2639 dw_val_class_unsigned_const,
2640 dw_val_class_long_long,
2641 dw_val_class_vec,
2642 dw_val_class_flag,
2643 dw_val_class_die_ref,
2644 dw_val_class_fde_ref,
2645 dw_val_class_lbl_id,
2646 dw_val_class_lineptr,
2647 dw_val_class_str,
2648 dw_val_class_macptr
2651 /* Describe a double word constant value. */
2652 /* ??? Every instance of long_long in the code really means CONST_DOUBLE. */
2654 typedef struct dw_long_long_struct GTY(())
2656 unsigned long hi;
2657 unsigned long low;
2659 dw_long_long_const;
2661 /* Describe a floating point constant value, or a vector constant value. */
2663 typedef struct dw_vec_struct GTY(())
2665 unsigned char * GTY((length ("%h.length"))) array;
2666 unsigned length;
2667 unsigned elt_size;
2669 dw_vec_const;
2671 /* The dw_val_node describes an attribute's value, as it is
2672 represented internally. */
2674 typedef struct dw_val_struct GTY(())
2676 enum dw_val_class val_class;
2677 union dw_val_struct_union
2679 rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
2680 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
2681 dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
2682 dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
2683 HOST_WIDE_INT GTY ((default)) val_int;
2684 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
2685 dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
2686 dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
2687 struct dw_val_die_union
2689 dw_die_ref die;
2690 int external;
2691 } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
2692 unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
2693 struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
2694 char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
2695 unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
2697 GTY ((desc ("%1.val_class"))) v;
2699 dw_val_node;
2701 /* Locations in memory are described using a sequence of stack machine
2702 operations. */
2704 typedef struct dw_loc_descr_struct GTY(())
2706 dw_loc_descr_ref dw_loc_next;
2707 enum dwarf_location_atom dw_loc_opc;
2708 dw_val_node dw_loc_oprnd1;
2709 dw_val_node dw_loc_oprnd2;
2710 int dw_loc_addr;
2712 dw_loc_descr_node;
2714 /* Location lists are ranges + location descriptions for that range,
2715 so you can track variables that are in different places over
2716 their entire life. */
2717 typedef struct dw_loc_list_struct GTY(())
2719 dw_loc_list_ref dw_loc_next;
2720 const char *begin; /* Label for begin address of range */
2721 const char *end; /* Label for end address of range */
2722 char *ll_symbol; /* Label for beginning of location list.
2723 Only on head of list */
2724 const char *section; /* Section this loclist is relative to */
2725 dw_loc_descr_ref expr;
2726 } dw_loc_list_node;
2728 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2730 static const char *dwarf_stack_op_name (unsigned);
2731 static dw_loc_descr_ref new_loc_descr (enum dwarf_location_atom,
2732 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2733 static void add_loc_descr (dw_loc_descr_ref *, dw_loc_descr_ref);
2734 static unsigned long size_of_loc_descr (dw_loc_descr_ref);
2735 static unsigned long size_of_locs (dw_loc_descr_ref);
2736 static void output_loc_operands (dw_loc_descr_ref);
2737 static void output_loc_sequence (dw_loc_descr_ref);
2739 /* Convert a DWARF stack opcode into its string name. */
2741 static const char *
2742 dwarf_stack_op_name (unsigned int op)
2744 switch (op)
2746 case DW_OP_addr:
2747 case INTERNAL_DW_OP_tls_addr:
2748 return "DW_OP_addr";
2749 case DW_OP_deref:
2750 return "DW_OP_deref";
2751 case DW_OP_const1u:
2752 return "DW_OP_const1u";
2753 case DW_OP_const1s:
2754 return "DW_OP_const1s";
2755 case DW_OP_const2u:
2756 return "DW_OP_const2u";
2757 case DW_OP_const2s:
2758 return "DW_OP_const2s";
2759 case DW_OP_const4u:
2760 return "DW_OP_const4u";
2761 case DW_OP_const4s:
2762 return "DW_OP_const4s";
2763 case DW_OP_const8u:
2764 return "DW_OP_const8u";
2765 case DW_OP_const8s:
2766 return "DW_OP_const8s";
2767 case DW_OP_constu:
2768 return "DW_OP_constu";
2769 case DW_OP_consts:
2770 return "DW_OP_consts";
2771 case DW_OP_dup:
2772 return "DW_OP_dup";
2773 case DW_OP_drop:
2774 return "DW_OP_drop";
2775 case DW_OP_over:
2776 return "DW_OP_over";
2777 case DW_OP_pick:
2778 return "DW_OP_pick";
2779 case DW_OP_swap:
2780 return "DW_OP_swap";
2781 case DW_OP_rot:
2782 return "DW_OP_rot";
2783 case DW_OP_xderef:
2784 return "DW_OP_xderef";
2785 case DW_OP_abs:
2786 return "DW_OP_abs";
2787 case DW_OP_and:
2788 return "DW_OP_and";
2789 case DW_OP_div:
2790 return "DW_OP_div";
2791 case DW_OP_minus:
2792 return "DW_OP_minus";
2793 case DW_OP_mod:
2794 return "DW_OP_mod";
2795 case DW_OP_mul:
2796 return "DW_OP_mul";
2797 case DW_OP_neg:
2798 return "DW_OP_neg";
2799 case DW_OP_not:
2800 return "DW_OP_not";
2801 case DW_OP_or:
2802 return "DW_OP_or";
2803 case DW_OP_plus:
2804 return "DW_OP_plus";
2805 case DW_OP_plus_uconst:
2806 return "DW_OP_plus_uconst";
2807 case DW_OP_shl:
2808 return "DW_OP_shl";
2809 case DW_OP_shr:
2810 return "DW_OP_shr";
2811 case DW_OP_shra:
2812 return "DW_OP_shra";
2813 case DW_OP_xor:
2814 return "DW_OP_xor";
2815 case DW_OP_bra:
2816 return "DW_OP_bra";
2817 case DW_OP_eq:
2818 return "DW_OP_eq";
2819 case DW_OP_ge:
2820 return "DW_OP_ge";
2821 case DW_OP_gt:
2822 return "DW_OP_gt";
2823 case DW_OP_le:
2824 return "DW_OP_le";
2825 case DW_OP_lt:
2826 return "DW_OP_lt";
2827 case DW_OP_ne:
2828 return "DW_OP_ne";
2829 case DW_OP_skip:
2830 return "DW_OP_skip";
2831 case DW_OP_lit0:
2832 return "DW_OP_lit0";
2833 case DW_OP_lit1:
2834 return "DW_OP_lit1";
2835 case DW_OP_lit2:
2836 return "DW_OP_lit2";
2837 case DW_OP_lit3:
2838 return "DW_OP_lit3";
2839 case DW_OP_lit4:
2840 return "DW_OP_lit4";
2841 case DW_OP_lit5:
2842 return "DW_OP_lit5";
2843 case DW_OP_lit6:
2844 return "DW_OP_lit6";
2845 case DW_OP_lit7:
2846 return "DW_OP_lit7";
2847 case DW_OP_lit8:
2848 return "DW_OP_lit8";
2849 case DW_OP_lit9:
2850 return "DW_OP_lit9";
2851 case DW_OP_lit10:
2852 return "DW_OP_lit10";
2853 case DW_OP_lit11:
2854 return "DW_OP_lit11";
2855 case DW_OP_lit12:
2856 return "DW_OP_lit12";
2857 case DW_OP_lit13:
2858 return "DW_OP_lit13";
2859 case DW_OP_lit14:
2860 return "DW_OP_lit14";
2861 case DW_OP_lit15:
2862 return "DW_OP_lit15";
2863 case DW_OP_lit16:
2864 return "DW_OP_lit16";
2865 case DW_OP_lit17:
2866 return "DW_OP_lit17";
2867 case DW_OP_lit18:
2868 return "DW_OP_lit18";
2869 case DW_OP_lit19:
2870 return "DW_OP_lit19";
2871 case DW_OP_lit20:
2872 return "DW_OP_lit20";
2873 case DW_OP_lit21:
2874 return "DW_OP_lit21";
2875 case DW_OP_lit22:
2876 return "DW_OP_lit22";
2877 case DW_OP_lit23:
2878 return "DW_OP_lit23";
2879 case DW_OP_lit24:
2880 return "DW_OP_lit24";
2881 case DW_OP_lit25:
2882 return "DW_OP_lit25";
2883 case DW_OP_lit26:
2884 return "DW_OP_lit26";
2885 case DW_OP_lit27:
2886 return "DW_OP_lit27";
2887 case DW_OP_lit28:
2888 return "DW_OP_lit28";
2889 case DW_OP_lit29:
2890 return "DW_OP_lit29";
2891 case DW_OP_lit30:
2892 return "DW_OP_lit30";
2893 case DW_OP_lit31:
2894 return "DW_OP_lit31";
2895 case DW_OP_reg0:
2896 return "DW_OP_reg0";
2897 case DW_OP_reg1:
2898 return "DW_OP_reg1";
2899 case DW_OP_reg2:
2900 return "DW_OP_reg2";
2901 case DW_OP_reg3:
2902 return "DW_OP_reg3";
2903 case DW_OP_reg4:
2904 return "DW_OP_reg4";
2905 case DW_OP_reg5:
2906 return "DW_OP_reg5";
2907 case DW_OP_reg6:
2908 return "DW_OP_reg6";
2909 case DW_OP_reg7:
2910 return "DW_OP_reg7";
2911 case DW_OP_reg8:
2912 return "DW_OP_reg8";
2913 case DW_OP_reg9:
2914 return "DW_OP_reg9";
2915 case DW_OP_reg10:
2916 return "DW_OP_reg10";
2917 case DW_OP_reg11:
2918 return "DW_OP_reg11";
2919 case DW_OP_reg12:
2920 return "DW_OP_reg12";
2921 case DW_OP_reg13:
2922 return "DW_OP_reg13";
2923 case DW_OP_reg14:
2924 return "DW_OP_reg14";
2925 case DW_OP_reg15:
2926 return "DW_OP_reg15";
2927 case DW_OP_reg16:
2928 return "DW_OP_reg16";
2929 case DW_OP_reg17:
2930 return "DW_OP_reg17";
2931 case DW_OP_reg18:
2932 return "DW_OP_reg18";
2933 case DW_OP_reg19:
2934 return "DW_OP_reg19";
2935 case DW_OP_reg20:
2936 return "DW_OP_reg20";
2937 case DW_OP_reg21:
2938 return "DW_OP_reg21";
2939 case DW_OP_reg22:
2940 return "DW_OP_reg22";
2941 case DW_OP_reg23:
2942 return "DW_OP_reg23";
2943 case DW_OP_reg24:
2944 return "DW_OP_reg24";
2945 case DW_OP_reg25:
2946 return "DW_OP_reg25";
2947 case DW_OP_reg26:
2948 return "DW_OP_reg26";
2949 case DW_OP_reg27:
2950 return "DW_OP_reg27";
2951 case DW_OP_reg28:
2952 return "DW_OP_reg28";
2953 case DW_OP_reg29:
2954 return "DW_OP_reg29";
2955 case DW_OP_reg30:
2956 return "DW_OP_reg30";
2957 case DW_OP_reg31:
2958 return "DW_OP_reg31";
2959 case DW_OP_breg0:
2960 return "DW_OP_breg0";
2961 case DW_OP_breg1:
2962 return "DW_OP_breg1";
2963 case DW_OP_breg2:
2964 return "DW_OP_breg2";
2965 case DW_OP_breg3:
2966 return "DW_OP_breg3";
2967 case DW_OP_breg4:
2968 return "DW_OP_breg4";
2969 case DW_OP_breg5:
2970 return "DW_OP_breg5";
2971 case DW_OP_breg6:
2972 return "DW_OP_breg6";
2973 case DW_OP_breg7:
2974 return "DW_OP_breg7";
2975 case DW_OP_breg8:
2976 return "DW_OP_breg8";
2977 case DW_OP_breg9:
2978 return "DW_OP_breg9";
2979 case DW_OP_breg10:
2980 return "DW_OP_breg10";
2981 case DW_OP_breg11:
2982 return "DW_OP_breg11";
2983 case DW_OP_breg12:
2984 return "DW_OP_breg12";
2985 case DW_OP_breg13:
2986 return "DW_OP_breg13";
2987 case DW_OP_breg14:
2988 return "DW_OP_breg14";
2989 case DW_OP_breg15:
2990 return "DW_OP_breg15";
2991 case DW_OP_breg16:
2992 return "DW_OP_breg16";
2993 case DW_OP_breg17:
2994 return "DW_OP_breg17";
2995 case DW_OP_breg18:
2996 return "DW_OP_breg18";
2997 case DW_OP_breg19:
2998 return "DW_OP_breg19";
2999 case DW_OP_breg20:
3000 return "DW_OP_breg20";
3001 case DW_OP_breg21:
3002 return "DW_OP_breg21";
3003 case DW_OP_breg22:
3004 return "DW_OP_breg22";
3005 case DW_OP_breg23:
3006 return "DW_OP_breg23";
3007 case DW_OP_breg24:
3008 return "DW_OP_breg24";
3009 case DW_OP_breg25:
3010 return "DW_OP_breg25";
3011 case DW_OP_breg26:
3012 return "DW_OP_breg26";
3013 case DW_OP_breg27:
3014 return "DW_OP_breg27";
3015 case DW_OP_breg28:
3016 return "DW_OP_breg28";
3017 case DW_OP_breg29:
3018 return "DW_OP_breg29";
3019 case DW_OP_breg30:
3020 return "DW_OP_breg30";
3021 case DW_OP_breg31:
3022 return "DW_OP_breg31";
3023 case DW_OP_regx:
3024 return "DW_OP_regx";
3025 case DW_OP_fbreg:
3026 return "DW_OP_fbreg";
3027 case DW_OP_bregx:
3028 return "DW_OP_bregx";
3029 case DW_OP_piece:
3030 return "DW_OP_piece";
3031 case DW_OP_deref_size:
3032 return "DW_OP_deref_size";
3033 case DW_OP_xderef_size:
3034 return "DW_OP_xderef_size";
3035 case DW_OP_nop:
3036 return "DW_OP_nop";
3037 case DW_OP_push_object_address:
3038 return "DW_OP_push_object_address";
3039 case DW_OP_call2:
3040 return "DW_OP_call2";
3041 case DW_OP_call4:
3042 return "DW_OP_call4";
3043 case DW_OP_call_ref:
3044 return "DW_OP_call_ref";
3045 case DW_OP_GNU_push_tls_address:
3046 return "DW_OP_GNU_push_tls_address";
3047 default:
3048 return "OP_<unknown>";
3052 /* Return a pointer to a newly allocated location description. Location
3053 descriptions are simple expression terms that can be strung
3054 together to form more complicated location (address) descriptions. */
3056 static inline dw_loc_descr_ref
3057 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
3058 unsigned HOST_WIDE_INT oprnd2)
3060 dw_loc_descr_ref descr = ggc_alloc_cleared (sizeof (dw_loc_descr_node));
3062 descr->dw_loc_opc = op;
3063 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
3064 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
3065 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
3066 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
3068 return descr;
3071 /* Add a location description term to a location description expression. */
3073 static inline void
3074 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
3076 dw_loc_descr_ref *d;
3078 /* Find the end of the chain. */
3079 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
3082 *d = descr;
3085 /* Return the size of a location descriptor. */
3087 static unsigned long
3088 size_of_loc_descr (dw_loc_descr_ref loc)
3090 unsigned long size = 1;
3092 switch (loc->dw_loc_opc)
3094 case DW_OP_addr:
3095 case INTERNAL_DW_OP_tls_addr:
3096 size += DWARF2_ADDR_SIZE;
3097 break;
3098 case DW_OP_const1u:
3099 case DW_OP_const1s:
3100 size += 1;
3101 break;
3102 case DW_OP_const2u:
3103 case DW_OP_const2s:
3104 size += 2;
3105 break;
3106 case DW_OP_const4u:
3107 case DW_OP_const4s:
3108 size += 4;
3109 break;
3110 case DW_OP_const8u:
3111 case DW_OP_const8s:
3112 size += 8;
3113 break;
3114 case DW_OP_constu:
3115 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3116 break;
3117 case DW_OP_consts:
3118 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3119 break;
3120 case DW_OP_pick:
3121 size += 1;
3122 break;
3123 case DW_OP_plus_uconst:
3124 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3125 break;
3126 case DW_OP_skip:
3127 case DW_OP_bra:
3128 size += 2;
3129 break;
3130 case DW_OP_breg0:
3131 case DW_OP_breg1:
3132 case DW_OP_breg2:
3133 case DW_OP_breg3:
3134 case DW_OP_breg4:
3135 case DW_OP_breg5:
3136 case DW_OP_breg6:
3137 case DW_OP_breg7:
3138 case DW_OP_breg8:
3139 case DW_OP_breg9:
3140 case DW_OP_breg10:
3141 case DW_OP_breg11:
3142 case DW_OP_breg12:
3143 case DW_OP_breg13:
3144 case DW_OP_breg14:
3145 case DW_OP_breg15:
3146 case DW_OP_breg16:
3147 case DW_OP_breg17:
3148 case DW_OP_breg18:
3149 case DW_OP_breg19:
3150 case DW_OP_breg20:
3151 case DW_OP_breg21:
3152 case DW_OP_breg22:
3153 case DW_OP_breg23:
3154 case DW_OP_breg24:
3155 case DW_OP_breg25:
3156 case DW_OP_breg26:
3157 case DW_OP_breg27:
3158 case DW_OP_breg28:
3159 case DW_OP_breg29:
3160 case DW_OP_breg30:
3161 case DW_OP_breg31:
3162 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3163 break;
3164 case DW_OP_regx:
3165 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3166 break;
3167 case DW_OP_fbreg:
3168 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3169 break;
3170 case DW_OP_bregx:
3171 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3172 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
3173 break;
3174 case DW_OP_piece:
3175 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3176 break;
3177 case DW_OP_deref_size:
3178 case DW_OP_xderef_size:
3179 size += 1;
3180 break;
3181 case DW_OP_call2:
3182 size += 2;
3183 break;
3184 case DW_OP_call4:
3185 size += 4;
3186 break;
3187 case DW_OP_call_ref:
3188 size += DWARF2_ADDR_SIZE;
3189 break;
3190 default:
3191 break;
3194 return size;
3197 /* Return the size of a series of location descriptors. */
3199 static unsigned long
3200 size_of_locs (dw_loc_descr_ref loc)
3202 unsigned long size;
3204 for (size = 0; loc != NULL; loc = loc->dw_loc_next)
3206 loc->dw_loc_addr = size;
3207 size += size_of_loc_descr (loc);
3210 return size;
3213 /* Output location description stack opcode's operands (if any). */
3215 static void
3216 output_loc_operands (dw_loc_descr_ref loc)
3218 dw_val_ref val1 = &loc->dw_loc_oprnd1;
3219 dw_val_ref val2 = &loc->dw_loc_oprnd2;
3221 switch (loc->dw_loc_opc)
3223 #ifdef DWARF2_DEBUGGING_INFO
3224 case DW_OP_addr:
3225 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
3226 break;
3227 case DW_OP_const2u:
3228 case DW_OP_const2s:
3229 dw2_asm_output_data (2, val1->v.val_int, NULL);
3230 break;
3231 case DW_OP_const4u:
3232 case DW_OP_const4s:
3233 dw2_asm_output_data (4, val1->v.val_int, NULL);
3234 break;
3235 case DW_OP_const8u:
3236 case DW_OP_const8s:
3237 gcc_assert (HOST_BITS_PER_LONG >= 64);
3238 dw2_asm_output_data (8, val1->v.val_int, NULL);
3239 break;
3240 case DW_OP_skip:
3241 case DW_OP_bra:
3243 int offset;
3245 gcc_assert (val1->val_class == dw_val_class_loc);
3246 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
3248 dw2_asm_output_data (2, offset, NULL);
3250 break;
3251 #else
3252 case DW_OP_addr:
3253 case DW_OP_const2u:
3254 case DW_OP_const2s:
3255 case DW_OP_const4u:
3256 case DW_OP_const4s:
3257 case DW_OP_const8u:
3258 case DW_OP_const8s:
3259 case DW_OP_skip:
3260 case DW_OP_bra:
3261 /* We currently don't make any attempt to make sure these are
3262 aligned properly like we do for the main unwind info, so
3263 don't support emitting things larger than a byte if we're
3264 only doing unwinding. */
3265 gcc_unreachable ();
3266 #endif
3267 case DW_OP_const1u:
3268 case DW_OP_const1s:
3269 dw2_asm_output_data (1, val1->v.val_int, NULL);
3270 break;
3271 case DW_OP_constu:
3272 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3273 break;
3274 case DW_OP_consts:
3275 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3276 break;
3277 case DW_OP_pick:
3278 dw2_asm_output_data (1, val1->v.val_int, NULL);
3279 break;
3280 case DW_OP_plus_uconst:
3281 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3282 break;
3283 case DW_OP_breg0:
3284 case DW_OP_breg1:
3285 case DW_OP_breg2:
3286 case DW_OP_breg3:
3287 case DW_OP_breg4:
3288 case DW_OP_breg5:
3289 case DW_OP_breg6:
3290 case DW_OP_breg7:
3291 case DW_OP_breg8:
3292 case DW_OP_breg9:
3293 case DW_OP_breg10:
3294 case DW_OP_breg11:
3295 case DW_OP_breg12:
3296 case DW_OP_breg13:
3297 case DW_OP_breg14:
3298 case DW_OP_breg15:
3299 case DW_OP_breg16:
3300 case DW_OP_breg17:
3301 case DW_OP_breg18:
3302 case DW_OP_breg19:
3303 case DW_OP_breg20:
3304 case DW_OP_breg21:
3305 case DW_OP_breg22:
3306 case DW_OP_breg23:
3307 case DW_OP_breg24:
3308 case DW_OP_breg25:
3309 case DW_OP_breg26:
3310 case DW_OP_breg27:
3311 case DW_OP_breg28:
3312 case DW_OP_breg29:
3313 case DW_OP_breg30:
3314 case DW_OP_breg31:
3315 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3316 break;
3317 case DW_OP_regx:
3318 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3319 break;
3320 case DW_OP_fbreg:
3321 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3322 break;
3323 case DW_OP_bregx:
3324 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3325 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
3326 break;
3327 case DW_OP_piece:
3328 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3329 break;
3330 case DW_OP_deref_size:
3331 case DW_OP_xderef_size:
3332 dw2_asm_output_data (1, val1->v.val_int, NULL);
3333 break;
3335 case INTERNAL_DW_OP_tls_addr:
3336 if (targetm.asm_out.output_dwarf_dtprel)
3338 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
3339 DWARF2_ADDR_SIZE,
3340 val1->v.val_addr);
3341 fputc ('\n', asm_out_file);
3343 else
3344 gcc_unreachable ();
3345 break;
3347 default:
3348 /* Other codes have no operands. */
3349 break;
3353 /* Output a sequence of location operations. */
3355 static void
3356 output_loc_sequence (dw_loc_descr_ref loc)
3358 for (; loc != NULL; loc = loc->dw_loc_next)
3360 /* Output the opcode. */
3361 dw2_asm_output_data (1, loc->dw_loc_opc,
3362 "%s", dwarf_stack_op_name (loc->dw_loc_opc));
3364 /* Output the operand(s) (if any). */
3365 output_loc_operands (loc);
3369 /* This routine will generate the correct assembly data for a location
3370 description based on a cfi entry with a complex address. */
3372 static void
3373 output_cfa_loc (dw_cfi_ref cfi)
3375 dw_loc_descr_ref loc;
3376 unsigned long size;
3378 /* Output the size of the block. */
3379 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
3380 size = size_of_locs (loc);
3381 dw2_asm_output_data_uleb128 (size, NULL);
3383 /* Now output the operations themselves. */
3384 output_loc_sequence (loc);
3387 /* This function builds a dwarf location descriptor sequence from a
3388 dw_cfa_location, adding the given OFFSET to the result of the
3389 expression. */
3391 static struct dw_loc_descr_struct *
3392 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
3394 struct dw_loc_descr_struct *head, *tmp;
3396 offset += cfa->offset;
3398 if (cfa->indirect)
3400 if (cfa->base_offset)
3402 if (cfa->reg <= 31)
3403 head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->base_offset, 0);
3404 else
3405 head = new_loc_descr (DW_OP_bregx, cfa->reg, cfa->base_offset);
3407 else if (cfa->reg <= 31)
3408 head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3409 else
3410 head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3412 head->dw_loc_oprnd1.val_class = dw_val_class_const;
3413 tmp = new_loc_descr (DW_OP_deref, 0, 0);
3414 add_loc_descr (&head, tmp);
3415 if (offset != 0)
3417 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
3418 add_loc_descr (&head, tmp);
3421 else
3423 if (offset == 0)
3424 if (cfa->reg <= 31)
3425 head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3426 else
3427 head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3428 else if (cfa->reg <= 31)
3429 head = new_loc_descr (DW_OP_breg0 + cfa->reg, offset, 0);
3430 else
3431 head = new_loc_descr (DW_OP_bregx, cfa->reg, offset);
3434 return head;
3437 /* This function fills in aa dw_cfa_location structure from a dwarf location
3438 descriptor sequence. */
3440 static void
3441 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
3443 struct dw_loc_descr_struct *ptr;
3444 cfa->offset = 0;
3445 cfa->base_offset = 0;
3446 cfa->indirect = 0;
3447 cfa->reg = -1;
3449 for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
3451 enum dwarf_location_atom op = ptr->dw_loc_opc;
3453 switch (op)
3455 case DW_OP_reg0:
3456 case DW_OP_reg1:
3457 case DW_OP_reg2:
3458 case DW_OP_reg3:
3459 case DW_OP_reg4:
3460 case DW_OP_reg5:
3461 case DW_OP_reg6:
3462 case DW_OP_reg7:
3463 case DW_OP_reg8:
3464 case DW_OP_reg9:
3465 case DW_OP_reg10:
3466 case DW_OP_reg11:
3467 case DW_OP_reg12:
3468 case DW_OP_reg13:
3469 case DW_OP_reg14:
3470 case DW_OP_reg15:
3471 case DW_OP_reg16:
3472 case DW_OP_reg17:
3473 case DW_OP_reg18:
3474 case DW_OP_reg19:
3475 case DW_OP_reg20:
3476 case DW_OP_reg21:
3477 case DW_OP_reg22:
3478 case DW_OP_reg23:
3479 case DW_OP_reg24:
3480 case DW_OP_reg25:
3481 case DW_OP_reg26:
3482 case DW_OP_reg27:
3483 case DW_OP_reg28:
3484 case DW_OP_reg29:
3485 case DW_OP_reg30:
3486 case DW_OP_reg31:
3487 cfa->reg = op - DW_OP_reg0;
3488 break;
3489 case DW_OP_regx:
3490 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3491 break;
3492 case DW_OP_breg0:
3493 case DW_OP_breg1:
3494 case DW_OP_breg2:
3495 case DW_OP_breg3:
3496 case DW_OP_breg4:
3497 case DW_OP_breg5:
3498 case DW_OP_breg6:
3499 case DW_OP_breg7:
3500 case DW_OP_breg8:
3501 case DW_OP_breg9:
3502 case DW_OP_breg10:
3503 case DW_OP_breg11:
3504 case DW_OP_breg12:
3505 case DW_OP_breg13:
3506 case DW_OP_breg14:
3507 case DW_OP_breg15:
3508 case DW_OP_breg16:
3509 case DW_OP_breg17:
3510 case DW_OP_breg18:
3511 case DW_OP_breg19:
3512 case DW_OP_breg20:
3513 case DW_OP_breg21:
3514 case DW_OP_breg22:
3515 case DW_OP_breg23:
3516 case DW_OP_breg24:
3517 case DW_OP_breg25:
3518 case DW_OP_breg26:
3519 case DW_OP_breg27:
3520 case DW_OP_breg28:
3521 case DW_OP_breg29:
3522 case DW_OP_breg30:
3523 case DW_OP_breg31:
3524 cfa->reg = op - DW_OP_breg0;
3525 cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
3526 break;
3527 case DW_OP_bregx:
3528 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3529 cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
3530 break;
3531 case DW_OP_deref:
3532 cfa->indirect = 1;
3533 break;
3534 case DW_OP_plus_uconst:
3535 cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
3536 break;
3537 default:
3538 internal_error ("DW_LOC_OP %s not implemented",
3539 dwarf_stack_op_name (ptr->dw_loc_opc));
3543 #endif /* .debug_frame support */
3545 /* And now, the support for symbolic debugging information. */
3546 #ifdef DWARF2_DEBUGGING_INFO
3548 /* .debug_str support. */
3549 static int output_indirect_string (void **, void *);
3551 static void dwarf2out_init (const char *);
3552 static void dwarf2out_finish (const char *);
3553 static void dwarf2out_define (unsigned int, const char *);
3554 static void dwarf2out_undef (unsigned int, const char *);
3555 static void dwarf2out_start_source_file (unsigned, const char *);
3556 static void dwarf2out_end_source_file (unsigned);
3557 static void dwarf2out_begin_block (unsigned, unsigned);
3558 static void dwarf2out_end_block (unsigned, unsigned);
3559 static bool dwarf2out_ignore_block (tree);
3560 static void dwarf2out_global_decl (tree);
3561 static void dwarf2out_type_decl (tree, int);
3562 static void dwarf2out_imported_module_or_decl (tree, tree);
3563 static void dwarf2out_abstract_function (tree);
3564 static void dwarf2out_var_location (rtx);
3565 static void dwarf2out_begin_function (tree);
3566 static void dwarf2out_switch_text_section (void);
3568 /* The debug hooks structure. */
3570 const struct gcc_debug_hooks dwarf2_debug_hooks =
3572 dwarf2out_init,
3573 dwarf2out_finish,
3574 dwarf2out_define,
3575 dwarf2out_undef,
3576 dwarf2out_start_source_file,
3577 dwarf2out_end_source_file,
3578 dwarf2out_begin_block,
3579 dwarf2out_end_block,
3580 dwarf2out_ignore_block,
3581 dwarf2out_source_line,
3582 dwarf2out_begin_prologue,
3583 debug_nothing_int_charstar, /* end_prologue */
3584 dwarf2out_end_epilogue,
3585 dwarf2out_begin_function,
3586 debug_nothing_int, /* end_function */
3587 dwarf2out_decl, /* function_decl */
3588 dwarf2out_global_decl,
3589 dwarf2out_type_decl, /* type_decl */
3590 dwarf2out_imported_module_or_decl,
3591 debug_nothing_tree, /* deferred_inline_function */
3592 /* The DWARF 2 backend tries to reduce debugging bloat by not
3593 emitting the abstract description of inline functions until
3594 something tries to reference them. */
3595 dwarf2out_abstract_function, /* outlining_inline_function */
3596 debug_nothing_rtx, /* label */
3597 debug_nothing_int, /* handle_pch */
3598 dwarf2out_var_location,
3599 dwarf2out_switch_text_section,
3600 1 /* start_end_main_source_file */
3602 #endif
3604 /* NOTE: In the comments in this file, many references are made to
3605 "Debugging Information Entries". This term is abbreviated as `DIE'
3606 throughout the remainder of this file. */
3608 /* An internal representation of the DWARF output is built, and then
3609 walked to generate the DWARF debugging info. The walk of the internal
3610 representation is done after the entire program has been compiled.
3611 The types below are used to describe the internal representation. */
3613 /* Various DIE's use offsets relative to the beginning of the
3614 .debug_info section to refer to each other. */
3616 typedef long int dw_offset;
3618 /* Define typedefs here to avoid circular dependencies. */
3620 typedef struct dw_attr_struct *dw_attr_ref;
3621 typedef struct dw_line_info_struct *dw_line_info_ref;
3622 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
3623 typedef struct pubname_struct *pubname_ref;
3624 typedef struct dw_ranges_struct *dw_ranges_ref;
3626 /* Each entry in the line_info_table maintains the file and
3627 line number associated with the label generated for that
3628 entry. The label gives the PC value associated with
3629 the line number entry. */
3631 typedef struct dw_line_info_struct GTY(())
3633 unsigned long dw_file_num;
3634 unsigned long dw_line_num;
3636 dw_line_info_entry;
3638 /* Line information for functions in separate sections; each one gets its
3639 own sequence. */
3640 typedef struct dw_separate_line_info_struct GTY(())
3642 unsigned long dw_file_num;
3643 unsigned long dw_line_num;
3644 unsigned long function;
3646 dw_separate_line_info_entry;
3648 /* Each DIE attribute has a field specifying the attribute kind,
3649 a link to the next attribute in the chain, and an attribute value.
3650 Attributes are typically linked below the DIE they modify. */
3652 typedef struct dw_attr_struct GTY(())
3654 enum dwarf_attribute dw_attr;
3655 dw_attr_ref dw_attr_next;
3656 dw_val_node dw_attr_val;
3658 dw_attr_node;
3660 /* The Debugging Information Entry (DIE) structure */
3662 typedef struct die_struct GTY(())
3664 enum dwarf_tag die_tag;
3665 char *die_symbol;
3666 dw_attr_ref die_attr;
3667 dw_die_ref die_parent;
3668 dw_die_ref die_child;
3669 dw_die_ref die_sib;
3670 dw_die_ref die_definition; /* ref from a specification to its definition */
3671 dw_offset die_offset;
3672 unsigned long die_abbrev;
3673 int die_mark;
3674 unsigned int decl_id;
3676 die_node;
3678 /* The pubname structure */
3680 typedef struct pubname_struct GTY(())
3682 dw_die_ref die;
3683 char *name;
3685 pubname_entry;
3687 struct dw_ranges_struct GTY(())
3689 int block_num;
3692 /* The limbo die list structure. */
3693 typedef struct limbo_die_struct GTY(())
3695 dw_die_ref die;
3696 tree created_for;
3697 struct limbo_die_struct *next;
3699 limbo_die_node;
3701 /* How to start an assembler comment. */
3702 #ifndef ASM_COMMENT_START
3703 #define ASM_COMMENT_START ";#"
3704 #endif
3706 /* Define a macro which returns nonzero for a TYPE_DECL which was
3707 implicitly generated for a tagged type.
3709 Note that unlike the gcc front end (which generates a NULL named
3710 TYPE_DECL node for each complete tagged type, each array type, and
3711 each function type node created) the g++ front end generates a
3712 _named_ TYPE_DECL node for each tagged type node created.
3713 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3714 generate a DW_TAG_typedef DIE for them. */
3716 #define TYPE_DECL_IS_STUB(decl) \
3717 (DECL_NAME (decl) == NULL_TREE \
3718 || (DECL_ARTIFICIAL (decl) \
3719 && is_tagged_type (TREE_TYPE (decl)) \
3720 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3721 /* This is necessary for stub decls that \
3722 appear in nested inline functions. */ \
3723 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3724 && (decl_ultimate_origin (decl) \
3725 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3727 /* Information concerning the compilation unit's programming
3728 language, and compiler version. */
3730 /* Fixed size portion of the DWARF compilation unit header. */
3731 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3732 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
3734 /* Fixed size portion of public names info. */
3735 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3737 /* Fixed size portion of the address range info. */
3738 #define DWARF_ARANGES_HEADER_SIZE \
3739 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3740 DWARF2_ADDR_SIZE * 2) \
3741 - DWARF_INITIAL_LENGTH_SIZE)
3743 /* Size of padding portion in the address range info. It must be
3744 aligned to twice the pointer size. */
3745 #define DWARF_ARANGES_PAD_SIZE \
3746 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3747 DWARF2_ADDR_SIZE * 2) \
3748 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3750 /* Use assembler line directives if available. */
3751 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3752 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3753 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3754 #else
3755 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3756 #endif
3757 #endif
3759 /* Minimum line offset in a special line info. opcode.
3760 This value was chosen to give a reasonable range of values. */
3761 #define DWARF_LINE_BASE -10
3763 /* First special line opcode - leave room for the standard opcodes. */
3764 #define DWARF_LINE_OPCODE_BASE 10
3766 /* Range of line offsets in a special line info. opcode. */
3767 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3769 /* Flag that indicates the initial value of the is_stmt_start flag.
3770 In the present implementation, we do not mark any lines as
3771 the beginning of a source statement, because that information
3772 is not made available by the GCC front-end. */
3773 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3775 #ifdef DWARF2_DEBUGGING_INFO
3776 /* This location is used by calc_die_sizes() to keep track
3777 the offset of each DIE within the .debug_info section. */
3778 static unsigned long next_die_offset;
3779 #endif
3781 /* Record the root of the DIE's built for the current compilation unit. */
3782 static GTY(()) dw_die_ref comp_unit_die;
3784 /* A list of DIEs with a NULL parent waiting to be relocated. */
3785 static GTY(()) limbo_die_node *limbo_die_list;
3787 /* Filenames referenced by this compilation unit. */
3788 static GTY(()) varray_type file_table;
3789 static GTY(()) varray_type file_table_emitted;
3790 static GTY(()) size_t file_table_last_lookup_index;
3792 /* A hash table of references to DIE's that describe declarations.
3793 The key is a DECL_UID() which is a unique number identifying each decl. */
3794 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
3796 /* Node of the variable location list. */
3797 struct var_loc_node GTY ((chain_next ("%h.next")))
3799 rtx GTY (()) var_loc_note;
3800 const char * GTY (()) label;
3801 const char * GTY (()) section_label;
3802 struct var_loc_node * GTY (()) next;
3805 /* Variable location list. */
3806 struct var_loc_list_def GTY (())
3808 struct var_loc_node * GTY (()) first;
3810 /* Do not mark the last element of the chained list because
3811 it is marked through the chain. */
3812 struct var_loc_node * GTY ((skip ("%h"))) last;
3814 /* DECL_UID of the variable decl. */
3815 unsigned int decl_id;
3817 typedef struct var_loc_list_def var_loc_list;
3820 /* Table of decl location linked lists. */
3821 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
3823 /* A pointer to the base of a list of references to DIE's that
3824 are uniquely identified by their tag, presence/absence of
3825 children DIE's, and list of attribute/value pairs. */
3826 static GTY((length ("abbrev_die_table_allocated")))
3827 dw_die_ref *abbrev_die_table;
3829 /* Number of elements currently allocated for abbrev_die_table. */
3830 static GTY(()) unsigned abbrev_die_table_allocated;
3832 /* Number of elements in type_die_table currently in use. */
3833 static GTY(()) unsigned abbrev_die_table_in_use;
3835 /* Size (in elements) of increments by which we may expand the
3836 abbrev_die_table. */
3837 #define ABBREV_DIE_TABLE_INCREMENT 256
3839 /* A pointer to the base of a table that contains line information
3840 for each source code line in .text in the compilation unit. */
3841 static GTY((length ("line_info_table_allocated")))
3842 dw_line_info_ref line_info_table;
3844 /* Number of elements currently allocated for line_info_table. */
3845 static GTY(()) unsigned line_info_table_allocated;
3847 /* Number of elements in line_info_table currently in use. */
3848 static GTY(()) unsigned line_info_table_in_use;
3850 /* True if the compilation unit places functions in more than one section. */
3851 static GTY(()) bool have_multiple_function_sections = false;
3853 /* A pointer to the base of a table that contains line information
3854 for each source code line outside of .text in the compilation unit. */
3855 static GTY ((length ("separate_line_info_table_allocated")))
3856 dw_separate_line_info_ref separate_line_info_table;
3858 /* Number of elements currently allocated for separate_line_info_table. */
3859 static GTY(()) unsigned separate_line_info_table_allocated;
3861 /* Number of elements in separate_line_info_table currently in use. */
3862 static GTY(()) unsigned separate_line_info_table_in_use;
3864 /* Size (in elements) of increments by which we may expand the
3865 line_info_table. */
3866 #define LINE_INFO_TABLE_INCREMENT 1024
3868 /* A pointer to the base of a table that contains a list of publicly
3869 accessible names. */
3870 static GTY ((length ("pubname_table_allocated"))) pubname_ref pubname_table;
3872 /* Number of elements currently allocated for pubname_table. */
3873 static GTY(()) unsigned pubname_table_allocated;
3875 /* Number of elements in pubname_table currently in use. */
3876 static GTY(()) unsigned pubname_table_in_use;
3878 /* Size (in elements) of increments by which we may expand the
3879 pubname_table. */
3880 #define PUBNAME_TABLE_INCREMENT 64
3882 /* Array of dies for which we should generate .debug_arange info. */
3883 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
3885 /* Number of elements currently allocated for arange_table. */
3886 static GTY(()) unsigned arange_table_allocated;
3888 /* Number of elements in arange_table currently in use. */
3889 static GTY(()) unsigned arange_table_in_use;
3891 /* Size (in elements) of increments by which we may expand the
3892 arange_table. */
3893 #define ARANGE_TABLE_INCREMENT 64
3895 /* Array of dies for which we should generate .debug_ranges info. */
3896 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
3898 /* Number of elements currently allocated for ranges_table. */
3899 static GTY(()) unsigned ranges_table_allocated;
3901 /* Number of elements in ranges_table currently in use. */
3902 static GTY(()) unsigned ranges_table_in_use;
3904 /* Size (in elements) of increments by which we may expand the
3905 ranges_table. */
3906 #define RANGES_TABLE_INCREMENT 64
3908 /* Whether we have location lists that need outputting */
3909 static GTY(()) bool have_location_lists;
3911 /* Unique label counter. */
3912 static GTY(()) unsigned int loclabel_num;
3914 #ifdef DWARF2_DEBUGGING_INFO
3915 /* Record whether the function being analyzed contains inlined functions. */
3916 static int current_function_has_inlines;
3917 #endif
3918 #if 0 && defined (MIPS_DEBUGGING_INFO)
3919 static int comp_unit_has_inlines;
3920 #endif
3922 /* Number of file tables emitted in maybe_emit_file(). */
3923 static GTY(()) int emitcount = 0;
3925 /* Number of internal labels generated by gen_internal_sym(). */
3926 static GTY(()) int label_num;
3928 #ifdef DWARF2_DEBUGGING_INFO
3930 /* Offset from the "steady-state frame pointer" to the frame base,
3931 within the current function. */
3932 static HOST_WIDE_INT frame_pointer_fb_offset;
3934 /* Forward declarations for functions defined in this file. */
3936 static int is_pseudo_reg (rtx);
3937 static tree type_main_variant (tree);
3938 static int is_tagged_type (tree);
3939 static const char *dwarf_tag_name (unsigned);
3940 static const char *dwarf_attr_name (unsigned);
3941 static const char *dwarf_form_name (unsigned);
3942 static tree decl_ultimate_origin (tree);
3943 static tree block_ultimate_origin (tree);
3944 static tree decl_class_context (tree);
3945 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
3946 static inline enum dw_val_class AT_class (dw_attr_ref);
3947 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3948 static inline unsigned AT_flag (dw_attr_ref);
3949 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3950 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3951 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3952 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3953 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
3954 unsigned long);
3955 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3956 unsigned int, unsigned char *);
3957 static hashval_t debug_str_do_hash (const void *);
3958 static int debug_str_eq (const void *, const void *);
3959 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3960 static inline const char *AT_string (dw_attr_ref);
3961 static int AT_string_form (dw_attr_ref);
3962 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3963 static void add_AT_specification (dw_die_ref, dw_die_ref);
3964 static inline dw_die_ref AT_ref (dw_attr_ref);
3965 static inline int AT_ref_external (dw_attr_ref);
3966 static inline void set_AT_ref_external (dw_attr_ref, int);
3967 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3968 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3969 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3970 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3971 dw_loc_list_ref);
3972 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3973 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
3974 static inline rtx AT_addr (dw_attr_ref);
3975 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3976 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3977 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3978 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3979 unsigned HOST_WIDE_INT);
3980 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3981 unsigned long);
3982 static inline const char *AT_lbl (dw_attr_ref);
3983 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3984 static const char *get_AT_low_pc (dw_die_ref);
3985 static const char *get_AT_hi_pc (dw_die_ref);
3986 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3987 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3988 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3989 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3990 static bool is_c_family (void);
3991 static bool is_cxx (void);
3992 static bool is_java (void);
3993 static bool is_fortran (void);
3994 static bool is_ada (void);
3995 static void remove_AT (dw_die_ref, enum dwarf_attribute);
3996 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3997 static inline void free_die (dw_die_ref);
3998 static void remove_children (dw_die_ref);
3999 static void add_child_die (dw_die_ref, dw_die_ref);
4000 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
4001 static dw_die_ref lookup_type_die (tree);
4002 static void equate_type_number_to_die (tree, dw_die_ref);
4003 static hashval_t decl_die_table_hash (const void *);
4004 static int decl_die_table_eq (const void *, const void *);
4005 static dw_die_ref lookup_decl_die (tree);
4006 static hashval_t decl_loc_table_hash (const void *);
4007 static int decl_loc_table_eq (const void *, const void *);
4008 static var_loc_list *lookup_decl_loc (tree);
4009 static void equate_decl_number_to_die (tree, dw_die_ref);
4010 static void add_var_loc_to_decl (tree, struct var_loc_node *);
4011 static void print_spaces (FILE *);
4012 static void print_die (dw_die_ref, FILE *);
4013 static void print_dwarf_line_table (FILE *);
4014 static void reverse_die_lists (dw_die_ref);
4015 static void reverse_all_dies (dw_die_ref);
4016 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
4017 static dw_die_ref pop_compile_unit (dw_die_ref);
4018 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
4019 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
4020 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
4021 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
4022 static int same_dw_val_p (dw_val_node *, dw_val_node *, int *);
4023 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
4024 static int same_die_p (dw_die_ref, dw_die_ref, int *);
4025 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
4026 static void compute_section_prefix (dw_die_ref);
4027 static int is_type_die (dw_die_ref);
4028 static int is_comdat_die (dw_die_ref);
4029 static int is_symbol_die (dw_die_ref);
4030 static void assign_symbol_names (dw_die_ref);
4031 static void break_out_includes (dw_die_ref);
4032 static hashval_t htab_cu_hash (const void *);
4033 static int htab_cu_eq (const void *, const void *);
4034 static void htab_cu_del (void *);
4035 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
4036 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
4037 static void add_sibling_attributes (dw_die_ref);
4038 static void build_abbrev_table (dw_die_ref);
4039 static void output_location_lists (dw_die_ref);
4040 static int constant_size (long unsigned);
4041 static unsigned long size_of_die (dw_die_ref);
4042 static void calc_die_sizes (dw_die_ref);
4043 static void mark_dies (dw_die_ref);
4044 static void unmark_dies (dw_die_ref);
4045 static void unmark_all_dies (dw_die_ref);
4046 static unsigned long size_of_pubnames (void);
4047 static unsigned long size_of_aranges (void);
4048 static enum dwarf_form value_format (dw_attr_ref);
4049 static void output_value_format (dw_attr_ref);
4050 static void output_abbrev_section (void);
4051 static void output_die_symbol (dw_die_ref);
4052 static void output_die (dw_die_ref);
4053 static void output_compilation_unit_header (void);
4054 static void output_comp_unit (dw_die_ref, int);
4055 static const char *dwarf2_name (tree, int);
4056 static void add_pubname (tree, dw_die_ref);
4057 static void output_pubnames (void);
4058 static void add_arange (tree, dw_die_ref);
4059 static void output_aranges (void);
4060 static unsigned int add_ranges (tree);
4061 static void output_ranges (void);
4062 static void output_line_info (void);
4063 static void output_file_names (void);
4064 static dw_die_ref base_type_die (tree);
4065 static tree root_type (tree);
4066 static int is_base_type (tree);
4067 static bool is_subrange_type (tree);
4068 static dw_die_ref subrange_type_die (tree, dw_die_ref);
4069 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
4070 static int type_is_enum (tree);
4071 static unsigned int dbx_reg_number (rtx);
4072 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
4073 static dw_loc_descr_ref reg_loc_descriptor (rtx);
4074 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int);
4075 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx);
4076 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4077 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT);
4078 static int is_based_loc (rtx);
4079 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode);
4080 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx);
4081 static dw_loc_descr_ref loc_descriptor (rtx);
4082 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
4083 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
4084 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
4085 static tree field_type (tree);
4086 static unsigned int simple_type_align_in_bits (tree);
4087 static unsigned int simple_decl_align_in_bits (tree);
4088 static unsigned HOST_WIDE_INT simple_type_size_in_bits (tree);
4089 static HOST_WIDE_INT field_byte_offset (tree);
4090 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
4091 dw_loc_descr_ref);
4092 static void add_data_member_location_attribute (dw_die_ref, tree);
4093 static void add_const_value_attribute (dw_die_ref, rtx);
4094 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
4095 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4096 static void insert_float (rtx, unsigned char *);
4097 static rtx rtl_for_decl_location (tree);
4098 static void add_location_or_const_value_attribute (dw_die_ref, tree,
4099 enum dwarf_attribute);
4100 static void tree_add_const_value_attribute (dw_die_ref, tree);
4101 static void add_name_attribute (dw_die_ref, const char *);
4102 static void add_comp_dir_attribute (dw_die_ref);
4103 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
4104 static void add_subscript_info (dw_die_ref, tree);
4105 static void add_byte_size_attribute (dw_die_ref, tree);
4106 static void add_bit_offset_attribute (dw_die_ref, tree);
4107 static void add_bit_size_attribute (dw_die_ref, tree);
4108 static void add_prototyped_attribute (dw_die_ref, tree);
4109 static void add_abstract_origin_attribute (dw_die_ref, tree);
4110 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
4111 static void add_src_coords_attributes (dw_die_ref, tree);
4112 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
4113 static void push_decl_scope (tree);
4114 static void pop_decl_scope (void);
4115 static dw_die_ref scope_die_for (tree, dw_die_ref);
4116 static inline int local_scope_p (dw_die_ref);
4117 static inline int class_or_namespace_scope_p (dw_die_ref);
4118 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
4119 static void add_calling_convention_attribute (dw_die_ref, tree);
4120 static const char *type_tag (tree);
4121 static tree member_declared_type (tree);
4122 #if 0
4123 static const char *decl_start_label (tree);
4124 #endif
4125 static void gen_array_type_die (tree, dw_die_ref);
4126 #if 0
4127 static void gen_entry_point_die (tree, dw_die_ref);
4128 #endif
4129 static void gen_inlined_enumeration_type_die (tree, dw_die_ref);
4130 static void gen_inlined_structure_type_die (tree, dw_die_ref);
4131 static void gen_inlined_union_type_die (tree, dw_die_ref);
4132 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
4133 static dw_die_ref gen_formal_parameter_die (tree, dw_die_ref);
4134 static void gen_unspecified_parameters_die (tree, dw_die_ref);
4135 static void gen_formal_types_die (tree, dw_die_ref);
4136 static void gen_subprogram_die (tree, dw_die_ref);
4137 static void gen_variable_die (tree, dw_die_ref);
4138 static void gen_label_die (tree, dw_die_ref);
4139 static void gen_lexical_block_die (tree, dw_die_ref, int);
4140 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
4141 static void gen_field_die (tree, dw_die_ref);
4142 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
4143 static dw_die_ref gen_compile_unit_die (const char *);
4144 static void gen_inheritance_die (tree, tree, dw_die_ref);
4145 static void gen_member_die (tree, dw_die_ref);
4146 static void gen_struct_or_union_type_die (tree, dw_die_ref);
4147 static void gen_subroutine_type_die (tree, dw_die_ref);
4148 static void gen_typedef_die (tree, dw_die_ref);
4149 static void gen_type_die (tree, dw_die_ref);
4150 static void gen_tagged_type_instantiation_die (tree, dw_die_ref);
4151 static void gen_block_die (tree, dw_die_ref, int);
4152 static void decls_for_scope (tree, dw_die_ref, int);
4153 static int is_redundant_typedef (tree);
4154 static void gen_namespace_die (tree);
4155 static void gen_decl_die (tree, dw_die_ref);
4156 static dw_die_ref force_decl_die (tree);
4157 static dw_die_ref force_type_die (tree);
4158 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
4159 static void declare_in_namespace (tree, dw_die_ref);
4160 static unsigned lookup_filename (const char *);
4161 static void init_file_table (void);
4162 static void retry_incomplete_types (void);
4163 static void gen_type_die_for_member (tree, tree, dw_die_ref);
4164 static void splice_child_die (dw_die_ref, dw_die_ref);
4165 static int file_info_cmp (const void *, const void *);
4166 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
4167 const char *, const char *, unsigned);
4168 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
4169 const char *, const char *,
4170 const char *);
4171 static void output_loc_list (dw_loc_list_ref);
4172 static char *gen_internal_sym (const char *);
4174 static void prune_unmark_dies (dw_die_ref);
4175 static void prune_unused_types_mark (dw_die_ref, int);
4176 static void prune_unused_types_walk (dw_die_ref);
4177 static void prune_unused_types_walk_attribs (dw_die_ref);
4178 static void prune_unused_types_prune (dw_die_ref);
4179 static void prune_unused_types (void);
4180 static int maybe_emit_file (int);
4182 /* Section names used to hold DWARF debugging information. */
4183 #ifndef DEBUG_INFO_SECTION
4184 #define DEBUG_INFO_SECTION ".debug_info"
4185 #endif
4186 #ifndef DEBUG_ABBREV_SECTION
4187 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
4188 #endif
4189 #ifndef DEBUG_ARANGES_SECTION
4190 #define DEBUG_ARANGES_SECTION ".debug_aranges"
4191 #endif
4192 #ifndef DEBUG_MACINFO_SECTION
4193 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
4194 #endif
4195 #ifndef DEBUG_LINE_SECTION
4196 #define DEBUG_LINE_SECTION ".debug_line"
4197 #endif
4198 #ifndef DEBUG_LOC_SECTION
4199 #define DEBUG_LOC_SECTION ".debug_loc"
4200 #endif
4201 #ifndef DEBUG_PUBNAMES_SECTION
4202 #define DEBUG_PUBNAMES_SECTION ".debug_pubnames"
4203 #endif
4204 #ifndef DEBUG_STR_SECTION
4205 #define DEBUG_STR_SECTION ".debug_str"
4206 #endif
4207 #ifndef DEBUG_RANGES_SECTION
4208 #define DEBUG_RANGES_SECTION ".debug_ranges"
4209 #endif
4211 /* Standard ELF section names for compiled code and data. */
4212 #ifndef TEXT_SECTION_NAME
4213 #define TEXT_SECTION_NAME ".text"
4214 #endif
4216 /* Section flags for .debug_str section. */
4217 #define DEBUG_STR_SECTION_FLAGS \
4218 (HAVE_GAS_SHF_MERGE && flag_merge_constants \
4219 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
4220 : SECTION_DEBUG)
4222 /* Labels we insert at beginning sections we can reference instead of
4223 the section names themselves. */
4225 #ifndef TEXT_SECTION_LABEL
4226 #define TEXT_SECTION_LABEL "Ltext"
4227 #endif
4228 #ifndef COLD_TEXT_SECTION_LABEL
4229 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
4230 #endif
4231 #ifndef DEBUG_LINE_SECTION_LABEL
4232 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
4233 #endif
4234 #ifndef DEBUG_INFO_SECTION_LABEL
4235 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
4236 #endif
4237 #ifndef DEBUG_ABBREV_SECTION_LABEL
4238 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
4239 #endif
4240 #ifndef DEBUG_LOC_SECTION_LABEL
4241 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
4242 #endif
4243 #ifndef DEBUG_RANGES_SECTION_LABEL
4244 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
4245 #endif
4246 #ifndef DEBUG_MACINFO_SECTION_LABEL
4247 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
4248 #endif
4250 /* Definitions of defaults for formats and names of various special
4251 (artificial) labels which may be generated within this file (when the -g
4252 options is used and DWARF2_DEBUGGING_INFO is in effect.
4253 If necessary, these may be overridden from within the tm.h file, but
4254 typically, overriding these defaults is unnecessary. */
4256 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4257 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4258 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4259 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4260 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4261 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4262 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4263 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4264 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4265 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4267 #ifndef TEXT_END_LABEL
4268 #define TEXT_END_LABEL "Letext"
4269 #endif
4270 #ifndef COLD_END_LABEL
4271 #define COLD_END_LABEL "Letext_cold"
4272 #endif
4273 #ifndef BLOCK_BEGIN_LABEL
4274 #define BLOCK_BEGIN_LABEL "LBB"
4275 #endif
4276 #ifndef BLOCK_END_LABEL
4277 #define BLOCK_END_LABEL "LBE"
4278 #endif
4279 #ifndef LINE_CODE_LABEL
4280 #define LINE_CODE_LABEL "LM"
4281 #endif
4282 #ifndef SEPARATE_LINE_CODE_LABEL
4283 #define SEPARATE_LINE_CODE_LABEL "LSM"
4284 #endif
4286 /* We allow a language front-end to designate a function that is to be
4287 called to "demangle" any name before it is put into a DIE. */
4289 static const char *(*demangle_name_func) (const char *);
4291 void
4292 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4294 demangle_name_func = func;
4297 /* Test if rtl node points to a pseudo register. */
4299 static inline int
4300 is_pseudo_reg (rtx rtl)
4302 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4303 || (GET_CODE (rtl) == SUBREG
4304 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4307 /* Return a reference to a type, with its const and volatile qualifiers
4308 removed. */
4310 static inline tree
4311 type_main_variant (tree type)
4313 type = TYPE_MAIN_VARIANT (type);
4315 /* ??? There really should be only one main variant among any group of
4316 variants of a given type (and all of the MAIN_VARIANT values for all
4317 members of the group should point to that one type) but sometimes the C
4318 front-end messes this up for array types, so we work around that bug
4319 here. */
4320 if (TREE_CODE (type) == ARRAY_TYPE)
4321 while (type != TYPE_MAIN_VARIANT (type))
4322 type = TYPE_MAIN_VARIANT (type);
4324 return type;
4327 /* Return nonzero if the given type node represents a tagged type. */
4329 static inline int
4330 is_tagged_type (tree type)
4332 enum tree_code code = TREE_CODE (type);
4334 return (code == RECORD_TYPE || code == UNION_TYPE
4335 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4338 /* Convert a DIE tag into its string name. */
4340 static const char *
4341 dwarf_tag_name (unsigned int tag)
4343 switch (tag)
4345 case DW_TAG_padding:
4346 return "DW_TAG_padding";
4347 case DW_TAG_array_type:
4348 return "DW_TAG_array_type";
4349 case DW_TAG_class_type:
4350 return "DW_TAG_class_type";
4351 case DW_TAG_entry_point:
4352 return "DW_TAG_entry_point";
4353 case DW_TAG_enumeration_type:
4354 return "DW_TAG_enumeration_type";
4355 case DW_TAG_formal_parameter:
4356 return "DW_TAG_formal_parameter";
4357 case DW_TAG_imported_declaration:
4358 return "DW_TAG_imported_declaration";
4359 case DW_TAG_label:
4360 return "DW_TAG_label";
4361 case DW_TAG_lexical_block:
4362 return "DW_TAG_lexical_block";
4363 case DW_TAG_member:
4364 return "DW_TAG_member";
4365 case DW_TAG_pointer_type:
4366 return "DW_TAG_pointer_type";
4367 case DW_TAG_reference_type:
4368 return "DW_TAG_reference_type";
4369 case DW_TAG_compile_unit:
4370 return "DW_TAG_compile_unit";
4371 case DW_TAG_string_type:
4372 return "DW_TAG_string_type";
4373 case DW_TAG_structure_type:
4374 return "DW_TAG_structure_type";
4375 case DW_TAG_subroutine_type:
4376 return "DW_TAG_subroutine_type";
4377 case DW_TAG_typedef:
4378 return "DW_TAG_typedef";
4379 case DW_TAG_union_type:
4380 return "DW_TAG_union_type";
4381 case DW_TAG_unspecified_parameters:
4382 return "DW_TAG_unspecified_parameters";
4383 case DW_TAG_variant:
4384 return "DW_TAG_variant";
4385 case DW_TAG_common_block:
4386 return "DW_TAG_common_block";
4387 case DW_TAG_common_inclusion:
4388 return "DW_TAG_common_inclusion";
4389 case DW_TAG_inheritance:
4390 return "DW_TAG_inheritance";
4391 case DW_TAG_inlined_subroutine:
4392 return "DW_TAG_inlined_subroutine";
4393 case DW_TAG_module:
4394 return "DW_TAG_module";
4395 case DW_TAG_ptr_to_member_type:
4396 return "DW_TAG_ptr_to_member_type";
4397 case DW_TAG_set_type:
4398 return "DW_TAG_set_type";
4399 case DW_TAG_subrange_type:
4400 return "DW_TAG_subrange_type";
4401 case DW_TAG_with_stmt:
4402 return "DW_TAG_with_stmt";
4403 case DW_TAG_access_declaration:
4404 return "DW_TAG_access_declaration";
4405 case DW_TAG_base_type:
4406 return "DW_TAG_base_type";
4407 case DW_TAG_catch_block:
4408 return "DW_TAG_catch_block";
4409 case DW_TAG_const_type:
4410 return "DW_TAG_const_type";
4411 case DW_TAG_constant:
4412 return "DW_TAG_constant";
4413 case DW_TAG_enumerator:
4414 return "DW_TAG_enumerator";
4415 case DW_TAG_file_type:
4416 return "DW_TAG_file_type";
4417 case DW_TAG_friend:
4418 return "DW_TAG_friend";
4419 case DW_TAG_namelist:
4420 return "DW_TAG_namelist";
4421 case DW_TAG_namelist_item:
4422 return "DW_TAG_namelist_item";
4423 case DW_TAG_namespace:
4424 return "DW_TAG_namespace";
4425 case DW_TAG_packed_type:
4426 return "DW_TAG_packed_type";
4427 case DW_TAG_subprogram:
4428 return "DW_TAG_subprogram";
4429 case DW_TAG_template_type_param:
4430 return "DW_TAG_template_type_param";
4431 case DW_TAG_template_value_param:
4432 return "DW_TAG_template_value_param";
4433 case DW_TAG_thrown_type:
4434 return "DW_TAG_thrown_type";
4435 case DW_TAG_try_block:
4436 return "DW_TAG_try_block";
4437 case DW_TAG_variant_part:
4438 return "DW_TAG_variant_part";
4439 case DW_TAG_variable:
4440 return "DW_TAG_variable";
4441 case DW_TAG_volatile_type:
4442 return "DW_TAG_volatile_type";
4443 case DW_TAG_imported_module:
4444 return "DW_TAG_imported_module";
4445 case DW_TAG_MIPS_loop:
4446 return "DW_TAG_MIPS_loop";
4447 case DW_TAG_format_label:
4448 return "DW_TAG_format_label";
4449 case DW_TAG_function_template:
4450 return "DW_TAG_function_template";
4451 case DW_TAG_class_template:
4452 return "DW_TAG_class_template";
4453 case DW_TAG_GNU_BINCL:
4454 return "DW_TAG_GNU_BINCL";
4455 case DW_TAG_GNU_EINCL:
4456 return "DW_TAG_GNU_EINCL";
4457 default:
4458 return "DW_TAG_<unknown>";
4462 /* Convert a DWARF attribute code into its string name. */
4464 static const char *
4465 dwarf_attr_name (unsigned int attr)
4467 switch (attr)
4469 case DW_AT_sibling:
4470 return "DW_AT_sibling";
4471 case DW_AT_location:
4472 return "DW_AT_location";
4473 case DW_AT_name:
4474 return "DW_AT_name";
4475 case DW_AT_ordering:
4476 return "DW_AT_ordering";
4477 case DW_AT_subscr_data:
4478 return "DW_AT_subscr_data";
4479 case DW_AT_byte_size:
4480 return "DW_AT_byte_size";
4481 case DW_AT_bit_offset:
4482 return "DW_AT_bit_offset";
4483 case DW_AT_bit_size:
4484 return "DW_AT_bit_size";
4485 case DW_AT_element_list:
4486 return "DW_AT_element_list";
4487 case DW_AT_stmt_list:
4488 return "DW_AT_stmt_list";
4489 case DW_AT_low_pc:
4490 return "DW_AT_low_pc";
4491 case DW_AT_high_pc:
4492 return "DW_AT_high_pc";
4493 case DW_AT_language:
4494 return "DW_AT_language";
4495 case DW_AT_member:
4496 return "DW_AT_member";
4497 case DW_AT_discr:
4498 return "DW_AT_discr";
4499 case DW_AT_discr_value:
4500 return "DW_AT_discr_value";
4501 case DW_AT_visibility:
4502 return "DW_AT_visibility";
4503 case DW_AT_import:
4504 return "DW_AT_import";
4505 case DW_AT_string_length:
4506 return "DW_AT_string_length";
4507 case DW_AT_common_reference:
4508 return "DW_AT_common_reference";
4509 case DW_AT_comp_dir:
4510 return "DW_AT_comp_dir";
4511 case DW_AT_const_value:
4512 return "DW_AT_const_value";
4513 case DW_AT_containing_type:
4514 return "DW_AT_containing_type";
4515 case DW_AT_default_value:
4516 return "DW_AT_default_value";
4517 case DW_AT_inline:
4518 return "DW_AT_inline";
4519 case DW_AT_is_optional:
4520 return "DW_AT_is_optional";
4521 case DW_AT_lower_bound:
4522 return "DW_AT_lower_bound";
4523 case DW_AT_producer:
4524 return "DW_AT_producer";
4525 case DW_AT_prototyped:
4526 return "DW_AT_prototyped";
4527 case DW_AT_return_addr:
4528 return "DW_AT_return_addr";
4529 case DW_AT_start_scope:
4530 return "DW_AT_start_scope";
4531 case DW_AT_stride_size:
4532 return "DW_AT_stride_size";
4533 case DW_AT_upper_bound:
4534 return "DW_AT_upper_bound";
4535 case DW_AT_abstract_origin:
4536 return "DW_AT_abstract_origin";
4537 case DW_AT_accessibility:
4538 return "DW_AT_accessibility";
4539 case DW_AT_address_class:
4540 return "DW_AT_address_class";
4541 case DW_AT_artificial:
4542 return "DW_AT_artificial";
4543 case DW_AT_base_types:
4544 return "DW_AT_base_types";
4545 case DW_AT_calling_convention:
4546 return "DW_AT_calling_convention";
4547 case DW_AT_count:
4548 return "DW_AT_count";
4549 case DW_AT_data_member_location:
4550 return "DW_AT_data_member_location";
4551 case DW_AT_decl_column:
4552 return "DW_AT_decl_column";
4553 case DW_AT_decl_file:
4554 return "DW_AT_decl_file";
4555 case DW_AT_decl_line:
4556 return "DW_AT_decl_line";
4557 case DW_AT_declaration:
4558 return "DW_AT_declaration";
4559 case DW_AT_discr_list:
4560 return "DW_AT_discr_list";
4561 case DW_AT_encoding:
4562 return "DW_AT_encoding";
4563 case DW_AT_external:
4564 return "DW_AT_external";
4565 case DW_AT_frame_base:
4566 return "DW_AT_frame_base";
4567 case DW_AT_friend:
4568 return "DW_AT_friend";
4569 case DW_AT_identifier_case:
4570 return "DW_AT_identifier_case";
4571 case DW_AT_macro_info:
4572 return "DW_AT_macro_info";
4573 case DW_AT_namelist_items:
4574 return "DW_AT_namelist_items";
4575 case DW_AT_priority:
4576 return "DW_AT_priority";
4577 case DW_AT_segment:
4578 return "DW_AT_segment";
4579 case DW_AT_specification:
4580 return "DW_AT_specification";
4581 case DW_AT_static_link:
4582 return "DW_AT_static_link";
4583 case DW_AT_type:
4584 return "DW_AT_type";
4585 case DW_AT_use_location:
4586 return "DW_AT_use_location";
4587 case DW_AT_variable_parameter:
4588 return "DW_AT_variable_parameter";
4589 case DW_AT_virtuality:
4590 return "DW_AT_virtuality";
4591 case DW_AT_vtable_elem_location:
4592 return "DW_AT_vtable_elem_location";
4594 case DW_AT_allocated:
4595 return "DW_AT_allocated";
4596 case DW_AT_associated:
4597 return "DW_AT_associated";
4598 case DW_AT_data_location:
4599 return "DW_AT_data_location";
4600 case DW_AT_stride:
4601 return "DW_AT_stride";
4602 case DW_AT_entry_pc:
4603 return "DW_AT_entry_pc";
4604 case DW_AT_use_UTF8:
4605 return "DW_AT_use_UTF8";
4606 case DW_AT_extension:
4607 return "DW_AT_extension";
4608 case DW_AT_ranges:
4609 return "DW_AT_ranges";
4610 case DW_AT_trampoline:
4611 return "DW_AT_trampoline";
4612 case DW_AT_call_column:
4613 return "DW_AT_call_column";
4614 case DW_AT_call_file:
4615 return "DW_AT_call_file";
4616 case DW_AT_call_line:
4617 return "DW_AT_call_line";
4619 case DW_AT_MIPS_fde:
4620 return "DW_AT_MIPS_fde";
4621 case DW_AT_MIPS_loop_begin:
4622 return "DW_AT_MIPS_loop_begin";
4623 case DW_AT_MIPS_tail_loop_begin:
4624 return "DW_AT_MIPS_tail_loop_begin";
4625 case DW_AT_MIPS_epilog_begin:
4626 return "DW_AT_MIPS_epilog_begin";
4627 case DW_AT_MIPS_loop_unroll_factor:
4628 return "DW_AT_MIPS_loop_unroll_factor";
4629 case DW_AT_MIPS_software_pipeline_depth:
4630 return "DW_AT_MIPS_software_pipeline_depth";
4631 case DW_AT_MIPS_linkage_name:
4632 return "DW_AT_MIPS_linkage_name";
4633 case DW_AT_MIPS_stride:
4634 return "DW_AT_MIPS_stride";
4635 case DW_AT_MIPS_abstract_name:
4636 return "DW_AT_MIPS_abstract_name";
4637 case DW_AT_MIPS_clone_origin:
4638 return "DW_AT_MIPS_clone_origin";
4639 case DW_AT_MIPS_has_inlines:
4640 return "DW_AT_MIPS_has_inlines";
4642 case DW_AT_sf_names:
4643 return "DW_AT_sf_names";
4644 case DW_AT_src_info:
4645 return "DW_AT_src_info";
4646 case DW_AT_mac_info:
4647 return "DW_AT_mac_info";
4648 case DW_AT_src_coords:
4649 return "DW_AT_src_coords";
4650 case DW_AT_body_begin:
4651 return "DW_AT_body_begin";
4652 case DW_AT_body_end:
4653 return "DW_AT_body_end";
4654 case DW_AT_GNU_vector:
4655 return "DW_AT_GNU_vector";
4657 case DW_AT_VMS_rtnbeg_pd_address:
4658 return "DW_AT_VMS_rtnbeg_pd_address";
4660 default:
4661 return "DW_AT_<unknown>";
4665 /* Convert a DWARF value form code into its string name. */
4667 static const char *
4668 dwarf_form_name (unsigned int form)
4670 switch (form)
4672 case DW_FORM_addr:
4673 return "DW_FORM_addr";
4674 case DW_FORM_block2:
4675 return "DW_FORM_block2";
4676 case DW_FORM_block4:
4677 return "DW_FORM_block4";
4678 case DW_FORM_data2:
4679 return "DW_FORM_data2";
4680 case DW_FORM_data4:
4681 return "DW_FORM_data4";
4682 case DW_FORM_data8:
4683 return "DW_FORM_data8";
4684 case DW_FORM_string:
4685 return "DW_FORM_string";
4686 case DW_FORM_block:
4687 return "DW_FORM_block";
4688 case DW_FORM_block1:
4689 return "DW_FORM_block1";
4690 case DW_FORM_data1:
4691 return "DW_FORM_data1";
4692 case DW_FORM_flag:
4693 return "DW_FORM_flag";
4694 case DW_FORM_sdata:
4695 return "DW_FORM_sdata";
4696 case DW_FORM_strp:
4697 return "DW_FORM_strp";
4698 case DW_FORM_udata:
4699 return "DW_FORM_udata";
4700 case DW_FORM_ref_addr:
4701 return "DW_FORM_ref_addr";
4702 case DW_FORM_ref1:
4703 return "DW_FORM_ref1";
4704 case DW_FORM_ref2:
4705 return "DW_FORM_ref2";
4706 case DW_FORM_ref4:
4707 return "DW_FORM_ref4";
4708 case DW_FORM_ref8:
4709 return "DW_FORM_ref8";
4710 case DW_FORM_ref_udata:
4711 return "DW_FORM_ref_udata";
4712 case DW_FORM_indirect:
4713 return "DW_FORM_indirect";
4714 default:
4715 return "DW_FORM_<unknown>";
4719 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4720 instance of an inlined instance of a decl which is local to an inline
4721 function, so we have to trace all of the way back through the origin chain
4722 to find out what sort of node actually served as the original seed for the
4723 given block. */
4725 static tree
4726 decl_ultimate_origin (tree decl)
4728 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4729 return NULL_TREE;
4731 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4732 nodes in the function to point to themselves; ignore that if
4733 we're trying to output the abstract instance of this function. */
4734 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4735 return NULL_TREE;
4737 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4738 most distant ancestor, this should never happen. */
4739 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4741 return DECL_ABSTRACT_ORIGIN (decl);
4744 /* Determine the "ultimate origin" of a block. The block may be an inlined
4745 instance of an inlined instance of a block which is local to an inline
4746 function, so we have to trace all of the way back through the origin chain
4747 to find out what sort of node actually served as the original seed for the
4748 given block. */
4750 static tree
4751 block_ultimate_origin (tree block)
4753 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
4755 /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
4756 nodes in the function to point to themselves; ignore that if
4757 we're trying to output the abstract instance of this function. */
4758 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
4759 return NULL_TREE;
4761 if (immediate_origin == NULL_TREE)
4762 return NULL_TREE;
4763 else
4765 tree ret_val;
4766 tree lookahead = immediate_origin;
4770 ret_val = lookahead;
4771 lookahead = (TREE_CODE (ret_val) == BLOCK
4772 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
4774 while (lookahead != NULL && lookahead != ret_val);
4776 /* The block's abstract origin chain may not be the *ultimate* origin of
4777 the block. It could lead to a DECL that has an abstract origin set.
4778 If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
4779 will give us if it has one). Note that DECL's abstract origins are
4780 supposed to be the most distant ancestor (or so decl_ultimate_origin
4781 claims), so we don't need to loop following the DECL origins. */
4782 if (DECL_P (ret_val))
4783 return DECL_ORIGIN (ret_val);
4785 return ret_val;
4789 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4790 of a virtual function may refer to a base class, so we check the 'this'
4791 parameter. */
4793 static tree
4794 decl_class_context (tree decl)
4796 tree context = NULL_TREE;
4798 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4799 context = DECL_CONTEXT (decl);
4800 else
4801 context = TYPE_MAIN_VARIANT
4802 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4804 if (context && !TYPE_P (context))
4805 context = NULL_TREE;
4807 return context;
4810 /* Add an attribute/value pair to a DIE. We build the lists up in reverse
4811 addition order, and correct that in reverse_all_dies. */
4813 static inline void
4814 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
4816 if (die != NULL && attr != NULL)
4818 attr->dw_attr_next = die->die_attr;
4819 die->die_attr = attr;
4823 static inline enum dw_val_class
4824 AT_class (dw_attr_ref a)
4826 return a->dw_attr_val.val_class;
4829 /* Add a flag value attribute to a DIE. */
4831 static inline void
4832 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4834 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4836 attr->dw_attr_next = NULL;
4837 attr->dw_attr = attr_kind;
4838 attr->dw_attr_val.val_class = dw_val_class_flag;
4839 attr->dw_attr_val.v.val_flag = flag;
4840 add_dwarf_attr (die, attr);
4843 static inline unsigned
4844 AT_flag (dw_attr_ref a)
4846 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4847 return a->dw_attr_val.v.val_flag;
4850 /* Add a signed integer attribute value to a DIE. */
4852 static inline void
4853 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4855 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4857 attr->dw_attr_next = NULL;
4858 attr->dw_attr = attr_kind;
4859 attr->dw_attr_val.val_class = dw_val_class_const;
4860 attr->dw_attr_val.v.val_int = int_val;
4861 add_dwarf_attr (die, attr);
4864 static inline HOST_WIDE_INT
4865 AT_int (dw_attr_ref a)
4867 gcc_assert (a && AT_class (a) == dw_val_class_const);
4868 return a->dw_attr_val.v.val_int;
4871 /* Add an unsigned integer attribute value to a DIE. */
4873 static inline void
4874 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4875 unsigned HOST_WIDE_INT unsigned_val)
4877 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4879 attr->dw_attr_next = NULL;
4880 attr->dw_attr = attr_kind;
4881 attr->dw_attr_val.val_class = dw_val_class_unsigned_const;
4882 attr->dw_attr_val.v.val_unsigned = unsigned_val;
4883 add_dwarf_attr (die, attr);
4886 static inline unsigned HOST_WIDE_INT
4887 AT_unsigned (dw_attr_ref a)
4889 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
4890 return a->dw_attr_val.v.val_unsigned;
4893 /* Add an unsigned double integer attribute value to a DIE. */
4895 static inline void
4896 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
4897 long unsigned int val_hi, long unsigned int val_low)
4899 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4901 attr->dw_attr_next = NULL;
4902 attr->dw_attr = attr_kind;
4903 attr->dw_attr_val.val_class = dw_val_class_long_long;
4904 attr->dw_attr_val.v.val_long_long.hi = val_hi;
4905 attr->dw_attr_val.v.val_long_long.low = val_low;
4906 add_dwarf_attr (die, attr);
4909 /* Add a floating point attribute value to a DIE and return it. */
4911 static inline void
4912 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4913 unsigned int length, unsigned int elt_size, unsigned char *array)
4915 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4917 attr->dw_attr_next = NULL;
4918 attr->dw_attr = attr_kind;
4919 attr->dw_attr_val.val_class = dw_val_class_vec;
4920 attr->dw_attr_val.v.val_vec.length = length;
4921 attr->dw_attr_val.v.val_vec.elt_size = elt_size;
4922 attr->dw_attr_val.v.val_vec.array = array;
4923 add_dwarf_attr (die, attr);
4926 /* Hash and equality functions for debug_str_hash. */
4928 static hashval_t
4929 debug_str_do_hash (const void *x)
4931 return htab_hash_string (((const struct indirect_string_node *)x)->str);
4934 static int
4935 debug_str_eq (const void *x1, const void *x2)
4937 return strcmp ((((const struct indirect_string_node *)x1)->str),
4938 (const char *)x2) == 0;
4941 /* Add a string attribute value to a DIE. */
4943 static inline void
4944 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4946 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4947 struct indirect_string_node *node;
4948 void **slot;
4950 if (! debug_str_hash)
4951 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
4952 debug_str_eq, NULL);
4954 slot = htab_find_slot_with_hash (debug_str_hash, str,
4955 htab_hash_string (str), INSERT);
4956 if (*slot == NULL)
4957 *slot = ggc_alloc_cleared (sizeof (struct indirect_string_node));
4958 node = (struct indirect_string_node *) *slot;
4959 node->str = ggc_strdup (str);
4960 node->refcount++;
4962 attr->dw_attr_next = NULL;
4963 attr->dw_attr = attr_kind;
4964 attr->dw_attr_val.val_class = dw_val_class_str;
4965 attr->dw_attr_val.v.val_str = node;
4966 add_dwarf_attr (die, attr);
4969 static inline const char *
4970 AT_string (dw_attr_ref a)
4972 gcc_assert (a && AT_class (a) == dw_val_class_str);
4973 return a->dw_attr_val.v.val_str->str;
4976 /* Find out whether a string should be output inline in DIE
4977 or out-of-line in .debug_str section. */
4979 static int
4980 AT_string_form (dw_attr_ref a)
4982 struct indirect_string_node *node;
4983 unsigned int len;
4984 char label[32];
4986 gcc_assert (a && AT_class (a) == dw_val_class_str);
4988 node = a->dw_attr_val.v.val_str;
4989 if (node->form)
4990 return node->form;
4992 len = strlen (node->str) + 1;
4994 /* If the string is shorter or equal to the size of the reference, it is
4995 always better to put it inline. */
4996 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4997 return node->form = DW_FORM_string;
4999 /* If we cannot expect the linker to merge strings in .debug_str
5000 section, only put it into .debug_str if it is worth even in this
5001 single module. */
5002 if ((debug_str_section->common.flags & SECTION_MERGE) == 0
5003 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
5004 return node->form = DW_FORM_string;
5006 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
5007 ++dw2_string_counter;
5008 node->label = xstrdup (label);
5010 return node->form = DW_FORM_strp;
5013 /* Add a DIE reference attribute value to a DIE. */
5015 static inline void
5016 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
5018 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5020 attr->dw_attr_next = NULL;
5021 attr->dw_attr = attr_kind;
5022 attr->dw_attr_val.val_class = dw_val_class_die_ref;
5023 attr->dw_attr_val.v.val_die_ref.die = targ_die;
5024 attr->dw_attr_val.v.val_die_ref.external = 0;
5025 add_dwarf_attr (die, attr);
5028 /* Add an AT_specification attribute to a DIE, and also make the back
5029 pointer from the specification to the definition. */
5031 static inline void
5032 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
5034 add_AT_die_ref (die, DW_AT_specification, targ_die);
5035 gcc_assert (!targ_die->die_definition);
5036 targ_die->die_definition = die;
5039 static inline dw_die_ref
5040 AT_ref (dw_attr_ref a)
5042 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5043 return a->dw_attr_val.v.val_die_ref.die;
5046 static inline int
5047 AT_ref_external (dw_attr_ref a)
5049 if (a && AT_class (a) == dw_val_class_die_ref)
5050 return a->dw_attr_val.v.val_die_ref.external;
5052 return 0;
5055 static inline void
5056 set_AT_ref_external (dw_attr_ref a, int i)
5058 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5059 a->dw_attr_val.v.val_die_ref.external = i;
5062 /* Add an FDE reference attribute value to a DIE. */
5064 static inline void
5065 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
5067 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5069 attr->dw_attr_next = NULL;
5070 attr->dw_attr = attr_kind;
5071 attr->dw_attr_val.val_class = dw_val_class_fde_ref;
5072 attr->dw_attr_val.v.val_fde_index = targ_fde;
5073 add_dwarf_attr (die, attr);
5076 /* Add a location description attribute value to a DIE. */
5078 static inline void
5079 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
5081 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5083 attr->dw_attr_next = NULL;
5084 attr->dw_attr = attr_kind;
5085 attr->dw_attr_val.val_class = dw_val_class_loc;
5086 attr->dw_attr_val.v.val_loc = loc;
5087 add_dwarf_attr (die, attr);
5090 static inline dw_loc_descr_ref
5091 AT_loc (dw_attr_ref a)
5093 gcc_assert (a && AT_class (a) == dw_val_class_loc);
5094 return a->dw_attr_val.v.val_loc;
5097 static inline void
5098 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
5100 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5102 attr->dw_attr_next = NULL;
5103 attr->dw_attr = attr_kind;
5104 attr->dw_attr_val.val_class = dw_val_class_loc_list;
5105 attr->dw_attr_val.v.val_loc_list = loc_list;
5106 add_dwarf_attr (die, attr);
5107 have_location_lists = true;
5110 static inline dw_loc_list_ref
5111 AT_loc_list (dw_attr_ref a)
5113 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
5114 return a->dw_attr_val.v.val_loc_list;
5117 /* Add an address constant attribute value to a DIE. */
5119 static inline void
5120 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
5122 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5124 attr->dw_attr_next = NULL;
5125 attr->dw_attr = attr_kind;
5126 attr->dw_attr_val.val_class = dw_val_class_addr;
5127 attr->dw_attr_val.v.val_addr = addr;
5128 add_dwarf_attr (die, attr);
5131 static inline rtx
5132 AT_addr (dw_attr_ref a)
5134 gcc_assert (a && AT_class (a) == dw_val_class_addr);
5135 return a->dw_attr_val.v.val_addr;
5138 /* Add a label identifier attribute value to a DIE. */
5140 static inline void
5141 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
5143 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5145 attr->dw_attr_next = NULL;
5146 attr->dw_attr = attr_kind;
5147 attr->dw_attr_val.val_class = dw_val_class_lbl_id;
5148 attr->dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5149 add_dwarf_attr (die, attr);
5152 /* Add a section offset attribute value to a DIE, an offset into the
5153 debug_line section. */
5155 static inline void
5156 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5157 const char *label)
5159 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5161 attr->dw_attr_next = NULL;
5162 attr->dw_attr = attr_kind;
5163 attr->dw_attr_val.val_class = dw_val_class_lineptr;
5164 attr->dw_attr_val.v.val_lbl_id = xstrdup (label);
5165 add_dwarf_attr (die, attr);
5168 /* Add a section offset attribute value to a DIE, an offset into the
5169 debug_macinfo section. */
5171 static inline void
5172 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5173 const char *label)
5175 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5177 attr->dw_attr_next = NULL;
5178 attr->dw_attr = attr_kind;
5179 attr->dw_attr_val.val_class = dw_val_class_macptr;
5180 attr->dw_attr_val.v.val_lbl_id = xstrdup (label);
5181 add_dwarf_attr (die, attr);
5184 /* Add an offset attribute value to a DIE. */
5186 static inline void
5187 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
5188 unsigned HOST_WIDE_INT offset)
5190 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5192 attr->dw_attr_next = NULL;
5193 attr->dw_attr = attr_kind;
5194 attr->dw_attr_val.val_class = dw_val_class_offset;
5195 attr->dw_attr_val.v.val_offset = offset;
5196 add_dwarf_attr (die, attr);
5199 /* Add an range_list attribute value to a DIE. */
5201 static void
5202 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5203 long unsigned int offset)
5205 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5207 attr->dw_attr_next = NULL;
5208 attr->dw_attr = attr_kind;
5209 attr->dw_attr_val.val_class = dw_val_class_range_list;
5210 attr->dw_attr_val.v.val_offset = offset;
5211 add_dwarf_attr (die, attr);
5214 static inline const char *
5215 AT_lbl (dw_attr_ref a)
5217 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5218 || AT_class (a) == dw_val_class_lineptr
5219 || AT_class (a) == dw_val_class_macptr));
5220 return a->dw_attr_val.v.val_lbl_id;
5223 /* Get the attribute of type attr_kind. */
5225 static dw_attr_ref
5226 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5228 dw_attr_ref a;
5229 dw_die_ref spec = NULL;
5231 if (die != NULL)
5233 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
5234 if (a->dw_attr == attr_kind)
5235 return a;
5236 else if (a->dw_attr == DW_AT_specification
5237 || a->dw_attr == DW_AT_abstract_origin)
5238 spec = AT_ref (a);
5240 if (spec)
5241 return get_AT (spec, attr_kind);
5244 return NULL;
5247 /* Return the "low pc" attribute value, typically associated with a subprogram
5248 DIE. Return null if the "low pc" attribute is either not present, or if it
5249 cannot be represented as an assembler label identifier. */
5251 static inline const char *
5252 get_AT_low_pc (dw_die_ref die)
5254 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
5256 return a ? AT_lbl (a) : NULL;
5259 /* Return the "high pc" attribute value, typically associated with a subprogram
5260 DIE. Return null if the "high pc" attribute is either not present, or if it
5261 cannot be represented as an assembler label identifier. */
5263 static inline const char *
5264 get_AT_hi_pc (dw_die_ref die)
5266 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
5268 return a ? AT_lbl (a) : NULL;
5271 /* Return the value of the string attribute designated by ATTR_KIND, or
5272 NULL if it is not present. */
5274 static inline const char *
5275 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5277 dw_attr_ref a = get_AT (die, attr_kind);
5279 return a ? AT_string (a) : NULL;
5282 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5283 if it is not present. */
5285 static inline int
5286 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5288 dw_attr_ref a = get_AT (die, attr_kind);
5290 return a ? AT_flag (a) : 0;
5293 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5294 if it is not present. */
5296 static inline unsigned
5297 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5299 dw_attr_ref a = get_AT (die, attr_kind);
5301 return a ? AT_unsigned (a) : 0;
5304 static inline dw_die_ref
5305 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5307 dw_attr_ref a = get_AT (die, attr_kind);
5309 return a ? AT_ref (a) : NULL;
5312 /* Return TRUE if the language is C or C++. */
5314 static inline bool
5315 is_c_family (void)
5317 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5319 return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
5320 || lang == DW_LANG_C99
5321 || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
5324 /* Return TRUE if the language is C++. */
5326 static inline bool
5327 is_cxx (void)
5329 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5331 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
5334 /* Return TRUE if the language is Fortran. */
5336 static inline bool
5337 is_fortran (void)
5339 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5341 return (lang == DW_LANG_Fortran77
5342 || lang == DW_LANG_Fortran90
5343 || lang == DW_LANG_Fortran95);
5346 /* Return TRUE if the language is Java. */
5348 static inline bool
5349 is_java (void)
5351 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5353 return lang == DW_LANG_Java;
5356 /* Return TRUE if the language is Ada. */
5358 static inline bool
5359 is_ada (void)
5361 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5363 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5366 /* Free up the memory used by A. */
5368 static inline void free_AT (dw_attr_ref);
5369 static inline void
5370 free_AT (dw_attr_ref a)
5372 if (AT_class (a) == dw_val_class_str)
5373 if (a->dw_attr_val.v.val_str->refcount)
5374 a->dw_attr_val.v.val_str->refcount--;
5377 /* Remove the specified attribute if present. */
5379 static void
5380 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5382 dw_attr_ref *p;
5383 dw_attr_ref removed = NULL;
5385 if (die != NULL)
5387 for (p = &(die->die_attr); *p; p = &((*p)->dw_attr_next))
5388 if ((*p)->dw_attr == attr_kind)
5390 removed = *p;
5391 *p = (*p)->dw_attr_next;
5392 break;
5395 if (removed != 0)
5396 free_AT (removed);
5400 /* Remove child die whose die_tag is specified tag. */
5402 static void
5403 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5405 dw_die_ref current, prev, next;
5406 current = die->die_child;
5407 prev = NULL;
5408 while (current != NULL)
5410 if (current->die_tag == tag)
5412 next = current->die_sib;
5413 if (prev == NULL)
5414 die->die_child = next;
5415 else
5416 prev->die_sib = next;
5417 free_die (current);
5418 current = next;
5420 else
5422 prev = current;
5423 current = current->die_sib;
5428 /* Free up the memory used by DIE. */
5430 static inline void
5431 free_die (dw_die_ref die)
5433 remove_children (die);
5436 /* Discard the children of this DIE. */
5438 static void
5439 remove_children (dw_die_ref die)
5441 dw_die_ref child_die = die->die_child;
5443 die->die_child = NULL;
5445 while (child_die != NULL)
5447 dw_die_ref tmp_die = child_die;
5448 dw_attr_ref a;
5450 child_die = child_die->die_sib;
5452 for (a = tmp_die->die_attr; a != NULL;)
5454 dw_attr_ref tmp_a = a;
5456 a = a->dw_attr_next;
5457 free_AT (tmp_a);
5460 free_die (tmp_die);
5464 /* Add a child DIE below its parent. We build the lists up in reverse
5465 addition order, and correct that in reverse_all_dies. */
5467 static inline void
5468 add_child_die (dw_die_ref die, dw_die_ref child_die)
5470 if (die != NULL && child_die != NULL)
5472 gcc_assert (die != child_die);
5474 child_die->die_parent = die;
5475 child_die->die_sib = die->die_child;
5476 die->die_child = child_die;
5480 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5481 is the specification, to the front of PARENT's list of children. */
5483 static void
5484 splice_child_die (dw_die_ref parent, dw_die_ref child)
5486 dw_die_ref *p;
5488 /* We want the declaration DIE from inside the class, not the
5489 specification DIE at toplevel. */
5490 if (child->die_parent != parent)
5492 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5494 if (tmp)
5495 child = tmp;
5498 gcc_assert (child->die_parent == parent
5499 || (child->die_parent
5500 == get_AT_ref (parent, DW_AT_specification)));
5502 for (p = &(child->die_parent->die_child); *p; p = &((*p)->die_sib))
5503 if (*p == child)
5505 *p = child->die_sib;
5506 break;
5509 child->die_parent = parent;
5510 child->die_sib = parent->die_child;
5511 parent->die_child = child;
5514 /* Return a pointer to a newly created DIE node. */
5516 static inline dw_die_ref
5517 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5519 dw_die_ref die = ggc_alloc_cleared (sizeof (die_node));
5521 die->die_tag = tag_value;
5523 if (parent_die != NULL)
5524 add_child_die (parent_die, die);
5525 else
5527 limbo_die_node *limbo_node;
5529 limbo_node = ggc_alloc_cleared (sizeof (limbo_die_node));
5530 limbo_node->die = die;
5531 limbo_node->created_for = t;
5532 limbo_node->next = limbo_die_list;
5533 limbo_die_list = limbo_node;
5536 return die;
5539 /* Return the DIE associated with the given type specifier. */
5541 static inline dw_die_ref
5542 lookup_type_die (tree type)
5544 return TYPE_SYMTAB_DIE (type);
5547 /* Equate a DIE to a given type specifier. */
5549 static inline void
5550 equate_type_number_to_die (tree type, dw_die_ref type_die)
5552 TYPE_SYMTAB_DIE (type) = type_die;
5555 /* Returns a hash value for X (which really is a die_struct). */
5557 static hashval_t
5558 decl_die_table_hash (const void *x)
5560 return (hashval_t) ((const dw_die_ref) x)->decl_id;
5563 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5565 static int
5566 decl_die_table_eq (const void *x, const void *y)
5568 return (((const dw_die_ref) x)->decl_id == DECL_UID ((const tree) y));
5571 /* Return the DIE associated with a given declaration. */
5573 static inline dw_die_ref
5574 lookup_decl_die (tree decl)
5576 return htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
5579 /* Returns a hash value for X (which really is a var_loc_list). */
5581 static hashval_t
5582 decl_loc_table_hash (const void *x)
5584 return (hashval_t) ((const var_loc_list *) x)->decl_id;
5587 /* Return nonzero if decl_id of var_loc_list X is the same as
5588 UID of decl *Y. */
5590 static int
5591 decl_loc_table_eq (const void *x, const void *y)
5593 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const tree) y));
5596 /* Return the var_loc list associated with a given declaration. */
5598 static inline var_loc_list *
5599 lookup_decl_loc (tree decl)
5601 return htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5604 /* Equate a DIE to a particular declaration. */
5606 static void
5607 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5609 unsigned int decl_id = DECL_UID (decl);
5610 void **slot;
5612 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5613 *slot = decl_die;
5614 decl_die->decl_id = decl_id;
5617 /* Add a variable location node to the linked list for DECL. */
5619 static void
5620 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
5622 unsigned int decl_id = DECL_UID (decl);
5623 var_loc_list *temp;
5624 void **slot;
5626 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5627 if (*slot == NULL)
5629 temp = ggc_alloc_cleared (sizeof (var_loc_list));
5630 temp->decl_id = decl_id;
5631 *slot = temp;
5633 else
5634 temp = *slot;
5636 if (temp->last)
5638 /* If the current location is the same as the end of the list,
5639 we have nothing to do. */
5640 if (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
5641 NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
5643 /* Add LOC to the end of list and update LAST. */
5644 temp->last->next = loc;
5645 temp->last = loc;
5648 /* Do not add empty location to the beginning of the list. */
5649 else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
5651 temp->first = loc;
5652 temp->last = loc;
5656 /* Keep track of the number of spaces used to indent the
5657 output of the debugging routines that print the structure of
5658 the DIE internal representation. */
5659 static int print_indent;
5661 /* Indent the line the number of spaces given by print_indent. */
5663 static inline void
5664 print_spaces (FILE *outfile)
5666 fprintf (outfile, "%*s", print_indent, "");
5669 /* Print the information associated with a given DIE, and its children.
5670 This routine is a debugging aid only. */
5672 static void
5673 print_die (dw_die_ref die, FILE *outfile)
5675 dw_attr_ref a;
5676 dw_die_ref c;
5678 print_spaces (outfile);
5679 fprintf (outfile, "DIE %4lu: %s\n",
5680 die->die_offset, dwarf_tag_name (die->die_tag));
5681 print_spaces (outfile);
5682 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5683 fprintf (outfile, " offset: %lu\n", die->die_offset);
5685 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
5687 print_spaces (outfile);
5688 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5690 switch (AT_class (a))
5692 case dw_val_class_addr:
5693 fprintf (outfile, "address");
5694 break;
5695 case dw_val_class_offset:
5696 fprintf (outfile, "offset");
5697 break;
5698 case dw_val_class_loc:
5699 fprintf (outfile, "location descriptor");
5700 break;
5701 case dw_val_class_loc_list:
5702 fprintf (outfile, "location list -> label:%s",
5703 AT_loc_list (a)->ll_symbol);
5704 break;
5705 case dw_val_class_range_list:
5706 fprintf (outfile, "range list");
5707 break;
5708 case dw_val_class_const:
5709 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5710 break;
5711 case dw_val_class_unsigned_const:
5712 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5713 break;
5714 case dw_val_class_long_long:
5715 fprintf (outfile, "constant (%lu,%lu)",
5716 a->dw_attr_val.v.val_long_long.hi,
5717 a->dw_attr_val.v.val_long_long.low);
5718 break;
5719 case dw_val_class_vec:
5720 fprintf (outfile, "floating-point or vector constant");
5721 break;
5722 case dw_val_class_flag:
5723 fprintf (outfile, "%u", AT_flag (a));
5724 break;
5725 case dw_val_class_die_ref:
5726 if (AT_ref (a) != NULL)
5728 if (AT_ref (a)->die_symbol)
5729 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
5730 else
5731 fprintf (outfile, "die -> %lu", AT_ref (a)->die_offset);
5733 else
5734 fprintf (outfile, "die -> <null>");
5735 break;
5736 case dw_val_class_lbl_id:
5737 case dw_val_class_lineptr:
5738 case dw_val_class_macptr:
5739 fprintf (outfile, "label: %s", AT_lbl (a));
5740 break;
5741 case dw_val_class_str:
5742 if (AT_string (a) != NULL)
5743 fprintf (outfile, "\"%s\"", AT_string (a));
5744 else
5745 fprintf (outfile, "<null>");
5746 break;
5747 default:
5748 break;
5751 fprintf (outfile, "\n");
5754 if (die->die_child != NULL)
5756 print_indent += 4;
5757 for (c = die->die_child; c != NULL; c = c->die_sib)
5758 print_die (c, outfile);
5760 print_indent -= 4;
5762 if (print_indent == 0)
5763 fprintf (outfile, "\n");
5766 /* Print the contents of the source code line number correspondence table.
5767 This routine is a debugging aid only. */
5769 static void
5770 print_dwarf_line_table (FILE *outfile)
5772 unsigned i;
5773 dw_line_info_ref line_info;
5775 fprintf (outfile, "\n\nDWARF source line information\n");
5776 for (i = 1; i < line_info_table_in_use; i++)
5778 line_info = &line_info_table[i];
5779 fprintf (outfile, "%5d: ", i);
5780 fprintf (outfile, "%-20s",
5781 VARRAY_CHAR_PTR (file_table, line_info->dw_file_num));
5782 fprintf (outfile, "%6ld", line_info->dw_line_num);
5783 fprintf (outfile, "\n");
5786 fprintf (outfile, "\n\n");
5789 /* Print the information collected for a given DIE. */
5791 void
5792 debug_dwarf_die (dw_die_ref die)
5794 print_die (die, stderr);
5797 /* Print all DWARF information collected for the compilation unit.
5798 This routine is a debugging aid only. */
5800 void
5801 debug_dwarf (void)
5803 print_indent = 0;
5804 print_die (comp_unit_die, stderr);
5805 if (! DWARF2_ASM_LINE_DEBUG_INFO)
5806 print_dwarf_line_table (stderr);
5809 /* We build up the lists of children and attributes by pushing new ones
5810 onto the beginning of the list. Reverse the lists for DIE so that
5811 they are in order of addition. */
5813 static void
5814 reverse_die_lists (dw_die_ref die)
5816 dw_die_ref c, cp, cn;
5817 dw_attr_ref a, ap, an;
5819 for (a = die->die_attr, ap = 0; a; a = an)
5821 an = a->dw_attr_next;
5822 a->dw_attr_next = ap;
5823 ap = a;
5826 die->die_attr = ap;
5828 for (c = die->die_child, cp = 0; c; c = cn)
5830 cn = c->die_sib;
5831 c->die_sib = cp;
5832 cp = c;
5835 die->die_child = cp;
5838 /* reverse_die_lists only reverses the single die you pass it. Since we used to
5839 reverse all dies in add_sibling_attributes, which runs through all the dies,
5840 it would reverse all the dies. Now, however, since we don't call
5841 reverse_die_lists in add_sibling_attributes, we need a routine to
5842 recursively reverse all the dies. This is that routine. */
5844 static void
5845 reverse_all_dies (dw_die_ref die)
5847 dw_die_ref c;
5849 reverse_die_lists (die);
5851 for (c = die->die_child; c; c = c->die_sib)
5852 reverse_all_dies (c);
5855 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5856 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5857 DIE that marks the start of the DIEs for this include file. */
5859 static dw_die_ref
5860 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5862 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5863 dw_die_ref new_unit = gen_compile_unit_die (filename);
5865 new_unit->die_sib = old_unit;
5866 return new_unit;
5869 /* Close an include-file CU and reopen the enclosing one. */
5871 static dw_die_ref
5872 pop_compile_unit (dw_die_ref old_unit)
5874 dw_die_ref new_unit = old_unit->die_sib;
5876 old_unit->die_sib = NULL;
5877 return new_unit;
5880 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5881 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5883 /* Calculate the checksum of a location expression. */
5885 static inline void
5886 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5888 CHECKSUM (loc->dw_loc_opc);
5889 CHECKSUM (loc->dw_loc_oprnd1);
5890 CHECKSUM (loc->dw_loc_oprnd2);
5893 /* Calculate the checksum of an attribute. */
5895 static void
5896 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5898 dw_loc_descr_ref loc;
5899 rtx r;
5901 CHECKSUM (at->dw_attr);
5903 /* We don't care about differences in file numbering. */
5904 if (at->dw_attr == DW_AT_decl_file
5905 /* Or that this was compiled with a different compiler snapshot; if
5906 the output is the same, that's what matters. */
5907 || at->dw_attr == DW_AT_producer)
5908 return;
5910 switch (AT_class (at))
5912 case dw_val_class_const:
5913 CHECKSUM (at->dw_attr_val.v.val_int);
5914 break;
5915 case dw_val_class_unsigned_const:
5916 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5917 break;
5918 case dw_val_class_long_long:
5919 CHECKSUM (at->dw_attr_val.v.val_long_long);
5920 break;
5921 case dw_val_class_vec:
5922 CHECKSUM (at->dw_attr_val.v.val_vec);
5923 break;
5924 case dw_val_class_flag:
5925 CHECKSUM (at->dw_attr_val.v.val_flag);
5926 break;
5927 case dw_val_class_str:
5928 CHECKSUM_STRING (AT_string (at));
5929 break;
5931 case dw_val_class_addr:
5932 r = AT_addr (at);
5933 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5934 CHECKSUM_STRING (XSTR (r, 0));
5935 break;
5937 case dw_val_class_offset:
5938 CHECKSUM (at->dw_attr_val.v.val_offset);
5939 break;
5941 case dw_val_class_loc:
5942 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5943 loc_checksum (loc, ctx);
5944 break;
5946 case dw_val_class_die_ref:
5947 die_checksum (AT_ref (at), ctx, mark);
5948 break;
5950 case dw_val_class_fde_ref:
5951 case dw_val_class_lbl_id:
5952 case dw_val_class_lineptr:
5953 case dw_val_class_macptr:
5954 break;
5956 default:
5957 break;
5961 /* Calculate the checksum of a DIE. */
5963 static void
5964 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5966 dw_die_ref c;
5967 dw_attr_ref a;
5969 /* To avoid infinite recursion. */
5970 if (die->die_mark)
5972 CHECKSUM (die->die_mark);
5973 return;
5975 die->die_mark = ++(*mark);
5977 CHECKSUM (die->die_tag);
5979 for (a = die->die_attr; a; a = a->dw_attr_next)
5980 attr_checksum (a, ctx, mark);
5982 for (c = die->die_child; c; c = c->die_sib)
5983 die_checksum (c, ctx, mark);
5986 #undef CHECKSUM
5987 #undef CHECKSUM_STRING
5989 /* Do the location expressions look same? */
5990 static inline int
5991 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
5993 return loc1->dw_loc_opc == loc2->dw_loc_opc
5994 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
5995 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
5998 /* Do the values look the same? */
5999 static int
6000 same_dw_val_p (dw_val_node *v1, dw_val_node *v2, int *mark)
6002 dw_loc_descr_ref loc1, loc2;
6003 rtx r1, r2;
6005 if (v1->val_class != v2->val_class)
6006 return 0;
6008 switch (v1->val_class)
6010 case dw_val_class_const:
6011 return v1->v.val_int == v2->v.val_int;
6012 case dw_val_class_unsigned_const:
6013 return v1->v.val_unsigned == v2->v.val_unsigned;
6014 case dw_val_class_long_long:
6015 return v1->v.val_long_long.hi == v2->v.val_long_long.hi
6016 && v1->v.val_long_long.low == v2->v.val_long_long.low;
6017 case dw_val_class_vec:
6018 if (v1->v.val_vec.length != v2->v.val_vec.length
6019 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6020 return 0;
6021 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6022 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6023 return 0;
6024 return 1;
6025 case dw_val_class_flag:
6026 return v1->v.val_flag == v2->v.val_flag;
6027 case dw_val_class_str:
6028 return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6030 case dw_val_class_addr:
6031 r1 = v1->v.val_addr;
6032 r2 = v2->v.val_addr;
6033 if (GET_CODE (r1) != GET_CODE (r2))
6034 return 0;
6035 gcc_assert (GET_CODE (r1) == SYMBOL_REF);
6036 return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
6038 case dw_val_class_offset:
6039 return v1->v.val_offset == v2->v.val_offset;
6041 case dw_val_class_loc:
6042 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6043 loc1 && loc2;
6044 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6045 if (!same_loc_p (loc1, loc2, mark))
6046 return 0;
6047 return !loc1 && !loc2;
6049 case dw_val_class_die_ref:
6050 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6052 case dw_val_class_fde_ref:
6053 case dw_val_class_lbl_id:
6054 case dw_val_class_lineptr:
6055 case dw_val_class_macptr:
6056 return 1;
6058 default:
6059 return 1;
6063 /* Do the attributes look the same? */
6065 static int
6066 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6068 if (at1->dw_attr != at2->dw_attr)
6069 return 0;
6071 /* We don't care about differences in file numbering. */
6072 if (at1->dw_attr == DW_AT_decl_file
6073 /* Or that this was compiled with a different compiler snapshot; if
6074 the output is the same, that's what matters. */
6075 || at1->dw_attr == DW_AT_producer)
6076 return 1;
6078 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6081 /* Do the dies look the same? */
6083 static int
6084 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6086 dw_die_ref c1, c2;
6087 dw_attr_ref a1, a2;
6089 /* To avoid infinite recursion. */
6090 if (die1->die_mark)
6091 return die1->die_mark == die2->die_mark;
6092 die1->die_mark = die2->die_mark = ++(*mark);
6094 if (die1->die_tag != die2->die_tag)
6095 return 0;
6097 for (a1 = die1->die_attr, a2 = die2->die_attr;
6098 a1 && a2;
6099 a1 = a1->dw_attr_next, a2 = a2->dw_attr_next)
6100 if (!same_attr_p (a1, a2, mark))
6101 return 0;
6102 if (a1 || a2)
6103 return 0;
6105 for (c1 = die1->die_child, c2 = die2->die_child;
6106 c1 && c2;
6107 c1 = c1->die_sib, c2 = c2->die_sib)
6108 if (!same_die_p (c1, c2, mark))
6109 return 0;
6110 if (c1 || c2)
6111 return 0;
6113 return 1;
6116 /* Do the dies look the same? Wrapper around same_die_p. */
6118 static int
6119 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6121 int mark = 0;
6122 int ret = same_die_p (die1, die2, &mark);
6124 unmark_all_dies (die1);
6125 unmark_all_dies (die2);
6127 return ret;
6130 /* The prefix to attach to symbols on DIEs in the current comdat debug
6131 info section. */
6132 static char *comdat_symbol_id;
6134 /* The index of the current symbol within the current comdat CU. */
6135 static unsigned int comdat_symbol_number;
6137 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6138 children, and set comdat_symbol_id accordingly. */
6140 static void
6141 compute_section_prefix (dw_die_ref unit_die)
6143 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6144 const char *base = die_name ? lbasename (die_name) : "anonymous";
6145 char *name = alloca (strlen (base) + 64);
6146 char *p;
6147 int i, mark;
6148 unsigned char checksum[16];
6149 struct md5_ctx ctx;
6151 /* Compute the checksum of the DIE, then append part of it as hex digits to
6152 the name filename of the unit. */
6154 md5_init_ctx (&ctx);
6155 mark = 0;
6156 die_checksum (unit_die, &ctx, &mark);
6157 unmark_all_dies (unit_die);
6158 md5_finish_ctx (&ctx, checksum);
6160 sprintf (name, "%s.", base);
6161 clean_symbol_name (name);
6163 p = name + strlen (name);
6164 for (i = 0; i < 4; i++)
6166 sprintf (p, "%.2x", checksum[i]);
6167 p += 2;
6170 comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
6171 comdat_symbol_number = 0;
6174 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6176 static int
6177 is_type_die (dw_die_ref die)
6179 switch (die->die_tag)
6181 case DW_TAG_array_type:
6182 case DW_TAG_class_type:
6183 case DW_TAG_enumeration_type:
6184 case DW_TAG_pointer_type:
6185 case DW_TAG_reference_type:
6186 case DW_TAG_string_type:
6187 case DW_TAG_structure_type:
6188 case DW_TAG_subroutine_type:
6189 case DW_TAG_union_type:
6190 case DW_TAG_ptr_to_member_type:
6191 case DW_TAG_set_type:
6192 case DW_TAG_subrange_type:
6193 case DW_TAG_base_type:
6194 case DW_TAG_const_type:
6195 case DW_TAG_file_type:
6196 case DW_TAG_packed_type:
6197 case DW_TAG_volatile_type:
6198 case DW_TAG_typedef:
6199 return 1;
6200 default:
6201 return 0;
6205 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6206 Basically, we want to choose the bits that are likely to be shared between
6207 compilations (types) and leave out the bits that are specific to individual
6208 compilations (functions). */
6210 static int
6211 is_comdat_die (dw_die_ref c)
6213 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6214 we do for stabs. The advantage is a greater likelihood of sharing between
6215 objects that don't include headers in the same order (and therefore would
6216 put the base types in a different comdat). jason 8/28/00 */
6218 if (c->die_tag == DW_TAG_base_type)
6219 return 0;
6221 if (c->die_tag == DW_TAG_pointer_type
6222 || c->die_tag == DW_TAG_reference_type
6223 || c->die_tag == DW_TAG_const_type
6224 || c->die_tag == DW_TAG_volatile_type)
6226 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6228 return t ? is_comdat_die (t) : 0;
6231 return is_type_die (c);
6234 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6235 compilation unit. */
6237 static int
6238 is_symbol_die (dw_die_ref c)
6240 return (is_type_die (c)
6241 || (get_AT (c, DW_AT_declaration)
6242 && !get_AT (c, DW_AT_specification)));
6245 static char *
6246 gen_internal_sym (const char *prefix)
6248 char buf[256];
6250 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6251 return xstrdup (buf);
6254 /* Assign symbols to all worthy DIEs under DIE. */
6256 static void
6257 assign_symbol_names (dw_die_ref die)
6259 dw_die_ref c;
6261 if (is_symbol_die (die))
6263 if (comdat_symbol_id)
6265 char *p = alloca (strlen (comdat_symbol_id) + 64);
6267 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6268 comdat_symbol_id, comdat_symbol_number++);
6269 die->die_symbol = xstrdup (p);
6271 else
6272 die->die_symbol = gen_internal_sym ("LDIE");
6275 for (c = die->die_child; c != NULL; c = c->die_sib)
6276 assign_symbol_names (c);
6279 struct cu_hash_table_entry
6281 dw_die_ref cu;
6282 unsigned min_comdat_num, max_comdat_num;
6283 struct cu_hash_table_entry *next;
6286 /* Routines to manipulate hash table of CUs. */
6287 static hashval_t
6288 htab_cu_hash (const void *of)
6290 const struct cu_hash_table_entry *entry = of;
6292 return htab_hash_string (entry->cu->die_symbol);
6295 static int
6296 htab_cu_eq (const void *of1, const void *of2)
6298 const struct cu_hash_table_entry *entry1 = of1;
6299 const struct die_struct *entry2 = of2;
6301 return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
6304 static void
6305 htab_cu_del (void *what)
6307 struct cu_hash_table_entry *next, *entry = what;
6309 while (entry)
6311 next = entry->next;
6312 free (entry);
6313 entry = next;
6317 /* Check whether we have already seen this CU and set up SYM_NUM
6318 accordingly. */
6319 static int
6320 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6322 struct cu_hash_table_entry dummy;
6323 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6325 dummy.max_comdat_num = 0;
6327 slot = (struct cu_hash_table_entry **)
6328 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6329 INSERT);
6330 entry = *slot;
6332 for (; entry; last = entry, entry = entry->next)
6334 if (same_die_p_wrap (cu, entry->cu))
6335 break;
6338 if (entry)
6340 *sym_num = entry->min_comdat_num;
6341 return 1;
6344 entry = XCNEW (struct cu_hash_table_entry);
6345 entry->cu = cu;
6346 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6347 entry->next = *slot;
6348 *slot = entry;
6350 return 0;
6353 /* Record SYM_NUM to record of CU in HTABLE. */
6354 static void
6355 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6357 struct cu_hash_table_entry **slot, *entry;
6359 slot = (struct cu_hash_table_entry **)
6360 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6361 NO_INSERT);
6362 entry = *slot;
6364 entry->max_comdat_num = sym_num;
6367 /* Traverse the DIE (which is always comp_unit_die), and set up
6368 additional compilation units for each of the include files we see
6369 bracketed by BINCL/EINCL. */
6371 static void
6372 break_out_includes (dw_die_ref die)
6374 dw_die_ref *ptr;
6375 dw_die_ref unit = NULL;
6376 limbo_die_node *node, **pnode;
6377 htab_t cu_hash_table;
6379 for (ptr = &(die->die_child); *ptr;)
6381 dw_die_ref c = *ptr;
6383 if (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6384 || (unit && is_comdat_die (c)))
6386 /* This DIE is for a secondary CU; remove it from the main one. */
6387 *ptr = c->die_sib;
6389 if (c->die_tag == DW_TAG_GNU_BINCL)
6391 unit = push_new_compile_unit (unit, c);
6392 free_die (c);
6394 else if (c->die_tag == DW_TAG_GNU_EINCL)
6396 unit = pop_compile_unit (unit);
6397 free_die (c);
6399 else
6400 add_child_die (unit, c);
6402 else
6404 /* Leave this DIE in the main CU. */
6405 ptr = &(c->die_sib);
6406 continue;
6410 #if 0
6411 /* We can only use this in debugging, since the frontend doesn't check
6412 to make sure that we leave every include file we enter. */
6413 gcc_assert (!unit);
6414 #endif
6416 assign_symbol_names (die);
6417 cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6418 for (node = limbo_die_list, pnode = &limbo_die_list;
6419 node;
6420 node = node->next)
6422 int is_dupl;
6424 compute_section_prefix (node->die);
6425 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6426 &comdat_symbol_number);
6427 assign_symbol_names (node->die);
6428 if (is_dupl)
6429 *pnode = node->next;
6430 else
6432 pnode = &node->next;
6433 record_comdat_symbol_number (node->die, cu_hash_table,
6434 comdat_symbol_number);
6437 htab_delete (cu_hash_table);
6440 /* Traverse the DIE and add a sibling attribute if it may have the
6441 effect of speeding up access to siblings. To save some space,
6442 avoid generating sibling attributes for DIE's without children. */
6444 static void
6445 add_sibling_attributes (dw_die_ref die)
6447 dw_die_ref c;
6449 if (die->die_tag != DW_TAG_compile_unit
6450 && die->die_sib && die->die_child != NULL)
6451 /* Add the sibling link to the front of the attribute list. */
6452 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
6454 for (c = die->die_child; c != NULL; c = c->die_sib)
6455 add_sibling_attributes (c);
6458 /* Output all location lists for the DIE and its children. */
6460 static void
6461 output_location_lists (dw_die_ref die)
6463 dw_die_ref c;
6464 dw_attr_ref d_attr;
6466 for (d_attr = die->die_attr; d_attr; d_attr = d_attr->dw_attr_next)
6467 if (AT_class (d_attr) == dw_val_class_loc_list)
6468 output_loc_list (AT_loc_list (d_attr));
6470 for (c = die->die_child; c != NULL; c = c->die_sib)
6471 output_location_lists (c);
6475 /* The format of each DIE (and its attribute value pairs) is encoded in an
6476 abbreviation table. This routine builds the abbreviation table and assigns
6477 a unique abbreviation id for each abbreviation entry. The children of each
6478 die are visited recursively. */
6480 static void
6481 build_abbrev_table (dw_die_ref die)
6483 unsigned long abbrev_id;
6484 unsigned int n_alloc;
6485 dw_die_ref c;
6486 dw_attr_ref d_attr, a_attr;
6488 /* Scan the DIE references, and mark as external any that refer to
6489 DIEs from other CUs (i.e. those which are not marked). */
6490 for (d_attr = die->die_attr; d_attr; d_attr = d_attr->dw_attr_next)
6491 if (AT_class (d_attr) == dw_val_class_die_ref
6492 && AT_ref (d_attr)->die_mark == 0)
6494 gcc_assert (AT_ref (d_attr)->die_symbol);
6496 set_AT_ref_external (d_attr, 1);
6499 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6501 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6503 if (abbrev->die_tag == die->die_tag)
6505 if ((abbrev->die_child != NULL) == (die->die_child != NULL))
6507 a_attr = abbrev->die_attr;
6508 d_attr = die->die_attr;
6510 while (a_attr != NULL && d_attr != NULL)
6512 if ((a_attr->dw_attr != d_attr->dw_attr)
6513 || (value_format (a_attr) != value_format (d_attr)))
6514 break;
6516 a_attr = a_attr->dw_attr_next;
6517 d_attr = d_attr->dw_attr_next;
6520 if (a_attr == NULL && d_attr == NULL)
6521 break;
6526 if (abbrev_id >= abbrev_die_table_in_use)
6528 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
6530 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
6531 abbrev_die_table = ggc_realloc (abbrev_die_table,
6532 sizeof (dw_die_ref) * n_alloc);
6534 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
6535 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
6536 abbrev_die_table_allocated = n_alloc;
6539 ++abbrev_die_table_in_use;
6540 abbrev_die_table[abbrev_id] = die;
6543 die->die_abbrev = abbrev_id;
6544 for (c = die->die_child; c != NULL; c = c->die_sib)
6545 build_abbrev_table (c);
6548 /* Return the power-of-two number of bytes necessary to represent VALUE. */
6550 static int
6551 constant_size (long unsigned int value)
6553 int log;
6555 if (value == 0)
6556 log = 0;
6557 else
6558 log = floor_log2 (value);
6560 log = log / 8;
6561 log = 1 << (floor_log2 (log) + 1);
6563 return log;
6566 /* Return the size of a DIE as it is represented in the
6567 .debug_info section. */
6569 static unsigned long
6570 size_of_die (dw_die_ref die)
6572 unsigned long size = 0;
6573 dw_attr_ref a;
6575 size += size_of_uleb128 (die->die_abbrev);
6576 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
6578 switch (AT_class (a))
6580 case dw_val_class_addr:
6581 size += DWARF2_ADDR_SIZE;
6582 break;
6583 case dw_val_class_offset:
6584 size += DWARF_OFFSET_SIZE;
6585 break;
6586 case dw_val_class_loc:
6588 unsigned long lsize = size_of_locs (AT_loc (a));
6590 /* Block length. */
6591 size += constant_size (lsize);
6592 size += lsize;
6594 break;
6595 case dw_val_class_loc_list:
6596 size += DWARF_OFFSET_SIZE;
6597 break;
6598 case dw_val_class_range_list:
6599 size += DWARF_OFFSET_SIZE;
6600 break;
6601 case dw_val_class_const:
6602 size += size_of_sleb128 (AT_int (a));
6603 break;
6604 case dw_val_class_unsigned_const:
6605 size += constant_size (AT_unsigned (a));
6606 break;
6607 case dw_val_class_long_long:
6608 size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
6609 break;
6610 case dw_val_class_vec:
6611 size += 1 + (a->dw_attr_val.v.val_vec.length
6612 * a->dw_attr_val.v.val_vec.elt_size); /* block */
6613 break;
6614 case dw_val_class_flag:
6615 size += 1;
6616 break;
6617 case dw_val_class_die_ref:
6618 if (AT_ref_external (a))
6619 size += DWARF2_ADDR_SIZE;
6620 else
6621 size += DWARF_OFFSET_SIZE;
6622 break;
6623 case dw_val_class_fde_ref:
6624 size += DWARF_OFFSET_SIZE;
6625 break;
6626 case dw_val_class_lbl_id:
6627 size += DWARF2_ADDR_SIZE;
6628 break;
6629 case dw_val_class_lineptr:
6630 case dw_val_class_macptr:
6631 size += DWARF_OFFSET_SIZE;
6632 break;
6633 case dw_val_class_str:
6634 if (AT_string_form (a) == DW_FORM_strp)
6635 size += DWARF_OFFSET_SIZE;
6636 else
6637 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
6638 break;
6639 default:
6640 gcc_unreachable ();
6644 return size;
6647 /* Size the debugging information associated with a given DIE. Visits the
6648 DIE's children recursively. Updates the global variable next_die_offset, on
6649 each time through. Uses the current value of next_die_offset to update the
6650 die_offset field in each DIE. */
6652 static void
6653 calc_die_sizes (dw_die_ref die)
6655 dw_die_ref c;
6657 die->die_offset = next_die_offset;
6658 next_die_offset += size_of_die (die);
6660 for (c = die->die_child; c != NULL; c = c->die_sib)
6661 calc_die_sizes (c);
6663 if (die->die_child != NULL)
6664 /* Count the null byte used to terminate sibling lists. */
6665 next_die_offset += 1;
6668 /* Set the marks for a die and its children. We do this so
6669 that we know whether or not a reference needs to use FORM_ref_addr; only
6670 DIEs in the same CU will be marked. We used to clear out the offset
6671 and use that as the flag, but ran into ordering problems. */
6673 static void
6674 mark_dies (dw_die_ref die)
6676 dw_die_ref c;
6678 gcc_assert (!die->die_mark);
6680 die->die_mark = 1;
6681 for (c = die->die_child; c; c = c->die_sib)
6682 mark_dies (c);
6685 /* Clear the marks for a die and its children. */
6687 static void
6688 unmark_dies (dw_die_ref die)
6690 dw_die_ref c;
6692 gcc_assert (die->die_mark);
6694 die->die_mark = 0;
6695 for (c = die->die_child; c; c = c->die_sib)
6696 unmark_dies (c);
6699 /* Clear the marks for a die, its children and referred dies. */
6701 static void
6702 unmark_all_dies (dw_die_ref die)
6704 dw_die_ref c;
6705 dw_attr_ref a;
6707 if (!die->die_mark)
6708 return;
6709 die->die_mark = 0;
6711 for (c = die->die_child; c; c = c->die_sib)
6712 unmark_all_dies (c);
6714 for (a = die->die_attr; a; a = a->dw_attr_next)
6715 if (AT_class (a) == dw_val_class_die_ref)
6716 unmark_all_dies (AT_ref (a));
6719 /* Return the size of the .debug_pubnames table generated for the
6720 compilation unit. */
6722 static unsigned long
6723 size_of_pubnames (void)
6725 unsigned long size;
6726 unsigned i;
6728 size = DWARF_PUBNAMES_HEADER_SIZE;
6729 for (i = 0; i < pubname_table_in_use; i++)
6731 pubname_ref p = &pubname_table[i];
6732 size += DWARF_OFFSET_SIZE + strlen (p->name) + 1;
6735 size += DWARF_OFFSET_SIZE;
6736 return size;
6739 /* Return the size of the information in the .debug_aranges section. */
6741 static unsigned long
6742 size_of_aranges (void)
6744 unsigned long size;
6746 size = DWARF_ARANGES_HEADER_SIZE;
6748 /* Count the address/length pair for this compilation unit. */
6749 size += 2 * DWARF2_ADDR_SIZE;
6750 size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
6752 /* Count the two zero words used to terminated the address range table. */
6753 size += 2 * DWARF2_ADDR_SIZE;
6754 return size;
6757 /* Select the encoding of an attribute value. */
6759 static enum dwarf_form
6760 value_format (dw_attr_ref a)
6762 switch (a->dw_attr_val.val_class)
6764 case dw_val_class_addr:
6765 return DW_FORM_addr;
6766 case dw_val_class_range_list:
6767 case dw_val_class_offset:
6768 case dw_val_class_loc_list:
6769 switch (DWARF_OFFSET_SIZE)
6771 case 4:
6772 return DW_FORM_data4;
6773 case 8:
6774 return DW_FORM_data8;
6775 default:
6776 gcc_unreachable ();
6778 case dw_val_class_loc:
6779 switch (constant_size (size_of_locs (AT_loc (a))))
6781 case 1:
6782 return DW_FORM_block1;
6783 case 2:
6784 return DW_FORM_block2;
6785 default:
6786 gcc_unreachable ();
6788 case dw_val_class_const:
6789 return DW_FORM_sdata;
6790 case dw_val_class_unsigned_const:
6791 switch (constant_size (AT_unsigned (a)))
6793 case 1:
6794 return DW_FORM_data1;
6795 case 2:
6796 return DW_FORM_data2;
6797 case 4:
6798 return DW_FORM_data4;
6799 case 8:
6800 return DW_FORM_data8;
6801 default:
6802 gcc_unreachable ();
6804 case dw_val_class_long_long:
6805 return DW_FORM_block1;
6806 case dw_val_class_vec:
6807 return DW_FORM_block1;
6808 case dw_val_class_flag:
6809 return DW_FORM_flag;
6810 case dw_val_class_die_ref:
6811 if (AT_ref_external (a))
6812 return DW_FORM_ref_addr;
6813 else
6814 return DW_FORM_ref;
6815 case dw_val_class_fde_ref:
6816 return DW_FORM_data;
6817 case dw_val_class_lbl_id:
6818 return DW_FORM_addr;
6819 case dw_val_class_lineptr:
6820 case dw_val_class_macptr:
6821 return DW_FORM_data;
6822 case dw_val_class_str:
6823 return AT_string_form (a);
6825 default:
6826 gcc_unreachable ();
6830 /* Output the encoding of an attribute value. */
6832 static void
6833 output_value_format (dw_attr_ref a)
6835 enum dwarf_form form = value_format (a);
6837 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
6840 /* Output the .debug_abbrev section which defines the DIE abbreviation
6841 table. */
6843 static void
6844 output_abbrev_section (void)
6846 unsigned long abbrev_id;
6848 dw_attr_ref a_attr;
6850 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6852 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6854 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
6855 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
6856 dwarf_tag_name (abbrev->die_tag));
6858 if (abbrev->die_child != NULL)
6859 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
6860 else
6861 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
6863 for (a_attr = abbrev->die_attr; a_attr != NULL;
6864 a_attr = a_attr->dw_attr_next)
6866 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
6867 dwarf_attr_name (a_attr->dw_attr));
6868 output_value_format (a_attr);
6871 dw2_asm_output_data (1, 0, NULL);
6872 dw2_asm_output_data (1, 0, NULL);
6875 /* Terminate the table. */
6876 dw2_asm_output_data (1, 0, NULL);
6879 /* Output a symbol we can use to refer to this DIE from another CU. */
6881 static inline void
6882 output_die_symbol (dw_die_ref die)
6884 char *sym = die->die_symbol;
6886 if (sym == 0)
6887 return;
6889 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
6890 /* We make these global, not weak; if the target doesn't support
6891 .linkonce, it doesn't support combining the sections, so debugging
6892 will break. */
6893 targetm.asm_out.globalize_label (asm_out_file, sym);
6895 ASM_OUTPUT_LABEL (asm_out_file, sym);
6898 /* Return a new location list, given the begin and end range, and the
6899 expression. gensym tells us whether to generate a new internal symbol for
6900 this location list node, which is done for the head of the list only. */
6902 static inline dw_loc_list_ref
6903 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
6904 const char *section, unsigned int gensym)
6906 dw_loc_list_ref retlist = ggc_alloc_cleared (sizeof (dw_loc_list_node));
6908 retlist->begin = begin;
6909 retlist->end = end;
6910 retlist->expr = expr;
6911 retlist->section = section;
6912 if (gensym)
6913 retlist->ll_symbol = gen_internal_sym ("LLST");
6915 return retlist;
6918 /* Add a location description expression to a location list. */
6920 static inline void
6921 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
6922 const char *begin, const char *end,
6923 const char *section)
6925 dw_loc_list_ref *d;
6927 /* Find the end of the chain. */
6928 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
6931 /* Add a new location list node to the list. */
6932 *d = new_loc_list (descr, begin, end, section, 0);
6935 static void
6936 dwarf2out_switch_text_section (void)
6938 dw_fde_ref fde;
6940 gcc_assert (cfun);
6942 fde = &fde_table[fde_table_in_use - 1];
6943 fde->dw_fde_switched_sections = true;
6944 fde->dw_fde_hot_section_label = cfun->hot_section_label;
6945 fde->dw_fde_hot_section_end_label = cfun->hot_section_end_label;
6946 fde->dw_fde_unlikely_section_label = cfun->cold_section_label;
6947 fde->dw_fde_unlikely_section_end_label = cfun->cold_section_end_label;
6948 have_multiple_function_sections = true;
6951 /* Output the location list given to us. */
6953 static void
6954 output_loc_list (dw_loc_list_ref list_head)
6956 dw_loc_list_ref curr = list_head;
6958 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
6960 /* Walk the location list, and output each range + expression. */
6961 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
6963 unsigned long size;
6964 if (!have_multiple_function_sections)
6966 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
6967 "Location list begin address (%s)",
6968 list_head->ll_symbol);
6969 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
6970 "Location list end address (%s)",
6971 list_head->ll_symbol);
6973 else
6975 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
6976 "Location list begin address (%s)",
6977 list_head->ll_symbol);
6978 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
6979 "Location list end address (%s)",
6980 list_head->ll_symbol);
6982 size = size_of_locs (curr->expr);
6984 /* Output the block length for this list of location operations. */
6985 gcc_assert (size <= 0xffff);
6986 dw2_asm_output_data (2, size, "%s", "Location expression size");
6988 output_loc_sequence (curr->expr);
6991 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
6992 "Location list terminator begin (%s)",
6993 list_head->ll_symbol);
6994 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
6995 "Location list terminator end (%s)",
6996 list_head->ll_symbol);
6999 /* Output the DIE and its attributes. Called recursively to generate
7000 the definitions of each child DIE. */
7002 static void
7003 output_die (dw_die_ref die)
7005 dw_attr_ref a;
7006 dw_die_ref c;
7007 unsigned long size;
7009 /* If someone in another CU might refer to us, set up a symbol for
7010 them to point to. */
7011 if (die->die_symbol)
7012 output_die_symbol (die);
7014 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
7015 die->die_offset, dwarf_tag_name (die->die_tag));
7017 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
7019 const char *name = dwarf_attr_name (a->dw_attr);
7021 switch (AT_class (a))
7023 case dw_val_class_addr:
7024 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
7025 break;
7027 case dw_val_class_offset:
7028 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
7029 "%s", name);
7030 break;
7032 case dw_val_class_range_list:
7034 char *p = strchr (ranges_section_label, '\0');
7036 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
7037 a->dw_attr_val.v.val_offset);
7038 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
7039 debug_ranges_section, "%s", name);
7040 *p = '\0';
7042 break;
7044 case dw_val_class_loc:
7045 size = size_of_locs (AT_loc (a));
7047 /* Output the block length for this list of location operations. */
7048 dw2_asm_output_data (constant_size (size), size, "%s", name);
7050 output_loc_sequence (AT_loc (a));
7051 break;
7053 case dw_val_class_const:
7054 /* ??? It would be slightly more efficient to use a scheme like is
7055 used for unsigned constants below, but gdb 4.x does not sign
7056 extend. Gdb 5.x does sign extend. */
7057 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
7058 break;
7060 case dw_val_class_unsigned_const:
7061 dw2_asm_output_data (constant_size (AT_unsigned (a)),
7062 AT_unsigned (a), "%s", name);
7063 break;
7065 case dw_val_class_long_long:
7067 unsigned HOST_WIDE_INT first, second;
7069 dw2_asm_output_data (1,
7070 2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7071 "%s", name);
7073 if (WORDS_BIG_ENDIAN)
7075 first = a->dw_attr_val.v.val_long_long.hi;
7076 second = a->dw_attr_val.v.val_long_long.low;
7078 else
7080 first = a->dw_attr_val.v.val_long_long.low;
7081 second = a->dw_attr_val.v.val_long_long.hi;
7084 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7085 first, "long long constant");
7086 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7087 second, NULL);
7089 break;
7091 case dw_val_class_vec:
7093 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
7094 unsigned int len = a->dw_attr_val.v.val_vec.length;
7095 unsigned int i;
7096 unsigned char *p;
7098 dw2_asm_output_data (1, len * elt_size, "%s", name);
7099 if (elt_size > sizeof (HOST_WIDE_INT))
7101 elt_size /= 2;
7102 len *= 2;
7104 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
7105 i < len;
7106 i++, p += elt_size)
7107 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
7108 "fp or vector constant word %u", i);
7109 break;
7112 case dw_val_class_flag:
7113 dw2_asm_output_data (1, AT_flag (a), "%s", name);
7114 break;
7116 case dw_val_class_loc_list:
7118 char *sym = AT_loc_list (a)->ll_symbol;
7120 gcc_assert (sym);
7121 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
7122 "%s", name);
7124 break;
7126 case dw_val_class_die_ref:
7127 if (AT_ref_external (a))
7129 char *sym = AT_ref (a)->die_symbol;
7131 gcc_assert (sym);
7132 dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, debug_info_section,
7133 "%s", name);
7135 else
7137 gcc_assert (AT_ref (a)->die_offset);
7138 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
7139 "%s", name);
7141 break;
7143 case dw_val_class_fde_ref:
7145 char l1[20];
7147 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
7148 a->dw_attr_val.v.val_fde_index * 2);
7149 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
7150 "%s", name);
7152 break;
7154 case dw_val_class_lbl_id:
7155 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
7156 break;
7158 case dw_val_class_lineptr:
7159 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7160 debug_line_section, "%s", name);
7161 break;
7163 case dw_val_class_macptr:
7164 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7165 debug_macinfo_section, "%s", name);
7166 break;
7168 case dw_val_class_str:
7169 if (AT_string_form (a) == DW_FORM_strp)
7170 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
7171 a->dw_attr_val.v.val_str->label,
7172 debug_str_section,
7173 "%s: \"%s\"", name, AT_string (a));
7174 else
7175 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
7176 break;
7178 default:
7179 gcc_unreachable ();
7183 for (c = die->die_child; c != NULL; c = c->die_sib)
7184 output_die (c);
7186 /* Add null byte to terminate sibling list. */
7187 if (die->die_child != NULL)
7188 dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
7189 die->die_offset);
7192 /* Output the compilation unit that appears at the beginning of the
7193 .debug_info section, and precedes the DIE descriptions. */
7195 static void
7196 output_compilation_unit_header (void)
7198 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7199 dw2_asm_output_data (4, 0xffffffff,
7200 "Initial length escape value indicating 64-bit DWARF extension");
7201 dw2_asm_output_data (DWARF_OFFSET_SIZE,
7202 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
7203 "Length of Compilation Unit Info");
7204 dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
7205 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
7206 debug_abbrev_section,
7207 "Offset Into Abbrev. Section");
7208 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
7211 /* Output the compilation unit DIE and its children. */
7213 static void
7214 output_comp_unit (dw_die_ref die, int output_if_empty)
7216 const char *secname;
7217 char *oldsym, *tmp;
7219 /* Unless we are outputting main CU, we may throw away empty ones. */
7220 if (!output_if_empty && die->die_child == NULL)
7221 return;
7223 /* Even if there are no children of this DIE, we must output the information
7224 about the compilation unit. Otherwise, on an empty translation unit, we
7225 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
7226 will then complain when examining the file. First mark all the DIEs in
7227 this CU so we know which get local refs. */
7228 mark_dies (die);
7230 build_abbrev_table (die);
7232 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
7233 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7234 calc_die_sizes (die);
7236 oldsym = die->die_symbol;
7237 if (oldsym)
7239 tmp = alloca (strlen (oldsym) + 24);
7241 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
7242 secname = tmp;
7243 die->die_symbol = NULL;
7244 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
7246 else
7247 switch_to_section (debug_info_section);
7249 /* Output debugging information. */
7250 output_compilation_unit_header ();
7251 output_die (die);
7253 /* Leave the marks on the main CU, so we can check them in
7254 output_pubnames. */
7255 if (oldsym)
7257 unmark_dies (die);
7258 die->die_symbol = oldsym;
7262 /* The DWARF2 pubname for a nested thingy looks like "A::f". The
7263 output of lang_hooks.decl_printable_name for C++ looks like
7264 "A::f(int)". Let's drop the argument list, and maybe the scope. */
7266 static const char *
7267 dwarf2_name (tree decl, int scope)
7269 return lang_hooks.decl_printable_name (decl, scope ? 1 : 0);
7272 /* Add a new entry to .debug_pubnames if appropriate. */
7274 static void
7275 add_pubname (tree decl, dw_die_ref die)
7277 pubname_ref p;
7279 if (! TREE_PUBLIC (decl))
7280 return;
7282 if (pubname_table_in_use == pubname_table_allocated)
7284 pubname_table_allocated += PUBNAME_TABLE_INCREMENT;
7285 pubname_table
7286 = ggc_realloc (pubname_table,
7287 (pubname_table_allocated * sizeof (pubname_entry)));
7288 memset (pubname_table + pubname_table_in_use, 0,
7289 PUBNAME_TABLE_INCREMENT * sizeof (pubname_entry));
7292 p = &pubname_table[pubname_table_in_use++];
7293 p->die = die;
7294 p->name = xstrdup (dwarf2_name (decl, 1));
7297 /* Output the public names table used to speed up access to externally
7298 visible names. For now, only generate entries for externally
7299 visible procedures. */
7301 static void
7302 output_pubnames (void)
7304 unsigned i;
7305 unsigned long pubnames_length = size_of_pubnames ();
7307 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7308 dw2_asm_output_data (4, 0xffffffff,
7309 "Initial length escape value indicating 64-bit DWARF extension");
7310 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7311 "Length of Public Names Info");
7312 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7313 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7314 debug_info_section,
7315 "Offset of Compilation Unit Info");
7316 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
7317 "Compilation Unit Length");
7319 for (i = 0; i < pubname_table_in_use; i++)
7321 pubname_ref pub = &pubname_table[i];
7323 /* We shouldn't see pubnames for DIEs outside of the main CU. */
7324 gcc_assert (pub->die->die_mark);
7326 dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
7327 "DIE offset");
7329 dw2_asm_output_nstring (pub->name, -1, "external name");
7332 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
7335 /* Add a new entry to .debug_aranges if appropriate. */
7337 static void
7338 add_arange (tree decl, dw_die_ref die)
7340 if (! DECL_SECTION_NAME (decl))
7341 return;
7343 if (arange_table_in_use == arange_table_allocated)
7345 arange_table_allocated += ARANGE_TABLE_INCREMENT;
7346 arange_table = ggc_realloc (arange_table,
7347 (arange_table_allocated
7348 * sizeof (dw_die_ref)));
7349 memset (arange_table + arange_table_in_use, 0,
7350 ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
7353 arange_table[arange_table_in_use++] = die;
7356 /* Output the information that goes into the .debug_aranges table.
7357 Namely, define the beginning and ending address range of the
7358 text section generated for this compilation unit. */
7360 static void
7361 output_aranges (void)
7363 unsigned i;
7364 unsigned long aranges_length = size_of_aranges ();
7366 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7367 dw2_asm_output_data (4, 0xffffffff,
7368 "Initial length escape value indicating 64-bit DWARF extension");
7369 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
7370 "Length of Address Ranges Info");
7371 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7372 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7373 debug_info_section,
7374 "Offset of Compilation Unit Info");
7375 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
7376 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
7378 /* We need to align to twice the pointer size here. */
7379 if (DWARF_ARANGES_PAD_SIZE)
7381 /* Pad using a 2 byte words so that padding is correct for any
7382 pointer size. */
7383 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
7384 2 * DWARF2_ADDR_SIZE);
7385 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
7386 dw2_asm_output_data (2, 0, NULL);
7389 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
7390 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
7391 text_section_label, "Length");
7392 if (flag_reorder_blocks_and_partition)
7394 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
7395 "Address");
7396 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
7397 cold_text_section_label, "Length");
7400 for (i = 0; i < arange_table_in_use; i++)
7402 dw_die_ref die = arange_table[i];
7404 /* We shouldn't see aranges for DIEs outside of the main CU. */
7405 gcc_assert (die->die_mark);
7407 if (die->die_tag == DW_TAG_subprogram)
7409 dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
7410 "Address");
7411 dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
7412 get_AT_low_pc (die), "Length");
7414 else
7416 /* A static variable; extract the symbol from DW_AT_location.
7417 Note that this code isn't currently hit, as we only emit
7418 aranges for functions (jason 9/23/99). */
7419 dw_attr_ref a = get_AT (die, DW_AT_location);
7420 dw_loc_descr_ref loc;
7422 gcc_assert (a && AT_class (a) == dw_val_class_loc);
7424 loc = AT_loc (a);
7425 gcc_assert (loc->dw_loc_opc == DW_OP_addr);
7427 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
7428 loc->dw_loc_oprnd1.v.val_addr, "Address");
7429 dw2_asm_output_data (DWARF2_ADDR_SIZE,
7430 get_AT_unsigned (die, DW_AT_byte_size),
7431 "Length");
7435 /* Output the terminator words. */
7436 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7437 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7440 /* Add a new entry to .debug_ranges. Return the offset at which it
7441 was placed. */
7443 static unsigned int
7444 add_ranges (tree block)
7446 unsigned int in_use = ranges_table_in_use;
7448 if (in_use == ranges_table_allocated)
7450 ranges_table_allocated += RANGES_TABLE_INCREMENT;
7451 ranges_table
7452 = ggc_realloc (ranges_table, (ranges_table_allocated
7453 * sizeof (struct dw_ranges_struct)));
7454 memset (ranges_table + ranges_table_in_use, 0,
7455 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
7458 ranges_table[in_use].block_num = (block ? BLOCK_NUMBER (block) : 0);
7459 ranges_table_in_use = in_use + 1;
7461 return in_use * 2 * DWARF2_ADDR_SIZE;
7464 static void
7465 output_ranges (void)
7467 unsigned i;
7468 static const char *const start_fmt = "Offset 0x%x";
7469 const char *fmt = start_fmt;
7471 for (i = 0; i < ranges_table_in_use; i++)
7473 int block_num = ranges_table[i].block_num;
7475 if (block_num)
7477 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
7478 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
7480 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
7481 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
7483 /* If all code is in the text section, then the compilation
7484 unit base address defaults to DW_AT_low_pc, which is the
7485 base of the text section. */
7486 if (!have_multiple_function_sections)
7488 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
7489 text_section_label,
7490 fmt, i * 2 * DWARF2_ADDR_SIZE);
7491 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
7492 text_section_label, NULL);
7495 /* Otherwise, we add a DW_AT_entry_pc attribute to force the
7496 compilation unit base address to zero, which allows us to
7497 use absolute addresses, and not worry about whether the
7498 target supports cross-section arithmetic. */
7499 else
7501 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
7502 fmt, i * 2 * DWARF2_ADDR_SIZE);
7503 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
7506 fmt = NULL;
7508 else
7510 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7511 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7512 fmt = start_fmt;
7517 /* Data structure containing information about input files. */
7518 struct file_info
7520 char *path; /* Complete file name. */
7521 char *fname; /* File name part. */
7522 int length; /* Length of entire string. */
7523 int file_idx; /* Index in input file table. */
7524 int dir_idx; /* Index in directory table. */
7527 /* Data structure containing information about directories with source
7528 files. */
7529 struct dir_info
7531 char *path; /* Path including directory name. */
7532 int length; /* Path length. */
7533 int prefix; /* Index of directory entry which is a prefix. */
7534 int count; /* Number of files in this directory. */
7535 int dir_idx; /* Index of directory used as base. */
7536 int used; /* Used in the end? */
7539 /* Callback function for file_info comparison. We sort by looking at
7540 the directories in the path. */
7542 static int
7543 file_info_cmp (const void *p1, const void *p2)
7545 const struct file_info *s1 = p1;
7546 const struct file_info *s2 = p2;
7547 unsigned char *cp1;
7548 unsigned char *cp2;
7550 /* Take care of file names without directories. We need to make sure that
7551 we return consistent values to qsort since some will get confused if
7552 we return the same value when identical operands are passed in opposite
7553 orders. So if neither has a directory, return 0 and otherwise return
7554 1 or -1 depending on which one has the directory. */
7555 if ((s1->path == s1->fname || s2->path == s2->fname))
7556 return (s2->path == s2->fname) - (s1->path == s1->fname);
7558 cp1 = (unsigned char *) s1->path;
7559 cp2 = (unsigned char *) s2->path;
7561 while (1)
7563 ++cp1;
7564 ++cp2;
7565 /* Reached the end of the first path? If so, handle like above. */
7566 if ((cp1 == (unsigned char *) s1->fname)
7567 || (cp2 == (unsigned char *) s2->fname))
7568 return ((cp2 == (unsigned char *) s2->fname)
7569 - (cp1 == (unsigned char *) s1->fname));
7571 /* Character of current path component the same? */
7572 else if (*cp1 != *cp2)
7573 return *cp1 - *cp2;
7577 /* Output the directory table and the file name table. We try to minimize
7578 the total amount of memory needed. A heuristic is used to avoid large
7579 slowdowns with many input files. */
7581 static void
7582 output_file_names (void)
7584 struct file_info *files;
7585 struct dir_info *dirs;
7586 int *saved;
7587 int *savehere;
7588 int *backmap;
7589 size_t ndirs;
7590 int idx_offset;
7591 size_t i;
7592 int idx;
7594 /* Handle the case where file_table is empty. */
7595 if (VARRAY_ACTIVE_SIZE (file_table) <= 1)
7597 dw2_asm_output_data (1, 0, "End directory table");
7598 dw2_asm_output_data (1, 0, "End file name table");
7599 return;
7602 /* Allocate the various arrays we need. */
7603 files = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (struct file_info));
7604 dirs = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (struct dir_info));
7606 /* Sort the file names. */
7607 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7609 char *f;
7611 /* Skip all leading "./". */
7612 f = VARRAY_CHAR_PTR (file_table, i);
7613 while (f[0] == '.' && f[1] == '/')
7614 f += 2;
7616 /* Create a new array entry. */
7617 files[i].path = f;
7618 files[i].length = strlen (f);
7619 files[i].file_idx = i;
7621 /* Search for the file name part. */
7622 f = strrchr (f, '/');
7623 files[i].fname = f == NULL ? files[i].path : f + 1;
7626 qsort (files + 1, VARRAY_ACTIVE_SIZE (file_table) - 1,
7627 sizeof (files[0]), file_info_cmp);
7629 /* Find all the different directories used. */
7630 dirs[0].path = files[1].path;
7631 dirs[0].length = files[1].fname - files[1].path;
7632 dirs[0].prefix = -1;
7633 dirs[0].count = 1;
7634 dirs[0].dir_idx = 0;
7635 dirs[0].used = 0;
7636 files[1].dir_idx = 0;
7637 ndirs = 1;
7639 for (i = 2; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7640 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
7641 && memcmp (dirs[ndirs - 1].path, files[i].path,
7642 dirs[ndirs - 1].length) == 0)
7644 /* Same directory as last entry. */
7645 files[i].dir_idx = ndirs - 1;
7646 ++dirs[ndirs - 1].count;
7648 else
7650 size_t j;
7652 /* This is a new directory. */
7653 dirs[ndirs].path = files[i].path;
7654 dirs[ndirs].length = files[i].fname - files[i].path;
7655 dirs[ndirs].count = 1;
7656 dirs[ndirs].dir_idx = ndirs;
7657 dirs[ndirs].used = 0;
7658 files[i].dir_idx = ndirs;
7660 /* Search for a prefix. */
7661 dirs[ndirs].prefix = -1;
7662 for (j = 0; j < ndirs; j++)
7663 if (dirs[j].length < dirs[ndirs].length
7664 && dirs[j].length > 1
7665 && (dirs[ndirs].prefix == -1
7666 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
7667 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
7668 dirs[ndirs].prefix = j;
7670 ++ndirs;
7673 /* Now to the actual work. We have to find a subset of the directories which
7674 allow expressing the file name using references to the directory table
7675 with the least amount of characters. We do not do an exhaustive search
7676 where we would have to check out every combination of every single
7677 possible prefix. Instead we use a heuristic which provides nearly optimal
7678 results in most cases and never is much off. */
7679 saved = alloca (ndirs * sizeof (int));
7680 savehere = alloca (ndirs * sizeof (int));
7682 memset (saved, '\0', ndirs * sizeof (saved[0]));
7683 for (i = 0; i < ndirs; i++)
7685 size_t j;
7686 int total;
7688 /* We can always save some space for the current directory. But this
7689 does not mean it will be enough to justify adding the directory. */
7690 savehere[i] = dirs[i].length;
7691 total = (savehere[i] - saved[i]) * dirs[i].count;
7693 for (j = i + 1; j < ndirs; j++)
7695 savehere[j] = 0;
7696 if (saved[j] < dirs[i].length)
7698 /* Determine whether the dirs[i] path is a prefix of the
7699 dirs[j] path. */
7700 int k;
7702 k = dirs[j].prefix;
7703 while (k != -1 && k != (int) i)
7704 k = dirs[k].prefix;
7706 if (k == (int) i)
7708 /* Yes it is. We can possibly safe some memory but
7709 writing the filenames in dirs[j] relative to
7710 dirs[i]. */
7711 savehere[j] = dirs[i].length;
7712 total += (savehere[j] - saved[j]) * dirs[j].count;
7717 /* Check whether we can safe enough to justify adding the dirs[i]
7718 directory. */
7719 if (total > dirs[i].length + 1)
7721 /* It's worthwhile adding. */
7722 for (j = i; j < ndirs; j++)
7723 if (savehere[j] > 0)
7725 /* Remember how much we saved for this directory so far. */
7726 saved[j] = savehere[j];
7728 /* Remember the prefix directory. */
7729 dirs[j].dir_idx = i;
7734 /* We have to emit them in the order they appear in the file_table array
7735 since the index is used in the debug info generation. To do this
7736 efficiently we generate a back-mapping of the indices first. */
7737 backmap = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (int));
7738 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7740 backmap[files[i].file_idx] = i;
7742 /* Mark this directory as used. */
7743 dirs[dirs[files[i].dir_idx].dir_idx].used = 1;
7746 /* That was it. We are ready to emit the information. First emit the
7747 directory name table. We have to make sure the first actually emitted
7748 directory name has index one; zero is reserved for the current working
7749 directory. Make sure we do not confuse these indices with the one for the
7750 constructed table (even though most of the time they are identical). */
7751 idx = 1;
7752 idx_offset = dirs[0].length > 0 ? 1 : 0;
7753 for (i = 1 - idx_offset; i < ndirs; i++)
7754 if (dirs[i].used != 0)
7756 dirs[i].used = idx++;
7757 dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
7758 "Directory Entry: 0x%x", dirs[i].used);
7761 dw2_asm_output_data (1, 0, "End directory table");
7763 /* Correct the index for the current working directory entry if it
7764 exists. */
7765 if (idx_offset == 0)
7766 dirs[0].used = 0;
7768 /* Now write all the file names. */
7769 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7771 int file_idx = backmap[i];
7772 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
7774 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
7775 "File Entry: 0x%lx", (unsigned long) i);
7777 /* Include directory index. */
7778 dw2_asm_output_data_uleb128 (dirs[dir_idx].used, NULL);
7780 /* Modification time. */
7781 dw2_asm_output_data_uleb128 (0, NULL);
7783 /* File length in bytes. */
7784 dw2_asm_output_data_uleb128 (0, NULL);
7787 dw2_asm_output_data (1, 0, "End file name table");
7791 /* Output the source line number correspondence information. This
7792 information goes into the .debug_line section. */
7794 static void
7795 output_line_info (void)
7797 char l1[20], l2[20], p1[20], p2[20];
7798 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
7799 char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
7800 unsigned opc;
7801 unsigned n_op_args;
7802 unsigned long lt_index;
7803 unsigned long current_line;
7804 long line_offset;
7805 long line_delta;
7806 unsigned long current_file;
7807 unsigned long function;
7809 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
7810 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
7811 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
7812 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
7814 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7815 dw2_asm_output_data (4, 0xffffffff,
7816 "Initial length escape value indicating 64-bit DWARF extension");
7817 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
7818 "Length of Source Line Info");
7819 ASM_OUTPUT_LABEL (asm_out_file, l1);
7821 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7822 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
7823 ASM_OUTPUT_LABEL (asm_out_file, p1);
7825 /* Define the architecture-dependent minimum instruction length (in
7826 bytes). In this implementation of DWARF, this field is used for
7827 information purposes only. Since GCC generates assembly language,
7828 we have no a priori knowledge of how many instruction bytes are
7829 generated for each source line, and therefore can use only the
7830 DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
7831 commands. Accordingly, we fix this as `1', which is "correct
7832 enough" for all architectures, and don't let the target override. */
7833 dw2_asm_output_data (1, 1,
7834 "Minimum Instruction Length");
7836 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
7837 "Default is_stmt_start flag");
7838 dw2_asm_output_data (1, DWARF_LINE_BASE,
7839 "Line Base Value (Special Opcodes)");
7840 dw2_asm_output_data (1, DWARF_LINE_RANGE,
7841 "Line Range Value (Special Opcodes)");
7842 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
7843 "Special Opcode Base");
7845 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
7847 switch (opc)
7849 case DW_LNS_advance_pc:
7850 case DW_LNS_advance_line:
7851 case DW_LNS_set_file:
7852 case DW_LNS_set_column:
7853 case DW_LNS_fixed_advance_pc:
7854 n_op_args = 1;
7855 break;
7856 default:
7857 n_op_args = 0;
7858 break;
7861 dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
7862 opc, n_op_args);
7865 /* Write out the information about the files we use. */
7866 output_file_names ();
7867 ASM_OUTPUT_LABEL (asm_out_file, p2);
7869 /* We used to set the address register to the first location in the text
7870 section here, but that didn't accomplish anything since we already
7871 have a line note for the opening brace of the first function. */
7873 /* Generate the line number to PC correspondence table, encoded as
7874 a series of state machine operations. */
7875 current_file = 1;
7876 current_line = 1;
7878 if (cfun && in_cold_section_p)
7879 strcpy (prev_line_label, cfun->cold_section_label);
7880 else
7881 strcpy (prev_line_label, text_section_label);
7882 for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
7884 dw_line_info_ref line_info = &line_info_table[lt_index];
7886 #if 0
7887 /* Disable this optimization for now; GDB wants to see two line notes
7888 at the beginning of a function so it can find the end of the
7889 prologue. */
7891 /* Don't emit anything for redundant notes. Just updating the
7892 address doesn't accomplish anything, because we already assume
7893 that anything after the last address is this line. */
7894 if (line_info->dw_line_num == current_line
7895 && line_info->dw_file_num == current_file)
7896 continue;
7897 #endif
7899 /* Emit debug info for the address of the current line.
7901 Unfortunately, we have little choice here currently, and must always
7902 use the most general form. GCC does not know the address delta
7903 itself, so we can't use DW_LNS_advance_pc. Many ports do have length
7904 attributes which will give an upper bound on the address range. We
7905 could perhaps use length attributes to determine when it is safe to
7906 use DW_LNS_fixed_advance_pc. */
7908 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
7909 if (0)
7911 /* This can handle deltas up to 0xffff. This takes 3 bytes. */
7912 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7913 "DW_LNS_fixed_advance_pc");
7914 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
7916 else
7918 /* This can handle any delta. This takes
7919 4+DWARF2_ADDR_SIZE bytes. */
7920 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7921 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7922 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7923 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7926 strcpy (prev_line_label, line_label);
7928 /* Emit debug info for the source file of the current line, if
7929 different from the previous line. */
7930 if (line_info->dw_file_num != current_file)
7932 current_file = line_info->dw_file_num;
7933 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
7934 dw2_asm_output_data_uleb128 (current_file, "(\"%s\")",
7935 VARRAY_CHAR_PTR (file_table,
7936 current_file));
7939 /* Emit debug info for the current line number, choosing the encoding
7940 that uses the least amount of space. */
7941 if (line_info->dw_line_num != current_line)
7943 line_offset = line_info->dw_line_num - current_line;
7944 line_delta = line_offset - DWARF_LINE_BASE;
7945 current_line = line_info->dw_line_num;
7946 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
7947 /* This can handle deltas from -10 to 234, using the current
7948 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE. This
7949 takes 1 byte. */
7950 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
7951 "line %lu", current_line);
7952 else
7954 /* This can handle any delta. This takes at least 4 bytes,
7955 depending on the value being encoded. */
7956 dw2_asm_output_data (1, DW_LNS_advance_line,
7957 "advance to line %lu", current_line);
7958 dw2_asm_output_data_sleb128 (line_offset, NULL);
7959 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7962 else
7963 /* We still need to start a new row, so output a copy insn. */
7964 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7967 /* Emit debug info for the address of the end of the function. */
7968 if (0)
7970 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7971 "DW_LNS_fixed_advance_pc");
7972 dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
7974 else
7976 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7977 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7978 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7979 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
7982 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
7983 dw2_asm_output_data_uleb128 (1, NULL);
7984 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
7986 function = 0;
7987 current_file = 1;
7988 current_line = 1;
7989 for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
7991 dw_separate_line_info_ref line_info
7992 = &separate_line_info_table[lt_index];
7994 #if 0
7995 /* Don't emit anything for redundant notes. */
7996 if (line_info->dw_line_num == current_line
7997 && line_info->dw_file_num == current_file
7998 && line_info->function == function)
7999 goto cont;
8000 #endif
8002 /* Emit debug info for the address of the current line. If this is
8003 a new function, or the first line of a function, then we need
8004 to handle it differently. */
8005 ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
8006 lt_index);
8007 if (function != line_info->function)
8009 function = line_info->function;
8011 /* Set the address register to the first line in the function. */
8012 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8013 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8014 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8015 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8017 else
8019 /* ??? See the DW_LNS_advance_pc comment above. */
8020 if (0)
8022 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8023 "DW_LNS_fixed_advance_pc");
8024 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8026 else
8028 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8029 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8030 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8031 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8035 strcpy (prev_line_label, line_label);
8037 /* Emit debug info for the source file of the current line, if
8038 different from the previous line. */
8039 if (line_info->dw_file_num != current_file)
8041 current_file = line_info->dw_file_num;
8042 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8043 dw2_asm_output_data_uleb128 (current_file, "(\"%s\")",
8044 VARRAY_CHAR_PTR (file_table,
8045 current_file));
8048 /* Emit debug info for the current line number, choosing the encoding
8049 that uses the least amount of space. */
8050 if (line_info->dw_line_num != current_line)
8052 line_offset = line_info->dw_line_num - current_line;
8053 line_delta = line_offset - DWARF_LINE_BASE;
8054 current_line = line_info->dw_line_num;
8055 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
8056 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
8057 "line %lu", current_line);
8058 else
8060 dw2_asm_output_data (1, DW_LNS_advance_line,
8061 "advance to line %lu", current_line);
8062 dw2_asm_output_data_sleb128 (line_offset, NULL);
8063 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8066 else
8067 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8069 #if 0
8070 cont:
8071 #endif
8073 lt_index++;
8075 /* If we're done with a function, end its sequence. */
8076 if (lt_index == separate_line_info_table_in_use
8077 || separate_line_info_table[lt_index].function != function)
8079 current_file = 1;
8080 current_line = 1;
8082 /* Emit debug info for the address of the end of the function. */
8083 ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
8084 if (0)
8086 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8087 "DW_LNS_fixed_advance_pc");
8088 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8090 else
8092 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8093 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8094 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8095 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8098 /* Output the marker for the end of this sequence. */
8099 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
8100 dw2_asm_output_data_uleb128 (1, NULL);
8101 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
8105 /* Output the marker for the end of the line number info. */
8106 ASM_OUTPUT_LABEL (asm_out_file, l2);
8109 /* Given a pointer to a tree node for some base type, return a pointer to
8110 a DIE that describes the given type.
8112 This routine must only be called for GCC type nodes that correspond to
8113 Dwarf base (fundamental) types. */
8115 static dw_die_ref
8116 base_type_die (tree type)
8118 dw_die_ref base_type_result;
8119 enum dwarf_type encoding;
8121 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
8122 return 0;
8124 switch (TREE_CODE (type))
8126 case INTEGER_TYPE:
8127 if (TYPE_STRING_FLAG (type))
8129 if (TYPE_UNSIGNED (type))
8130 encoding = DW_ATE_unsigned_char;
8131 else
8132 encoding = DW_ATE_signed_char;
8134 else if (TYPE_UNSIGNED (type))
8135 encoding = DW_ATE_unsigned;
8136 else
8137 encoding = DW_ATE_signed;
8138 break;
8140 case REAL_TYPE:
8141 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
8142 encoding = DW_ATE_decimal_float;
8143 else
8144 encoding = DW_ATE_float;
8145 break;
8147 /* Dwarf2 doesn't know anything about complex ints, so use
8148 a user defined type for it. */
8149 case COMPLEX_TYPE:
8150 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
8151 encoding = DW_ATE_complex_float;
8152 else
8153 encoding = DW_ATE_lo_user;
8154 break;
8156 case BOOLEAN_TYPE:
8157 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
8158 encoding = DW_ATE_boolean;
8159 break;
8161 default:
8162 /* No other TREE_CODEs are Dwarf fundamental types. */
8163 gcc_unreachable ();
8166 base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
8168 /* This probably indicates a bug. */
8169 if (! TYPE_NAME (type))
8170 add_name_attribute (base_type_result, "__unknown__");
8172 add_AT_unsigned (base_type_result, DW_AT_byte_size,
8173 int_size_in_bytes (type));
8174 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
8176 return base_type_result;
8179 /* Given a pointer to an arbitrary ..._TYPE tree node, return a pointer to
8180 the Dwarf "root" type for the given input type. The Dwarf "root" type of
8181 a given type is generally the same as the given type, except that if the
8182 given type is a pointer or reference type, then the root type of the given
8183 type is the root type of the "basis" type for the pointer or reference
8184 type. (This definition of the "root" type is recursive.) Also, the root
8185 type of a `const' qualified type or a `volatile' qualified type is the
8186 root type of the given type without the qualifiers. */
8188 static tree
8189 root_type (tree type)
8191 if (TREE_CODE (type) == ERROR_MARK)
8192 return error_mark_node;
8194 switch (TREE_CODE (type))
8196 case ERROR_MARK:
8197 return error_mark_node;
8199 case POINTER_TYPE:
8200 case REFERENCE_TYPE:
8201 return type_main_variant (root_type (TREE_TYPE (type)));
8203 default:
8204 return type_main_variant (type);
8208 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
8209 given input type is a Dwarf "fundamental" type. Otherwise return null. */
8211 static inline int
8212 is_base_type (tree type)
8214 switch (TREE_CODE (type))
8216 case ERROR_MARK:
8217 case VOID_TYPE:
8218 case INTEGER_TYPE:
8219 case REAL_TYPE:
8220 case COMPLEX_TYPE:
8221 case BOOLEAN_TYPE:
8222 return 1;
8224 case ARRAY_TYPE:
8225 case RECORD_TYPE:
8226 case UNION_TYPE:
8227 case QUAL_UNION_TYPE:
8228 case ENUMERAL_TYPE:
8229 case FUNCTION_TYPE:
8230 case METHOD_TYPE:
8231 case POINTER_TYPE:
8232 case REFERENCE_TYPE:
8233 case OFFSET_TYPE:
8234 case LANG_TYPE:
8235 case VECTOR_TYPE:
8236 return 0;
8238 default:
8239 gcc_unreachable ();
8242 return 0;
8245 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
8246 node, return the size in bits for the type if it is a constant, or else
8247 return the alignment for the type if the type's size is not constant, or
8248 else return BITS_PER_WORD if the type actually turns out to be an
8249 ERROR_MARK node. */
8251 static inline unsigned HOST_WIDE_INT
8252 simple_type_size_in_bits (tree type)
8254 if (TREE_CODE (type) == ERROR_MARK)
8255 return BITS_PER_WORD;
8256 else if (TYPE_SIZE (type) == NULL_TREE)
8257 return 0;
8258 else if (host_integerp (TYPE_SIZE (type), 1))
8259 return tree_low_cst (TYPE_SIZE (type), 1);
8260 else
8261 return TYPE_ALIGN (type);
8264 /* Return true if the debug information for the given type should be
8265 emitted as a subrange type. */
8267 static inline bool
8268 is_subrange_type (tree type)
8270 tree subtype = TREE_TYPE (type);
8272 /* Subrange types are identified by the fact that they are integer
8273 types, and that they have a subtype which is either an integer type
8274 or an enumeral type. */
8276 if (TREE_CODE (type) != INTEGER_TYPE
8277 || subtype == NULL_TREE)
8278 return false;
8280 if (TREE_CODE (subtype) != INTEGER_TYPE
8281 && TREE_CODE (subtype) != ENUMERAL_TYPE)
8282 return false;
8284 if (TREE_CODE (type) == TREE_CODE (subtype)
8285 && int_size_in_bytes (type) == int_size_in_bytes (subtype)
8286 && TYPE_MIN_VALUE (type) != NULL
8287 && TYPE_MIN_VALUE (subtype) != NULL
8288 && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
8289 && TYPE_MAX_VALUE (type) != NULL
8290 && TYPE_MAX_VALUE (subtype) != NULL
8291 && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
8293 /* The type and its subtype have the same representation. If in
8294 addition the two types also have the same name, then the given
8295 type is not a subrange type, but rather a plain base type. */
8296 /* FIXME: brobecker/2004-03-22:
8297 Sizetype INTEGER_CSTs nodes are canonicalized. It should
8298 therefore be sufficient to check the TYPE_SIZE node pointers
8299 rather than checking the actual size. Unfortunately, we have
8300 found some cases, such as in the Ada "integer" type, where
8301 this is not the case. Until this problem is solved, we need to
8302 keep checking the actual size. */
8303 tree type_name = TYPE_NAME (type);
8304 tree subtype_name = TYPE_NAME (subtype);
8306 if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
8307 type_name = DECL_NAME (type_name);
8309 if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
8310 subtype_name = DECL_NAME (subtype_name);
8312 if (type_name == subtype_name)
8313 return false;
8316 return true;
8319 /* Given a pointer to a tree node for a subrange type, return a pointer
8320 to a DIE that describes the given type. */
8322 static dw_die_ref
8323 subrange_type_die (tree type, dw_die_ref context_die)
8325 dw_die_ref subrange_die;
8326 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
8328 if (context_die == NULL)
8329 context_die = comp_unit_die;
8331 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
8333 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
8335 /* The size of the subrange type and its base type do not match,
8336 so we need to generate a size attribute for the subrange type. */
8337 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
8340 if (TYPE_MIN_VALUE (type) != NULL)
8341 add_bound_info (subrange_die, DW_AT_lower_bound,
8342 TYPE_MIN_VALUE (type));
8343 if (TYPE_MAX_VALUE (type) != NULL)
8344 add_bound_info (subrange_die, DW_AT_upper_bound,
8345 TYPE_MAX_VALUE (type));
8347 return subrange_die;
8350 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
8351 entry that chains various modifiers in front of the given type. */
8353 static dw_die_ref
8354 modified_type_die (tree type, int is_const_type, int is_volatile_type,
8355 dw_die_ref context_die)
8357 enum tree_code code = TREE_CODE (type);
8358 dw_die_ref mod_type_die;
8359 dw_die_ref sub_die = NULL;
8360 tree item_type = NULL;
8361 tree qualified_type;
8362 tree name;
8364 if (code == ERROR_MARK)
8365 return NULL;
8367 /* See if we already have the appropriately qualified variant of
8368 this type. */
8369 qualified_type
8370 = get_qualified_type (type,
8371 ((is_const_type ? TYPE_QUAL_CONST : 0)
8372 | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
8374 /* If we do, then we can just use its DIE, if it exists. */
8375 if (qualified_type)
8377 mod_type_die = lookup_type_die (qualified_type);
8378 if (mod_type_die)
8379 return mod_type_die;
8382 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
8384 /* Handle C typedef types. */
8385 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
8387 tree dtype = TREE_TYPE (name);
8389 if (qualified_type == dtype)
8391 /* For a named type, use the typedef. */
8392 gen_type_die (qualified_type, context_die);
8393 return lookup_type_die (qualified_type);
8395 else if (DECL_ORIGINAL_TYPE (name)
8396 && (is_const_type < TYPE_READONLY (dtype)
8397 || is_volatile_type < TYPE_VOLATILE (dtype)))
8398 /* cv-unqualified version of named type. Just use the unnamed
8399 type to which it refers. */
8400 return modified_type_die (DECL_ORIGINAL_TYPE (name),
8401 is_const_type, is_volatile_type,
8402 context_die);
8403 /* Else cv-qualified version of named type; fall through. */
8406 if (is_const_type)
8408 mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
8409 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
8411 else if (is_volatile_type)
8413 mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
8414 sub_die = modified_type_die (type, 0, 0, context_die);
8416 else if (code == POINTER_TYPE)
8418 mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
8419 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8420 simple_type_size_in_bits (type) / BITS_PER_UNIT);
8421 item_type = TREE_TYPE (type);
8423 else if (code == REFERENCE_TYPE)
8425 mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
8426 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8427 simple_type_size_in_bits (type) / BITS_PER_UNIT);
8428 item_type = TREE_TYPE (type);
8430 else if (is_subrange_type (type))
8432 mod_type_die = subrange_type_die (type, context_die);
8433 item_type = TREE_TYPE (type);
8435 else if (is_base_type (type))
8436 mod_type_die = base_type_die (type);
8437 else
8439 gen_type_die (type, context_die);
8441 /* We have to get the type_main_variant here (and pass that to the
8442 `lookup_type_die' routine) because the ..._TYPE node we have
8443 might simply be a *copy* of some original type node (where the
8444 copy was created to help us keep track of typedef names) and
8445 that copy might have a different TYPE_UID from the original
8446 ..._TYPE node. */
8447 if (TREE_CODE (type) != VECTOR_TYPE)
8448 return lookup_type_die (type_main_variant (type));
8449 else
8450 /* Vectors have the debugging information in the type,
8451 not the main variant. */
8452 return lookup_type_die (type);
8455 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
8456 don't output a DW_TAG_typedef, since there isn't one in the
8457 user's program; just attach a DW_AT_name to the type. */
8458 if (name
8459 && (TREE_CODE (name) != TYPE_DECL || TREE_TYPE (name) == qualified_type))
8461 if (TREE_CODE (name) == TYPE_DECL)
8462 /* Could just call add_name_and_src_coords_attributes here,
8463 but since this is a builtin type it doesn't have any
8464 useful source coordinates anyway. */
8465 name = DECL_NAME (name);
8466 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
8469 if (qualified_type)
8470 equate_type_number_to_die (qualified_type, mod_type_die);
8472 if (item_type)
8473 /* We must do this after the equate_type_number_to_die call, in case
8474 this is a recursive type. This ensures that the modified_type_die
8475 recursion will terminate even if the type is recursive. Recursive
8476 types are possible in Ada. */
8477 sub_die = modified_type_die (item_type,
8478 TYPE_READONLY (item_type),
8479 TYPE_VOLATILE (item_type),
8480 context_die);
8482 if (sub_die != NULL)
8483 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
8485 return mod_type_die;
8488 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
8489 an enumerated type. */
8491 static inline int
8492 type_is_enum (tree type)
8494 return TREE_CODE (type) == ENUMERAL_TYPE;
8497 /* Return the DBX register number described by a given RTL node. */
8499 static unsigned int
8500 dbx_reg_number (rtx rtl)
8502 unsigned regno = REGNO (rtl);
8504 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
8506 #ifdef LEAF_REG_REMAP
8507 regno = LEAF_REG_REMAP (regno);
8508 #endif
8510 return DBX_REGISTER_NUMBER (regno);
8513 /* Optionally add a DW_OP_piece term to a location description expression.
8514 DW_OP_piece is only added if the location description expression already
8515 doesn't end with DW_OP_piece. */
8517 static void
8518 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
8520 dw_loc_descr_ref loc;
8522 if (*list_head != NULL)
8524 /* Find the end of the chain. */
8525 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
8528 if (loc->dw_loc_opc != DW_OP_piece)
8529 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
8533 /* Return a location descriptor that designates a machine register or
8534 zero if there is none. */
8536 static dw_loc_descr_ref
8537 reg_loc_descriptor (rtx rtl)
8539 rtx regs;
8541 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
8542 return 0;
8544 regs = targetm.dwarf_register_span (rtl);
8546 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
8547 return multiple_reg_loc_descriptor (rtl, regs);
8548 else
8549 return one_reg_loc_descriptor (dbx_reg_number (rtl));
8552 /* Return a location descriptor that designates a machine register for
8553 a given hard register number. */
8555 static dw_loc_descr_ref
8556 one_reg_loc_descriptor (unsigned int regno)
8558 if (regno <= 31)
8559 return new_loc_descr (DW_OP_reg0 + regno, 0, 0);
8560 else
8561 return new_loc_descr (DW_OP_regx, regno, 0);
8564 /* Given an RTL of a register, return a location descriptor that
8565 designates a value that spans more than one register. */
8567 static dw_loc_descr_ref
8568 multiple_reg_loc_descriptor (rtx rtl, rtx regs)
8570 int nregs, size, i;
8571 unsigned reg;
8572 dw_loc_descr_ref loc_result = NULL;
8574 reg = REGNO (rtl);
8575 #ifdef LEAF_REG_REMAP
8576 reg = LEAF_REG_REMAP (reg);
8577 #endif
8578 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
8579 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
8581 /* Simple, contiguous registers. */
8582 if (regs == NULL_RTX)
8584 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
8586 loc_result = NULL;
8587 while (nregs--)
8589 dw_loc_descr_ref t;
8591 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg));
8592 add_loc_descr (&loc_result, t);
8593 add_loc_descr_op_piece (&loc_result, size);
8594 ++reg;
8596 return loc_result;
8599 /* Now onto stupid register sets in non contiguous locations. */
8601 gcc_assert (GET_CODE (regs) == PARALLEL);
8603 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8604 loc_result = NULL;
8606 for (i = 0; i < XVECLEN (regs, 0); ++i)
8608 dw_loc_descr_ref t;
8610 t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)));
8611 add_loc_descr (&loc_result, t);
8612 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8613 add_loc_descr_op_piece (&loc_result, size);
8615 return loc_result;
8618 /* Return a location descriptor that designates a constant. */
8620 static dw_loc_descr_ref
8621 int_loc_descriptor (HOST_WIDE_INT i)
8623 enum dwarf_location_atom op;
8625 /* Pick the smallest representation of a constant, rather than just
8626 defaulting to the LEB encoding. */
8627 if (i >= 0)
8629 if (i <= 31)
8630 op = DW_OP_lit0 + i;
8631 else if (i <= 0xff)
8632 op = DW_OP_const1u;
8633 else if (i <= 0xffff)
8634 op = DW_OP_const2u;
8635 else if (HOST_BITS_PER_WIDE_INT == 32
8636 || i <= 0xffffffff)
8637 op = DW_OP_const4u;
8638 else
8639 op = DW_OP_constu;
8641 else
8643 if (i >= -0x80)
8644 op = DW_OP_const1s;
8645 else if (i >= -0x8000)
8646 op = DW_OP_const2s;
8647 else if (HOST_BITS_PER_WIDE_INT == 32
8648 || i >= -0x80000000)
8649 op = DW_OP_const4s;
8650 else
8651 op = DW_OP_consts;
8654 return new_loc_descr (op, i, 0);
8657 /* Return a location descriptor that designates a base+offset location. */
8659 static dw_loc_descr_ref
8660 based_loc_descr (rtx reg, HOST_WIDE_INT offset)
8662 unsigned int regno;
8664 /* We only use "frame base" when we're sure we're talking about the
8665 post-prologue local stack frame. We do this by *not* running
8666 register elimination until this point, and recognizing the special
8667 argument pointer and soft frame pointer rtx's. */
8668 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
8670 rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
8672 if (elim != reg)
8674 if (GET_CODE (elim) == PLUS)
8676 offset += INTVAL (XEXP (elim, 1));
8677 elim = XEXP (elim, 0);
8679 gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
8680 : stack_pointer_rtx));
8681 offset += frame_pointer_fb_offset;
8683 return new_loc_descr (DW_OP_fbreg, offset, 0);
8687 regno = dbx_reg_number (reg);
8688 if (regno <= 31)
8689 return new_loc_descr (DW_OP_breg0 + regno, offset, 0);
8690 else
8691 return new_loc_descr (DW_OP_bregx, regno, offset);
8694 /* Return true if this RTL expression describes a base+offset calculation. */
8696 static inline int
8697 is_based_loc (rtx rtl)
8699 return (GET_CODE (rtl) == PLUS
8700 && ((REG_P (XEXP (rtl, 0))
8701 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
8702 && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
8705 /* The following routine converts the RTL for a variable or parameter
8706 (resident in memory) into an equivalent Dwarf representation of a
8707 mechanism for getting the address of that same variable onto the top of a
8708 hypothetical "address evaluation" stack.
8710 When creating memory location descriptors, we are effectively transforming
8711 the RTL for a memory-resident object into its Dwarf postfix expression
8712 equivalent. This routine recursively descends an RTL tree, turning
8713 it into Dwarf postfix code as it goes.
8715 MODE is the mode of the memory reference, needed to handle some
8716 autoincrement addressing modes.
8718 CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
8719 location list for RTL.
8721 Return 0 if we can't represent the location. */
8723 static dw_loc_descr_ref
8724 mem_loc_descriptor (rtx rtl, enum machine_mode mode)
8726 dw_loc_descr_ref mem_loc_result = NULL;
8727 enum dwarf_location_atom op;
8729 /* Note that for a dynamically sized array, the location we will generate a
8730 description of here will be the lowest numbered location which is
8731 actually within the array. That's *not* necessarily the same as the
8732 zeroth element of the array. */
8734 rtl = targetm.delegitimize_address (rtl);
8736 switch (GET_CODE (rtl))
8738 case POST_INC:
8739 case POST_DEC:
8740 case POST_MODIFY:
8741 /* POST_INC and POST_DEC can be handled just like a SUBREG. So we
8742 just fall into the SUBREG code. */
8744 /* ... fall through ... */
8746 case SUBREG:
8747 /* The case of a subreg may arise when we have a local (register)
8748 variable or a formal (register) parameter which doesn't quite fill
8749 up an entire register. For now, just assume that it is
8750 legitimate to make the Dwarf info refer to the whole register which
8751 contains the given subreg. */
8752 rtl = XEXP (rtl, 0);
8754 /* ... fall through ... */
8756 case REG:
8757 /* Whenever a register number forms a part of the description of the
8758 method for calculating the (dynamic) address of a memory resident
8759 object, DWARF rules require the register number be referred to as
8760 a "base register". This distinction is not based in any way upon
8761 what category of register the hardware believes the given register
8762 belongs to. This is strictly DWARF terminology we're dealing with
8763 here. Note that in cases where the location of a memory-resident
8764 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
8765 OP_CONST (0)) the actual DWARF location descriptor that we generate
8766 may just be OP_BASEREG (basereg). This may look deceptively like
8767 the object in question was allocated to a register (rather than in
8768 memory) so DWARF consumers need to be aware of the subtle
8769 distinction between OP_REG and OP_BASEREG. */
8770 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
8771 mem_loc_result = based_loc_descr (rtl, 0);
8772 break;
8774 case MEM:
8775 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl));
8776 if (mem_loc_result != 0)
8777 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
8778 break;
8780 case LO_SUM:
8781 rtl = XEXP (rtl, 1);
8783 /* ... fall through ... */
8785 case LABEL_REF:
8786 /* Some ports can transform a symbol ref into a label ref, because
8787 the symbol ref is too far away and has to be dumped into a constant
8788 pool. */
8789 case CONST:
8790 case SYMBOL_REF:
8791 /* Alternatively, the symbol in the constant pool might be referenced
8792 by a different symbol. */
8793 if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
8795 bool marked;
8796 rtx tmp = get_pool_constant_mark (rtl, &marked);
8798 if (GET_CODE (tmp) == SYMBOL_REF)
8800 rtl = tmp;
8801 if (CONSTANT_POOL_ADDRESS_P (tmp))
8802 get_pool_constant_mark (tmp, &marked);
8803 else
8804 marked = true;
8807 /* If all references to this pool constant were optimized away,
8808 it was not output and thus we can't represent it.
8809 FIXME: might try to use DW_OP_const_value here, though
8810 DW_OP_piece complicates it. */
8811 if (!marked)
8812 return 0;
8815 mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
8816 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
8817 mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
8818 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
8819 break;
8821 case PRE_MODIFY:
8822 /* Extract the PLUS expression nested inside and fall into
8823 PLUS code below. */
8824 rtl = XEXP (rtl, 1);
8825 goto plus;
8827 case PRE_INC:
8828 case PRE_DEC:
8829 /* Turn these into a PLUS expression and fall into the PLUS code
8830 below. */
8831 rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
8832 GEN_INT (GET_CODE (rtl) == PRE_INC
8833 ? GET_MODE_UNIT_SIZE (mode)
8834 : -GET_MODE_UNIT_SIZE (mode)));
8836 /* ... fall through ... */
8838 case PLUS:
8839 plus:
8840 if (is_based_loc (rtl))
8841 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
8842 INTVAL (XEXP (rtl, 1)));
8843 else
8845 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode);
8846 if (mem_loc_result == 0)
8847 break;
8849 if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
8850 && INTVAL (XEXP (rtl, 1)) >= 0)
8851 add_loc_descr (&mem_loc_result,
8852 new_loc_descr (DW_OP_plus_uconst,
8853 INTVAL (XEXP (rtl, 1)), 0));
8854 else
8856 add_loc_descr (&mem_loc_result,
8857 mem_loc_descriptor (XEXP (rtl, 1), mode));
8858 add_loc_descr (&mem_loc_result,
8859 new_loc_descr (DW_OP_plus, 0, 0));
8862 break;
8864 /* If a pseudo-reg is optimized away, it is possible for it to
8865 be replaced with a MEM containing a multiply or shift. */
8866 case MULT:
8867 op = DW_OP_mul;
8868 goto do_binop;
8870 case ASHIFT:
8871 op = DW_OP_shl;
8872 goto do_binop;
8874 case ASHIFTRT:
8875 op = DW_OP_shra;
8876 goto do_binop;
8878 case LSHIFTRT:
8879 op = DW_OP_shr;
8880 goto do_binop;
8882 do_binop:
8884 dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode);
8885 dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode);
8887 if (op0 == 0 || op1 == 0)
8888 break;
8890 mem_loc_result = op0;
8891 add_loc_descr (&mem_loc_result, op1);
8892 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
8893 break;
8896 case CONST_INT:
8897 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
8898 break;
8900 default:
8901 gcc_unreachable ();
8904 return mem_loc_result;
8907 /* Return a descriptor that describes the concatenation of two locations.
8908 This is typically a complex variable. */
8910 static dw_loc_descr_ref
8911 concat_loc_descriptor (rtx x0, rtx x1)
8913 dw_loc_descr_ref cc_loc_result = NULL;
8914 dw_loc_descr_ref x0_ref = loc_descriptor (x0);
8915 dw_loc_descr_ref x1_ref = loc_descriptor (x1);
8917 if (x0_ref == 0 || x1_ref == 0)
8918 return 0;
8920 cc_loc_result = x0_ref;
8921 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
8923 add_loc_descr (&cc_loc_result, x1_ref);
8924 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
8926 return cc_loc_result;
8929 /* Output a proper Dwarf location descriptor for a variable or parameter
8930 which is either allocated in a register or in a memory location. For a
8931 register, we just generate an OP_REG and the register number. For a
8932 memory location we provide a Dwarf postfix expression describing how to
8933 generate the (dynamic) address of the object onto the address stack.
8935 If we don't know how to describe it, return 0. */
8937 static dw_loc_descr_ref
8938 loc_descriptor (rtx rtl)
8940 dw_loc_descr_ref loc_result = NULL;
8942 switch (GET_CODE (rtl))
8944 case SUBREG:
8945 /* The case of a subreg may arise when we have a local (register)
8946 variable or a formal (register) parameter which doesn't quite fill
8947 up an entire register. For now, just assume that it is
8948 legitimate to make the Dwarf info refer to the whole register which
8949 contains the given subreg. */
8950 rtl = SUBREG_REG (rtl);
8952 /* ... fall through ... */
8954 case REG:
8955 loc_result = reg_loc_descriptor (rtl);
8956 break;
8958 case MEM:
8959 loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl));
8960 break;
8962 case CONCAT:
8963 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1));
8964 break;
8966 case VAR_LOCATION:
8967 /* Single part. */
8968 if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
8970 loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0));
8971 break;
8974 rtl = XEXP (rtl, 1);
8975 /* FALLTHRU */
8977 case PARALLEL:
8979 rtvec par_elems = XVEC (rtl, 0);
8980 int num_elem = GET_NUM_ELEM (par_elems);
8981 enum machine_mode mode;
8982 int i;
8984 /* Create the first one, so we have something to add to. */
8985 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0));
8986 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
8987 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
8988 for (i = 1; i < num_elem; i++)
8990 dw_loc_descr_ref temp;
8992 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0));
8993 add_loc_descr (&loc_result, temp);
8994 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
8995 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
8998 break;
9000 default:
9001 gcc_unreachable ();
9004 return loc_result;
9007 /* Similar, but generate the descriptor from trees instead of rtl. This comes
9008 up particularly with variable length arrays. WANT_ADDRESS is 2 if this is
9009 a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
9010 top-level invocation, and we require the address of LOC; is 0 if we require
9011 the value of LOC. */
9013 static dw_loc_descr_ref
9014 loc_descriptor_from_tree_1 (tree loc, int want_address)
9016 dw_loc_descr_ref ret, ret1;
9017 int have_address = 0;
9018 enum dwarf_location_atom op;
9020 /* ??? Most of the time we do not take proper care for sign/zero
9021 extending the values properly. Hopefully this won't be a real
9022 problem... */
9024 switch (TREE_CODE (loc))
9026 case ERROR_MARK:
9027 return 0;
9029 case PLACEHOLDER_EXPR:
9030 /* This case involves extracting fields from an object to determine the
9031 position of other fields. We don't try to encode this here. The
9032 only user of this is Ada, which encodes the needed information using
9033 the names of types. */
9034 return 0;
9036 case CALL_EXPR:
9037 return 0;
9039 case PREINCREMENT_EXPR:
9040 case PREDECREMENT_EXPR:
9041 case POSTINCREMENT_EXPR:
9042 case POSTDECREMENT_EXPR:
9043 /* There are no opcodes for these operations. */
9044 return 0;
9046 case ADDR_EXPR:
9047 /* If we already want an address, there's nothing we can do. */
9048 if (want_address)
9049 return 0;
9051 /* Otherwise, process the argument and look for the address. */
9052 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
9054 case VAR_DECL:
9055 if (DECL_THREAD_LOCAL_P (loc))
9057 rtx rtl;
9059 /* If this is not defined, we have no way to emit the data. */
9060 if (!targetm.asm_out.output_dwarf_dtprel)
9061 return 0;
9063 /* The way DW_OP_GNU_push_tls_address is specified, we can only
9064 look up addresses of objects in the current module. */
9065 if (DECL_EXTERNAL (loc))
9066 return 0;
9068 rtl = rtl_for_decl_location (loc);
9069 if (rtl == NULL_RTX)
9070 return 0;
9072 if (!MEM_P (rtl))
9073 return 0;
9074 rtl = XEXP (rtl, 0);
9075 if (! CONSTANT_P (rtl))
9076 return 0;
9078 ret = new_loc_descr (INTERNAL_DW_OP_tls_addr, 0, 0);
9079 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9080 ret->dw_loc_oprnd1.v.val_addr = rtl;
9082 ret1 = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
9083 add_loc_descr (&ret, ret1);
9085 have_address = 1;
9086 break;
9088 /* FALLTHRU */
9090 case PARM_DECL:
9091 if (DECL_HAS_VALUE_EXPR_P (loc))
9092 return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
9093 want_address);
9094 /* FALLTHRU */
9096 case RESULT_DECL:
9098 rtx rtl = rtl_for_decl_location (loc);
9100 if (rtl == NULL_RTX)
9101 return 0;
9102 else if (GET_CODE (rtl) == CONST_INT)
9104 HOST_WIDE_INT val = INTVAL (rtl);
9105 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
9106 val &= GET_MODE_MASK (DECL_MODE (loc));
9107 ret = int_loc_descriptor (val);
9109 else if (GET_CODE (rtl) == CONST_STRING)
9110 return 0;
9111 else if (CONSTANT_P (rtl))
9113 ret = new_loc_descr (DW_OP_addr, 0, 0);
9114 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9115 ret->dw_loc_oprnd1.v.val_addr = rtl;
9117 else
9119 enum machine_mode mode;
9121 /* Certain constructs can only be represented at top-level. */
9122 if (want_address == 2)
9123 return loc_descriptor (rtl);
9125 mode = GET_MODE (rtl);
9126 if (MEM_P (rtl))
9128 rtl = XEXP (rtl, 0);
9129 have_address = 1;
9131 ret = mem_loc_descriptor (rtl, mode);
9134 break;
9136 case INDIRECT_REF:
9137 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9138 have_address = 1;
9139 break;
9141 case COMPOUND_EXPR:
9142 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
9144 case NOP_EXPR:
9145 case CONVERT_EXPR:
9146 case NON_LVALUE_EXPR:
9147 case VIEW_CONVERT_EXPR:
9148 case SAVE_EXPR:
9149 case MODIFY_EXPR:
9150 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), want_address);
9152 case COMPONENT_REF:
9153 case BIT_FIELD_REF:
9154 case ARRAY_REF:
9155 case ARRAY_RANGE_REF:
9157 tree obj, offset;
9158 HOST_WIDE_INT bitsize, bitpos, bytepos;
9159 enum machine_mode mode;
9160 int volatilep;
9161 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
9163 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
9164 &unsignedp, &volatilep, false);
9166 if (obj == loc)
9167 return 0;
9169 ret = loc_descriptor_from_tree_1 (obj, 1);
9170 if (ret == 0
9171 || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
9172 return 0;
9174 if (offset != NULL_TREE)
9176 /* Variable offset. */
9177 add_loc_descr (&ret, loc_descriptor_from_tree_1 (offset, 0));
9178 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9181 bytepos = bitpos / BITS_PER_UNIT;
9182 if (bytepos > 0)
9183 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
9184 else if (bytepos < 0)
9186 add_loc_descr (&ret, int_loc_descriptor (bytepos));
9187 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9190 have_address = 1;
9191 break;
9194 case INTEGER_CST:
9195 if (host_integerp (loc, 0))
9196 ret = int_loc_descriptor (tree_low_cst (loc, 0));
9197 else
9198 return 0;
9199 break;
9201 case CONSTRUCTOR:
9203 /* Get an RTL for this, if something has been emitted. */
9204 rtx rtl = lookup_constant_def (loc);
9205 enum machine_mode mode;
9207 if (!rtl || !MEM_P (rtl))
9208 return 0;
9209 mode = GET_MODE (rtl);
9210 rtl = XEXP (rtl, 0);
9211 ret = mem_loc_descriptor (rtl, mode);
9212 have_address = 1;
9213 break;
9216 case TRUTH_AND_EXPR:
9217 case TRUTH_ANDIF_EXPR:
9218 case BIT_AND_EXPR:
9219 op = DW_OP_and;
9220 goto do_binop;
9222 case TRUTH_XOR_EXPR:
9223 case BIT_XOR_EXPR:
9224 op = DW_OP_xor;
9225 goto do_binop;
9227 case TRUTH_OR_EXPR:
9228 case TRUTH_ORIF_EXPR:
9229 case BIT_IOR_EXPR:
9230 op = DW_OP_or;
9231 goto do_binop;
9233 case FLOOR_DIV_EXPR:
9234 case CEIL_DIV_EXPR:
9235 case ROUND_DIV_EXPR:
9236 case TRUNC_DIV_EXPR:
9237 op = DW_OP_div;
9238 goto do_binop;
9240 case MINUS_EXPR:
9241 op = DW_OP_minus;
9242 goto do_binop;
9244 case FLOOR_MOD_EXPR:
9245 case CEIL_MOD_EXPR:
9246 case ROUND_MOD_EXPR:
9247 case TRUNC_MOD_EXPR:
9248 op = DW_OP_mod;
9249 goto do_binop;
9251 case MULT_EXPR:
9252 op = DW_OP_mul;
9253 goto do_binop;
9255 case LSHIFT_EXPR:
9256 op = DW_OP_shl;
9257 goto do_binop;
9259 case RSHIFT_EXPR:
9260 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
9261 goto do_binop;
9263 case PLUS_EXPR:
9264 if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
9265 && host_integerp (TREE_OPERAND (loc, 1), 0))
9267 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9268 if (ret == 0)
9269 return 0;
9271 add_loc_descr (&ret,
9272 new_loc_descr (DW_OP_plus_uconst,
9273 tree_low_cst (TREE_OPERAND (loc, 1),
9275 0));
9276 break;
9279 op = DW_OP_plus;
9280 goto do_binop;
9282 case LE_EXPR:
9283 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9284 return 0;
9286 op = DW_OP_le;
9287 goto do_binop;
9289 case GE_EXPR:
9290 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9291 return 0;
9293 op = DW_OP_ge;
9294 goto do_binop;
9296 case LT_EXPR:
9297 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9298 return 0;
9300 op = DW_OP_lt;
9301 goto do_binop;
9303 case GT_EXPR:
9304 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9305 return 0;
9307 op = DW_OP_gt;
9308 goto do_binop;
9310 case EQ_EXPR:
9311 op = DW_OP_eq;
9312 goto do_binop;
9314 case NE_EXPR:
9315 op = DW_OP_ne;
9316 goto do_binop;
9318 do_binop:
9319 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9320 ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9321 if (ret == 0 || ret1 == 0)
9322 return 0;
9324 add_loc_descr (&ret, ret1);
9325 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9326 break;
9328 case TRUTH_NOT_EXPR:
9329 case BIT_NOT_EXPR:
9330 op = DW_OP_not;
9331 goto do_unop;
9333 case ABS_EXPR:
9334 op = DW_OP_abs;
9335 goto do_unop;
9337 case NEGATE_EXPR:
9338 op = DW_OP_neg;
9339 goto do_unop;
9341 do_unop:
9342 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9343 if (ret == 0)
9344 return 0;
9346 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9347 break;
9349 case MIN_EXPR:
9350 case MAX_EXPR:
9352 const enum tree_code code =
9353 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
9355 loc = build3 (COND_EXPR, TREE_TYPE (loc),
9356 build2 (code, integer_type_node,
9357 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
9358 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
9361 /* ... fall through ... */
9363 case COND_EXPR:
9365 dw_loc_descr_ref lhs
9366 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9367 dw_loc_descr_ref rhs
9368 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
9369 dw_loc_descr_ref bra_node, jump_node, tmp;
9371 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9372 if (ret == 0 || lhs == 0 || rhs == 0)
9373 return 0;
9375 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
9376 add_loc_descr (&ret, bra_node);
9378 add_loc_descr (&ret, rhs);
9379 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
9380 add_loc_descr (&ret, jump_node);
9382 add_loc_descr (&ret, lhs);
9383 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9384 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
9386 /* ??? Need a node to point the skip at. Use a nop. */
9387 tmp = new_loc_descr (DW_OP_nop, 0, 0);
9388 add_loc_descr (&ret, tmp);
9389 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9390 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
9392 break;
9394 case FIX_TRUNC_EXPR:
9395 case FIX_CEIL_EXPR:
9396 case FIX_FLOOR_EXPR:
9397 case FIX_ROUND_EXPR:
9398 return 0;
9400 default:
9401 /* Leave front-end specific codes as simply unknown. This comes
9402 up, for instance, with the C STMT_EXPR. */
9403 if ((unsigned int) TREE_CODE (loc)
9404 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
9405 return 0;
9407 #ifdef ENABLE_CHECKING
9408 /* Otherwise this is a generic code; we should just lists all of
9409 these explicitly. We forgot one. */
9410 gcc_unreachable ();
9411 #else
9412 /* In a release build, we want to degrade gracefully: better to
9413 generate incomplete debugging information than to crash. */
9414 return NULL;
9415 #endif
9418 /* Show if we can't fill the request for an address. */
9419 if (want_address && !have_address)
9420 return 0;
9422 /* If we've got an address and don't want one, dereference. */
9423 if (!want_address && have_address && ret)
9425 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
9427 if (size > DWARF2_ADDR_SIZE || size == -1)
9428 return 0;
9429 else if (size == DWARF2_ADDR_SIZE)
9430 op = DW_OP_deref;
9431 else
9432 op = DW_OP_deref_size;
9434 add_loc_descr (&ret, new_loc_descr (op, size, 0));
9437 return ret;
9440 static inline dw_loc_descr_ref
9441 loc_descriptor_from_tree (tree loc)
9443 return loc_descriptor_from_tree_1 (loc, 2);
9446 /* Given a value, round it up to the lowest multiple of `boundary'
9447 which is not less than the value itself. */
9449 static inline HOST_WIDE_INT
9450 ceiling (HOST_WIDE_INT value, unsigned int boundary)
9452 return (((value + boundary - 1) / boundary) * boundary);
9455 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
9456 pointer to the declared type for the relevant field variable, or return
9457 `integer_type_node' if the given node turns out to be an
9458 ERROR_MARK node. */
9460 static inline tree
9461 field_type (tree decl)
9463 tree type;
9465 if (TREE_CODE (decl) == ERROR_MARK)
9466 return integer_type_node;
9468 type = DECL_BIT_FIELD_TYPE (decl);
9469 if (type == NULL_TREE)
9470 type = TREE_TYPE (decl);
9472 return type;
9475 /* Given a pointer to a tree node, return the alignment in bits for
9476 it, or else return BITS_PER_WORD if the node actually turns out to
9477 be an ERROR_MARK node. */
9479 static inline unsigned
9480 simple_type_align_in_bits (tree type)
9482 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
9485 static inline unsigned
9486 simple_decl_align_in_bits (tree decl)
9488 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
9491 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
9492 lowest addressed byte of the "containing object" for the given FIELD_DECL,
9493 or return 0 if we are unable to determine what that offset is, either
9494 because the argument turns out to be a pointer to an ERROR_MARK node, or
9495 because the offset is actually variable. (We can't handle the latter case
9496 just yet). */
9498 static HOST_WIDE_INT
9499 field_byte_offset (tree decl)
9501 unsigned int type_align_in_bits;
9502 unsigned int decl_align_in_bits;
9503 unsigned HOST_WIDE_INT type_size_in_bits;
9504 HOST_WIDE_INT object_offset_in_bits;
9505 tree type;
9506 tree field_size_tree;
9507 HOST_WIDE_INT bitpos_int;
9508 HOST_WIDE_INT deepest_bitpos;
9509 unsigned HOST_WIDE_INT field_size_in_bits;
9511 if (TREE_CODE (decl) == ERROR_MARK)
9512 return 0;
9514 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
9516 type = field_type (decl);
9517 field_size_tree = DECL_SIZE (decl);
9519 /* The size could be unspecified if there was an error, or for
9520 a flexible array member. */
9521 if (! field_size_tree)
9522 field_size_tree = bitsize_zero_node;
9524 /* We cannot yet cope with fields whose positions are variable, so
9525 for now, when we see such things, we simply return 0. Someday, we may
9526 be able to handle such cases, but it will be damn difficult. */
9527 if (! host_integerp (bit_position (decl), 0))
9528 return 0;
9530 bitpos_int = int_bit_position (decl);
9532 /* If we don't know the size of the field, pretend it's a full word. */
9533 if (host_integerp (field_size_tree, 1))
9534 field_size_in_bits = tree_low_cst (field_size_tree, 1);
9535 else
9536 field_size_in_bits = BITS_PER_WORD;
9538 type_size_in_bits = simple_type_size_in_bits (type);
9539 type_align_in_bits = simple_type_align_in_bits (type);
9540 decl_align_in_bits = simple_decl_align_in_bits (decl);
9542 /* The GCC front-end doesn't make any attempt to keep track of the starting
9543 bit offset (relative to the start of the containing structure type) of the
9544 hypothetical "containing object" for a bit-field. Thus, when computing
9545 the byte offset value for the start of the "containing object" of a
9546 bit-field, we must deduce this information on our own. This can be rather
9547 tricky to do in some cases. For example, handling the following structure
9548 type definition when compiling for an i386/i486 target (which only aligns
9549 long long's to 32-bit boundaries) can be very tricky:
9551 struct S { int field1; long long field2:31; };
9553 Fortunately, there is a simple rule-of-thumb which can be used in such
9554 cases. When compiling for an i386/i486, GCC will allocate 8 bytes for the
9555 structure shown above. It decides to do this based upon one simple rule
9556 for bit-field allocation. GCC allocates each "containing object" for each
9557 bit-field at the first (i.e. lowest addressed) legitimate alignment
9558 boundary (based upon the required minimum alignment for the declared type
9559 of the field) which it can possibly use, subject to the condition that
9560 there is still enough available space remaining in the containing object
9561 (when allocated at the selected point) to fully accommodate all of the
9562 bits of the bit-field itself.
9564 This simple rule makes it obvious why GCC allocates 8 bytes for each
9565 object of the structure type shown above. When looking for a place to
9566 allocate the "containing object" for `field2', the compiler simply tries
9567 to allocate a 64-bit "containing object" at each successive 32-bit
9568 boundary (starting at zero) until it finds a place to allocate that 64-
9569 bit field such that at least 31 contiguous (and previously unallocated)
9570 bits remain within that selected 64 bit field. (As it turns out, for the
9571 example above, the compiler finds it is OK to allocate the "containing
9572 object" 64-bit field at bit-offset zero within the structure type.)
9574 Here we attempt to work backwards from the limited set of facts we're
9575 given, and we try to deduce from those facts, where GCC must have believed
9576 that the containing object started (within the structure type). The value
9577 we deduce is then used (by the callers of this routine) to generate
9578 DW_AT_location and DW_AT_bit_offset attributes for fields (both bit-fields
9579 and, in the case of DW_AT_location, regular fields as well). */
9581 /* Figure out the bit-distance from the start of the structure to the
9582 "deepest" bit of the bit-field. */
9583 deepest_bitpos = bitpos_int + field_size_in_bits;
9585 /* This is the tricky part. Use some fancy footwork to deduce where the
9586 lowest addressed bit of the containing object must be. */
9587 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9589 /* Round up to type_align by default. This works best for bitfields. */
9590 object_offset_in_bits += type_align_in_bits - 1;
9591 object_offset_in_bits /= type_align_in_bits;
9592 object_offset_in_bits *= type_align_in_bits;
9594 if (object_offset_in_bits > bitpos_int)
9596 /* Sigh, the decl must be packed. */
9597 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9599 /* Round up to decl_align instead. */
9600 object_offset_in_bits += decl_align_in_bits - 1;
9601 object_offset_in_bits /= decl_align_in_bits;
9602 object_offset_in_bits *= decl_align_in_bits;
9605 return object_offset_in_bits / BITS_PER_UNIT;
9608 /* The following routines define various Dwarf attributes and any data
9609 associated with them. */
9611 /* Add a location description attribute value to a DIE.
9613 This emits location attributes suitable for whole variables and
9614 whole parameters. Note that the location attributes for struct fields are
9615 generated by the routine `data_member_location_attribute' below. */
9617 static inline void
9618 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
9619 dw_loc_descr_ref descr)
9621 if (descr != 0)
9622 add_AT_loc (die, attr_kind, descr);
9625 /* Attach the specialized form of location attribute used for data members of
9626 struct and union types. In the special case of a FIELD_DECL node which
9627 represents a bit-field, the "offset" part of this special location
9628 descriptor must indicate the distance in bytes from the lowest-addressed
9629 byte of the containing struct or union type to the lowest-addressed byte of
9630 the "containing object" for the bit-field. (See the `field_byte_offset'
9631 function above).
9633 For any given bit-field, the "containing object" is a hypothetical object
9634 (of some integral or enum type) within which the given bit-field lives. The
9635 type of this hypothetical "containing object" is always the same as the
9636 declared type of the individual bit-field itself (for GCC anyway... the
9637 DWARF spec doesn't actually mandate this). Note that it is the size (in
9638 bytes) of the hypothetical "containing object" which will be given in the
9639 DW_AT_byte_size attribute for this bit-field. (See the
9640 `byte_size_attribute' function below.) It is also used when calculating the
9641 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
9642 function below.) */
9644 static void
9645 add_data_member_location_attribute (dw_die_ref die, tree decl)
9647 HOST_WIDE_INT offset;
9648 dw_loc_descr_ref loc_descr = 0;
9650 if (TREE_CODE (decl) == TREE_BINFO)
9652 /* We're working on the TAG_inheritance for a base class. */
9653 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
9655 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
9656 aren't at a fixed offset from all (sub)objects of the same
9657 type. We need to extract the appropriate offset from our
9658 vtable. The following dwarf expression means
9660 BaseAddr = ObAddr + *((*ObAddr) - Offset)
9662 This is specific to the V3 ABI, of course. */
9664 dw_loc_descr_ref tmp;
9666 /* Make a copy of the object address. */
9667 tmp = new_loc_descr (DW_OP_dup, 0, 0);
9668 add_loc_descr (&loc_descr, tmp);
9670 /* Extract the vtable address. */
9671 tmp = new_loc_descr (DW_OP_deref, 0, 0);
9672 add_loc_descr (&loc_descr, tmp);
9674 /* Calculate the address of the offset. */
9675 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
9676 gcc_assert (offset < 0);
9678 tmp = int_loc_descriptor (-offset);
9679 add_loc_descr (&loc_descr, tmp);
9680 tmp = new_loc_descr (DW_OP_minus, 0, 0);
9681 add_loc_descr (&loc_descr, tmp);
9683 /* Extract the offset. */
9684 tmp = new_loc_descr (DW_OP_deref, 0, 0);
9685 add_loc_descr (&loc_descr, tmp);
9687 /* Add it to the object address. */
9688 tmp = new_loc_descr (DW_OP_plus, 0, 0);
9689 add_loc_descr (&loc_descr, tmp);
9691 else
9692 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
9694 else
9695 offset = field_byte_offset (decl);
9697 if (! loc_descr)
9699 enum dwarf_location_atom op;
9701 /* The DWARF2 standard says that we should assume that the structure
9702 address is already on the stack, so we can specify a structure field
9703 address by using DW_OP_plus_uconst. */
9705 #ifdef MIPS_DEBUGGING_INFO
9706 /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
9707 operator correctly. It works only if we leave the offset on the
9708 stack. */
9709 op = DW_OP_constu;
9710 #else
9711 op = DW_OP_plus_uconst;
9712 #endif
9714 loc_descr = new_loc_descr (op, offset, 0);
9717 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
9720 /* Writes integer values to dw_vec_const array. */
9722 static void
9723 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
9725 while (size != 0)
9727 *dest++ = val & 0xff;
9728 val >>= 8;
9729 --size;
9733 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
9735 static HOST_WIDE_INT
9736 extract_int (const unsigned char *src, unsigned int size)
9738 HOST_WIDE_INT val = 0;
9740 src += size;
9741 while (size != 0)
9743 val <<= 8;
9744 val |= *--src & 0xff;
9745 --size;
9747 return val;
9750 /* Writes floating point values to dw_vec_const array. */
9752 static void
9753 insert_float (rtx rtl, unsigned char *array)
9755 REAL_VALUE_TYPE rv;
9756 long val[4];
9757 int i;
9759 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
9760 real_to_target (val, &rv, GET_MODE (rtl));
9762 /* real_to_target puts 32-bit pieces in each long. Pack them. */
9763 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
9765 insert_int (val[i], 4, array);
9766 array += 4;
9770 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
9771 does not have a "location" either in memory or in a register. These
9772 things can arise in GNU C when a constant is passed as an actual parameter
9773 to an inlined function. They can also arise in C++ where declared
9774 constants do not necessarily get memory "homes". */
9776 static void
9777 add_const_value_attribute (dw_die_ref die, rtx rtl)
9779 switch (GET_CODE (rtl))
9781 case CONST_INT:
9783 HOST_WIDE_INT val = INTVAL (rtl);
9785 if (val < 0)
9786 add_AT_int (die, DW_AT_const_value, val);
9787 else
9788 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
9790 break;
9792 case CONST_DOUBLE:
9793 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
9794 floating-point constant. A CONST_DOUBLE is used whenever the
9795 constant requires more than one word in order to be adequately
9796 represented. We output CONST_DOUBLEs as blocks. */
9798 enum machine_mode mode = GET_MODE (rtl);
9800 if (SCALAR_FLOAT_MODE_P (mode))
9802 unsigned int length = GET_MODE_SIZE (mode);
9803 unsigned char *array = ggc_alloc (length);
9805 insert_float (rtl, array);
9806 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
9808 else
9810 /* ??? We really should be using HOST_WIDE_INT throughout. */
9811 gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
9813 add_AT_long_long (die, DW_AT_const_value,
9814 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
9817 break;
9819 case CONST_VECTOR:
9821 enum machine_mode mode = GET_MODE (rtl);
9822 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
9823 unsigned int length = CONST_VECTOR_NUNITS (rtl);
9824 unsigned char *array = ggc_alloc (length * elt_size);
9825 unsigned int i;
9826 unsigned char *p;
9828 switch (GET_MODE_CLASS (mode))
9830 case MODE_VECTOR_INT:
9831 for (i = 0, p = array; i < length; i++, p += elt_size)
9833 rtx elt = CONST_VECTOR_ELT (rtl, i);
9834 HOST_WIDE_INT lo, hi;
9836 switch (GET_CODE (elt))
9838 case CONST_INT:
9839 lo = INTVAL (elt);
9840 hi = -(lo < 0);
9841 break;
9843 case CONST_DOUBLE:
9844 lo = CONST_DOUBLE_LOW (elt);
9845 hi = CONST_DOUBLE_HIGH (elt);
9846 break;
9848 default:
9849 gcc_unreachable ();
9852 if (elt_size <= sizeof (HOST_WIDE_INT))
9853 insert_int (lo, elt_size, p);
9854 else
9856 unsigned char *p0 = p;
9857 unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
9859 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
9860 if (WORDS_BIG_ENDIAN)
9862 p0 = p1;
9863 p1 = p;
9865 insert_int (lo, sizeof (HOST_WIDE_INT), p0);
9866 insert_int (hi, sizeof (HOST_WIDE_INT), p1);
9869 break;
9871 case MODE_VECTOR_FLOAT:
9872 for (i = 0, p = array; i < length; i++, p += elt_size)
9874 rtx elt = CONST_VECTOR_ELT (rtl, i);
9875 insert_float (elt, p);
9877 break;
9879 default:
9880 gcc_unreachable ();
9883 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
9885 break;
9887 case CONST_STRING:
9888 add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
9889 break;
9891 case SYMBOL_REF:
9892 case LABEL_REF:
9893 case CONST:
9894 add_AT_addr (die, DW_AT_const_value, rtl);
9895 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
9896 break;
9898 case PLUS:
9899 /* In cases where an inlined instance of an inline function is passed
9900 the address of an `auto' variable (which is local to the caller) we
9901 can get a situation where the DECL_RTL of the artificial local
9902 variable (for the inlining) which acts as a stand-in for the
9903 corresponding formal parameter (of the inline function) will look
9904 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
9905 exactly a compile-time constant expression, but it isn't the address
9906 of the (artificial) local variable either. Rather, it represents the
9907 *value* which the artificial local variable always has during its
9908 lifetime. We currently have no way to represent such quasi-constant
9909 values in Dwarf, so for now we just punt and generate nothing. */
9910 break;
9912 default:
9913 /* No other kinds of rtx should be possible here. */
9914 gcc_unreachable ();
9919 /* Determine whether the evaluation of EXPR references any variables
9920 or functions which aren't otherwise used (and therefore may not be
9921 output). */
9922 static tree
9923 reference_to_unused (tree * tp, int * walk_subtrees,
9924 void * data ATTRIBUTE_UNUSED)
9926 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
9927 *walk_subtrees = 0;
9929 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
9930 && ! TREE_ASM_WRITTEN (*tp))
9931 return *tp;
9932 else
9933 return NULL_TREE;
9936 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
9937 for use in a later add_const_value_attribute call. */
9939 static rtx
9940 rtl_for_decl_init (tree init, tree type)
9942 rtx rtl = NULL_RTX;
9944 /* If a variable is initialized with a string constant without embedded
9945 zeros, build CONST_STRING. */
9946 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
9948 tree enttype = TREE_TYPE (type);
9949 tree domain = TYPE_DOMAIN (type);
9950 enum machine_mode mode = TYPE_MODE (enttype);
9952 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
9953 && domain
9954 && integer_zerop (TYPE_MIN_VALUE (domain))
9955 && compare_tree_int (TYPE_MAX_VALUE (domain),
9956 TREE_STRING_LENGTH (init) - 1) == 0
9957 && ((size_t) TREE_STRING_LENGTH (init)
9958 == strlen (TREE_STRING_POINTER (init)) + 1))
9959 rtl = gen_rtx_CONST_STRING (VOIDmode,
9960 ggc_strdup (TREE_STRING_POINTER (init)));
9962 /* Although DWARF could easily handle other kinds of aggregates, we
9963 have no way to represent such values as RTL constants, so skip
9964 those. */
9965 else if (AGGREGATE_TYPE_P (type))
9967 /* If the initializer is something that we know will expand into an
9968 immediate RTL constant, expand it now. We must be careful not to
9969 reference variables which won't be output. */
9970 else if (initializer_constant_valid_p (init, type)
9971 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
9973 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
9975 /* If expand_expr returns a MEM, it wasn't immediate. */
9976 gcc_assert (!rtl || !MEM_P (rtl));
9979 return rtl;
9982 /* Generate RTL for the variable DECL to represent its location. */
9984 static rtx
9985 rtl_for_decl_location (tree decl)
9987 rtx rtl;
9989 /* Here we have to decide where we are going to say the parameter "lives"
9990 (as far as the debugger is concerned). We only have a couple of
9991 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
9993 DECL_RTL normally indicates where the parameter lives during most of the
9994 activation of the function. If optimization is enabled however, this
9995 could be either NULL or else a pseudo-reg. Both of those cases indicate
9996 that the parameter doesn't really live anywhere (as far as the code
9997 generation parts of GCC are concerned) during most of the function's
9998 activation. That will happen (for example) if the parameter is never
9999 referenced within the function.
10001 We could just generate a location descriptor here for all non-NULL
10002 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
10003 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
10004 where DECL_RTL is NULL or is a pseudo-reg.
10006 Note however that we can only get away with using DECL_INCOMING_RTL as
10007 a backup substitute for DECL_RTL in certain limited cases. In cases
10008 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
10009 we can be sure that the parameter was passed using the same type as it is
10010 declared to have within the function, and that its DECL_INCOMING_RTL
10011 points us to a place where a value of that type is passed.
10013 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
10014 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
10015 because in these cases DECL_INCOMING_RTL points us to a value of some
10016 type which is *different* from the type of the parameter itself. Thus,
10017 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
10018 such cases, the debugger would end up (for example) trying to fetch a
10019 `float' from a place which actually contains the first part of a
10020 `double'. That would lead to really incorrect and confusing
10021 output at debug-time.
10023 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
10024 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
10025 are a couple of exceptions however. On little-endian machines we can
10026 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
10027 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
10028 an integral type that is smaller than TREE_TYPE (decl). These cases arise
10029 when (on a little-endian machine) a non-prototyped function has a
10030 parameter declared to be of type `short' or `char'. In such cases,
10031 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
10032 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
10033 passed `int' value. If the debugger then uses that address to fetch
10034 a `short' or a `char' (on a little-endian machine) the result will be
10035 the correct data, so we allow for such exceptional cases below.
10037 Note that our goal here is to describe the place where the given formal
10038 parameter lives during most of the function's activation (i.e. between the
10039 end of the prologue and the start of the epilogue). We'll do that as best
10040 as we can. Note however that if the given formal parameter is modified
10041 sometime during the execution of the function, then a stack backtrace (at
10042 debug-time) will show the function as having been called with the *new*
10043 value rather than the value which was originally passed in. This happens
10044 rarely enough that it is not a major problem, but it *is* a problem, and
10045 I'd like to fix it.
10047 A future version of dwarf2out.c may generate two additional attributes for
10048 any given DW_TAG_formal_parameter DIE which will describe the "passed
10049 type" and the "passed location" for the given formal parameter in addition
10050 to the attributes we now generate to indicate the "declared type" and the
10051 "active location" for each parameter. This additional set of attributes
10052 could be used by debuggers for stack backtraces. Separately, note that
10053 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
10054 This happens (for example) for inlined-instances of inline function formal
10055 parameters which are never referenced. This really shouldn't be
10056 happening. All PARM_DECL nodes should get valid non-NULL
10057 DECL_INCOMING_RTL values. FIXME. */
10059 /* Use DECL_RTL as the "location" unless we find something better. */
10060 rtl = DECL_RTL_IF_SET (decl);
10062 /* When generating abstract instances, ignore everything except
10063 constants, symbols living in memory, and symbols living in
10064 fixed registers. */
10065 if (! reload_completed)
10067 if (rtl
10068 && (CONSTANT_P (rtl)
10069 || (MEM_P (rtl)
10070 && CONSTANT_P (XEXP (rtl, 0)))
10071 || (REG_P (rtl)
10072 && TREE_CODE (decl) == VAR_DECL
10073 && TREE_STATIC (decl))))
10075 rtl = targetm.delegitimize_address (rtl);
10076 return rtl;
10078 rtl = NULL_RTX;
10080 else if (TREE_CODE (decl) == PARM_DECL)
10082 if (rtl == NULL_RTX || is_pseudo_reg (rtl))
10084 tree declared_type = TREE_TYPE (decl);
10085 tree passed_type = DECL_ARG_TYPE (decl);
10086 enum machine_mode dmode = TYPE_MODE (declared_type);
10087 enum machine_mode pmode = TYPE_MODE (passed_type);
10089 /* This decl represents a formal parameter which was optimized out.
10090 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
10091 all cases where (rtl == NULL_RTX) just below. */
10092 if (dmode == pmode)
10093 rtl = DECL_INCOMING_RTL (decl);
10094 else if (SCALAR_INT_MODE_P (dmode)
10095 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
10096 && DECL_INCOMING_RTL (decl))
10098 rtx inc = DECL_INCOMING_RTL (decl);
10099 if (REG_P (inc))
10100 rtl = inc;
10101 else if (MEM_P (inc))
10103 if (BYTES_BIG_ENDIAN)
10104 rtl = adjust_address_nv (inc, dmode,
10105 GET_MODE_SIZE (pmode)
10106 - GET_MODE_SIZE (dmode));
10107 else
10108 rtl = inc;
10113 /* If the parm was passed in registers, but lives on the stack, then
10114 make a big endian correction if the mode of the type of the
10115 parameter is not the same as the mode of the rtl. */
10116 /* ??? This is the same series of checks that are made in dbxout.c before
10117 we reach the big endian correction code there. It isn't clear if all
10118 of these checks are necessary here, but keeping them all is the safe
10119 thing to do. */
10120 else if (MEM_P (rtl)
10121 && XEXP (rtl, 0) != const0_rtx
10122 && ! CONSTANT_P (XEXP (rtl, 0))
10123 /* Not passed in memory. */
10124 && !MEM_P (DECL_INCOMING_RTL (decl))
10125 /* Not passed by invisible reference. */
10126 && (!REG_P (XEXP (rtl, 0))
10127 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
10128 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
10129 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
10130 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
10131 #endif
10133 /* Big endian correction check. */
10134 && BYTES_BIG_ENDIAN
10135 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
10136 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
10137 < UNITS_PER_WORD))
10139 int offset = (UNITS_PER_WORD
10140 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
10142 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10143 plus_constant (XEXP (rtl, 0), offset));
10146 else if (TREE_CODE (decl) == VAR_DECL
10147 && rtl
10148 && MEM_P (rtl)
10149 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
10150 && BYTES_BIG_ENDIAN)
10152 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
10153 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
10155 /* If a variable is declared "register" yet is smaller than
10156 a register, then if we store the variable to memory, it
10157 looks like we're storing a register-sized value, when in
10158 fact we are not. We need to adjust the offset of the
10159 storage location to reflect the actual value's bytes,
10160 else gdb will not be able to display it. */
10161 if (rsize > dsize)
10162 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10163 plus_constant (XEXP (rtl, 0), rsize-dsize));
10166 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
10167 and will have been substituted directly into all expressions that use it.
10168 C does not have such a concept, but C++ and other languages do. */
10169 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
10170 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
10172 if (rtl)
10173 rtl = targetm.delegitimize_address (rtl);
10175 /* If we don't look past the constant pool, we risk emitting a
10176 reference to a constant pool entry that isn't referenced from
10177 code, and thus is not emitted. */
10178 if (rtl)
10179 rtl = avoid_constant_pool_reference (rtl);
10181 return rtl;
10184 /* We need to figure out what section we should use as the base for the
10185 address ranges where a given location is valid.
10186 1. If this particular DECL has a section associated with it, use that.
10187 2. If this function has a section associated with it, use that.
10188 3. Otherwise, use the text section.
10189 XXX: If you split a variable across multiple sections, we won't notice. */
10191 static const char *
10192 secname_for_decl (tree decl)
10194 const char *secname;
10196 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
10198 tree sectree = DECL_SECTION_NAME (decl);
10199 secname = TREE_STRING_POINTER (sectree);
10201 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
10203 tree sectree = DECL_SECTION_NAME (current_function_decl);
10204 secname = TREE_STRING_POINTER (sectree);
10206 else if (cfun && in_cold_section_p)
10207 secname = cfun->cold_section_label;
10208 else
10209 secname = text_section_label;
10211 return secname;
10214 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
10215 data attribute for a variable or a parameter. We generate the
10216 DW_AT_const_value attribute only in those cases where the given variable
10217 or parameter does not have a true "location" either in memory or in a
10218 register. This can happen (for example) when a constant is passed as an
10219 actual argument in a call to an inline function. (It's possible that
10220 these things can crop up in other ways also.) Note that one type of
10221 constant value which can be passed into an inlined function is a constant
10222 pointer. This can happen for example if an actual argument in an inlined
10223 function call evaluates to a compile-time constant address. */
10225 static void
10226 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
10227 enum dwarf_attribute attr)
10229 rtx rtl;
10230 dw_loc_descr_ref descr;
10231 var_loc_list *loc_list;
10232 struct var_loc_node *node;
10233 if (TREE_CODE (decl) == ERROR_MARK)
10234 return;
10236 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
10237 || TREE_CODE (decl) == RESULT_DECL);
10239 /* See if we possibly have multiple locations for this variable. */
10240 loc_list = lookup_decl_loc (decl);
10242 /* If it truly has multiple locations, the first and last node will
10243 differ. */
10244 if (loc_list && loc_list->first != loc_list->last)
10246 const char *endname, *secname;
10247 dw_loc_list_ref list;
10248 rtx varloc;
10250 /* Now that we know what section we are using for a base,
10251 actually construct the list of locations.
10252 The first location information is what is passed to the
10253 function that creates the location list, and the remaining
10254 locations just get added on to that list.
10255 Note that we only know the start address for a location
10256 (IE location changes), so to build the range, we use
10257 the range [current location start, next location start].
10258 This means we have to special case the last node, and generate
10259 a range of [last location start, end of function label]. */
10261 node = loc_list->first;
10262 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10263 secname = secname_for_decl (decl);
10265 list = new_loc_list (loc_descriptor (varloc),
10266 node->label, node->next->label, secname, 1);
10267 node = node->next;
10269 for (; node->next; node = node->next)
10270 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10272 /* The variable has a location between NODE->LABEL and
10273 NODE->NEXT->LABEL. */
10274 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10275 add_loc_descr_to_loc_list (&list, loc_descriptor (varloc),
10276 node->label, node->next->label, secname);
10279 /* If the variable has a location at the last label
10280 it keeps its location until the end of function. */
10281 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10283 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
10285 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10286 if (!current_function_decl)
10287 endname = text_end_label;
10288 else
10290 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
10291 current_function_funcdef_no);
10292 endname = ggc_strdup (label_id);
10294 add_loc_descr_to_loc_list (&list, loc_descriptor (varloc),
10295 node->label, endname, secname);
10298 /* Finally, add the location list to the DIE, and we are done. */
10299 add_AT_loc_list (die, attr, list);
10300 return;
10303 /* Try to get some constant RTL for this decl, and use that as the value of
10304 the location. */
10306 rtl = rtl_for_decl_location (decl);
10307 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
10309 add_const_value_attribute (die, rtl);
10310 return;
10313 /* If we have tried to generate the location otherwise, and it
10314 didn't work out (we wouldn't be here if we did), and we have a one entry
10315 location list, try generating a location from that. */
10316 if (loc_list && loc_list->first)
10318 node = loc_list->first;
10319 descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note));
10320 if (descr)
10322 add_AT_location_description (die, attr, descr);
10323 return;
10327 /* We couldn't get any rtl, so try directly generating the location
10328 description from the tree. */
10329 descr = loc_descriptor_from_tree (decl);
10330 if (descr)
10332 add_AT_location_description (die, attr, descr);
10333 return;
10335 /* None of that worked, so it must not really have a location;
10336 try adding a constant value attribute from the DECL_INITIAL. */
10337 tree_add_const_value_attribute (die, decl);
10340 /* If we don't have a copy of this variable in memory for some reason (such
10341 as a C++ member constant that doesn't have an out-of-line definition),
10342 we should tell the debugger about the constant value. */
10344 static void
10345 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
10347 tree init = DECL_INITIAL (decl);
10348 tree type = TREE_TYPE (decl);
10349 rtx rtl;
10351 if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
10352 /* OK */;
10353 else
10354 return;
10356 rtl = rtl_for_decl_init (init, type);
10357 if (rtl)
10358 add_const_value_attribute (var_die, rtl);
10361 /* Convert the CFI instructions for the current function into a
10362 location list. This is used for DW_AT_frame_base when we targeting
10363 a dwarf2 consumer that does not support the dwarf3
10364 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
10365 expressions. */
10367 static dw_loc_list_ref
10368 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
10370 dw_fde_ref fde;
10371 dw_loc_list_ref list, *list_tail;
10372 dw_cfi_ref cfi;
10373 dw_cfa_location last_cfa, next_cfa;
10374 const char *start_label, *last_label, *section;
10376 fde = &fde_table[fde_table_in_use - 1];
10378 section = secname_for_decl (current_function_decl);
10379 list_tail = &list;
10380 list = NULL;
10382 next_cfa.reg = INVALID_REGNUM;
10383 next_cfa.offset = 0;
10384 next_cfa.indirect = 0;
10385 next_cfa.base_offset = 0;
10387 start_label = fde->dw_fde_begin;
10389 /* ??? Bald assumption that the CIE opcode list does not contain
10390 advance opcodes. */
10391 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
10392 lookup_cfa_1 (cfi, &next_cfa);
10394 last_cfa = next_cfa;
10395 last_label = start_label;
10397 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
10398 switch (cfi->dw_cfi_opc)
10400 case DW_CFA_advance_loc1:
10401 case DW_CFA_advance_loc2:
10402 case DW_CFA_advance_loc4:
10403 if (!cfa_equal_p (&last_cfa, &next_cfa))
10405 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
10406 start_label, last_label, section,
10407 list == NULL);
10409 list_tail = &(*list_tail)->dw_loc_next;
10410 last_cfa = next_cfa;
10411 start_label = last_label;
10413 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
10414 break;
10416 case DW_CFA_advance_loc:
10417 /* The encoding is complex enough that we should never emit this. */
10418 case DW_CFA_remember_state:
10419 case DW_CFA_restore_state:
10420 /* We don't handle these two in this function. It would be possible
10421 if it were to be required. */
10422 gcc_unreachable ();
10424 default:
10425 lookup_cfa_1 (cfi, &next_cfa);
10426 break;
10429 if (!cfa_equal_p (&last_cfa, &next_cfa))
10431 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
10432 start_label, last_label, section,
10433 list == NULL);
10434 list_tail = &(*list_tail)->dw_loc_next;
10435 start_label = last_label;
10437 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
10438 start_label, fde->dw_fde_end, section,
10439 list == NULL);
10441 return list;
10444 /* Compute a displacement from the "steady-state frame pointer" to the
10445 frame base (often the same as the CFA), and store it in
10446 frame_pointer_fb_offset. OFFSET is added to the displacement
10447 before the latter is negated. */
10449 static void
10450 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
10452 rtx reg, elim;
10454 #ifdef FRAME_POINTER_CFA_OFFSET
10455 reg = frame_pointer_rtx;
10456 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
10457 #else
10458 reg = arg_pointer_rtx;
10459 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
10460 #endif
10462 elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10463 if (GET_CODE (elim) == PLUS)
10465 offset += INTVAL (XEXP (elim, 1));
10466 elim = XEXP (elim, 0);
10468 gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
10469 : stack_pointer_rtx));
10471 frame_pointer_fb_offset = -offset;
10474 /* Generate a DW_AT_name attribute given some string value to be included as
10475 the value of the attribute. */
10477 static void
10478 add_name_attribute (dw_die_ref die, const char *name_string)
10480 if (name_string != NULL && *name_string != 0)
10482 if (demangle_name_func)
10483 name_string = (*demangle_name_func) (name_string);
10485 add_AT_string (die, DW_AT_name, name_string);
10489 /* Generate a DW_AT_comp_dir attribute for DIE. */
10491 static void
10492 add_comp_dir_attribute (dw_die_ref die)
10494 const char *wd = get_src_pwd ();
10495 if (wd != NULL)
10496 add_AT_string (die, DW_AT_comp_dir, wd);
10499 /* Given a tree node describing an array bound (either lower or upper) output
10500 a representation for that bound. */
10502 static void
10503 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
10505 switch (TREE_CODE (bound))
10507 case ERROR_MARK:
10508 return;
10510 /* All fixed-bounds are represented by INTEGER_CST nodes. */
10511 case INTEGER_CST:
10512 if (! host_integerp (bound, 0)
10513 || (bound_attr == DW_AT_lower_bound
10514 && (((is_c_family () || is_java ()) && integer_zerop (bound))
10515 || (is_fortran () && integer_onep (bound)))))
10516 /* Use the default. */
10518 else
10519 add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
10520 break;
10522 case CONVERT_EXPR:
10523 case NOP_EXPR:
10524 case NON_LVALUE_EXPR:
10525 case VIEW_CONVERT_EXPR:
10526 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
10527 break;
10529 case SAVE_EXPR:
10530 break;
10532 case VAR_DECL:
10533 case PARM_DECL:
10534 case RESULT_DECL:
10536 dw_die_ref decl_die = lookup_decl_die (bound);
10538 /* ??? Can this happen, or should the variable have been bound
10539 first? Probably it can, since I imagine that we try to create
10540 the types of parameters in the order in which they exist in
10541 the list, and won't have created a forward reference to a
10542 later parameter. */
10543 if (decl_die != NULL)
10544 add_AT_die_ref (subrange_die, bound_attr, decl_die);
10545 break;
10548 default:
10550 /* Otherwise try to create a stack operation procedure to
10551 evaluate the value of the array bound. */
10553 dw_die_ref ctx, decl_die;
10554 dw_loc_descr_ref loc;
10556 loc = loc_descriptor_from_tree (bound);
10557 if (loc == NULL)
10558 break;
10560 if (current_function_decl == 0)
10561 ctx = comp_unit_die;
10562 else
10563 ctx = lookup_decl_die (current_function_decl);
10565 decl_die = new_die (DW_TAG_variable, ctx, bound);
10566 add_AT_flag (decl_die, DW_AT_artificial, 1);
10567 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
10568 add_AT_loc (decl_die, DW_AT_location, loc);
10570 add_AT_die_ref (subrange_die, bound_attr, decl_die);
10571 break;
10576 /* Note that the block of subscript information for an array type also
10577 includes information about the element type of type given array type. */
10579 static void
10580 add_subscript_info (dw_die_ref type_die, tree type)
10582 #ifndef MIPS_DEBUGGING_INFO
10583 unsigned dimension_number;
10584 #endif
10585 tree lower, upper;
10586 dw_die_ref subrange_die;
10588 /* The GNU compilers represent multidimensional array types as sequences of
10589 one dimensional array types whose element types are themselves array
10590 types. Here we squish that down, so that each multidimensional array
10591 type gets only one array_type DIE in the Dwarf debugging info. The draft
10592 Dwarf specification say that we are allowed to do this kind of
10593 compression in C (because there is no difference between an array or
10594 arrays and a multidimensional array in C) but for other source languages
10595 (e.g. Ada) we probably shouldn't do this. */
10597 /* ??? The SGI dwarf reader fails for multidimensional arrays with a
10598 const enum type. E.g. const enum machine_mode insn_operand_mode[2][10].
10599 We work around this by disabling this feature. See also
10600 gen_array_type_die. */
10601 #ifndef MIPS_DEBUGGING_INFO
10602 for (dimension_number = 0;
10603 TREE_CODE (type) == ARRAY_TYPE;
10604 type = TREE_TYPE (type), dimension_number++)
10605 #endif
10607 tree domain = TYPE_DOMAIN (type);
10609 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
10610 and (in GNU C only) variable bounds. Handle all three forms
10611 here. */
10612 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
10613 if (domain)
10615 /* We have an array type with specified bounds. */
10616 lower = TYPE_MIN_VALUE (domain);
10617 upper = TYPE_MAX_VALUE (domain);
10619 /* Define the index type. */
10620 if (TREE_TYPE (domain))
10622 /* ??? This is probably an Ada unnamed subrange type. Ignore the
10623 TREE_TYPE field. We can't emit debug info for this
10624 because it is an unnamed integral type. */
10625 if (TREE_CODE (domain) == INTEGER_TYPE
10626 && TYPE_NAME (domain) == NULL_TREE
10627 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
10628 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
10630 else
10631 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
10632 type_die);
10635 /* ??? If upper is NULL, the array has unspecified length,
10636 but it does have a lower bound. This happens with Fortran
10637 dimension arr(N:*)
10638 Since the debugger is definitely going to need to know N
10639 to produce useful results, go ahead and output the lower
10640 bound solo, and hope the debugger can cope. */
10642 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
10643 if (upper)
10644 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
10647 /* Otherwise we have an array type with an unspecified length. The
10648 DWARF-2 spec does not say how to handle this; let's just leave out the
10649 bounds. */
10653 static void
10654 add_byte_size_attribute (dw_die_ref die, tree tree_node)
10656 unsigned size;
10658 switch (TREE_CODE (tree_node))
10660 case ERROR_MARK:
10661 size = 0;
10662 break;
10663 case ENUMERAL_TYPE:
10664 case RECORD_TYPE:
10665 case UNION_TYPE:
10666 case QUAL_UNION_TYPE:
10667 size = int_size_in_bytes (tree_node);
10668 break;
10669 case FIELD_DECL:
10670 /* For a data member of a struct or union, the DW_AT_byte_size is
10671 generally given as the number of bytes normally allocated for an
10672 object of the *declared* type of the member itself. This is true
10673 even for bit-fields. */
10674 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
10675 break;
10676 default:
10677 gcc_unreachable ();
10680 /* Note that `size' might be -1 when we get to this point. If it is, that
10681 indicates that the byte size of the entity in question is variable. We
10682 have no good way of expressing this fact in Dwarf at the present time,
10683 so just let the -1 pass on through. */
10684 add_AT_unsigned (die, DW_AT_byte_size, size);
10687 /* For a FIELD_DECL node which represents a bit-field, output an attribute
10688 which specifies the distance in bits from the highest order bit of the
10689 "containing object" for the bit-field to the highest order bit of the
10690 bit-field itself.
10692 For any given bit-field, the "containing object" is a hypothetical object
10693 (of some integral or enum type) within which the given bit-field lives. The
10694 type of this hypothetical "containing object" is always the same as the
10695 declared type of the individual bit-field itself. The determination of the
10696 exact location of the "containing object" for a bit-field is rather
10697 complicated. It's handled by the `field_byte_offset' function (above).
10699 Note that it is the size (in bytes) of the hypothetical "containing object"
10700 which will be given in the DW_AT_byte_size attribute for this bit-field.
10701 (See `byte_size_attribute' above). */
10703 static inline void
10704 add_bit_offset_attribute (dw_die_ref die, tree decl)
10706 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
10707 tree type = DECL_BIT_FIELD_TYPE (decl);
10708 HOST_WIDE_INT bitpos_int;
10709 HOST_WIDE_INT highest_order_object_bit_offset;
10710 HOST_WIDE_INT highest_order_field_bit_offset;
10711 HOST_WIDE_INT unsigned bit_offset;
10713 /* Must be a field and a bit field. */
10714 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
10716 /* We can't yet handle bit-fields whose offsets are variable, so if we
10717 encounter such things, just return without generating any attribute
10718 whatsoever. Likewise for variable or too large size. */
10719 if (! host_integerp (bit_position (decl), 0)
10720 || ! host_integerp (DECL_SIZE (decl), 1))
10721 return;
10723 bitpos_int = int_bit_position (decl);
10725 /* Note that the bit offset is always the distance (in bits) from the
10726 highest-order bit of the "containing object" to the highest-order bit of
10727 the bit-field itself. Since the "high-order end" of any object or field
10728 is different on big-endian and little-endian machines, the computation
10729 below must take account of these differences. */
10730 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
10731 highest_order_field_bit_offset = bitpos_int;
10733 if (! BYTES_BIG_ENDIAN)
10735 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
10736 highest_order_object_bit_offset += simple_type_size_in_bits (type);
10739 bit_offset
10740 = (! BYTES_BIG_ENDIAN
10741 ? highest_order_object_bit_offset - highest_order_field_bit_offset
10742 : highest_order_field_bit_offset - highest_order_object_bit_offset);
10744 add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
10747 /* For a FIELD_DECL node which represents a bit field, output an attribute
10748 which specifies the length in bits of the given field. */
10750 static inline void
10751 add_bit_size_attribute (dw_die_ref die, tree decl)
10753 /* Must be a field and a bit field. */
10754 gcc_assert (TREE_CODE (decl) == FIELD_DECL
10755 && DECL_BIT_FIELD_TYPE (decl));
10757 if (host_integerp (DECL_SIZE (decl), 1))
10758 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
10761 /* If the compiled language is ANSI C, then add a 'prototyped'
10762 attribute, if arg types are given for the parameters of a function. */
10764 static inline void
10765 add_prototyped_attribute (dw_die_ref die, tree func_type)
10767 if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
10768 && TYPE_ARG_TYPES (func_type) != NULL)
10769 add_AT_flag (die, DW_AT_prototyped, 1);
10772 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
10773 by looking in either the type declaration or object declaration
10774 equate table. */
10776 static inline void
10777 add_abstract_origin_attribute (dw_die_ref die, tree origin)
10779 dw_die_ref origin_die = NULL;
10781 if (TREE_CODE (origin) != FUNCTION_DECL)
10783 /* We may have gotten separated from the block for the inlined
10784 function, if we're in an exception handler or some such; make
10785 sure that the abstract function has been written out.
10787 Doing this for nested functions is wrong, however; functions are
10788 distinct units, and our context might not even be inline. */
10789 tree fn = origin;
10791 if (TYPE_P (fn))
10792 fn = TYPE_STUB_DECL (fn);
10794 fn = decl_function_context (fn);
10795 if (fn)
10796 dwarf2out_abstract_function (fn);
10799 if (DECL_P (origin))
10800 origin_die = lookup_decl_die (origin);
10801 else if (TYPE_P (origin))
10802 origin_die = lookup_type_die (origin);
10804 /* XXX: Functions that are never lowered don't always have correct block
10805 trees (in the case of java, they simply have no block tree, in some other
10806 languages). For these functions, there is nothing we can really do to
10807 output correct debug info for inlined functions in all cases. Rather
10808 than die, we'll just produce deficient debug info now, in that we will
10809 have variables without a proper abstract origin. In the future, when all
10810 functions are lowered, we should re-add a gcc_assert (origin_die)
10811 here. */
10813 if (origin_die)
10814 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
10817 /* We do not currently support the pure_virtual attribute. */
10819 static inline void
10820 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
10822 if (DECL_VINDEX (func_decl))
10824 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
10826 if (host_integerp (DECL_VINDEX (func_decl), 0))
10827 add_AT_loc (die, DW_AT_vtable_elem_location,
10828 new_loc_descr (DW_OP_constu,
10829 tree_low_cst (DECL_VINDEX (func_decl), 0),
10830 0));
10832 /* GNU extension: Record what type this method came from originally. */
10833 if (debug_info_level > DINFO_LEVEL_TERSE)
10834 add_AT_die_ref (die, DW_AT_containing_type,
10835 lookup_type_die (DECL_CONTEXT (func_decl)));
10839 /* Add source coordinate attributes for the given decl. */
10841 static void
10842 add_src_coords_attributes (dw_die_ref die, tree decl)
10844 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
10845 unsigned file_index = lookup_filename (s.file);
10847 add_AT_unsigned (die, DW_AT_decl_file, file_index);
10848 add_AT_unsigned (die, DW_AT_decl_line, s.line);
10851 /* Add a DW_AT_name attribute and source coordinate attribute for the
10852 given decl, but only if it actually has a name. */
10854 static void
10855 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
10857 tree decl_name;
10859 decl_name = DECL_NAME (decl);
10860 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
10862 add_name_attribute (die, dwarf2_name (decl, 0));
10863 if (! DECL_ARTIFICIAL (decl))
10864 add_src_coords_attributes (die, decl);
10866 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
10867 && TREE_PUBLIC (decl)
10868 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
10869 && !DECL_ABSTRACT (decl)
10870 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl)))
10871 add_AT_string (die, DW_AT_MIPS_linkage_name,
10872 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
10875 #ifdef VMS_DEBUGGING_INFO
10876 /* Get the function's name, as described by its RTL. This may be different
10877 from the DECL_NAME name used in the source file. */
10878 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
10880 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
10881 XEXP (DECL_RTL (decl), 0));
10882 VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
10884 #endif
10887 /* Push a new declaration scope. */
10889 static void
10890 push_decl_scope (tree scope)
10892 VEC_safe_push (tree, gc, decl_scope_table, scope);
10895 /* Pop a declaration scope. */
10897 static inline void
10898 pop_decl_scope (void)
10900 VEC_pop (tree, decl_scope_table);
10903 /* Return the DIE for the scope that immediately contains this type.
10904 Non-named types get global scope. Named types nested in other
10905 types get their containing scope if it's open, or global scope
10906 otherwise. All other types (i.e. function-local named types) get
10907 the current active scope. */
10909 static dw_die_ref
10910 scope_die_for (tree t, dw_die_ref context_die)
10912 dw_die_ref scope_die = NULL;
10913 tree containing_scope;
10914 int i;
10916 /* Non-types always go in the current scope. */
10917 gcc_assert (TYPE_P (t));
10919 containing_scope = TYPE_CONTEXT (t);
10921 /* Use the containing namespace if it was passed in (for a declaration). */
10922 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
10924 if (context_die == lookup_decl_die (containing_scope))
10925 /* OK */;
10926 else
10927 containing_scope = NULL_TREE;
10930 /* Ignore function type "scopes" from the C frontend. They mean that
10931 a tagged type is local to a parmlist of a function declarator, but
10932 that isn't useful to DWARF. */
10933 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
10934 containing_scope = NULL_TREE;
10936 if (containing_scope == NULL_TREE)
10937 scope_die = comp_unit_die;
10938 else if (TYPE_P (containing_scope))
10940 /* For types, we can just look up the appropriate DIE. But
10941 first we check to see if we're in the middle of emitting it
10942 so we know where the new DIE should go. */
10943 for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
10944 if (VEC_index (tree, decl_scope_table, i) == containing_scope)
10945 break;
10947 if (i < 0)
10949 gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
10950 || TREE_ASM_WRITTEN (containing_scope));
10952 /* If none of the current dies are suitable, we get file scope. */
10953 scope_die = comp_unit_die;
10955 else
10956 scope_die = lookup_type_die (containing_scope);
10958 else
10959 scope_die = context_die;
10961 return scope_die;
10964 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
10966 static inline int
10967 local_scope_p (dw_die_ref context_die)
10969 for (; context_die; context_die = context_die->die_parent)
10970 if (context_die->die_tag == DW_TAG_inlined_subroutine
10971 || context_die->die_tag == DW_TAG_subprogram)
10972 return 1;
10974 return 0;
10977 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
10978 whether or not to treat a DIE in this context as a declaration. */
10980 static inline int
10981 class_or_namespace_scope_p (dw_die_ref context_die)
10983 return (context_die
10984 && (context_die->die_tag == DW_TAG_structure_type
10985 || context_die->die_tag == DW_TAG_union_type
10986 || context_die->die_tag == DW_TAG_namespace));
10989 /* Many forms of DIEs require a "type description" attribute. This
10990 routine locates the proper "type descriptor" die for the type given
10991 by 'type', and adds a DW_AT_type attribute below the given die. */
10993 static void
10994 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
10995 int decl_volatile, dw_die_ref context_die)
10997 enum tree_code code = TREE_CODE (type);
10998 dw_die_ref type_die = NULL;
11000 /* ??? If this type is an unnamed subrange type of an integral or
11001 floating-point type, use the inner type. This is because we have no
11002 support for unnamed types in base_type_die. This can happen if this is
11003 an Ada subrange type. Correct solution is emit a subrange type die. */
11004 if ((code == INTEGER_TYPE || code == REAL_TYPE)
11005 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
11006 type = TREE_TYPE (type), code = TREE_CODE (type);
11008 if (code == ERROR_MARK
11009 /* Handle a special case. For functions whose return type is void, we
11010 generate *no* type attribute. (Note that no object may have type
11011 `void', so this only applies to function return types). */
11012 || code == VOID_TYPE)
11013 return;
11015 type_die = modified_type_die (type,
11016 decl_const || TYPE_READONLY (type),
11017 decl_volatile || TYPE_VOLATILE (type),
11018 context_die);
11020 if (type_die != NULL)
11021 add_AT_die_ref (object_die, DW_AT_type, type_die);
11024 /* Given an object die, add the calling convention attribute for the
11025 function call type. */
11026 static void
11027 add_calling_convention_attribute (dw_die_ref subr_die, tree type)
11029 enum dwarf_calling_convention value = DW_CC_normal;
11031 value = targetm.dwarf_calling_convention (type);
11033 /* Only add the attribute if the backend requests it, and
11034 is not DW_CC_normal. */
11035 if (value && (value != DW_CC_normal))
11036 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
11039 /* Given a tree pointer to a struct, class, union, or enum type node, return
11040 a pointer to the (string) tag name for the given type, or zero if the type
11041 was declared without a tag. */
11043 static const char *
11044 type_tag (tree type)
11046 const char *name = 0;
11048 if (TYPE_NAME (type) != 0)
11050 tree t = 0;
11052 /* Find the IDENTIFIER_NODE for the type name. */
11053 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
11054 t = TYPE_NAME (type);
11056 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
11057 a TYPE_DECL node, regardless of whether or not a `typedef' was
11058 involved. */
11059 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11060 && ! DECL_IGNORED_P (TYPE_NAME (type)))
11061 t = DECL_NAME (TYPE_NAME (type));
11063 /* Now get the name as a string, or invent one. */
11064 if (t != 0)
11065 name = IDENTIFIER_POINTER (t);
11068 return (name == 0 || *name == '\0') ? 0 : name;
11071 /* Return the type associated with a data member, make a special check
11072 for bit field types. */
11074 static inline tree
11075 member_declared_type (tree member)
11077 return (DECL_BIT_FIELD_TYPE (member)
11078 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
11081 /* Get the decl's label, as described by its RTL. This may be different
11082 from the DECL_NAME name used in the source file. */
11084 #if 0
11085 static const char *
11086 decl_start_label (tree decl)
11088 rtx x;
11089 const char *fnname;
11091 x = DECL_RTL (decl);
11092 gcc_assert (MEM_P (x));
11094 x = XEXP (x, 0);
11095 gcc_assert (GET_CODE (x) == SYMBOL_REF);
11097 fnname = XSTR (x, 0);
11098 return fnname;
11100 #endif
11102 /* These routines generate the internal representation of the DIE's for
11103 the compilation unit. Debugging information is collected by walking
11104 the declaration trees passed in from dwarf2out_decl(). */
11106 static void
11107 gen_array_type_die (tree type, dw_die_ref context_die)
11109 dw_die_ref scope_die = scope_die_for (type, context_die);
11110 dw_die_ref array_die;
11111 tree element_type;
11113 /* ??? The SGI dwarf reader fails for array of array of enum types unless
11114 the inner array type comes before the outer array type. Thus we must
11115 call gen_type_die before we call new_die. See below also. */
11116 #ifdef MIPS_DEBUGGING_INFO
11117 gen_type_die (TREE_TYPE (type), context_die);
11118 #endif
11120 array_die = new_die (DW_TAG_array_type, scope_die, type);
11121 add_name_attribute (array_die, type_tag (type));
11122 equate_type_number_to_die (type, array_die);
11124 if (TREE_CODE (type) == VECTOR_TYPE)
11126 /* The frontend feeds us a representation for the vector as a struct
11127 containing an array. Pull out the array type. */
11128 type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
11129 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
11132 #if 0
11133 /* We default the array ordering. SDB will probably do
11134 the right things even if DW_AT_ordering is not present. It's not even
11135 an issue until we start to get into multidimensional arrays anyway. If
11136 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
11137 then we'll have to put the DW_AT_ordering attribute back in. (But if
11138 and when we find out that we need to put these in, we will only do so
11139 for multidimensional arrays. */
11140 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
11141 #endif
11143 #ifdef MIPS_DEBUGGING_INFO
11144 /* The SGI compilers handle arrays of unknown bound by setting
11145 AT_declaration and not emitting any subrange DIEs. */
11146 if (! TYPE_DOMAIN (type))
11147 add_AT_flag (array_die, DW_AT_declaration, 1);
11148 else
11149 #endif
11150 add_subscript_info (array_die, type);
11152 /* Add representation of the type of the elements of this array type. */
11153 element_type = TREE_TYPE (type);
11155 /* ??? The SGI dwarf reader fails for multidimensional arrays with a
11156 const enum type. E.g. const enum machine_mode insn_operand_mode[2][10].
11157 We work around this by disabling this feature. See also
11158 add_subscript_info. */
11159 #ifndef MIPS_DEBUGGING_INFO
11160 while (TREE_CODE (element_type) == ARRAY_TYPE)
11161 element_type = TREE_TYPE (element_type);
11163 gen_type_die (element_type, context_die);
11164 #endif
11166 add_type_attribute (array_die, element_type, 0, 0, context_die);
11169 #if 0
11170 static void
11171 gen_entry_point_die (tree decl, dw_die_ref context_die)
11173 tree origin = decl_ultimate_origin (decl);
11174 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
11176 if (origin != NULL)
11177 add_abstract_origin_attribute (decl_die, origin);
11178 else
11180 add_name_and_src_coords_attributes (decl_die, decl);
11181 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
11182 0, 0, context_die);
11185 if (DECL_ABSTRACT (decl))
11186 equate_decl_number_to_die (decl, decl_die);
11187 else
11188 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
11190 #endif
11192 /* Walk through the list of incomplete types again, trying once more to
11193 emit full debugging info for them. */
11195 static void
11196 retry_incomplete_types (void)
11198 int i;
11200 for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
11201 gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
11204 /* Generate a DIE to represent an inlined instance of an enumeration type. */
11206 static void
11207 gen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
11209 dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
11211 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11212 be incomplete and such types are not marked. */
11213 add_abstract_origin_attribute (type_die, type);
11216 /* Generate a DIE to represent an inlined instance of a structure type. */
11218 static void
11219 gen_inlined_structure_type_die (tree type, dw_die_ref context_die)
11221 dw_die_ref type_die = new_die (DW_TAG_structure_type, context_die, type);
11223 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11224 be incomplete and such types are not marked. */
11225 add_abstract_origin_attribute (type_die, type);
11228 /* Generate a DIE to represent an inlined instance of a union type. */
11230 static void
11231 gen_inlined_union_type_die (tree type, dw_die_ref context_die)
11233 dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
11235 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11236 be incomplete and such types are not marked. */
11237 add_abstract_origin_attribute (type_die, type);
11240 /* Generate a DIE to represent an enumeration type. Note that these DIEs
11241 include all of the information about the enumeration values also. Each
11242 enumerated type name/value is listed as a child of the enumerated type
11243 DIE. */
11245 static dw_die_ref
11246 gen_enumeration_type_die (tree type, dw_die_ref context_die)
11248 dw_die_ref type_die = lookup_type_die (type);
11250 if (type_die == NULL)
11252 type_die = new_die (DW_TAG_enumeration_type,
11253 scope_die_for (type, context_die), type);
11254 equate_type_number_to_die (type, type_die);
11255 add_name_attribute (type_die, type_tag (type));
11257 else if (! TYPE_SIZE (type))
11258 return type_die;
11259 else
11260 remove_AT (type_die, DW_AT_declaration);
11262 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
11263 given enum type is incomplete, do not generate the DW_AT_byte_size
11264 attribute or the DW_AT_element_list attribute. */
11265 if (TYPE_SIZE (type))
11267 tree link;
11269 TREE_ASM_WRITTEN (type) = 1;
11270 add_byte_size_attribute (type_die, type);
11271 if (TYPE_STUB_DECL (type) != NULL_TREE)
11272 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
11274 /* If the first reference to this type was as the return type of an
11275 inline function, then it may not have a parent. Fix this now. */
11276 if (type_die->die_parent == NULL)
11277 add_child_die (scope_die_for (type, context_die), type_die);
11279 for (link = TYPE_VALUES (type);
11280 link != NULL; link = TREE_CHAIN (link))
11282 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
11283 tree value = TREE_VALUE (link);
11285 add_name_attribute (enum_die,
11286 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
11288 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
11289 /* DWARF2 does not provide a way of indicating whether or
11290 not enumeration constants are signed or unsigned. GDB
11291 always assumes the values are signed, so we output all
11292 values as if they were signed. That means that
11293 enumeration constants with very large unsigned values
11294 will appear to have negative values in the debugger. */
11295 add_AT_int (enum_die, DW_AT_const_value,
11296 tree_low_cst (value, tree_int_cst_sgn (value) > 0));
11299 else
11300 add_AT_flag (type_die, DW_AT_declaration, 1);
11302 return type_die;
11305 /* Generate a DIE to represent either a real live formal parameter decl or to
11306 represent just the type of some formal parameter position in some function
11307 type.
11309 Note that this routine is a bit unusual because its argument may be a
11310 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
11311 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
11312 node. If it's the former then this function is being called to output a
11313 DIE to represent a formal parameter object (or some inlining thereof). If
11314 it's the latter, then this function is only being called to output a
11315 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
11316 argument type of some subprogram type. */
11318 static dw_die_ref
11319 gen_formal_parameter_die (tree node, dw_die_ref context_die)
11321 dw_die_ref parm_die
11322 = new_die (DW_TAG_formal_parameter, context_die, node);
11323 tree origin;
11325 switch (TREE_CODE_CLASS (TREE_CODE (node)))
11327 case tcc_declaration:
11328 origin = decl_ultimate_origin (node);
11329 if (origin != NULL)
11330 add_abstract_origin_attribute (parm_die, origin);
11331 else
11333 add_name_and_src_coords_attributes (parm_die, node);
11334 add_type_attribute (parm_die, TREE_TYPE (node),
11335 TREE_READONLY (node),
11336 TREE_THIS_VOLATILE (node),
11337 context_die);
11338 if (DECL_ARTIFICIAL (node))
11339 add_AT_flag (parm_die, DW_AT_artificial, 1);
11342 equate_decl_number_to_die (node, parm_die);
11343 if (! DECL_ABSTRACT (node))
11344 add_location_or_const_value_attribute (parm_die, node, DW_AT_location);
11346 break;
11348 case tcc_type:
11349 /* We were called with some kind of a ..._TYPE node. */
11350 add_type_attribute (parm_die, node, 0, 0, context_die);
11351 break;
11353 default:
11354 gcc_unreachable ();
11357 return parm_die;
11360 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
11361 at the end of an (ANSI prototyped) formal parameters list. */
11363 static void
11364 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
11366 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
11369 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
11370 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
11371 parameters as specified in some function type specification (except for
11372 those which appear as part of a function *definition*). */
11374 static void
11375 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
11377 tree link;
11378 tree formal_type = NULL;
11379 tree first_parm_type;
11380 tree arg;
11382 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
11384 arg = DECL_ARGUMENTS (function_or_method_type);
11385 function_or_method_type = TREE_TYPE (function_or_method_type);
11387 else
11388 arg = NULL_TREE;
11390 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
11392 /* Make our first pass over the list of formal parameter types and output a
11393 DW_TAG_formal_parameter DIE for each one. */
11394 for (link = first_parm_type; link; )
11396 dw_die_ref parm_die;
11398 formal_type = TREE_VALUE (link);
11399 if (formal_type == void_type_node)
11400 break;
11402 /* Output a (nameless) DIE to represent the formal parameter itself. */
11403 parm_die = gen_formal_parameter_die (formal_type, context_die);
11404 if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
11405 && link == first_parm_type)
11406 || (arg && DECL_ARTIFICIAL (arg)))
11407 add_AT_flag (parm_die, DW_AT_artificial, 1);
11409 link = TREE_CHAIN (link);
11410 if (arg)
11411 arg = TREE_CHAIN (arg);
11414 /* If this function type has an ellipsis, add a
11415 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
11416 if (formal_type != void_type_node)
11417 gen_unspecified_parameters_die (function_or_method_type, context_die);
11419 /* Make our second (and final) pass over the list of formal parameter types
11420 and output DIEs to represent those types (as necessary). */
11421 for (link = TYPE_ARG_TYPES (function_or_method_type);
11422 link && TREE_VALUE (link);
11423 link = TREE_CHAIN (link))
11424 gen_type_die (TREE_VALUE (link), context_die);
11427 /* We want to generate the DIE for TYPE so that we can generate the
11428 die for MEMBER, which has been defined; we will need to refer back
11429 to the member declaration nested within TYPE. If we're trying to
11430 generate minimal debug info for TYPE, processing TYPE won't do the
11431 trick; we need to attach the member declaration by hand. */
11433 static void
11434 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
11436 gen_type_die (type, context_die);
11438 /* If we're trying to avoid duplicate debug info, we may not have
11439 emitted the member decl for this function. Emit it now. */
11440 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
11441 && ! lookup_decl_die (member))
11443 dw_die_ref type_die;
11444 gcc_assert (!decl_ultimate_origin (member));
11446 push_decl_scope (type);
11447 type_die = lookup_type_die (type);
11448 if (TREE_CODE (member) == FUNCTION_DECL)
11449 gen_subprogram_die (member, type_die);
11450 else if (TREE_CODE (member) == FIELD_DECL)
11452 /* Ignore the nameless fields that are used to skip bits but handle
11453 C++ anonymous unions and structs. */
11454 if (DECL_NAME (member) != NULL_TREE
11455 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
11456 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
11458 gen_type_die (member_declared_type (member), type_die);
11459 gen_field_die (member, type_die);
11462 else
11463 gen_variable_die (member, type_die);
11465 pop_decl_scope ();
11469 /* Generate the DWARF2 info for the "abstract" instance of a function which we
11470 may later generate inlined and/or out-of-line instances of. */
11472 static void
11473 dwarf2out_abstract_function (tree decl)
11475 dw_die_ref old_die;
11476 tree save_fn;
11477 tree context;
11478 int was_abstract = DECL_ABSTRACT (decl);
11480 /* Make sure we have the actual abstract inline, not a clone. */
11481 decl = DECL_ORIGIN (decl);
11483 old_die = lookup_decl_die (decl);
11484 if (old_die && get_AT (old_die, DW_AT_inline))
11485 /* We've already generated the abstract instance. */
11486 return;
11488 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
11489 we don't get confused by DECL_ABSTRACT. */
11490 if (debug_info_level > DINFO_LEVEL_TERSE)
11492 context = decl_class_context (decl);
11493 if (context)
11494 gen_type_die_for_member
11495 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
11498 /* Pretend we've just finished compiling this function. */
11499 save_fn = current_function_decl;
11500 current_function_decl = decl;
11502 set_decl_abstract_flags (decl, 1);
11503 dwarf2out_decl (decl);
11504 if (! was_abstract)
11505 set_decl_abstract_flags (decl, 0);
11507 current_function_decl = save_fn;
11510 /* Generate a DIE to represent a declared function (either file-scope or
11511 block-local). */
11513 static void
11514 gen_subprogram_die (tree decl, dw_die_ref context_die)
11516 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
11517 tree origin = decl_ultimate_origin (decl);
11518 dw_die_ref subr_die;
11519 tree fn_arg_types;
11520 tree outer_scope;
11521 dw_die_ref old_die = lookup_decl_die (decl);
11522 int declaration = (current_function_decl != decl
11523 || class_or_namespace_scope_p (context_die));
11525 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
11526 started to generate the abstract instance of an inline, decided to output
11527 its containing class, and proceeded to emit the declaration of the inline
11528 from the member list for the class. If so, DECLARATION takes priority;
11529 we'll get back to the abstract instance when done with the class. */
11531 /* The class-scope declaration DIE must be the primary DIE. */
11532 if (origin && declaration && class_or_namespace_scope_p (context_die))
11534 origin = NULL;
11535 gcc_assert (!old_die);
11538 /* Now that the C++ front end lazily declares artificial member fns, we
11539 might need to retrofit the declaration into its class. */
11540 if (!declaration && !origin && !old_die
11541 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
11542 && !class_or_namespace_scope_p (context_die)
11543 && debug_info_level > DINFO_LEVEL_TERSE)
11544 old_die = force_decl_die (decl);
11546 if (origin != NULL)
11548 gcc_assert (!declaration || local_scope_p (context_die));
11550 /* Fixup die_parent for the abstract instance of a nested
11551 inline function. */
11552 if (old_die && old_die->die_parent == NULL)
11553 add_child_die (context_die, old_die);
11555 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11556 add_abstract_origin_attribute (subr_die, origin);
11558 else if (old_die)
11560 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11561 unsigned file_index = lookup_filename (s.file);
11563 if (!get_AT_flag (old_die, DW_AT_declaration)
11564 /* We can have a normal definition following an inline one in the
11565 case of redefinition of GNU C extern inlines.
11566 It seems reasonable to use AT_specification in this case. */
11567 && !get_AT (old_die, DW_AT_inline))
11569 /* Detect and ignore this case, where we are trying to output
11570 something we have already output. */
11571 return;
11574 /* If the definition comes from the same place as the declaration,
11575 maybe use the old DIE. We always want the DIE for this function
11576 that has the *_pc attributes to be under comp_unit_die so the
11577 debugger can find it. We also need to do this for abstract
11578 instances of inlines, since the spec requires the out-of-line copy
11579 to have the same parent. For local class methods, this doesn't
11580 apply; we just use the old DIE. */
11581 if ((old_die->die_parent == comp_unit_die || context_die == NULL)
11582 && (DECL_ARTIFICIAL (decl)
11583 || (get_AT_unsigned (old_die, DW_AT_decl_file) == file_index
11584 && (get_AT_unsigned (old_die, DW_AT_decl_line)
11585 == (unsigned) s.line))))
11587 subr_die = old_die;
11589 /* Clear out the declaration attribute and the formal parameters.
11590 Do not remove all children, because it is possible that this
11591 declaration die was forced using force_decl_die(). In such
11592 cases die that forced declaration die (e.g. TAG_imported_module)
11593 is one of the children that we do not want to remove. */
11594 remove_AT (subr_die, DW_AT_declaration);
11595 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
11597 else
11599 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11600 add_AT_specification (subr_die, old_die);
11601 if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
11602 add_AT_unsigned (subr_die, DW_AT_decl_file, file_index);
11603 if (get_AT_unsigned (old_die, DW_AT_decl_line)
11604 != (unsigned) s.line)
11605 add_AT_unsigned
11606 (subr_die, DW_AT_decl_line, s.line);
11609 else
11611 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11613 if (TREE_PUBLIC (decl))
11614 add_AT_flag (subr_die, DW_AT_external, 1);
11616 add_name_and_src_coords_attributes (subr_die, decl);
11617 if (debug_info_level > DINFO_LEVEL_TERSE)
11619 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
11620 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
11621 0, 0, context_die);
11624 add_pure_or_virtual_attribute (subr_die, decl);
11625 if (DECL_ARTIFICIAL (decl))
11626 add_AT_flag (subr_die, DW_AT_artificial, 1);
11628 if (TREE_PROTECTED (decl))
11629 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
11630 else if (TREE_PRIVATE (decl))
11631 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
11634 if (declaration)
11636 if (!old_die || !get_AT (old_die, DW_AT_inline))
11638 add_AT_flag (subr_die, DW_AT_declaration, 1);
11640 /* The first time we see a member function, it is in the context of
11641 the class to which it belongs. We make sure of this by emitting
11642 the class first. The next time is the definition, which is
11643 handled above. The two may come from the same source text.
11645 Note that force_decl_die() forces function declaration die. It is
11646 later reused to represent definition. */
11647 equate_decl_number_to_die (decl, subr_die);
11650 else if (DECL_ABSTRACT (decl))
11652 if (DECL_DECLARED_INLINE_P (decl))
11654 if (cgraph_function_possibly_inlined_p (decl))
11655 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
11656 else
11657 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
11659 else
11661 if (cgraph_function_possibly_inlined_p (decl))
11662 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
11663 else
11664 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
11667 equate_decl_number_to_die (decl, subr_die);
11669 else if (!DECL_EXTERNAL (decl))
11671 HOST_WIDE_INT cfa_fb_offset;
11673 if (!old_die || !get_AT (old_die, DW_AT_inline))
11674 equate_decl_number_to_die (decl, subr_die);
11676 if (!flag_reorder_blocks_and_partition)
11678 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
11679 current_function_funcdef_no);
11680 add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
11681 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
11682 current_function_funcdef_no);
11683 add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
11685 add_pubname (decl, subr_die);
11686 add_arange (decl, subr_die);
11688 else
11689 { /* Do nothing for now; maybe need to duplicate die, one for
11690 hot section and ond for cold section, then use the hot/cold
11691 section begin/end labels to generate the aranges... */
11693 add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
11694 add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
11695 add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
11696 add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
11698 add_pubname (decl, subr_die);
11699 add_arange (decl, subr_die);
11700 add_arange (decl, subr_die);
11704 #ifdef MIPS_DEBUGGING_INFO
11705 /* Add a reference to the FDE for this routine. */
11706 add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
11707 #endif
11709 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
11711 /* We define the "frame base" as the function's CFA. This is more
11712 convenient for several reasons: (1) It's stable across the prologue
11713 and epilogue, which makes it better than just a frame pointer,
11714 (2) With dwarf3, there exists a one-byte encoding that allows us
11715 to reference the .debug_frame data by proxy, but failing that,
11716 (3) We can at least reuse the code inspection and interpretation
11717 code that determines the CFA position at various points in the
11718 function. */
11719 /* ??? Use some command-line or configury switch to enable the use
11720 of dwarf3 DW_OP_call_frame_cfa. At present there are no dwarf
11721 consumers that understand it; fall back to "pure" dwarf2 and
11722 convert the CFA data into a location list. */
11724 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
11725 if (list->dw_loc_next)
11726 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
11727 else
11728 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
11731 /* Compute a displacement from the "steady-state frame pointer" to
11732 the CFA. The former is what all stack slots and argument slots
11733 will reference in the rtl; the later is what we've told the
11734 debugger about. We'll need to adjust all frame_base references
11735 by this displacement. */
11736 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
11738 if (cfun->static_chain_decl)
11739 add_AT_location_description (subr_die, DW_AT_static_link,
11740 loc_descriptor_from_tree (cfun->static_chain_decl));
11743 /* Now output descriptions of the arguments for this function. This gets
11744 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
11745 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
11746 `...' at the end of the formal parameter list. In order to find out if
11747 there was a trailing ellipsis or not, we must instead look at the type
11748 associated with the FUNCTION_DECL. This will be a node of type
11749 FUNCTION_TYPE. If the chain of type nodes hanging off of this
11750 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
11751 an ellipsis at the end. */
11753 /* In the case where we are describing a mere function declaration, all we
11754 need to do here (and all we *can* do here) is to describe the *types* of
11755 its formal parameters. */
11756 if (debug_info_level <= DINFO_LEVEL_TERSE)
11758 else if (declaration)
11759 gen_formal_types_die (decl, subr_die);
11760 else
11762 /* Generate DIEs to represent all known formal parameters. */
11763 tree arg_decls = DECL_ARGUMENTS (decl);
11764 tree parm;
11766 /* When generating DIEs, generate the unspecified_parameters DIE
11767 instead if we come across the arg "__builtin_va_alist" */
11768 for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
11769 if (TREE_CODE (parm) == PARM_DECL)
11771 if (DECL_NAME (parm)
11772 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
11773 "__builtin_va_alist"))
11774 gen_unspecified_parameters_die (parm, subr_die);
11775 else
11776 gen_decl_die (parm, subr_die);
11779 /* Decide whether we need an unspecified_parameters DIE at the end.
11780 There are 2 more cases to do this for: 1) the ansi ... declaration -
11781 this is detectable when the end of the arg list is not a
11782 void_type_node 2) an unprototyped function declaration (not a
11783 definition). This just means that we have no info about the
11784 parameters at all. */
11785 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
11786 if (fn_arg_types != NULL)
11788 /* This is the prototyped case, check for.... */
11789 if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
11790 gen_unspecified_parameters_die (decl, subr_die);
11792 else if (DECL_INITIAL (decl) == NULL_TREE)
11793 gen_unspecified_parameters_die (decl, subr_die);
11796 /* Output Dwarf info for all of the stuff within the body of the function
11797 (if it has one - it may be just a declaration). */
11798 outer_scope = DECL_INITIAL (decl);
11800 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
11801 a function. This BLOCK actually represents the outermost binding contour
11802 for the function, i.e. the contour in which the function's formal
11803 parameters and labels get declared. Curiously, it appears that the front
11804 end doesn't actually put the PARM_DECL nodes for the current function onto
11805 the BLOCK_VARS list for this outer scope, but are strung off of the
11806 DECL_ARGUMENTS list for the function instead.
11808 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
11809 the LABEL_DECL nodes for the function however, and we output DWARF info
11810 for those in decls_for_scope. Just within the `outer_scope' there will be
11811 a BLOCK node representing the function's outermost pair of curly braces,
11812 and any blocks used for the base and member initializers of a C++
11813 constructor function. */
11814 if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
11816 /* Emit a DW_TAG_variable DIE for a named return value. */
11817 if (DECL_NAME (DECL_RESULT (decl)))
11818 gen_decl_die (DECL_RESULT (decl), subr_die);
11820 current_function_has_inlines = 0;
11821 decls_for_scope (outer_scope, subr_die, 0);
11823 #if 0 && defined (MIPS_DEBUGGING_INFO)
11824 if (current_function_has_inlines)
11826 add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
11827 if (! comp_unit_has_inlines)
11829 add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
11830 comp_unit_has_inlines = 1;
11833 #endif
11835 /* Add the calling convention attribute if requested. */
11836 add_calling_convention_attribute (subr_die, TREE_TYPE (decl));
11840 /* Generate a DIE to represent a declared data object. */
11842 static void
11843 gen_variable_die (tree decl, dw_die_ref context_die)
11845 tree origin = decl_ultimate_origin (decl);
11846 dw_die_ref var_die = new_die (DW_TAG_variable, context_die, decl);
11848 dw_die_ref old_die = lookup_decl_die (decl);
11849 int declaration = (DECL_EXTERNAL (decl)
11850 /* If DECL is COMDAT and has not actually been
11851 emitted, we cannot take its address; there
11852 might end up being no definition anywhere in
11853 the program. For example, consider the C++
11854 test case:
11856 template <class T>
11857 struct S { static const int i = 7; };
11859 template <class T>
11860 const int S<T>::i;
11862 int f() { return S<int>::i; }
11864 Here, S<int>::i is not DECL_EXTERNAL, but no
11865 definition is required, so the compiler will
11866 not emit a definition. */
11867 || (TREE_CODE (decl) == VAR_DECL
11868 && DECL_COMDAT (decl) && !TREE_ASM_WRITTEN (decl))
11869 || class_or_namespace_scope_p (context_die));
11871 if (origin != NULL)
11872 add_abstract_origin_attribute (var_die, origin);
11874 /* Loop unrolling can create multiple blocks that refer to the same
11875 static variable, so we must test for the DW_AT_declaration flag.
11877 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
11878 copy decls and set the DECL_ABSTRACT flag on them instead of
11879 sharing them.
11881 ??? Duplicated blocks have been rewritten to use .debug_ranges.
11883 ??? The declare_in_namespace support causes us to get two DIEs for one
11884 variable, both of which are declarations. We want to avoid considering
11885 one to be a specification, so we must test that this DIE is not a
11886 declaration. */
11887 else if (old_die && TREE_STATIC (decl) && ! declaration
11888 && get_AT_flag (old_die, DW_AT_declaration) == 1)
11890 /* This is a definition of a C++ class level static. */
11891 add_AT_specification (var_die, old_die);
11892 if (DECL_NAME (decl))
11894 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11895 unsigned file_index = lookup_filename (s.file);
11897 if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
11898 add_AT_unsigned (var_die, DW_AT_decl_file, file_index);
11900 if (get_AT_unsigned (old_die, DW_AT_decl_line)
11901 != (unsigned) s.line)
11903 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
11906 else
11908 add_name_and_src_coords_attributes (var_die, decl);
11909 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
11910 TREE_THIS_VOLATILE (decl), context_die);
11912 if (TREE_PUBLIC (decl))
11913 add_AT_flag (var_die, DW_AT_external, 1);
11915 if (DECL_ARTIFICIAL (decl))
11916 add_AT_flag (var_die, DW_AT_artificial, 1);
11918 if (TREE_PROTECTED (decl))
11919 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
11920 else if (TREE_PRIVATE (decl))
11921 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
11924 if (declaration)
11925 add_AT_flag (var_die, DW_AT_declaration, 1);
11927 if (DECL_ABSTRACT (decl) || declaration)
11928 equate_decl_number_to_die (decl, var_die);
11930 if (! declaration && ! DECL_ABSTRACT (decl))
11932 add_location_or_const_value_attribute (var_die, decl, DW_AT_location);
11933 add_pubname (decl, var_die);
11935 else
11936 tree_add_const_value_attribute (var_die, decl);
11939 /* Generate a DIE to represent a label identifier. */
11941 static void
11942 gen_label_die (tree decl, dw_die_ref context_die)
11944 tree origin = decl_ultimate_origin (decl);
11945 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
11946 rtx insn;
11947 char label[MAX_ARTIFICIAL_LABEL_BYTES];
11949 if (origin != NULL)
11950 add_abstract_origin_attribute (lbl_die, origin);
11951 else
11952 add_name_and_src_coords_attributes (lbl_die, decl);
11954 if (DECL_ABSTRACT (decl))
11955 equate_decl_number_to_die (decl, lbl_die);
11956 else
11958 insn = DECL_RTL_IF_SET (decl);
11960 /* Deleted labels are programmer specified labels which have been
11961 eliminated because of various optimizations. We still emit them
11962 here so that it is possible to put breakpoints on them. */
11963 if (insn
11964 && (LABEL_P (insn)
11965 || ((NOTE_P (insn)
11966 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED_LABEL))))
11968 /* When optimization is enabled (via -O) some parts of the compiler
11969 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
11970 represent source-level labels which were explicitly declared by
11971 the user. This really shouldn't be happening though, so catch
11972 it if it ever does happen. */
11973 gcc_assert (!INSN_DELETED_P (insn));
11975 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
11976 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
11981 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
11982 attributes to the DIE for a block STMT, to describe where the inlined
11983 function was called from. This is similar to add_src_coords_attributes. */
11985 static inline void
11986 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
11988 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
11989 unsigned file_index = lookup_filename (s.file);
11991 add_AT_unsigned (die, DW_AT_call_file, file_index);
11992 add_AT_unsigned (die, DW_AT_call_line, s.line);
11995 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
11996 Add low_pc and high_pc attributes to the DIE for a block STMT. */
11998 static inline void
11999 add_high_low_attributes (tree stmt, dw_die_ref die)
12001 char label[MAX_ARTIFICIAL_LABEL_BYTES];
12003 if (BLOCK_FRAGMENT_CHAIN (stmt))
12005 tree chain;
12007 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
12009 chain = BLOCK_FRAGMENT_CHAIN (stmt);
12012 add_ranges (chain);
12013 chain = BLOCK_FRAGMENT_CHAIN (chain);
12015 while (chain);
12016 add_ranges (NULL);
12018 else
12020 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
12021 BLOCK_NUMBER (stmt));
12022 add_AT_lbl_id (die, DW_AT_low_pc, label);
12023 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
12024 BLOCK_NUMBER (stmt));
12025 add_AT_lbl_id (die, DW_AT_high_pc, label);
12029 /* Generate a DIE for a lexical block. */
12031 static void
12032 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
12034 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
12036 if (! BLOCK_ABSTRACT (stmt))
12037 add_high_low_attributes (stmt, stmt_die);
12039 decls_for_scope (stmt, stmt_die, depth);
12042 /* Generate a DIE for an inlined subprogram. */
12044 static void
12045 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
12047 tree decl = block_ultimate_origin (stmt);
12049 /* Emit info for the abstract instance first, if we haven't yet. We
12050 must emit this even if the block is abstract, otherwise when we
12051 emit the block below (or elsewhere), we may end up trying to emit
12052 a die whose origin die hasn't been emitted, and crashing. */
12053 dwarf2out_abstract_function (decl);
12055 if (! BLOCK_ABSTRACT (stmt))
12057 dw_die_ref subr_die
12058 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
12060 add_abstract_origin_attribute (subr_die, decl);
12061 add_high_low_attributes (stmt, subr_die);
12062 add_call_src_coords_attributes (stmt, subr_die);
12064 decls_for_scope (stmt, subr_die, depth);
12065 current_function_has_inlines = 1;
12067 else
12068 /* We may get here if we're the outer block of function A that was
12069 inlined into function B that was inlined into function C. When
12070 generating debugging info for C, dwarf2out_abstract_function(B)
12071 would mark all inlined blocks as abstract, including this one.
12072 So, we wouldn't (and shouldn't) expect labels to be generated
12073 for this one. Instead, just emit debugging info for
12074 declarations within the block. This is particularly important
12075 in the case of initializers of arguments passed from B to us:
12076 if they're statement expressions containing declarations, we
12077 wouldn't generate dies for their abstract variables, and then,
12078 when generating dies for the real variables, we'd die (pun
12079 intended :-) */
12080 gen_lexical_block_die (stmt, context_die, depth);
12083 /* Generate a DIE for a field in a record, or structure. */
12085 static void
12086 gen_field_die (tree decl, dw_die_ref context_die)
12088 dw_die_ref decl_die;
12090 if (TREE_TYPE (decl) == error_mark_node)
12091 return;
12093 decl_die = new_die (DW_TAG_member, context_die, decl);
12094 add_name_and_src_coords_attributes (decl_die, decl);
12095 add_type_attribute (decl_die, member_declared_type (decl),
12096 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
12097 context_die);
12099 if (DECL_BIT_FIELD_TYPE (decl))
12101 add_byte_size_attribute (decl_die, decl);
12102 add_bit_size_attribute (decl_die, decl);
12103 add_bit_offset_attribute (decl_die, decl);
12106 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
12107 add_data_member_location_attribute (decl_die, decl);
12109 if (DECL_ARTIFICIAL (decl))
12110 add_AT_flag (decl_die, DW_AT_artificial, 1);
12112 if (TREE_PROTECTED (decl))
12113 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
12114 else if (TREE_PRIVATE (decl))
12115 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
12117 /* Equate decl number to die, so that we can look up this decl later on. */
12118 equate_decl_number_to_die (decl, decl_die);
12121 #if 0
12122 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
12123 Use modified_type_die instead.
12124 We keep this code here just in case these types of DIEs may be needed to
12125 represent certain things in other languages (e.g. Pascal) someday. */
12127 static void
12128 gen_pointer_type_die (tree type, dw_die_ref context_die)
12130 dw_die_ref ptr_die
12131 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
12133 equate_type_number_to_die (type, ptr_die);
12134 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
12135 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
12138 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
12139 Use modified_type_die instead.
12140 We keep this code here just in case these types of DIEs may be needed to
12141 represent certain things in other languages (e.g. Pascal) someday. */
12143 static void
12144 gen_reference_type_die (tree type, dw_die_ref context_die)
12146 dw_die_ref ref_die
12147 = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
12149 equate_type_number_to_die (type, ref_die);
12150 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
12151 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
12153 #endif
12155 /* Generate a DIE for a pointer to a member type. */
12157 static void
12158 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
12160 dw_die_ref ptr_die
12161 = new_die (DW_TAG_ptr_to_member_type,
12162 scope_die_for (type, context_die), type);
12164 equate_type_number_to_die (type, ptr_die);
12165 add_AT_die_ref (ptr_die, DW_AT_containing_type,
12166 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
12167 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
12170 /* Generate the DIE for the compilation unit. */
12172 static dw_die_ref
12173 gen_compile_unit_die (const char *filename)
12175 dw_die_ref die;
12176 char producer[250];
12177 const char *language_string = lang_hooks.name;
12178 int language;
12180 die = new_die (DW_TAG_compile_unit, NULL, NULL);
12182 if (filename)
12184 add_name_attribute (die, filename);
12185 /* Don't add cwd for <built-in>. */
12186 if (filename[0] != DIR_SEPARATOR && filename[0] != '<')
12187 add_comp_dir_attribute (die);
12190 sprintf (producer, "%s %s", language_string, version_string);
12192 #ifdef MIPS_DEBUGGING_INFO
12193 /* The MIPS/SGI compilers place the 'cc' command line options in the producer
12194 string. The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
12195 not appear in the producer string, the debugger reaches the conclusion
12196 that the object file is stripped and has no debugging information.
12197 To get the MIPS/SGI debugger to believe that there is debugging
12198 information in the object file, we add a -g to the producer string. */
12199 if (debug_info_level > DINFO_LEVEL_TERSE)
12200 strcat (producer, " -g");
12201 #endif
12203 add_AT_string (die, DW_AT_producer, producer);
12205 if (strcmp (language_string, "GNU C++") == 0)
12206 language = DW_LANG_C_plus_plus;
12207 else if (strcmp (language_string, "GNU Ada") == 0)
12208 language = DW_LANG_Ada95;
12209 else if (strcmp (language_string, "GNU F77") == 0)
12210 language = DW_LANG_Fortran77;
12211 else if (strcmp (language_string, "GNU F95") == 0)
12212 language = DW_LANG_Fortran95;
12213 else if (strcmp (language_string, "GNU Pascal") == 0)
12214 language = DW_LANG_Pascal83;
12215 else if (strcmp (language_string, "GNU Java") == 0)
12216 language = DW_LANG_Java;
12217 else if (strcmp (language_string, "GNU Objective-C") == 0)
12218 language = DW_LANG_ObjC;
12219 else if (strcmp (language_string, "GNU Objective-C++") == 0)
12220 language = DW_LANG_ObjC_plus_plus;
12221 else
12222 language = DW_LANG_C89;
12224 add_AT_unsigned (die, DW_AT_language, language);
12225 return die;
12228 /* Generate the DIE for a base class. */
12230 static void
12231 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
12233 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
12235 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
12236 add_data_member_location_attribute (die, binfo);
12238 if (BINFO_VIRTUAL_P (binfo))
12239 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
12241 if (access == access_public_node)
12242 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
12243 else if (access == access_protected_node)
12244 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
12247 /* Generate a DIE for a class member. */
12249 static void
12250 gen_member_die (tree type, dw_die_ref context_die)
12252 tree member;
12253 tree binfo = TYPE_BINFO (type);
12254 dw_die_ref child;
12256 /* If this is not an incomplete type, output descriptions of each of its
12257 members. Note that as we output the DIEs necessary to represent the
12258 members of this record or union type, we will also be trying to output
12259 DIEs to represent the *types* of those members. However the `type'
12260 function (above) will specifically avoid generating type DIEs for member
12261 types *within* the list of member DIEs for this (containing) type except
12262 for those types (of members) which are explicitly marked as also being
12263 members of this (containing) type themselves. The g++ front- end can
12264 force any given type to be treated as a member of some other (containing)
12265 type by setting the TYPE_CONTEXT of the given (member) type to point to
12266 the TREE node representing the appropriate (containing) type. */
12268 /* First output info about the base classes. */
12269 if (binfo)
12271 VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
12272 int i;
12273 tree base;
12275 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
12276 gen_inheritance_die (base,
12277 (accesses ? VEC_index (tree, accesses, i)
12278 : access_public_node), context_die);
12281 /* Now output info about the data members and type members. */
12282 for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
12284 /* If we thought we were generating minimal debug info for TYPE
12285 and then changed our minds, some of the member declarations
12286 may have already been defined. Don't define them again, but
12287 do put them in the right order. */
12289 child = lookup_decl_die (member);
12290 if (child)
12291 splice_child_die (context_die, child);
12292 else
12293 gen_decl_die (member, context_die);
12296 /* Now output info about the function members (if any). */
12297 for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
12299 /* Don't include clones in the member list. */
12300 if (DECL_ABSTRACT_ORIGIN (member))
12301 continue;
12303 child = lookup_decl_die (member);
12304 if (child)
12305 splice_child_die (context_die, child);
12306 else
12307 gen_decl_die (member, context_die);
12311 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
12312 is set, we pretend that the type was never defined, so we only get the
12313 member DIEs needed by later specification DIEs. */
12315 static void
12316 gen_struct_or_union_type_die (tree type, dw_die_ref context_die)
12318 dw_die_ref type_die = lookup_type_die (type);
12319 dw_die_ref scope_die = 0;
12320 int nested = 0;
12321 int complete = (TYPE_SIZE (type)
12322 && (! TYPE_STUB_DECL (type)
12323 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
12324 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
12326 if (type_die && ! complete)
12327 return;
12329 if (TYPE_CONTEXT (type) != NULL_TREE
12330 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
12331 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
12332 nested = 1;
12334 scope_die = scope_die_for (type, context_die);
12336 if (! type_die || (nested && scope_die == comp_unit_die))
12337 /* First occurrence of type or toplevel definition of nested class. */
12339 dw_die_ref old_die = type_die;
12341 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
12342 ? DW_TAG_structure_type : DW_TAG_union_type,
12343 scope_die, type);
12344 equate_type_number_to_die (type, type_die);
12345 if (old_die)
12346 add_AT_specification (type_die, old_die);
12347 else
12348 add_name_attribute (type_die, type_tag (type));
12350 else
12351 remove_AT (type_die, DW_AT_declaration);
12353 /* If this type has been completed, then give it a byte_size attribute and
12354 then give a list of members. */
12355 if (complete && !ns_decl)
12357 /* Prevent infinite recursion in cases where the type of some member of
12358 this type is expressed in terms of this type itself. */
12359 TREE_ASM_WRITTEN (type) = 1;
12360 add_byte_size_attribute (type_die, type);
12361 if (TYPE_STUB_DECL (type) != NULL_TREE)
12362 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
12364 /* If the first reference to this type was as the return type of an
12365 inline function, then it may not have a parent. Fix this now. */
12366 if (type_die->die_parent == NULL)
12367 add_child_die (scope_die, type_die);
12369 push_decl_scope (type);
12370 gen_member_die (type, type_die);
12371 pop_decl_scope ();
12373 /* GNU extension: Record what type our vtable lives in. */
12374 if (TYPE_VFIELD (type))
12376 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
12378 gen_type_die (vtype, context_die);
12379 add_AT_die_ref (type_die, DW_AT_containing_type,
12380 lookup_type_die (vtype));
12383 else
12385 add_AT_flag (type_die, DW_AT_declaration, 1);
12387 /* We don't need to do this for function-local types. */
12388 if (TYPE_STUB_DECL (type)
12389 && ! decl_function_context (TYPE_STUB_DECL (type)))
12390 VEC_safe_push (tree, gc, incomplete_types, type);
12394 /* Generate a DIE for a subroutine _type_. */
12396 static void
12397 gen_subroutine_type_die (tree type, dw_die_ref context_die)
12399 tree return_type = TREE_TYPE (type);
12400 dw_die_ref subr_die
12401 = new_die (DW_TAG_subroutine_type,
12402 scope_die_for (type, context_die), type);
12404 equate_type_number_to_die (type, subr_die);
12405 add_prototyped_attribute (subr_die, type);
12406 add_type_attribute (subr_die, return_type, 0, 0, context_die);
12407 gen_formal_types_die (type, subr_die);
12410 /* Generate a DIE for a type definition. */
12412 static void
12413 gen_typedef_die (tree decl, dw_die_ref context_die)
12415 dw_die_ref type_die;
12416 tree origin;
12418 if (TREE_ASM_WRITTEN (decl))
12419 return;
12421 TREE_ASM_WRITTEN (decl) = 1;
12422 type_die = new_die (DW_TAG_typedef, context_die, decl);
12423 origin = decl_ultimate_origin (decl);
12424 if (origin != NULL)
12425 add_abstract_origin_attribute (type_die, origin);
12426 else
12428 tree type;
12430 add_name_and_src_coords_attributes (type_die, decl);
12431 if (DECL_ORIGINAL_TYPE (decl))
12433 type = DECL_ORIGINAL_TYPE (decl);
12435 gcc_assert (type != TREE_TYPE (decl));
12436 equate_type_number_to_die (TREE_TYPE (decl), type_die);
12438 else
12439 type = TREE_TYPE (decl);
12441 add_type_attribute (type_die, type, TREE_READONLY (decl),
12442 TREE_THIS_VOLATILE (decl), context_die);
12445 if (DECL_ABSTRACT (decl))
12446 equate_decl_number_to_die (decl, type_die);
12449 /* Generate a type description DIE. */
12451 static void
12452 gen_type_die (tree type, dw_die_ref context_die)
12454 int need_pop;
12456 if (type == NULL_TREE || type == error_mark_node)
12457 return;
12459 if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12460 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
12462 if (TREE_ASM_WRITTEN (type))
12463 return;
12465 /* Prevent broken recursion; we can't hand off to the same type. */
12466 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
12468 TREE_ASM_WRITTEN (type) = 1;
12469 gen_decl_die (TYPE_NAME (type), context_die);
12470 return;
12473 /* We are going to output a DIE to represent the unqualified version
12474 of this type (i.e. without any const or volatile qualifiers) so
12475 get the main variant (i.e. the unqualified version) of this type
12476 now. (Vectors are special because the debugging info is in the
12477 cloned type itself). */
12478 if (TREE_CODE (type) != VECTOR_TYPE)
12479 type = type_main_variant (type);
12481 if (TREE_ASM_WRITTEN (type))
12482 return;
12484 switch (TREE_CODE (type))
12486 case ERROR_MARK:
12487 break;
12489 case POINTER_TYPE:
12490 case REFERENCE_TYPE:
12491 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
12492 ensures that the gen_type_die recursion will terminate even if the
12493 type is recursive. Recursive types are possible in Ada. */
12494 /* ??? We could perhaps do this for all types before the switch
12495 statement. */
12496 TREE_ASM_WRITTEN (type) = 1;
12498 /* For these types, all that is required is that we output a DIE (or a
12499 set of DIEs) to represent the "basis" type. */
12500 gen_type_die (TREE_TYPE (type), context_die);
12501 break;
12503 case OFFSET_TYPE:
12504 /* This code is used for C++ pointer-to-data-member types.
12505 Output a description of the relevant class type. */
12506 gen_type_die (TYPE_OFFSET_BASETYPE (type), context_die);
12508 /* Output a description of the type of the object pointed to. */
12509 gen_type_die (TREE_TYPE (type), context_die);
12511 /* Now output a DIE to represent this pointer-to-data-member type
12512 itself. */
12513 gen_ptr_to_mbr_type_die (type, context_die);
12514 break;
12516 case FUNCTION_TYPE:
12517 /* Force out return type (in case it wasn't forced out already). */
12518 gen_type_die (TREE_TYPE (type), context_die);
12519 gen_subroutine_type_die (type, context_die);
12520 break;
12522 case METHOD_TYPE:
12523 /* Force out return type (in case it wasn't forced out already). */
12524 gen_type_die (TREE_TYPE (type), context_die);
12525 gen_subroutine_type_die (type, context_die);
12526 break;
12528 case ARRAY_TYPE:
12529 gen_array_type_die (type, context_die);
12530 break;
12532 case VECTOR_TYPE:
12533 gen_array_type_die (type, context_die);
12534 break;
12536 case ENUMERAL_TYPE:
12537 case RECORD_TYPE:
12538 case UNION_TYPE:
12539 case QUAL_UNION_TYPE:
12540 /* If this is a nested type whose containing class hasn't been written
12541 out yet, writing it out will cover this one, too. This does not apply
12542 to instantiations of member class templates; they need to be added to
12543 the containing class as they are generated. FIXME: This hurts the
12544 idea of combining type decls from multiple TUs, since we can't predict
12545 what set of template instantiations we'll get. */
12546 if (TYPE_CONTEXT (type)
12547 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
12548 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
12550 gen_type_die (TYPE_CONTEXT (type), context_die);
12552 if (TREE_ASM_WRITTEN (type))
12553 return;
12555 /* If that failed, attach ourselves to the stub. */
12556 push_decl_scope (TYPE_CONTEXT (type));
12557 context_die = lookup_type_die (TYPE_CONTEXT (type));
12558 need_pop = 1;
12560 else
12562 declare_in_namespace (type, context_die);
12563 need_pop = 0;
12566 if (TREE_CODE (type) == ENUMERAL_TYPE)
12567 gen_enumeration_type_die (type, context_die);
12568 else
12569 gen_struct_or_union_type_die (type, context_die);
12571 if (need_pop)
12572 pop_decl_scope ();
12574 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
12575 it up if it is ever completed. gen_*_type_die will set it for us
12576 when appropriate. */
12577 return;
12579 case VOID_TYPE:
12580 case INTEGER_TYPE:
12581 case REAL_TYPE:
12582 case COMPLEX_TYPE:
12583 case BOOLEAN_TYPE:
12584 /* No DIEs needed for fundamental types. */
12585 break;
12587 case LANG_TYPE:
12588 /* No Dwarf representation currently defined. */
12589 break;
12591 default:
12592 gcc_unreachable ();
12595 TREE_ASM_WRITTEN (type) = 1;
12598 /* Generate a DIE for a tagged type instantiation. */
12600 static void
12601 gen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
12603 if (type == NULL_TREE || type == error_mark_node)
12604 return;
12606 /* We are going to output a DIE to represent the unqualified version of
12607 this type (i.e. without any const or volatile qualifiers) so make sure
12608 that we have the main variant (i.e. the unqualified version) of this
12609 type now. */
12610 gcc_assert (type == type_main_variant (type));
12612 /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
12613 an instance of an unresolved type. */
12615 switch (TREE_CODE (type))
12617 case ERROR_MARK:
12618 break;
12620 case ENUMERAL_TYPE:
12621 gen_inlined_enumeration_type_die (type, context_die);
12622 break;
12624 case RECORD_TYPE:
12625 gen_inlined_structure_type_die (type, context_die);
12626 break;
12628 case UNION_TYPE:
12629 case QUAL_UNION_TYPE:
12630 gen_inlined_union_type_die (type, context_die);
12631 break;
12633 default:
12634 gcc_unreachable ();
12638 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
12639 things which are local to the given block. */
12641 static void
12642 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
12644 int must_output_die = 0;
12645 tree origin;
12646 tree decl;
12647 enum tree_code origin_code;
12649 /* Ignore blocks that are NULL. */
12650 if (stmt == NULL_TREE)
12651 return;
12653 /* If the block is one fragment of a non-contiguous block, do not
12654 process the variables, since they will have been done by the
12655 origin block. Do process subblocks. */
12656 if (BLOCK_FRAGMENT_ORIGIN (stmt))
12658 tree sub;
12660 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
12661 gen_block_die (sub, context_die, depth + 1);
12663 return;
12666 /* Determine the "ultimate origin" of this block. This block may be an
12667 inlined instance of an inlined instance of inline function, so we have
12668 to trace all of the way back through the origin chain to find out what
12669 sort of node actually served as the original seed for the creation of
12670 the current block. */
12671 origin = block_ultimate_origin (stmt);
12672 origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
12674 /* Determine if we need to output any Dwarf DIEs at all to represent this
12675 block. */
12676 if (origin_code == FUNCTION_DECL)
12677 /* The outer scopes for inlinings *must* always be represented. We
12678 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
12679 must_output_die = 1;
12680 else
12682 /* In the case where the current block represents an inlining of the
12683 "body block" of an inline function, we must *NOT* output any DIE for
12684 this block because we have already output a DIE to represent the whole
12685 inlined function scope and the "body block" of any function doesn't
12686 really represent a different scope according to ANSI C rules. So we
12687 check here to make sure that this block does not represent a "body
12688 block inlining" before trying to set the MUST_OUTPUT_DIE flag. */
12689 if (! is_body_block (origin ? origin : stmt))
12691 /* Determine if this block directly contains any "significant"
12692 local declarations which we will need to output DIEs for. */
12693 if (debug_info_level > DINFO_LEVEL_TERSE)
12694 /* We are not in terse mode so *any* local declaration counts
12695 as being a "significant" one. */
12696 must_output_die = (BLOCK_VARS (stmt) != NULL
12697 && (TREE_USED (stmt)
12698 || TREE_ASM_WRITTEN (stmt)
12699 || BLOCK_ABSTRACT (stmt)));
12700 else
12701 /* We are in terse mode, so only local (nested) function
12702 definitions count as "significant" local declarations. */
12703 for (decl = BLOCK_VARS (stmt);
12704 decl != NULL; decl = TREE_CHAIN (decl))
12705 if (TREE_CODE (decl) == FUNCTION_DECL
12706 && DECL_INITIAL (decl))
12708 must_output_die = 1;
12709 break;
12714 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
12715 DIE for any block which contains no significant local declarations at
12716 all. Rather, in such cases we just call `decls_for_scope' so that any
12717 needed Dwarf info for any sub-blocks will get properly generated. Note
12718 that in terse mode, our definition of what constitutes a "significant"
12719 local declaration gets restricted to include only inlined function
12720 instances and local (nested) function definitions. */
12721 if (must_output_die)
12723 if (origin_code == FUNCTION_DECL)
12724 gen_inlined_subroutine_die (stmt, context_die, depth);
12725 else
12726 gen_lexical_block_die (stmt, context_die, depth);
12728 else
12729 decls_for_scope (stmt, context_die, depth);
12732 /* Generate all of the decls declared within a given scope and (recursively)
12733 all of its sub-blocks. */
12735 static void
12736 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
12738 tree decl;
12739 tree subblocks;
12741 /* Ignore NULL blocks. */
12742 if (stmt == NULL_TREE)
12743 return;
12745 if (TREE_USED (stmt))
12747 /* Output the DIEs to represent all of the data objects and typedefs
12748 declared directly within this block but not within any nested
12749 sub-blocks. Also, nested function and tag DIEs have been
12750 generated with a parent of NULL; fix that up now. */
12751 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
12753 dw_die_ref die;
12755 if (TREE_CODE (decl) == FUNCTION_DECL)
12756 die = lookup_decl_die (decl);
12757 else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
12758 die = lookup_type_die (TREE_TYPE (decl));
12759 else
12760 die = NULL;
12762 if (die != NULL && die->die_parent == NULL)
12763 add_child_die (context_die, die);
12764 /* Do not produce debug information for static variables since
12765 these might be optimized out. We are called for these later
12766 in cgraph_varpool_analyze_pending_decls. */
12767 if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
12769 else
12770 gen_decl_die (decl, context_die);
12774 /* If we're at -g1, we're not interested in subblocks. */
12775 if (debug_info_level <= DINFO_LEVEL_TERSE)
12776 return;
12778 /* Output the DIEs to represent all sub-blocks (and the items declared
12779 therein) of this block. */
12780 for (subblocks = BLOCK_SUBBLOCKS (stmt);
12781 subblocks != NULL;
12782 subblocks = BLOCK_CHAIN (subblocks))
12783 gen_block_die (subblocks, context_die, depth + 1);
12786 /* Is this a typedef we can avoid emitting? */
12788 static inline int
12789 is_redundant_typedef (tree decl)
12791 if (TYPE_DECL_IS_STUB (decl))
12792 return 1;
12794 if (DECL_ARTIFICIAL (decl)
12795 && DECL_CONTEXT (decl)
12796 && is_tagged_type (DECL_CONTEXT (decl))
12797 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
12798 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
12799 /* Also ignore the artificial member typedef for the class name. */
12800 return 1;
12802 return 0;
12805 /* Returns the DIE for decl. A DIE will always be returned. */
12807 static dw_die_ref
12808 force_decl_die (tree decl)
12810 dw_die_ref decl_die;
12811 unsigned saved_external_flag;
12812 tree save_fn = NULL_TREE;
12813 decl_die = lookup_decl_die (decl);
12814 if (!decl_die)
12816 dw_die_ref context_die;
12817 tree decl_context = DECL_CONTEXT (decl);
12818 if (decl_context)
12820 /* Find die that represents this context. */
12821 if (TYPE_P (decl_context))
12822 context_die = force_type_die (decl_context);
12823 else
12824 context_die = force_decl_die (decl_context);
12826 else
12827 context_die = comp_unit_die;
12829 decl_die = lookup_decl_die (decl);
12830 if (decl_die)
12831 return decl_die;
12833 switch (TREE_CODE (decl))
12835 case FUNCTION_DECL:
12836 /* Clear current_function_decl, so that gen_subprogram_die thinks
12837 that this is a declaration. At this point, we just want to force
12838 declaration die. */
12839 save_fn = current_function_decl;
12840 current_function_decl = NULL_TREE;
12841 gen_subprogram_die (decl, context_die);
12842 current_function_decl = save_fn;
12843 break;
12845 case VAR_DECL:
12846 /* Set external flag to force declaration die. Restore it after
12847 gen_decl_die() call. */
12848 saved_external_flag = DECL_EXTERNAL (decl);
12849 DECL_EXTERNAL (decl) = 1;
12850 gen_decl_die (decl, context_die);
12851 DECL_EXTERNAL (decl) = saved_external_flag;
12852 break;
12854 case NAMESPACE_DECL:
12855 dwarf2out_decl (decl);
12856 break;
12858 default:
12859 gcc_unreachable ();
12862 /* We should be able to find the DIE now. */
12863 if (!decl_die)
12864 decl_die = lookup_decl_die (decl);
12865 gcc_assert (decl_die);
12868 return decl_die;
12871 /* Returns the DIE for TYPE. A DIE is always returned. */
12873 static dw_die_ref
12874 force_type_die (tree type)
12876 dw_die_ref type_die;
12878 type_die = lookup_type_die (type);
12879 if (!type_die)
12881 dw_die_ref context_die;
12882 if (TYPE_CONTEXT (type))
12884 if (TYPE_P (TYPE_CONTEXT (type)))
12885 context_die = force_type_die (TYPE_CONTEXT (type));
12886 else
12887 context_die = force_decl_die (TYPE_CONTEXT (type));
12889 else
12890 context_die = comp_unit_die;
12892 type_die = lookup_type_die (type);
12893 if (type_die)
12894 return type_die;
12895 gen_type_die (type, context_die);
12896 type_die = lookup_type_die (type);
12897 gcc_assert (type_die);
12899 return type_die;
12902 /* Force out any required namespaces to be able to output DECL,
12903 and return the new context_die for it, if it's changed. */
12905 static dw_die_ref
12906 setup_namespace_context (tree thing, dw_die_ref context_die)
12908 tree context = (DECL_P (thing)
12909 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
12910 if (context && TREE_CODE (context) == NAMESPACE_DECL)
12911 /* Force out the namespace. */
12912 context_die = force_decl_die (context);
12914 return context_die;
12917 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
12918 type) within its namespace, if appropriate.
12920 For compatibility with older debuggers, namespace DIEs only contain
12921 declarations; all definitions are emitted at CU scope. */
12923 static void
12924 declare_in_namespace (tree thing, dw_die_ref context_die)
12926 dw_die_ref ns_context;
12928 if (debug_info_level <= DINFO_LEVEL_TERSE)
12929 return;
12931 /* If this decl is from an inlined function, then don't try to emit it in its
12932 namespace, as we will get confused. It would have already been emitted
12933 when the abstract instance of the inline function was emitted anyways. */
12934 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
12935 return;
12937 ns_context = setup_namespace_context (thing, context_die);
12939 if (ns_context != context_die)
12941 if (DECL_P (thing))
12942 gen_decl_die (thing, ns_context);
12943 else
12944 gen_type_die (thing, ns_context);
12948 /* Generate a DIE for a namespace or namespace alias. */
12950 static void
12951 gen_namespace_die (tree decl)
12953 dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
12955 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
12956 they are an alias of. */
12957 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
12959 /* Output a real namespace. */
12960 dw_die_ref namespace_die
12961 = new_die (DW_TAG_namespace, context_die, decl);
12962 add_name_and_src_coords_attributes (namespace_die, decl);
12963 equate_decl_number_to_die (decl, namespace_die);
12965 else
12967 /* Output a namespace alias. */
12969 /* Force out the namespace we are an alias of, if necessary. */
12970 dw_die_ref origin_die
12971 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
12973 /* Now create the namespace alias DIE. */
12974 dw_die_ref namespace_die
12975 = new_die (DW_TAG_imported_declaration, context_die, decl);
12976 add_name_and_src_coords_attributes (namespace_die, decl);
12977 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
12978 equate_decl_number_to_die (decl, namespace_die);
12982 /* Generate Dwarf debug information for a decl described by DECL. */
12984 static void
12985 gen_decl_die (tree decl, dw_die_ref context_die)
12987 tree origin;
12989 if (DECL_P (decl) && DECL_IGNORED_P (decl))
12990 return;
12992 switch (TREE_CODE (decl))
12994 case ERROR_MARK:
12995 break;
12997 case CONST_DECL:
12998 /* The individual enumerators of an enum type get output when we output
12999 the Dwarf representation of the relevant enum type itself. */
13000 break;
13002 case FUNCTION_DECL:
13003 /* Don't output any DIEs to represent mere function declarations,
13004 unless they are class members or explicit block externs. */
13005 if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
13006 && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
13007 break;
13009 #if 0
13010 /* FIXME */
13011 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
13012 on local redeclarations of global functions. That seems broken. */
13013 if (current_function_decl != decl)
13014 /* This is only a declaration. */;
13015 #endif
13017 /* If we're emitting a clone, emit info for the abstract instance. */
13018 if (DECL_ORIGIN (decl) != decl)
13019 dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
13021 /* If we're emitting an out-of-line copy of an inline function,
13022 emit info for the abstract instance and set up to refer to it. */
13023 else if (cgraph_function_possibly_inlined_p (decl)
13024 && ! DECL_ABSTRACT (decl)
13025 && ! class_or_namespace_scope_p (context_die)
13026 /* dwarf2out_abstract_function won't emit a die if this is just
13027 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
13028 that case, because that works only if we have a die. */
13029 && DECL_INITIAL (decl) != NULL_TREE)
13031 dwarf2out_abstract_function (decl);
13032 set_decl_origin_self (decl);
13035 /* Otherwise we're emitting the primary DIE for this decl. */
13036 else if (debug_info_level > DINFO_LEVEL_TERSE)
13038 /* Before we describe the FUNCTION_DECL itself, make sure that we
13039 have described its return type. */
13040 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
13042 /* And its virtual context. */
13043 if (DECL_VINDEX (decl) != NULL_TREE)
13044 gen_type_die (DECL_CONTEXT (decl), context_die);
13046 /* And its containing type. */
13047 origin = decl_class_context (decl);
13048 if (origin != NULL_TREE)
13049 gen_type_die_for_member (origin, decl, context_die);
13051 /* And its containing namespace. */
13052 declare_in_namespace (decl, context_die);
13055 /* Now output a DIE to represent the function itself. */
13056 gen_subprogram_die (decl, context_die);
13057 break;
13059 case TYPE_DECL:
13060 /* If we are in terse mode, don't generate any DIEs to represent any
13061 actual typedefs. */
13062 if (debug_info_level <= DINFO_LEVEL_TERSE)
13063 break;
13065 /* In the special case of a TYPE_DECL node representing the declaration
13066 of some type tag, if the given TYPE_DECL is marked as having been
13067 instantiated from some other (original) TYPE_DECL node (e.g. one which
13068 was generated within the original definition of an inline function) we
13069 have to generate a special (abbreviated) DW_TAG_structure_type,
13070 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. */
13071 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
13073 gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
13074 break;
13077 if (is_redundant_typedef (decl))
13078 gen_type_die (TREE_TYPE (decl), context_die);
13079 else
13080 /* Output a DIE to represent the typedef itself. */
13081 gen_typedef_die (decl, context_die);
13082 break;
13084 case LABEL_DECL:
13085 if (debug_info_level >= DINFO_LEVEL_NORMAL)
13086 gen_label_die (decl, context_die);
13087 break;
13089 case VAR_DECL:
13090 case RESULT_DECL:
13091 /* If we are in terse mode, don't generate any DIEs to represent any
13092 variable declarations or definitions. */
13093 if (debug_info_level <= DINFO_LEVEL_TERSE)
13094 break;
13096 /* Output any DIEs that are needed to specify the type of this data
13097 object. */
13098 gen_type_die (TREE_TYPE (decl), context_die);
13100 /* And its containing type. */
13101 origin = decl_class_context (decl);
13102 if (origin != NULL_TREE)
13103 gen_type_die_for_member (origin, decl, context_die);
13105 /* And its containing namespace. */
13106 declare_in_namespace (decl, context_die);
13108 /* Now output the DIE to represent the data object itself. This gets
13109 complicated because of the possibility that the VAR_DECL really
13110 represents an inlined instance of a formal parameter for an inline
13111 function. */
13112 origin = decl_ultimate_origin (decl);
13113 if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
13114 gen_formal_parameter_die (decl, context_die);
13115 else
13116 gen_variable_die (decl, context_die);
13117 break;
13119 case FIELD_DECL:
13120 /* Ignore the nameless fields that are used to skip bits but handle C++
13121 anonymous unions and structs. */
13122 if (DECL_NAME (decl) != NULL_TREE
13123 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
13124 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
13126 gen_type_die (member_declared_type (decl), context_die);
13127 gen_field_die (decl, context_die);
13129 break;
13131 case PARM_DECL:
13132 gen_type_die (TREE_TYPE (decl), context_die);
13133 gen_formal_parameter_die (decl, context_die);
13134 break;
13136 case NAMESPACE_DECL:
13137 gen_namespace_die (decl);
13138 break;
13140 default:
13141 /* Probably some frontend-internal decl. Assume we don't care. */
13142 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
13143 break;
13147 /* Output debug information for global decl DECL. Called from toplev.c after
13148 compilation proper has finished. */
13150 static void
13151 dwarf2out_global_decl (tree decl)
13153 /* Output DWARF2 information for file-scope tentative data object
13154 declarations, file-scope (extern) function declarations (which had no
13155 corresponding body) and file-scope tagged type declarations and
13156 definitions which have not yet been forced out. */
13157 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
13158 dwarf2out_decl (decl);
13161 /* Output debug information for type decl DECL. Called from toplev.c
13162 and from language front ends (to record built-in types). */
13163 static void
13164 dwarf2out_type_decl (tree decl, int local)
13166 if (!local)
13167 dwarf2out_decl (decl);
13170 /* Output debug information for imported module or decl. */
13172 static void
13173 dwarf2out_imported_module_or_decl (tree decl, tree context)
13175 dw_die_ref imported_die, at_import_die;
13176 dw_die_ref scope_die;
13177 unsigned file_index;
13178 expanded_location xloc;
13180 if (debug_info_level <= DINFO_LEVEL_TERSE)
13181 return;
13183 gcc_assert (decl);
13185 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
13186 We need decl DIE for reference and scope die. First, get DIE for the decl
13187 itself. */
13189 /* Get the scope die for decl context. Use comp_unit_die for global module
13190 or decl. If die is not found for non globals, force new die. */
13191 if (!context)
13192 scope_die = comp_unit_die;
13193 else if (TYPE_P (context))
13194 scope_die = force_type_die (context);
13195 else
13196 scope_die = force_decl_die (context);
13198 /* For TYPE_DECL or CONST_DECL, lookup TREE_TYPE. */
13199 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
13200 at_import_die = force_type_die (TREE_TYPE (decl));
13201 else
13203 at_import_die = lookup_decl_die (decl);
13204 if (!at_import_die)
13206 /* If we're trying to avoid duplicate debug info, we may not have
13207 emitted the member decl for this field. Emit it now. */
13208 if (TREE_CODE (decl) == FIELD_DECL)
13210 tree type = DECL_CONTEXT (decl);
13211 dw_die_ref type_context_die;
13213 if (TYPE_CONTEXT (type))
13214 if (TYPE_P (TYPE_CONTEXT (type)))
13215 type_context_die = force_type_die (TYPE_CONTEXT (type));
13216 else
13217 type_context_die = force_decl_die (TYPE_CONTEXT (type));
13218 else
13219 type_context_die = comp_unit_die;
13220 gen_type_die_for_member (type, decl, type_context_die);
13222 at_import_die = force_decl_die (decl);
13226 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
13227 if (TREE_CODE (decl) == NAMESPACE_DECL)
13228 imported_die = new_die (DW_TAG_imported_module, scope_die, context);
13229 else
13230 imported_die = new_die (DW_TAG_imported_declaration, scope_die, context);
13232 xloc = expand_location (input_location);
13233 file_index = lookup_filename (xloc.file);
13234 add_AT_unsigned (imported_die, DW_AT_decl_file, file_index);
13235 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
13236 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
13239 /* Write the debugging output for DECL. */
13241 void
13242 dwarf2out_decl (tree decl)
13244 dw_die_ref context_die = comp_unit_die;
13246 switch (TREE_CODE (decl))
13248 case ERROR_MARK:
13249 return;
13251 case FUNCTION_DECL:
13252 /* What we would really like to do here is to filter out all mere
13253 file-scope declarations of file-scope functions which are never
13254 referenced later within this translation unit (and keep all of ones
13255 that *are* referenced later on) but we aren't clairvoyant, so we have
13256 no idea which functions will be referenced in the future (i.e. later
13257 on within the current translation unit). So here we just ignore all
13258 file-scope function declarations which are not also definitions. If
13259 and when the debugger needs to know something about these functions,
13260 it will have to hunt around and find the DWARF information associated
13261 with the definition of the function.
13263 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
13264 nodes represent definitions and which ones represent mere
13265 declarations. We have to check DECL_INITIAL instead. That's because
13266 the C front-end supports some weird semantics for "extern inline"
13267 function definitions. These can get inlined within the current
13268 translation unit (and thus, we need to generate Dwarf info for their
13269 abstract instances so that the Dwarf info for the concrete inlined
13270 instances can have something to refer to) but the compiler never
13271 generates any out-of-lines instances of such things (despite the fact
13272 that they *are* definitions).
13274 The important point is that the C front-end marks these "extern
13275 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
13276 them anyway. Note that the C++ front-end also plays some similar games
13277 for inline function definitions appearing within include files which
13278 also contain `#pragma interface' pragmas. */
13279 if (DECL_INITIAL (decl) == NULL_TREE)
13280 return;
13282 /* If we're a nested function, initially use a parent of NULL; if we're
13283 a plain function, this will be fixed up in decls_for_scope. If
13284 we're a method, it will be ignored, since we already have a DIE. */
13285 if (decl_function_context (decl)
13286 /* But if we're in terse mode, we don't care about scope. */
13287 && debug_info_level > DINFO_LEVEL_TERSE)
13288 context_die = NULL;
13289 break;
13291 case VAR_DECL:
13292 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
13293 declaration and if the declaration was never even referenced from
13294 within this entire compilation unit. We suppress these DIEs in
13295 order to save space in the .debug section (by eliminating entries
13296 which are probably useless). Note that we must not suppress
13297 block-local extern declarations (whether used or not) because that
13298 would screw-up the debugger's name lookup mechanism and cause it to
13299 miss things which really ought to be in scope at a given point. */
13300 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
13301 return;
13303 /* For local statics lookup proper context die. */
13304 if (TREE_STATIC (decl) && decl_function_context (decl))
13305 context_die = lookup_decl_die (DECL_CONTEXT (decl));
13307 /* If we are in terse mode, don't generate any DIEs to represent any
13308 variable declarations or definitions. */
13309 if (debug_info_level <= DINFO_LEVEL_TERSE)
13310 return;
13311 break;
13313 case NAMESPACE_DECL:
13314 if (debug_info_level <= DINFO_LEVEL_TERSE)
13315 return;
13316 if (lookup_decl_die (decl) != NULL)
13317 return;
13318 break;
13320 case TYPE_DECL:
13321 /* Don't emit stubs for types unless they are needed by other DIEs. */
13322 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
13323 return;
13325 /* Don't bother trying to generate any DIEs to represent any of the
13326 normal built-in types for the language we are compiling. */
13327 if (DECL_IS_BUILTIN (decl))
13329 /* OK, we need to generate one for `bool' so GDB knows what type
13330 comparisons have. */
13331 if (is_cxx ()
13332 && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
13333 && ! DECL_IGNORED_P (decl))
13334 modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
13336 return;
13339 /* If we are in terse mode, don't generate any DIEs for types. */
13340 if (debug_info_level <= DINFO_LEVEL_TERSE)
13341 return;
13343 /* If we're a function-scope tag, initially use a parent of NULL;
13344 this will be fixed up in decls_for_scope. */
13345 if (decl_function_context (decl))
13346 context_die = NULL;
13348 break;
13350 default:
13351 return;
13354 gen_decl_die (decl, context_die);
13357 /* Output a marker (i.e. a label) for the beginning of the generated code for
13358 a lexical block. */
13360 static void
13361 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
13362 unsigned int blocknum)
13364 switch_to_section (current_function_section ());
13365 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
13368 /* Output a marker (i.e. a label) for the end of the generated code for a
13369 lexical block. */
13371 static void
13372 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
13374 switch_to_section (current_function_section ());
13375 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
13378 /* Returns nonzero if it is appropriate not to emit any debugging
13379 information for BLOCK, because it doesn't contain any instructions.
13381 Don't allow this for blocks with nested functions or local classes
13382 as we would end up with orphans, and in the presence of scheduling
13383 we may end up calling them anyway. */
13385 static bool
13386 dwarf2out_ignore_block (tree block)
13388 tree decl;
13390 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
13391 if (TREE_CODE (decl) == FUNCTION_DECL
13392 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
13393 return 0;
13395 return 1;
13398 /* Lookup FILE_NAME (in the list of filenames that we know about here in
13399 dwarf2out.c) and return its "index". The index of each (known) filename is
13400 just a unique number which is associated with only that one filename. We
13401 need such numbers for the sake of generating labels (in the .debug_sfnames
13402 section) and references to those files numbers (in the .debug_srcinfo
13403 and.debug_macinfo sections). If the filename given as an argument is not
13404 found in our current list, add it to the list and assign it the next
13405 available unique index number. In order to speed up searches, we remember
13406 the index of the filename was looked up last. This handles the majority of
13407 all searches. */
13409 static unsigned
13410 lookup_filename (const char *file_name)
13412 size_t i, n;
13413 char *save_file_name;
13415 /* Check to see if the file name that was searched on the previous
13416 call matches this file name. If so, return the index. */
13417 if (file_table_last_lookup_index != 0)
13419 const char *last
13420 = VARRAY_CHAR_PTR (file_table, file_table_last_lookup_index);
13421 if (strcmp (file_name, last) == 0)
13422 return file_table_last_lookup_index;
13425 /* Didn't match the previous lookup, search the table. */
13426 n = VARRAY_ACTIVE_SIZE (file_table);
13427 for (i = 1; i < n; i++)
13428 if (strcmp (file_name, VARRAY_CHAR_PTR (file_table, i)) == 0)
13430 file_table_last_lookup_index = i;
13431 return i;
13434 /* Add the new entry to the end of the filename table. */
13435 file_table_last_lookup_index = n;
13436 save_file_name = (char *) ggc_strdup (file_name);
13437 VARRAY_PUSH_CHAR_PTR (file_table, save_file_name);
13438 VARRAY_PUSH_UINT (file_table_emitted, 0);
13440 /* If the assembler is emitting the file table, and we aren't eliminating
13441 unused debug types, then we must emit .file here. If we are eliminating
13442 unused debug types, then this will be done by the maybe_emit_file call in
13443 prune_unused_types_walk_attribs. */
13445 if (DWARF2_ASM_LINE_DEBUG_INFO && ! flag_eliminate_unused_debug_types)
13446 return maybe_emit_file (i);
13448 return i;
13451 /* If the assembler will construct the file table, then translate the compiler
13452 internal file table number into the assembler file table number, and emit
13453 a .file directive if we haven't already emitted one yet. The file table
13454 numbers are different because we prune debug info for unused variables and
13455 types, which may include filenames. */
13457 static int
13458 maybe_emit_file (int fileno)
13460 if (DWARF2_ASM_LINE_DEBUG_INFO && fileno > 0)
13462 if (!VARRAY_UINT (file_table_emitted, fileno))
13464 VARRAY_UINT (file_table_emitted, fileno) = ++emitcount;
13465 fprintf (asm_out_file, "\t.file %u ",
13466 VARRAY_UINT (file_table_emitted, fileno));
13467 output_quoted_string (asm_out_file,
13468 VARRAY_CHAR_PTR (file_table, fileno));
13469 fputc ('\n', asm_out_file);
13471 return VARRAY_UINT (file_table_emitted, fileno);
13473 else
13474 return fileno;
13477 /* Initialize the compiler internal file table. */
13479 static void
13480 init_file_table (void)
13482 /* Allocate the initial hunk of the file_table. */
13483 VARRAY_CHAR_PTR_INIT (file_table, 64, "file_table");
13484 VARRAY_UINT_INIT (file_table_emitted, 64, "file_table_emitted");
13486 /* Skip the first entry - file numbers begin at 1. */
13487 VARRAY_PUSH_CHAR_PTR (file_table, NULL);
13488 VARRAY_PUSH_UINT (file_table_emitted, 0);
13489 file_table_last_lookup_index = 0;
13492 /* Called by the final INSN scan whenever we see a var location. We
13493 use it to drop labels in the right places, and throw the location in
13494 our lookup table. */
13496 static void
13497 dwarf2out_var_location (rtx loc_note)
13499 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
13500 struct var_loc_node *newloc;
13501 rtx prev_insn;
13502 static rtx last_insn;
13503 static const char *last_label;
13504 tree decl;
13506 if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
13507 return;
13508 prev_insn = PREV_INSN (loc_note);
13510 newloc = ggc_alloc_cleared (sizeof (struct var_loc_node));
13511 /* If the insn we processed last time is the previous insn
13512 and it is also a var location note, use the label we emitted
13513 last time. */
13514 if (last_insn != NULL_RTX
13515 && last_insn == prev_insn
13516 && NOTE_P (prev_insn)
13517 && NOTE_LINE_NUMBER (prev_insn) == NOTE_INSN_VAR_LOCATION)
13519 newloc->label = last_label;
13521 else
13523 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
13524 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
13525 loclabel_num++;
13526 newloc->label = ggc_strdup (loclabel);
13528 newloc->var_loc_note = loc_note;
13529 newloc->next = NULL;
13531 if (cfun && in_cold_section_p)
13532 newloc->section_label = cfun->cold_section_label;
13533 else
13534 newloc->section_label = text_section_label;
13536 last_insn = loc_note;
13537 last_label = newloc->label;
13538 decl = NOTE_VAR_LOCATION_DECL (loc_note);
13539 if (DECL_DEBUG_EXPR_IS_FROM (decl) && DECL_DEBUG_EXPR (decl)
13540 && DECL_P (DECL_DEBUG_EXPR (decl)))
13541 decl = DECL_DEBUG_EXPR (decl);
13542 add_var_loc_to_decl (decl, newloc);
13545 /* We need to reset the locations at the beginning of each
13546 function. We can't do this in the end_function hook, because the
13547 declarations that use the locations won't have been output when
13548 that hook is called. Also compute have_multiple_function_sections here. */
13550 static void
13551 dwarf2out_begin_function (tree fun)
13553 htab_empty (decl_loc_table);
13555 if (function_section (fun) != text_section)
13556 have_multiple_function_sections = true;
13559 /* Output a label to mark the beginning of a source code line entry
13560 and record information relating to this source line, in
13561 'line_info_table' for later output of the .debug_line section. */
13563 static void
13564 dwarf2out_source_line (unsigned int line, const char *filename)
13566 if (debug_info_level >= DINFO_LEVEL_NORMAL
13567 && line != 0)
13569 switch_to_section (current_function_section ());
13571 /* If requested, emit something human-readable. */
13572 if (flag_debug_asm)
13573 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
13574 filename, line);
13576 if (DWARF2_ASM_LINE_DEBUG_INFO)
13578 unsigned file_num = lookup_filename (filename);
13580 file_num = maybe_emit_file (file_num);
13582 /* Emit the .loc directive understood by GNU as. */
13583 fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
13585 /* Indicate that line number info exists. */
13586 line_info_table_in_use++;
13588 else if (function_section (current_function_decl) != text_section)
13590 dw_separate_line_info_ref line_info;
13591 targetm.asm_out.internal_label (asm_out_file, SEPARATE_LINE_CODE_LABEL,
13592 separate_line_info_table_in_use);
13594 /* Expand the line info table if necessary. */
13595 if (separate_line_info_table_in_use
13596 == separate_line_info_table_allocated)
13598 separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
13599 separate_line_info_table
13600 = ggc_realloc (separate_line_info_table,
13601 separate_line_info_table_allocated
13602 * sizeof (dw_separate_line_info_entry));
13603 memset (separate_line_info_table
13604 + separate_line_info_table_in_use,
13606 (LINE_INFO_TABLE_INCREMENT
13607 * sizeof (dw_separate_line_info_entry)));
13610 /* Add the new entry at the end of the line_info_table. */
13611 line_info
13612 = &separate_line_info_table[separate_line_info_table_in_use++];
13613 line_info->dw_file_num = lookup_filename (filename);
13614 line_info->dw_line_num = line;
13615 line_info->function = current_function_funcdef_no;
13617 else
13619 dw_line_info_ref line_info;
13621 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
13622 line_info_table_in_use);
13624 /* Expand the line info table if necessary. */
13625 if (line_info_table_in_use == line_info_table_allocated)
13627 line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
13628 line_info_table
13629 = ggc_realloc (line_info_table,
13630 (line_info_table_allocated
13631 * sizeof (dw_line_info_entry)));
13632 memset (line_info_table + line_info_table_in_use, 0,
13633 LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
13636 /* Add the new entry at the end of the line_info_table. */
13637 line_info = &line_info_table[line_info_table_in_use++];
13638 line_info->dw_file_num = lookup_filename (filename);
13639 line_info->dw_line_num = line;
13644 /* Record the beginning of a new source file. */
13646 static void
13647 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
13649 if (flag_eliminate_dwarf2_dups)
13651 /* Record the beginning of the file for break_out_includes. */
13652 dw_die_ref bincl_die;
13654 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
13655 add_AT_string (bincl_die, DW_AT_name, filename);
13658 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13660 int fileno;
13662 switch_to_section (debug_macinfo_section);
13663 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
13664 dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
13665 lineno);
13667 fileno = maybe_emit_file (lookup_filename (filename));
13668 dw2_asm_output_data_uleb128 (fileno, "Filename we just started");
13672 /* Record the end of a source file. */
13674 static void
13675 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
13677 if (flag_eliminate_dwarf2_dups)
13678 /* Record the end of the file for break_out_includes. */
13679 new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
13681 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13683 switch_to_section (debug_macinfo_section);
13684 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
13688 /* Called from debug_define in toplev.c. The `buffer' parameter contains
13689 the tail part of the directive line, i.e. the part which is past the
13690 initial whitespace, #, whitespace, directive-name, whitespace part. */
13692 static void
13693 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
13694 const char *buffer ATTRIBUTE_UNUSED)
13696 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13698 switch_to_section (debug_macinfo_section);
13699 dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
13700 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
13701 dw2_asm_output_nstring (buffer, -1, "The macro");
13705 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
13706 the tail part of the directive line, i.e. the part which is past the
13707 initial whitespace, #, whitespace, directive-name, whitespace part. */
13709 static void
13710 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
13711 const char *buffer ATTRIBUTE_UNUSED)
13713 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13715 switch_to_section (debug_macinfo_section);
13716 dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
13717 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
13718 dw2_asm_output_nstring (buffer, -1, "The macro");
13722 /* Set up for Dwarf output at the start of compilation. */
13724 static void
13725 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
13727 init_file_table ();
13729 /* Allocate the decl_die_table. */
13730 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
13731 decl_die_table_eq, NULL);
13733 /* Allocate the decl_loc_table. */
13734 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
13735 decl_loc_table_eq, NULL);
13737 /* Allocate the initial hunk of the decl_scope_table. */
13738 decl_scope_table = VEC_alloc (tree, gc, 256);
13740 /* Allocate the initial hunk of the abbrev_die_table. */
13741 abbrev_die_table = ggc_alloc_cleared (ABBREV_DIE_TABLE_INCREMENT
13742 * sizeof (dw_die_ref));
13743 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
13744 /* Zero-th entry is allocated, but unused. */
13745 abbrev_die_table_in_use = 1;
13747 /* Allocate the initial hunk of the line_info_table. */
13748 line_info_table = ggc_alloc_cleared (LINE_INFO_TABLE_INCREMENT
13749 * sizeof (dw_line_info_entry));
13750 line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
13752 /* Zero-th entry is allocated, but unused. */
13753 line_info_table_in_use = 1;
13755 /* Generate the initial DIE for the .debug section. Note that the (string)
13756 value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
13757 will (typically) be a relative pathname and that this pathname should be
13758 taken as being relative to the directory from which the compiler was
13759 invoked when the given (base) source file was compiled. We will fill
13760 in this value in dwarf2out_finish. */
13761 comp_unit_die = gen_compile_unit_die (NULL);
13763 incomplete_types = VEC_alloc (tree, gc, 64);
13765 used_rtx_array = VEC_alloc (rtx, gc, 32);
13767 debug_info_section = get_section (DEBUG_INFO_SECTION,
13768 SECTION_DEBUG, NULL);
13769 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
13770 SECTION_DEBUG, NULL);
13771 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
13772 SECTION_DEBUG, NULL);
13773 debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
13774 SECTION_DEBUG, NULL);
13775 debug_line_section = get_section (DEBUG_LINE_SECTION,
13776 SECTION_DEBUG, NULL);
13777 debug_loc_section = get_section (DEBUG_LOC_SECTION,
13778 SECTION_DEBUG, NULL);
13779 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
13780 SECTION_DEBUG, NULL);
13781 debug_str_section = get_section (DEBUG_STR_SECTION,
13782 DEBUG_STR_SECTION_FLAGS, NULL);
13783 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
13784 SECTION_DEBUG, NULL);
13785 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
13786 SECTION_DEBUG, NULL);
13788 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
13789 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
13790 DEBUG_ABBREV_SECTION_LABEL, 0);
13791 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
13792 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
13793 COLD_TEXT_SECTION_LABEL, 0);
13794 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
13796 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
13797 DEBUG_INFO_SECTION_LABEL, 0);
13798 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
13799 DEBUG_LINE_SECTION_LABEL, 0);
13800 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
13801 DEBUG_RANGES_SECTION_LABEL, 0);
13802 switch_to_section (debug_abbrev_section);
13803 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
13804 switch_to_section (debug_info_section);
13805 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
13806 switch_to_section (debug_line_section);
13807 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
13809 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13811 switch_to_section (debug_macinfo_section);
13812 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
13813 DEBUG_MACINFO_SECTION_LABEL, 0);
13814 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
13817 switch_to_section (text_section);
13818 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
13819 if (flag_reorder_blocks_and_partition)
13821 switch_to_section (unlikely_text_section ());
13822 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
13826 /* A helper function for dwarf2out_finish called through
13827 ht_forall. Emit one queued .debug_str string. */
13829 static int
13830 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
13832 struct indirect_string_node *node = (struct indirect_string_node *) *h;
13834 if (node->form == DW_FORM_strp)
13836 switch_to_section (debug_str_section);
13837 ASM_OUTPUT_LABEL (asm_out_file, node->label);
13838 assemble_string (node->str, strlen (node->str) + 1);
13841 return 1;
13846 /* Clear the marks for a die and its children.
13847 Be cool if the mark isn't set. */
13849 static void
13850 prune_unmark_dies (dw_die_ref die)
13852 dw_die_ref c;
13853 die->die_mark = 0;
13854 for (c = die->die_child; c; c = c->die_sib)
13855 prune_unmark_dies (c);
13859 /* Given DIE that we're marking as used, find any other dies
13860 it references as attributes and mark them as used. */
13862 static void
13863 prune_unused_types_walk_attribs (dw_die_ref die)
13865 dw_attr_ref a;
13867 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
13869 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
13871 /* A reference to another DIE.
13872 Make sure that it will get emitted. */
13873 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
13875 else if (a->dw_attr == DW_AT_decl_file || a->dw_attr == DW_AT_call_file)
13877 /* A reference to a file. Make sure the file name is emitted. */
13878 a->dw_attr_val.v.val_unsigned =
13879 maybe_emit_file (a->dw_attr_val.v.val_unsigned);
13885 /* Mark DIE as being used. If DOKIDS is true, then walk down
13886 to DIE's children. */
13888 static void
13889 prune_unused_types_mark (dw_die_ref die, int dokids)
13891 dw_die_ref c;
13893 if (die->die_mark == 0)
13895 /* We haven't done this node yet. Mark it as used. */
13896 die->die_mark = 1;
13898 /* We also have to mark its parents as used.
13899 (But we don't want to mark our parents' kids due to this.) */
13900 if (die->die_parent)
13901 prune_unused_types_mark (die->die_parent, 0);
13903 /* Mark any referenced nodes. */
13904 prune_unused_types_walk_attribs (die);
13906 /* If this node is a specification,
13907 also mark the definition, if it exists. */
13908 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
13909 prune_unused_types_mark (die->die_definition, 1);
13912 if (dokids && die->die_mark != 2)
13914 /* We need to walk the children, but haven't done so yet.
13915 Remember that we've walked the kids. */
13916 die->die_mark = 2;
13918 /* Walk them. */
13919 for (c = die->die_child; c; c = c->die_sib)
13921 /* If this is an array type, we need to make sure our
13922 kids get marked, even if they're types. */
13923 if (die->die_tag == DW_TAG_array_type)
13924 prune_unused_types_mark (c, 1);
13925 else
13926 prune_unused_types_walk (c);
13932 /* Walk the tree DIE and mark types that we actually use. */
13934 static void
13935 prune_unused_types_walk (dw_die_ref die)
13937 dw_die_ref c;
13939 /* Don't do anything if this node is already marked. */
13940 if (die->die_mark)
13941 return;
13943 switch (die->die_tag) {
13944 case DW_TAG_const_type:
13945 case DW_TAG_packed_type:
13946 case DW_TAG_pointer_type:
13947 case DW_TAG_reference_type:
13948 case DW_TAG_volatile_type:
13949 case DW_TAG_typedef:
13950 case DW_TAG_array_type:
13951 case DW_TAG_structure_type:
13952 case DW_TAG_union_type:
13953 case DW_TAG_class_type:
13954 case DW_TAG_friend:
13955 case DW_TAG_variant_part:
13956 case DW_TAG_enumeration_type:
13957 case DW_TAG_subroutine_type:
13958 case DW_TAG_string_type:
13959 case DW_TAG_set_type:
13960 case DW_TAG_subrange_type:
13961 case DW_TAG_ptr_to_member_type:
13962 case DW_TAG_file_type:
13963 /* It's a type node --- don't mark it. */
13964 return;
13966 default:
13967 /* Mark everything else. */
13968 break;
13971 die->die_mark = 1;
13973 /* Now, mark any dies referenced from here. */
13974 prune_unused_types_walk_attribs (die);
13976 /* Mark children. */
13977 for (c = die->die_child; c; c = c->die_sib)
13978 prune_unused_types_walk (c);
13982 /* Remove from the tree DIE any dies that aren't marked. */
13984 static void
13985 prune_unused_types_prune (dw_die_ref die)
13987 dw_die_ref c, p, n;
13989 gcc_assert (die->die_mark);
13991 p = NULL;
13992 for (c = die->die_child; c; c = n)
13994 n = c->die_sib;
13995 if (c->die_mark)
13997 prune_unused_types_prune (c);
13998 p = c;
14000 else
14002 if (p)
14003 p->die_sib = n;
14004 else
14005 die->die_child = n;
14006 free_die (c);
14012 /* Remove dies representing declarations that we never use. */
14014 static void
14015 prune_unused_types (void)
14017 unsigned int i;
14018 limbo_die_node *node;
14020 /* Clear all the marks. */
14021 prune_unmark_dies (comp_unit_die);
14022 for (node = limbo_die_list; node; node = node->next)
14023 prune_unmark_dies (node->die);
14025 /* Set the mark on nodes that are actually used. */
14026 prune_unused_types_walk (comp_unit_die);
14027 for (node = limbo_die_list; node; node = node->next)
14028 prune_unused_types_walk (node->die);
14030 /* Also set the mark on nodes referenced from the
14031 pubname_table or arange_table. */
14032 for (i = 0; i < pubname_table_in_use; i++)
14033 prune_unused_types_mark (pubname_table[i].die, 1);
14034 for (i = 0; i < arange_table_in_use; i++)
14035 prune_unused_types_mark (arange_table[i], 1);
14037 /* Get rid of nodes that aren't marked. */
14038 prune_unused_types_prune (comp_unit_die);
14039 for (node = limbo_die_list; node; node = node->next)
14040 prune_unused_types_prune (node->die);
14042 /* Leave the marks clear. */
14043 prune_unmark_dies (comp_unit_die);
14044 for (node = limbo_die_list; node; node = node->next)
14045 prune_unmark_dies (node->die);
14048 /* Output stuff that dwarf requires at the end of every file,
14049 and generate the DWARF-2 debugging info. */
14051 static void
14052 dwarf2out_finish (const char *filename)
14054 limbo_die_node *node, *next_node;
14055 dw_die_ref die = 0;
14057 /* Add the name for the main input file now. We delayed this from
14058 dwarf2out_init to avoid complications with PCH. */
14059 add_name_attribute (comp_unit_die, filename);
14060 if (filename[0] != DIR_SEPARATOR)
14061 add_comp_dir_attribute (comp_unit_die);
14062 else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
14064 size_t i;
14065 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
14066 if (VARRAY_CHAR_PTR (file_table, i)[0] != DIR_SEPARATOR
14067 /* Don't add cwd for <built-in>. */
14068 && VARRAY_CHAR_PTR (file_table, i)[0] != '<')
14070 add_comp_dir_attribute (comp_unit_die);
14071 break;
14075 /* Traverse the limbo die list, and add parent/child links. The only
14076 dies without parents that should be here are concrete instances of
14077 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
14078 For concrete instances, we can get the parent die from the abstract
14079 instance. */
14080 for (node = limbo_die_list; node; node = next_node)
14082 next_node = node->next;
14083 die = node->die;
14085 if (die->die_parent == NULL)
14087 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
14089 if (origin)
14090 add_child_die (origin->die_parent, die);
14091 else if (die == comp_unit_die)
14093 else if (errorcount > 0 || sorrycount > 0)
14094 /* It's OK to be confused by errors in the input. */
14095 add_child_die (comp_unit_die, die);
14096 else
14098 /* In certain situations, the lexical block containing a
14099 nested function can be optimized away, which results
14100 in the nested function die being orphaned. Likewise
14101 with the return type of that nested function. Force
14102 this to be a child of the containing function.
14104 It may happen that even the containing function got fully
14105 inlined and optimized out. In that case we are lost and
14106 assign the empty child. This should not be big issue as
14107 the function is likely unreachable too. */
14108 tree context = NULL_TREE;
14110 gcc_assert (node->created_for);
14112 if (DECL_P (node->created_for))
14113 context = DECL_CONTEXT (node->created_for);
14114 else if (TYPE_P (node->created_for))
14115 context = TYPE_CONTEXT (node->created_for);
14117 gcc_assert (context && TREE_CODE (context) == FUNCTION_DECL);
14119 origin = lookup_decl_die (context);
14120 if (origin)
14121 add_child_die (origin, die);
14122 else
14123 add_child_die (comp_unit_die, die);
14128 limbo_die_list = NULL;
14130 /* Walk through the list of incomplete types again, trying once more to
14131 emit full debugging info for them. */
14132 retry_incomplete_types ();
14134 /* We need to reverse all the dies before break_out_includes, or
14135 we'll see the end of an include file before the beginning. */
14136 reverse_all_dies (comp_unit_die);
14138 if (flag_eliminate_unused_debug_types)
14139 prune_unused_types ();
14141 /* Generate separate CUs for each of the include files we've seen.
14142 They will go into limbo_die_list. */
14143 if (flag_eliminate_dwarf2_dups)
14144 break_out_includes (comp_unit_die);
14146 /* Traverse the DIE's and add add sibling attributes to those DIE's
14147 that have children. */
14148 add_sibling_attributes (comp_unit_die);
14149 for (node = limbo_die_list; node; node = node->next)
14150 add_sibling_attributes (node->die);
14152 /* Output a terminator label for the .text section. */
14153 switch_to_section (text_section);
14154 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
14155 if (flag_reorder_blocks_and_partition)
14157 switch_to_section (unlikely_text_section ());
14158 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
14161 /* Output the source line correspondence table. We must do this
14162 even if there is no line information. Otherwise, on an empty
14163 translation unit, we will generate a present, but empty,
14164 .debug_info section. IRIX 6.5 `nm' will then complain when
14165 examining the file. */
14166 if (! DWARF2_ASM_LINE_DEBUG_INFO)
14168 switch_to_section (debug_line_section);
14169 output_line_info ();
14172 /* We can only use the low/high_pc attributes if all of the code was
14173 in .text. */
14174 if (!have_multiple_function_sections)
14176 add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
14177 add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
14180 /* If it wasn't, we need to give .debug_loc and .debug_ranges an appropriate
14181 "base address". Use zero so that these addresses become absolute. */
14182 else if (have_location_lists || ranges_table_in_use)
14183 add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
14185 /* Output location list section if necessary. */
14186 if (have_location_lists)
14188 /* Output the location lists info. */
14189 switch_to_section (debug_loc_section);
14190 ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
14191 DEBUG_LOC_SECTION_LABEL, 0);
14192 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
14193 output_location_lists (die);
14196 if (debug_info_level >= DINFO_LEVEL_NORMAL)
14197 add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
14198 debug_line_section_label);
14200 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14201 add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
14203 /* Output all of the compilation units. We put the main one last so that
14204 the offsets are available to output_pubnames. */
14205 for (node = limbo_die_list; node; node = node->next)
14206 output_comp_unit (node->die, 0);
14208 output_comp_unit (comp_unit_die, 0);
14210 /* Output the abbreviation table. */
14211 switch_to_section (debug_abbrev_section);
14212 output_abbrev_section ();
14214 /* Output public names table if necessary. */
14215 if (pubname_table_in_use)
14217 switch_to_section (debug_pubnames_section);
14218 output_pubnames ();
14221 /* Output the address range information. We only put functions in the arange
14222 table, so don't write it out if we don't have any. */
14223 if (fde_table_in_use)
14225 switch_to_section (debug_aranges_section);
14226 output_aranges ();
14229 /* Output ranges section if necessary. */
14230 if (ranges_table_in_use)
14232 switch_to_section (debug_ranges_section);
14233 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
14234 output_ranges ();
14237 /* Have to end the macro section. */
14238 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14240 switch_to_section (debug_macinfo_section);
14241 dw2_asm_output_data (1, 0, "End compilation unit");
14244 /* If we emitted any DW_FORM_strp form attribute, output the string
14245 table too. */
14246 if (debug_str_hash)
14247 htab_traverse (debug_str_hash, output_indirect_string, NULL);
14249 #else
14251 /* This should never be used, but its address is needed for comparisons. */
14252 const struct gcc_debug_hooks dwarf2_debug_hooks;
14254 #endif /* DWARF2_DEBUGGING_INFO */
14256 #include "gt-dwarf2out.h"