Sync usage with man page.
[netbsd-mini2440.git] / gnu / dist / gcc4 / gcc / dwarf2out.c
blob0eaf18d79021890d47d8477be3c934d3de5b8b59
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 /* Decide whether we want to emit frame unwind information for the current
103 translation unit. */
106 dwarf2out_do_frame (void)
108 /* We want to emit correct CFA location expressions or lists, so we
109 have to return true if we're going to output debug info, even if
110 we're not going to output frame or unwind info. */
111 return (write_symbols == DWARF2_DEBUG
112 || write_symbols == VMS_AND_DWARF2_DEBUG
113 || DWARF2_FRAME_INFO
114 #ifdef DWARF2_UNWIND_INFO
115 || (DWARF2_UNWIND_INFO
116 && (flag_unwind_tables
117 || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)))
118 #endif
122 /* The size of the target's pointer type. */
123 #ifndef PTR_SIZE
124 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
125 #endif
127 /* Various versions of targetm.eh_frame_section. Note these must appear
128 outside the DWARF2_DEBUGGING_INFO || DWARF2_UNWIND_INFO macro guards. */
130 /* Version of targetm.eh_frame_section for systems with named sections. */
131 void
132 named_section_eh_frame_section (void)
134 #ifdef EH_FRAME_SECTION_NAME
135 int flags;
137 if (EH_TABLES_CAN_BE_READ_ONLY)
139 int fde_encoding;
140 int per_encoding;
141 int lsda_encoding;
143 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
144 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
145 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
146 flags = (! flag_pic
147 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
148 && (fde_encoding & 0x70) != DW_EH_PE_aligned
149 && (per_encoding & 0x70) != DW_EH_PE_absptr
150 && (per_encoding & 0x70) != DW_EH_PE_aligned
151 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
152 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
153 ? 0 : SECTION_WRITE;
155 else
156 flags = SECTION_WRITE;
157 named_section_flags (EH_FRAME_SECTION_NAME, flags);
158 #endif
161 /* Version of targetm.eh_frame_section for systems using collect2. */
162 void
163 collect2_eh_frame_section (void)
165 tree label = get_file_function_name ('F');
167 data_section ();
168 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
169 targetm.asm_out.globalize_label (asm_out_file, IDENTIFIER_POINTER (label));
170 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
173 /* Default version of targetm.eh_frame_section. */
174 void
175 default_eh_frame_section (void)
177 #ifdef EH_FRAME_SECTION_NAME
178 named_section_eh_frame_section ();
179 #else
180 collect2_eh_frame_section ();
181 #endif
184 DEF_VEC_P(rtx);
185 DEF_VEC_ALLOC_P(rtx,gc);
187 /* Array of RTXes referenced by the debugging information, which therefore
188 must be kept around forever. */
189 static GTY(()) VEC(rtx,gc) *used_rtx_array;
191 /* A pointer to the base of a list of incomplete types which might be
192 completed at some later time. incomplete_types_list needs to be a
193 VEC(tree,gc) because we want to tell the garbage collector about
194 it. */
195 static GTY(()) VEC(tree,gc) *incomplete_types;
197 /* A pointer to the base of a table of references to declaration
198 scopes. This table is a display which tracks the nesting
199 of declaration scopes at the current scope and containing
200 scopes. This table is used to find the proper place to
201 define type declaration DIE's. */
202 static GTY(()) VEC(tree,gc) *decl_scope_table;
204 /* How to start an assembler comment. */
205 #ifndef ASM_COMMENT_START
206 #define ASM_COMMENT_START ";#"
207 #endif
209 typedef struct dw_cfi_struct *dw_cfi_ref;
210 typedef struct dw_fde_struct *dw_fde_ref;
211 typedef union dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
213 /* Call frames are described using a sequence of Call Frame
214 Information instructions. The register number, offset
215 and address fields are provided as possible operands;
216 their use is selected by the opcode field. */
218 enum dw_cfi_oprnd_type {
219 dw_cfi_oprnd_unused,
220 dw_cfi_oprnd_reg_num,
221 dw_cfi_oprnd_offset,
222 dw_cfi_oprnd_addr,
223 dw_cfi_oprnd_loc
226 typedef union dw_cfi_oprnd_struct GTY(())
228 unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
229 HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
230 const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
231 struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
233 dw_cfi_oprnd;
235 typedef struct dw_cfi_struct GTY(())
237 dw_cfi_ref dw_cfi_next;
238 enum dwarf_call_frame_info dw_cfi_opc;
239 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
240 dw_cfi_oprnd1;
241 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
242 dw_cfi_oprnd2;
244 dw_cfi_node;
246 /* This is how we define the location of the CFA. We use to handle it
247 as REG + OFFSET all the time, but now it can be more complex.
248 It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
249 Instead of passing around REG and OFFSET, we pass a copy
250 of this structure. */
251 typedef struct cfa_loc GTY(())
253 HOST_WIDE_INT offset;
254 HOST_WIDE_INT base_offset;
255 unsigned int reg;
256 int indirect; /* 1 if CFA is accessed via a dereference. */
257 } dw_cfa_location;
259 /* All call frame descriptions (FDE's) in the GCC generated DWARF
260 refer to a single Common Information Entry (CIE), defined at
261 the beginning of the .debug_frame section. This use of a single
262 CIE obviates the need to keep track of multiple CIE's
263 in the DWARF generation routines below. */
265 typedef struct dw_fde_struct GTY(())
267 tree decl;
268 const char *dw_fde_begin;
269 const char *dw_fde_current_label;
270 const char *dw_fde_end;
271 const char *dw_fde_hot_section_label;
272 const char *dw_fde_hot_section_end_label;
273 const char *dw_fde_unlikely_section_label;
274 const char *dw_fde_unlikely_section_end_label;
275 bool dw_fde_switched_sections;
276 dw_cfi_ref dw_fde_cfi;
277 unsigned funcdef_number;
278 unsigned all_throwers_are_sibcalls : 1;
279 unsigned nothrow : 1;
280 unsigned uses_eh_lsda : 1;
282 dw_fde_node;
284 /* Maximum size (in bytes) of an artificially generated label. */
285 #define MAX_ARTIFICIAL_LABEL_BYTES 30
287 /* The size of addresses as they appear in the Dwarf 2 data.
288 Some architectures use word addresses to refer to code locations,
289 but Dwarf 2 info always uses byte addresses. On such machines,
290 Dwarf 2 addresses need to be larger than the architecture's
291 pointers. */
292 #ifndef DWARF2_ADDR_SIZE
293 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
294 #endif
296 /* The size in bytes of a DWARF field indicating an offset or length
297 relative to a debug info section, specified to be 4 bytes in the
298 DWARF-2 specification. The SGI/MIPS ABI defines it to be the same
299 as PTR_SIZE. */
301 #ifndef DWARF_OFFSET_SIZE
302 #define DWARF_OFFSET_SIZE 4
303 #endif
305 /* According to the (draft) DWARF 3 specification, the initial length
306 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
307 bytes are 0xffffffff, followed by the length stored in the next 8
308 bytes.
310 However, the SGI/MIPS ABI uses an initial length which is equal to
311 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
313 #ifndef DWARF_INITIAL_LENGTH_SIZE
314 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
315 #endif
317 #define DWARF_VERSION 2
319 /* Round SIZE up to the nearest BOUNDARY. */
320 #define DWARF_ROUND(SIZE,BOUNDARY) \
321 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
323 /* Offsets recorded in opcodes are a multiple of this alignment factor. */
324 #ifndef DWARF_CIE_DATA_ALIGNMENT
325 #ifdef STACK_GROWS_DOWNWARD
326 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
327 #else
328 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
329 #endif
330 #endif
332 /* A pointer to the base of a table that contains frame description
333 information for each routine. */
334 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
336 /* Number of elements currently allocated for fde_table. */
337 static GTY(()) unsigned fde_table_allocated;
339 /* Number of elements in fde_table currently in use. */
340 static GTY(()) unsigned fde_table_in_use;
342 /* Size (in elements) of increments by which we may expand the
343 fde_table. */
344 #define FDE_TABLE_INCREMENT 256
346 /* A list of call frame insns for the CIE. */
347 static GTY(()) dw_cfi_ref cie_cfi_head;
349 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
350 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
351 attribute that accelerates the lookup of the FDE associated
352 with the subprogram. This variable holds the table index of the FDE
353 associated with the current function (body) definition. */
354 static unsigned current_funcdef_fde;
355 #endif
357 struct indirect_string_node GTY(())
359 const char *str;
360 unsigned int refcount;
361 unsigned int form;
362 char *label;
365 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
367 static GTY(()) int dw2_string_counter;
368 static GTY(()) unsigned long dwarf2out_cfi_label_num;
370 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
372 /* Forward declarations for functions defined in this file. */
374 static char *stripattributes (const char *);
375 static const char *dwarf_cfi_name (unsigned);
376 static dw_cfi_ref new_cfi (void);
377 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
378 static void add_fde_cfi (const char *, dw_cfi_ref);
379 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *);
380 static void lookup_cfa (dw_cfa_location *);
381 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
382 static void initial_return_save (rtx);
383 static HOST_WIDE_INT stack_adjust_offset (rtx);
384 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
385 static void output_call_frame_info (int);
386 static void dwarf2out_stack_adjust (rtx, bool);
387 static void flush_queued_reg_saves (void);
388 static bool clobbers_queued_reg_save (rtx);
389 static void dwarf2out_frame_debug_expr (rtx, const char *);
391 /* Support for complex CFA locations. */
392 static void output_cfa_loc (dw_cfi_ref);
393 static void get_cfa_from_loc_descr (dw_cfa_location *,
394 struct dw_loc_descr_struct *);
395 static struct dw_loc_descr_struct *build_cfa_loc
396 (dw_cfa_location *);
397 static void def_cfa_1 (const char *, dw_cfa_location *);
399 /* How to start an assembler comment. */
400 #ifndef ASM_COMMENT_START
401 #define ASM_COMMENT_START ";#"
402 #endif
404 /* Data and reference forms for relocatable data. */
405 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
406 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
408 #ifndef DEBUG_FRAME_SECTION
409 #define DEBUG_FRAME_SECTION ".debug_frame"
410 #endif
412 #ifndef FUNC_BEGIN_LABEL
413 #define FUNC_BEGIN_LABEL "LFB"
414 #endif
416 #ifndef FUNC_END_LABEL
417 #define FUNC_END_LABEL "LFE"
418 #endif
420 #ifndef FRAME_BEGIN_LABEL
421 #define FRAME_BEGIN_LABEL "Lframe"
422 #endif
423 #define CIE_AFTER_SIZE_LABEL "LSCIE"
424 #define CIE_END_LABEL "LECIE"
425 #define FDE_LABEL "LSFDE"
426 #define FDE_AFTER_SIZE_LABEL "LASFDE"
427 #define FDE_END_LABEL "LEFDE"
428 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
429 #define LINE_NUMBER_END_LABEL "LELT"
430 #define LN_PROLOG_AS_LABEL "LASLTP"
431 #define LN_PROLOG_END_LABEL "LELTP"
432 #define DIE_LABEL_PREFIX "DW"
434 /* The DWARF 2 CFA column which tracks the return address. Normally this
435 is the column for PC, or the first column after all of the hard
436 registers. */
437 #ifndef DWARF_FRAME_RETURN_COLUMN
438 #ifdef PC_REGNUM
439 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGNUM (PC_REGNUM)
440 #else
441 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGISTERS
442 #endif
443 #endif
445 /* The mapping from gcc register number to DWARF 2 CFA column number. By
446 default, we just provide columns for all registers. */
447 #ifndef DWARF_FRAME_REGNUM
448 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
449 #endif
451 /* Hook used by __throw. */
454 expand_builtin_dwarf_sp_column (void)
456 return GEN_INT (DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
459 /* Return a pointer to a copy of the section string name S with all
460 attributes stripped off, and an asterisk prepended (for assemble_name). */
462 static inline char *
463 stripattributes (const char *s)
465 char *stripped = xmalloc (strlen (s) + 2);
466 char *p = stripped;
468 *p++ = '*';
470 while (*s && *s != ',')
471 *p++ = *s++;
473 *p = '\0';
474 return stripped;
477 /* Generate code to initialize the register size table. */
479 void
480 expand_builtin_init_dwarf_reg_sizes (tree address)
482 int i;
483 enum machine_mode mode = TYPE_MODE (char_type_node);
484 rtx addr = expand_expr (address, NULL_RTX, VOIDmode, 0);
485 rtx mem = gen_rtx_MEM (BLKmode, addr);
486 bool wrote_return_column = false;
488 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
489 if (DWARF_FRAME_REGNUM (i) < DWARF_FRAME_REGISTERS)
491 HOST_WIDE_INT offset = DWARF_FRAME_REGNUM (i) * GET_MODE_SIZE (mode);
492 enum machine_mode save_mode = reg_raw_mode[i];
493 HOST_WIDE_INT size;
495 if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
496 save_mode = choose_hard_reg_mode (i, 1, true);
497 if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
499 if (save_mode == VOIDmode)
500 continue;
501 wrote_return_column = true;
503 size = GET_MODE_SIZE (save_mode);
504 if (offset < 0)
505 continue;
507 emit_move_insn (adjust_address (mem, mode, offset),
508 gen_int_mode (size, mode));
511 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
512 gcc_assert (wrote_return_column);
513 i = DWARF_ALT_FRAME_RETURN_COLUMN;
514 wrote_return_column = false;
515 #else
516 i = DWARF_FRAME_RETURN_COLUMN;
517 #endif
519 if (! wrote_return_column)
521 enum machine_mode save_mode = Pmode;
522 HOST_WIDE_INT offset = i * GET_MODE_SIZE (mode);
523 HOST_WIDE_INT size = GET_MODE_SIZE (save_mode);
524 emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
528 /* Convert a DWARF call frame info. operation to its string name */
530 static const char *
531 dwarf_cfi_name (unsigned int cfi_opc)
533 switch (cfi_opc)
535 case DW_CFA_advance_loc:
536 return "DW_CFA_advance_loc";
537 case DW_CFA_offset:
538 return "DW_CFA_offset";
539 case DW_CFA_restore:
540 return "DW_CFA_restore";
541 case DW_CFA_nop:
542 return "DW_CFA_nop";
543 case DW_CFA_set_loc:
544 return "DW_CFA_set_loc";
545 case DW_CFA_advance_loc1:
546 return "DW_CFA_advance_loc1";
547 case DW_CFA_advance_loc2:
548 return "DW_CFA_advance_loc2";
549 case DW_CFA_advance_loc4:
550 return "DW_CFA_advance_loc4";
551 case DW_CFA_offset_extended:
552 return "DW_CFA_offset_extended";
553 case DW_CFA_restore_extended:
554 return "DW_CFA_restore_extended";
555 case DW_CFA_undefined:
556 return "DW_CFA_undefined";
557 case DW_CFA_same_value:
558 return "DW_CFA_same_value";
559 case DW_CFA_register:
560 return "DW_CFA_register";
561 case DW_CFA_remember_state:
562 return "DW_CFA_remember_state";
563 case DW_CFA_restore_state:
564 return "DW_CFA_restore_state";
565 case DW_CFA_def_cfa:
566 return "DW_CFA_def_cfa";
567 case DW_CFA_def_cfa_register:
568 return "DW_CFA_def_cfa_register";
569 case DW_CFA_def_cfa_offset:
570 return "DW_CFA_def_cfa_offset";
572 /* DWARF 3 */
573 case DW_CFA_def_cfa_expression:
574 return "DW_CFA_def_cfa_expression";
575 case DW_CFA_expression:
576 return "DW_CFA_expression";
577 case DW_CFA_offset_extended_sf:
578 return "DW_CFA_offset_extended_sf";
579 case DW_CFA_def_cfa_sf:
580 return "DW_CFA_def_cfa_sf";
581 case DW_CFA_def_cfa_offset_sf:
582 return "DW_CFA_def_cfa_offset_sf";
584 /* SGI/MIPS specific */
585 case DW_CFA_MIPS_advance_loc8:
586 return "DW_CFA_MIPS_advance_loc8";
588 /* GNU extensions */
589 case DW_CFA_GNU_window_save:
590 return "DW_CFA_GNU_window_save";
591 case DW_CFA_GNU_args_size:
592 return "DW_CFA_GNU_args_size";
593 case DW_CFA_GNU_negative_offset_extended:
594 return "DW_CFA_GNU_negative_offset_extended";
596 default:
597 return "DW_CFA_<unknown>";
601 /* Return a pointer to a newly allocated Call Frame Instruction. */
603 static inline dw_cfi_ref
604 new_cfi (void)
606 dw_cfi_ref cfi = ggc_alloc (sizeof (dw_cfi_node));
608 cfi->dw_cfi_next = NULL;
609 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
610 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
612 return cfi;
615 /* Add a Call Frame Instruction to list of instructions. */
617 static inline void
618 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
620 dw_cfi_ref *p;
622 /* Find the end of the chain. */
623 for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
626 *p = cfi;
629 /* Generate a new label for the CFI info to refer to. */
631 char *
632 dwarf2out_cfi_label (void)
634 static char label[20];
636 ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
637 ASM_OUTPUT_LABEL (asm_out_file, label);
638 return label;
641 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
642 or to the CIE if LABEL is NULL. */
644 static void
645 add_fde_cfi (const char *label, dw_cfi_ref cfi)
647 if (label)
649 dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
651 if (*label == 0)
652 label = dwarf2out_cfi_label ();
654 if (fde->dw_fde_current_label == NULL
655 || strcmp (label, fde->dw_fde_current_label) != 0)
657 dw_cfi_ref xcfi;
659 label = xstrdup (label);
661 /* Set the location counter to the new label. */
662 xcfi = new_cfi ();
663 /* If we have a current label, advance from there, otherwise
664 set the location directly using set_loc. */
665 xcfi->dw_cfi_opc = fde->dw_fde_current_label
666 ? DW_CFA_advance_loc4
667 : DW_CFA_set_loc;
668 xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
669 add_cfi (&fde->dw_fde_cfi, xcfi);
671 fde->dw_fde_current_label = label;
674 add_cfi (&fde->dw_fde_cfi, cfi);
677 else
678 add_cfi (&cie_cfi_head, cfi);
681 /* Subroutine of lookup_cfa. */
683 static void
684 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc)
686 switch (cfi->dw_cfi_opc)
688 case DW_CFA_def_cfa_offset:
689 loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
690 break;
691 case DW_CFA_def_cfa_offset_sf:
692 loc->offset
693 = cfi->dw_cfi_oprnd1.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
694 break;
695 case DW_CFA_def_cfa_register:
696 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
697 break;
698 case DW_CFA_def_cfa:
699 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
700 loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
701 break;
702 case DW_CFA_def_cfa_sf:
703 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
704 loc->offset
705 = cfi->dw_cfi_oprnd2.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
706 break;
707 case DW_CFA_def_cfa_expression:
708 get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
709 break;
710 default:
711 break;
715 /* Find the previous value for the CFA. */
717 static void
718 lookup_cfa (dw_cfa_location *loc)
720 dw_cfi_ref cfi;
722 loc->reg = INVALID_REGNUM;
723 loc->offset = 0;
724 loc->indirect = 0;
725 loc->base_offset = 0;
727 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
728 lookup_cfa_1 (cfi, loc);
730 if (fde_table_in_use)
732 dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
733 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
734 lookup_cfa_1 (cfi, loc);
738 /* The current rule for calculating the DWARF2 canonical frame address. */
739 static dw_cfa_location cfa;
741 /* The register used for saving registers to the stack, and its offset
742 from the CFA. */
743 static dw_cfa_location cfa_store;
745 /* The running total of the size of arguments pushed onto the stack. */
746 static HOST_WIDE_INT args_size;
748 /* The last args_size we actually output. */
749 static HOST_WIDE_INT old_args_size;
751 /* Entry point to update the canonical frame address (CFA).
752 LABEL is passed to add_fde_cfi. The value of CFA is now to be
753 calculated from REG+OFFSET. */
755 void
756 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
758 dw_cfa_location loc;
759 loc.indirect = 0;
760 loc.base_offset = 0;
761 loc.reg = reg;
762 loc.offset = offset;
763 def_cfa_1 (label, &loc);
766 /* Determine if two dw_cfa_location structures define the same data. */
768 static bool
769 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
771 return (loc1->reg == loc2->reg
772 && loc1->offset == loc2->offset
773 && loc1->indirect == loc2->indirect
774 && (loc1->indirect == 0
775 || loc1->base_offset == loc2->base_offset));
778 /* This routine does the actual work. The CFA is now calculated from
779 the dw_cfa_location structure. */
781 static void
782 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
784 dw_cfi_ref cfi;
785 dw_cfa_location old_cfa, loc;
787 cfa = *loc_p;
788 loc = *loc_p;
790 if (cfa_store.reg == loc.reg && loc.indirect == 0)
791 cfa_store.offset = loc.offset;
793 loc.reg = DWARF_FRAME_REGNUM (loc.reg);
794 lookup_cfa (&old_cfa);
796 /* If nothing changed, no need to issue any call frame instructions. */
797 if (cfa_equal_p (&loc, &old_cfa))
798 return;
800 cfi = new_cfi ();
802 if (loc.reg == old_cfa.reg && !loc.indirect)
804 /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
805 the CFA register did not change but the offset did. */
806 if (loc.offset < 0)
808 HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
809 gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
811 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
812 cfi->dw_cfi_oprnd1.dw_cfi_offset = f_offset;
814 else
816 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
817 cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
821 #ifndef MIPS_DEBUGGING_INFO /* SGI dbx thinks this means no offset. */
822 else if (loc.offset == old_cfa.offset
823 && old_cfa.reg != INVALID_REGNUM
824 && !loc.indirect)
826 /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
827 indicating the CFA register has changed to <register> but the
828 offset has not changed. */
829 cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
830 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
832 #endif
834 else if (loc.indirect == 0)
836 /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
837 indicating the CFA register has changed to <register> with
838 the specified offset. */
839 if (loc.offset < 0)
841 HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
842 gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
844 cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
845 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
846 cfi->dw_cfi_oprnd2.dw_cfi_offset = f_offset;
848 else
850 cfi->dw_cfi_opc = DW_CFA_def_cfa;
851 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
852 cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
855 else
857 /* Construct a DW_CFA_def_cfa_expression instruction to
858 calculate the CFA using a full location expression since no
859 register-offset pair is available. */
860 struct dw_loc_descr_struct *loc_list;
862 cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
863 loc_list = build_cfa_loc (&loc);
864 cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
867 add_fde_cfi (label, cfi);
870 /* Add the CFI for saving a register. REG is the CFA column number.
871 LABEL is passed to add_fde_cfi.
872 If SREG is -1, the register is saved at OFFSET from the CFA;
873 otherwise it is saved in SREG. */
875 static void
876 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
878 dw_cfi_ref cfi = new_cfi ();
880 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
882 if (sreg == INVALID_REGNUM)
884 if (reg & ~0x3f)
885 /* The register number won't fit in 6 bits, so we have to use
886 the long form. */
887 cfi->dw_cfi_opc = DW_CFA_offset_extended;
888 else
889 cfi->dw_cfi_opc = DW_CFA_offset;
891 #ifdef ENABLE_CHECKING
893 /* If we get an offset that is not a multiple of
894 DWARF_CIE_DATA_ALIGNMENT, there is either a bug in the
895 definition of DWARF_CIE_DATA_ALIGNMENT, or a bug in the machine
896 description. */
897 HOST_WIDE_INT check_offset = offset / DWARF_CIE_DATA_ALIGNMENT;
899 gcc_assert (check_offset * DWARF_CIE_DATA_ALIGNMENT == offset);
901 #endif
902 offset /= DWARF_CIE_DATA_ALIGNMENT;
903 if (offset < 0)
904 cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
906 cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
908 else if (sreg == reg)
909 cfi->dw_cfi_opc = DW_CFA_same_value;
910 else
912 cfi->dw_cfi_opc = DW_CFA_register;
913 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
916 add_fde_cfi (label, cfi);
919 /* Add the CFI for saving a register window. LABEL is passed to reg_save.
920 This CFI tells the unwinder that it needs to restore the window registers
921 from the previous frame's window save area.
923 ??? Perhaps we should note in the CIE where windows are saved (instead of
924 assuming 0(cfa)) and what registers are in the window. */
926 void
927 dwarf2out_window_save (const char *label)
929 dw_cfi_ref cfi = new_cfi ();
931 cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
932 add_fde_cfi (label, cfi);
935 /* Add a CFI to update the running total of the size of arguments
936 pushed onto the stack. */
938 void
939 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
941 dw_cfi_ref cfi;
943 if (size == old_args_size)
944 return;
946 old_args_size = size;
948 cfi = new_cfi ();
949 cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
950 cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
951 add_fde_cfi (label, cfi);
954 /* Entry point for saving a register to the stack. REG is the GCC register
955 number. LABEL and OFFSET are passed to reg_save. */
957 void
958 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
960 reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
963 /* Entry point for saving the return address in the stack.
964 LABEL and OFFSET are passed to reg_save. */
966 void
967 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
969 reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
972 /* Entry point for saving the return address in a register.
973 LABEL and SREG are passed to reg_save. */
975 void
976 dwarf2out_return_reg (const char *label, unsigned int sreg)
978 reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
981 /* Record the initial position of the return address. RTL is
982 INCOMING_RETURN_ADDR_RTX. */
984 static void
985 initial_return_save (rtx rtl)
987 unsigned int reg = INVALID_REGNUM;
988 HOST_WIDE_INT offset = 0;
990 switch (GET_CODE (rtl))
992 case REG:
993 /* RA is in a register. */
994 reg = DWARF_FRAME_REGNUM (REGNO (rtl));
995 break;
997 case MEM:
998 /* RA is on the stack. */
999 rtl = XEXP (rtl, 0);
1000 switch (GET_CODE (rtl))
1002 case REG:
1003 gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1004 offset = 0;
1005 break;
1007 case PLUS:
1008 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1009 offset = INTVAL (XEXP (rtl, 1));
1010 break;
1012 case MINUS:
1013 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1014 offset = -INTVAL (XEXP (rtl, 1));
1015 break;
1017 default:
1018 gcc_unreachable ();
1021 break;
1023 case PLUS:
1024 /* The return address is at some offset from any value we can
1025 actually load. For instance, on the SPARC it is in %i7+8. Just
1026 ignore the offset for now; it doesn't matter for unwinding frames. */
1027 gcc_assert (GET_CODE (XEXP (rtl, 1)) == CONST_INT);
1028 initial_return_save (XEXP (rtl, 0));
1029 return;
1031 default:
1032 gcc_unreachable ();
1035 if (reg != DWARF_FRAME_RETURN_COLUMN)
1036 reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1039 /* Given a SET, calculate the amount of stack adjustment it
1040 contains. */
1042 static HOST_WIDE_INT
1043 stack_adjust_offset (rtx pattern)
1045 rtx src = SET_SRC (pattern);
1046 rtx dest = SET_DEST (pattern);
1047 HOST_WIDE_INT offset = 0;
1048 enum rtx_code code;
1050 if (dest == stack_pointer_rtx)
1052 /* (set (reg sp) (plus (reg sp) (const_int))) */
1053 code = GET_CODE (src);
1054 if (! (code == PLUS || code == MINUS)
1055 || XEXP (src, 0) != stack_pointer_rtx
1056 || GET_CODE (XEXP (src, 1)) != CONST_INT)
1057 return 0;
1059 offset = INTVAL (XEXP (src, 1));
1060 if (code == PLUS)
1061 offset = -offset;
1063 else if (MEM_P (dest))
1065 /* (set (mem (pre_dec (reg sp))) (foo)) */
1066 src = XEXP (dest, 0);
1067 code = GET_CODE (src);
1069 switch (code)
1071 case PRE_MODIFY:
1072 case POST_MODIFY:
1073 if (XEXP (src, 0) == stack_pointer_rtx)
1075 rtx val = XEXP (XEXP (src, 1), 1);
1076 /* We handle only adjustments by constant amount. */
1077 gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1078 && GET_CODE (val) == CONST_INT);
1079 offset = -INTVAL (val);
1080 break;
1082 return 0;
1084 case PRE_DEC:
1085 case POST_DEC:
1086 if (XEXP (src, 0) == stack_pointer_rtx)
1088 offset = GET_MODE_SIZE (GET_MODE (dest));
1089 break;
1091 return 0;
1093 case PRE_INC:
1094 case POST_INC:
1095 if (XEXP (src, 0) == stack_pointer_rtx)
1097 offset = -GET_MODE_SIZE (GET_MODE (dest));
1098 break;
1100 return 0;
1102 default:
1103 return 0;
1106 else
1107 return 0;
1109 return offset;
1112 /* Check INSN to see if it looks like a push or a stack adjustment, and
1113 make a note of it if it does. EH uses this information to find out how
1114 much extra space it needs to pop off the stack. */
1116 static void
1117 dwarf2out_stack_adjust (rtx insn, bool after_p)
1119 HOST_WIDE_INT offset;
1120 const char *label;
1121 int i;
1123 /* Don't handle epilogues at all. Certainly it would be wrong to do so
1124 with this function. Proper support would require all frame-related
1125 insns to be marked, and to be able to handle saving state around
1126 epilogues textually in the middle of the function. */
1127 if (prologue_epilogue_contains (insn) || sibcall_epilogue_contains (insn))
1128 return;
1130 /* If only calls can throw, and we have a frame pointer,
1131 save up adjustments until we see the CALL_INSN. */
1132 if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1134 if (CALL_P (insn) && !after_p)
1136 /* Extract the size of the args from the CALL rtx itself. */
1137 insn = PATTERN (insn);
1138 if (GET_CODE (insn) == PARALLEL)
1139 insn = XVECEXP (insn, 0, 0);
1140 if (GET_CODE (insn) == SET)
1141 insn = SET_SRC (insn);
1142 gcc_assert (GET_CODE (insn) == CALL);
1143 dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1145 return;
1148 if (CALL_P (insn) && !after_p)
1150 if (!flag_asynchronous_unwind_tables)
1151 dwarf2out_args_size ("", args_size);
1152 return;
1154 else if (BARRIER_P (insn))
1156 /* When we see a BARRIER, we know to reset args_size to 0. Usually
1157 the compiler will have already emitted a stack adjustment, but
1158 doesn't bother for calls to noreturn functions. */
1159 #ifdef STACK_GROWS_DOWNWARD
1160 offset = -args_size;
1161 #else
1162 offset = args_size;
1163 #endif
1165 else if (GET_CODE (PATTERN (insn)) == SET)
1166 offset = stack_adjust_offset (PATTERN (insn));
1167 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1168 || GET_CODE (PATTERN (insn)) == SEQUENCE)
1170 /* There may be stack adjustments inside compound insns. Search
1171 for them. */
1172 for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1173 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1174 offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i));
1176 else
1177 return;
1179 if (offset == 0)
1180 return;
1182 if (cfa.reg == STACK_POINTER_REGNUM)
1183 cfa.offset += offset;
1185 #ifndef STACK_GROWS_DOWNWARD
1186 offset = -offset;
1187 #endif
1189 args_size += offset;
1190 if (args_size < 0)
1191 args_size = 0;
1193 label = dwarf2out_cfi_label ();
1194 def_cfa_1 (label, &cfa);
1195 if (flag_asynchronous_unwind_tables)
1196 dwarf2out_args_size (label, args_size);
1199 #endif
1201 /* We delay emitting a register save until either (a) we reach the end
1202 of the prologue or (b) the register is clobbered. This clusters
1203 register saves so that there are fewer pc advances. */
1205 struct queued_reg_save GTY(())
1207 struct queued_reg_save *next;
1208 rtx reg;
1209 HOST_WIDE_INT cfa_offset;
1210 rtx saved_reg;
1213 static GTY(()) struct queued_reg_save *queued_reg_saves;
1215 /* The caller's ORIG_REG is saved in SAVED_IN_REG. */
1216 struct reg_saved_in_data GTY(()) {
1217 rtx orig_reg;
1218 rtx saved_in_reg;
1221 /* A list of registers saved in other registers.
1222 The list intentionally has a small maximum capacity of 4; if your
1223 port needs more than that, you might consider implementing a
1224 more efficient data structure. */
1225 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1226 static GTY(()) size_t num_regs_saved_in_regs;
1228 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1229 static const char *last_reg_save_label;
1231 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1232 SREG, or if SREG is NULL then it is saved at OFFSET to the CFA. */
1234 static void
1235 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1237 struct queued_reg_save *q;
1239 /* Duplicates waste space, but it's also necessary to remove them
1240 for correctness, since the queue gets output in reverse
1241 order. */
1242 for (q = queued_reg_saves; q != NULL; q = q->next)
1243 if (REGNO (q->reg) == REGNO (reg))
1244 break;
1246 if (q == NULL)
1248 q = ggc_alloc (sizeof (*q));
1249 q->next = queued_reg_saves;
1250 queued_reg_saves = q;
1253 q->reg = reg;
1254 q->cfa_offset = offset;
1255 q->saved_reg = sreg;
1257 last_reg_save_label = label;
1260 /* Output all the entries in QUEUED_REG_SAVES. */
1262 static void
1263 flush_queued_reg_saves (void)
1265 struct queued_reg_save *q;
1267 for (q = queued_reg_saves; q; q = q->next)
1269 size_t i;
1270 unsigned int reg, sreg;
1272 for (i = 0; i < num_regs_saved_in_regs; i++)
1273 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1274 break;
1275 if (q->saved_reg && i == num_regs_saved_in_regs)
1277 gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1278 num_regs_saved_in_regs++;
1280 if (i != num_regs_saved_in_regs)
1282 regs_saved_in_regs[i].orig_reg = q->reg;
1283 regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1286 reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1287 if (q->saved_reg)
1288 sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1289 else
1290 sreg = INVALID_REGNUM;
1291 reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1294 queued_reg_saves = NULL;
1295 last_reg_save_label = NULL;
1298 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1299 location for? Or, does it clobber a register which we've previously
1300 said that some other register is saved in, and for which we now
1301 have a new location for? */
1303 static bool
1304 clobbers_queued_reg_save (rtx insn)
1306 struct queued_reg_save *q;
1308 for (q = queued_reg_saves; q; q = q->next)
1310 size_t i;
1311 if (modified_in_p (q->reg, insn))
1312 return true;
1313 for (i = 0; i < num_regs_saved_in_regs; i++)
1314 if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1315 && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1316 return true;
1319 return false;
1322 /* Entry point for saving the first register into the second. */
1324 void
1325 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1327 size_t i;
1328 unsigned int regno, sregno;
1330 for (i = 0; i < num_regs_saved_in_regs; i++)
1331 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1332 break;
1333 if (i == num_regs_saved_in_regs)
1335 gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1336 num_regs_saved_in_regs++;
1338 regs_saved_in_regs[i].orig_reg = reg;
1339 regs_saved_in_regs[i].saved_in_reg = sreg;
1341 regno = DWARF_FRAME_REGNUM (REGNO (reg));
1342 sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1343 reg_save (label, regno, sregno, 0);
1346 /* What register, if any, is currently saved in REG? */
1348 static rtx
1349 reg_saved_in (rtx reg)
1351 unsigned int regn = REGNO (reg);
1352 size_t i;
1353 struct queued_reg_save *q;
1355 for (q = queued_reg_saves; q; q = q->next)
1356 if (q->saved_reg && regn == REGNO (q->saved_reg))
1357 return q->reg;
1359 for (i = 0; i < num_regs_saved_in_regs; i++)
1360 if (regs_saved_in_regs[i].saved_in_reg
1361 && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1362 return regs_saved_in_regs[i].orig_reg;
1364 return NULL_RTX;
1368 /* A temporary register holding an integral value used in adjusting SP
1369 or setting up the store_reg. The "offset" field holds the integer
1370 value, not an offset. */
1371 static dw_cfa_location cfa_temp;
1373 /* Record call frame debugging information for an expression EXPR,
1374 which either sets SP or FP (adjusting how we calculate the frame
1375 address) or saves a register to the stack or another register.
1376 LABEL indicates the address of EXPR.
1378 This function encodes a state machine mapping rtxes to actions on
1379 cfa, cfa_store, and cfa_temp.reg. We describe these rules so
1380 users need not read the source code.
1382 The High-Level Picture
1384 Changes in the register we use to calculate the CFA: Currently we
1385 assume that if you copy the CFA register into another register, we
1386 should take the other one as the new CFA register; this seems to
1387 work pretty well. If it's wrong for some target, it's simple
1388 enough not to set RTX_FRAME_RELATED_P on the insn in question.
1390 Changes in the register we use for saving registers to the stack:
1391 This is usually SP, but not always. Again, we deduce that if you
1392 copy SP into another register (and SP is not the CFA register),
1393 then the new register is the one we will be using for register
1394 saves. This also seems to work.
1396 Register saves: There's not much guesswork about this one; if
1397 RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1398 register save, and the register used to calculate the destination
1399 had better be the one we think we're using for this purpose.
1400 It's also assumed that a copy from a call-saved register to another
1401 register is saving that register if RTX_FRAME_RELATED_P is set on
1402 that instruction. If the copy is from a call-saved register to
1403 the *same* register, that means that the register is now the same
1404 value as in the caller.
1406 Except: If the register being saved is the CFA register, and the
1407 offset is nonzero, we are saving the CFA, so we assume we have to
1408 use DW_CFA_def_cfa_expression. If the offset is 0, we assume that
1409 the intent is to save the value of SP from the previous frame.
1411 In addition, if a register has previously been saved to a different
1412 register,
1414 Invariants / Summaries of Rules
1416 cfa current rule for calculating the CFA. It usually
1417 consists of a register and an offset.
1418 cfa_store register used by prologue code to save things to the stack
1419 cfa_store.offset is the offset from the value of
1420 cfa_store.reg to the actual CFA
1421 cfa_temp register holding an integral value. cfa_temp.offset
1422 stores the value, which will be used to adjust the
1423 stack pointer. cfa_temp is also used like cfa_store,
1424 to track stores to the stack via fp or a temp reg.
1426 Rules 1- 4: Setting a register's value to cfa.reg or an expression
1427 with cfa.reg as the first operand changes the cfa.reg and its
1428 cfa.offset. Rule 1 and 4 also set cfa_temp.reg and
1429 cfa_temp.offset.
1431 Rules 6- 9: Set a non-cfa.reg register value to a constant or an
1432 expression yielding a constant. This sets cfa_temp.reg
1433 and cfa_temp.offset.
1435 Rule 5: Create a new register cfa_store used to save items to the
1436 stack.
1438 Rules 10-14: Save a register to the stack. Define offset as the
1439 difference of the original location and cfa_store's
1440 location (or cfa_temp's location if cfa_temp is used).
1442 The Rules
1444 "{a,b}" indicates a choice of a xor b.
1445 "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1447 Rule 1:
1448 (set <reg1> <reg2>:cfa.reg)
1449 effects: cfa.reg = <reg1>
1450 cfa.offset unchanged
1451 cfa_temp.reg = <reg1>
1452 cfa_temp.offset = cfa.offset
1454 Rule 2:
1455 (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1456 {<const_int>,<reg>:cfa_temp.reg}))
1457 effects: cfa.reg = sp if fp used
1458 cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
1459 cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1460 if cfa_store.reg==sp
1462 Rule 3:
1463 (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
1464 effects: cfa.reg = fp
1465 cfa_offset += +/- <const_int>
1467 Rule 4:
1468 (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
1469 constraints: <reg1> != fp
1470 <reg1> != sp
1471 effects: cfa.reg = <reg1>
1472 cfa_temp.reg = <reg1>
1473 cfa_temp.offset = cfa.offset
1475 Rule 5:
1476 (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1477 constraints: <reg1> != fp
1478 <reg1> != sp
1479 effects: cfa_store.reg = <reg1>
1480 cfa_store.offset = cfa.offset - cfa_temp.offset
1482 Rule 6:
1483 (set <reg> <const_int>)
1484 effects: cfa_temp.reg = <reg>
1485 cfa_temp.offset = <const_int>
1487 Rule 7:
1488 (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1489 effects: cfa_temp.reg = <reg1>
1490 cfa_temp.offset |= <const_int>
1492 Rule 8:
1493 (set <reg> (high <exp>))
1494 effects: none
1496 Rule 9:
1497 (set <reg> (lo_sum <exp> <const_int>))
1498 effects: cfa_temp.reg = <reg>
1499 cfa_temp.offset = <const_int>
1501 Rule 10:
1502 (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1503 effects: cfa_store.offset -= <const_int>
1504 cfa.offset = cfa_store.offset if cfa.reg == sp
1505 cfa.reg = sp
1506 cfa.base_offset = -cfa_store.offset
1508 Rule 11:
1509 (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1510 effects: cfa_store.offset += -/+ mode_size(mem)
1511 cfa.offset = cfa_store.offset if cfa.reg == sp
1512 cfa.reg = sp
1513 cfa.base_offset = -cfa_store.offset
1515 Rule 12:
1516 (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1518 <reg2>)
1519 effects: cfa.reg = <reg1>
1520 cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
1522 Rule 13:
1523 (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1524 effects: cfa.reg = <reg1>
1525 cfa.base_offset = -{cfa_store,cfa_temp}.offset
1527 Rule 14:
1528 (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
1529 effects: cfa.reg = <reg1>
1530 cfa.base_offset = -cfa_temp.offset
1531 cfa_temp.offset -= mode_size(mem)
1533 Rule 15:
1534 (set <reg> {unspec, unspec_volatile})
1535 effects: target-dependent */
1537 static void
1538 dwarf2out_frame_debug_expr (rtx expr, const char *label)
1540 rtx src, dest;
1541 HOST_WIDE_INT offset;
1543 /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1544 the PARALLEL independently. The first element is always processed if
1545 it is a SET. This is for backward compatibility. Other elements
1546 are processed only if they are SETs and the RTX_FRAME_RELATED_P
1547 flag is set in them. */
1548 if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
1550 int par_index;
1551 int limit = XVECLEN (expr, 0);
1553 for (par_index = 0; par_index < limit; par_index++)
1554 if (GET_CODE (XVECEXP (expr, 0, par_index)) == SET
1555 && (RTX_FRAME_RELATED_P (XVECEXP (expr, 0, par_index))
1556 || par_index == 0))
1557 dwarf2out_frame_debug_expr (XVECEXP (expr, 0, par_index), label);
1559 return;
1562 gcc_assert (GET_CODE (expr) == SET);
1564 src = SET_SRC (expr);
1565 dest = SET_DEST (expr);
1567 if (REG_P (src))
1569 rtx rsi = reg_saved_in (src);
1570 if (rsi)
1571 src = rsi;
1574 switch (GET_CODE (dest))
1576 case REG:
1577 switch (GET_CODE (src))
1579 /* Setting FP from SP. */
1580 case REG:
1581 if (cfa.reg == (unsigned) REGNO (src))
1583 /* Rule 1 */
1584 /* Update the CFA rule wrt SP or FP. Make sure src is
1585 relative to the current CFA register.
1587 We used to require that dest be either SP or FP, but the
1588 ARM copies SP to a temporary register, and from there to
1589 FP. So we just rely on the backends to only set
1590 RTX_FRAME_RELATED_P on appropriate insns. */
1591 cfa.reg = REGNO (dest);
1592 cfa_temp.reg = cfa.reg;
1593 cfa_temp.offset = cfa.offset;
1595 else
1597 /* Saving a register in a register. */
1598 gcc_assert (!fixed_regs [REGNO (dest)]
1599 /* For the SPARC and its register window. */
1600 || (DWARF_FRAME_REGNUM (REGNO (src))
1601 == DWARF_FRAME_RETURN_COLUMN));
1602 queue_reg_save (label, src, dest, 0);
1604 break;
1606 case PLUS:
1607 case MINUS:
1608 case LO_SUM:
1609 if (dest == stack_pointer_rtx)
1611 /* Rule 2 */
1612 /* Adjusting SP. */
1613 switch (GET_CODE (XEXP (src, 1)))
1615 case CONST_INT:
1616 offset = INTVAL (XEXP (src, 1));
1617 break;
1618 case REG:
1619 gcc_assert ((unsigned) REGNO (XEXP (src, 1))
1620 == cfa_temp.reg);
1621 offset = cfa_temp.offset;
1622 break;
1623 default:
1624 gcc_unreachable ();
1627 if (XEXP (src, 0) == hard_frame_pointer_rtx)
1629 /* Restoring SP from FP in the epilogue. */
1630 gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
1631 cfa.reg = STACK_POINTER_REGNUM;
1633 else if (GET_CODE (src) == LO_SUM)
1634 /* Assume we've set the source reg of the LO_SUM from sp. */
1636 else
1637 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
1639 if (GET_CODE (src) != MINUS)
1640 offset = -offset;
1641 if (cfa.reg == STACK_POINTER_REGNUM)
1642 cfa.offset += offset;
1643 if (cfa_store.reg == STACK_POINTER_REGNUM)
1644 cfa_store.offset += offset;
1646 else if (dest == hard_frame_pointer_rtx)
1648 /* Rule 3 */
1649 /* Either setting the FP from an offset of the SP,
1650 or adjusting the FP */
1651 gcc_assert (frame_pointer_needed);
1653 gcc_assert (REG_P (XEXP (src, 0))
1654 && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
1655 && GET_CODE (XEXP (src, 1)) == CONST_INT);
1656 offset = INTVAL (XEXP (src, 1));
1657 if (GET_CODE (src) != MINUS)
1658 offset = -offset;
1659 cfa.offset += offset;
1660 cfa.reg = HARD_FRAME_POINTER_REGNUM;
1662 else
1664 gcc_assert (GET_CODE (src) != MINUS);
1666 /* Rule 4 */
1667 if (REG_P (XEXP (src, 0))
1668 && REGNO (XEXP (src, 0)) == cfa.reg
1669 && GET_CODE (XEXP (src, 1)) == CONST_INT)
1671 /* Setting a temporary CFA register that will be copied
1672 into the FP later on. */
1673 offset = - INTVAL (XEXP (src, 1));
1674 cfa.offset += offset;
1675 cfa.reg = REGNO (dest);
1676 /* Or used to save regs to the stack. */
1677 cfa_temp.reg = cfa.reg;
1678 cfa_temp.offset = cfa.offset;
1681 /* Rule 5 */
1682 else if (REG_P (XEXP (src, 0))
1683 && REGNO (XEXP (src, 0)) == cfa_temp.reg
1684 && XEXP (src, 1) == stack_pointer_rtx)
1686 /* Setting a scratch register that we will use instead
1687 of SP for saving registers to the stack. */
1688 gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
1689 cfa_store.reg = REGNO (dest);
1690 cfa_store.offset = cfa.offset - cfa_temp.offset;
1693 /* Rule 9 */
1694 else if (GET_CODE (src) == LO_SUM
1695 && GET_CODE (XEXP (src, 1)) == CONST_INT)
1697 cfa_temp.reg = REGNO (dest);
1698 cfa_temp.offset = INTVAL (XEXP (src, 1));
1700 else
1701 gcc_unreachable ();
1703 break;
1705 /* Rule 6 */
1706 case CONST_INT:
1707 cfa_temp.reg = REGNO (dest);
1708 cfa_temp.offset = INTVAL (src);
1709 break;
1711 /* Rule 7 */
1712 case IOR:
1713 gcc_assert (REG_P (XEXP (src, 0))
1714 && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
1715 && GET_CODE (XEXP (src, 1)) == CONST_INT);
1717 if ((unsigned) REGNO (dest) != cfa_temp.reg)
1718 cfa_temp.reg = REGNO (dest);
1719 cfa_temp.offset |= INTVAL (XEXP (src, 1));
1720 break;
1722 /* Skip over HIGH, assuming it will be followed by a LO_SUM,
1723 which will fill in all of the bits. */
1724 /* Rule 8 */
1725 case HIGH:
1726 break;
1728 /* Rule 15 */
1729 case UNSPEC:
1730 case UNSPEC_VOLATILE:
1731 gcc_assert (targetm.dwarf_handle_frame_unspec);
1732 targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
1733 return;
1735 default:
1736 gcc_unreachable ();
1739 def_cfa_1 (label, &cfa);
1740 break;
1742 case MEM:
1743 gcc_assert (REG_P (src));
1745 /* Saving a register to the stack. Make sure dest is relative to the
1746 CFA register. */
1747 switch (GET_CODE (XEXP (dest, 0)))
1749 /* Rule 10 */
1750 /* With a push. */
1751 case PRE_MODIFY:
1752 /* We can't handle variable size modifications. */
1753 gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
1754 == CONST_INT);
1755 offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
1757 gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1758 && cfa_store.reg == STACK_POINTER_REGNUM);
1760 cfa_store.offset += offset;
1761 if (cfa.reg == STACK_POINTER_REGNUM)
1762 cfa.offset = cfa_store.offset;
1764 offset = -cfa_store.offset;
1765 break;
1767 /* Rule 11 */
1768 case PRE_INC:
1769 case PRE_DEC:
1770 offset = GET_MODE_SIZE (GET_MODE (dest));
1771 if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
1772 offset = -offset;
1774 gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1775 && cfa_store.reg == STACK_POINTER_REGNUM);
1777 cfa_store.offset += offset;
1778 if (cfa.reg == STACK_POINTER_REGNUM)
1779 cfa.offset = cfa_store.offset;
1781 offset = -cfa_store.offset;
1782 break;
1784 /* Rule 12 */
1785 /* With an offset. */
1786 case PLUS:
1787 case MINUS:
1788 case LO_SUM:
1790 int regno;
1792 gcc_assert (GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT);
1793 offset = INTVAL (XEXP (XEXP (dest, 0), 1));
1794 if (GET_CODE (XEXP (dest, 0)) == MINUS)
1795 offset = -offset;
1797 regno = REGNO (XEXP (XEXP (dest, 0), 0));
1799 if (cfa_store.reg == (unsigned) regno)
1800 offset -= cfa_store.offset;
1801 else
1803 gcc_assert (cfa_temp.reg == (unsigned) regno);
1804 offset -= cfa_temp.offset;
1807 break;
1809 /* Rule 13 */
1810 /* Without an offset. */
1811 case REG:
1813 int regno = REGNO (XEXP (dest, 0));
1815 if (cfa_store.reg == (unsigned) regno)
1816 offset = -cfa_store.offset;
1817 else
1819 gcc_assert (cfa_temp.reg == (unsigned) regno);
1820 offset = -cfa_temp.offset;
1823 break;
1825 /* Rule 14 */
1826 case POST_INC:
1827 gcc_assert (cfa_temp.reg
1828 == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
1829 offset = -cfa_temp.offset;
1830 cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
1831 break;
1833 default:
1834 gcc_unreachable ();
1837 if (REGNO (src) != STACK_POINTER_REGNUM
1838 && REGNO (src) != HARD_FRAME_POINTER_REGNUM
1839 && (unsigned) REGNO (src) == cfa.reg)
1841 /* We're storing the current CFA reg into the stack. */
1843 if (cfa.offset == 0)
1845 /* If the source register is exactly the CFA, assume
1846 we're saving SP like any other register; this happens
1847 on the ARM. */
1848 def_cfa_1 (label, &cfa);
1849 queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
1850 break;
1852 else
1854 /* Otherwise, we'll need to look in the stack to
1855 calculate the CFA. */
1856 rtx x = XEXP (dest, 0);
1858 if (!REG_P (x))
1859 x = XEXP (x, 0);
1860 gcc_assert (REG_P (x));
1862 cfa.reg = REGNO (x);
1863 cfa.base_offset = offset;
1864 cfa.indirect = 1;
1865 def_cfa_1 (label, &cfa);
1866 break;
1870 def_cfa_1 (label, &cfa);
1871 queue_reg_save (label, src, NULL_RTX, offset);
1872 break;
1874 default:
1875 gcc_unreachable ();
1879 /* Record call frame debugging information for INSN, which either
1880 sets SP or FP (adjusting how we calculate the frame address) or saves a
1881 register to the stack. If INSN is NULL_RTX, initialize our state.
1883 If AFTER_P is false, we're being called before the insn is emitted,
1884 otherwise after. Call instructions get invoked twice. */
1886 void
1887 dwarf2out_frame_debug (rtx insn, bool after_p)
1889 const char *label;
1890 rtx src;
1892 if (insn == NULL_RTX)
1894 size_t i;
1896 /* Flush any queued register saves. */
1897 flush_queued_reg_saves ();
1899 /* Set up state for generating call frame debug info. */
1900 lookup_cfa (&cfa);
1901 gcc_assert (cfa.reg
1902 == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
1904 cfa.reg = STACK_POINTER_REGNUM;
1905 cfa_store = cfa;
1906 cfa_temp.reg = -1;
1907 cfa_temp.offset = 0;
1909 for (i = 0; i < num_regs_saved_in_regs; i++)
1911 regs_saved_in_regs[i].orig_reg = NULL_RTX;
1912 regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
1914 num_regs_saved_in_regs = 0;
1915 return;
1918 if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
1919 flush_queued_reg_saves ();
1921 if (! RTX_FRAME_RELATED_P (insn))
1923 if (!ACCUMULATE_OUTGOING_ARGS)
1924 dwarf2out_stack_adjust (insn, after_p);
1925 return;
1928 label = dwarf2out_cfi_label ();
1929 src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1930 if (src)
1931 insn = XEXP (src, 0);
1932 else
1933 insn = PATTERN (insn);
1935 dwarf2out_frame_debug_expr (insn, label);
1938 #endif
1940 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
1941 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
1942 (enum dwarf_call_frame_info cfi);
1944 static enum dw_cfi_oprnd_type
1945 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
1947 switch (cfi)
1949 case DW_CFA_nop:
1950 case DW_CFA_GNU_window_save:
1951 return dw_cfi_oprnd_unused;
1953 case DW_CFA_set_loc:
1954 case DW_CFA_advance_loc1:
1955 case DW_CFA_advance_loc2:
1956 case DW_CFA_advance_loc4:
1957 case DW_CFA_MIPS_advance_loc8:
1958 return dw_cfi_oprnd_addr;
1960 case DW_CFA_offset:
1961 case DW_CFA_offset_extended:
1962 case DW_CFA_def_cfa:
1963 case DW_CFA_offset_extended_sf:
1964 case DW_CFA_def_cfa_sf:
1965 case DW_CFA_restore_extended:
1966 case DW_CFA_undefined:
1967 case DW_CFA_same_value:
1968 case DW_CFA_def_cfa_register:
1969 case DW_CFA_register:
1970 return dw_cfi_oprnd_reg_num;
1972 case DW_CFA_def_cfa_offset:
1973 case DW_CFA_GNU_args_size:
1974 case DW_CFA_def_cfa_offset_sf:
1975 return dw_cfi_oprnd_offset;
1977 case DW_CFA_def_cfa_expression:
1978 case DW_CFA_expression:
1979 return dw_cfi_oprnd_loc;
1981 default:
1982 gcc_unreachable ();
1986 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
1987 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
1988 (enum dwarf_call_frame_info cfi);
1990 static enum dw_cfi_oprnd_type
1991 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
1993 switch (cfi)
1995 case DW_CFA_def_cfa:
1996 case DW_CFA_def_cfa_sf:
1997 case DW_CFA_offset:
1998 case DW_CFA_offset_extended_sf:
1999 case DW_CFA_offset_extended:
2000 return dw_cfi_oprnd_offset;
2002 case DW_CFA_register:
2003 return dw_cfi_oprnd_reg_num;
2005 default:
2006 return dw_cfi_oprnd_unused;
2010 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2012 /* Map register numbers held in the call frame info that gcc has
2013 collected using DWARF_FRAME_REGNUM to those that should be output in
2014 .debug_frame and .eh_frame. */
2015 #ifndef DWARF2_FRAME_REG_OUT
2016 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
2017 #endif
2019 /* Output a Call Frame Information opcode and its operand(s). */
2021 static void
2022 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
2024 unsigned long r;
2025 if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
2026 dw2_asm_output_data (1, (cfi->dw_cfi_opc
2027 | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
2028 "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
2029 cfi->dw_cfi_oprnd1.dw_cfi_offset);
2030 else if (cfi->dw_cfi_opc == DW_CFA_offset)
2032 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2033 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2034 "DW_CFA_offset, column 0x%lx", r);
2035 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2037 else if (cfi->dw_cfi_opc == DW_CFA_restore)
2039 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2040 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2041 "DW_CFA_restore, column 0x%lx", r);
2043 else
2045 dw2_asm_output_data (1, cfi->dw_cfi_opc,
2046 "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
2048 switch (cfi->dw_cfi_opc)
2050 case DW_CFA_set_loc:
2051 if (for_eh)
2052 dw2_asm_output_encoded_addr_rtx (
2053 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
2054 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
2055 false, NULL);
2056 else
2057 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2058 cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
2059 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2060 break;
2062 case DW_CFA_advance_loc1:
2063 dw2_asm_output_delta (1, 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_advance_loc2:
2069 dw2_asm_output_delta (2, 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_advance_loc4:
2075 dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2076 fde->dw_fde_current_label, NULL);
2077 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2078 break;
2080 case DW_CFA_MIPS_advance_loc8:
2081 dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2082 fde->dw_fde_current_label, NULL);
2083 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2084 break;
2086 case DW_CFA_offset_extended:
2087 case DW_CFA_def_cfa:
2088 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2089 dw2_asm_output_data_uleb128 (r, NULL);
2090 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2091 break;
2093 case DW_CFA_offset_extended_sf:
2094 case DW_CFA_def_cfa_sf:
2095 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2096 dw2_asm_output_data_uleb128 (r, NULL);
2097 dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2098 break;
2100 case DW_CFA_restore_extended:
2101 case DW_CFA_undefined:
2102 case DW_CFA_same_value:
2103 case DW_CFA_def_cfa_register:
2104 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2105 dw2_asm_output_data_uleb128 (r, NULL);
2106 break;
2108 case DW_CFA_register:
2109 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2110 dw2_asm_output_data_uleb128 (r, NULL);
2111 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
2112 dw2_asm_output_data_uleb128 (r, NULL);
2113 break;
2115 case DW_CFA_def_cfa_offset:
2116 case DW_CFA_GNU_args_size:
2117 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2118 break;
2120 case DW_CFA_def_cfa_offset_sf:
2121 dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2122 break;
2124 case DW_CFA_GNU_window_save:
2125 break;
2127 case DW_CFA_def_cfa_expression:
2128 case DW_CFA_expression:
2129 output_cfa_loc (cfi);
2130 break;
2132 case DW_CFA_GNU_negative_offset_extended:
2133 /* Obsoleted by DW_CFA_offset_extended_sf. */
2134 gcc_unreachable ();
2136 default:
2137 break;
2142 /* Output the call frame information used to record information
2143 that relates to calculating the frame pointer, and records the
2144 location of saved registers. */
2146 static void
2147 output_call_frame_info (int for_eh)
2149 unsigned int i;
2150 dw_fde_ref fde;
2151 dw_cfi_ref cfi;
2152 char l1[20], l2[20], section_start_label[20];
2153 bool any_lsda_needed = false;
2154 char augmentation[6];
2155 int augmentation_size;
2156 int fde_encoding = DW_EH_PE_absptr;
2157 int per_encoding = DW_EH_PE_absptr;
2158 int lsda_encoding = DW_EH_PE_absptr;
2159 int return_reg;
2161 /* Don't emit a CIE if there won't be any FDEs. */
2162 if (fde_table_in_use == 0)
2163 return;
2165 /* If we make FDEs linkonce, we may have to emit an empty label for
2166 an FDE that wouldn't otherwise be emitted. We want to avoid
2167 having an FDE kept around when the function it refers to is
2168 discarded. Example where this matters: a primary function
2169 template in C++ requires EH information, but an explicit
2170 specialization doesn't. */
2171 if (TARGET_USES_WEAK_UNWIND_INFO
2172 && ! flag_asynchronous_unwind_tables
2173 && for_eh)
2174 for (i = 0; i < fde_table_in_use; i++)
2175 if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
2176 && !fde_table[i].uses_eh_lsda
2177 && ! DECL_WEAK (fde_table[i].decl))
2178 targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
2179 for_eh, /* empty */ 1);
2181 /* If we don't have any functions we'll want to unwind out of, don't
2182 emit any EH unwind information. Note that if exceptions aren't
2183 enabled, we won't have collected nothrow information, and if we
2184 asked for asynchronous tables, we always want this info. */
2185 if (for_eh)
2187 bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
2189 for (i = 0; i < fde_table_in_use; i++)
2190 if (fde_table[i].uses_eh_lsda)
2191 any_eh_needed = any_lsda_needed = true;
2192 else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2193 any_eh_needed = true;
2194 else if (! fde_table[i].nothrow
2195 && ! fde_table[i].all_throwers_are_sibcalls)
2196 any_eh_needed = true;
2198 if (! any_eh_needed)
2199 return;
2202 /* We're going to be generating comments, so turn on app. */
2203 if (flag_debug_asm)
2204 app_enable ();
2206 if (for_eh)
2207 targetm.asm_out.eh_frame_section ();
2208 else
2209 named_section_flags (DEBUG_FRAME_SECTION, SECTION_DEBUG);
2211 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
2212 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
2214 /* Output the CIE. */
2215 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
2216 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
2217 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2218 "Length of Common Information Entry");
2219 ASM_OUTPUT_LABEL (asm_out_file, l1);
2221 /* Now that the CIE pointer is PC-relative for EH,
2222 use 0 to identify the CIE. */
2223 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
2224 (for_eh ? 0 : DW_CIE_ID),
2225 "CIE Identifier Tag");
2227 dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
2229 augmentation[0] = 0;
2230 augmentation_size = 0;
2231 if (for_eh)
2233 char *p;
2235 /* Augmentation:
2236 z Indicates that a uleb128 is present to size the
2237 augmentation section.
2238 L Indicates the encoding (and thus presence) of
2239 an LSDA pointer in the FDE augmentation.
2240 R Indicates a non-default pointer encoding for
2241 FDE code pointers.
2242 P Indicates the presence of an encoding + language
2243 personality routine in the CIE augmentation. */
2245 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
2246 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
2247 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
2249 p = augmentation + 1;
2250 if (eh_personality_libfunc)
2252 *p++ = 'P';
2253 augmentation_size += 1 + size_of_encoded_value (per_encoding);
2255 if (any_lsda_needed)
2257 *p++ = 'L';
2258 augmentation_size += 1;
2260 if (fde_encoding != DW_EH_PE_absptr)
2262 *p++ = 'R';
2263 augmentation_size += 1;
2265 if (p > augmentation + 1)
2267 augmentation[0] = 'z';
2268 *p = '\0';
2271 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
2272 if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
2274 int offset = ( 4 /* Length */
2275 + 4 /* CIE Id */
2276 + 1 /* CIE version */
2277 + strlen (augmentation) + 1 /* Augmentation */
2278 + size_of_uleb128 (1) /* Code alignment */
2279 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
2280 + 1 /* RA column */
2281 + 1 /* Augmentation size */
2282 + 1 /* Personality encoding */ );
2283 int pad = -offset & (PTR_SIZE - 1);
2285 augmentation_size += pad;
2287 /* Augmentations should be small, so there's scarce need to
2288 iterate for a solution. Die if we exceed one uleb128 byte. */
2289 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
2293 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
2294 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
2295 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
2296 "CIE Data Alignment Factor");
2298 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
2299 if (DW_CIE_VERSION == 1)
2300 dw2_asm_output_data (1, return_reg, "CIE RA Column");
2301 else
2302 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
2304 if (augmentation[0])
2306 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
2307 if (eh_personality_libfunc)
2309 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
2310 eh_data_format_name (per_encoding));
2311 dw2_asm_output_encoded_addr_rtx (per_encoding,
2312 eh_personality_libfunc,
2313 true, NULL);
2316 if (any_lsda_needed)
2317 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
2318 eh_data_format_name (lsda_encoding));
2320 if (fde_encoding != DW_EH_PE_absptr)
2321 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
2322 eh_data_format_name (fde_encoding));
2325 for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
2326 output_cfi (cfi, NULL, for_eh);
2328 /* Pad the CIE out to an address sized boundary. */
2329 ASM_OUTPUT_ALIGN (asm_out_file,
2330 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
2331 ASM_OUTPUT_LABEL (asm_out_file, l2);
2333 /* Loop through all of the FDE's. */
2334 for (i = 0; i < fde_table_in_use; i++)
2336 fde = &fde_table[i];
2338 /* Don't emit EH unwind info for leaf functions that don't need it. */
2339 if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
2340 && (fde->nothrow || fde->all_throwers_are_sibcalls)
2341 && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2342 && !fde->uses_eh_lsda)
2343 continue;
2345 targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
2346 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
2347 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
2348 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
2349 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2350 "FDE Length");
2351 ASM_OUTPUT_LABEL (asm_out_file, l1);
2353 if (for_eh)
2354 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
2355 else
2356 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
2357 "FDE CIE offset");
2359 if (for_eh)
2361 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
2362 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
2363 dw2_asm_output_encoded_addr_rtx (fde_encoding,
2364 sym_ref,
2365 false,
2366 "FDE initial location");
2367 if (fde->dw_fde_switched_sections)
2369 rtx sym_ref2 = gen_rtx_SYMBOL_REF (Pmode,
2370 fde->dw_fde_unlikely_section_label);
2371 rtx sym_ref3= gen_rtx_SYMBOL_REF (Pmode,
2372 fde->dw_fde_hot_section_label);
2373 SYMBOL_REF_FLAGS (sym_ref2) |= SYMBOL_FLAG_LOCAL;
2374 SYMBOL_REF_FLAGS (sym_ref3) |= SYMBOL_FLAG_LOCAL;
2375 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref3, false,
2376 "FDE initial location");
2377 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2378 fde->dw_fde_hot_section_end_label,
2379 fde->dw_fde_hot_section_label,
2380 "FDE address range");
2381 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref2, false,
2382 "FDE initial location");
2383 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2384 fde->dw_fde_unlikely_section_end_label,
2385 fde->dw_fde_unlikely_section_label,
2386 "FDE address range");
2388 else
2389 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2390 fde->dw_fde_end, fde->dw_fde_begin,
2391 "FDE address range");
2393 else
2395 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
2396 "FDE initial location");
2397 if (fde->dw_fde_switched_sections)
2399 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2400 fde->dw_fde_hot_section_label,
2401 "FDE initial location");
2402 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2403 fde->dw_fde_hot_section_end_label,
2404 fde->dw_fde_hot_section_label,
2405 "FDE address range");
2406 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2407 fde->dw_fde_unlikely_section_label,
2408 "FDE initial location");
2409 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2410 fde->dw_fde_unlikely_section_end_label,
2411 fde->dw_fde_unlikely_section_label,
2412 "FDE address range");
2414 else
2415 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2416 fde->dw_fde_end, fde->dw_fde_begin,
2417 "FDE address range");
2420 if (augmentation[0])
2422 if (any_lsda_needed)
2424 int size = size_of_encoded_value (lsda_encoding);
2426 if (lsda_encoding == DW_EH_PE_aligned)
2428 int offset = ( 4 /* Length */
2429 + 4 /* CIE offset */
2430 + 2 * size_of_encoded_value (fde_encoding)
2431 + 1 /* Augmentation size */ );
2432 int pad = -offset & (PTR_SIZE - 1);
2434 size += pad;
2435 gcc_assert (size_of_uleb128 (size) == 1);
2438 dw2_asm_output_data_uleb128 (size, "Augmentation size");
2440 if (fde->uses_eh_lsda)
2442 ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
2443 fde->funcdef_number);
2444 dw2_asm_output_encoded_addr_rtx (
2445 lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
2446 false, "Language Specific Data Area");
2448 else
2450 if (lsda_encoding == DW_EH_PE_aligned)
2451 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2452 dw2_asm_output_data
2453 (size_of_encoded_value (lsda_encoding), 0,
2454 "Language Specific Data Area (none)");
2457 else
2458 dw2_asm_output_data_uleb128 (0, "Augmentation size");
2461 /* Loop through the Call Frame Instructions associated with
2462 this FDE. */
2463 fde->dw_fde_current_label = fde->dw_fde_begin;
2464 for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
2465 output_cfi (cfi, fde, for_eh);
2467 /* Pad the FDE out to an address sized boundary. */
2468 ASM_OUTPUT_ALIGN (asm_out_file,
2469 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
2470 ASM_OUTPUT_LABEL (asm_out_file, l2);
2473 if (for_eh && targetm.terminate_dw2_eh_frame_info)
2474 dw2_asm_output_data (4, 0, "End of Table");
2475 #ifdef MIPS_DEBUGGING_INFO
2476 /* Work around Irix 6 assembler bug whereby labels at the end of a section
2477 get a value of 0. Putting .align 0 after the label fixes it. */
2478 ASM_OUTPUT_ALIGN (asm_out_file, 0);
2479 #endif
2481 /* Turn off app to make assembly quicker. */
2482 if (flag_debug_asm)
2483 app_disable ();
2486 /* Output a marker (i.e. a label) for the beginning of a function, before
2487 the prologue. */
2489 void
2490 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
2491 const char *file ATTRIBUTE_UNUSED)
2493 char label[MAX_ARTIFICIAL_LABEL_BYTES];
2494 char * dup_label;
2495 dw_fde_ref fde;
2497 current_function_func_begin_label = NULL;
2499 #ifdef TARGET_UNWIND_INFO
2500 /* ??? current_function_func_begin_label is also used by except.c
2501 for call-site information. We must emit this label if it might
2502 be used. */
2503 if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
2504 && ! dwarf2out_do_frame ())
2505 return;
2506 #else
2507 if (! dwarf2out_do_frame ())
2508 return;
2509 #endif
2511 function_section (current_function_decl);
2512 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
2513 current_function_funcdef_no);
2514 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
2515 current_function_funcdef_no);
2516 dup_label = xstrdup (label);
2517 current_function_func_begin_label = dup_label;
2519 #ifdef TARGET_UNWIND_INFO
2520 /* We can elide the fde allocation if we're not emitting debug info. */
2521 if (! dwarf2out_do_frame ())
2522 return;
2523 #endif
2525 /* Expand the fde table if necessary. */
2526 if (fde_table_in_use == fde_table_allocated)
2528 fde_table_allocated += FDE_TABLE_INCREMENT;
2529 fde_table = ggc_realloc (fde_table,
2530 fde_table_allocated * sizeof (dw_fde_node));
2531 memset (fde_table + fde_table_in_use, 0,
2532 FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2535 /* Record the FDE associated with this function. */
2536 current_funcdef_fde = fde_table_in_use;
2538 /* Add the new FDE at the end of the fde_table. */
2539 fde = &fde_table[fde_table_in_use++];
2540 fde->decl = current_function_decl;
2541 fde->dw_fde_begin = dup_label;
2542 fde->dw_fde_current_label = dup_label;
2543 fde->dw_fde_hot_section_label = NULL;
2544 fde->dw_fde_hot_section_end_label = NULL;
2545 fde->dw_fde_unlikely_section_label = NULL;
2546 fde->dw_fde_unlikely_section_end_label = NULL;
2547 fde->dw_fde_switched_sections = false;
2548 fde->dw_fde_end = NULL;
2549 fde->dw_fde_cfi = NULL;
2550 fde->funcdef_number = current_function_funcdef_no;
2551 fde->nothrow = TREE_NOTHROW (current_function_decl);
2552 fde->uses_eh_lsda = cfun->uses_eh_lsda;
2553 fde->all_throwers_are_sibcalls = cfun->all_throwers_are_sibcalls;
2555 args_size = old_args_size = 0;
2557 /* We only want to output line number information for the genuine dwarf2
2558 prologue case, not the eh frame case. */
2559 #ifdef DWARF2_DEBUGGING_INFO
2560 if (file)
2561 dwarf2out_source_line (line, file);
2562 #endif
2565 /* Output a marker (i.e. a label) for the absolute end of the generated code
2566 for a function definition. This gets called *after* the epilogue code has
2567 been generated. */
2569 void
2570 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
2571 const char *file ATTRIBUTE_UNUSED)
2573 dw_fde_ref fde;
2574 char label[MAX_ARTIFICIAL_LABEL_BYTES];
2576 /* Output a label to mark the endpoint of the code generated for this
2577 function. */
2578 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
2579 current_function_funcdef_no);
2580 ASM_OUTPUT_LABEL (asm_out_file, label);
2581 fde = &fde_table[fde_table_in_use - 1];
2582 fde->dw_fde_end = xstrdup (label);
2585 void
2586 dwarf2out_frame_init (void)
2588 /* Allocate the initial hunk of the fde_table. */
2589 fde_table = ggc_alloc_cleared (FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2590 fde_table_allocated = FDE_TABLE_INCREMENT;
2591 fde_table_in_use = 0;
2593 /* Generate the CFA instructions common to all FDE's. Do it now for the
2594 sake of lookup_cfa. */
2596 /* On entry, the Canonical Frame Address is at SP. */
2597 dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
2599 #ifdef DWARF2_UNWIND_INFO
2600 if (DWARF2_UNWIND_INFO)
2601 initial_return_save (INCOMING_RETURN_ADDR_RTX);
2602 #endif
2605 void
2606 dwarf2out_frame_finish (void)
2608 /* Output call frame information. */
2609 if (DWARF2_FRAME_INFO)
2610 output_call_frame_info (0);
2612 #ifndef TARGET_UNWIND_INFO
2613 /* Output another copy for the unwinder. */
2614 if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
2615 output_call_frame_info (1);
2616 #endif
2618 #endif
2620 /* And now, the subset of the debugging information support code necessary
2621 for emitting location expressions. */
2623 /* We need some way to distinguish DW_OP_addr with a direct symbol
2624 relocation from DW_OP_addr with a dtp-relative symbol relocation. */
2625 #define INTERNAL_DW_OP_tls_addr (0x100 + DW_OP_addr)
2628 typedef struct dw_val_struct *dw_val_ref;
2629 typedef struct die_struct *dw_die_ref;
2630 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
2631 typedef struct dw_loc_list_struct *dw_loc_list_ref;
2633 /* Each DIE may have a series of attribute/value pairs. Values
2634 can take on several forms. The forms that are used in this
2635 implementation are listed below. */
2637 enum dw_val_class
2639 dw_val_class_addr,
2640 dw_val_class_offset,
2641 dw_val_class_loc,
2642 dw_val_class_loc_list,
2643 dw_val_class_range_list,
2644 dw_val_class_const,
2645 dw_val_class_unsigned_const,
2646 dw_val_class_long_long,
2647 dw_val_class_vec,
2648 dw_val_class_flag,
2649 dw_val_class_die_ref,
2650 dw_val_class_fde_ref,
2651 dw_val_class_lbl_id,
2652 dw_val_class_lbl_offset,
2653 dw_val_class_str
2656 /* Describe a double word constant value. */
2657 /* ??? Every instance of long_long in the code really means CONST_DOUBLE. */
2659 typedef struct dw_long_long_struct GTY(())
2661 unsigned long hi;
2662 unsigned long low;
2664 dw_long_long_const;
2666 /* Describe a floating point constant value, or a vector constant value. */
2668 typedef struct dw_vec_struct GTY(())
2670 unsigned char * GTY((length ("%h.length"))) array;
2671 unsigned length;
2672 unsigned elt_size;
2674 dw_vec_const;
2676 /* The dw_val_node describes an attribute's value, as it is
2677 represented internally. */
2679 typedef struct dw_val_struct GTY(())
2681 enum dw_val_class val_class;
2682 union dw_val_struct_union
2684 rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
2685 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
2686 dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
2687 dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
2688 HOST_WIDE_INT GTY ((default)) val_int;
2689 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
2690 dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
2691 dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
2692 struct dw_val_die_union
2694 dw_die_ref die;
2695 int external;
2696 } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
2697 unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
2698 struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
2699 char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
2700 unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
2702 GTY ((desc ("%1.val_class"))) v;
2704 dw_val_node;
2706 /* Locations in memory are described using a sequence of stack machine
2707 operations. */
2709 typedef struct dw_loc_descr_struct GTY(())
2711 dw_loc_descr_ref dw_loc_next;
2712 enum dwarf_location_atom dw_loc_opc;
2713 dw_val_node dw_loc_oprnd1;
2714 dw_val_node dw_loc_oprnd2;
2715 int dw_loc_addr;
2717 dw_loc_descr_node;
2719 /* Location lists are ranges + location descriptions for that range,
2720 so you can track variables that are in different places over
2721 their entire life. */
2722 typedef struct dw_loc_list_struct GTY(())
2724 dw_loc_list_ref dw_loc_next;
2725 const char *begin; /* Label for begin address of range */
2726 const char *end; /* Label for end address of range */
2727 char *ll_symbol; /* Label for beginning of location list.
2728 Only on head of list */
2729 const char *section; /* Section this loclist is relative to */
2730 dw_loc_descr_ref expr;
2731 } dw_loc_list_node;
2733 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2735 static const char *dwarf_stack_op_name (unsigned);
2736 static dw_loc_descr_ref new_loc_descr (enum dwarf_location_atom,
2737 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2738 static void add_loc_descr (dw_loc_descr_ref *, dw_loc_descr_ref);
2739 static unsigned long size_of_loc_descr (dw_loc_descr_ref);
2740 static unsigned long size_of_locs (dw_loc_descr_ref);
2741 static void output_loc_operands (dw_loc_descr_ref);
2742 static void output_loc_sequence (dw_loc_descr_ref);
2744 /* Convert a DWARF stack opcode into its string name. */
2746 static const char *
2747 dwarf_stack_op_name (unsigned int op)
2749 switch (op)
2751 case DW_OP_addr:
2752 case INTERNAL_DW_OP_tls_addr:
2753 return "DW_OP_addr";
2754 case DW_OP_deref:
2755 return "DW_OP_deref";
2756 case DW_OP_const1u:
2757 return "DW_OP_const1u";
2758 case DW_OP_const1s:
2759 return "DW_OP_const1s";
2760 case DW_OP_const2u:
2761 return "DW_OP_const2u";
2762 case DW_OP_const2s:
2763 return "DW_OP_const2s";
2764 case DW_OP_const4u:
2765 return "DW_OP_const4u";
2766 case DW_OP_const4s:
2767 return "DW_OP_const4s";
2768 case DW_OP_const8u:
2769 return "DW_OP_const8u";
2770 case DW_OP_const8s:
2771 return "DW_OP_const8s";
2772 case DW_OP_constu:
2773 return "DW_OP_constu";
2774 case DW_OP_consts:
2775 return "DW_OP_consts";
2776 case DW_OP_dup:
2777 return "DW_OP_dup";
2778 case DW_OP_drop:
2779 return "DW_OP_drop";
2780 case DW_OP_over:
2781 return "DW_OP_over";
2782 case DW_OP_pick:
2783 return "DW_OP_pick";
2784 case DW_OP_swap:
2785 return "DW_OP_swap";
2786 case DW_OP_rot:
2787 return "DW_OP_rot";
2788 case DW_OP_xderef:
2789 return "DW_OP_xderef";
2790 case DW_OP_abs:
2791 return "DW_OP_abs";
2792 case DW_OP_and:
2793 return "DW_OP_and";
2794 case DW_OP_div:
2795 return "DW_OP_div";
2796 case DW_OP_minus:
2797 return "DW_OP_minus";
2798 case DW_OP_mod:
2799 return "DW_OP_mod";
2800 case DW_OP_mul:
2801 return "DW_OP_mul";
2802 case DW_OP_neg:
2803 return "DW_OP_neg";
2804 case DW_OP_not:
2805 return "DW_OP_not";
2806 case DW_OP_or:
2807 return "DW_OP_or";
2808 case DW_OP_plus:
2809 return "DW_OP_plus";
2810 case DW_OP_plus_uconst:
2811 return "DW_OP_plus_uconst";
2812 case DW_OP_shl:
2813 return "DW_OP_shl";
2814 case DW_OP_shr:
2815 return "DW_OP_shr";
2816 case DW_OP_shra:
2817 return "DW_OP_shra";
2818 case DW_OP_xor:
2819 return "DW_OP_xor";
2820 case DW_OP_bra:
2821 return "DW_OP_bra";
2822 case DW_OP_eq:
2823 return "DW_OP_eq";
2824 case DW_OP_ge:
2825 return "DW_OP_ge";
2826 case DW_OP_gt:
2827 return "DW_OP_gt";
2828 case DW_OP_le:
2829 return "DW_OP_le";
2830 case DW_OP_lt:
2831 return "DW_OP_lt";
2832 case DW_OP_ne:
2833 return "DW_OP_ne";
2834 case DW_OP_skip:
2835 return "DW_OP_skip";
2836 case DW_OP_lit0:
2837 return "DW_OP_lit0";
2838 case DW_OP_lit1:
2839 return "DW_OP_lit1";
2840 case DW_OP_lit2:
2841 return "DW_OP_lit2";
2842 case DW_OP_lit3:
2843 return "DW_OP_lit3";
2844 case DW_OP_lit4:
2845 return "DW_OP_lit4";
2846 case DW_OP_lit5:
2847 return "DW_OP_lit5";
2848 case DW_OP_lit6:
2849 return "DW_OP_lit6";
2850 case DW_OP_lit7:
2851 return "DW_OP_lit7";
2852 case DW_OP_lit8:
2853 return "DW_OP_lit8";
2854 case DW_OP_lit9:
2855 return "DW_OP_lit9";
2856 case DW_OP_lit10:
2857 return "DW_OP_lit10";
2858 case DW_OP_lit11:
2859 return "DW_OP_lit11";
2860 case DW_OP_lit12:
2861 return "DW_OP_lit12";
2862 case DW_OP_lit13:
2863 return "DW_OP_lit13";
2864 case DW_OP_lit14:
2865 return "DW_OP_lit14";
2866 case DW_OP_lit15:
2867 return "DW_OP_lit15";
2868 case DW_OP_lit16:
2869 return "DW_OP_lit16";
2870 case DW_OP_lit17:
2871 return "DW_OP_lit17";
2872 case DW_OP_lit18:
2873 return "DW_OP_lit18";
2874 case DW_OP_lit19:
2875 return "DW_OP_lit19";
2876 case DW_OP_lit20:
2877 return "DW_OP_lit20";
2878 case DW_OP_lit21:
2879 return "DW_OP_lit21";
2880 case DW_OP_lit22:
2881 return "DW_OP_lit22";
2882 case DW_OP_lit23:
2883 return "DW_OP_lit23";
2884 case DW_OP_lit24:
2885 return "DW_OP_lit24";
2886 case DW_OP_lit25:
2887 return "DW_OP_lit25";
2888 case DW_OP_lit26:
2889 return "DW_OP_lit26";
2890 case DW_OP_lit27:
2891 return "DW_OP_lit27";
2892 case DW_OP_lit28:
2893 return "DW_OP_lit28";
2894 case DW_OP_lit29:
2895 return "DW_OP_lit29";
2896 case DW_OP_lit30:
2897 return "DW_OP_lit30";
2898 case DW_OP_lit31:
2899 return "DW_OP_lit31";
2900 case DW_OP_reg0:
2901 return "DW_OP_reg0";
2902 case DW_OP_reg1:
2903 return "DW_OP_reg1";
2904 case DW_OP_reg2:
2905 return "DW_OP_reg2";
2906 case DW_OP_reg3:
2907 return "DW_OP_reg3";
2908 case DW_OP_reg4:
2909 return "DW_OP_reg4";
2910 case DW_OP_reg5:
2911 return "DW_OP_reg5";
2912 case DW_OP_reg6:
2913 return "DW_OP_reg6";
2914 case DW_OP_reg7:
2915 return "DW_OP_reg7";
2916 case DW_OP_reg8:
2917 return "DW_OP_reg8";
2918 case DW_OP_reg9:
2919 return "DW_OP_reg9";
2920 case DW_OP_reg10:
2921 return "DW_OP_reg10";
2922 case DW_OP_reg11:
2923 return "DW_OP_reg11";
2924 case DW_OP_reg12:
2925 return "DW_OP_reg12";
2926 case DW_OP_reg13:
2927 return "DW_OP_reg13";
2928 case DW_OP_reg14:
2929 return "DW_OP_reg14";
2930 case DW_OP_reg15:
2931 return "DW_OP_reg15";
2932 case DW_OP_reg16:
2933 return "DW_OP_reg16";
2934 case DW_OP_reg17:
2935 return "DW_OP_reg17";
2936 case DW_OP_reg18:
2937 return "DW_OP_reg18";
2938 case DW_OP_reg19:
2939 return "DW_OP_reg19";
2940 case DW_OP_reg20:
2941 return "DW_OP_reg20";
2942 case DW_OP_reg21:
2943 return "DW_OP_reg21";
2944 case DW_OP_reg22:
2945 return "DW_OP_reg22";
2946 case DW_OP_reg23:
2947 return "DW_OP_reg23";
2948 case DW_OP_reg24:
2949 return "DW_OP_reg24";
2950 case DW_OP_reg25:
2951 return "DW_OP_reg25";
2952 case DW_OP_reg26:
2953 return "DW_OP_reg26";
2954 case DW_OP_reg27:
2955 return "DW_OP_reg27";
2956 case DW_OP_reg28:
2957 return "DW_OP_reg28";
2958 case DW_OP_reg29:
2959 return "DW_OP_reg29";
2960 case DW_OP_reg30:
2961 return "DW_OP_reg30";
2962 case DW_OP_reg31:
2963 return "DW_OP_reg31";
2964 case DW_OP_breg0:
2965 return "DW_OP_breg0";
2966 case DW_OP_breg1:
2967 return "DW_OP_breg1";
2968 case DW_OP_breg2:
2969 return "DW_OP_breg2";
2970 case DW_OP_breg3:
2971 return "DW_OP_breg3";
2972 case DW_OP_breg4:
2973 return "DW_OP_breg4";
2974 case DW_OP_breg5:
2975 return "DW_OP_breg5";
2976 case DW_OP_breg6:
2977 return "DW_OP_breg6";
2978 case DW_OP_breg7:
2979 return "DW_OP_breg7";
2980 case DW_OP_breg8:
2981 return "DW_OP_breg8";
2982 case DW_OP_breg9:
2983 return "DW_OP_breg9";
2984 case DW_OP_breg10:
2985 return "DW_OP_breg10";
2986 case DW_OP_breg11:
2987 return "DW_OP_breg11";
2988 case DW_OP_breg12:
2989 return "DW_OP_breg12";
2990 case DW_OP_breg13:
2991 return "DW_OP_breg13";
2992 case DW_OP_breg14:
2993 return "DW_OP_breg14";
2994 case DW_OP_breg15:
2995 return "DW_OP_breg15";
2996 case DW_OP_breg16:
2997 return "DW_OP_breg16";
2998 case DW_OP_breg17:
2999 return "DW_OP_breg17";
3000 case DW_OP_breg18:
3001 return "DW_OP_breg18";
3002 case DW_OP_breg19:
3003 return "DW_OP_breg19";
3004 case DW_OP_breg20:
3005 return "DW_OP_breg20";
3006 case DW_OP_breg21:
3007 return "DW_OP_breg21";
3008 case DW_OP_breg22:
3009 return "DW_OP_breg22";
3010 case DW_OP_breg23:
3011 return "DW_OP_breg23";
3012 case DW_OP_breg24:
3013 return "DW_OP_breg24";
3014 case DW_OP_breg25:
3015 return "DW_OP_breg25";
3016 case DW_OP_breg26:
3017 return "DW_OP_breg26";
3018 case DW_OP_breg27:
3019 return "DW_OP_breg27";
3020 case DW_OP_breg28:
3021 return "DW_OP_breg28";
3022 case DW_OP_breg29:
3023 return "DW_OP_breg29";
3024 case DW_OP_breg30:
3025 return "DW_OP_breg30";
3026 case DW_OP_breg31:
3027 return "DW_OP_breg31";
3028 case DW_OP_regx:
3029 return "DW_OP_regx";
3030 case DW_OP_fbreg:
3031 return "DW_OP_fbreg";
3032 case DW_OP_bregx:
3033 return "DW_OP_bregx";
3034 case DW_OP_piece:
3035 return "DW_OP_piece";
3036 case DW_OP_deref_size:
3037 return "DW_OP_deref_size";
3038 case DW_OP_xderef_size:
3039 return "DW_OP_xderef_size";
3040 case DW_OP_nop:
3041 return "DW_OP_nop";
3042 case DW_OP_push_object_address:
3043 return "DW_OP_push_object_address";
3044 case DW_OP_call2:
3045 return "DW_OP_call2";
3046 case DW_OP_call4:
3047 return "DW_OP_call4";
3048 case DW_OP_call_ref:
3049 return "DW_OP_call_ref";
3050 case DW_OP_GNU_push_tls_address:
3051 return "DW_OP_GNU_push_tls_address";
3052 default:
3053 return "OP_<unknown>";
3057 /* Return a pointer to a newly allocated location description. Location
3058 descriptions are simple expression terms that can be strung
3059 together to form more complicated location (address) descriptions. */
3061 static inline dw_loc_descr_ref
3062 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
3063 unsigned HOST_WIDE_INT oprnd2)
3065 dw_loc_descr_ref descr = ggc_alloc_cleared (sizeof (dw_loc_descr_node));
3067 descr->dw_loc_opc = op;
3068 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
3069 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
3070 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
3071 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
3073 return descr;
3076 /* Add a location description term to a location description expression. */
3078 static inline void
3079 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
3081 dw_loc_descr_ref *d;
3083 /* Find the end of the chain. */
3084 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
3087 *d = descr;
3090 /* Return the size of a location descriptor. */
3092 static unsigned long
3093 size_of_loc_descr (dw_loc_descr_ref loc)
3095 unsigned long size = 1;
3097 switch (loc->dw_loc_opc)
3099 case DW_OP_addr:
3100 case INTERNAL_DW_OP_tls_addr:
3101 size += DWARF2_ADDR_SIZE;
3102 break;
3103 case DW_OP_const1u:
3104 case DW_OP_const1s:
3105 size += 1;
3106 break;
3107 case DW_OP_const2u:
3108 case DW_OP_const2s:
3109 size += 2;
3110 break;
3111 case DW_OP_const4u:
3112 case DW_OP_const4s:
3113 size += 4;
3114 break;
3115 case DW_OP_const8u:
3116 case DW_OP_const8s:
3117 size += 8;
3118 break;
3119 case DW_OP_constu:
3120 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3121 break;
3122 case DW_OP_consts:
3123 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3124 break;
3125 case DW_OP_pick:
3126 size += 1;
3127 break;
3128 case DW_OP_plus_uconst:
3129 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3130 break;
3131 case DW_OP_skip:
3132 case DW_OP_bra:
3133 size += 2;
3134 break;
3135 case DW_OP_breg0:
3136 case DW_OP_breg1:
3137 case DW_OP_breg2:
3138 case DW_OP_breg3:
3139 case DW_OP_breg4:
3140 case DW_OP_breg5:
3141 case DW_OP_breg6:
3142 case DW_OP_breg7:
3143 case DW_OP_breg8:
3144 case DW_OP_breg9:
3145 case DW_OP_breg10:
3146 case DW_OP_breg11:
3147 case DW_OP_breg12:
3148 case DW_OP_breg13:
3149 case DW_OP_breg14:
3150 case DW_OP_breg15:
3151 case DW_OP_breg16:
3152 case DW_OP_breg17:
3153 case DW_OP_breg18:
3154 case DW_OP_breg19:
3155 case DW_OP_breg20:
3156 case DW_OP_breg21:
3157 case DW_OP_breg22:
3158 case DW_OP_breg23:
3159 case DW_OP_breg24:
3160 case DW_OP_breg25:
3161 case DW_OP_breg26:
3162 case DW_OP_breg27:
3163 case DW_OP_breg28:
3164 case DW_OP_breg29:
3165 case DW_OP_breg30:
3166 case DW_OP_breg31:
3167 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3168 break;
3169 case DW_OP_regx:
3170 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3171 break;
3172 case DW_OP_fbreg:
3173 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3174 break;
3175 case DW_OP_bregx:
3176 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3177 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
3178 break;
3179 case DW_OP_piece:
3180 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3181 break;
3182 case DW_OP_deref_size:
3183 case DW_OP_xderef_size:
3184 size += 1;
3185 break;
3186 case DW_OP_call2:
3187 size += 2;
3188 break;
3189 case DW_OP_call4:
3190 size += 4;
3191 break;
3192 case DW_OP_call_ref:
3193 size += DWARF2_ADDR_SIZE;
3194 break;
3195 default:
3196 break;
3199 return size;
3202 /* Return the size of a series of location descriptors. */
3204 static unsigned long
3205 size_of_locs (dw_loc_descr_ref loc)
3207 unsigned long size;
3209 for (size = 0; loc != NULL; loc = loc->dw_loc_next)
3211 loc->dw_loc_addr = size;
3212 size += size_of_loc_descr (loc);
3215 return size;
3218 /* Output location description stack opcode's operands (if any). */
3220 static void
3221 output_loc_operands (dw_loc_descr_ref loc)
3223 dw_val_ref val1 = &loc->dw_loc_oprnd1;
3224 dw_val_ref val2 = &loc->dw_loc_oprnd2;
3226 switch (loc->dw_loc_opc)
3228 #ifdef DWARF2_DEBUGGING_INFO
3229 case DW_OP_addr:
3230 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
3231 break;
3232 case DW_OP_const2u:
3233 case DW_OP_const2s:
3234 dw2_asm_output_data (2, val1->v.val_int, NULL);
3235 break;
3236 case DW_OP_const4u:
3237 case DW_OP_const4s:
3238 dw2_asm_output_data (4, val1->v.val_int, NULL);
3239 break;
3240 case DW_OP_const8u:
3241 case DW_OP_const8s:
3242 gcc_assert (HOST_BITS_PER_LONG >= 64);
3243 dw2_asm_output_data (8, val1->v.val_int, NULL);
3244 break;
3245 case DW_OP_skip:
3246 case DW_OP_bra:
3248 int offset;
3250 gcc_assert (val1->val_class == dw_val_class_loc);
3251 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
3253 dw2_asm_output_data (2, offset, NULL);
3255 break;
3256 #else
3257 case DW_OP_addr:
3258 case DW_OP_const2u:
3259 case DW_OP_const2s:
3260 case DW_OP_const4u:
3261 case DW_OP_const4s:
3262 case DW_OP_const8u:
3263 case DW_OP_const8s:
3264 case DW_OP_skip:
3265 case DW_OP_bra:
3266 /* We currently don't make any attempt to make sure these are
3267 aligned properly like we do for the main unwind info, so
3268 don't support emitting things larger than a byte if we're
3269 only doing unwinding. */
3270 gcc_unreachable ();
3271 #endif
3272 case DW_OP_const1u:
3273 case DW_OP_const1s:
3274 dw2_asm_output_data (1, val1->v.val_int, NULL);
3275 break;
3276 case DW_OP_constu:
3277 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3278 break;
3279 case DW_OP_consts:
3280 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3281 break;
3282 case DW_OP_pick:
3283 dw2_asm_output_data (1, val1->v.val_int, NULL);
3284 break;
3285 case DW_OP_plus_uconst:
3286 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3287 break;
3288 case DW_OP_breg0:
3289 case DW_OP_breg1:
3290 case DW_OP_breg2:
3291 case DW_OP_breg3:
3292 case DW_OP_breg4:
3293 case DW_OP_breg5:
3294 case DW_OP_breg6:
3295 case DW_OP_breg7:
3296 case DW_OP_breg8:
3297 case DW_OP_breg9:
3298 case DW_OP_breg10:
3299 case DW_OP_breg11:
3300 case DW_OP_breg12:
3301 case DW_OP_breg13:
3302 case DW_OP_breg14:
3303 case DW_OP_breg15:
3304 case DW_OP_breg16:
3305 case DW_OP_breg17:
3306 case DW_OP_breg18:
3307 case DW_OP_breg19:
3308 case DW_OP_breg20:
3309 case DW_OP_breg21:
3310 case DW_OP_breg22:
3311 case DW_OP_breg23:
3312 case DW_OP_breg24:
3313 case DW_OP_breg25:
3314 case DW_OP_breg26:
3315 case DW_OP_breg27:
3316 case DW_OP_breg28:
3317 case DW_OP_breg29:
3318 case DW_OP_breg30:
3319 case DW_OP_breg31:
3320 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3321 break;
3322 case DW_OP_regx:
3323 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3324 break;
3325 case DW_OP_fbreg:
3326 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3327 break;
3328 case DW_OP_bregx:
3329 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3330 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
3331 break;
3332 case DW_OP_piece:
3333 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3334 break;
3335 case DW_OP_deref_size:
3336 case DW_OP_xderef_size:
3337 dw2_asm_output_data (1, val1->v.val_int, NULL);
3338 break;
3340 case INTERNAL_DW_OP_tls_addr:
3341 if (targetm.asm_out.output_dwarf_dtprel)
3343 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
3344 DWARF2_ADDR_SIZE,
3345 val1->v.val_addr);
3346 fputc ('\n', asm_out_file);
3348 else
3349 gcc_unreachable ();
3350 break;
3352 default:
3353 /* Other codes have no operands. */
3354 break;
3358 /* Output a sequence of location operations. */
3360 static void
3361 output_loc_sequence (dw_loc_descr_ref loc)
3363 for (; loc != NULL; loc = loc->dw_loc_next)
3365 /* Output the opcode. */
3366 dw2_asm_output_data (1, loc->dw_loc_opc,
3367 "%s", dwarf_stack_op_name (loc->dw_loc_opc));
3369 /* Output the operand(s) (if any). */
3370 output_loc_operands (loc);
3374 /* This routine will generate the correct assembly data for a location
3375 description based on a cfi entry with a complex address. */
3377 static void
3378 output_cfa_loc (dw_cfi_ref cfi)
3380 dw_loc_descr_ref loc;
3381 unsigned long size;
3383 /* Output the size of the block. */
3384 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
3385 size = size_of_locs (loc);
3386 dw2_asm_output_data_uleb128 (size, NULL);
3388 /* Now output the operations themselves. */
3389 output_loc_sequence (loc);
3392 /* This function builds a dwarf location descriptor sequence from
3393 a dw_cfa_location. */
3395 static struct dw_loc_descr_struct *
3396 build_cfa_loc (dw_cfa_location *cfa)
3398 struct dw_loc_descr_struct *head, *tmp;
3400 if (cfa->indirect)
3402 if (cfa->base_offset)
3404 if (cfa->reg <= 31)
3405 head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->base_offset, 0);
3406 else
3407 head = new_loc_descr (DW_OP_bregx, cfa->reg, cfa->base_offset);
3409 else if (cfa->reg <= 31)
3410 head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3411 else
3412 head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3414 head->dw_loc_oprnd1.val_class = dw_val_class_const;
3415 tmp = new_loc_descr (DW_OP_deref, 0, 0);
3416 add_loc_descr (&head, tmp);
3417 if (cfa->offset != 0)
3419 tmp = new_loc_descr (DW_OP_plus_uconst, cfa->offset, 0);
3420 add_loc_descr (&head, tmp);
3423 else
3425 if (cfa->offset == 0)
3426 if (cfa->reg <= 31)
3427 head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3428 else
3429 head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3430 else if (cfa->reg <= 31)
3431 head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->offset, 0);
3432 else
3433 head = new_loc_descr (DW_OP_bregx, cfa->reg, cfa->offset);
3436 return head;
3439 /* This function fills in aa dw_cfa_location structure from a dwarf location
3440 descriptor sequence. */
3442 static void
3443 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
3445 struct dw_loc_descr_struct *ptr;
3446 cfa->offset = 0;
3447 cfa->base_offset = 0;
3448 cfa->indirect = 0;
3449 cfa->reg = -1;
3451 for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
3453 enum dwarf_location_atom op = ptr->dw_loc_opc;
3455 switch (op)
3457 case DW_OP_reg0:
3458 case DW_OP_reg1:
3459 case DW_OP_reg2:
3460 case DW_OP_reg3:
3461 case DW_OP_reg4:
3462 case DW_OP_reg5:
3463 case DW_OP_reg6:
3464 case DW_OP_reg7:
3465 case DW_OP_reg8:
3466 case DW_OP_reg9:
3467 case DW_OP_reg10:
3468 case DW_OP_reg11:
3469 case DW_OP_reg12:
3470 case DW_OP_reg13:
3471 case DW_OP_reg14:
3472 case DW_OP_reg15:
3473 case DW_OP_reg16:
3474 case DW_OP_reg17:
3475 case DW_OP_reg18:
3476 case DW_OP_reg19:
3477 case DW_OP_reg20:
3478 case DW_OP_reg21:
3479 case DW_OP_reg22:
3480 case DW_OP_reg23:
3481 case DW_OP_reg24:
3482 case DW_OP_reg25:
3483 case DW_OP_reg26:
3484 case DW_OP_reg27:
3485 case DW_OP_reg28:
3486 case DW_OP_reg29:
3487 case DW_OP_reg30:
3488 case DW_OP_reg31:
3489 cfa->reg = op - DW_OP_reg0;
3490 break;
3491 case DW_OP_regx:
3492 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3493 break;
3494 case DW_OP_breg0:
3495 case DW_OP_breg1:
3496 case DW_OP_breg2:
3497 case DW_OP_breg3:
3498 case DW_OP_breg4:
3499 case DW_OP_breg5:
3500 case DW_OP_breg6:
3501 case DW_OP_breg7:
3502 case DW_OP_breg8:
3503 case DW_OP_breg9:
3504 case DW_OP_breg10:
3505 case DW_OP_breg11:
3506 case DW_OP_breg12:
3507 case DW_OP_breg13:
3508 case DW_OP_breg14:
3509 case DW_OP_breg15:
3510 case DW_OP_breg16:
3511 case DW_OP_breg17:
3512 case DW_OP_breg18:
3513 case DW_OP_breg19:
3514 case DW_OP_breg20:
3515 case DW_OP_breg21:
3516 case DW_OP_breg22:
3517 case DW_OP_breg23:
3518 case DW_OP_breg24:
3519 case DW_OP_breg25:
3520 case DW_OP_breg26:
3521 case DW_OP_breg27:
3522 case DW_OP_breg28:
3523 case DW_OP_breg29:
3524 case DW_OP_breg30:
3525 case DW_OP_breg31:
3526 cfa->reg = op - DW_OP_breg0;
3527 cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
3528 break;
3529 case DW_OP_bregx:
3530 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3531 cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
3532 break;
3533 case DW_OP_deref:
3534 cfa->indirect = 1;
3535 break;
3536 case DW_OP_plus_uconst:
3537 cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
3538 break;
3539 default:
3540 internal_error ("DW_LOC_OP %s not implemented",
3541 dwarf_stack_op_name (ptr->dw_loc_opc));
3545 #endif /* .debug_frame support */
3547 /* And now, the support for symbolic debugging information. */
3548 #ifdef DWARF2_DEBUGGING_INFO
3550 /* .debug_str support. */
3551 static int output_indirect_string (void **, void *);
3553 static void dwarf2out_init (const char *);
3554 static void dwarf2out_finish (const char *);
3555 static void dwarf2out_define (unsigned int, const char *);
3556 static void dwarf2out_undef (unsigned int, const char *);
3557 static void dwarf2out_start_source_file (unsigned, const char *);
3558 static void dwarf2out_end_source_file (unsigned);
3559 static void dwarf2out_begin_block (unsigned, unsigned);
3560 static void dwarf2out_end_block (unsigned, unsigned);
3561 static bool dwarf2out_ignore_block (tree);
3562 static void dwarf2out_global_decl (tree);
3563 static void dwarf2out_type_decl (tree, int);
3564 static void dwarf2out_imported_module_or_decl (tree, tree);
3565 static void dwarf2out_abstract_function (tree);
3566 static void dwarf2out_var_location (rtx);
3567 static void dwarf2out_begin_function (tree);
3569 /* The debug hooks structure. */
3571 const struct gcc_debug_hooks dwarf2_debug_hooks =
3573 dwarf2out_init,
3574 dwarf2out_finish,
3575 dwarf2out_define,
3576 dwarf2out_undef,
3577 dwarf2out_start_source_file,
3578 dwarf2out_end_source_file,
3579 dwarf2out_begin_block,
3580 dwarf2out_end_block,
3581 dwarf2out_ignore_block,
3582 dwarf2out_source_line,
3583 dwarf2out_begin_prologue,
3584 debug_nothing_int_charstar, /* end_prologue */
3585 dwarf2out_end_epilogue,
3586 dwarf2out_begin_function,
3587 debug_nothing_int, /* end_function */
3588 dwarf2out_decl, /* function_decl */
3589 dwarf2out_global_decl,
3590 dwarf2out_type_decl, /* type_decl */
3591 dwarf2out_imported_module_or_decl,
3592 debug_nothing_tree, /* deferred_inline_function */
3593 /* The DWARF 2 backend tries to reduce debugging bloat by not
3594 emitting the abstract description of inline functions until
3595 something tries to reference them. */
3596 dwarf2out_abstract_function, /* outlining_inline_function */
3597 debug_nothing_rtx, /* label */
3598 debug_nothing_int, /* handle_pch */
3599 dwarf2out_var_location,
3600 dwarf2out_switch_text_section,
3601 1 /* start_end_main_source_file */
3603 #endif
3605 /* NOTE: In the comments in this file, many references are made to
3606 "Debugging Information Entries". This term is abbreviated as `DIE'
3607 throughout the remainder of this file. */
3609 /* An internal representation of the DWARF output is built, and then
3610 walked to generate the DWARF debugging info. The walk of the internal
3611 representation is done after the entire program has been compiled.
3612 The types below are used to describe the internal representation. */
3614 /* Various DIE's use offsets relative to the beginning of the
3615 .debug_info section to refer to each other. */
3617 typedef long int dw_offset;
3619 /* Define typedefs here to avoid circular dependencies. */
3621 typedef struct dw_attr_struct *dw_attr_ref;
3622 typedef struct dw_line_info_struct *dw_line_info_ref;
3623 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
3624 typedef struct pubname_struct *pubname_ref;
3625 typedef struct dw_ranges_struct *dw_ranges_ref;
3627 /* Each entry in the line_info_table maintains the file and
3628 line number associated with the label generated for that
3629 entry. The label gives the PC value associated with
3630 the line number entry. */
3632 typedef struct dw_line_info_struct GTY(())
3634 unsigned long dw_file_num;
3635 unsigned long dw_line_num;
3637 dw_line_info_entry;
3639 /* Line information for functions in separate sections; each one gets its
3640 own sequence. */
3641 typedef struct dw_separate_line_info_struct GTY(())
3643 unsigned long dw_file_num;
3644 unsigned long dw_line_num;
3645 unsigned long function;
3647 dw_separate_line_info_entry;
3649 /* Each DIE attribute has a field specifying the attribute kind,
3650 a link to the next attribute in the chain, and an attribute value.
3651 Attributes are typically linked below the DIE they modify. */
3653 typedef struct dw_attr_struct GTY(())
3655 enum dwarf_attribute dw_attr;
3656 dw_attr_ref dw_attr_next;
3657 dw_val_node dw_attr_val;
3659 dw_attr_node;
3661 /* The Debugging Information Entry (DIE) structure */
3663 typedef struct die_struct GTY(())
3665 enum dwarf_tag die_tag;
3666 char *die_symbol;
3667 dw_attr_ref die_attr;
3668 dw_die_ref die_parent;
3669 dw_die_ref die_child;
3670 dw_die_ref die_sib;
3671 dw_die_ref die_definition; /* ref from a specification to its definition */
3672 dw_offset die_offset;
3673 unsigned long die_abbrev;
3674 int die_mark;
3675 unsigned int decl_id;
3677 die_node;
3679 /* The pubname structure */
3681 typedef struct pubname_struct GTY(())
3683 dw_die_ref die;
3684 char *name;
3686 pubname_entry;
3688 struct dw_ranges_struct GTY(())
3690 int block_num;
3693 /* The limbo die list structure. */
3694 typedef struct limbo_die_struct GTY(())
3696 dw_die_ref die;
3697 tree created_for;
3698 struct limbo_die_struct *next;
3700 limbo_die_node;
3702 /* How to start an assembler comment. */
3703 #ifndef ASM_COMMENT_START
3704 #define ASM_COMMENT_START ";#"
3705 #endif
3707 /* Define a macro which returns nonzero for a TYPE_DECL which was
3708 implicitly generated for a tagged type.
3710 Note that unlike the gcc front end (which generates a NULL named
3711 TYPE_DECL node for each complete tagged type, each array type, and
3712 each function type node created) the g++ front end generates a
3713 _named_ TYPE_DECL node for each tagged type node created.
3714 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3715 generate a DW_TAG_typedef DIE for them. */
3717 #define TYPE_DECL_IS_STUB(decl) \
3718 (DECL_NAME (decl) == NULL_TREE \
3719 || (DECL_ARTIFICIAL (decl) \
3720 && is_tagged_type (TREE_TYPE (decl)) \
3721 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3722 /* This is necessary for stub decls that \
3723 appear in nested inline functions. */ \
3724 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3725 && (decl_ultimate_origin (decl) \
3726 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3728 /* Information concerning the compilation unit's programming
3729 language, and compiler version. */
3731 /* Fixed size portion of the DWARF compilation unit header. */
3732 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3733 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
3735 /* Fixed size portion of public names info. */
3736 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3738 /* Fixed size portion of the address range info. */
3739 #define DWARF_ARANGES_HEADER_SIZE \
3740 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3741 DWARF2_ADDR_SIZE * 2) \
3742 - DWARF_INITIAL_LENGTH_SIZE)
3744 /* Size of padding portion in the address range info. It must be
3745 aligned to twice the pointer size. */
3746 #define DWARF_ARANGES_PAD_SIZE \
3747 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3748 DWARF2_ADDR_SIZE * 2) \
3749 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3751 /* Use assembler line directives if available. */
3752 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3753 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3754 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3755 #else
3756 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3757 #endif
3758 #endif
3760 /* Minimum line offset in a special line info. opcode.
3761 This value was chosen to give a reasonable range of values. */
3762 #define DWARF_LINE_BASE -10
3764 /* First special line opcode - leave room for the standard opcodes. */
3765 #define DWARF_LINE_OPCODE_BASE 10
3767 /* Range of line offsets in a special line info. opcode. */
3768 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3770 /* Flag that indicates the initial value of the is_stmt_start flag.
3771 In the present implementation, we do not mark any lines as
3772 the beginning of a source statement, because that information
3773 is not made available by the GCC front-end. */
3774 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3776 #ifdef DWARF2_DEBUGGING_INFO
3777 /* This location is used by calc_die_sizes() to keep track
3778 the offset of each DIE within the .debug_info section. */
3779 static unsigned long next_die_offset;
3780 #endif
3782 /* Record the root of the DIE's built for the current compilation unit. */
3783 static GTY(()) dw_die_ref comp_unit_die;
3785 /* A list of DIEs with a NULL parent waiting to be relocated. */
3786 static GTY(()) limbo_die_node *limbo_die_list;
3788 /* Filenames referenced by this compilation unit. */
3789 static GTY(()) varray_type file_table;
3790 static GTY(()) varray_type file_table_emitted;
3791 static GTY(()) size_t file_table_last_lookup_index;
3793 /* A hash table of references to DIE's that describe declarations.
3794 The key is a DECL_UID() which is a unique number identifying each decl. */
3795 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
3797 /* Node of the variable location list. */
3798 struct var_loc_node GTY ((chain_next ("%h.next")))
3800 rtx GTY (()) var_loc_note;
3801 const char * GTY (()) label;
3802 const char * GTY (()) section_label;
3803 struct var_loc_node * GTY (()) next;
3806 /* Variable location list. */
3807 struct var_loc_list_def GTY (())
3809 struct var_loc_node * GTY (()) first;
3811 /* Do not mark the last element of the chained list because
3812 it is marked through the chain. */
3813 struct var_loc_node * GTY ((skip ("%h"))) last;
3815 /* DECL_UID of the variable decl. */
3816 unsigned int decl_id;
3818 typedef struct var_loc_list_def var_loc_list;
3821 /* Table of decl location linked lists. */
3822 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
3824 /* A pointer to the base of a list of references to DIE's that
3825 are uniquely identified by their tag, presence/absence of
3826 children DIE's, and list of attribute/value pairs. */
3827 static GTY((length ("abbrev_die_table_allocated")))
3828 dw_die_ref *abbrev_die_table;
3830 /* Number of elements currently allocated for abbrev_die_table. */
3831 static GTY(()) unsigned abbrev_die_table_allocated;
3833 /* Number of elements in type_die_table currently in use. */
3834 static GTY(()) unsigned abbrev_die_table_in_use;
3836 /* Size (in elements) of increments by which we may expand the
3837 abbrev_die_table. */
3838 #define ABBREV_DIE_TABLE_INCREMENT 256
3840 /* A pointer to the base of a table that contains line information
3841 for each source code line in .text in the compilation unit. */
3842 static GTY((length ("line_info_table_allocated")))
3843 dw_line_info_ref line_info_table;
3845 /* Number of elements currently allocated for line_info_table. */
3846 static GTY(()) unsigned line_info_table_allocated;
3848 /* Number of elements in line_info_table currently in use. */
3849 static GTY(()) unsigned line_info_table_in_use;
3851 /* True if the compilation unit contains more than one .text section. */
3852 static GTY(()) bool have_switched_text_section = false;
3854 /* A pointer to the base of a table that contains line information
3855 for each source code line outside of .text in the compilation unit. */
3856 static GTY ((length ("separate_line_info_table_allocated")))
3857 dw_separate_line_info_ref separate_line_info_table;
3859 /* Number of elements currently allocated for separate_line_info_table. */
3860 static GTY(()) unsigned separate_line_info_table_allocated;
3862 /* Number of elements in separate_line_info_table currently in use. */
3863 static GTY(()) unsigned separate_line_info_table_in_use;
3865 /* Size (in elements) of increments by which we may expand the
3866 line_info_table. */
3867 #define LINE_INFO_TABLE_INCREMENT 1024
3869 /* A pointer to the base of a table that contains a list of publicly
3870 accessible names. */
3871 static GTY ((length ("pubname_table_allocated"))) pubname_ref pubname_table;
3873 /* Number of elements currently allocated for pubname_table. */
3874 static GTY(()) unsigned pubname_table_allocated;
3876 /* Number of elements in pubname_table currently in use. */
3877 static GTY(()) unsigned pubname_table_in_use;
3879 /* Size (in elements) of increments by which we may expand the
3880 pubname_table. */
3881 #define PUBNAME_TABLE_INCREMENT 64
3883 /* Array of dies for which we should generate .debug_arange info. */
3884 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
3886 /* Number of elements currently allocated for arange_table. */
3887 static GTY(()) unsigned arange_table_allocated;
3889 /* Number of elements in arange_table currently in use. */
3890 static GTY(()) unsigned arange_table_in_use;
3892 /* Size (in elements) of increments by which we may expand the
3893 arange_table. */
3894 #define ARANGE_TABLE_INCREMENT 64
3896 /* Array of dies for which we should generate .debug_ranges info. */
3897 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
3899 /* Number of elements currently allocated for ranges_table. */
3900 static GTY(()) unsigned ranges_table_allocated;
3902 /* Number of elements in ranges_table currently in use. */
3903 static GTY(()) unsigned ranges_table_in_use;
3905 /* Size (in elements) of increments by which we may expand the
3906 ranges_table. */
3907 #define RANGES_TABLE_INCREMENT 64
3909 /* Whether we have location lists that need outputting */
3910 static GTY(()) unsigned have_location_lists;
3912 /* Unique label counter. */
3913 static GTY(()) unsigned int loclabel_num;
3915 #ifdef DWARF2_DEBUGGING_INFO
3916 /* Record whether the function being analyzed contains inlined functions. */
3917 static int current_function_has_inlines;
3918 #endif
3919 #if 0 && defined (MIPS_DEBUGGING_INFO)
3920 static int comp_unit_has_inlines;
3921 #endif
3923 /* Number of file tables emitted in maybe_emit_file(). */
3924 static GTY(()) int emitcount = 0;
3926 /* Number of internal labels generated by gen_internal_sym(). */
3927 static GTY(()) int label_num;
3929 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
3930 void
3931 dwarf2out_switch_text_section (void)
3933 dw_fde_ref fde;
3935 gcc_assert (cfun);
3937 fde = &fde_table[fde_table_in_use - 1];
3938 fde->dw_fde_switched_sections = true;
3939 fde->dw_fde_hot_section_label = cfun->hot_section_label;
3940 fde->dw_fde_hot_section_end_label = cfun->hot_section_end_label;
3941 fde->dw_fde_unlikely_section_label = cfun->cold_section_label;
3942 fde->dw_fde_unlikely_section_end_label = cfun->cold_section_end_label;
3943 have_switched_text_section = true;
3945 /* Reset the current label on switching text sections, so that we
3946 don't attempt to advance_loc4 between labels in different sections. */
3947 fde->dw_fde_current_label = NULL;
3950 #endif
3952 #ifdef DWARF2_DEBUGGING_INFO
3954 /* Offset from the "steady-state frame pointer" to the CFA,
3955 within the current function. */
3956 static HOST_WIDE_INT frame_pointer_cfa_offset;
3958 /* Forward declarations for functions defined in this file. */
3960 static int is_pseudo_reg (rtx);
3961 static tree type_main_variant (tree);
3962 static int is_tagged_type (tree);
3963 static const char *dwarf_tag_name (unsigned);
3964 static const char *dwarf_attr_name (unsigned);
3965 static const char *dwarf_form_name (unsigned);
3966 static tree decl_ultimate_origin (tree);
3967 static tree block_ultimate_origin (tree);
3968 static tree decl_class_context (tree);
3969 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
3970 static inline enum dw_val_class AT_class (dw_attr_ref);
3971 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3972 static inline unsigned AT_flag (dw_attr_ref);
3973 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3974 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3975 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3976 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3977 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
3978 unsigned long);
3979 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3980 unsigned int, unsigned char *);
3981 static hashval_t debug_str_do_hash (const void *);
3982 static int debug_str_eq (const void *, const void *);
3983 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3984 static inline const char *AT_string (dw_attr_ref);
3985 static int AT_string_form (dw_attr_ref);
3986 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3987 static void add_AT_specification (dw_die_ref, dw_die_ref);
3988 static inline dw_die_ref AT_ref (dw_attr_ref);
3989 static inline int AT_ref_external (dw_attr_ref);
3990 static inline void set_AT_ref_external (dw_attr_ref, int);
3991 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3992 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3993 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3994 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3995 dw_loc_list_ref);
3996 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3997 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
3998 static inline rtx AT_addr (dw_attr_ref);
3999 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
4000 static void add_AT_lbl_offset (dw_die_ref, enum dwarf_attribute, const char *);
4001 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
4002 unsigned HOST_WIDE_INT);
4003 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
4004 unsigned long);
4005 static inline const char *AT_lbl (dw_attr_ref);
4006 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
4007 static const char *get_AT_low_pc (dw_die_ref);
4008 static const char *get_AT_hi_pc (dw_die_ref);
4009 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
4010 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
4011 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
4012 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
4013 static bool is_c_family (void);
4014 static bool is_cxx (void);
4015 static bool is_java (void);
4016 static bool is_fortran (void);
4017 static bool is_ada (void);
4018 static void remove_AT (dw_die_ref, enum dwarf_attribute);
4019 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
4020 static inline void free_die (dw_die_ref);
4021 static void remove_children (dw_die_ref);
4022 static void add_child_die (dw_die_ref, dw_die_ref);
4023 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
4024 static dw_die_ref lookup_type_die (tree);
4025 static void equate_type_number_to_die (tree, dw_die_ref);
4026 static hashval_t decl_die_table_hash (const void *);
4027 static int decl_die_table_eq (const void *, const void *);
4028 static dw_die_ref lookup_decl_die (tree);
4029 static hashval_t decl_loc_table_hash (const void *);
4030 static int decl_loc_table_eq (const void *, const void *);
4031 static var_loc_list *lookup_decl_loc (tree);
4032 static void equate_decl_number_to_die (tree, dw_die_ref);
4033 static void add_var_loc_to_decl (tree, struct var_loc_node *);
4034 static void print_spaces (FILE *);
4035 static void print_die (dw_die_ref, FILE *);
4036 static void print_dwarf_line_table (FILE *);
4037 static void reverse_die_lists (dw_die_ref);
4038 static void reverse_all_dies (dw_die_ref);
4039 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
4040 static dw_die_ref pop_compile_unit (dw_die_ref);
4041 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
4042 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
4043 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
4044 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
4045 static int same_dw_val_p (dw_val_node *, dw_val_node *, int *);
4046 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
4047 static int same_die_p (dw_die_ref, dw_die_ref, int *);
4048 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
4049 static void compute_section_prefix (dw_die_ref);
4050 static int is_type_die (dw_die_ref);
4051 static int is_comdat_die (dw_die_ref);
4052 static int is_symbol_die (dw_die_ref);
4053 static void assign_symbol_names (dw_die_ref);
4054 static void break_out_includes (dw_die_ref);
4055 static hashval_t htab_cu_hash (const void *);
4056 static int htab_cu_eq (const void *, const void *);
4057 static void htab_cu_del (void *);
4058 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
4059 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
4060 static void add_sibling_attributes (dw_die_ref);
4061 static void build_abbrev_table (dw_die_ref);
4062 static void output_location_lists (dw_die_ref);
4063 static int constant_size (long unsigned);
4064 static unsigned long size_of_die (dw_die_ref);
4065 static void calc_die_sizes (dw_die_ref);
4066 static void mark_dies (dw_die_ref);
4067 static void unmark_dies (dw_die_ref);
4068 static void unmark_all_dies (dw_die_ref);
4069 static unsigned long size_of_pubnames (void);
4070 static unsigned long size_of_aranges (void);
4071 static enum dwarf_form value_format (dw_attr_ref);
4072 static void output_value_format (dw_attr_ref);
4073 static void output_abbrev_section (void);
4074 static void output_die_symbol (dw_die_ref);
4075 static void output_die (dw_die_ref);
4076 static void output_compilation_unit_header (void);
4077 static void output_comp_unit (dw_die_ref, int);
4078 static const char *dwarf2_name (tree, int);
4079 static void add_pubname (tree, dw_die_ref);
4080 static void output_pubnames (void);
4081 static void add_arange (tree, dw_die_ref);
4082 static void output_aranges (void);
4083 static unsigned int add_ranges (tree);
4084 static void output_ranges (void);
4085 static void output_line_info (void);
4086 static void output_file_names (void);
4087 static dw_die_ref base_type_die (tree);
4088 static tree root_type (tree);
4089 static int is_base_type (tree);
4090 static bool is_subrange_type (tree);
4091 static dw_die_ref subrange_type_die (tree, dw_die_ref);
4092 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
4093 static int type_is_enum (tree);
4094 static unsigned int dbx_reg_number (rtx);
4095 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
4096 static dw_loc_descr_ref reg_loc_descriptor (rtx);
4097 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int);
4098 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx);
4099 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4100 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT);
4101 static int is_based_loc (rtx);
4102 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode);
4103 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx);
4104 static dw_loc_descr_ref loc_descriptor (rtx);
4105 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
4106 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
4107 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
4108 static tree field_type (tree);
4109 static unsigned int simple_type_align_in_bits (tree);
4110 static unsigned int simple_decl_align_in_bits (tree);
4111 static unsigned HOST_WIDE_INT simple_type_size_in_bits (tree);
4112 static HOST_WIDE_INT field_byte_offset (tree);
4113 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
4114 dw_loc_descr_ref);
4115 static void add_data_member_location_attribute (dw_die_ref, tree);
4116 static void add_const_value_attribute (dw_die_ref, rtx);
4117 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
4118 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4119 static void insert_float (rtx, unsigned char *);
4120 static rtx rtl_for_decl_location (tree);
4121 static void add_location_or_const_value_attribute (dw_die_ref, tree,
4122 enum dwarf_attribute);
4123 static void tree_add_const_value_attribute (dw_die_ref, tree);
4124 static void add_name_attribute (dw_die_ref, const char *);
4125 static void add_comp_dir_attribute (dw_die_ref);
4126 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
4127 static void add_subscript_info (dw_die_ref, tree);
4128 static void add_byte_size_attribute (dw_die_ref, tree);
4129 static void add_bit_offset_attribute (dw_die_ref, tree);
4130 static void add_bit_size_attribute (dw_die_ref, tree);
4131 static void add_prototyped_attribute (dw_die_ref, tree);
4132 static void add_abstract_origin_attribute (dw_die_ref, tree);
4133 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
4134 static void add_src_coords_attributes (dw_die_ref, tree);
4135 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
4136 static void push_decl_scope (tree);
4137 static void pop_decl_scope (void);
4138 static dw_die_ref scope_die_for (tree, dw_die_ref);
4139 static inline int local_scope_p (dw_die_ref);
4140 static inline int class_or_namespace_scope_p (dw_die_ref);
4141 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
4142 static void add_calling_convention_attribute (dw_die_ref, tree);
4143 static const char *type_tag (tree);
4144 static tree member_declared_type (tree);
4145 #if 0
4146 static const char *decl_start_label (tree);
4147 #endif
4148 static void gen_array_type_die (tree, dw_die_ref);
4149 #if 0
4150 static void gen_entry_point_die (tree, dw_die_ref);
4151 #endif
4152 static void gen_inlined_enumeration_type_die (tree, dw_die_ref);
4153 static void gen_inlined_structure_type_die (tree, dw_die_ref);
4154 static void gen_inlined_union_type_die (tree, dw_die_ref);
4155 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
4156 static dw_die_ref gen_formal_parameter_die (tree, dw_die_ref);
4157 static void gen_unspecified_parameters_die (tree, dw_die_ref);
4158 static void gen_formal_types_die (tree, dw_die_ref);
4159 static void gen_subprogram_die (tree, dw_die_ref);
4160 static void gen_variable_die (tree, dw_die_ref);
4161 static void gen_label_die (tree, dw_die_ref);
4162 static void gen_lexical_block_die (tree, dw_die_ref, int);
4163 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
4164 static void gen_field_die (tree, dw_die_ref);
4165 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
4166 static dw_die_ref gen_compile_unit_die (const char *);
4167 static void gen_string_type_die (tree, dw_die_ref);
4168 static void gen_inheritance_die (tree, tree, dw_die_ref);
4169 static void gen_member_die (tree, dw_die_ref);
4170 static void gen_struct_or_union_type_die (tree, dw_die_ref);
4171 static void gen_subroutine_type_die (tree, dw_die_ref);
4172 static void gen_typedef_die (tree, dw_die_ref);
4173 static void gen_type_die (tree, dw_die_ref);
4174 static void gen_tagged_type_instantiation_die (tree, dw_die_ref);
4175 static void gen_block_die (tree, dw_die_ref, int);
4176 static void decls_for_scope (tree, dw_die_ref, int);
4177 static int is_redundant_typedef (tree);
4178 static void gen_namespace_die (tree);
4179 static void gen_decl_die (tree, dw_die_ref);
4180 static dw_die_ref force_decl_die (tree);
4181 static dw_die_ref force_type_die (tree);
4182 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
4183 static void declare_in_namespace (tree, dw_die_ref);
4184 static unsigned lookup_filename (const char *);
4185 static void init_file_table (void);
4186 static void retry_incomplete_types (void);
4187 static void gen_type_die_for_member (tree, tree, dw_die_ref);
4188 static void splice_child_die (dw_die_ref, dw_die_ref);
4189 static int file_info_cmp (const void *, const void *);
4190 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
4191 const char *, const char *, unsigned);
4192 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
4193 const char *, const char *,
4194 const char *);
4195 static void output_loc_list (dw_loc_list_ref);
4196 static char *gen_internal_sym (const char *);
4198 static void prune_unmark_dies (dw_die_ref);
4199 static void prune_unused_types_mark (dw_die_ref, int);
4200 static void prune_unused_types_walk (dw_die_ref);
4201 static void prune_unused_types_walk_attribs (dw_die_ref);
4202 static void prune_unused_types_prune (dw_die_ref);
4203 static void prune_unused_types (void);
4204 static int maybe_emit_file (int);
4206 /* Section names used to hold DWARF debugging information. */
4207 #ifndef DEBUG_INFO_SECTION
4208 #define DEBUG_INFO_SECTION ".debug_info"
4209 #endif
4210 #ifndef DEBUG_ABBREV_SECTION
4211 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
4212 #endif
4213 #ifndef DEBUG_ARANGES_SECTION
4214 #define DEBUG_ARANGES_SECTION ".debug_aranges"
4215 #endif
4216 #ifndef DEBUG_MACINFO_SECTION
4217 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
4218 #endif
4219 #ifndef DEBUG_LINE_SECTION
4220 #define DEBUG_LINE_SECTION ".debug_line"
4221 #endif
4222 #ifndef DEBUG_LOC_SECTION
4223 #define DEBUG_LOC_SECTION ".debug_loc"
4224 #endif
4225 #ifndef DEBUG_PUBNAMES_SECTION
4226 #define DEBUG_PUBNAMES_SECTION ".debug_pubnames"
4227 #endif
4228 #ifndef DEBUG_STR_SECTION
4229 #define DEBUG_STR_SECTION ".debug_str"
4230 #endif
4231 #ifndef DEBUG_RANGES_SECTION
4232 #define DEBUG_RANGES_SECTION ".debug_ranges"
4233 #endif
4235 /* Standard ELF section names for compiled code and data. */
4236 #ifndef TEXT_SECTION_NAME
4237 #define TEXT_SECTION_NAME ".text"
4238 #endif
4240 /* Section flags for .debug_str section. */
4241 #define DEBUG_STR_SECTION_FLAGS \
4242 (HAVE_GAS_SHF_MERGE && flag_merge_constants \
4243 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
4244 : SECTION_DEBUG)
4246 /* Labels we insert at beginning sections we can reference instead of
4247 the section names themselves. */
4249 #ifndef TEXT_SECTION_LABEL
4250 #define TEXT_SECTION_LABEL "Ltext"
4251 #endif
4252 #ifndef COLD_TEXT_SECTION_LABEL
4253 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
4254 #endif
4255 #ifndef DEBUG_LINE_SECTION_LABEL
4256 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
4257 #endif
4258 #ifndef DEBUG_INFO_SECTION_LABEL
4259 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
4260 #endif
4261 #ifndef DEBUG_ABBREV_SECTION_LABEL
4262 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
4263 #endif
4264 #ifndef DEBUG_LOC_SECTION_LABEL
4265 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
4266 #endif
4267 #ifndef DEBUG_RANGES_SECTION_LABEL
4268 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
4269 #endif
4270 #ifndef DEBUG_MACINFO_SECTION_LABEL
4271 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
4272 #endif
4274 /* Definitions of defaults for formats and names of various special
4275 (artificial) labels which may be generated within this file (when the -g
4276 options is used and DWARF2_DEBUGGING_INFO is in effect.
4277 If necessary, these may be overridden from within the tm.h file, but
4278 typically, overriding these defaults is unnecessary. */
4280 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4281 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4282 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4283 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4284 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4285 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4286 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4287 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4288 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4289 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4291 #ifndef TEXT_END_LABEL
4292 #define TEXT_END_LABEL "Letext"
4293 #endif
4294 #ifndef COLD_END_LABEL
4295 #define COLD_END_LABEL "Letext_cold"
4296 #endif
4297 #ifndef BLOCK_BEGIN_LABEL
4298 #define BLOCK_BEGIN_LABEL "LBB"
4299 #endif
4300 #ifndef BLOCK_END_LABEL
4301 #define BLOCK_END_LABEL "LBE"
4302 #endif
4303 #ifndef LINE_CODE_LABEL
4304 #define LINE_CODE_LABEL "LM"
4305 #endif
4306 #ifndef SEPARATE_LINE_CODE_LABEL
4307 #define SEPARATE_LINE_CODE_LABEL "LSM"
4308 #endif
4310 /* We allow a language front-end to designate a function that is to be
4311 called to "demangle" any name before it is put into a DIE. */
4313 static const char *(*demangle_name_func) (const char *);
4315 void
4316 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4318 demangle_name_func = func;
4321 /* Test if rtl node points to a pseudo register. */
4323 static inline int
4324 is_pseudo_reg (rtx rtl)
4326 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4327 || (GET_CODE (rtl) == SUBREG
4328 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4331 /* Return a reference to a type, with its const and volatile qualifiers
4332 removed. */
4334 static inline tree
4335 type_main_variant (tree type)
4337 type = TYPE_MAIN_VARIANT (type);
4339 /* ??? There really should be only one main variant among any group of
4340 variants of a given type (and all of the MAIN_VARIANT values for all
4341 members of the group should point to that one type) but sometimes the C
4342 front-end messes this up for array types, so we work around that bug
4343 here. */
4344 if (TREE_CODE (type) == ARRAY_TYPE)
4345 while (type != TYPE_MAIN_VARIANT (type))
4346 type = TYPE_MAIN_VARIANT (type);
4348 return type;
4351 /* Return nonzero if the given type node represents a tagged type. */
4353 static inline int
4354 is_tagged_type (tree type)
4356 enum tree_code code = TREE_CODE (type);
4358 return (code == RECORD_TYPE || code == UNION_TYPE
4359 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4362 /* Convert a DIE tag into its string name. */
4364 static const char *
4365 dwarf_tag_name (unsigned int tag)
4367 switch (tag)
4369 case DW_TAG_padding:
4370 return "DW_TAG_padding";
4371 case DW_TAG_array_type:
4372 return "DW_TAG_array_type";
4373 case DW_TAG_class_type:
4374 return "DW_TAG_class_type";
4375 case DW_TAG_entry_point:
4376 return "DW_TAG_entry_point";
4377 case DW_TAG_enumeration_type:
4378 return "DW_TAG_enumeration_type";
4379 case DW_TAG_formal_parameter:
4380 return "DW_TAG_formal_parameter";
4381 case DW_TAG_imported_declaration:
4382 return "DW_TAG_imported_declaration";
4383 case DW_TAG_label:
4384 return "DW_TAG_label";
4385 case DW_TAG_lexical_block:
4386 return "DW_TAG_lexical_block";
4387 case DW_TAG_member:
4388 return "DW_TAG_member";
4389 case DW_TAG_pointer_type:
4390 return "DW_TAG_pointer_type";
4391 case DW_TAG_reference_type:
4392 return "DW_TAG_reference_type";
4393 case DW_TAG_compile_unit:
4394 return "DW_TAG_compile_unit";
4395 case DW_TAG_string_type:
4396 return "DW_TAG_string_type";
4397 case DW_TAG_structure_type:
4398 return "DW_TAG_structure_type";
4399 case DW_TAG_subroutine_type:
4400 return "DW_TAG_subroutine_type";
4401 case DW_TAG_typedef:
4402 return "DW_TAG_typedef";
4403 case DW_TAG_union_type:
4404 return "DW_TAG_union_type";
4405 case DW_TAG_unspecified_parameters:
4406 return "DW_TAG_unspecified_parameters";
4407 case DW_TAG_variant:
4408 return "DW_TAG_variant";
4409 case DW_TAG_common_block:
4410 return "DW_TAG_common_block";
4411 case DW_TAG_common_inclusion:
4412 return "DW_TAG_common_inclusion";
4413 case DW_TAG_inheritance:
4414 return "DW_TAG_inheritance";
4415 case DW_TAG_inlined_subroutine:
4416 return "DW_TAG_inlined_subroutine";
4417 case DW_TAG_module:
4418 return "DW_TAG_module";
4419 case DW_TAG_ptr_to_member_type:
4420 return "DW_TAG_ptr_to_member_type";
4421 case DW_TAG_set_type:
4422 return "DW_TAG_set_type";
4423 case DW_TAG_subrange_type:
4424 return "DW_TAG_subrange_type";
4425 case DW_TAG_with_stmt:
4426 return "DW_TAG_with_stmt";
4427 case DW_TAG_access_declaration:
4428 return "DW_TAG_access_declaration";
4429 case DW_TAG_base_type:
4430 return "DW_TAG_base_type";
4431 case DW_TAG_catch_block:
4432 return "DW_TAG_catch_block";
4433 case DW_TAG_const_type:
4434 return "DW_TAG_const_type";
4435 case DW_TAG_constant:
4436 return "DW_TAG_constant";
4437 case DW_TAG_enumerator:
4438 return "DW_TAG_enumerator";
4439 case DW_TAG_file_type:
4440 return "DW_TAG_file_type";
4441 case DW_TAG_friend:
4442 return "DW_TAG_friend";
4443 case DW_TAG_namelist:
4444 return "DW_TAG_namelist";
4445 case DW_TAG_namelist_item:
4446 return "DW_TAG_namelist_item";
4447 case DW_TAG_namespace:
4448 return "DW_TAG_namespace";
4449 case DW_TAG_packed_type:
4450 return "DW_TAG_packed_type";
4451 case DW_TAG_subprogram:
4452 return "DW_TAG_subprogram";
4453 case DW_TAG_template_type_param:
4454 return "DW_TAG_template_type_param";
4455 case DW_TAG_template_value_param:
4456 return "DW_TAG_template_value_param";
4457 case DW_TAG_thrown_type:
4458 return "DW_TAG_thrown_type";
4459 case DW_TAG_try_block:
4460 return "DW_TAG_try_block";
4461 case DW_TAG_variant_part:
4462 return "DW_TAG_variant_part";
4463 case DW_TAG_variable:
4464 return "DW_TAG_variable";
4465 case DW_TAG_volatile_type:
4466 return "DW_TAG_volatile_type";
4467 case DW_TAG_imported_module:
4468 return "DW_TAG_imported_module";
4469 case DW_TAG_MIPS_loop:
4470 return "DW_TAG_MIPS_loop";
4471 case DW_TAG_format_label:
4472 return "DW_TAG_format_label";
4473 case DW_TAG_function_template:
4474 return "DW_TAG_function_template";
4475 case DW_TAG_class_template:
4476 return "DW_TAG_class_template";
4477 case DW_TAG_GNU_BINCL:
4478 return "DW_TAG_GNU_BINCL";
4479 case DW_TAG_GNU_EINCL:
4480 return "DW_TAG_GNU_EINCL";
4481 default:
4482 return "DW_TAG_<unknown>";
4486 /* Convert a DWARF attribute code into its string name. */
4488 static const char *
4489 dwarf_attr_name (unsigned int attr)
4491 switch (attr)
4493 case DW_AT_sibling:
4494 return "DW_AT_sibling";
4495 case DW_AT_location:
4496 return "DW_AT_location";
4497 case DW_AT_name:
4498 return "DW_AT_name";
4499 case DW_AT_ordering:
4500 return "DW_AT_ordering";
4501 case DW_AT_subscr_data:
4502 return "DW_AT_subscr_data";
4503 case DW_AT_byte_size:
4504 return "DW_AT_byte_size";
4505 case DW_AT_bit_offset:
4506 return "DW_AT_bit_offset";
4507 case DW_AT_bit_size:
4508 return "DW_AT_bit_size";
4509 case DW_AT_element_list:
4510 return "DW_AT_element_list";
4511 case DW_AT_stmt_list:
4512 return "DW_AT_stmt_list";
4513 case DW_AT_low_pc:
4514 return "DW_AT_low_pc";
4515 case DW_AT_high_pc:
4516 return "DW_AT_high_pc";
4517 case DW_AT_language:
4518 return "DW_AT_language";
4519 case DW_AT_member:
4520 return "DW_AT_member";
4521 case DW_AT_discr:
4522 return "DW_AT_discr";
4523 case DW_AT_discr_value:
4524 return "DW_AT_discr_value";
4525 case DW_AT_visibility:
4526 return "DW_AT_visibility";
4527 case DW_AT_import:
4528 return "DW_AT_import";
4529 case DW_AT_string_length:
4530 return "DW_AT_string_length";
4531 case DW_AT_common_reference:
4532 return "DW_AT_common_reference";
4533 case DW_AT_comp_dir:
4534 return "DW_AT_comp_dir";
4535 case DW_AT_const_value:
4536 return "DW_AT_const_value";
4537 case DW_AT_containing_type:
4538 return "DW_AT_containing_type";
4539 case DW_AT_default_value:
4540 return "DW_AT_default_value";
4541 case DW_AT_inline:
4542 return "DW_AT_inline";
4543 case DW_AT_is_optional:
4544 return "DW_AT_is_optional";
4545 case DW_AT_lower_bound:
4546 return "DW_AT_lower_bound";
4547 case DW_AT_producer:
4548 return "DW_AT_producer";
4549 case DW_AT_prototyped:
4550 return "DW_AT_prototyped";
4551 case DW_AT_return_addr:
4552 return "DW_AT_return_addr";
4553 case DW_AT_start_scope:
4554 return "DW_AT_start_scope";
4555 case DW_AT_stride_size:
4556 return "DW_AT_stride_size";
4557 case DW_AT_upper_bound:
4558 return "DW_AT_upper_bound";
4559 case DW_AT_abstract_origin:
4560 return "DW_AT_abstract_origin";
4561 case DW_AT_accessibility:
4562 return "DW_AT_accessibility";
4563 case DW_AT_address_class:
4564 return "DW_AT_address_class";
4565 case DW_AT_artificial:
4566 return "DW_AT_artificial";
4567 case DW_AT_base_types:
4568 return "DW_AT_base_types";
4569 case DW_AT_calling_convention:
4570 return "DW_AT_calling_convention";
4571 case DW_AT_count:
4572 return "DW_AT_count";
4573 case DW_AT_data_member_location:
4574 return "DW_AT_data_member_location";
4575 case DW_AT_decl_column:
4576 return "DW_AT_decl_column";
4577 case DW_AT_decl_file:
4578 return "DW_AT_decl_file";
4579 case DW_AT_decl_line:
4580 return "DW_AT_decl_line";
4581 case DW_AT_declaration:
4582 return "DW_AT_declaration";
4583 case DW_AT_discr_list:
4584 return "DW_AT_discr_list";
4585 case DW_AT_encoding:
4586 return "DW_AT_encoding";
4587 case DW_AT_external:
4588 return "DW_AT_external";
4589 case DW_AT_frame_base:
4590 return "DW_AT_frame_base";
4591 case DW_AT_friend:
4592 return "DW_AT_friend";
4593 case DW_AT_identifier_case:
4594 return "DW_AT_identifier_case";
4595 case DW_AT_macro_info:
4596 return "DW_AT_macro_info";
4597 case DW_AT_namelist_items:
4598 return "DW_AT_namelist_items";
4599 case DW_AT_priority:
4600 return "DW_AT_priority";
4601 case DW_AT_segment:
4602 return "DW_AT_segment";
4603 case DW_AT_specification:
4604 return "DW_AT_specification";
4605 case DW_AT_static_link:
4606 return "DW_AT_static_link";
4607 case DW_AT_type:
4608 return "DW_AT_type";
4609 case DW_AT_use_location:
4610 return "DW_AT_use_location";
4611 case DW_AT_variable_parameter:
4612 return "DW_AT_variable_parameter";
4613 case DW_AT_virtuality:
4614 return "DW_AT_virtuality";
4615 case DW_AT_vtable_elem_location:
4616 return "DW_AT_vtable_elem_location";
4618 case DW_AT_allocated:
4619 return "DW_AT_allocated";
4620 case DW_AT_associated:
4621 return "DW_AT_associated";
4622 case DW_AT_data_location:
4623 return "DW_AT_data_location";
4624 case DW_AT_stride:
4625 return "DW_AT_stride";
4626 case DW_AT_entry_pc:
4627 return "DW_AT_entry_pc";
4628 case DW_AT_use_UTF8:
4629 return "DW_AT_use_UTF8";
4630 case DW_AT_extension:
4631 return "DW_AT_extension";
4632 case DW_AT_ranges:
4633 return "DW_AT_ranges";
4634 case DW_AT_trampoline:
4635 return "DW_AT_trampoline";
4636 case DW_AT_call_column:
4637 return "DW_AT_call_column";
4638 case DW_AT_call_file:
4639 return "DW_AT_call_file";
4640 case DW_AT_call_line:
4641 return "DW_AT_call_line";
4643 case DW_AT_MIPS_fde:
4644 return "DW_AT_MIPS_fde";
4645 case DW_AT_MIPS_loop_begin:
4646 return "DW_AT_MIPS_loop_begin";
4647 case DW_AT_MIPS_tail_loop_begin:
4648 return "DW_AT_MIPS_tail_loop_begin";
4649 case DW_AT_MIPS_epilog_begin:
4650 return "DW_AT_MIPS_epilog_begin";
4651 case DW_AT_MIPS_loop_unroll_factor:
4652 return "DW_AT_MIPS_loop_unroll_factor";
4653 case DW_AT_MIPS_software_pipeline_depth:
4654 return "DW_AT_MIPS_software_pipeline_depth";
4655 case DW_AT_MIPS_linkage_name:
4656 return "DW_AT_MIPS_linkage_name";
4657 case DW_AT_MIPS_stride:
4658 return "DW_AT_MIPS_stride";
4659 case DW_AT_MIPS_abstract_name:
4660 return "DW_AT_MIPS_abstract_name";
4661 case DW_AT_MIPS_clone_origin:
4662 return "DW_AT_MIPS_clone_origin";
4663 case DW_AT_MIPS_has_inlines:
4664 return "DW_AT_MIPS_has_inlines";
4666 case DW_AT_sf_names:
4667 return "DW_AT_sf_names";
4668 case DW_AT_src_info:
4669 return "DW_AT_src_info";
4670 case DW_AT_mac_info:
4671 return "DW_AT_mac_info";
4672 case DW_AT_src_coords:
4673 return "DW_AT_src_coords";
4674 case DW_AT_body_begin:
4675 return "DW_AT_body_begin";
4676 case DW_AT_body_end:
4677 return "DW_AT_body_end";
4678 case DW_AT_GNU_vector:
4679 return "DW_AT_GNU_vector";
4681 case DW_AT_VMS_rtnbeg_pd_address:
4682 return "DW_AT_VMS_rtnbeg_pd_address";
4684 default:
4685 return "DW_AT_<unknown>";
4689 /* Convert a DWARF value form code into its string name. */
4691 static const char *
4692 dwarf_form_name (unsigned int form)
4694 switch (form)
4696 case DW_FORM_addr:
4697 return "DW_FORM_addr";
4698 case DW_FORM_block2:
4699 return "DW_FORM_block2";
4700 case DW_FORM_block4:
4701 return "DW_FORM_block4";
4702 case DW_FORM_data2:
4703 return "DW_FORM_data2";
4704 case DW_FORM_data4:
4705 return "DW_FORM_data4";
4706 case DW_FORM_data8:
4707 return "DW_FORM_data8";
4708 case DW_FORM_string:
4709 return "DW_FORM_string";
4710 case DW_FORM_block:
4711 return "DW_FORM_block";
4712 case DW_FORM_block1:
4713 return "DW_FORM_block1";
4714 case DW_FORM_data1:
4715 return "DW_FORM_data1";
4716 case DW_FORM_flag:
4717 return "DW_FORM_flag";
4718 case DW_FORM_sdata:
4719 return "DW_FORM_sdata";
4720 case DW_FORM_strp:
4721 return "DW_FORM_strp";
4722 case DW_FORM_udata:
4723 return "DW_FORM_udata";
4724 case DW_FORM_ref_addr:
4725 return "DW_FORM_ref_addr";
4726 case DW_FORM_ref1:
4727 return "DW_FORM_ref1";
4728 case DW_FORM_ref2:
4729 return "DW_FORM_ref2";
4730 case DW_FORM_ref4:
4731 return "DW_FORM_ref4";
4732 case DW_FORM_ref8:
4733 return "DW_FORM_ref8";
4734 case DW_FORM_ref_udata:
4735 return "DW_FORM_ref_udata";
4736 case DW_FORM_indirect:
4737 return "DW_FORM_indirect";
4738 default:
4739 return "DW_FORM_<unknown>";
4743 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4744 instance of an inlined instance of a decl which is local to an inline
4745 function, so we have to trace all of the way back through the origin chain
4746 to find out what sort of node actually served as the original seed for the
4747 given block. */
4749 static tree
4750 decl_ultimate_origin (tree decl)
4752 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4753 return NULL_TREE;
4755 /* output_inline_function sets DECL_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 (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4759 return NULL_TREE;
4761 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4762 most distant ancestor, this should never happen. */
4763 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4765 return DECL_ABSTRACT_ORIGIN (decl);
4768 /* Determine the "ultimate origin" of a block. The block may be an inlined
4769 instance of an inlined instance of a block which is local to an inline
4770 function, so we have to trace all of the way back through the origin chain
4771 to find out what sort of node actually served as the original seed for the
4772 given block. */
4774 static tree
4775 block_ultimate_origin (tree block)
4777 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
4779 /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
4780 nodes in the function to point to themselves; ignore that if
4781 we're trying to output the abstract instance of this function. */
4782 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
4783 return NULL_TREE;
4785 if (immediate_origin == NULL_TREE)
4786 return NULL_TREE;
4787 else
4789 tree ret_val;
4790 tree lookahead = immediate_origin;
4794 ret_val = lookahead;
4795 lookahead = (TREE_CODE (ret_val) == BLOCK
4796 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
4798 while (lookahead != NULL && lookahead != ret_val);
4800 /* The block's abstract origin chain may not be the *ultimate* origin of
4801 the block. It could lead to a DECL that has an abstract origin set.
4802 If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
4803 will give us if it has one). Note that DECL's abstract origins are
4804 supposed to be the most distant ancestor (or so decl_ultimate_origin
4805 claims), so we don't need to loop following the DECL origins. */
4806 if (DECL_P (ret_val))
4807 return DECL_ORIGIN (ret_val);
4809 return ret_val;
4813 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4814 of a virtual function may refer to a base class, so we check the 'this'
4815 parameter. */
4817 static tree
4818 decl_class_context (tree decl)
4820 tree context = NULL_TREE;
4822 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4823 context = DECL_CONTEXT (decl);
4824 else
4825 context = TYPE_MAIN_VARIANT
4826 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4828 if (context && !TYPE_P (context))
4829 context = NULL_TREE;
4831 return context;
4834 /* Add an attribute/value pair to a DIE. We build the lists up in reverse
4835 addition order, and correct that in reverse_all_dies. */
4837 static inline void
4838 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
4840 if (die != NULL && attr != NULL)
4842 attr->dw_attr_next = die->die_attr;
4843 die->die_attr = attr;
4847 static inline enum dw_val_class
4848 AT_class (dw_attr_ref a)
4850 return a->dw_attr_val.val_class;
4853 /* Add a flag value attribute to a DIE. */
4855 static inline void
4856 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4858 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4860 attr->dw_attr_next = NULL;
4861 attr->dw_attr = attr_kind;
4862 attr->dw_attr_val.val_class = dw_val_class_flag;
4863 attr->dw_attr_val.v.val_flag = flag;
4864 add_dwarf_attr (die, attr);
4867 static inline unsigned
4868 AT_flag (dw_attr_ref a)
4870 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4871 return a->dw_attr_val.v.val_flag;
4874 /* Add a signed integer attribute value to a DIE. */
4876 static inline void
4877 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4879 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4881 attr->dw_attr_next = NULL;
4882 attr->dw_attr = attr_kind;
4883 attr->dw_attr_val.val_class = dw_val_class_const;
4884 attr->dw_attr_val.v.val_int = int_val;
4885 add_dwarf_attr (die, attr);
4888 static inline HOST_WIDE_INT
4889 AT_int (dw_attr_ref a)
4891 gcc_assert (a && AT_class (a) == dw_val_class_const);
4892 return a->dw_attr_val.v.val_int;
4895 /* Add an unsigned integer attribute value to a DIE. */
4897 static inline void
4898 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4899 unsigned HOST_WIDE_INT unsigned_val)
4901 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4903 attr->dw_attr_next = NULL;
4904 attr->dw_attr = attr_kind;
4905 attr->dw_attr_val.val_class = dw_val_class_unsigned_const;
4906 attr->dw_attr_val.v.val_unsigned = unsigned_val;
4907 add_dwarf_attr (die, attr);
4910 static inline unsigned HOST_WIDE_INT
4911 AT_unsigned (dw_attr_ref a)
4913 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
4914 return a->dw_attr_val.v.val_unsigned;
4917 /* Add an unsigned double integer attribute value to a DIE. */
4919 static inline void
4920 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
4921 long unsigned int val_hi, long unsigned int val_low)
4923 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4925 attr->dw_attr_next = NULL;
4926 attr->dw_attr = attr_kind;
4927 attr->dw_attr_val.val_class = dw_val_class_long_long;
4928 attr->dw_attr_val.v.val_long_long.hi = val_hi;
4929 attr->dw_attr_val.v.val_long_long.low = val_low;
4930 add_dwarf_attr (die, attr);
4933 /* Add a floating point attribute value to a DIE and return it. */
4935 static inline void
4936 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4937 unsigned int length, unsigned int elt_size, unsigned char *array)
4939 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4941 attr->dw_attr_next = NULL;
4942 attr->dw_attr = attr_kind;
4943 attr->dw_attr_val.val_class = dw_val_class_vec;
4944 attr->dw_attr_val.v.val_vec.length = length;
4945 attr->dw_attr_val.v.val_vec.elt_size = elt_size;
4946 attr->dw_attr_val.v.val_vec.array = array;
4947 add_dwarf_attr (die, attr);
4950 /* Hash and equality functions for debug_str_hash. */
4952 static hashval_t
4953 debug_str_do_hash (const void *x)
4955 return htab_hash_string (((const struct indirect_string_node *)x)->str);
4958 static int
4959 debug_str_eq (const void *x1, const void *x2)
4961 return strcmp ((((const struct indirect_string_node *)x1)->str),
4962 (const char *)x2) == 0;
4965 /* Add a string attribute value to a DIE. */
4967 static inline void
4968 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4970 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4971 struct indirect_string_node *node;
4972 void **slot;
4974 if (! debug_str_hash)
4975 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
4976 debug_str_eq, NULL);
4978 slot = htab_find_slot_with_hash (debug_str_hash, str,
4979 htab_hash_string (str), INSERT);
4980 if (*slot == NULL)
4981 *slot = ggc_alloc_cleared (sizeof (struct indirect_string_node));
4982 node = (struct indirect_string_node *) *slot;
4983 node->str = ggc_strdup (str);
4984 node->refcount++;
4986 attr->dw_attr_next = NULL;
4987 attr->dw_attr = attr_kind;
4988 attr->dw_attr_val.val_class = dw_val_class_str;
4989 attr->dw_attr_val.v.val_str = node;
4990 add_dwarf_attr (die, attr);
4993 static inline const char *
4994 AT_string (dw_attr_ref a)
4996 gcc_assert (a && AT_class (a) == dw_val_class_str);
4997 return a->dw_attr_val.v.val_str->str;
5000 /* Find out whether a string should be output inline in DIE
5001 or out-of-line in .debug_str section. */
5003 static int
5004 AT_string_form (dw_attr_ref a)
5006 struct indirect_string_node *node;
5007 unsigned int len;
5008 char label[32];
5010 gcc_assert (a && AT_class (a) == dw_val_class_str);
5012 node = a->dw_attr_val.v.val_str;
5013 if (node->form)
5014 return node->form;
5016 len = strlen (node->str) + 1;
5018 /* If the string is shorter or equal to the size of the reference, it is
5019 always better to put it inline. */
5020 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
5021 return node->form = DW_FORM_string;
5023 /* If we cannot expect the linker to merge strings in .debug_str
5024 section, only put it into .debug_str if it is worth even in this
5025 single module. */
5026 if ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) == 0
5027 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
5028 return node->form = DW_FORM_string;
5030 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
5031 ++dw2_string_counter;
5032 node->label = xstrdup (label);
5034 return node->form = DW_FORM_strp;
5037 /* Add a DIE reference attribute value to a DIE. */
5039 static inline void
5040 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
5042 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5044 attr->dw_attr_next = NULL;
5045 attr->dw_attr = attr_kind;
5046 attr->dw_attr_val.val_class = dw_val_class_die_ref;
5047 attr->dw_attr_val.v.val_die_ref.die = targ_die;
5048 attr->dw_attr_val.v.val_die_ref.external = 0;
5049 add_dwarf_attr (die, attr);
5052 /* Add an AT_specification attribute to a DIE, and also make the back
5053 pointer from the specification to the definition. */
5055 static inline void
5056 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
5058 add_AT_die_ref (die, DW_AT_specification, targ_die);
5059 gcc_assert (!targ_die->die_definition);
5060 targ_die->die_definition = die;
5063 static inline dw_die_ref
5064 AT_ref (dw_attr_ref a)
5066 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5067 return a->dw_attr_val.v.val_die_ref.die;
5070 static inline int
5071 AT_ref_external (dw_attr_ref a)
5073 if (a && AT_class (a) == dw_val_class_die_ref)
5074 return a->dw_attr_val.v.val_die_ref.external;
5076 return 0;
5079 static inline void
5080 set_AT_ref_external (dw_attr_ref a, int i)
5082 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5083 a->dw_attr_val.v.val_die_ref.external = i;
5086 /* Add an FDE reference attribute value to a DIE. */
5088 static inline void
5089 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
5091 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5093 attr->dw_attr_next = NULL;
5094 attr->dw_attr = attr_kind;
5095 attr->dw_attr_val.val_class = dw_val_class_fde_ref;
5096 attr->dw_attr_val.v.val_fde_index = targ_fde;
5097 add_dwarf_attr (die, attr);
5100 /* Add a location description attribute value to a DIE. */
5102 static inline void
5103 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
5105 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5107 attr->dw_attr_next = NULL;
5108 attr->dw_attr = attr_kind;
5109 attr->dw_attr_val.val_class = dw_val_class_loc;
5110 attr->dw_attr_val.v.val_loc = loc;
5111 add_dwarf_attr (die, attr);
5114 static inline dw_loc_descr_ref
5115 AT_loc (dw_attr_ref a)
5117 gcc_assert (a && AT_class (a) == dw_val_class_loc);
5118 return a->dw_attr_val.v.val_loc;
5121 static inline void
5122 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
5124 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5126 attr->dw_attr_next = NULL;
5127 attr->dw_attr = attr_kind;
5128 attr->dw_attr_val.val_class = dw_val_class_loc_list;
5129 attr->dw_attr_val.v.val_loc_list = loc_list;
5130 add_dwarf_attr (die, attr);
5131 have_location_lists = 1;
5134 static inline dw_loc_list_ref
5135 AT_loc_list (dw_attr_ref a)
5137 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
5138 return a->dw_attr_val.v.val_loc_list;
5141 /* Add an address constant attribute value to a DIE. */
5143 static inline void
5144 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
5146 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5148 attr->dw_attr_next = NULL;
5149 attr->dw_attr = attr_kind;
5150 attr->dw_attr_val.val_class = dw_val_class_addr;
5151 attr->dw_attr_val.v.val_addr = addr;
5152 add_dwarf_attr (die, attr);
5155 static inline rtx
5156 AT_addr (dw_attr_ref a)
5158 gcc_assert (a && AT_class (a) == dw_val_class_addr);
5159 return a->dw_attr_val.v.val_addr;
5162 /* Add a label identifier attribute value to a DIE. */
5164 static inline void
5165 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
5167 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5169 attr->dw_attr_next = NULL;
5170 attr->dw_attr = attr_kind;
5171 attr->dw_attr_val.val_class = dw_val_class_lbl_id;
5172 attr->dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5173 add_dwarf_attr (die, attr);
5176 /* Add a section offset attribute value to a DIE. */
5178 static inline void
5179 add_AT_lbl_offset (dw_die_ref die, enum dwarf_attribute attr_kind, const char *label)
5181 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5183 attr->dw_attr_next = NULL;
5184 attr->dw_attr = attr_kind;
5185 attr->dw_attr_val.val_class = dw_val_class_lbl_offset;
5186 attr->dw_attr_val.v.val_lbl_id = xstrdup (label);
5187 add_dwarf_attr (die, attr);
5190 /* Add an offset attribute value to a DIE. */
5192 static inline void
5193 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
5194 unsigned HOST_WIDE_INT offset)
5196 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5198 attr->dw_attr_next = NULL;
5199 attr->dw_attr = attr_kind;
5200 attr->dw_attr_val.val_class = dw_val_class_offset;
5201 attr->dw_attr_val.v.val_offset = offset;
5202 add_dwarf_attr (die, attr);
5205 /* Add an range_list attribute value to a DIE. */
5207 static void
5208 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5209 long unsigned int offset)
5211 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5213 attr->dw_attr_next = NULL;
5214 attr->dw_attr = attr_kind;
5215 attr->dw_attr_val.val_class = dw_val_class_range_list;
5216 attr->dw_attr_val.v.val_offset = offset;
5217 add_dwarf_attr (die, attr);
5220 static inline const char *
5221 AT_lbl (dw_attr_ref a)
5223 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5224 || AT_class (a) == dw_val_class_lbl_offset));
5225 return a->dw_attr_val.v.val_lbl_id;
5228 /* Get the attribute of type attr_kind. */
5230 static dw_attr_ref
5231 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5233 dw_attr_ref a;
5234 dw_die_ref spec = NULL;
5236 if (die != NULL)
5238 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
5239 if (a->dw_attr == attr_kind)
5240 return a;
5241 else if (a->dw_attr == DW_AT_specification
5242 || a->dw_attr == DW_AT_abstract_origin)
5243 spec = AT_ref (a);
5245 if (spec)
5246 return get_AT (spec, attr_kind);
5249 return NULL;
5252 /* Return the "low pc" attribute value, typically associated with a subprogram
5253 DIE. Return null if the "low pc" attribute is either not present, or if it
5254 cannot be represented as an assembler label identifier. */
5256 static inline const char *
5257 get_AT_low_pc (dw_die_ref die)
5259 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
5261 return a ? AT_lbl (a) : NULL;
5264 /* Return the "high pc" attribute value, typically associated with a subprogram
5265 DIE. Return null if the "high pc" attribute is either not present, or if it
5266 cannot be represented as an assembler label identifier. */
5268 static inline const char *
5269 get_AT_hi_pc (dw_die_ref die)
5271 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
5273 return a ? AT_lbl (a) : NULL;
5276 /* Return the value of the string attribute designated by ATTR_KIND, or
5277 NULL if it is not present. */
5279 static inline const char *
5280 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5282 dw_attr_ref a = get_AT (die, attr_kind);
5284 return a ? AT_string (a) : NULL;
5287 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5288 if it is not present. */
5290 static inline int
5291 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5293 dw_attr_ref a = get_AT (die, attr_kind);
5295 return a ? AT_flag (a) : 0;
5298 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5299 if it is not present. */
5301 static inline unsigned
5302 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5304 dw_attr_ref a = get_AT (die, attr_kind);
5306 return a ? AT_unsigned (a) : 0;
5309 static inline dw_die_ref
5310 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5312 dw_attr_ref a = get_AT (die, attr_kind);
5314 return a ? AT_ref (a) : NULL;
5317 /* Return TRUE if the language is C or C++. */
5319 static inline bool
5320 is_c_family (void)
5322 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5324 return (lang == DW_LANG_C || lang == DW_LANG_C89
5325 || lang == DW_LANG_C_plus_plus);
5328 /* Return TRUE if the language is C++. */
5330 static inline bool
5331 is_cxx (void)
5333 return (get_AT_unsigned (comp_unit_die, DW_AT_language)
5334 == DW_LANG_C_plus_plus);
5337 /* Return TRUE if the language is Fortran. */
5339 static inline bool
5340 is_fortran (void)
5342 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5344 return (lang == DW_LANG_Fortran77
5345 || lang == DW_LANG_Fortran90
5346 || lang == DW_LANG_Fortran95);
5349 /* Return TRUE if the language is Java. */
5351 static inline bool
5352 is_java (void)
5354 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5356 return lang == DW_LANG_Java;
5359 /* Return TRUE if the language is Ada. */
5361 static inline bool
5362 is_ada (void)
5364 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5366 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5369 /* Free up the memory used by A. */
5371 static inline void free_AT (dw_attr_ref);
5372 static inline void
5373 free_AT (dw_attr_ref a)
5375 if (AT_class (a) == dw_val_class_str)
5376 if (a->dw_attr_val.v.val_str->refcount)
5377 a->dw_attr_val.v.val_str->refcount--;
5380 /* Remove the specified attribute if present. */
5382 static void
5383 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5385 dw_attr_ref *p;
5386 dw_attr_ref removed = NULL;
5388 if (die != NULL)
5390 for (p = &(die->die_attr); *p; p = &((*p)->dw_attr_next))
5391 if ((*p)->dw_attr == attr_kind)
5393 removed = *p;
5394 *p = (*p)->dw_attr_next;
5395 break;
5398 if (removed != 0)
5399 free_AT (removed);
5403 /* Remove child die whose die_tag is specified tag. */
5405 static void
5406 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5408 dw_die_ref current, prev, next;
5409 current = die->die_child;
5410 prev = NULL;
5411 while (current != NULL)
5413 if (current->die_tag == tag)
5415 next = current->die_sib;
5416 if (prev == NULL)
5417 die->die_child = next;
5418 else
5419 prev->die_sib = next;
5420 free_die (current);
5421 current = next;
5423 else
5425 prev = current;
5426 current = current->die_sib;
5431 /* Free up the memory used by DIE. */
5433 static inline void
5434 free_die (dw_die_ref die)
5436 remove_children (die);
5439 /* Discard the children of this DIE. */
5441 static void
5442 remove_children (dw_die_ref die)
5444 dw_die_ref child_die = die->die_child;
5446 die->die_child = NULL;
5448 while (child_die != NULL)
5450 dw_die_ref tmp_die = child_die;
5451 dw_attr_ref a;
5453 child_die = child_die->die_sib;
5455 for (a = tmp_die->die_attr; a != NULL;)
5457 dw_attr_ref tmp_a = a;
5459 a = a->dw_attr_next;
5460 free_AT (tmp_a);
5463 free_die (tmp_die);
5467 /* Add a child DIE below its parent. We build the lists up in reverse
5468 addition order, and correct that in reverse_all_dies. */
5470 static inline void
5471 add_child_die (dw_die_ref die, dw_die_ref child_die)
5473 if (die != NULL && child_die != NULL)
5475 gcc_assert (die != child_die);
5477 child_die->die_parent = die;
5478 child_die->die_sib = die->die_child;
5479 die->die_child = child_die;
5483 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5484 is the specification, to the front of PARENT's list of children. */
5486 static void
5487 splice_child_die (dw_die_ref parent, dw_die_ref child)
5489 dw_die_ref *p;
5491 /* We want the declaration DIE from inside the class, not the
5492 specification DIE at toplevel. */
5493 if (child->die_parent != parent)
5495 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5497 if (tmp)
5498 child = tmp;
5501 gcc_assert (child->die_parent == parent
5502 || (child->die_parent
5503 == get_AT_ref (parent, DW_AT_specification)));
5505 for (p = &(child->die_parent->die_child); *p; p = &((*p)->die_sib))
5506 if (*p == child)
5508 *p = child->die_sib;
5509 break;
5512 child->die_parent = parent;
5513 child->die_sib = parent->die_child;
5514 parent->die_child = child;
5517 /* Return a pointer to a newly created DIE node. */
5519 static inline dw_die_ref
5520 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5522 dw_die_ref die = ggc_alloc_cleared (sizeof (die_node));
5524 die->die_tag = tag_value;
5526 if (parent_die != NULL)
5527 add_child_die (parent_die, die);
5528 else
5530 limbo_die_node *limbo_node;
5532 limbo_node = ggc_alloc_cleared (sizeof (limbo_die_node));
5533 limbo_node->die = die;
5534 limbo_node->created_for = t;
5535 limbo_node->next = limbo_die_list;
5536 limbo_die_list = limbo_node;
5539 return die;
5542 /* Return the DIE associated with the given type specifier. */
5544 static inline dw_die_ref
5545 lookup_type_die (tree type)
5547 return TYPE_SYMTAB_DIE (type);
5550 /* Equate a DIE to a given type specifier. */
5552 static inline void
5553 equate_type_number_to_die (tree type, dw_die_ref type_die)
5555 TYPE_SYMTAB_DIE (type) = type_die;
5558 /* Returns a hash value for X (which really is a die_struct). */
5560 static hashval_t
5561 decl_die_table_hash (const void *x)
5563 return (hashval_t) ((const dw_die_ref) x)->decl_id;
5566 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5568 static int
5569 decl_die_table_eq (const void *x, const void *y)
5571 return (((const dw_die_ref) x)->decl_id == DECL_UID ((const tree) y));
5574 /* Return the DIE associated with a given declaration. */
5576 static inline dw_die_ref
5577 lookup_decl_die (tree decl)
5579 return htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
5582 /* Returns a hash value for X (which really is a var_loc_list). */
5584 static hashval_t
5585 decl_loc_table_hash (const void *x)
5587 return (hashval_t) ((const var_loc_list *) x)->decl_id;
5590 /* Return nonzero if decl_id of var_loc_list X is the same as
5591 UID of decl *Y. */
5593 static int
5594 decl_loc_table_eq (const void *x, const void *y)
5596 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const tree) y));
5599 /* Return the var_loc list associated with a given declaration. */
5601 static inline var_loc_list *
5602 lookup_decl_loc (tree decl)
5604 return htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5607 /* Equate a DIE to a particular declaration. */
5609 static void
5610 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5612 unsigned int decl_id = DECL_UID (decl);
5613 void **slot;
5615 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5616 *slot = decl_die;
5617 decl_die->decl_id = decl_id;
5620 /* Add a variable location node to the linked list for DECL. */
5622 static void
5623 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
5625 unsigned int decl_id = DECL_UID (decl);
5626 var_loc_list *temp;
5627 void **slot;
5629 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5630 if (*slot == NULL)
5632 temp = ggc_alloc_cleared (sizeof (var_loc_list));
5633 temp->decl_id = decl_id;
5634 *slot = temp;
5636 else
5637 temp = *slot;
5639 if (temp->last)
5641 /* If the current location is the same as the end of the list,
5642 we have nothing to do. */
5643 if (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
5644 NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
5646 /* Add LOC to the end of list and update LAST. */
5647 temp->last->next = loc;
5648 temp->last = loc;
5651 /* Do not add empty location to the beginning of the list. */
5652 else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
5654 temp->first = loc;
5655 temp->last = loc;
5659 /* Keep track of the number of spaces used to indent the
5660 output of the debugging routines that print the structure of
5661 the DIE internal representation. */
5662 static int print_indent;
5664 /* Indent the line the number of spaces given by print_indent. */
5666 static inline void
5667 print_spaces (FILE *outfile)
5669 fprintf (outfile, "%*s", print_indent, "");
5672 /* Print the information associated with a given DIE, and its children.
5673 This routine is a debugging aid only. */
5675 static void
5676 print_die (dw_die_ref die, FILE *outfile)
5678 dw_attr_ref a;
5679 dw_die_ref c;
5681 print_spaces (outfile);
5682 fprintf (outfile, "DIE %4lu: %s\n",
5683 die->die_offset, dwarf_tag_name (die->die_tag));
5684 print_spaces (outfile);
5685 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5686 fprintf (outfile, " offset: %lu\n", die->die_offset);
5688 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
5690 print_spaces (outfile);
5691 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5693 switch (AT_class (a))
5695 case dw_val_class_addr:
5696 fprintf (outfile, "address");
5697 break;
5698 case dw_val_class_offset:
5699 fprintf (outfile, "offset");
5700 break;
5701 case dw_val_class_loc:
5702 fprintf (outfile, "location descriptor");
5703 break;
5704 case dw_val_class_loc_list:
5705 fprintf (outfile, "location list -> label:%s",
5706 AT_loc_list (a)->ll_symbol);
5707 break;
5708 case dw_val_class_range_list:
5709 fprintf (outfile, "range list");
5710 break;
5711 case dw_val_class_const:
5712 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5713 break;
5714 case dw_val_class_unsigned_const:
5715 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5716 break;
5717 case dw_val_class_long_long:
5718 fprintf (outfile, "constant (%lu,%lu)",
5719 a->dw_attr_val.v.val_long_long.hi,
5720 a->dw_attr_val.v.val_long_long.low);
5721 break;
5722 case dw_val_class_vec:
5723 fprintf (outfile, "floating-point or vector constant");
5724 break;
5725 case dw_val_class_flag:
5726 fprintf (outfile, "%u", AT_flag (a));
5727 break;
5728 case dw_val_class_die_ref:
5729 if (AT_ref (a) != NULL)
5731 if (AT_ref (a)->die_symbol)
5732 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
5733 else
5734 fprintf (outfile, "die -> %lu", AT_ref (a)->die_offset);
5736 else
5737 fprintf (outfile, "die -> <null>");
5738 break;
5739 case dw_val_class_lbl_id:
5740 case dw_val_class_lbl_offset:
5741 fprintf (outfile, "label: %s", AT_lbl (a));
5742 break;
5743 case dw_val_class_str:
5744 if (AT_string (a) != NULL)
5745 fprintf (outfile, "\"%s\"", AT_string (a));
5746 else
5747 fprintf (outfile, "<null>");
5748 break;
5749 default:
5750 break;
5753 fprintf (outfile, "\n");
5756 if (die->die_child != NULL)
5758 print_indent += 4;
5759 for (c = die->die_child; c != NULL; c = c->die_sib)
5760 print_die (c, outfile);
5762 print_indent -= 4;
5764 if (print_indent == 0)
5765 fprintf (outfile, "\n");
5768 /* Print the contents of the source code line number correspondence table.
5769 This routine is a debugging aid only. */
5771 static void
5772 print_dwarf_line_table (FILE *outfile)
5774 unsigned i;
5775 dw_line_info_ref line_info;
5777 fprintf (outfile, "\n\nDWARF source line information\n");
5778 for (i = 1; i < line_info_table_in_use; i++)
5780 line_info = &line_info_table[i];
5781 fprintf (outfile, "%5d: ", i);
5782 fprintf (outfile, "%-20s",
5783 VARRAY_CHAR_PTR (file_table, line_info->dw_file_num));
5784 fprintf (outfile, "%6ld", line_info->dw_line_num);
5785 fprintf (outfile, "\n");
5788 fprintf (outfile, "\n\n");
5791 /* Print the information collected for a given DIE. */
5793 void
5794 debug_dwarf_die (dw_die_ref die)
5796 print_die (die, stderr);
5799 /* Print all DWARF information collected for the compilation unit.
5800 This routine is a debugging aid only. */
5802 void
5803 debug_dwarf (void)
5805 print_indent = 0;
5806 print_die (comp_unit_die, stderr);
5807 if (! DWARF2_ASM_LINE_DEBUG_INFO)
5808 print_dwarf_line_table (stderr);
5811 /* We build up the lists of children and attributes by pushing new ones
5812 onto the beginning of the list. Reverse the lists for DIE so that
5813 they are in order of addition. */
5815 static void
5816 reverse_die_lists (dw_die_ref die)
5818 dw_die_ref c, cp, cn;
5819 dw_attr_ref a, ap, an;
5821 for (a = die->die_attr, ap = 0; a; a = an)
5823 an = a->dw_attr_next;
5824 a->dw_attr_next = ap;
5825 ap = a;
5828 die->die_attr = ap;
5830 for (c = die->die_child, cp = 0; c; c = cn)
5832 cn = c->die_sib;
5833 c->die_sib = cp;
5834 cp = c;
5837 die->die_child = cp;
5840 /* reverse_die_lists only reverses the single die you pass it. Since we used to
5841 reverse all dies in add_sibling_attributes, which runs through all the dies,
5842 it would reverse all the dies. Now, however, since we don't call
5843 reverse_die_lists in add_sibling_attributes, we need a routine to
5844 recursively reverse all the dies. This is that routine. */
5846 static void
5847 reverse_all_dies (dw_die_ref die)
5849 dw_die_ref c;
5851 reverse_die_lists (die);
5853 for (c = die->die_child; c; c = c->die_sib)
5854 reverse_all_dies (c);
5857 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5858 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5859 DIE that marks the start of the DIEs for this include file. */
5861 static dw_die_ref
5862 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5864 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5865 dw_die_ref new_unit = gen_compile_unit_die (filename);
5867 new_unit->die_sib = old_unit;
5868 return new_unit;
5871 /* Close an include-file CU and reopen the enclosing one. */
5873 static dw_die_ref
5874 pop_compile_unit (dw_die_ref old_unit)
5876 dw_die_ref new_unit = old_unit->die_sib;
5878 old_unit->die_sib = NULL;
5879 return new_unit;
5882 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5883 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5885 /* Calculate the checksum of a location expression. */
5887 static inline void
5888 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5890 CHECKSUM (loc->dw_loc_opc);
5891 CHECKSUM (loc->dw_loc_oprnd1);
5892 CHECKSUM (loc->dw_loc_oprnd2);
5895 /* Calculate the checksum of an attribute. */
5897 static void
5898 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5900 dw_loc_descr_ref loc;
5901 rtx r;
5903 CHECKSUM (at->dw_attr);
5905 /* We don't care about differences in file numbering. */
5906 if (at->dw_attr == DW_AT_decl_file
5907 /* Or that this was compiled with a different compiler snapshot; if
5908 the output is the same, that's what matters. */
5909 || at->dw_attr == DW_AT_producer)
5910 return;
5912 switch (AT_class (at))
5914 case dw_val_class_const:
5915 CHECKSUM (at->dw_attr_val.v.val_int);
5916 break;
5917 case dw_val_class_unsigned_const:
5918 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5919 break;
5920 case dw_val_class_long_long:
5921 CHECKSUM (at->dw_attr_val.v.val_long_long);
5922 break;
5923 case dw_val_class_vec:
5924 CHECKSUM (at->dw_attr_val.v.val_vec);
5925 break;
5926 case dw_val_class_flag:
5927 CHECKSUM (at->dw_attr_val.v.val_flag);
5928 break;
5929 case dw_val_class_str:
5930 CHECKSUM_STRING (AT_string (at));
5931 break;
5933 case dw_val_class_addr:
5934 r = AT_addr (at);
5935 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5936 CHECKSUM_STRING (XSTR (r, 0));
5937 break;
5939 case dw_val_class_offset:
5940 CHECKSUM (at->dw_attr_val.v.val_offset);
5941 break;
5943 case dw_val_class_loc:
5944 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5945 loc_checksum (loc, ctx);
5946 break;
5948 case dw_val_class_die_ref:
5949 die_checksum (AT_ref (at), ctx, mark);
5950 break;
5952 case dw_val_class_fde_ref:
5953 case dw_val_class_lbl_id:
5954 case dw_val_class_lbl_offset:
5955 break;
5957 default:
5958 break;
5962 /* Calculate the checksum of a DIE. */
5964 static void
5965 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5967 dw_die_ref c;
5968 dw_attr_ref a;
5970 /* To avoid infinite recursion. */
5971 if (die->die_mark)
5973 CHECKSUM (die->die_mark);
5974 return;
5976 die->die_mark = ++(*mark);
5978 CHECKSUM (die->die_tag);
5980 for (a = die->die_attr; a; a = a->dw_attr_next)
5981 attr_checksum (a, ctx, mark);
5983 for (c = die->die_child; c; c = c->die_sib)
5984 die_checksum (c, ctx, mark);
5987 #undef CHECKSUM
5988 #undef CHECKSUM_STRING
5990 /* Do the location expressions look same? */
5991 static inline int
5992 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
5994 return loc1->dw_loc_opc == loc2->dw_loc_opc
5995 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
5996 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
5999 /* Do the values look the same? */
6000 static int
6001 same_dw_val_p (dw_val_node *v1, dw_val_node *v2, int *mark)
6003 dw_loc_descr_ref loc1, loc2;
6004 rtx r1, r2;
6006 if (v1->val_class != v2->val_class)
6007 return 0;
6009 switch (v1->val_class)
6011 case dw_val_class_const:
6012 return v1->v.val_int == v2->v.val_int;
6013 case dw_val_class_unsigned_const:
6014 return v1->v.val_unsigned == v2->v.val_unsigned;
6015 case dw_val_class_long_long:
6016 return v1->v.val_long_long.hi == v2->v.val_long_long.hi
6017 && v1->v.val_long_long.low == v2->v.val_long_long.low;
6018 case dw_val_class_vec:
6019 if (v1->v.val_vec.length != v2->v.val_vec.length
6020 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6021 return 0;
6022 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6023 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6024 return 0;
6025 return 1;
6026 case dw_val_class_flag:
6027 return v1->v.val_flag == v2->v.val_flag;
6028 case dw_val_class_str:
6029 return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6031 case dw_val_class_addr:
6032 r1 = v1->v.val_addr;
6033 r2 = v2->v.val_addr;
6034 if (GET_CODE (r1) != GET_CODE (r2))
6035 return 0;
6036 gcc_assert (GET_CODE (r1) == SYMBOL_REF);
6037 return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
6039 case dw_val_class_offset:
6040 return v1->v.val_offset == v2->v.val_offset;
6042 case dw_val_class_loc:
6043 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6044 loc1 && loc2;
6045 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6046 if (!same_loc_p (loc1, loc2, mark))
6047 return 0;
6048 return !loc1 && !loc2;
6050 case dw_val_class_die_ref:
6051 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6053 case dw_val_class_fde_ref:
6054 case dw_val_class_lbl_id:
6055 case dw_val_class_lbl_offset:
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))
6243 || c->die_tag == DW_TAG_namespace);
6246 static char *
6247 gen_internal_sym (const char *prefix)
6249 char buf[256];
6251 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6252 return xstrdup (buf);
6255 /* Assign symbols to all worthy DIEs under DIE. */
6257 static void
6258 assign_symbol_names (dw_die_ref die)
6260 dw_die_ref c;
6262 if (is_symbol_die (die))
6264 if (comdat_symbol_id)
6266 char *p = alloca (strlen (comdat_symbol_id) + 64);
6268 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6269 comdat_symbol_id, comdat_symbol_number++);
6270 die->die_symbol = xstrdup (p);
6272 else
6273 die->die_symbol = gen_internal_sym ("LDIE");
6276 for (c = die->die_child; c != NULL; c = c->die_sib)
6277 assign_symbol_names (c);
6280 struct cu_hash_table_entry
6282 dw_die_ref cu;
6283 unsigned min_comdat_num, max_comdat_num;
6284 struct cu_hash_table_entry *next;
6287 /* Routines to manipulate hash table of CUs. */
6288 static hashval_t
6289 htab_cu_hash (const void *of)
6291 const struct cu_hash_table_entry *entry = of;
6293 return htab_hash_string (entry->cu->die_symbol);
6296 static int
6297 htab_cu_eq (const void *of1, const void *of2)
6299 const struct cu_hash_table_entry *entry1 = of1;
6300 const struct die_struct *entry2 = of2;
6302 return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
6305 static void
6306 htab_cu_del (void *what)
6308 struct cu_hash_table_entry *next, *entry = what;
6310 while (entry)
6312 next = entry->next;
6313 free (entry);
6314 entry = next;
6318 /* Check whether we have already seen this CU and set up SYM_NUM
6319 accordingly. */
6320 static int
6321 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6323 struct cu_hash_table_entry dummy;
6324 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6326 dummy.max_comdat_num = 0;
6328 slot = (struct cu_hash_table_entry **)
6329 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6330 INSERT);
6331 entry = *slot;
6333 for (; entry; last = entry, entry = entry->next)
6335 if (same_die_p_wrap (cu, entry->cu))
6336 break;
6339 if (entry)
6341 *sym_num = entry->min_comdat_num;
6342 return 1;
6345 entry = xcalloc (1, sizeof (struct cu_hash_table_entry));
6346 entry->cu = cu;
6347 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6348 entry->next = *slot;
6349 *slot = entry;
6351 return 0;
6354 /* Record SYM_NUM to record of CU in HTABLE. */
6355 static void
6356 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6358 struct cu_hash_table_entry **slot, *entry;
6360 slot = (struct cu_hash_table_entry **)
6361 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6362 NO_INSERT);
6363 entry = *slot;
6365 entry->max_comdat_num = sym_num;
6368 /* Traverse the DIE (which is always comp_unit_die), and set up
6369 additional compilation units for each of the include files we see
6370 bracketed by BINCL/EINCL. */
6372 static void
6373 break_out_includes (dw_die_ref die)
6375 dw_die_ref *ptr;
6376 dw_die_ref unit = NULL;
6377 limbo_die_node *node, **pnode;
6378 htab_t cu_hash_table;
6380 for (ptr = &(die->die_child); *ptr;)
6382 dw_die_ref c = *ptr;
6384 if (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6385 || (unit && is_comdat_die (c)))
6387 /* This DIE is for a secondary CU; remove it from the main one. */
6388 *ptr = c->die_sib;
6390 if (c->die_tag == DW_TAG_GNU_BINCL)
6392 unit = push_new_compile_unit (unit, c);
6393 free_die (c);
6395 else if (c->die_tag == DW_TAG_GNU_EINCL)
6397 unit = pop_compile_unit (unit);
6398 free_die (c);
6400 else
6401 add_child_die (unit, c);
6403 else
6405 /* Leave this DIE in the main CU. */
6406 ptr = &(c->die_sib);
6407 continue;
6411 #if 0
6412 /* We can only use this in debugging, since the frontend doesn't check
6413 to make sure that we leave every include file we enter. */
6414 gcc_assert (!unit);
6415 #endif
6417 assign_symbol_names (die);
6418 cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6419 for (node = limbo_die_list, pnode = &limbo_die_list;
6420 node;
6421 node = node->next)
6423 int is_dupl;
6425 compute_section_prefix (node->die);
6426 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6427 &comdat_symbol_number);
6428 assign_symbol_names (node->die);
6429 if (is_dupl)
6430 *pnode = node->next;
6431 else
6433 pnode = &node->next;
6434 record_comdat_symbol_number (node->die, cu_hash_table,
6435 comdat_symbol_number);
6438 htab_delete (cu_hash_table);
6441 /* Traverse the DIE and add a sibling attribute if it may have the
6442 effect of speeding up access to siblings. To save some space,
6443 avoid generating sibling attributes for DIE's without children. */
6445 static void
6446 add_sibling_attributes (dw_die_ref die)
6448 dw_die_ref c;
6450 if (die->die_tag != DW_TAG_compile_unit
6451 && die->die_sib && die->die_child != NULL)
6452 /* Add the sibling link to the front of the attribute list. */
6453 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
6455 for (c = die->die_child; c != NULL; c = c->die_sib)
6456 add_sibling_attributes (c);
6459 /* Output all location lists for the DIE and its children. */
6461 static void
6462 output_location_lists (dw_die_ref die)
6464 dw_die_ref c;
6465 dw_attr_ref d_attr;
6467 for (d_attr = die->die_attr; d_attr; d_attr = d_attr->dw_attr_next)
6468 if (AT_class (d_attr) == dw_val_class_loc_list)
6469 output_loc_list (AT_loc_list (d_attr));
6471 for (c = die->die_child; c != NULL; c = c->die_sib)
6472 output_location_lists (c);
6476 /* The format of each DIE (and its attribute value pairs) is encoded in an
6477 abbreviation table. This routine builds the abbreviation table and assigns
6478 a unique abbreviation id for each abbreviation entry. The children of each
6479 die are visited recursively. */
6481 static void
6482 build_abbrev_table (dw_die_ref die)
6484 unsigned long abbrev_id;
6485 unsigned int n_alloc;
6486 dw_die_ref c;
6487 dw_attr_ref d_attr, a_attr;
6489 /* Scan the DIE references, and mark as external any that refer to
6490 DIEs from other CUs (i.e. those which are not marked). */
6491 for (d_attr = die->die_attr; d_attr; d_attr = d_attr->dw_attr_next)
6492 if (AT_class (d_attr) == dw_val_class_die_ref
6493 && AT_ref (d_attr)->die_mark == 0)
6495 gcc_assert (AT_ref (d_attr)->die_symbol);
6497 set_AT_ref_external (d_attr, 1);
6500 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6502 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6504 if (abbrev->die_tag == die->die_tag)
6506 if ((abbrev->die_child != NULL) == (die->die_child != NULL))
6508 a_attr = abbrev->die_attr;
6509 d_attr = die->die_attr;
6511 while (a_attr != NULL && d_attr != NULL)
6513 if ((a_attr->dw_attr != d_attr->dw_attr)
6514 || (value_format (a_attr) != value_format (d_attr)))
6515 break;
6517 a_attr = a_attr->dw_attr_next;
6518 d_attr = d_attr->dw_attr_next;
6521 if (a_attr == NULL && d_attr == NULL)
6522 break;
6527 if (abbrev_id >= abbrev_die_table_in_use)
6529 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
6531 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
6532 abbrev_die_table = ggc_realloc (abbrev_die_table,
6533 sizeof (dw_die_ref) * n_alloc);
6535 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
6536 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
6537 abbrev_die_table_allocated = n_alloc;
6540 ++abbrev_die_table_in_use;
6541 abbrev_die_table[abbrev_id] = die;
6544 die->die_abbrev = abbrev_id;
6545 for (c = die->die_child; c != NULL; c = c->die_sib)
6546 build_abbrev_table (c);
6549 /* Return the power-of-two number of bytes necessary to represent VALUE. */
6551 static int
6552 constant_size (long unsigned int value)
6554 int log;
6556 if (value == 0)
6557 log = 0;
6558 else
6559 log = floor_log2 (value);
6561 log = log / 8;
6562 log = 1 << (floor_log2 (log) + 1);
6564 return log;
6567 /* Return the size of a DIE as it is represented in the
6568 .debug_info section. */
6570 static unsigned long
6571 size_of_die (dw_die_ref die)
6573 unsigned long size = 0;
6574 dw_attr_ref a;
6576 size += size_of_uleb128 (die->die_abbrev);
6577 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
6579 switch (AT_class (a))
6581 case dw_val_class_addr:
6582 size += DWARF2_ADDR_SIZE;
6583 break;
6584 case dw_val_class_offset:
6585 size += DWARF_OFFSET_SIZE;
6586 break;
6587 case dw_val_class_loc:
6589 unsigned long lsize = size_of_locs (AT_loc (a));
6591 /* Block length. */
6592 size += constant_size (lsize);
6593 size += lsize;
6595 break;
6596 case dw_val_class_loc_list:
6597 size += DWARF_OFFSET_SIZE;
6598 break;
6599 case dw_val_class_range_list:
6600 size += DWARF_OFFSET_SIZE;
6601 break;
6602 case dw_val_class_const:
6603 size += size_of_sleb128 (AT_int (a));
6604 break;
6605 case dw_val_class_unsigned_const:
6606 size += constant_size (AT_unsigned (a));
6607 break;
6608 case dw_val_class_long_long:
6609 size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
6610 break;
6611 case dw_val_class_vec:
6612 size += 1 + (a->dw_attr_val.v.val_vec.length
6613 * a->dw_attr_val.v.val_vec.elt_size); /* block */
6614 break;
6615 case dw_val_class_flag:
6616 size += 1;
6617 break;
6618 case dw_val_class_die_ref:
6619 if (AT_ref_external (a))
6620 size += DWARF2_ADDR_SIZE;
6621 else
6622 size += DWARF_OFFSET_SIZE;
6623 break;
6624 case dw_val_class_fde_ref:
6625 size += DWARF_OFFSET_SIZE;
6626 break;
6627 case dw_val_class_lbl_id:
6628 size += DWARF2_ADDR_SIZE;
6629 break;
6630 case dw_val_class_lbl_offset:
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 switch (DWARF_OFFSET_SIZE)
6770 case 4:
6771 return DW_FORM_data4;
6772 case 8:
6773 return DW_FORM_data8;
6774 default:
6775 gcc_unreachable ();
6777 case dw_val_class_loc_list:
6778 /* FIXME: Could be DW_FORM_data8, with a > 32 bit size
6779 .debug_loc section */
6780 return DW_FORM_data4;
6781 case dw_val_class_loc:
6782 switch (constant_size (size_of_locs (AT_loc (a))))
6784 case 1:
6785 return DW_FORM_block1;
6786 case 2:
6787 return DW_FORM_block2;
6788 default:
6789 gcc_unreachable ();
6791 case dw_val_class_const:
6792 return DW_FORM_sdata;
6793 case dw_val_class_unsigned_const:
6794 switch (constant_size (AT_unsigned (a)))
6796 case 1:
6797 return DW_FORM_data1;
6798 case 2:
6799 return DW_FORM_data2;
6800 case 4:
6801 return DW_FORM_data4;
6802 case 8:
6803 return DW_FORM_data8;
6804 default:
6805 gcc_unreachable ();
6807 case dw_val_class_long_long:
6808 return DW_FORM_block1;
6809 case dw_val_class_vec:
6810 return DW_FORM_block1;
6811 case dw_val_class_flag:
6812 return DW_FORM_flag;
6813 case dw_val_class_die_ref:
6814 if (AT_ref_external (a))
6815 return DW_FORM_ref_addr;
6816 else
6817 return DW_FORM_ref;
6818 case dw_val_class_fde_ref:
6819 return DW_FORM_data;
6820 case dw_val_class_lbl_id:
6821 return DW_FORM_addr;
6822 case dw_val_class_lbl_offset:
6823 return DW_FORM_data;
6824 case dw_val_class_str:
6825 return AT_string_form (a);
6827 default:
6828 gcc_unreachable ();
6832 /* Output the encoding of an attribute value. */
6834 static void
6835 output_value_format (dw_attr_ref a)
6837 enum dwarf_form form = value_format (a);
6839 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
6842 /* Output the .debug_abbrev section which defines the DIE abbreviation
6843 table. */
6845 static void
6846 output_abbrev_section (void)
6848 unsigned long abbrev_id;
6850 dw_attr_ref a_attr;
6852 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6854 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6856 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
6857 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
6858 dwarf_tag_name (abbrev->die_tag));
6860 if (abbrev->die_child != NULL)
6861 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
6862 else
6863 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
6865 for (a_attr = abbrev->die_attr; a_attr != NULL;
6866 a_attr = a_attr->dw_attr_next)
6868 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
6869 dwarf_attr_name (a_attr->dw_attr));
6870 output_value_format (a_attr);
6873 dw2_asm_output_data (1, 0, NULL);
6874 dw2_asm_output_data (1, 0, NULL);
6877 /* Terminate the table. */
6878 dw2_asm_output_data (1, 0, NULL);
6881 /* Output a symbol we can use to refer to this DIE from another CU. */
6883 static inline void
6884 output_die_symbol (dw_die_ref die)
6886 char *sym = die->die_symbol;
6888 if (sym == 0)
6889 return;
6891 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
6892 /* We make these global, not weak; if the target doesn't support
6893 .linkonce, it doesn't support combining the sections, so debugging
6894 will break. */
6895 targetm.asm_out.globalize_label (asm_out_file, sym);
6897 ASM_OUTPUT_LABEL (asm_out_file, sym);
6900 /* Return a new location list, given the begin and end range, and the
6901 expression. gensym tells us whether to generate a new internal symbol for
6902 this location list node, which is done for the head of the list only. */
6904 static inline dw_loc_list_ref
6905 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
6906 const char *section, unsigned int gensym)
6908 dw_loc_list_ref retlist = ggc_alloc_cleared (sizeof (dw_loc_list_node));
6910 retlist->begin = begin;
6911 retlist->end = end;
6912 retlist->expr = expr;
6913 retlist->section = section;
6914 if (gensym)
6915 retlist->ll_symbol = gen_internal_sym ("LLST");
6917 return retlist;
6920 /* Add a location description expression to a location list. */
6922 static inline void
6923 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
6924 const char *begin, const char *end,
6925 const char *section)
6927 dw_loc_list_ref *d;
6929 /* Find the end of the chain. */
6930 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
6933 /* Add a new location list node to the list. */
6934 *d = new_loc_list (descr, begin, end, section, 0);
6937 /* Output the location list given to us. */
6939 static void
6940 output_loc_list (dw_loc_list_ref list_head)
6942 dw_loc_list_ref curr = list_head;
6944 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
6946 /* Walk the location list, and output each range + expression. */
6947 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
6949 unsigned long size;
6950 if (!separate_line_info_table_in_use && !have_switched_text_section)
6952 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
6953 "Location list begin address (%s)",
6954 list_head->ll_symbol);
6955 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
6956 "Location list end address (%s)",
6957 list_head->ll_symbol);
6959 else
6961 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
6962 "Location list begin address (%s)",
6963 list_head->ll_symbol);
6964 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
6965 "Location list end address (%s)",
6966 list_head->ll_symbol);
6968 size = size_of_locs (curr->expr);
6970 /* Output the block length for this list of location operations. */
6971 gcc_assert (size <= 0xffff);
6972 dw2_asm_output_data (2, size, "%s", "Location expression size");
6974 output_loc_sequence (curr->expr);
6977 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
6978 "Location list terminator begin (%s)",
6979 list_head->ll_symbol);
6980 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
6981 "Location list terminator end (%s)",
6982 list_head->ll_symbol);
6985 /* Output the DIE and its attributes. Called recursively to generate
6986 the definitions of each child DIE. */
6988 static void
6989 output_die (dw_die_ref die)
6991 dw_attr_ref a;
6992 dw_die_ref c;
6993 unsigned long size;
6995 /* If someone in another CU might refer to us, set up a symbol for
6996 them to point to. */
6997 if (die->die_symbol)
6998 output_die_symbol (die);
7000 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
7001 die->die_offset, dwarf_tag_name (die->die_tag));
7003 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
7005 const char *name = dwarf_attr_name (a->dw_attr);
7007 switch (AT_class (a))
7009 case dw_val_class_addr:
7010 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
7011 break;
7013 case dw_val_class_offset:
7014 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
7015 "%s", name);
7016 break;
7018 case dw_val_class_range_list:
7020 char *p = strchr (ranges_section_label, '\0');
7022 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
7023 a->dw_attr_val.v.val_offset);
7024 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
7025 "%s", name);
7026 *p = '\0';
7028 break;
7030 case dw_val_class_loc:
7031 size = size_of_locs (AT_loc (a));
7033 /* Output the block length for this list of location operations. */
7034 dw2_asm_output_data (constant_size (size), size, "%s", name);
7036 output_loc_sequence (AT_loc (a));
7037 break;
7039 case dw_val_class_const:
7040 /* ??? It would be slightly more efficient to use a scheme like is
7041 used for unsigned constants below, but gdb 4.x does not sign
7042 extend. Gdb 5.x does sign extend. */
7043 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
7044 break;
7046 case dw_val_class_unsigned_const:
7047 dw2_asm_output_data (constant_size (AT_unsigned (a)),
7048 AT_unsigned (a), "%s", name);
7049 break;
7051 case dw_val_class_long_long:
7053 unsigned HOST_WIDE_INT first, second;
7055 dw2_asm_output_data (1,
7056 2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7057 "%s", name);
7059 if (WORDS_BIG_ENDIAN)
7061 first = a->dw_attr_val.v.val_long_long.hi;
7062 second = a->dw_attr_val.v.val_long_long.low;
7064 else
7066 first = a->dw_attr_val.v.val_long_long.low;
7067 second = a->dw_attr_val.v.val_long_long.hi;
7070 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7071 first, "long long constant");
7072 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7073 second, NULL);
7075 break;
7077 case dw_val_class_vec:
7079 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
7080 unsigned int len = a->dw_attr_val.v.val_vec.length;
7081 unsigned int i;
7082 unsigned char *p;
7084 dw2_asm_output_data (1, len * elt_size, "%s", name);
7085 if (elt_size > sizeof (HOST_WIDE_INT))
7087 elt_size /= 2;
7088 len *= 2;
7090 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
7091 i < len;
7092 i++, p += elt_size)
7093 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
7094 "fp or vector constant word %u", i);
7095 break;
7098 case dw_val_class_flag:
7099 dw2_asm_output_data (1, AT_flag (a), "%s", name);
7100 break;
7102 case dw_val_class_loc_list:
7104 char *sym = AT_loc_list (a)->ll_symbol;
7106 gcc_assert (sym);
7107 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, "%s", name);
7109 break;
7111 case dw_val_class_die_ref:
7112 if (AT_ref_external (a))
7114 char *sym = AT_ref (a)->die_symbol;
7116 gcc_assert (sym);
7117 dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, "%s", name);
7119 else
7121 gcc_assert (AT_ref (a)->die_offset);
7122 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
7123 "%s", name);
7125 break;
7127 case dw_val_class_fde_ref:
7129 char l1[20];
7131 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
7132 a->dw_attr_val.v.val_fde_index * 2);
7133 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, "%s", name);
7135 break;
7137 case dw_val_class_lbl_id:
7138 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
7139 break;
7141 case dw_val_class_lbl_offset:
7142 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a), "%s", name);
7143 break;
7145 case dw_val_class_str:
7146 if (AT_string_form (a) == DW_FORM_strp)
7147 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
7148 a->dw_attr_val.v.val_str->label,
7149 "%s: \"%s\"", name, AT_string (a));
7150 else
7151 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
7152 break;
7154 default:
7155 gcc_unreachable ();
7159 for (c = die->die_child; c != NULL; c = c->die_sib)
7160 output_die (c);
7162 /* Add null byte to terminate sibling list. */
7163 if (die->die_child != NULL)
7164 dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
7165 die->die_offset);
7168 /* Output the compilation unit that appears at the beginning of the
7169 .debug_info section, and precedes the DIE descriptions. */
7171 static void
7172 output_compilation_unit_header (void)
7174 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7175 dw2_asm_output_data (4, 0xffffffff,
7176 "Initial length escape value indicating 64-bit DWARF extension");
7177 dw2_asm_output_data (DWARF_OFFSET_SIZE,
7178 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
7179 "Length of Compilation Unit Info");
7180 dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
7181 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
7182 "Offset Into Abbrev. Section");
7183 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
7186 /* Output the compilation unit DIE and its children. */
7188 static void
7189 output_comp_unit (dw_die_ref die, int output_if_empty)
7191 const char *secname;
7192 char *oldsym, *tmp;
7194 /* Unless we are outputting main CU, we may throw away empty ones. */
7195 if (!output_if_empty && die->die_child == NULL)
7196 return;
7198 /* Even if there are no children of this DIE, we must output the information
7199 about the compilation unit. Otherwise, on an empty translation unit, we
7200 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
7201 will then complain when examining the file. First mark all the DIEs in
7202 this CU so we know which get local refs. */
7203 mark_dies (die);
7205 build_abbrev_table (die);
7207 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
7208 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7209 calc_die_sizes (die);
7211 oldsym = die->die_symbol;
7212 if (oldsym)
7214 tmp = alloca (strlen (oldsym) + 24);
7216 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
7217 secname = tmp;
7218 die->die_symbol = NULL;
7220 else
7221 secname = (const char *) DEBUG_INFO_SECTION;
7223 /* Output debugging information. */
7224 named_section_flags (secname, SECTION_DEBUG);
7225 output_compilation_unit_header ();
7226 output_die (die);
7228 /* Leave the marks on the main CU, so we can check them in
7229 output_pubnames. */
7230 if (oldsym)
7232 unmark_dies (die);
7233 die->die_symbol = oldsym;
7237 /* The DWARF2 pubname for a nested thingy looks like "A::f". The
7238 output of lang_hooks.decl_printable_name for C++ looks like
7239 "A::f(int)". Let's drop the argument list, and maybe the scope. */
7241 static const char *
7242 dwarf2_name (tree decl, int scope)
7244 return lang_hooks.decl_printable_name (decl, scope ? 1 : 0);
7247 /* Add a new entry to .debug_pubnames if appropriate. */
7249 static void
7250 add_pubname (tree decl, dw_die_ref die)
7252 pubname_ref p;
7254 if (! TREE_PUBLIC (decl))
7255 return;
7257 if (pubname_table_in_use == pubname_table_allocated)
7259 pubname_table_allocated += PUBNAME_TABLE_INCREMENT;
7260 pubname_table
7261 = ggc_realloc (pubname_table,
7262 (pubname_table_allocated * sizeof (pubname_entry)));
7263 memset (pubname_table + pubname_table_in_use, 0,
7264 PUBNAME_TABLE_INCREMENT * sizeof (pubname_entry));
7267 p = &pubname_table[pubname_table_in_use++];
7268 p->die = die;
7269 p->name = xstrdup (dwarf2_name (decl, 1));
7272 /* Output the public names table used to speed up access to externally
7273 visible names. For now, only generate entries for externally
7274 visible procedures. */
7276 static void
7277 output_pubnames (void)
7279 unsigned i;
7280 unsigned long pubnames_length = size_of_pubnames ();
7282 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7283 dw2_asm_output_data (4, 0xffffffff,
7284 "Initial length escape value indicating 64-bit DWARF extension");
7285 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7286 "Length of Public Names Info");
7287 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7288 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7289 "Offset of Compilation Unit Info");
7290 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
7291 "Compilation Unit Length");
7293 for (i = 0; i < pubname_table_in_use; i++)
7295 pubname_ref pub = &pubname_table[i];
7297 /* We shouldn't see pubnames for DIEs outside of the main CU. */
7298 gcc_assert (pub->die->die_mark);
7300 dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
7301 "DIE offset");
7303 dw2_asm_output_nstring (pub->name, -1, "external name");
7306 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
7309 /* Add a new entry to .debug_aranges if appropriate. */
7311 static void
7312 add_arange (tree decl, dw_die_ref die)
7314 if (! DECL_SECTION_NAME (decl))
7315 return;
7317 if (arange_table_in_use == arange_table_allocated)
7319 arange_table_allocated += ARANGE_TABLE_INCREMENT;
7320 arange_table = ggc_realloc (arange_table,
7321 (arange_table_allocated
7322 * sizeof (dw_die_ref)));
7323 memset (arange_table + arange_table_in_use, 0,
7324 ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
7327 arange_table[arange_table_in_use++] = die;
7330 /* Output the information that goes into the .debug_aranges table.
7331 Namely, define the beginning and ending address range of the
7332 text section generated for this compilation unit. */
7334 static void
7335 output_aranges (void)
7337 unsigned i;
7338 unsigned long aranges_length = size_of_aranges ();
7340 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7341 dw2_asm_output_data (4, 0xffffffff,
7342 "Initial length escape value indicating 64-bit DWARF extension");
7343 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
7344 "Length of Address Ranges Info");
7345 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7346 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7347 "Offset of Compilation Unit Info");
7348 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
7349 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
7351 /* We need to align to twice the pointer size here. */
7352 if (DWARF_ARANGES_PAD_SIZE)
7354 /* Pad using a 2 byte words so that padding is correct for any
7355 pointer size. */
7356 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
7357 2 * DWARF2_ADDR_SIZE);
7358 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
7359 dw2_asm_output_data (2, 0, NULL);
7362 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
7363 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
7364 text_section_label, "Length");
7365 if (flag_reorder_blocks_and_partition)
7367 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
7368 "Address");
7369 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
7370 cold_text_section_label, "Length");
7373 for (i = 0; i < arange_table_in_use; i++)
7375 dw_die_ref die = arange_table[i];
7377 /* We shouldn't see aranges for DIEs outside of the main CU. */
7378 gcc_assert (die->die_mark);
7380 if (die->die_tag == DW_TAG_subprogram)
7382 dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
7383 "Address");
7384 dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
7385 get_AT_low_pc (die), "Length");
7387 else
7389 /* A static variable; extract the symbol from DW_AT_location.
7390 Note that this code isn't currently hit, as we only emit
7391 aranges for functions (jason 9/23/99). */
7392 dw_attr_ref a = get_AT (die, DW_AT_location);
7393 dw_loc_descr_ref loc;
7395 gcc_assert (a && AT_class (a) == dw_val_class_loc);
7397 loc = AT_loc (a);
7398 gcc_assert (loc->dw_loc_opc == DW_OP_addr);
7400 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
7401 loc->dw_loc_oprnd1.v.val_addr, "Address");
7402 dw2_asm_output_data (DWARF2_ADDR_SIZE,
7403 get_AT_unsigned (die, DW_AT_byte_size),
7404 "Length");
7408 /* Output the terminator words. */
7409 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7410 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7413 /* Add a new entry to .debug_ranges. Return the offset at which it
7414 was placed. */
7416 static unsigned int
7417 add_ranges (tree block)
7419 unsigned int in_use = ranges_table_in_use;
7421 if (in_use == ranges_table_allocated)
7423 ranges_table_allocated += RANGES_TABLE_INCREMENT;
7424 ranges_table
7425 = ggc_realloc (ranges_table, (ranges_table_allocated
7426 * sizeof (struct dw_ranges_struct)));
7427 memset (ranges_table + ranges_table_in_use, 0,
7428 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
7431 ranges_table[in_use].block_num = (block ? BLOCK_NUMBER (block) : 0);
7432 ranges_table_in_use = in_use + 1;
7434 return in_use * 2 * DWARF2_ADDR_SIZE;
7437 static void
7438 output_ranges (void)
7440 unsigned i;
7441 static const char *const start_fmt = "Offset 0x%x";
7442 const char *fmt = start_fmt;
7444 for (i = 0; i < ranges_table_in_use; i++)
7446 int block_num = ranges_table[i].block_num;
7448 if (block_num)
7450 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
7451 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
7453 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
7454 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
7456 /* If all code is in the text section, then the compilation
7457 unit base address defaults to DW_AT_low_pc, which is the
7458 base of the text section. */
7459 if (!separate_line_info_table_in_use && !have_switched_text_section)
7461 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
7462 text_section_label,
7463 fmt, i * 2 * DWARF2_ADDR_SIZE);
7464 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
7465 text_section_label, NULL);
7468 /* Otherwise, we add a DW_AT_entry_pc attribute to force the
7469 compilation unit base address to zero, which allows us to
7470 use absolute addresses, and not worry about whether the
7471 target supports cross-section arithmetic. */
7472 else
7474 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
7475 fmt, i * 2 * DWARF2_ADDR_SIZE);
7476 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
7479 fmt = NULL;
7481 else
7483 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7484 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7485 fmt = start_fmt;
7490 /* Data structure containing information about input files. */
7491 struct file_info
7493 char *path; /* Complete file name. */
7494 char *fname; /* File name part. */
7495 int length; /* Length of entire string. */
7496 int file_idx; /* Index in input file table. */
7497 int dir_idx; /* Index in directory table. */
7500 /* Data structure containing information about directories with source
7501 files. */
7502 struct dir_info
7504 char *path; /* Path including directory name. */
7505 int length; /* Path length. */
7506 int prefix; /* Index of directory entry which is a prefix. */
7507 int count; /* Number of files in this directory. */
7508 int dir_idx; /* Index of directory used as base. */
7509 int used; /* Used in the end? */
7512 /* Callback function for file_info comparison. We sort by looking at
7513 the directories in the path. */
7515 static int
7516 file_info_cmp (const void *p1, const void *p2)
7518 const struct file_info *s1 = p1;
7519 const struct file_info *s2 = p2;
7520 unsigned char *cp1;
7521 unsigned char *cp2;
7523 /* Take care of file names without directories. We need to make sure that
7524 we return consistent values to qsort since some will get confused if
7525 we return the same value when identical operands are passed in opposite
7526 orders. So if neither has a directory, return 0 and otherwise return
7527 1 or -1 depending on which one has the directory. */
7528 if ((s1->path == s1->fname || s2->path == s2->fname))
7529 return (s2->path == s2->fname) - (s1->path == s1->fname);
7531 cp1 = (unsigned char *) s1->path;
7532 cp2 = (unsigned char *) s2->path;
7534 while (1)
7536 ++cp1;
7537 ++cp2;
7538 /* Reached the end of the first path? If so, handle like above. */
7539 if ((cp1 == (unsigned char *) s1->fname)
7540 || (cp2 == (unsigned char *) s2->fname))
7541 return ((cp2 == (unsigned char *) s2->fname)
7542 - (cp1 == (unsigned char *) s1->fname));
7544 /* Character of current path component the same? */
7545 else if (*cp1 != *cp2)
7546 return *cp1 - *cp2;
7550 /* Output the directory table and the file name table. We try to minimize
7551 the total amount of memory needed. A heuristic is used to avoid large
7552 slowdowns with many input files. */
7554 static void
7555 output_file_names (void)
7557 struct file_info *files;
7558 struct dir_info *dirs;
7559 int *saved;
7560 int *savehere;
7561 int *backmap;
7562 size_t ndirs;
7563 int idx_offset;
7564 size_t i;
7565 int idx;
7567 /* Handle the case where file_table is empty. */
7568 if (VARRAY_ACTIVE_SIZE (file_table) <= 1)
7570 dw2_asm_output_data (1, 0, "End directory table");
7571 dw2_asm_output_data (1, 0, "End file name table");
7572 return;
7575 /* Allocate the various arrays we need. */
7576 files = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (struct file_info));
7577 dirs = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (struct dir_info));
7579 /* Sort the file names. */
7580 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7582 char *f;
7584 /* Skip all leading "./". */
7585 f = VARRAY_CHAR_PTR (file_table, i);
7586 while (f[0] == '.' && f[1] == '/')
7587 f += 2;
7589 /* Create a new array entry. */
7590 files[i].path = f;
7591 files[i].length = strlen (f);
7592 files[i].file_idx = i;
7594 /* Search for the file name part. */
7595 f = strrchr (f, '/');
7596 files[i].fname = f == NULL ? files[i].path : f + 1;
7599 qsort (files + 1, VARRAY_ACTIVE_SIZE (file_table) - 1,
7600 sizeof (files[0]), file_info_cmp);
7602 /* Find all the different directories used. */
7603 dirs[0].path = files[1].path;
7604 dirs[0].length = files[1].fname - files[1].path;
7605 dirs[0].prefix = -1;
7606 dirs[0].count = 1;
7607 dirs[0].dir_idx = 0;
7608 dirs[0].used = 0;
7609 files[1].dir_idx = 0;
7610 ndirs = 1;
7612 for (i = 2; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7613 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
7614 && memcmp (dirs[ndirs - 1].path, files[i].path,
7615 dirs[ndirs - 1].length) == 0)
7617 /* Same directory as last entry. */
7618 files[i].dir_idx = ndirs - 1;
7619 ++dirs[ndirs - 1].count;
7621 else
7623 size_t j;
7625 /* This is a new directory. */
7626 dirs[ndirs].path = files[i].path;
7627 dirs[ndirs].length = files[i].fname - files[i].path;
7628 dirs[ndirs].count = 1;
7629 dirs[ndirs].dir_idx = ndirs;
7630 dirs[ndirs].used = 0;
7631 files[i].dir_idx = ndirs;
7633 /* Search for a prefix. */
7634 dirs[ndirs].prefix = -1;
7635 for (j = 0; j < ndirs; j++)
7636 if (dirs[j].length < dirs[ndirs].length
7637 && dirs[j].length > 1
7638 && (dirs[ndirs].prefix == -1
7639 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
7640 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
7641 dirs[ndirs].prefix = j;
7643 ++ndirs;
7646 /* Now to the actual work. We have to find a subset of the directories which
7647 allow expressing the file name using references to the directory table
7648 with the least amount of characters. We do not do an exhaustive search
7649 where we would have to check out every combination of every single
7650 possible prefix. Instead we use a heuristic which provides nearly optimal
7651 results in most cases and never is much off. */
7652 saved = alloca (ndirs * sizeof (int));
7653 savehere = alloca (ndirs * sizeof (int));
7655 memset (saved, '\0', ndirs * sizeof (saved[0]));
7656 for (i = 0; i < ndirs; i++)
7658 size_t j;
7659 int total;
7661 /* We can always save some space for the current directory. But this
7662 does not mean it will be enough to justify adding the directory. */
7663 savehere[i] = dirs[i].length;
7664 total = (savehere[i] - saved[i]) * dirs[i].count;
7666 for (j = i + 1; j < ndirs; j++)
7668 savehere[j] = 0;
7669 if (saved[j] < dirs[i].length)
7671 /* Determine whether the dirs[i] path is a prefix of the
7672 dirs[j] path. */
7673 int k;
7675 k = dirs[j].prefix;
7676 while (k != -1 && k != (int) i)
7677 k = dirs[k].prefix;
7679 if (k == (int) i)
7681 /* Yes it is. We can possibly safe some memory but
7682 writing the filenames in dirs[j] relative to
7683 dirs[i]. */
7684 savehere[j] = dirs[i].length;
7685 total += (savehere[j] - saved[j]) * dirs[j].count;
7690 /* Check whether we can safe enough to justify adding the dirs[i]
7691 directory. */
7692 if (total > dirs[i].length + 1)
7694 /* It's worthwhile adding. */
7695 for (j = i; j < ndirs; j++)
7696 if (savehere[j] > 0)
7698 /* Remember how much we saved for this directory so far. */
7699 saved[j] = savehere[j];
7701 /* Remember the prefix directory. */
7702 dirs[j].dir_idx = i;
7707 /* We have to emit them in the order they appear in the file_table array
7708 since the index is used in the debug info generation. To do this
7709 efficiently we generate a back-mapping of the indices first. */
7710 backmap = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (int));
7711 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7713 backmap[files[i].file_idx] = i;
7715 /* Mark this directory as used. */
7716 dirs[dirs[files[i].dir_idx].dir_idx].used = 1;
7719 /* That was it. We are ready to emit the information. First emit the
7720 directory name table. We have to make sure the first actually emitted
7721 directory name has index one; zero is reserved for the current working
7722 directory. Make sure we do not confuse these indices with the one for the
7723 constructed table (even though most of the time they are identical). */
7724 idx = 1;
7725 idx_offset = dirs[0].length > 0 ? 1 : 0;
7726 for (i = 1 - idx_offset; i < ndirs; i++)
7727 if (dirs[i].used != 0)
7729 dirs[i].used = idx++;
7730 dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
7731 "Directory Entry: 0x%x", dirs[i].used);
7734 dw2_asm_output_data (1, 0, "End directory table");
7736 /* Correct the index for the current working directory entry if it
7737 exists. */
7738 if (idx_offset == 0)
7739 dirs[0].used = 0;
7741 /* Now write all the file names. */
7742 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7744 int file_idx = backmap[i];
7745 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
7747 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
7748 "File Entry: 0x%lx", (unsigned long) i);
7750 /* Include directory index. */
7751 dw2_asm_output_data_uleb128 (dirs[dir_idx].used, NULL);
7753 /* Modification time. */
7754 dw2_asm_output_data_uleb128 (0, NULL);
7756 /* File length in bytes. */
7757 dw2_asm_output_data_uleb128 (0, NULL);
7760 dw2_asm_output_data (1, 0, "End file name table");
7764 /* Output the source line number correspondence information. This
7765 information goes into the .debug_line section. */
7767 static void
7768 output_line_info (void)
7770 char l1[20], l2[20], p1[20], p2[20];
7771 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
7772 char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
7773 unsigned opc;
7774 unsigned n_op_args;
7775 unsigned long lt_index;
7776 unsigned long current_line;
7777 long line_offset;
7778 long line_delta;
7779 unsigned long current_file;
7780 unsigned long function;
7782 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
7783 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
7784 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
7785 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
7787 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7788 dw2_asm_output_data (4, 0xffffffff,
7789 "Initial length escape value indicating 64-bit DWARF extension");
7790 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
7791 "Length of Source Line Info");
7792 ASM_OUTPUT_LABEL (asm_out_file, l1);
7794 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7795 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
7796 ASM_OUTPUT_LABEL (asm_out_file, p1);
7798 /* Define the architecture-dependent minimum instruction length (in
7799 bytes). In this implementation of DWARF, this field is used for
7800 information purposes only. Since GCC generates assembly language,
7801 we have no a priori knowledge of how many instruction bytes are
7802 generated for each source line, and therefore can use only the
7803 DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
7804 commands. Accordingly, we fix this as `1', which is "correct
7805 enough" for all architectures, and don't let the target override. */
7806 dw2_asm_output_data (1, 1,
7807 "Minimum Instruction Length");
7809 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
7810 "Default is_stmt_start flag");
7811 dw2_asm_output_data (1, DWARF_LINE_BASE,
7812 "Line Base Value (Special Opcodes)");
7813 dw2_asm_output_data (1, DWARF_LINE_RANGE,
7814 "Line Range Value (Special Opcodes)");
7815 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
7816 "Special Opcode Base");
7818 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
7820 switch (opc)
7822 case DW_LNS_advance_pc:
7823 case DW_LNS_advance_line:
7824 case DW_LNS_set_file:
7825 case DW_LNS_set_column:
7826 case DW_LNS_fixed_advance_pc:
7827 n_op_args = 1;
7828 break;
7829 default:
7830 n_op_args = 0;
7831 break;
7834 dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
7835 opc, n_op_args);
7838 /* Write out the information about the files we use. */
7839 output_file_names ();
7840 ASM_OUTPUT_LABEL (asm_out_file, p2);
7842 /* We used to set the address register to the first location in the text
7843 section here, but that didn't accomplish anything since we already
7844 have a line note for the opening brace of the first function. */
7846 /* Generate the line number to PC correspondence table, encoded as
7847 a series of state machine operations. */
7848 current_file = 1;
7849 current_line = 1;
7851 if (cfun
7852 && (last_text_section == in_unlikely_executed_text
7853 || (last_text_section == in_named
7854 && last_text_section_name == cfun->unlikely_text_section_name)))
7855 strcpy (prev_line_label, cfun->cold_section_label);
7856 else
7857 strcpy (prev_line_label, text_section_label);
7858 for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
7860 dw_line_info_ref line_info = &line_info_table[lt_index];
7862 #if 0
7863 /* Disable this optimization for now; GDB wants to see two line notes
7864 at the beginning of a function so it can find the end of the
7865 prologue. */
7867 /* Don't emit anything for redundant notes. Just updating the
7868 address doesn't accomplish anything, because we already assume
7869 that anything after the last address is this line. */
7870 if (line_info->dw_line_num == current_line
7871 && line_info->dw_file_num == current_file)
7872 continue;
7873 #endif
7875 /* Emit debug info for the address of the current line.
7877 Unfortunately, we have little choice here currently, and must always
7878 use the most general form. GCC does not know the address delta
7879 itself, so we can't use DW_LNS_advance_pc. Many ports do have length
7880 attributes which will give an upper bound on the address range. We
7881 could perhaps use length attributes to determine when it is safe to
7882 use DW_LNS_fixed_advance_pc. */
7884 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
7885 if (0)
7887 /* This can handle deltas up to 0xffff. This takes 3 bytes. */
7888 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7889 "DW_LNS_fixed_advance_pc");
7890 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
7892 else
7894 /* This can handle any delta. This takes
7895 4+DWARF2_ADDR_SIZE bytes. */
7896 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7897 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7898 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7899 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7902 strcpy (prev_line_label, line_label);
7904 /* Emit debug info for the source file of the current line, if
7905 different from the previous line. */
7906 if (line_info->dw_file_num != current_file)
7908 current_file = line_info->dw_file_num;
7909 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
7910 dw2_asm_output_data_uleb128 (current_file, "(\"%s\")",
7911 VARRAY_CHAR_PTR (file_table,
7912 current_file));
7915 /* Emit debug info for the current line number, choosing the encoding
7916 that uses the least amount of space. */
7917 if (line_info->dw_line_num != current_line)
7919 line_offset = line_info->dw_line_num - current_line;
7920 line_delta = line_offset - DWARF_LINE_BASE;
7921 current_line = line_info->dw_line_num;
7922 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
7923 /* This can handle deltas from -10 to 234, using the current
7924 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE. This
7925 takes 1 byte. */
7926 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
7927 "line %lu", current_line);
7928 else
7930 /* This can handle any delta. This takes at least 4 bytes,
7931 depending on the value being encoded. */
7932 dw2_asm_output_data (1, DW_LNS_advance_line,
7933 "advance to line %lu", current_line);
7934 dw2_asm_output_data_sleb128 (line_offset, NULL);
7935 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7938 else
7939 /* We still need to start a new row, so output a copy insn. */
7940 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7943 /* Emit debug info for the address of the end of the function. */
7944 if (0)
7946 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7947 "DW_LNS_fixed_advance_pc");
7948 dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
7950 else
7952 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7953 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7954 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7955 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
7958 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
7959 dw2_asm_output_data_uleb128 (1, NULL);
7960 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
7962 function = 0;
7963 current_file = 1;
7964 current_line = 1;
7965 for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
7967 dw_separate_line_info_ref line_info
7968 = &separate_line_info_table[lt_index];
7970 #if 0
7971 /* Don't emit anything for redundant notes. */
7972 if (line_info->dw_line_num == current_line
7973 && line_info->dw_file_num == current_file
7974 && line_info->function == function)
7975 goto cont;
7976 #endif
7978 /* Emit debug info for the address of the current line. If this is
7979 a new function, or the first line of a function, then we need
7980 to handle it differently. */
7981 ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
7982 lt_index);
7983 if (function != line_info->function)
7985 function = line_info->function;
7987 /* Set the address register to the first line in the function. */
7988 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7989 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7990 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7991 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7993 else
7995 /* ??? See the DW_LNS_advance_pc comment above. */
7996 if (0)
7998 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7999 "DW_LNS_fixed_advance_pc");
8000 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8002 else
8004 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8005 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8006 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8007 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8011 strcpy (prev_line_label, line_label);
8013 /* Emit debug info for the source file of the current line, if
8014 different from the previous line. */
8015 if (line_info->dw_file_num != current_file)
8017 current_file = line_info->dw_file_num;
8018 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8019 dw2_asm_output_data_uleb128 (current_file, "(\"%s\")",
8020 VARRAY_CHAR_PTR (file_table,
8021 current_file));
8024 /* Emit debug info for the current line number, choosing the encoding
8025 that uses the least amount of space. */
8026 if (line_info->dw_line_num != current_line)
8028 line_offset = line_info->dw_line_num - current_line;
8029 line_delta = line_offset - DWARF_LINE_BASE;
8030 current_line = line_info->dw_line_num;
8031 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
8032 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
8033 "line %lu", current_line);
8034 else
8036 dw2_asm_output_data (1, DW_LNS_advance_line,
8037 "advance to line %lu", current_line);
8038 dw2_asm_output_data_sleb128 (line_offset, NULL);
8039 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8042 else
8043 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8045 #if 0
8046 cont:
8047 #endif
8049 lt_index++;
8051 /* If we're done with a function, end its sequence. */
8052 if (lt_index == separate_line_info_table_in_use
8053 || separate_line_info_table[lt_index].function != function)
8055 current_file = 1;
8056 current_line = 1;
8058 /* Emit debug info for the address of the end of the function. */
8059 ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
8060 if (0)
8062 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8063 "DW_LNS_fixed_advance_pc");
8064 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8066 else
8068 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8069 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8070 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8071 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8074 /* Output the marker for the end of this sequence. */
8075 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
8076 dw2_asm_output_data_uleb128 (1, NULL);
8077 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
8081 /* Output the marker for the end of the line number info. */
8082 ASM_OUTPUT_LABEL (asm_out_file, l2);
8085 /* Given a pointer to a tree node for some base type, return a pointer to
8086 a DIE that describes the given type.
8088 This routine must only be called for GCC type nodes that correspond to
8089 Dwarf base (fundamental) types. */
8091 static dw_die_ref
8092 base_type_die (tree type)
8094 dw_die_ref base_type_result;
8095 const char *type_name;
8096 enum dwarf_type encoding;
8097 tree name = TYPE_NAME (type);
8099 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
8100 return 0;
8102 if (name)
8104 if (TREE_CODE (name) == TYPE_DECL)
8105 name = DECL_NAME (name);
8107 type_name = IDENTIFIER_POINTER (name);
8109 else
8110 type_name = "__unknown__";
8112 switch (TREE_CODE (type))
8114 case INTEGER_TYPE:
8115 /* Carefully distinguish the C character types, without messing
8116 up if the language is not C. Note that we check only for the names
8117 that contain spaces; other names might occur by coincidence in other
8118 languages. */
8119 if (! (TYPE_PRECISION (type) == CHAR_TYPE_SIZE
8120 && (TYPE_MAIN_VARIANT (type) == char_type_node
8121 || ! strcmp (type_name, "signed char")
8122 || ! strcmp (type_name, "unsigned char"))))
8124 if (TYPE_UNSIGNED (type))
8125 encoding = DW_ATE_unsigned;
8126 else
8127 encoding = DW_ATE_signed;
8128 break;
8130 /* else fall through. */
8132 case CHAR_TYPE:
8133 /* GNU Pascal/Ada CHAR type. Not used in C. */
8134 if (TYPE_UNSIGNED (type))
8135 encoding = DW_ATE_unsigned_char;
8136 else
8137 encoding = DW_ATE_signed_char;
8138 break;
8140 case REAL_TYPE:
8141 encoding = DW_ATE_float;
8142 break;
8144 /* Dwarf2 doesn't know anything about complex ints, so use
8145 a user defined type for it. */
8146 case COMPLEX_TYPE:
8147 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
8148 encoding = DW_ATE_complex_float;
8149 else
8150 encoding = DW_ATE_lo_user;
8151 break;
8153 case BOOLEAN_TYPE:
8154 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
8155 encoding = DW_ATE_boolean;
8156 break;
8158 default:
8159 /* No other TREE_CODEs are Dwarf fundamental types. */
8160 gcc_unreachable ();
8163 base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
8164 if (demangle_name_func)
8165 type_name = (*demangle_name_func) (type_name);
8167 add_AT_string (base_type_result, DW_AT_name, type_name);
8168 add_AT_unsigned (base_type_result, DW_AT_byte_size,
8169 int_size_in_bytes (type));
8170 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
8172 return base_type_result;
8175 /* Given a pointer to an arbitrary ..._TYPE tree node, return a pointer to
8176 the Dwarf "root" type for the given input type. The Dwarf "root" type of
8177 a given type is generally the same as the given type, except that if the
8178 given type is a pointer or reference type, then the root type of the given
8179 type is the root type of the "basis" type for the pointer or reference
8180 type. (This definition of the "root" type is recursive.) Also, the root
8181 type of a `const' qualified type or a `volatile' qualified type is the
8182 root type of the given type without the qualifiers. */
8184 static tree
8185 root_type (tree type)
8187 if (TREE_CODE (type) == ERROR_MARK)
8188 return error_mark_node;
8190 switch (TREE_CODE (type))
8192 case ERROR_MARK:
8193 return error_mark_node;
8195 case POINTER_TYPE:
8196 case REFERENCE_TYPE:
8197 return type_main_variant (root_type (TREE_TYPE (type)));
8199 default:
8200 return type_main_variant (type);
8204 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
8205 given input type is a Dwarf "fundamental" type. Otherwise return null. */
8207 static inline int
8208 is_base_type (tree type)
8210 switch (TREE_CODE (type))
8212 case ERROR_MARK:
8213 case VOID_TYPE:
8214 case INTEGER_TYPE:
8215 case REAL_TYPE:
8216 case COMPLEX_TYPE:
8217 case BOOLEAN_TYPE:
8218 case CHAR_TYPE:
8219 return 1;
8221 case ARRAY_TYPE:
8222 case RECORD_TYPE:
8223 case UNION_TYPE:
8224 case QUAL_UNION_TYPE:
8225 case ENUMERAL_TYPE:
8226 case FUNCTION_TYPE:
8227 case METHOD_TYPE:
8228 case POINTER_TYPE:
8229 case REFERENCE_TYPE:
8230 case OFFSET_TYPE:
8231 case LANG_TYPE:
8232 case VECTOR_TYPE:
8233 return 0;
8235 default:
8236 gcc_unreachable ();
8239 return 0;
8242 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
8243 node, return the size in bits for the type if it is a constant, or else
8244 return the alignment for the type if the type's size is not constant, or
8245 else return BITS_PER_WORD if the type actually turns out to be an
8246 ERROR_MARK node. */
8248 static inline unsigned HOST_WIDE_INT
8249 simple_type_size_in_bits (tree type)
8251 if (TREE_CODE (type) == ERROR_MARK)
8252 return BITS_PER_WORD;
8253 else if (TYPE_SIZE (type) == NULL_TREE)
8254 return 0;
8255 else if (host_integerp (TYPE_SIZE (type), 1))
8256 return tree_low_cst (TYPE_SIZE (type), 1);
8257 else
8258 return TYPE_ALIGN (type);
8261 /* Return true if the debug information for the given type should be
8262 emitted as a subrange type. */
8264 static inline bool
8265 is_subrange_type (tree type)
8267 tree subtype = TREE_TYPE (type);
8269 /* Subrange types are identified by the fact that they are integer
8270 types, and that they have a subtype which is either an integer type
8271 or an enumeral type. */
8273 if (TREE_CODE (type) != INTEGER_TYPE
8274 || subtype == NULL_TREE)
8275 return false;
8277 if (TREE_CODE (subtype) != INTEGER_TYPE
8278 && TREE_CODE (subtype) != ENUMERAL_TYPE)
8279 return false;
8281 if (TREE_CODE (type) == TREE_CODE (subtype)
8282 && int_size_in_bytes (type) == int_size_in_bytes (subtype)
8283 && TYPE_MIN_VALUE (type) != NULL
8284 && TYPE_MIN_VALUE (subtype) != NULL
8285 && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
8286 && TYPE_MAX_VALUE (type) != NULL
8287 && TYPE_MAX_VALUE (subtype) != NULL
8288 && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
8290 /* The type and its subtype have the same representation. If in
8291 addition the two types also have the same name, then the given
8292 type is not a subrange type, but rather a plain base type. */
8293 /* FIXME: brobecker/2004-03-22:
8294 Sizetype INTEGER_CSTs nodes are canonicalized. It should
8295 therefore be sufficient to check the TYPE_SIZE node pointers
8296 rather than checking the actual size. Unfortunately, we have
8297 found some cases, such as in the Ada "integer" type, where
8298 this is not the case. Until this problem is solved, we need to
8299 keep checking the actual size. */
8300 tree type_name = TYPE_NAME (type);
8301 tree subtype_name = TYPE_NAME (subtype);
8303 if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
8304 type_name = DECL_NAME (type_name);
8306 if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
8307 subtype_name = DECL_NAME (subtype_name);
8309 if (type_name == subtype_name)
8310 return false;
8313 return true;
8316 /* Given a pointer to a tree node for a subrange type, return a pointer
8317 to a DIE that describes the given type. */
8319 static dw_die_ref
8320 subrange_type_die (tree type, dw_die_ref context_die)
8322 dw_die_ref subtype_die;
8323 dw_die_ref subrange_die;
8324 tree name = TYPE_NAME (type);
8325 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
8326 tree subtype = TREE_TYPE (type);
8328 if (context_die == NULL)
8329 context_die = comp_unit_die;
8331 if (TREE_CODE (subtype) == ENUMERAL_TYPE)
8332 subtype_die = gen_enumeration_type_die (subtype, context_die);
8333 else
8334 subtype_die = base_type_die (subtype);
8336 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
8338 if (name != NULL)
8340 if (TREE_CODE (name) == TYPE_DECL)
8341 name = DECL_NAME (name);
8342 add_name_attribute (subrange_die, IDENTIFIER_POINTER (name));
8345 if (int_size_in_bytes (subtype) != size_in_bytes)
8347 /* The size of the subrange type and its base type do not match,
8348 so we need to generate a size attribute for the subrange type. */
8349 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
8352 if (TYPE_MIN_VALUE (type) != NULL)
8353 add_bound_info (subrange_die, DW_AT_lower_bound,
8354 TYPE_MIN_VALUE (type));
8355 if (TYPE_MAX_VALUE (type) != NULL)
8356 add_bound_info (subrange_die, DW_AT_upper_bound,
8357 TYPE_MAX_VALUE (type));
8358 add_AT_die_ref (subrange_die, DW_AT_type, subtype_die);
8360 return subrange_die;
8363 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
8364 entry that chains various modifiers in front of the given type. */
8366 static dw_die_ref
8367 modified_type_die (tree type, int is_const_type, int is_volatile_type,
8368 dw_die_ref context_die)
8370 enum tree_code code = TREE_CODE (type);
8371 dw_die_ref mod_type_die = NULL;
8372 dw_die_ref sub_die = NULL;
8373 tree item_type = NULL;
8375 if (code != ERROR_MARK)
8377 tree qualified_type;
8379 /* See if we already have the appropriately qualified variant of
8380 this type. */
8381 qualified_type
8382 = get_qualified_type (type,
8383 ((is_const_type ? TYPE_QUAL_CONST : 0)
8384 | (is_volatile_type
8385 ? TYPE_QUAL_VOLATILE : 0)));
8387 /* If we do, then we can just use its DIE, if it exists. */
8388 if (qualified_type)
8390 mod_type_die = lookup_type_die (qualified_type);
8391 if (mod_type_die)
8392 return mod_type_die;
8395 /* Handle C typedef types. */
8396 if (qualified_type && TYPE_NAME (qualified_type)
8397 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL
8398 && DECL_ORIGINAL_TYPE (TYPE_NAME (qualified_type)))
8400 tree type_name = TYPE_NAME (qualified_type);
8401 tree dtype = TREE_TYPE (type_name);
8403 if (qualified_type == dtype)
8405 /* For a named type, use the typedef. */
8406 gen_type_die (qualified_type, context_die);
8407 mod_type_die = lookup_type_die (qualified_type);
8409 else if (is_const_type < TYPE_READONLY (dtype)
8410 || is_volatile_type < TYPE_VOLATILE (dtype)
8411 || (is_const_type <= TYPE_READONLY (dtype)
8412 && is_volatile_type <= TYPE_VOLATILE (dtype)
8413 && DECL_ORIGINAL_TYPE (type_name) != type))
8414 /* cv-unqualified version of named type. Just use the unnamed
8415 type to which it refers. */
8416 mod_type_die
8417 = modified_type_die (DECL_ORIGINAL_TYPE (type_name),
8418 is_const_type, is_volatile_type,
8419 context_die);
8421 /* Else cv-qualified version of named type; fall through. */
8424 if (mod_type_die)
8425 /* OK. */
8427 else if (is_const_type)
8429 mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
8430 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
8432 else if (is_volatile_type)
8434 mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
8435 sub_die = modified_type_die (type, 0, 0, context_die);
8437 else if (code == POINTER_TYPE)
8439 mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
8440 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8441 simple_type_size_in_bits (type) / BITS_PER_UNIT);
8442 #if 0
8443 add_AT_unsigned (mod_type_die, DW_AT_address_class, 0);
8444 #endif
8445 item_type = TREE_TYPE (type);
8447 else if (code == REFERENCE_TYPE)
8449 mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
8450 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8451 simple_type_size_in_bits (type) / BITS_PER_UNIT);
8452 #if 0
8453 add_AT_unsigned (mod_type_die, DW_AT_address_class, 0);
8454 #endif
8455 item_type = TREE_TYPE (type);
8457 else if (is_subrange_type (type))
8458 mod_type_die = subrange_type_die (type, context_die);
8459 else if (is_base_type (type))
8460 mod_type_die = base_type_die (type);
8461 else
8463 gen_type_die (type, context_die);
8465 /* We have to get the type_main_variant here (and pass that to the
8466 `lookup_type_die' routine) because the ..._TYPE node we have
8467 might simply be a *copy* of some original type node (where the
8468 copy was created to help us keep track of typedef names) and
8469 that copy might have a different TYPE_UID from the original
8470 ..._TYPE node. */
8471 if (TREE_CODE (type) != VECTOR_TYPE)
8472 mod_type_die = lookup_type_die (type_main_variant (type));
8473 else
8474 /* Vectors have the debugging information in the type,
8475 not the main variant. */
8476 mod_type_die = lookup_type_die (type);
8477 gcc_assert (mod_type_die);
8480 /* We want to equate the qualified type to the die below. */
8481 type = qualified_type;
8484 if (type)
8485 equate_type_number_to_die (type, mod_type_die);
8486 if (item_type)
8487 /* We must do this after the equate_type_number_to_die call, in case
8488 this is a recursive type. This ensures that the modified_type_die
8489 recursion will terminate even if the type is recursive. Recursive
8490 types are possible in Ada. */
8491 sub_die = modified_type_die (item_type,
8492 TYPE_READONLY (item_type),
8493 TYPE_VOLATILE (item_type),
8494 context_die);
8496 if (sub_die != NULL)
8497 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
8499 return mod_type_die;
8502 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
8503 an enumerated type. */
8505 static inline int
8506 type_is_enum (tree type)
8508 return TREE_CODE (type) == ENUMERAL_TYPE;
8511 /* Return the DBX register number described by a given RTL node. */
8513 static unsigned int
8514 dbx_reg_number (rtx rtl)
8516 unsigned regno = REGNO (rtl);
8518 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
8520 #ifdef LEAF_REG_REMAP
8521 if (current_function_uses_only_leaf_regs)
8523 int leaf_reg = LEAF_REG_REMAP (regno);
8524 if (leaf_reg != -1)
8525 regno = (unsigned) leaf_reg;
8527 #endif
8529 return DBX_REGISTER_NUMBER (regno);
8532 /* Optionally add a DW_OP_piece term to a location description expression.
8533 DW_OP_piece is only added if the location description expression already
8534 doesn't end with DW_OP_piece. */
8536 static void
8537 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
8539 dw_loc_descr_ref loc;
8541 if (*list_head != NULL)
8543 /* Find the end of the chain. */
8544 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
8547 if (loc->dw_loc_opc != DW_OP_piece)
8548 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
8552 /* Return a location descriptor that designates a machine register or
8553 zero if there is none. */
8555 static dw_loc_descr_ref
8556 reg_loc_descriptor (rtx rtl)
8558 rtx regs;
8560 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
8561 return 0;
8563 regs = targetm.dwarf_register_span (rtl);
8565 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
8566 return multiple_reg_loc_descriptor (rtl, regs);
8567 else
8568 return one_reg_loc_descriptor (dbx_reg_number (rtl));
8571 /* Return a location descriptor that designates a machine register for
8572 a given hard register number. */
8574 static dw_loc_descr_ref
8575 one_reg_loc_descriptor (unsigned int regno)
8577 if (regno <= 31)
8578 return new_loc_descr (DW_OP_reg0 + regno, 0, 0);
8579 else
8580 return new_loc_descr (DW_OP_regx, regno, 0);
8583 /* Given an RTL of a register, return a location descriptor that
8584 designates a value that spans more than one register. */
8586 static dw_loc_descr_ref
8587 multiple_reg_loc_descriptor (rtx rtl, rtx regs)
8589 int nregs, size, i;
8590 unsigned reg;
8591 dw_loc_descr_ref loc_result = NULL;
8593 reg = REGNO (rtl);
8594 #ifdef LEAF_REG_REMAP
8595 if (current_function_uses_only_leaf_regs)
8597 int leaf_reg = LEAF_REG_REMAP (reg);
8598 if (leaf_reg != -1)
8599 reg = (unsigned) leaf_reg;
8601 #endif
8602 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
8603 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
8605 /* Simple, contiguous registers. */
8606 if (regs == NULL_RTX)
8608 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
8610 loc_result = NULL;
8611 while (nregs--)
8613 dw_loc_descr_ref t;
8615 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg));
8616 add_loc_descr (&loc_result, t);
8617 add_loc_descr_op_piece (&loc_result, size);
8618 ++reg;
8620 return loc_result;
8623 /* Now onto stupid register sets in non contiguous locations. */
8625 gcc_assert (GET_CODE (regs) == PARALLEL);
8627 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8628 loc_result = NULL;
8630 for (i = 0; i < XVECLEN (regs, 0); ++i)
8632 dw_loc_descr_ref t;
8634 t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)));
8635 add_loc_descr (&loc_result, t);
8636 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8637 add_loc_descr_op_piece (&loc_result, size);
8639 return loc_result;
8642 /* Return a location descriptor that designates a constant. */
8644 static dw_loc_descr_ref
8645 int_loc_descriptor (HOST_WIDE_INT i)
8647 enum dwarf_location_atom op;
8649 /* Pick the smallest representation of a constant, rather than just
8650 defaulting to the LEB encoding. */
8651 if (i >= 0)
8653 if (i <= 31)
8654 op = DW_OP_lit0 + i;
8655 else if (i <= 0xff)
8656 op = DW_OP_const1u;
8657 else if (i <= 0xffff)
8658 op = DW_OP_const2u;
8659 else if (HOST_BITS_PER_WIDE_INT == 32
8660 || i <= 0xffffffff)
8661 op = DW_OP_const4u;
8662 else
8663 op = DW_OP_constu;
8665 else
8667 if (i >= -0x80)
8668 op = DW_OP_const1s;
8669 else if (i >= -0x8000)
8670 op = DW_OP_const2s;
8671 else if (HOST_BITS_PER_WIDE_INT == 32
8672 || i >= -0x80000000)
8673 op = DW_OP_const4s;
8674 else
8675 op = DW_OP_consts;
8678 return new_loc_descr (op, i, 0);
8681 /* Return a location descriptor that designates a base+offset location. */
8683 static dw_loc_descr_ref
8684 based_loc_descr (rtx reg, HOST_WIDE_INT offset)
8686 unsigned int regno;
8688 /* We only use "frame base" when we're sure we're talking about the
8689 post-prologue local stack frame. We do this by *not* running
8690 register elimination until this point, and recognizing the special
8691 argument pointer and soft frame pointer rtx's. */
8692 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
8694 rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
8696 if (elim != reg)
8698 if (GET_CODE (elim) == PLUS)
8700 offset += INTVAL (XEXP (elim, 1));
8701 elim = XEXP (elim, 0);
8703 gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
8704 : stack_pointer_rtx));
8705 offset += frame_pointer_cfa_offset;
8707 return new_loc_descr (DW_OP_fbreg, offset, 0);
8711 regno = dbx_reg_number (reg);
8712 if (regno <= 31)
8713 return new_loc_descr (DW_OP_breg0 + regno, offset, 0);
8714 else
8715 return new_loc_descr (DW_OP_bregx, regno, offset);
8718 /* Return true if this RTL expression describes a base+offset calculation. */
8720 static inline int
8721 is_based_loc (rtx rtl)
8723 return (GET_CODE (rtl) == PLUS
8724 && ((REG_P (XEXP (rtl, 0))
8725 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
8726 && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
8729 /* The following routine converts the RTL for a variable or parameter
8730 (resident in memory) into an equivalent Dwarf representation of a
8731 mechanism for getting the address of that same variable onto the top of a
8732 hypothetical "address evaluation" stack.
8734 When creating memory location descriptors, we are effectively transforming
8735 the RTL for a memory-resident object into its Dwarf postfix expression
8736 equivalent. This routine recursively descends an RTL tree, turning
8737 it into Dwarf postfix code as it goes.
8739 MODE is the mode of the memory reference, needed to handle some
8740 autoincrement addressing modes.
8742 CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
8743 location list for RTL.
8745 Return 0 if we can't represent the location. */
8747 static dw_loc_descr_ref
8748 mem_loc_descriptor (rtx rtl, enum machine_mode mode)
8750 dw_loc_descr_ref mem_loc_result = NULL;
8751 enum dwarf_location_atom op;
8753 /* Note that for a dynamically sized array, the location we will generate a
8754 description of here will be the lowest numbered location which is
8755 actually within the array. That's *not* necessarily the same as the
8756 zeroth element of the array. */
8758 rtl = targetm.delegitimize_address (rtl);
8760 switch (GET_CODE (rtl))
8762 case POST_INC:
8763 case POST_DEC:
8764 case POST_MODIFY:
8765 /* POST_INC and POST_DEC can be handled just like a SUBREG. So we
8766 just fall into the SUBREG code. */
8768 /* ... fall through ... */
8770 case SUBREG:
8771 /* The case of a subreg may arise when we have a local (register)
8772 variable or a formal (register) parameter which doesn't quite fill
8773 up an entire register. For now, just assume that it is
8774 legitimate to make the Dwarf info refer to the whole register which
8775 contains the given subreg. */
8776 rtl = XEXP (rtl, 0);
8778 /* ... fall through ... */
8780 case REG:
8781 /* Whenever a register number forms a part of the description of the
8782 method for calculating the (dynamic) address of a memory resident
8783 object, DWARF rules require the register number be referred to as
8784 a "base register". This distinction is not based in any way upon
8785 what category of register the hardware believes the given register
8786 belongs to. This is strictly DWARF terminology we're dealing with
8787 here. Note that in cases where the location of a memory-resident
8788 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
8789 OP_CONST (0)) the actual DWARF location descriptor that we generate
8790 may just be OP_BASEREG (basereg). This may look deceptively like
8791 the object in question was allocated to a register (rather than in
8792 memory) so DWARF consumers need to be aware of the subtle
8793 distinction between OP_REG and OP_BASEREG. */
8794 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
8795 mem_loc_result = based_loc_descr (rtl, 0);
8796 break;
8798 case MEM:
8799 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl));
8800 if (mem_loc_result != 0)
8801 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
8802 break;
8804 case LO_SUM:
8805 rtl = XEXP (rtl, 1);
8807 /* ... fall through ... */
8809 case LABEL_REF:
8810 /* Some ports can transform a symbol ref into a label ref, because
8811 the symbol ref is too far away and has to be dumped into a constant
8812 pool. */
8813 case CONST:
8814 case SYMBOL_REF:
8815 /* Alternatively, the symbol in the constant pool might be referenced
8816 by a different symbol. */
8817 if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
8819 bool marked;
8820 rtx tmp = get_pool_constant_mark (rtl, &marked);
8822 if (GET_CODE (tmp) == SYMBOL_REF)
8824 rtl = tmp;
8825 if (CONSTANT_POOL_ADDRESS_P (tmp))
8826 get_pool_constant_mark (tmp, &marked);
8827 else
8828 marked = true;
8831 /* If all references to this pool constant were optimized away,
8832 it was not output and thus we can't represent it.
8833 FIXME: might try to use DW_OP_const_value here, though
8834 DW_OP_piece complicates it. */
8835 if (!marked)
8836 return 0;
8839 mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
8840 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
8841 mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
8842 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
8843 break;
8845 case PRE_MODIFY:
8846 /* Extract the PLUS expression nested inside and fall into
8847 PLUS code below. */
8848 rtl = XEXP (rtl, 1);
8849 goto plus;
8851 case PRE_INC:
8852 case PRE_DEC:
8853 /* Turn these into a PLUS expression and fall into the PLUS code
8854 below. */
8855 rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
8856 GEN_INT (GET_CODE (rtl) == PRE_INC
8857 ? GET_MODE_UNIT_SIZE (mode)
8858 : -GET_MODE_UNIT_SIZE (mode)));
8860 /* ... fall through ... */
8862 case PLUS:
8863 plus:
8864 if (is_based_loc (rtl))
8865 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
8866 INTVAL (XEXP (rtl, 1)));
8867 else
8869 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode);
8870 if (mem_loc_result == 0)
8871 break;
8873 if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
8874 && INTVAL (XEXP (rtl, 1)) >= 0)
8875 add_loc_descr (&mem_loc_result,
8876 new_loc_descr (DW_OP_plus_uconst,
8877 INTVAL (XEXP (rtl, 1)), 0));
8878 else
8880 add_loc_descr (&mem_loc_result,
8881 mem_loc_descriptor (XEXP (rtl, 1), mode));
8882 add_loc_descr (&mem_loc_result,
8883 new_loc_descr (DW_OP_plus, 0, 0));
8886 break;
8888 /* If a pseudo-reg is optimized away, it is possible for it to
8889 be replaced with a MEM containing a multiply or shift. */
8890 case MULT:
8891 op = DW_OP_mul;
8892 goto do_binop;
8894 case ASHIFT:
8895 op = DW_OP_shl;
8896 goto do_binop;
8898 case ASHIFTRT:
8899 op = DW_OP_shra;
8900 goto do_binop;
8902 case LSHIFTRT:
8903 op = DW_OP_shr;
8904 goto do_binop;
8906 do_binop:
8908 dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode);
8909 dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode);
8911 if (op0 == 0 || op1 == 0)
8912 break;
8914 mem_loc_result = op0;
8915 add_loc_descr (&mem_loc_result, op1);
8916 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
8917 break;
8920 case CONST_INT:
8921 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
8922 break;
8924 default:
8925 gcc_unreachable ();
8928 return mem_loc_result;
8931 /* Return a descriptor that describes the concatenation of two locations.
8932 This is typically a complex variable. */
8934 static dw_loc_descr_ref
8935 concat_loc_descriptor (rtx x0, rtx x1)
8937 dw_loc_descr_ref cc_loc_result = NULL;
8938 dw_loc_descr_ref x0_ref = loc_descriptor (x0);
8939 dw_loc_descr_ref x1_ref = loc_descriptor (x1);
8941 if (x0_ref == 0 || x1_ref == 0)
8942 return 0;
8944 cc_loc_result = x0_ref;
8945 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
8947 add_loc_descr (&cc_loc_result, x1_ref);
8948 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
8950 return cc_loc_result;
8953 /* Output a proper Dwarf location descriptor for a variable or parameter
8954 which is either allocated in a register or in a memory location. For a
8955 register, we just generate an OP_REG and the register number. For a
8956 memory location we provide a Dwarf postfix expression describing how to
8957 generate the (dynamic) address of the object onto the address stack.
8959 If we don't know how to describe it, return 0. */
8961 static dw_loc_descr_ref
8962 loc_descriptor (rtx rtl)
8964 dw_loc_descr_ref loc_result = NULL;
8966 switch (GET_CODE (rtl))
8968 case SUBREG:
8969 /* The case of a subreg may arise when we have a local (register)
8970 variable or a formal (register) parameter which doesn't quite fill
8971 up an entire register. For now, just assume that it is
8972 legitimate to make the Dwarf info refer to the whole register which
8973 contains the given subreg. */
8974 rtl = SUBREG_REG (rtl);
8976 /* ... fall through ... */
8978 case REG:
8979 loc_result = reg_loc_descriptor (rtl);
8980 break;
8982 case MEM:
8983 loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl));
8984 break;
8986 case CONCAT:
8987 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1));
8988 break;
8990 case VAR_LOCATION:
8991 /* Single part. */
8992 if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
8994 loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0));
8995 break;
8998 rtl = XEXP (rtl, 1);
8999 /* FALLTHRU */
9001 case PARALLEL:
9003 rtvec par_elems = XVEC (rtl, 0);
9004 int num_elem = GET_NUM_ELEM (par_elems);
9005 enum machine_mode mode;
9006 int i;
9008 /* Create the first one, so we have something to add to. */
9009 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0));
9010 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
9011 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9012 for (i = 1; i < num_elem; i++)
9014 dw_loc_descr_ref temp;
9016 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0));
9017 add_loc_descr (&loc_result, temp);
9018 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
9019 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9022 break;
9024 default:
9025 gcc_unreachable ();
9028 return loc_result;
9031 /* Similar, but generate the descriptor from trees instead of rtl. This comes
9032 up particularly with variable length arrays. WANT_ADDRESS is 2 if this is
9033 a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
9034 top-level invocation, and we require the address of LOC; is 0 if we require
9035 the value of LOC. */
9037 static dw_loc_descr_ref
9038 loc_descriptor_from_tree_1 (tree loc, int want_address)
9040 dw_loc_descr_ref ret, ret1;
9041 int have_address = 0;
9042 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
9043 enum dwarf_location_atom op;
9045 /* ??? Most of the time we do not take proper care for sign/zero
9046 extending the values properly. Hopefully this won't be a real
9047 problem... */
9049 switch (TREE_CODE (loc))
9051 case ERROR_MARK:
9052 return 0;
9054 case PLACEHOLDER_EXPR:
9055 /* This case involves extracting fields from an object to determine the
9056 position of other fields. We don't try to encode this here. The
9057 only user of this is Ada, which encodes the needed information using
9058 the names of types. */
9059 return 0;
9061 case CALL_EXPR:
9062 return 0;
9064 case PREINCREMENT_EXPR:
9065 case PREDECREMENT_EXPR:
9066 case POSTINCREMENT_EXPR:
9067 case POSTDECREMENT_EXPR:
9068 /* There are no opcodes for these operations. */
9069 return 0;
9071 case ADDR_EXPR:
9072 /* If we already want an address, there's nothing we can do. */
9073 if (want_address)
9074 return 0;
9076 /* Otherwise, process the argument and look for the address. */
9077 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
9079 case VAR_DECL:
9080 if (DECL_THREAD_LOCAL_P (loc))
9082 rtx rtl;
9084 /* If this is not defined, we have no way to emit the data. */
9085 if (!targetm.asm_out.output_dwarf_dtprel)
9086 return 0;
9088 /* The way DW_OP_GNU_push_tls_address is specified, we can only
9089 look up addresses of objects in the current module. */
9090 if (DECL_EXTERNAL (loc))
9091 return 0;
9093 rtl = rtl_for_decl_location (loc);
9094 if (rtl == NULL_RTX)
9095 return 0;
9097 if (!MEM_P (rtl))
9098 return 0;
9099 rtl = XEXP (rtl, 0);
9100 if (! CONSTANT_P (rtl))
9101 return 0;
9103 ret = new_loc_descr (INTERNAL_DW_OP_tls_addr, 0, 0);
9104 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9105 ret->dw_loc_oprnd1.v.val_addr = rtl;
9107 ret1 = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
9108 add_loc_descr (&ret, ret1);
9110 have_address = 1;
9111 break;
9113 /* FALLTHRU */
9115 case PARM_DECL:
9116 if (DECL_HAS_VALUE_EXPR_P (loc))
9117 return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
9118 want_address);
9119 /* FALLTHRU */
9121 case RESULT_DECL:
9122 case FUNCTION_DECL:
9124 rtx rtl = rtl_for_decl_location (loc);
9126 if (rtl == NULL_RTX)
9127 return 0;
9128 else if (GET_CODE (rtl) == CONST_INT)
9130 HOST_WIDE_INT val = INTVAL (rtl);
9131 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
9132 val &= GET_MODE_MASK (DECL_MODE (loc));
9133 ret = int_loc_descriptor (val);
9135 else if (GET_CODE (rtl) == CONST_STRING)
9136 return 0;
9137 else if (CONSTANT_P (rtl))
9139 ret = new_loc_descr (DW_OP_addr, 0, 0);
9140 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9141 ret->dw_loc_oprnd1.v.val_addr = rtl;
9143 else
9145 enum machine_mode mode;
9147 /* Certain constructs can only be represented at top-level. */
9148 if (want_address == 2)
9149 return loc_descriptor (rtl);
9151 mode = GET_MODE (rtl);
9152 if (MEM_P (rtl))
9154 rtl = XEXP (rtl, 0);
9155 have_address = 1;
9157 ret = mem_loc_descriptor (rtl, mode);
9160 break;
9162 case INDIRECT_REF:
9163 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9164 have_address = 1;
9165 break;
9167 case COMPOUND_EXPR:
9168 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
9170 case NOP_EXPR:
9171 case CONVERT_EXPR:
9172 case NON_LVALUE_EXPR:
9173 case VIEW_CONVERT_EXPR:
9174 case SAVE_EXPR:
9175 case MODIFY_EXPR:
9176 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), want_address);
9178 case COMPONENT_REF:
9179 case BIT_FIELD_REF:
9180 case ARRAY_REF:
9181 case ARRAY_RANGE_REF:
9183 tree obj, offset;
9184 HOST_WIDE_INT bitsize, bitpos, bytepos;
9185 enum machine_mode mode;
9186 int volatilep;
9188 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
9189 &unsignedp, &volatilep, false);
9191 if (obj == loc)
9192 return 0;
9194 ret = loc_descriptor_from_tree_1 (obj, 1);
9195 if (ret == 0
9196 || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
9197 return 0;
9199 if (offset != NULL_TREE)
9201 /* Variable offset. */
9202 add_loc_descr (&ret, loc_descriptor_from_tree_1 (offset, 0));
9203 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9206 bytepos = bitpos / BITS_PER_UNIT;
9207 if (bytepos > 0)
9208 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
9209 else if (bytepos < 0)
9211 add_loc_descr (&ret, int_loc_descriptor (bytepos));
9212 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9215 have_address = 1;
9216 break;
9219 case INTEGER_CST:
9220 if (host_integerp (loc, 0))
9221 ret = int_loc_descriptor (tree_low_cst (loc, 0));
9222 else
9223 return 0;
9224 break;
9226 case CONSTRUCTOR:
9228 /* Get an RTL for this, if something has been emitted. */
9229 rtx rtl = lookup_constant_def (loc);
9230 enum machine_mode mode;
9232 if (!rtl || !MEM_P (rtl))
9233 return 0;
9234 mode = GET_MODE (rtl);
9235 rtl = XEXP (rtl, 0);
9236 ret = mem_loc_descriptor (rtl, mode);
9237 have_address = 1;
9238 break;
9241 case TRUTH_AND_EXPR:
9242 case TRUTH_ANDIF_EXPR:
9243 case BIT_AND_EXPR:
9244 op = DW_OP_and;
9245 goto do_binop;
9247 case TRUTH_XOR_EXPR:
9248 case BIT_XOR_EXPR:
9249 op = DW_OP_xor;
9250 goto do_binop;
9252 case TRUTH_OR_EXPR:
9253 case TRUTH_ORIF_EXPR:
9254 case BIT_IOR_EXPR:
9255 op = DW_OP_or;
9256 goto do_binop;
9258 case FLOOR_DIV_EXPR:
9259 case CEIL_DIV_EXPR:
9260 case ROUND_DIV_EXPR:
9261 case TRUNC_DIV_EXPR:
9262 op = DW_OP_div;
9263 goto do_binop;
9265 case MINUS_EXPR:
9266 op = DW_OP_minus;
9267 goto do_binop;
9269 case FLOOR_MOD_EXPR:
9270 case CEIL_MOD_EXPR:
9271 case ROUND_MOD_EXPR:
9272 case TRUNC_MOD_EXPR:
9273 op = DW_OP_mod;
9274 goto do_binop;
9276 case MULT_EXPR:
9277 op = DW_OP_mul;
9278 goto do_binop;
9280 case LSHIFT_EXPR:
9281 op = DW_OP_shl;
9282 goto do_binop;
9284 case RSHIFT_EXPR:
9285 op = (unsignedp ? DW_OP_shr : DW_OP_shra);
9286 goto do_binop;
9288 case PLUS_EXPR:
9289 if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
9290 && host_integerp (TREE_OPERAND (loc, 1), 0))
9292 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9293 if (ret == 0)
9294 return 0;
9296 add_loc_descr (&ret,
9297 new_loc_descr (DW_OP_plus_uconst,
9298 tree_low_cst (TREE_OPERAND (loc, 1),
9300 0));
9301 break;
9304 op = DW_OP_plus;
9305 goto do_binop;
9307 case LE_EXPR:
9308 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9309 return 0;
9311 op = DW_OP_le;
9312 goto do_binop;
9314 case GE_EXPR:
9315 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9316 return 0;
9318 op = DW_OP_ge;
9319 goto do_binop;
9321 case LT_EXPR:
9322 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9323 return 0;
9325 op = DW_OP_lt;
9326 goto do_binop;
9328 case GT_EXPR:
9329 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9330 return 0;
9332 op = DW_OP_gt;
9333 goto do_binop;
9335 case EQ_EXPR:
9336 op = DW_OP_eq;
9337 goto do_binop;
9339 case NE_EXPR:
9340 op = DW_OP_ne;
9341 goto do_binop;
9343 do_binop:
9344 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9345 ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9346 if (ret == 0 || ret1 == 0)
9347 return 0;
9349 add_loc_descr (&ret, ret1);
9350 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9351 break;
9353 case TRUTH_NOT_EXPR:
9354 case BIT_NOT_EXPR:
9355 op = DW_OP_not;
9356 goto do_unop;
9358 case ABS_EXPR:
9359 op = DW_OP_abs;
9360 goto do_unop;
9362 case NEGATE_EXPR:
9363 op = DW_OP_neg;
9364 goto do_unop;
9366 do_unop:
9367 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9368 if (ret == 0)
9369 return 0;
9371 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9372 break;
9374 case MIN_EXPR:
9375 case MAX_EXPR:
9377 const enum tree_code code =
9378 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
9380 loc = build3 (COND_EXPR, TREE_TYPE (loc),
9381 build2 (code, integer_type_node,
9382 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
9383 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
9386 /* ... fall through ... */
9388 case COND_EXPR:
9390 dw_loc_descr_ref lhs
9391 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9392 dw_loc_descr_ref rhs
9393 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
9394 dw_loc_descr_ref bra_node, jump_node, tmp;
9396 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9397 if (ret == 0 || lhs == 0 || rhs == 0)
9398 return 0;
9400 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
9401 add_loc_descr (&ret, bra_node);
9403 add_loc_descr (&ret, rhs);
9404 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
9405 add_loc_descr (&ret, jump_node);
9407 add_loc_descr (&ret, lhs);
9408 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9409 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
9411 /* ??? Need a node to point the skip at. Use a nop. */
9412 tmp = new_loc_descr (DW_OP_nop, 0, 0);
9413 add_loc_descr (&ret, tmp);
9414 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9415 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
9417 break;
9419 case FIX_TRUNC_EXPR:
9420 case FIX_CEIL_EXPR:
9421 case FIX_FLOOR_EXPR:
9422 case FIX_ROUND_EXPR:
9423 return 0;
9425 default:
9426 /* Leave front-end specific codes as simply unknown. This comes
9427 up, for instance, with the C STMT_EXPR. */
9428 if ((unsigned int) TREE_CODE (loc)
9429 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
9430 return 0;
9432 #ifdef ENABLE_CHECKING
9433 /* Otherwise this is a generic code; we should just lists all of
9434 these explicitly. We forgot one. */
9435 gcc_unreachable ();
9436 #else
9437 /* In a release build, we want to degrade gracefully: better to
9438 generate incomplete debugging information than to crash. */
9439 return NULL;
9440 #endif
9443 /* Show if we can't fill the request for an address. */
9444 if (want_address && !have_address)
9445 return 0;
9447 /* If we've got an address and don't want one, dereference. */
9448 if (!want_address && have_address && ret)
9450 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
9452 if (size > DWARF2_ADDR_SIZE || size == -1)
9453 return 0;
9454 else if (size == DWARF2_ADDR_SIZE)
9455 op = DW_OP_deref;
9456 else
9457 op = DW_OP_deref_size;
9459 add_loc_descr (&ret, new_loc_descr (op, size, 0));
9462 return ret;
9465 static inline dw_loc_descr_ref
9466 loc_descriptor_from_tree (tree loc)
9468 return loc_descriptor_from_tree_1 (loc, 2);
9471 /* Given a value, round it up to the lowest multiple of `boundary'
9472 which is not less than the value itself. */
9474 static inline HOST_WIDE_INT
9475 ceiling (HOST_WIDE_INT value, unsigned int boundary)
9477 return (((value + boundary - 1) / boundary) * boundary);
9480 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
9481 pointer to the declared type for the relevant field variable, or return
9482 `integer_type_node' if the given node turns out to be an
9483 ERROR_MARK node. */
9485 static inline tree
9486 field_type (tree decl)
9488 tree type;
9490 if (TREE_CODE (decl) == ERROR_MARK)
9491 return integer_type_node;
9493 type = DECL_BIT_FIELD_TYPE (decl);
9494 if (type == NULL_TREE)
9495 type = TREE_TYPE (decl);
9497 return type;
9500 /* Given a pointer to a tree node, return the alignment in bits for
9501 it, or else return BITS_PER_WORD if the node actually turns out to
9502 be an ERROR_MARK node. */
9504 static inline unsigned
9505 simple_type_align_in_bits (tree type)
9507 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
9510 static inline unsigned
9511 simple_decl_align_in_bits (tree decl)
9513 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
9516 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
9517 lowest addressed byte of the "containing object" for the given FIELD_DECL,
9518 or return 0 if we are unable to determine what that offset is, either
9519 because the argument turns out to be a pointer to an ERROR_MARK node, or
9520 because the offset is actually variable. (We can't handle the latter case
9521 just yet). */
9523 static HOST_WIDE_INT
9524 field_byte_offset (tree decl)
9526 unsigned int type_align_in_bits;
9527 unsigned int decl_align_in_bits;
9528 unsigned HOST_WIDE_INT type_size_in_bits;
9529 HOST_WIDE_INT object_offset_in_bits;
9530 tree type;
9531 tree field_size_tree;
9532 HOST_WIDE_INT bitpos_int;
9533 HOST_WIDE_INT deepest_bitpos;
9534 unsigned HOST_WIDE_INT field_size_in_bits;
9536 if (TREE_CODE (decl) == ERROR_MARK)
9537 return 0;
9539 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
9541 type = field_type (decl);
9542 field_size_tree = DECL_SIZE (decl);
9544 /* The size could be unspecified if there was an error, or for
9545 a flexible array member. */
9546 if (! field_size_tree)
9547 field_size_tree = bitsize_zero_node;
9549 /* We cannot yet cope with fields whose positions are variable, so
9550 for now, when we see such things, we simply return 0. Someday, we may
9551 be able to handle such cases, but it will be damn difficult. */
9552 if (! host_integerp (bit_position (decl), 0))
9553 return 0;
9555 bitpos_int = int_bit_position (decl);
9557 /* If we don't know the size of the field, pretend it's a full word. */
9558 if (host_integerp (field_size_tree, 1))
9559 field_size_in_bits = tree_low_cst (field_size_tree, 1);
9560 else
9561 field_size_in_bits = BITS_PER_WORD;
9563 type_size_in_bits = simple_type_size_in_bits (type);
9564 type_align_in_bits = simple_type_align_in_bits (type);
9565 decl_align_in_bits = simple_decl_align_in_bits (decl);
9567 /* The GCC front-end doesn't make any attempt to keep track of the starting
9568 bit offset (relative to the start of the containing structure type) of the
9569 hypothetical "containing object" for a bit-field. Thus, when computing
9570 the byte offset value for the start of the "containing object" of a
9571 bit-field, we must deduce this information on our own. This can be rather
9572 tricky to do in some cases. For example, handling the following structure
9573 type definition when compiling for an i386/i486 target (which only aligns
9574 long long's to 32-bit boundaries) can be very tricky:
9576 struct S { int field1; long long field2:31; };
9578 Fortunately, there is a simple rule-of-thumb which can be used in such
9579 cases. When compiling for an i386/i486, GCC will allocate 8 bytes for the
9580 structure shown above. It decides to do this based upon one simple rule
9581 for bit-field allocation. GCC allocates each "containing object" for each
9582 bit-field at the first (i.e. lowest addressed) legitimate alignment
9583 boundary (based upon the required minimum alignment for the declared type
9584 of the field) which it can possibly use, subject to the condition that
9585 there is still enough available space remaining in the containing object
9586 (when allocated at the selected point) to fully accommodate all of the
9587 bits of the bit-field itself.
9589 This simple rule makes it obvious why GCC allocates 8 bytes for each
9590 object of the structure type shown above. When looking for a place to
9591 allocate the "containing object" for `field2', the compiler simply tries
9592 to allocate a 64-bit "containing object" at each successive 32-bit
9593 boundary (starting at zero) until it finds a place to allocate that 64-
9594 bit field such that at least 31 contiguous (and previously unallocated)
9595 bits remain within that selected 64 bit field. (As it turns out, for the
9596 example above, the compiler finds it is OK to allocate the "containing
9597 object" 64-bit field at bit-offset zero within the structure type.)
9599 Here we attempt to work backwards from the limited set of facts we're
9600 given, and we try to deduce from those facts, where GCC must have believed
9601 that the containing object started (within the structure type). The value
9602 we deduce is then used (by the callers of this routine) to generate
9603 DW_AT_location and DW_AT_bit_offset attributes for fields (both bit-fields
9604 and, in the case of DW_AT_location, regular fields as well). */
9606 /* Figure out the bit-distance from the start of the structure to the
9607 "deepest" bit of the bit-field. */
9608 deepest_bitpos = bitpos_int + field_size_in_bits;
9610 /* This is the tricky part. Use some fancy footwork to deduce where the
9611 lowest addressed bit of the containing object must be. */
9612 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9614 /* Round up to type_align by default. This works best for bitfields. */
9615 object_offset_in_bits += type_align_in_bits - 1;
9616 object_offset_in_bits /= type_align_in_bits;
9617 object_offset_in_bits *= type_align_in_bits;
9619 if (object_offset_in_bits > bitpos_int)
9621 /* Sigh, the decl must be packed. */
9622 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9624 /* Round up to decl_align instead. */
9625 object_offset_in_bits += decl_align_in_bits - 1;
9626 object_offset_in_bits /= decl_align_in_bits;
9627 object_offset_in_bits *= decl_align_in_bits;
9630 return object_offset_in_bits / BITS_PER_UNIT;
9633 /* The following routines define various Dwarf attributes and any data
9634 associated with them. */
9636 /* Add a location description attribute value to a DIE.
9638 This emits location attributes suitable for whole variables and
9639 whole parameters. Note that the location attributes for struct fields are
9640 generated by the routine `data_member_location_attribute' below. */
9642 static inline void
9643 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
9644 dw_loc_descr_ref descr)
9646 if (descr != 0)
9647 add_AT_loc (die, attr_kind, descr);
9650 /* Attach the specialized form of location attribute used for data members of
9651 struct and union types. In the special case of a FIELD_DECL node which
9652 represents a bit-field, the "offset" part of this special location
9653 descriptor must indicate the distance in bytes from the lowest-addressed
9654 byte of the containing struct or union type to the lowest-addressed byte of
9655 the "containing object" for the bit-field. (See the `field_byte_offset'
9656 function above).
9658 For any given bit-field, the "containing object" is a hypothetical object
9659 (of some integral or enum type) within which the given bit-field lives. The
9660 type of this hypothetical "containing object" is always the same as the
9661 declared type of the individual bit-field itself (for GCC anyway... the
9662 DWARF spec doesn't actually mandate this). Note that it is the size (in
9663 bytes) of the hypothetical "containing object" which will be given in the
9664 DW_AT_byte_size attribute for this bit-field. (See the
9665 `byte_size_attribute' function below.) It is also used when calculating the
9666 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
9667 function below.) */
9669 static void
9670 add_data_member_location_attribute (dw_die_ref die, tree decl)
9672 HOST_WIDE_INT offset;
9673 dw_loc_descr_ref loc_descr = 0;
9675 if (TREE_CODE (decl) == TREE_BINFO)
9677 /* We're working on the TAG_inheritance for a base class. */
9678 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
9680 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
9681 aren't at a fixed offset from all (sub)objects of the same
9682 type. We need to extract the appropriate offset from our
9683 vtable. The following dwarf expression means
9685 BaseAddr = ObAddr + *((*ObAddr) - Offset)
9687 This is specific to the V3 ABI, of course. */
9689 dw_loc_descr_ref tmp;
9691 /* Make a copy of the object address. */
9692 tmp = new_loc_descr (DW_OP_dup, 0, 0);
9693 add_loc_descr (&loc_descr, tmp);
9695 /* Extract the vtable address. */
9696 tmp = new_loc_descr (DW_OP_deref, 0, 0);
9697 add_loc_descr (&loc_descr, tmp);
9699 /* Calculate the address of the offset. */
9700 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
9701 gcc_assert (offset < 0);
9703 tmp = int_loc_descriptor (-offset);
9704 add_loc_descr (&loc_descr, tmp);
9705 tmp = new_loc_descr (DW_OP_minus, 0, 0);
9706 add_loc_descr (&loc_descr, tmp);
9708 /* Extract the offset. */
9709 tmp = new_loc_descr (DW_OP_deref, 0, 0);
9710 add_loc_descr (&loc_descr, tmp);
9712 /* Add it to the object address. */
9713 tmp = new_loc_descr (DW_OP_plus, 0, 0);
9714 add_loc_descr (&loc_descr, tmp);
9716 else
9717 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
9719 else
9720 offset = field_byte_offset (decl);
9722 if (! loc_descr)
9724 enum dwarf_location_atom op;
9726 /* The DWARF2 standard says that we should assume that the structure
9727 address is already on the stack, so we can specify a structure field
9728 address by using DW_OP_plus_uconst. */
9730 #ifdef MIPS_DEBUGGING_INFO
9731 /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
9732 operator correctly. It works only if we leave the offset on the
9733 stack. */
9734 op = DW_OP_constu;
9735 #else
9736 op = DW_OP_plus_uconst;
9737 #endif
9739 loc_descr = new_loc_descr (op, offset, 0);
9742 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
9745 /* Writes integer values to dw_vec_const array. */
9747 static void
9748 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
9750 while (size != 0)
9752 *dest++ = val & 0xff;
9753 val >>= 8;
9754 --size;
9758 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
9760 static HOST_WIDE_INT
9761 extract_int (const unsigned char *src, unsigned int size)
9763 HOST_WIDE_INT val = 0;
9765 src += size;
9766 while (size != 0)
9768 val <<= 8;
9769 val |= *--src & 0xff;
9770 --size;
9772 return val;
9775 /* Writes floating point values to dw_vec_const array. */
9777 static void
9778 insert_float (rtx rtl, unsigned char *array)
9780 REAL_VALUE_TYPE rv;
9781 long val[4];
9782 int i;
9784 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
9785 real_to_target (val, &rv, GET_MODE (rtl));
9787 /* real_to_target puts 32-bit pieces in each long. Pack them. */
9788 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
9790 insert_int (val[i], 4, array);
9791 array += 4;
9795 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
9796 does not have a "location" either in memory or in a register. These
9797 things can arise in GNU C when a constant is passed as an actual parameter
9798 to an inlined function. They can also arise in C++ where declared
9799 constants do not necessarily get memory "homes". */
9801 static void
9802 add_const_value_attribute (dw_die_ref die, rtx rtl)
9804 switch (GET_CODE (rtl))
9806 case CONST_INT:
9808 HOST_WIDE_INT val = INTVAL (rtl);
9810 if (val < 0)
9811 add_AT_int (die, DW_AT_const_value, val);
9812 else
9813 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
9815 break;
9817 case CONST_DOUBLE:
9818 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
9819 floating-point constant. A CONST_DOUBLE is used whenever the
9820 constant requires more than one word in order to be adequately
9821 represented. We output CONST_DOUBLEs as blocks. */
9823 enum machine_mode mode = GET_MODE (rtl);
9825 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
9827 unsigned int length = GET_MODE_SIZE (mode);
9828 unsigned char *array = ggc_alloc (length);
9830 insert_float (rtl, array);
9831 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
9833 else
9835 /* ??? We really should be using HOST_WIDE_INT throughout. */
9836 gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
9838 add_AT_long_long (die, DW_AT_const_value,
9839 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
9842 break;
9844 case CONST_VECTOR:
9846 enum machine_mode mode = GET_MODE (rtl);
9847 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
9848 unsigned int length = CONST_VECTOR_NUNITS (rtl);
9849 unsigned char *array = ggc_alloc (length * elt_size);
9850 unsigned int i;
9851 unsigned char *p;
9853 switch (GET_MODE_CLASS (mode))
9855 case MODE_VECTOR_INT:
9856 for (i = 0, p = array; i < length; i++, p += elt_size)
9858 rtx elt = CONST_VECTOR_ELT (rtl, i);
9859 HOST_WIDE_INT lo, hi;
9861 switch (GET_CODE (elt))
9863 case CONST_INT:
9864 lo = INTVAL (elt);
9865 hi = -(lo < 0);
9866 break;
9868 case CONST_DOUBLE:
9869 lo = CONST_DOUBLE_LOW (elt);
9870 hi = CONST_DOUBLE_HIGH (elt);
9871 break;
9873 default:
9874 gcc_unreachable ();
9877 if (elt_size <= sizeof (HOST_WIDE_INT))
9878 insert_int (lo, elt_size, p);
9879 else
9881 unsigned char *p0 = p;
9882 unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
9884 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
9885 if (WORDS_BIG_ENDIAN)
9887 p0 = p1;
9888 p1 = p;
9890 insert_int (lo, sizeof (HOST_WIDE_INT), p0);
9891 insert_int (hi, sizeof (HOST_WIDE_INT), p1);
9894 break;
9896 case MODE_VECTOR_FLOAT:
9897 for (i = 0, p = array; i < length; i++, p += elt_size)
9899 rtx elt = CONST_VECTOR_ELT (rtl, i);
9900 insert_float (elt, p);
9902 break;
9904 default:
9905 gcc_unreachable ();
9908 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
9910 break;
9912 case CONST_STRING:
9913 add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
9914 break;
9916 case SYMBOL_REF:
9917 case LABEL_REF:
9918 case CONST:
9919 add_AT_addr (die, DW_AT_const_value, rtl);
9920 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
9921 break;
9923 case PLUS:
9924 /* In cases where an inlined instance of an inline function is passed
9925 the address of an `auto' variable (which is local to the caller) we
9926 can get a situation where the DECL_RTL of the artificial local
9927 variable (for the inlining) which acts as a stand-in for the
9928 corresponding formal parameter (of the inline function) will look
9929 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
9930 exactly a compile-time constant expression, but it isn't the address
9931 of the (artificial) local variable either. Rather, it represents the
9932 *value* which the artificial local variable always has during its
9933 lifetime. We currently have no way to represent such quasi-constant
9934 values in Dwarf, so for now we just punt and generate nothing. */
9935 break;
9937 default:
9938 /* No other kinds of rtx should be possible here. */
9939 gcc_unreachable ();
9944 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
9945 for use in a later add_const_value_attribute call. */
9947 static rtx
9948 rtl_for_decl_init (tree init, tree type)
9950 rtx rtl = NULL_RTX;
9952 /* If a variable is initialized with a string constant without embedded
9953 zeros, build CONST_STRING. */
9954 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
9956 tree enttype = TREE_TYPE (type);
9957 tree domain = TYPE_DOMAIN (type);
9958 enum machine_mode mode = TYPE_MODE (enttype);
9960 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
9961 && domain
9962 && integer_zerop (TYPE_MIN_VALUE (domain))
9963 && compare_tree_int (TYPE_MAX_VALUE (domain),
9964 TREE_STRING_LENGTH (init) - 1) == 0
9965 && ((size_t) TREE_STRING_LENGTH (init)
9966 == strlen (TREE_STRING_POINTER (init)) + 1))
9967 rtl = gen_rtx_CONST_STRING (VOIDmode,
9968 ggc_strdup (TREE_STRING_POINTER (init)));
9970 /* If the initializer is something that we know will expand into an
9971 immediate RTL constant, expand it now. Expanding anything else
9972 tends to produce unresolved symbols; see debug/5770 and c++/6381. */
9973 /* Aggregate, vector, and complex types may contain constructors that may
9974 result in code being generated when expand_expr is called, so we can't
9975 handle them here. Integer and float are useful and safe types to handle
9976 here. */
9977 else if ((INTEGRAL_TYPE_P (type) || SCALAR_FLOAT_TYPE_P (type))
9978 && initializer_constant_valid_p (init, type) == null_pointer_node)
9980 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
9982 /* If expand_expr returns a MEM, it wasn't immediate. */
9983 gcc_assert (!rtl || !MEM_P (rtl));
9986 return rtl;
9989 /* Generate RTL for the variable DECL to represent its location. */
9991 static rtx
9992 rtl_for_decl_location (tree decl)
9994 rtx rtl;
9996 /* Here we have to decide where we are going to say the parameter "lives"
9997 (as far as the debugger is concerned). We only have a couple of
9998 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
10000 DECL_RTL normally indicates where the parameter lives during most of the
10001 activation of the function. If optimization is enabled however, this
10002 could be either NULL or else a pseudo-reg. Both of those cases indicate
10003 that the parameter doesn't really live anywhere (as far as the code
10004 generation parts of GCC are concerned) during most of the function's
10005 activation. That will happen (for example) if the parameter is never
10006 referenced within the function.
10008 We could just generate a location descriptor here for all non-NULL
10009 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
10010 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
10011 where DECL_RTL is NULL or is a pseudo-reg.
10013 Note however that we can only get away with using DECL_INCOMING_RTL as
10014 a backup substitute for DECL_RTL in certain limited cases. In cases
10015 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
10016 we can be sure that the parameter was passed using the same type as it is
10017 declared to have within the function, and that its DECL_INCOMING_RTL
10018 points us to a place where a value of that type is passed.
10020 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
10021 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
10022 because in these cases DECL_INCOMING_RTL points us to a value of some
10023 type which is *different* from the type of the parameter itself. Thus,
10024 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
10025 such cases, the debugger would end up (for example) trying to fetch a
10026 `float' from a place which actually contains the first part of a
10027 `double'. That would lead to really incorrect and confusing
10028 output at debug-time.
10030 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
10031 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
10032 are a couple of exceptions however. On little-endian machines we can
10033 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
10034 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
10035 an integral type that is smaller than TREE_TYPE (decl). These cases arise
10036 when (on a little-endian machine) a non-prototyped function has a
10037 parameter declared to be of type `short' or `char'. In such cases,
10038 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
10039 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
10040 passed `int' value. If the debugger then uses that address to fetch
10041 a `short' or a `char' (on a little-endian machine) the result will be
10042 the correct data, so we allow for such exceptional cases below.
10044 Note that our goal here is to describe the place where the given formal
10045 parameter lives during most of the function's activation (i.e. between the
10046 end of the prologue and the start of the epilogue). We'll do that as best
10047 as we can. Note however that if the given formal parameter is modified
10048 sometime during the execution of the function, then a stack backtrace (at
10049 debug-time) will show the function as having been called with the *new*
10050 value rather than the value which was originally passed in. This happens
10051 rarely enough that it is not a major problem, but it *is* a problem, and
10052 I'd like to fix it.
10054 A future version of dwarf2out.c may generate two additional attributes for
10055 any given DW_TAG_formal_parameter DIE which will describe the "passed
10056 type" and the "passed location" for the given formal parameter in addition
10057 to the attributes we now generate to indicate the "declared type" and the
10058 "active location" for each parameter. This additional set of attributes
10059 could be used by debuggers for stack backtraces. Separately, note that
10060 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
10061 This happens (for example) for inlined-instances of inline function formal
10062 parameters which are never referenced. This really shouldn't be
10063 happening. All PARM_DECL nodes should get valid non-NULL
10064 DECL_INCOMING_RTL values. FIXME. */
10066 /* Use DECL_RTL as the "location" unless we find something better. */
10067 rtl = DECL_RTL_IF_SET (decl);
10069 /* When generating abstract instances, ignore everything except
10070 constants, symbols living in memory, and symbols living in
10071 fixed registers. */
10072 if (! reload_completed)
10074 if (rtl
10075 && (CONSTANT_P (rtl)
10076 || (MEM_P (rtl)
10077 && CONSTANT_P (XEXP (rtl, 0)))
10078 || (REG_P (rtl)
10079 && TREE_CODE (decl) == VAR_DECL
10080 && TREE_STATIC (decl))))
10082 rtl = targetm.delegitimize_address (rtl);
10083 return rtl;
10085 rtl = NULL_RTX;
10087 else if (TREE_CODE (decl) == PARM_DECL)
10089 if (rtl == NULL_RTX || is_pseudo_reg (rtl))
10091 tree declared_type = TREE_TYPE (decl);
10092 tree passed_type = DECL_ARG_TYPE (decl);
10093 enum machine_mode dmode = TYPE_MODE (declared_type);
10094 enum machine_mode pmode = TYPE_MODE (passed_type);
10096 /* This decl represents a formal parameter which was optimized out.
10097 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
10098 all cases where (rtl == NULL_RTX) just below. */
10099 if (dmode == pmode)
10100 rtl = DECL_INCOMING_RTL (decl);
10101 else if (SCALAR_INT_MODE_P (dmode)
10102 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
10103 && DECL_INCOMING_RTL (decl))
10105 rtx inc = DECL_INCOMING_RTL (decl);
10106 if (REG_P (inc))
10107 rtl = inc;
10108 else if (MEM_P (inc))
10110 if (BYTES_BIG_ENDIAN)
10111 rtl = adjust_address_nv (inc, dmode,
10112 GET_MODE_SIZE (pmode)
10113 - GET_MODE_SIZE (dmode));
10114 else
10115 rtl = inc;
10120 /* If the parm was passed in registers, but lives on the stack, then
10121 make a big endian correction if the mode of the type of the
10122 parameter is not the same as the mode of the rtl. */
10123 /* ??? This is the same series of checks that are made in dbxout.c before
10124 we reach the big endian correction code there. It isn't clear if all
10125 of these checks are necessary here, but keeping them all is the safe
10126 thing to do. */
10127 else if (MEM_P (rtl)
10128 && XEXP (rtl, 0) != const0_rtx
10129 && ! CONSTANT_P (XEXP (rtl, 0))
10130 /* Not passed in memory. */
10131 && !MEM_P (DECL_INCOMING_RTL (decl))
10132 /* Not passed by invisible reference. */
10133 && (!REG_P (XEXP (rtl, 0))
10134 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
10135 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
10136 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
10137 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
10138 #endif
10140 /* Big endian correction check. */
10141 && BYTES_BIG_ENDIAN
10142 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
10143 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
10144 < UNITS_PER_WORD))
10146 int offset = (UNITS_PER_WORD
10147 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
10149 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10150 plus_constant (XEXP (rtl, 0), offset));
10153 else if (TREE_CODE (decl) == VAR_DECL
10154 && rtl
10155 && MEM_P (rtl)
10156 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
10157 && BYTES_BIG_ENDIAN)
10159 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
10160 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
10162 /* If a variable is declared "register" yet is smaller than
10163 a register, then if we store the variable to memory, it
10164 looks like we're storing a register-sized value, when in
10165 fact we are not. We need to adjust the offset of the
10166 storage location to reflect the actual value's bytes,
10167 else gdb will not be able to display it. */
10168 if (rsize > dsize)
10169 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10170 plus_constant (XEXP (rtl, 0), rsize-dsize));
10173 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
10174 and will have been substituted directly into all expressions that use it.
10175 C does not have such a concept, but C++ and other languages do. */
10176 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
10177 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
10179 if (rtl)
10180 rtl = targetm.delegitimize_address (rtl);
10182 /* If we don't look past the constant pool, we risk emitting a
10183 reference to a constant pool entry that isn't referenced from
10184 code, and thus is not emitted. */
10185 if (rtl)
10186 rtl = avoid_constant_pool_reference (rtl);
10188 return rtl;
10191 /* We need to figure out what section we should use as the base for the
10192 address ranges where a given location is valid.
10193 1. If this particular DECL has a section associated with it, use that.
10194 2. If this function has a section associated with it, use that.
10195 3. Otherwise, use the text section.
10196 XXX: If you split a variable across multiple sections, we won't notice. */
10198 static const char *
10199 secname_for_decl (tree decl)
10201 const char *secname;
10203 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
10205 tree sectree = DECL_SECTION_NAME (decl);
10206 secname = TREE_STRING_POINTER (sectree);
10208 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
10210 tree sectree = DECL_SECTION_NAME (current_function_decl);
10211 secname = TREE_STRING_POINTER (sectree);
10213 else if (cfun
10214 && (last_text_section == in_unlikely_executed_text
10215 || (last_text_section == in_named
10216 && last_text_section_name
10217 == cfun->unlikely_text_section_name)))
10218 secname = cfun->cold_section_label;
10219 else
10220 secname = text_section_label;
10222 return secname;
10225 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
10226 data attribute for a variable or a parameter. We generate the
10227 DW_AT_const_value attribute only in those cases where the given variable
10228 or parameter does not have a true "location" either in memory or in a
10229 register. This can happen (for example) when a constant is passed as an
10230 actual argument in a call to an inline function. (It's possible that
10231 these things can crop up in other ways also.) Note that one type of
10232 constant value which can be passed into an inlined function is a constant
10233 pointer. This can happen for example if an actual argument in an inlined
10234 function call evaluates to a compile-time constant address. */
10236 static void
10237 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
10238 enum dwarf_attribute attr)
10240 rtx rtl;
10241 dw_loc_descr_ref descr;
10242 var_loc_list *loc_list;
10243 struct var_loc_node *node;
10244 if (TREE_CODE (decl) == ERROR_MARK)
10245 return;
10247 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
10248 || TREE_CODE (decl) == RESULT_DECL);
10250 /* See if we possibly have multiple locations for this variable. */
10251 loc_list = lookup_decl_loc (decl);
10253 /* If it truly has multiple locations, the first and last node will
10254 differ. */
10255 if (loc_list && loc_list->first != loc_list->last)
10257 const char *endname, *secname;
10258 dw_loc_list_ref list;
10259 rtx varloc;
10261 /* Now that we know what section we are using for a base,
10262 actually construct the list of locations.
10263 The first location information is what is passed to the
10264 function that creates the location list, and the remaining
10265 locations just get added on to that list.
10266 Note that we only know the start address for a location
10267 (IE location changes), so to build the range, we use
10268 the range [current location start, next location start].
10269 This means we have to special case the last node, and generate
10270 a range of [last location start, end of function label]. */
10272 node = loc_list->first;
10273 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10274 secname = secname_for_decl (decl);
10276 list = new_loc_list (loc_descriptor (varloc),
10277 node->label, node->next->label, secname, 1);
10278 node = node->next;
10280 for (; node->next; node = node->next)
10281 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10283 /* The variable has a location between NODE->LABEL and
10284 NODE->NEXT->LABEL. */
10285 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10286 add_loc_descr_to_loc_list (&list, loc_descriptor (varloc),
10287 node->label, node->next->label, secname);
10290 /* If the variable has a location at the last label
10291 it keeps its location until the end of function. */
10292 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10294 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
10296 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10297 if (!current_function_decl)
10298 endname = text_end_label;
10299 else
10301 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
10302 current_function_funcdef_no);
10303 endname = ggc_strdup (label_id);
10305 add_loc_descr_to_loc_list (&list, loc_descriptor (varloc),
10306 node->label, endname, secname);
10309 /* Finally, add the location list to the DIE, and we are done. */
10310 add_AT_loc_list (die, attr, list);
10311 return;
10314 /* Try to get some constant RTL for this decl, and use that as the value of
10315 the location. */
10317 rtl = rtl_for_decl_location (decl);
10318 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
10320 add_const_value_attribute (die, rtl);
10321 return;
10324 /* If we have tried to generate the location otherwise, and it
10325 didn't work out (we wouldn't be here if we did), and we have a one entry
10326 location list, try generating a location from that. */
10327 if (loc_list && loc_list->first)
10329 node = loc_list->first;
10330 descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note));
10331 if (descr)
10333 add_AT_location_description (die, attr, descr);
10334 return;
10338 /* We couldn't get any rtl, so try directly generating the location
10339 description from the tree. */
10340 descr = loc_descriptor_from_tree (decl);
10341 if (descr)
10343 add_AT_location_description (die, attr, descr);
10344 return;
10348 /* If we don't have a copy of this variable in memory for some reason (such
10349 as a C++ member constant that doesn't have an out-of-line definition),
10350 we should tell the debugger about the constant value. */
10352 static void
10353 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
10355 tree init = DECL_INITIAL (decl);
10356 tree type = TREE_TYPE (decl);
10357 rtx rtl;
10359 if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
10360 /* OK */;
10361 else
10362 return;
10364 rtl = rtl_for_decl_init (init, type);
10365 if (rtl)
10366 add_const_value_attribute (var_die, rtl);
10369 /* Convert the CFI instructions for the current function into a location
10370 list. This is used for DW_AT_frame_base when we targeting a dwarf2
10371 consumer that does not support the dwarf3 DW_OP_call_frame_cfa. */
10373 static dw_loc_list_ref
10374 convert_cfa_to_loc_list (void)
10376 dw_fde_ref fde;
10377 dw_loc_list_ref list, *list_tail;
10378 dw_cfi_ref cfi;
10379 dw_cfa_location last_cfa, next_cfa;
10380 const char *start_label, *last_label, *section;
10382 fde = &fde_table[fde_table_in_use - 1];
10384 section = secname_for_decl (current_function_decl);
10385 list_tail = &list;
10386 list = NULL;
10388 next_cfa.reg = INVALID_REGNUM;
10389 next_cfa.offset = 0;
10390 next_cfa.indirect = 0;
10391 next_cfa.base_offset = 0;
10393 start_label = fde->dw_fde_begin;
10395 /* ??? Bald assumption that the CIE opcode list does not contain
10396 advance opcodes. */
10397 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
10398 lookup_cfa_1 (cfi, &next_cfa);
10400 last_cfa = next_cfa;
10401 last_label = start_label;
10403 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
10404 switch (cfi->dw_cfi_opc)
10406 case DW_CFA_set_loc:
10407 case DW_CFA_advance_loc1:
10408 case DW_CFA_advance_loc2:
10409 case DW_CFA_advance_loc4:
10410 if (!cfa_equal_p (&last_cfa, &next_cfa))
10412 *list_tail = new_loc_list (build_cfa_loc (&last_cfa), start_label,
10413 last_label, section, list == NULL);
10415 list_tail = &(*list_tail)->dw_loc_next;
10416 last_cfa = next_cfa;
10417 start_label = last_label;
10419 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
10420 break;
10422 case DW_CFA_advance_loc:
10423 /* The encoding is complex enough that we should never emit this. */
10424 case DW_CFA_remember_state:
10425 case DW_CFA_restore_state:
10426 /* We don't handle these two in this function. It would be possible
10427 if it were to be required. */
10428 gcc_unreachable ();
10430 default:
10431 lookup_cfa_1 (cfi, &next_cfa);
10432 break;
10435 if (!cfa_equal_p (&last_cfa, &next_cfa))
10437 *list_tail = new_loc_list (build_cfa_loc (&last_cfa), start_label,
10438 last_label, section, list == NULL);
10439 list_tail = &(*list_tail)->dw_loc_next;
10440 start_label = last_label;
10442 *list_tail = new_loc_list (build_cfa_loc (&next_cfa), start_label,
10443 fde->dw_fde_end, section, list == NULL);
10445 return list;
10448 /* Compute a displacement from the "steady-state frame pointer" to
10449 the CFA, and store it in frame_pointer_cfa_offset. */
10451 static void
10452 compute_frame_pointer_to_cfa_displacement (void)
10454 HOST_WIDE_INT offset;
10455 rtx reg, elim;
10457 #ifdef FRAME_POINTER_CFA_OFFSET
10458 reg = frame_pointer_rtx;
10459 offset = FRAME_POINTER_CFA_OFFSET (current_function_decl);
10460 #else
10461 reg = arg_pointer_rtx;
10462 offset = ARG_POINTER_CFA_OFFSET (current_function_decl);
10463 #endif
10465 elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10466 if (GET_CODE (elim) == PLUS)
10468 offset += INTVAL (XEXP (elim, 1));
10469 elim = XEXP (elim, 0);
10471 gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
10472 : stack_pointer_rtx));
10474 frame_pointer_cfa_offset = -offset;
10477 /* Generate a DW_AT_name attribute given some string value to be included as
10478 the value of the attribute. */
10480 static void
10481 add_name_attribute (dw_die_ref die, const char *name_string)
10483 if (name_string != NULL && *name_string != 0)
10485 if (demangle_name_func)
10486 name_string = (*demangle_name_func) (name_string);
10488 add_AT_string (die, DW_AT_name, name_string);
10492 /* Generate a DW_AT_comp_dir attribute for DIE. */
10494 static void
10495 add_comp_dir_attribute (dw_die_ref die)
10497 const char *wd = get_src_pwd ();
10498 if (wd != NULL)
10499 add_AT_string (die, DW_AT_comp_dir, wd);
10502 /* Given a tree node describing an array bound (either lower or upper) output
10503 a representation for that bound. */
10505 static void
10506 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
10508 switch (TREE_CODE (bound))
10510 case ERROR_MARK:
10511 return;
10513 /* All fixed-bounds are represented by INTEGER_CST nodes. */
10514 case INTEGER_CST:
10515 if (! host_integerp (bound, 0)
10516 || (bound_attr == DW_AT_lower_bound
10517 && (((is_c_family () || is_java ()) && integer_zerop (bound))
10518 || (is_fortran () && integer_onep (bound)))))
10519 /* Use the default. */
10521 else
10522 add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
10523 break;
10525 case CONVERT_EXPR:
10526 case NOP_EXPR:
10527 case NON_LVALUE_EXPR:
10528 case VIEW_CONVERT_EXPR:
10529 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
10530 break;
10532 case SAVE_EXPR:
10533 break;
10535 case VAR_DECL:
10536 case PARM_DECL:
10537 case RESULT_DECL:
10539 dw_die_ref decl_die = lookup_decl_die (bound);
10541 /* ??? Can this happen, or should the variable have been bound
10542 first? Probably it can, since I imagine that we try to create
10543 the types of parameters in the order in which they exist in
10544 the list, and won't have created a forward reference to a
10545 later parameter. */
10546 if (decl_die != NULL)
10547 add_AT_die_ref (subrange_die, bound_attr, decl_die);
10548 break;
10551 default:
10553 /* Otherwise try to create a stack operation procedure to
10554 evaluate the value of the array bound. */
10556 dw_die_ref ctx, decl_die;
10557 dw_loc_descr_ref loc;
10559 loc = loc_descriptor_from_tree (bound);
10560 if (loc == NULL)
10561 break;
10563 if (current_function_decl == 0)
10564 ctx = comp_unit_die;
10565 else
10566 ctx = lookup_decl_die (current_function_decl);
10568 decl_die = new_die (DW_TAG_variable, ctx, bound);
10569 add_AT_flag (decl_die, DW_AT_artificial, 1);
10570 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
10571 add_AT_loc (decl_die, DW_AT_location, loc);
10573 add_AT_die_ref (subrange_die, bound_attr, decl_die);
10574 break;
10579 /* Note that the block of subscript information for an array type also
10580 includes information about the element type of type given array type. */
10582 static void
10583 add_subscript_info (dw_die_ref type_die, tree type)
10585 #ifndef MIPS_DEBUGGING_INFO
10586 unsigned dimension_number;
10587 #endif
10588 tree lower, upper;
10589 dw_die_ref subrange_die;
10591 /* The GNU compilers represent multidimensional array types as sequences of
10592 one dimensional array types whose element types are themselves array
10593 types. Here we squish that down, so that each multidimensional array
10594 type gets only one array_type DIE in the Dwarf debugging info. The draft
10595 Dwarf specification say that we are allowed to do this kind of
10596 compression in C (because there is no difference between an array or
10597 arrays and a multidimensional array in C) but for other source languages
10598 (e.g. Ada) we probably shouldn't do this. */
10600 /* ??? The SGI dwarf reader fails for multidimensional arrays with a
10601 const enum type. E.g. const enum machine_mode insn_operand_mode[2][10].
10602 We work around this by disabling this feature. See also
10603 gen_array_type_die. */
10604 #ifndef MIPS_DEBUGGING_INFO
10605 for (dimension_number = 0;
10606 TREE_CODE (type) == ARRAY_TYPE;
10607 type = TREE_TYPE (type), dimension_number++)
10608 #endif
10610 tree domain = TYPE_DOMAIN (type);
10612 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
10613 and (in GNU C only) variable bounds. Handle all three forms
10614 here. */
10615 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
10616 if (domain)
10618 /* We have an array type with specified bounds. */
10619 lower = TYPE_MIN_VALUE (domain);
10620 upper = TYPE_MAX_VALUE (domain);
10622 /* Define the index type. */
10623 if (TREE_TYPE (domain))
10625 /* ??? This is probably an Ada unnamed subrange type. Ignore the
10626 TREE_TYPE field. We can't emit debug info for this
10627 because it is an unnamed integral type. */
10628 if (TREE_CODE (domain) == INTEGER_TYPE
10629 && TYPE_NAME (domain) == NULL_TREE
10630 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
10631 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
10633 else
10634 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
10635 type_die);
10638 /* ??? If upper is NULL, the array has unspecified length,
10639 but it does have a lower bound. This happens with Fortran
10640 dimension arr(N:*)
10641 Since the debugger is definitely going to need to know N
10642 to produce useful results, go ahead and output the lower
10643 bound solo, and hope the debugger can cope. */
10645 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
10646 if (upper)
10647 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
10650 /* Otherwise we have an array type with an unspecified length. The
10651 DWARF-2 spec does not say how to handle this; let's just leave out the
10652 bounds. */
10656 static void
10657 add_byte_size_attribute (dw_die_ref die, tree tree_node)
10659 unsigned size;
10661 switch (TREE_CODE (tree_node))
10663 case ERROR_MARK:
10664 size = 0;
10665 break;
10666 case ENUMERAL_TYPE:
10667 case RECORD_TYPE:
10668 case UNION_TYPE:
10669 case QUAL_UNION_TYPE:
10670 size = int_size_in_bytes (tree_node);
10671 break;
10672 case FIELD_DECL:
10673 /* For a data member of a struct or union, the DW_AT_byte_size is
10674 generally given as the number of bytes normally allocated for an
10675 object of the *declared* type of the member itself. This is true
10676 even for bit-fields. */
10677 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
10678 break;
10679 default:
10680 gcc_unreachable ();
10683 /* Note that `size' might be -1 when we get to this point. If it is, that
10684 indicates that the byte size of the entity in question is variable. We
10685 have no good way of expressing this fact in Dwarf at the present time,
10686 so just let the -1 pass on through. */
10687 add_AT_unsigned (die, DW_AT_byte_size, size);
10690 /* For a FIELD_DECL node which represents a bit-field, output an attribute
10691 which specifies the distance in bits from the highest order bit of the
10692 "containing object" for the bit-field to the highest order bit of the
10693 bit-field itself.
10695 For any given bit-field, the "containing object" is a hypothetical object
10696 (of some integral or enum type) within which the given bit-field lives. The
10697 type of this hypothetical "containing object" is always the same as the
10698 declared type of the individual bit-field itself. The determination of the
10699 exact location of the "containing object" for a bit-field is rather
10700 complicated. It's handled by the `field_byte_offset' function (above).
10702 Note that it is the size (in bytes) of the hypothetical "containing object"
10703 which will be given in the DW_AT_byte_size attribute for this bit-field.
10704 (See `byte_size_attribute' above). */
10706 static inline void
10707 add_bit_offset_attribute (dw_die_ref die, tree decl)
10709 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
10710 tree type = DECL_BIT_FIELD_TYPE (decl);
10711 HOST_WIDE_INT bitpos_int;
10712 HOST_WIDE_INT highest_order_object_bit_offset;
10713 HOST_WIDE_INT highest_order_field_bit_offset;
10714 HOST_WIDE_INT unsigned bit_offset;
10716 /* Must be a field and a bit field. */
10717 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
10719 /* We can't yet handle bit-fields whose offsets are variable, so if we
10720 encounter such things, just return without generating any attribute
10721 whatsoever. Likewise for variable or too large size. */
10722 if (! host_integerp (bit_position (decl), 0)
10723 || ! host_integerp (DECL_SIZE (decl), 1))
10724 return;
10726 bitpos_int = int_bit_position (decl);
10728 /* Note that the bit offset is always the distance (in bits) from the
10729 highest-order bit of the "containing object" to the highest-order bit of
10730 the bit-field itself. Since the "high-order end" of any object or field
10731 is different on big-endian and little-endian machines, the computation
10732 below must take account of these differences. */
10733 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
10734 highest_order_field_bit_offset = bitpos_int;
10736 if (! BYTES_BIG_ENDIAN)
10738 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
10739 highest_order_object_bit_offset += simple_type_size_in_bits (type);
10742 bit_offset
10743 = (! BYTES_BIG_ENDIAN
10744 ? highest_order_object_bit_offset - highest_order_field_bit_offset
10745 : highest_order_field_bit_offset - highest_order_object_bit_offset);
10747 add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
10750 /* For a FIELD_DECL node which represents a bit field, output an attribute
10751 which specifies the length in bits of the given field. */
10753 static inline void
10754 add_bit_size_attribute (dw_die_ref die, tree decl)
10756 /* Must be a field and a bit field. */
10757 gcc_assert (TREE_CODE (decl) == FIELD_DECL
10758 && DECL_BIT_FIELD_TYPE (decl));
10760 if (host_integerp (DECL_SIZE (decl), 1))
10761 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
10764 /* If the compiled language is ANSI C, then add a 'prototyped'
10765 attribute, if arg types are given for the parameters of a function. */
10767 static inline void
10768 add_prototyped_attribute (dw_die_ref die, tree func_type)
10770 if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
10771 && TYPE_ARG_TYPES (func_type) != NULL)
10772 add_AT_flag (die, DW_AT_prototyped, 1);
10775 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
10776 by looking in either the type declaration or object declaration
10777 equate table. */
10779 static inline void
10780 add_abstract_origin_attribute (dw_die_ref die, tree origin)
10782 dw_die_ref origin_die = NULL;
10784 if (TREE_CODE (origin) != FUNCTION_DECL)
10786 /* We may have gotten separated from the block for the inlined
10787 function, if we're in an exception handler or some such; make
10788 sure that the abstract function has been written out.
10790 Doing this for nested functions is wrong, however; functions are
10791 distinct units, and our context might not even be inline. */
10792 tree fn = origin;
10794 if (TYPE_P (fn))
10795 fn = TYPE_STUB_DECL (fn);
10797 fn = decl_function_context (fn);
10798 if (fn)
10799 dwarf2out_abstract_function (fn);
10802 if (DECL_P (origin))
10803 origin_die = lookup_decl_die (origin);
10804 else if (TYPE_P (origin))
10805 origin_die = lookup_type_die (origin);
10807 /* XXX: Functions that are never lowered don't always have correct block
10808 trees (in the case of java, they simply have no block tree, in some other
10809 languages). For these functions, there is nothing we can really do to
10810 output correct debug info for inlined functions in all cases. Rather
10811 than die, we'll just produce deficient debug info now, in that we will
10812 have variables without a proper abstract origin. In the future, when all
10813 functions are lowered, we should re-add a gcc_assert (origin_die)
10814 here. */
10816 if (origin_die)
10817 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
10820 /* We do not currently support the pure_virtual attribute. */
10822 static inline void
10823 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
10825 if (DECL_VINDEX (func_decl))
10827 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
10829 if (host_integerp (DECL_VINDEX (func_decl), 0))
10830 add_AT_loc (die, DW_AT_vtable_elem_location,
10831 new_loc_descr (DW_OP_constu,
10832 tree_low_cst (DECL_VINDEX (func_decl), 0),
10833 0));
10835 /* GNU extension: Record what type this method came from originally. */
10836 if (debug_info_level > DINFO_LEVEL_TERSE)
10837 add_AT_die_ref (die, DW_AT_containing_type,
10838 lookup_type_die (DECL_CONTEXT (func_decl)));
10842 /* Add source coordinate attributes for the given decl. */
10844 static void
10845 add_src_coords_attributes (dw_die_ref die, tree decl)
10847 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
10848 unsigned file_index = lookup_filename (s.file);
10850 add_AT_unsigned (die, DW_AT_decl_file, file_index);
10851 add_AT_unsigned (die, DW_AT_decl_line, s.line);
10854 /* Add a DW_AT_name attribute and source coordinate attribute for the
10855 given decl, but only if it actually has a name. */
10857 static void
10858 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
10860 tree decl_name;
10862 decl_name = DECL_NAME (decl);
10863 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
10865 add_name_attribute (die, dwarf2_name (decl, 0));
10866 if (! DECL_ARTIFICIAL (decl))
10867 add_src_coords_attributes (die, decl);
10869 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
10870 && TREE_PUBLIC (decl)
10871 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
10872 && !DECL_ABSTRACT (decl)
10873 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl)))
10874 add_AT_string (die, DW_AT_MIPS_linkage_name,
10875 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
10878 #ifdef VMS_DEBUGGING_INFO
10879 /* Get the function's name, as described by its RTL. This may be different
10880 from the DECL_NAME name used in the source file. */
10881 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
10883 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
10884 XEXP (DECL_RTL (decl), 0));
10885 VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
10887 #endif
10890 /* Push a new declaration scope. */
10892 static void
10893 push_decl_scope (tree scope)
10895 VEC_safe_push (tree, gc, decl_scope_table, scope);
10898 /* Pop a declaration scope. */
10900 static inline void
10901 pop_decl_scope (void)
10903 VEC_pop (tree, decl_scope_table);
10906 /* Return the DIE for the scope that immediately contains this type.
10907 Non-named types get global scope. Named types nested in other
10908 types get their containing scope if it's open, or global scope
10909 otherwise. All other types (i.e. function-local named types) get
10910 the current active scope. */
10912 static dw_die_ref
10913 scope_die_for (tree t, dw_die_ref context_die)
10915 dw_die_ref scope_die = NULL;
10916 tree containing_scope;
10917 int i;
10919 /* Non-types always go in the current scope. */
10920 gcc_assert (TYPE_P (t));
10922 containing_scope = TYPE_CONTEXT (t);
10924 /* Use the containing namespace if it was passed in (for a declaration). */
10925 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
10927 if (context_die == lookup_decl_die (containing_scope))
10928 /* OK */;
10929 else
10930 containing_scope = NULL_TREE;
10933 /* Ignore function type "scopes" from the C frontend. They mean that
10934 a tagged type is local to a parmlist of a function declarator, but
10935 that isn't useful to DWARF. */
10936 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
10937 containing_scope = NULL_TREE;
10939 if (containing_scope == NULL_TREE)
10940 scope_die = comp_unit_die;
10941 else if (TYPE_P (containing_scope))
10943 /* For types, we can just look up the appropriate DIE. But
10944 first we check to see if we're in the middle of emitting it
10945 so we know where the new DIE should go. */
10946 for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
10947 if (VEC_index (tree, decl_scope_table, i) == containing_scope)
10948 break;
10950 if (i < 0)
10952 gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
10953 || TREE_ASM_WRITTEN (containing_scope));
10955 /* If none of the current dies are suitable, we get file scope. */
10956 scope_die = comp_unit_die;
10958 else
10959 scope_die = lookup_type_die (containing_scope);
10961 else
10962 scope_die = context_die;
10964 return scope_die;
10967 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
10969 static inline int
10970 local_scope_p (dw_die_ref context_die)
10972 for (; context_die; context_die = context_die->die_parent)
10973 if (context_die->die_tag == DW_TAG_inlined_subroutine
10974 || context_die->die_tag == DW_TAG_subprogram)
10975 return 1;
10977 return 0;
10980 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
10981 whether or not to treat a DIE in this context as a declaration. */
10983 static inline int
10984 class_or_namespace_scope_p (dw_die_ref context_die)
10986 return (context_die
10987 && (context_die->die_tag == DW_TAG_structure_type
10988 || context_die->die_tag == DW_TAG_union_type
10989 || context_die->die_tag == DW_TAG_namespace));
10992 /* Many forms of DIEs require a "type description" attribute. This
10993 routine locates the proper "type descriptor" die for the type given
10994 by 'type', and adds a DW_AT_type attribute below the given die. */
10996 static void
10997 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
10998 int decl_volatile, dw_die_ref context_die)
11000 enum tree_code code = TREE_CODE (type);
11001 dw_die_ref type_die = NULL;
11003 /* ??? If this type is an unnamed subrange type of an integral or
11004 floating-point type, use the inner type. This is because we have no
11005 support for unnamed types in base_type_die. This can happen if this is
11006 an Ada subrange type. Correct solution is emit a subrange type die. */
11007 if ((code == INTEGER_TYPE || code == REAL_TYPE)
11008 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
11009 type = TREE_TYPE (type), code = TREE_CODE (type);
11011 if (code == ERROR_MARK
11012 /* Handle a special case. For functions whose return type is void, we
11013 generate *no* type attribute. (Note that no object may have type
11014 `void', so this only applies to function return types). */
11015 || code == VOID_TYPE)
11016 return;
11018 type_die = modified_type_die (type,
11019 decl_const || TYPE_READONLY (type),
11020 decl_volatile || TYPE_VOLATILE (type),
11021 context_die);
11023 if (type_die != NULL)
11024 add_AT_die_ref (object_die, DW_AT_type, type_die);
11027 /* Given an object die, add the calling convention attribute for the
11028 function call type. */
11029 static void
11030 add_calling_convention_attribute (dw_die_ref subr_die, tree type)
11032 enum dwarf_calling_convention value = DW_CC_normal;
11034 value = targetm.dwarf_calling_convention (type);
11036 /* Only add the attribute if the backend requests it, and
11037 is not DW_CC_normal. */
11038 if (value && (value != DW_CC_normal))
11039 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
11042 /* Given a tree pointer to a struct, class, union, or enum type node, return
11043 a pointer to the (string) tag name for the given type, or zero if the type
11044 was declared without a tag. */
11046 static const char *
11047 type_tag (tree type)
11049 const char *name = 0;
11051 if (TYPE_NAME (type) != 0)
11053 tree t = 0;
11055 /* Find the IDENTIFIER_NODE for the type name. */
11056 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
11057 t = TYPE_NAME (type);
11059 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
11060 a TYPE_DECL node, regardless of whether or not a `typedef' was
11061 involved. */
11062 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11063 && ! DECL_IGNORED_P (TYPE_NAME (type)))
11064 t = DECL_NAME (TYPE_NAME (type));
11066 /* Now get the name as a string, or invent one. */
11067 if (t != 0)
11068 name = IDENTIFIER_POINTER (t);
11071 return (name == 0 || *name == '\0') ? 0 : name;
11074 /* Return the type associated with a data member, make a special check
11075 for bit field types. */
11077 static inline tree
11078 member_declared_type (tree member)
11080 return (DECL_BIT_FIELD_TYPE (member)
11081 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
11084 /* Get the decl's label, as described by its RTL. This may be different
11085 from the DECL_NAME name used in the source file. */
11087 #if 0
11088 static const char *
11089 decl_start_label (tree decl)
11091 rtx x;
11092 const char *fnname;
11094 x = DECL_RTL (decl);
11095 gcc_assert (MEM_P (x));
11097 x = XEXP (x, 0);
11098 gcc_assert (GET_CODE (x) == SYMBOL_REF);
11100 fnname = XSTR (x, 0);
11101 return fnname;
11103 #endif
11105 /* These routines generate the internal representation of the DIE's for
11106 the compilation unit. Debugging information is collected by walking
11107 the declaration trees passed in from dwarf2out_decl(). */
11109 static void
11110 gen_array_type_die (tree type, dw_die_ref context_die)
11112 dw_die_ref scope_die = scope_die_for (type, context_die);
11113 dw_die_ref array_die;
11114 tree element_type;
11116 /* ??? The SGI dwarf reader fails for array of array of enum types unless
11117 the inner array type comes before the outer array type. Thus we must
11118 call gen_type_die before we call new_die. See below also. */
11119 #ifdef MIPS_DEBUGGING_INFO
11120 gen_type_die (TREE_TYPE (type), context_die);
11121 #endif
11123 array_die = new_die (DW_TAG_array_type, scope_die, type);
11124 add_name_attribute (array_die, type_tag (type));
11125 equate_type_number_to_die (type, array_die);
11127 if (TREE_CODE (type) == VECTOR_TYPE)
11129 /* The frontend feeds us a representation for the vector as a struct
11130 containing an array. Pull out the array type. */
11131 type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
11132 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
11135 #if 0
11136 /* We default the array ordering. SDB will probably do
11137 the right things even if DW_AT_ordering is not present. It's not even
11138 an issue until we start to get into multidimensional arrays anyway. If
11139 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
11140 then we'll have to put the DW_AT_ordering attribute back in. (But if
11141 and when we find out that we need to put these in, we will only do so
11142 for multidimensional arrays. */
11143 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
11144 #endif
11146 #ifdef MIPS_DEBUGGING_INFO
11147 /* The SGI compilers handle arrays of unknown bound by setting
11148 AT_declaration and not emitting any subrange DIEs. */
11149 if (! TYPE_DOMAIN (type))
11150 add_AT_flag (array_die, DW_AT_declaration, 1);
11151 else
11152 #endif
11153 add_subscript_info (array_die, type);
11155 /* Add representation of the type of the elements of this array type. */
11156 element_type = TREE_TYPE (type);
11158 /* ??? The SGI dwarf reader fails for multidimensional arrays with a
11159 const enum type. E.g. const enum machine_mode insn_operand_mode[2][10].
11160 We work around this by disabling this feature. See also
11161 add_subscript_info. */
11162 #ifndef MIPS_DEBUGGING_INFO
11163 while (TREE_CODE (element_type) == ARRAY_TYPE)
11164 element_type = TREE_TYPE (element_type);
11166 gen_type_die (element_type, context_die);
11167 #endif
11169 add_type_attribute (array_die, element_type, 0, 0, context_die);
11172 #if 0
11173 static void
11174 gen_entry_point_die (tree decl, dw_die_ref context_die)
11176 tree origin = decl_ultimate_origin (decl);
11177 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
11179 if (origin != NULL)
11180 add_abstract_origin_attribute (decl_die, origin);
11181 else
11183 add_name_and_src_coords_attributes (decl_die, decl);
11184 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
11185 0, 0, context_die);
11188 if (DECL_ABSTRACT (decl))
11189 equate_decl_number_to_die (decl, decl_die);
11190 else
11191 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
11193 #endif
11195 /* Walk through the list of incomplete types again, trying once more to
11196 emit full debugging info for them. */
11198 static void
11199 retry_incomplete_types (void)
11201 int i;
11203 for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
11204 gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
11207 /* Generate a DIE to represent an inlined instance of an enumeration type. */
11209 static void
11210 gen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
11212 dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
11214 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11215 be incomplete and such types are not marked. */
11216 add_abstract_origin_attribute (type_die, type);
11219 /* Generate a DIE to represent an inlined instance of a structure type. */
11221 static void
11222 gen_inlined_structure_type_die (tree type, dw_die_ref context_die)
11224 dw_die_ref type_die = new_die (DW_TAG_structure_type, context_die, type);
11226 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11227 be incomplete and such types are not marked. */
11228 add_abstract_origin_attribute (type_die, type);
11231 /* Generate a DIE to represent an inlined instance of a union type. */
11233 static void
11234 gen_inlined_union_type_die (tree type, dw_die_ref context_die)
11236 dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
11238 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11239 be incomplete and such types are not marked. */
11240 add_abstract_origin_attribute (type_die, type);
11243 /* Generate a DIE to represent an enumeration type. Note that these DIEs
11244 include all of the information about the enumeration values also. Each
11245 enumerated type name/value is listed as a child of the enumerated type
11246 DIE. */
11248 static dw_die_ref
11249 gen_enumeration_type_die (tree type, dw_die_ref context_die)
11251 dw_die_ref type_die = lookup_type_die (type);
11253 if (type_die == NULL)
11255 type_die = new_die (DW_TAG_enumeration_type,
11256 scope_die_for (type, context_die), type);
11257 equate_type_number_to_die (type, type_die);
11258 add_name_attribute (type_die, type_tag (type));
11260 else if (! TYPE_SIZE (type))
11261 return type_die;
11262 else
11263 remove_AT (type_die, DW_AT_declaration);
11265 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
11266 given enum type is incomplete, do not generate the DW_AT_byte_size
11267 attribute or the DW_AT_element_list attribute. */
11268 if (TYPE_SIZE (type))
11270 tree link;
11272 TREE_ASM_WRITTEN (type) = 1;
11273 add_byte_size_attribute (type_die, type);
11274 if (TYPE_STUB_DECL (type) != NULL_TREE)
11275 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
11277 /* If the first reference to this type was as the return type of an
11278 inline function, then it may not have a parent. Fix this now. */
11279 if (type_die->die_parent == NULL)
11280 add_child_die (scope_die_for (type, context_die), type_die);
11282 for (link = TYPE_VALUES (type);
11283 link != NULL; link = TREE_CHAIN (link))
11285 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
11286 tree value = TREE_VALUE (link);
11288 add_name_attribute (enum_die,
11289 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
11291 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
11292 /* DWARF2 does not provide a way of indicating whether or
11293 not enumeration constants are signed or unsigned. GDB
11294 always assumes the values are signed, so we output all
11295 values as if they were signed. That means that
11296 enumeration constants with very large unsigned values
11297 will appear to have negative values in the debugger. */
11298 add_AT_int (enum_die, DW_AT_const_value,
11299 tree_low_cst (value, tree_int_cst_sgn (value) > 0));
11302 else
11303 add_AT_flag (type_die, DW_AT_declaration, 1);
11305 return type_die;
11308 /* Generate a DIE to represent either a real live formal parameter decl or to
11309 represent just the type of some formal parameter position in some function
11310 type.
11312 Note that this routine is a bit unusual because its argument may be a
11313 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
11314 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
11315 node. If it's the former then this function is being called to output a
11316 DIE to represent a formal parameter object (or some inlining thereof). If
11317 it's the latter, then this function is only being called to output a
11318 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
11319 argument type of some subprogram type. */
11321 static dw_die_ref
11322 gen_formal_parameter_die (tree node, dw_die_ref context_die)
11324 dw_die_ref parm_die
11325 = new_die (DW_TAG_formal_parameter, context_die, node);
11326 tree origin;
11328 switch (TREE_CODE_CLASS (TREE_CODE (node)))
11330 case tcc_declaration:
11331 origin = decl_ultimate_origin (node);
11332 if (origin != NULL)
11333 add_abstract_origin_attribute (parm_die, origin);
11334 else
11336 add_name_and_src_coords_attributes (parm_die, node);
11337 add_type_attribute (parm_die, TREE_TYPE (node),
11338 TREE_READONLY (node),
11339 TREE_THIS_VOLATILE (node),
11340 context_die);
11341 if (DECL_ARTIFICIAL (node))
11342 add_AT_flag (parm_die, DW_AT_artificial, 1);
11345 equate_decl_number_to_die (node, parm_die);
11346 if (! DECL_ABSTRACT (node))
11347 add_location_or_const_value_attribute (parm_die, node, DW_AT_location);
11349 break;
11351 case tcc_type:
11352 /* We were called with some kind of a ..._TYPE node. */
11353 add_type_attribute (parm_die, node, 0, 0, context_die);
11354 break;
11356 default:
11357 gcc_unreachable ();
11360 return parm_die;
11363 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
11364 at the end of an (ANSI prototyped) formal parameters list. */
11366 static void
11367 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
11369 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
11372 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
11373 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
11374 parameters as specified in some function type specification (except for
11375 those which appear as part of a function *definition*). */
11377 static void
11378 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
11380 tree link;
11381 tree formal_type = NULL;
11382 tree first_parm_type;
11383 tree arg;
11385 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
11387 arg = DECL_ARGUMENTS (function_or_method_type);
11388 function_or_method_type = TREE_TYPE (function_or_method_type);
11390 else
11391 arg = NULL_TREE;
11393 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
11395 /* Make our first pass over the list of formal parameter types and output a
11396 DW_TAG_formal_parameter DIE for each one. */
11397 for (link = first_parm_type; link; )
11399 dw_die_ref parm_die;
11401 formal_type = TREE_VALUE (link);
11402 if (formal_type == void_type_node)
11403 break;
11405 /* Output a (nameless) DIE to represent the formal parameter itself. */
11406 parm_die = gen_formal_parameter_die (formal_type, context_die);
11407 if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
11408 && link == first_parm_type)
11409 || (arg && DECL_ARTIFICIAL (arg)))
11410 add_AT_flag (parm_die, DW_AT_artificial, 1);
11412 link = TREE_CHAIN (link);
11413 if (arg)
11414 arg = TREE_CHAIN (arg);
11417 /* If this function type has an ellipsis, add a
11418 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
11419 if (formal_type != void_type_node)
11420 gen_unspecified_parameters_die (function_or_method_type, context_die);
11422 /* Make our second (and final) pass over the list of formal parameter types
11423 and output DIEs to represent those types (as necessary). */
11424 for (link = TYPE_ARG_TYPES (function_or_method_type);
11425 link && TREE_VALUE (link);
11426 link = TREE_CHAIN (link))
11427 gen_type_die (TREE_VALUE (link), context_die);
11430 /* We want to generate the DIE for TYPE so that we can generate the
11431 die for MEMBER, which has been defined; we will need to refer back
11432 to the member declaration nested within TYPE. If we're trying to
11433 generate minimal debug info for TYPE, processing TYPE won't do the
11434 trick; we need to attach the member declaration by hand. */
11436 static void
11437 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
11439 gen_type_die (type, context_die);
11441 /* If we're trying to avoid duplicate debug info, we may not have
11442 emitted the member decl for this function. Emit it now. */
11443 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
11444 && ! lookup_decl_die (member))
11446 dw_die_ref type_die;
11447 gcc_assert (!decl_ultimate_origin (member));
11449 push_decl_scope (type);
11450 type_die = lookup_type_die (type);
11451 if (TREE_CODE (member) == FUNCTION_DECL)
11452 gen_subprogram_die (member, type_die);
11453 else if (TREE_CODE (member) == FIELD_DECL)
11455 /* Ignore the nameless fields that are used to skip bits but handle
11456 C++ anonymous unions and structs. */
11457 if (DECL_NAME (member) != NULL_TREE
11458 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
11459 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
11461 gen_type_die (member_declared_type (member), type_die);
11462 gen_field_die (member, type_die);
11465 else
11466 gen_variable_die (member, type_die);
11468 pop_decl_scope ();
11472 /* Generate the DWARF2 info for the "abstract" instance of a function which we
11473 may later generate inlined and/or out-of-line instances of. */
11475 static void
11476 dwarf2out_abstract_function (tree decl)
11478 dw_die_ref old_die;
11479 tree save_fn;
11480 tree context;
11481 int was_abstract = DECL_ABSTRACT (decl);
11483 /* Make sure we have the actual abstract inline, not a clone. */
11484 decl = DECL_ORIGIN (decl);
11486 old_die = lookup_decl_die (decl);
11487 if (old_die && get_AT (old_die, DW_AT_inline))
11488 /* We've already generated the abstract instance. */
11489 return;
11491 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
11492 we don't get confused by DECL_ABSTRACT. */
11493 if (debug_info_level > DINFO_LEVEL_TERSE)
11495 context = decl_class_context (decl);
11496 if (context)
11497 gen_type_die_for_member
11498 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
11501 /* Pretend we've just finished compiling this function. */
11502 save_fn = current_function_decl;
11503 current_function_decl = decl;
11505 set_decl_abstract_flags (decl, 1);
11506 dwarf2out_decl (decl);
11507 if (! was_abstract)
11508 set_decl_abstract_flags (decl, 0);
11510 current_function_decl = save_fn;
11513 /* Generate a DIE to represent a declared function (either file-scope or
11514 block-local). */
11516 static void
11517 gen_subprogram_die (tree decl, dw_die_ref context_die)
11519 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
11520 tree origin = decl_ultimate_origin (decl);
11521 dw_die_ref subr_die;
11522 tree fn_arg_types;
11523 tree outer_scope;
11524 dw_die_ref old_die = lookup_decl_die (decl);
11525 int declaration = (current_function_decl != decl
11526 || class_or_namespace_scope_p (context_die));
11528 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
11529 started to generate the abstract instance of an inline, decided to output
11530 its containing class, and proceeded to emit the declaration of the inline
11531 from the member list for the class. If so, DECLARATION takes priority;
11532 we'll get back to the abstract instance when done with the class. */
11534 /* The class-scope declaration DIE must be the primary DIE. */
11535 if (origin && declaration && class_or_namespace_scope_p (context_die))
11537 origin = NULL;
11538 gcc_assert (!old_die);
11541 /* Now that the C++ front end lazily declares artificial member fns, we
11542 might need to retrofit the declaration into its class. */
11543 if (!declaration && !origin && !old_die
11544 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
11545 && !class_or_namespace_scope_p (context_die)
11546 && debug_info_level > DINFO_LEVEL_TERSE)
11547 old_die = force_decl_die (decl);
11549 if (origin != NULL)
11551 gcc_assert (!declaration || local_scope_p (context_die));
11553 /* Fixup die_parent for the abstract instance of a nested
11554 inline function. */
11555 if (old_die && old_die->die_parent == NULL)
11556 add_child_die (context_die, old_die);
11558 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11559 add_abstract_origin_attribute (subr_die, origin);
11561 else if (old_die)
11563 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11564 unsigned file_index = lookup_filename (s.file);
11566 if (!get_AT_flag (old_die, DW_AT_declaration)
11567 /* We can have a normal definition following an inline one in the
11568 case of redefinition of GNU C extern inlines.
11569 It seems reasonable to use AT_specification in this case. */
11570 && !get_AT (old_die, DW_AT_inline))
11572 /* Detect and ignore this case, where we are trying to output
11573 something we have already output. */
11574 return;
11577 /* If the definition comes from the same place as the declaration,
11578 maybe use the old DIE. We always want the DIE for this function
11579 that has the *_pc attributes to be under comp_unit_die so the
11580 debugger can find it. We also need to do this for abstract
11581 instances of inlines, since the spec requires the out-of-line copy
11582 to have the same parent. For local class methods, this doesn't
11583 apply; we just use the old DIE. */
11584 if ((old_die->die_parent == comp_unit_die || context_die == NULL)
11585 && (DECL_ARTIFICIAL (decl)
11586 || (get_AT_unsigned (old_die, DW_AT_decl_file) == file_index
11587 && (get_AT_unsigned (old_die, DW_AT_decl_line)
11588 == (unsigned) s.line))))
11590 subr_die = old_die;
11592 /* Clear out the declaration attribute and the formal parameters.
11593 Do not remove all children, because it is possible that this
11594 declaration die was forced using force_decl_die(). In such
11595 cases die that forced declaration die (e.g. TAG_imported_module)
11596 is one of the children that we do not want to remove. */
11597 remove_AT (subr_die, DW_AT_declaration);
11598 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
11600 else
11602 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11603 add_AT_specification (subr_die, old_die);
11604 if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
11605 add_AT_unsigned (subr_die, DW_AT_decl_file, file_index);
11606 if (get_AT_unsigned (old_die, DW_AT_decl_line)
11607 != (unsigned) s.line)
11608 add_AT_unsigned
11609 (subr_die, DW_AT_decl_line, s.line);
11612 else
11614 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11616 if (TREE_PUBLIC (decl))
11617 add_AT_flag (subr_die, DW_AT_external, 1);
11619 add_name_and_src_coords_attributes (subr_die, decl);
11620 if (debug_info_level > DINFO_LEVEL_TERSE)
11622 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
11623 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
11624 0, 0, context_die);
11627 add_pure_or_virtual_attribute (subr_die, decl);
11628 if (DECL_ARTIFICIAL (decl))
11629 add_AT_flag (subr_die, DW_AT_artificial, 1);
11631 if (TREE_PROTECTED (decl))
11632 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
11633 else if (TREE_PRIVATE (decl))
11634 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
11637 if (declaration)
11639 if (!old_die || !get_AT (old_die, DW_AT_inline))
11641 add_AT_flag (subr_die, DW_AT_declaration, 1);
11643 /* The first time we see a member function, it is in the context of
11644 the class to which it belongs. We make sure of this by emitting
11645 the class first. The next time is the definition, which is
11646 handled above. The two may come from the same source text.
11648 Note that force_decl_die() forces function declaration die. It is
11649 later reused to represent definition. */
11650 equate_decl_number_to_die (decl, subr_die);
11653 else if (DECL_ABSTRACT (decl))
11655 if (DECL_DECLARED_INLINE_P (decl))
11657 if (cgraph_function_possibly_inlined_p (decl))
11658 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
11659 else
11660 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
11662 else
11664 if (cgraph_function_possibly_inlined_p (decl))
11665 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
11666 else
11667 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
11670 equate_decl_number_to_die (decl, subr_die);
11672 else if (!DECL_EXTERNAL (decl))
11674 if (!old_die || !get_AT (old_die, DW_AT_inline))
11675 equate_decl_number_to_die (decl, subr_die);
11677 if (!flag_reorder_blocks_and_partition)
11679 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
11680 current_function_funcdef_no);
11681 add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
11682 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
11683 current_function_funcdef_no);
11684 add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
11686 add_pubname (decl, subr_die);
11687 add_arange (decl, subr_die);
11689 else
11690 { /* Do nothing for now; maybe need to duplicate die, one for
11691 hot section and ond for cold section, then use the hot/cold
11692 section begin/end labels to generate the aranges... */
11694 add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
11695 add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
11696 add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
11697 add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
11699 add_pubname (decl, subr_die);
11700 add_arange (decl, subr_die);
11701 add_arange (decl, subr_die);
11705 #ifdef MIPS_DEBUGGING_INFO
11706 /* Add a reference to the FDE for this routine. */
11707 add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
11708 #endif
11710 /* We define the "frame base" as the function's CFA. This is more
11711 convenient for several reasons: (1) It's stable across the prologue
11712 and epilogue, which makes it better than just a frame pointer,
11713 (2) With dwarf3, there exists a one-byte encoding that allows us
11714 to reference the .debug_frame data by proxy, but failing that,
11715 (3) We can at least reuse the code inspection and interpretation
11716 code that determines the CFA position at various points in the
11717 function. */
11718 /* ??? Use some command-line or configury switch to enable the use
11719 of dwarf3 DW_OP_call_frame_cfa. At present there are no dwarf
11720 consumers that understand it; fall back to "pure" dwarf2 and
11721 convert the CFA data into a location list. */
11723 dw_loc_list_ref list = convert_cfa_to_loc_list ();
11724 if (list->dw_loc_next)
11725 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
11726 else
11727 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
11730 /* Compute a displacement from the "steady-state frame pointer" to
11731 the CFA. The former is what all stack slots and argument slots
11732 will reference in the rtl; the later is what we've told the
11733 debugger about. We'll need to adjust all frame_base references
11734 by this displacement. */
11735 compute_frame_pointer_to_cfa_displacement ();
11737 if (cfun->static_chain_decl)
11738 add_AT_location_description (subr_die, DW_AT_static_link,
11739 loc_descriptor_from_tree (cfun->static_chain_decl));
11742 /* Now output descriptions of the arguments for this function. This gets
11743 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
11744 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
11745 `...' at the end of the formal parameter list. In order to find out if
11746 there was a trailing ellipsis or not, we must instead look at the type
11747 associated with the FUNCTION_DECL. This will be a node of type
11748 FUNCTION_TYPE. If the chain of type nodes hanging off of this
11749 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
11750 an ellipsis at the end. */
11752 /* In the case where we are describing a mere function declaration, all we
11753 need to do here (and all we *can* do here) is to describe the *types* of
11754 its formal parameters. */
11755 if (debug_info_level <= DINFO_LEVEL_TERSE)
11757 else if (declaration)
11758 gen_formal_types_die (decl, subr_die);
11759 else
11761 /* Generate DIEs to represent all known formal parameters. */
11762 tree arg_decls = DECL_ARGUMENTS (decl);
11763 tree parm;
11765 /* When generating DIEs, generate the unspecified_parameters DIE
11766 instead if we come across the arg "__builtin_va_alist" */
11767 for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
11768 if (TREE_CODE (parm) == PARM_DECL)
11770 if (DECL_NAME (parm)
11771 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
11772 "__builtin_va_alist"))
11773 gen_unspecified_parameters_die (parm, subr_die);
11774 else
11775 gen_decl_die (parm, subr_die);
11778 /* Decide whether we need an unspecified_parameters DIE at the end.
11779 There are 2 more cases to do this for: 1) the ansi ... declaration -
11780 this is detectable when the end of the arg list is not a
11781 void_type_node 2) an unprototyped function declaration (not a
11782 definition). This just means that we have no info about the
11783 parameters at all. */
11784 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
11785 if (fn_arg_types != NULL)
11787 /* This is the prototyped case, check for.... */
11788 if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
11789 gen_unspecified_parameters_die (decl, subr_die);
11791 else if (DECL_INITIAL (decl) == NULL_TREE)
11792 gen_unspecified_parameters_die (decl, subr_die);
11795 /* Output Dwarf info for all of the stuff within the body of the function
11796 (if it has one - it may be just a declaration). */
11797 outer_scope = DECL_INITIAL (decl);
11799 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
11800 a function. This BLOCK actually represents the outermost binding contour
11801 for the function, i.e. the contour in which the function's formal
11802 parameters and labels get declared. Curiously, it appears that the front
11803 end doesn't actually put the PARM_DECL nodes for the current function onto
11804 the BLOCK_VARS list for this outer scope, but are strung off of the
11805 DECL_ARGUMENTS list for the function instead.
11807 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
11808 the LABEL_DECL nodes for the function however, and we output DWARF info
11809 for those in decls_for_scope. Just within the `outer_scope' there will be
11810 a BLOCK node representing the function's outermost pair of curly braces,
11811 and any blocks used for the base and member initializers of a C++
11812 constructor function. */
11813 if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
11815 /* Emit a DW_TAG_variable DIE for a named return value. */
11816 if (DECL_NAME (DECL_RESULT (decl)))
11817 gen_decl_die (DECL_RESULT (decl), subr_die);
11819 current_function_has_inlines = 0;
11820 decls_for_scope (outer_scope, subr_die, 0);
11822 #if 0 && defined (MIPS_DEBUGGING_INFO)
11823 if (current_function_has_inlines)
11825 add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
11826 if (! comp_unit_has_inlines)
11828 add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
11829 comp_unit_has_inlines = 1;
11832 #endif
11834 /* Add the calling convention attribute if requested. */
11835 add_calling_convention_attribute (subr_die, TREE_TYPE (decl));
11839 /* Generate a DIE to represent a declared data object. */
11841 static void
11842 gen_variable_die (tree decl, dw_die_ref context_die)
11844 tree origin = decl_ultimate_origin (decl);
11845 dw_die_ref var_die = new_die (DW_TAG_variable, context_die, decl);
11847 dw_die_ref old_die = lookup_decl_die (decl);
11848 int declaration = (DECL_EXTERNAL (decl)
11849 /* If DECL is COMDAT and has not actually been
11850 emitted, we cannot take its address; there
11851 might end up being no definition anywhere in
11852 the program. For example, consider the C++
11853 test case:
11855 template <class T>
11856 struct S { static const int i = 7; };
11858 template <class T>
11859 const int S<T>::i;
11861 int f() { return S<int>::i; }
11863 Here, S<int>::i is not DECL_EXTERNAL, but no
11864 definition is required, so the compiler will
11865 not emit a definition. */
11866 || (TREE_CODE (decl) == VAR_DECL
11867 && DECL_COMDAT (decl) && !TREE_ASM_WRITTEN (decl))
11868 || class_or_namespace_scope_p (context_die));
11870 if (origin != NULL)
11871 add_abstract_origin_attribute (var_die, origin);
11873 /* Loop unrolling can create multiple blocks that refer to the same
11874 static variable, so we must test for the DW_AT_declaration flag.
11876 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
11877 copy decls and set the DECL_ABSTRACT flag on them instead of
11878 sharing them.
11880 ??? Duplicated blocks have been rewritten to use .debug_ranges.
11882 ??? The declare_in_namespace support causes us to get two DIEs for one
11883 variable, both of which are declarations. We want to avoid considering
11884 one to be a specification, so we must test that this DIE is not a
11885 declaration. */
11886 else if (old_die && TREE_STATIC (decl) && ! declaration
11887 && get_AT_flag (old_die, DW_AT_declaration) == 1)
11889 /* This is a definition of a C++ class level static. */
11890 add_AT_specification (var_die, old_die);
11891 if (DECL_NAME (decl))
11893 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11894 unsigned file_index = lookup_filename (s.file);
11896 if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
11897 add_AT_unsigned (var_die, DW_AT_decl_file, file_index);
11899 if (get_AT_unsigned (old_die, DW_AT_decl_line)
11900 != (unsigned) s.line)
11902 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
11905 else
11907 add_name_and_src_coords_attributes (var_die, decl);
11908 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
11909 TREE_THIS_VOLATILE (decl), context_die);
11911 if (TREE_PUBLIC (decl))
11912 add_AT_flag (var_die, DW_AT_external, 1);
11914 if (DECL_ARTIFICIAL (decl))
11915 add_AT_flag (var_die, DW_AT_artificial, 1);
11917 if (TREE_PROTECTED (decl))
11918 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
11919 else if (TREE_PRIVATE (decl))
11920 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
11923 if (declaration)
11924 add_AT_flag (var_die, DW_AT_declaration, 1);
11926 if (DECL_ABSTRACT (decl) || declaration)
11927 equate_decl_number_to_die (decl, var_die);
11929 if (! declaration && ! DECL_ABSTRACT (decl))
11931 add_location_or_const_value_attribute (var_die, decl, DW_AT_location);
11932 add_pubname (decl, var_die);
11934 else
11935 tree_add_const_value_attribute (var_die, decl);
11938 /* Generate a DIE to represent a label identifier. */
11940 static void
11941 gen_label_die (tree decl, dw_die_ref context_die)
11943 tree origin = decl_ultimate_origin (decl);
11944 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
11945 rtx insn;
11946 char label[MAX_ARTIFICIAL_LABEL_BYTES];
11948 if (origin != NULL)
11949 add_abstract_origin_attribute (lbl_die, origin);
11950 else
11951 add_name_and_src_coords_attributes (lbl_die, decl);
11953 if (DECL_ABSTRACT (decl))
11954 equate_decl_number_to_die (decl, lbl_die);
11955 else
11957 insn = DECL_RTL_IF_SET (decl);
11959 /* Deleted labels are programmer specified labels which have been
11960 eliminated because of various optimizations. We still emit them
11961 here so that it is possible to put breakpoints on them. */
11962 if (insn
11963 && (LABEL_P (insn)
11964 || ((NOTE_P (insn)
11965 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED_LABEL))))
11967 /* When optimization is enabled (via -O) some parts of the compiler
11968 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
11969 represent source-level labels which were explicitly declared by
11970 the user. This really shouldn't be happening though, so catch
11971 it if it ever does happen. */
11972 gcc_assert (!INSN_DELETED_P (insn));
11974 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
11975 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
11980 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
11981 attributes to the DIE for a block STMT, to describe where the inlined
11982 function was called from. This is similar to add_src_coords_attributes. */
11984 static inline void
11985 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
11987 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
11988 unsigned file_index = lookup_filename (s.file);
11990 add_AT_unsigned (die, DW_AT_call_file, file_index);
11991 add_AT_unsigned (die, DW_AT_call_line, s.line);
11994 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
11995 Add low_pc and high_pc attributes to the DIE for a block STMT. */
11997 static inline void
11998 add_high_low_attributes (tree stmt, dw_die_ref die)
12000 char label[MAX_ARTIFICIAL_LABEL_BYTES];
12002 if (BLOCK_FRAGMENT_CHAIN (stmt))
12004 tree chain;
12006 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
12008 chain = BLOCK_FRAGMENT_CHAIN (stmt);
12011 add_ranges (chain);
12012 chain = BLOCK_FRAGMENT_CHAIN (chain);
12014 while (chain);
12015 add_ranges (NULL);
12017 else
12019 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
12020 BLOCK_NUMBER (stmt));
12021 add_AT_lbl_id (die, DW_AT_low_pc, label);
12022 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
12023 BLOCK_NUMBER (stmt));
12024 add_AT_lbl_id (die, DW_AT_high_pc, label);
12028 /* Generate a DIE for a lexical block. */
12030 static void
12031 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
12033 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
12035 if (! BLOCK_ABSTRACT (stmt))
12036 add_high_low_attributes (stmt, stmt_die);
12038 decls_for_scope (stmt, stmt_die, depth);
12041 /* Generate a DIE for an inlined subprogram. */
12043 static void
12044 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
12046 tree decl = block_ultimate_origin (stmt);
12048 /* Emit info for the abstract instance first, if we haven't yet. We
12049 must emit this even if the block is abstract, otherwise when we
12050 emit the block below (or elsewhere), we may end up trying to emit
12051 a die whose origin die hasn't been emitted, and crashing. */
12052 dwarf2out_abstract_function (decl);
12054 if (! BLOCK_ABSTRACT (stmt))
12056 dw_die_ref subr_die
12057 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
12059 add_abstract_origin_attribute (subr_die, decl);
12060 add_high_low_attributes (stmt, subr_die);
12061 add_call_src_coords_attributes (stmt, subr_die);
12063 decls_for_scope (stmt, subr_die, depth);
12064 current_function_has_inlines = 1;
12066 else
12067 /* We may get here if we're the outer block of function A that was
12068 inlined into function B that was inlined into function C. When
12069 generating debugging info for C, dwarf2out_abstract_function(B)
12070 would mark all inlined blocks as abstract, including this one.
12071 So, we wouldn't (and shouldn't) expect labels to be generated
12072 for this one. Instead, just emit debugging info for
12073 declarations within the block. This is particularly important
12074 in the case of initializers of arguments passed from B to us:
12075 if they're statement expressions containing declarations, we
12076 wouldn't generate dies for their abstract variables, and then,
12077 when generating dies for the real variables, we'd die (pun
12078 intended :-) */
12079 gen_lexical_block_die (stmt, context_die, depth);
12082 /* Generate a DIE for a field in a record, or structure. */
12084 static void
12085 gen_field_die (tree decl, dw_die_ref context_die)
12087 dw_die_ref decl_die;
12089 if (TREE_TYPE (decl) == error_mark_node)
12090 return;
12092 decl_die = new_die (DW_TAG_member, context_die, decl);
12093 add_name_and_src_coords_attributes (decl_die, decl);
12094 add_type_attribute (decl_die, member_declared_type (decl),
12095 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
12096 context_die);
12098 if (DECL_BIT_FIELD_TYPE (decl))
12100 add_byte_size_attribute (decl_die, decl);
12101 add_bit_size_attribute (decl_die, decl);
12102 add_bit_offset_attribute (decl_die, decl);
12105 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
12106 add_data_member_location_attribute (decl_die, decl);
12108 if (DECL_ARTIFICIAL (decl))
12109 add_AT_flag (decl_die, DW_AT_artificial, 1);
12111 if (TREE_PROTECTED (decl))
12112 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
12113 else if (TREE_PRIVATE (decl))
12114 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
12116 /* Equate decl number to die, so that we can look up this decl later on. */
12117 equate_decl_number_to_die (decl, decl_die);
12120 #if 0
12121 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
12122 Use modified_type_die instead.
12123 We keep this code here just in case these types of DIEs may be needed to
12124 represent certain things in other languages (e.g. Pascal) someday. */
12126 static void
12127 gen_pointer_type_die (tree type, dw_die_ref context_die)
12129 dw_die_ref ptr_die
12130 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
12132 equate_type_number_to_die (type, ptr_die);
12133 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
12134 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
12137 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
12138 Use modified_type_die instead.
12139 We keep this code here just in case these types of DIEs may be needed to
12140 represent certain things in other languages (e.g. Pascal) someday. */
12142 static void
12143 gen_reference_type_die (tree type, dw_die_ref context_die)
12145 dw_die_ref ref_die
12146 = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
12148 equate_type_number_to_die (type, ref_die);
12149 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
12150 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
12152 #endif
12154 /* Generate a DIE for a pointer to a member type. */
12156 static void
12157 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
12159 dw_die_ref ptr_die
12160 = new_die (DW_TAG_ptr_to_member_type,
12161 scope_die_for (type, context_die), type);
12163 equate_type_number_to_die (type, ptr_die);
12164 add_AT_die_ref (ptr_die, DW_AT_containing_type,
12165 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
12166 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
12169 /* Generate the DIE for the compilation unit. */
12171 static dw_die_ref
12172 gen_compile_unit_die (const char *filename)
12174 dw_die_ref die;
12175 char producer[250];
12176 const char *language_string = lang_hooks.name;
12177 int language;
12179 die = new_die (DW_TAG_compile_unit, NULL, NULL);
12181 if (filename)
12183 add_name_attribute (die, filename);
12184 /* Don't add cwd for <built-in>. */
12185 if (filename[0] != DIR_SEPARATOR && filename[0] != '<')
12186 add_comp_dir_attribute (die);
12189 sprintf (producer, "%s %s", language_string, version_string);
12191 #ifdef MIPS_DEBUGGING_INFO
12192 /* The MIPS/SGI compilers place the 'cc' command line options in the producer
12193 string. The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
12194 not appear in the producer string, the debugger reaches the conclusion
12195 that the object file is stripped and has no debugging information.
12196 To get the MIPS/SGI debugger to believe that there is debugging
12197 information in the object file, we add a -g to the producer string. */
12198 if (debug_info_level > DINFO_LEVEL_TERSE)
12199 strcat (producer, " -g");
12200 #endif
12202 add_AT_string (die, DW_AT_producer, producer);
12204 if (strcmp (language_string, "GNU C++") == 0)
12205 language = DW_LANG_C_plus_plus;
12206 else if (strcmp (language_string, "GNU Ada") == 0)
12207 language = DW_LANG_Ada95;
12208 else if (strcmp (language_string, "GNU F77") == 0)
12209 language = DW_LANG_Fortran77;
12210 else if (strcmp (language_string, "GNU F95") == 0)
12211 language = DW_LANG_Fortran95;
12212 else if (strcmp (language_string, "GNU Pascal") == 0)
12213 language = DW_LANG_Pascal83;
12214 else if (strcmp (language_string, "GNU Java") == 0)
12215 language = DW_LANG_Java;
12216 else
12217 language = DW_LANG_C89;
12219 add_AT_unsigned (die, DW_AT_language, language);
12220 return die;
12223 /* Generate a DIE for a string type. */
12225 static void
12226 gen_string_type_die (tree type, dw_die_ref context_die)
12228 dw_die_ref type_die
12229 = new_die (DW_TAG_string_type, scope_die_for (type, context_die), type);
12231 equate_type_number_to_die (type, type_die);
12233 /* ??? Fudge the string length attribute for now.
12234 TODO: add string length info. */
12235 #if 0
12236 string_length_attribute (TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
12237 bound_representation (upper_bound, 0, 'u');
12238 #endif
12241 /* Generate the DIE for a base class. */
12243 static void
12244 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
12246 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
12248 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
12249 add_data_member_location_attribute (die, binfo);
12251 if (BINFO_VIRTUAL_P (binfo))
12252 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
12254 if (access == access_public_node)
12255 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
12256 else if (access == access_protected_node)
12257 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
12260 /* Generate a DIE for a class member. */
12262 static void
12263 gen_member_die (tree type, dw_die_ref context_die)
12265 tree member;
12266 tree binfo = TYPE_BINFO (type);
12267 dw_die_ref child;
12269 /* If this is not an incomplete type, output descriptions of each of its
12270 members. Note that as we output the DIEs necessary to represent the
12271 members of this record or union type, we will also be trying to output
12272 DIEs to represent the *types* of those members. However the `type'
12273 function (above) will specifically avoid generating type DIEs for member
12274 types *within* the list of member DIEs for this (containing) type except
12275 for those types (of members) which are explicitly marked as also being
12276 members of this (containing) type themselves. The g++ front- end can
12277 force any given type to be treated as a member of some other (containing)
12278 type by setting the TYPE_CONTEXT of the given (member) type to point to
12279 the TREE node representing the appropriate (containing) type. */
12281 /* First output info about the base classes. */
12282 if (binfo)
12284 VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
12285 int i;
12286 tree base;
12288 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
12289 gen_inheritance_die (base,
12290 (accesses ? VEC_index (tree, accesses, i)
12291 : access_public_node), context_die);
12294 /* Now output info about the data members and type members. */
12295 for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
12297 /* If we thought we were generating minimal debug info for TYPE
12298 and then changed our minds, some of the member declarations
12299 may have already been defined. Don't define them again, but
12300 do put them in the right order. */
12302 child = lookup_decl_die (member);
12303 if (child)
12304 splice_child_die (context_die, child);
12305 else
12306 gen_decl_die (member, context_die);
12309 /* Now output info about the function members (if any). */
12310 for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
12312 /* Don't include clones in the member list. */
12313 if (DECL_ABSTRACT_ORIGIN (member))
12314 continue;
12316 child = lookup_decl_die (member);
12317 if (child)
12318 splice_child_die (context_die, child);
12319 else
12320 gen_decl_die (member, context_die);
12324 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
12325 is set, we pretend that the type was never defined, so we only get the
12326 member DIEs needed by later specification DIEs. */
12328 static void
12329 gen_struct_or_union_type_die (tree type, dw_die_ref context_die)
12331 dw_die_ref type_die = lookup_type_die (type);
12332 dw_die_ref scope_die = 0;
12333 int nested = 0;
12334 int complete = (TYPE_SIZE (type)
12335 && (! TYPE_STUB_DECL (type)
12336 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
12337 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
12339 if (type_die && ! complete)
12340 return;
12342 if (TYPE_CONTEXT (type) != NULL_TREE
12343 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
12344 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
12345 nested = 1;
12347 scope_die = scope_die_for (type, context_die);
12349 if (! type_die || (nested && scope_die == comp_unit_die))
12350 /* First occurrence of type or toplevel definition of nested class. */
12352 dw_die_ref old_die = type_die;
12354 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
12355 ? DW_TAG_structure_type : DW_TAG_union_type,
12356 scope_die, type);
12357 equate_type_number_to_die (type, type_die);
12358 if (old_die)
12359 add_AT_specification (type_die, old_die);
12360 else
12361 add_name_attribute (type_die, type_tag (type));
12363 else
12364 remove_AT (type_die, DW_AT_declaration);
12366 /* If this type has been completed, then give it a byte_size attribute and
12367 then give a list of members. */
12368 if (complete && !ns_decl)
12370 /* Prevent infinite recursion in cases where the type of some member of
12371 this type is expressed in terms of this type itself. */
12372 TREE_ASM_WRITTEN (type) = 1;
12373 add_byte_size_attribute (type_die, type);
12374 if (TYPE_STUB_DECL (type) != NULL_TREE)
12375 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
12377 /* If the first reference to this type was as the return type of an
12378 inline function, then it may not have a parent. Fix this now. */
12379 if (type_die->die_parent == NULL)
12380 add_child_die (scope_die, type_die);
12382 push_decl_scope (type);
12383 gen_member_die (type, type_die);
12384 pop_decl_scope ();
12386 /* GNU extension: Record what type our vtable lives in. */
12387 if (TYPE_VFIELD (type))
12389 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
12391 gen_type_die (vtype, context_die);
12392 add_AT_die_ref (type_die, DW_AT_containing_type,
12393 lookup_type_die (vtype));
12396 else
12398 add_AT_flag (type_die, DW_AT_declaration, 1);
12400 /* We don't need to do this for function-local types. */
12401 if (TYPE_STUB_DECL (type)
12402 && ! decl_function_context (TYPE_STUB_DECL (type)))
12403 VEC_safe_push (tree, gc, incomplete_types, type);
12407 /* Generate a DIE for a subroutine _type_. */
12409 static void
12410 gen_subroutine_type_die (tree type, dw_die_ref context_die)
12412 tree return_type = TREE_TYPE (type);
12413 dw_die_ref subr_die
12414 = new_die (DW_TAG_subroutine_type,
12415 scope_die_for (type, context_die), type);
12417 equate_type_number_to_die (type, subr_die);
12418 add_prototyped_attribute (subr_die, type);
12419 add_type_attribute (subr_die, return_type, 0, 0, context_die);
12420 gen_formal_types_die (type, subr_die);
12423 /* Generate a DIE for a type definition. */
12425 static void
12426 gen_typedef_die (tree decl, dw_die_ref context_die)
12428 dw_die_ref type_die;
12429 tree origin;
12431 if (TREE_ASM_WRITTEN (decl))
12432 return;
12434 TREE_ASM_WRITTEN (decl) = 1;
12435 type_die = new_die (DW_TAG_typedef, context_die, decl);
12436 origin = decl_ultimate_origin (decl);
12437 if (origin != NULL)
12438 add_abstract_origin_attribute (type_die, origin);
12439 else
12441 tree type;
12443 add_name_and_src_coords_attributes (type_die, decl);
12444 if (DECL_ORIGINAL_TYPE (decl))
12446 type = DECL_ORIGINAL_TYPE (decl);
12448 gcc_assert (type != TREE_TYPE (decl));
12449 equate_type_number_to_die (TREE_TYPE (decl), type_die);
12451 else
12452 type = TREE_TYPE (decl);
12454 add_type_attribute (type_die, type, TREE_READONLY (decl),
12455 TREE_THIS_VOLATILE (decl), context_die);
12458 if (DECL_ABSTRACT (decl))
12459 equate_decl_number_to_die (decl, type_die);
12462 /* Generate a type description DIE. */
12464 static void
12465 gen_type_die (tree type, dw_die_ref context_die)
12467 int need_pop;
12469 if (type == NULL_TREE || type == error_mark_node)
12470 return;
12472 if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12473 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
12475 if (TREE_ASM_WRITTEN (type))
12476 return;
12478 /* Prevent broken recursion; we can't hand off to the same type. */
12479 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
12481 TREE_ASM_WRITTEN (type) = 1;
12482 gen_decl_die (TYPE_NAME (type), context_die);
12483 return;
12486 /* We are going to output a DIE to represent the unqualified version
12487 of this type (i.e. without any const or volatile qualifiers) so
12488 get the main variant (i.e. the unqualified version) of this type
12489 now. (Vectors are special because the debugging info is in the
12490 cloned type itself). */
12491 if (TREE_CODE (type) != VECTOR_TYPE)
12492 type = type_main_variant (type);
12494 if (TREE_ASM_WRITTEN (type))
12495 return;
12497 switch (TREE_CODE (type))
12499 case ERROR_MARK:
12500 break;
12502 case POINTER_TYPE:
12503 case REFERENCE_TYPE:
12504 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
12505 ensures that the gen_type_die recursion will terminate even if the
12506 type is recursive. Recursive types are possible in Ada. */
12507 /* ??? We could perhaps do this for all types before the switch
12508 statement. */
12509 TREE_ASM_WRITTEN (type) = 1;
12511 /* For these types, all that is required is that we output a DIE (or a
12512 set of DIEs) to represent the "basis" type. */
12513 gen_type_die (TREE_TYPE (type), context_die);
12514 break;
12516 case OFFSET_TYPE:
12517 /* This code is used for C++ pointer-to-data-member types.
12518 Output a description of the relevant class type. */
12519 gen_type_die (TYPE_OFFSET_BASETYPE (type), context_die);
12521 /* Output a description of the type of the object pointed to. */
12522 gen_type_die (TREE_TYPE (type), context_die);
12524 /* Now output a DIE to represent this pointer-to-data-member type
12525 itself. */
12526 gen_ptr_to_mbr_type_die (type, context_die);
12527 break;
12529 case FUNCTION_TYPE:
12530 /* Force out return type (in case it wasn't forced out already). */
12531 gen_type_die (TREE_TYPE (type), context_die);
12532 gen_subroutine_type_die (type, context_die);
12533 break;
12535 case METHOD_TYPE:
12536 /* Force out return type (in case it wasn't forced out already). */
12537 gen_type_die (TREE_TYPE (type), context_die);
12538 gen_subroutine_type_die (type, context_die);
12539 break;
12541 case ARRAY_TYPE:
12542 if (TYPE_STRING_FLAG (type) && TREE_CODE (TREE_TYPE (type)) == CHAR_TYPE)
12544 gen_type_die (TREE_TYPE (type), context_die);
12545 gen_string_type_die (type, context_die);
12547 else
12548 gen_array_type_die (type, context_die);
12549 break;
12551 case VECTOR_TYPE:
12552 gen_array_type_die (type, context_die);
12553 break;
12555 case ENUMERAL_TYPE:
12556 case RECORD_TYPE:
12557 case UNION_TYPE:
12558 case QUAL_UNION_TYPE:
12559 /* If this is a nested type whose containing class hasn't been written
12560 out yet, writing it out will cover this one, too. This does not apply
12561 to instantiations of member class templates; they need to be added to
12562 the containing class as they are generated. FIXME: This hurts the
12563 idea of combining type decls from multiple TUs, since we can't predict
12564 what set of template instantiations we'll get. */
12565 if (TYPE_CONTEXT (type)
12566 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
12567 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
12569 gen_type_die (TYPE_CONTEXT (type), context_die);
12571 if (TREE_ASM_WRITTEN (type))
12572 return;
12574 /* If that failed, attach ourselves to the stub. */
12575 push_decl_scope (TYPE_CONTEXT (type));
12576 context_die = lookup_type_die (TYPE_CONTEXT (type));
12577 need_pop = 1;
12579 else
12581 declare_in_namespace (type, context_die);
12582 need_pop = 0;
12585 if (TREE_CODE (type) == ENUMERAL_TYPE)
12587 /* This might have been written out by the call to
12588 declare_in_namespace. */
12589 if (!TREE_ASM_WRITTEN (type))
12590 gen_enumeration_type_die (type, context_die);
12592 else
12593 gen_struct_or_union_type_die (type, context_die);
12595 if (need_pop)
12596 pop_decl_scope ();
12598 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
12599 it up if it is ever completed. gen_*_type_die will set it for us
12600 when appropriate. */
12601 return;
12603 case VOID_TYPE:
12604 case INTEGER_TYPE:
12605 case REAL_TYPE:
12606 case COMPLEX_TYPE:
12607 case BOOLEAN_TYPE:
12608 case CHAR_TYPE:
12609 /* No DIEs needed for fundamental types. */
12610 break;
12612 case LANG_TYPE:
12613 /* No Dwarf representation currently defined. */
12614 break;
12616 default:
12617 gcc_unreachable ();
12620 TREE_ASM_WRITTEN (type) = 1;
12623 /* Generate a DIE for a tagged type instantiation. */
12625 static void
12626 gen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
12628 if (type == NULL_TREE || type == error_mark_node)
12629 return;
12631 /* We are going to output a DIE to represent the unqualified version of
12632 this type (i.e. without any const or volatile qualifiers) so make sure
12633 that we have the main variant (i.e. the unqualified version) of this
12634 type now. */
12635 gcc_assert (type == type_main_variant (type));
12637 /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
12638 an instance of an unresolved type. */
12640 switch (TREE_CODE (type))
12642 case ERROR_MARK:
12643 break;
12645 case ENUMERAL_TYPE:
12646 gen_inlined_enumeration_type_die (type, context_die);
12647 break;
12649 case RECORD_TYPE:
12650 gen_inlined_structure_type_die (type, context_die);
12651 break;
12653 case UNION_TYPE:
12654 case QUAL_UNION_TYPE:
12655 gen_inlined_union_type_die (type, context_die);
12656 break;
12658 default:
12659 gcc_unreachable ();
12663 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
12664 things which are local to the given block. */
12666 static void
12667 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
12669 int must_output_die = 0;
12670 tree origin;
12671 tree decl;
12672 enum tree_code origin_code;
12674 /* Ignore blocks that are NULL. */
12675 if (stmt == NULL_TREE)
12676 return;
12678 /* If the block is one fragment of a non-contiguous block, do not
12679 process the variables, since they will have been done by the
12680 origin block. Do process subblocks. */
12681 if (BLOCK_FRAGMENT_ORIGIN (stmt))
12683 tree sub;
12685 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
12686 gen_block_die (sub, context_die, depth + 1);
12688 return;
12691 /* Determine the "ultimate origin" of this block. This block may be an
12692 inlined instance of an inlined instance of inline function, so we have
12693 to trace all of the way back through the origin chain to find out what
12694 sort of node actually served as the original seed for the creation of
12695 the current block. */
12696 origin = block_ultimate_origin (stmt);
12697 origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
12699 /* Determine if we need to output any Dwarf DIEs at all to represent this
12700 block. */
12701 if (origin_code == FUNCTION_DECL)
12702 /* The outer scopes for inlinings *must* always be represented. We
12703 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
12704 must_output_die = 1;
12705 else
12707 /* In the case where the current block represents an inlining of the
12708 "body block" of an inline function, we must *NOT* output any DIE for
12709 this block because we have already output a DIE to represent the whole
12710 inlined function scope and the "body block" of any function doesn't
12711 really represent a different scope according to ANSI C rules. So we
12712 check here to make sure that this block does not represent a "body
12713 block inlining" before trying to set the MUST_OUTPUT_DIE flag. */
12714 if (! is_body_block (origin ? origin : stmt))
12716 /* Determine if this block directly contains any "significant"
12717 local declarations which we will need to output DIEs for. */
12718 if (debug_info_level > DINFO_LEVEL_TERSE)
12719 /* We are not in terse mode so *any* local declaration counts
12720 as being a "significant" one. */
12721 must_output_die = (BLOCK_VARS (stmt) != NULL
12722 && (TREE_USED (stmt)
12723 || TREE_ASM_WRITTEN (stmt)
12724 || BLOCK_ABSTRACT (stmt)));
12725 else
12726 /* We are in terse mode, so only local (nested) function
12727 definitions count as "significant" local declarations. */
12728 for (decl = BLOCK_VARS (stmt);
12729 decl != NULL; decl = TREE_CHAIN (decl))
12730 if (TREE_CODE (decl) == FUNCTION_DECL
12731 && DECL_INITIAL (decl))
12733 must_output_die = 1;
12734 break;
12739 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
12740 DIE for any block which contains no significant local declarations at
12741 all. Rather, in such cases we just call `decls_for_scope' so that any
12742 needed Dwarf info for any sub-blocks will get properly generated. Note
12743 that in terse mode, our definition of what constitutes a "significant"
12744 local declaration gets restricted to include only inlined function
12745 instances and local (nested) function definitions. */
12746 if (must_output_die)
12748 if (origin_code == FUNCTION_DECL)
12749 gen_inlined_subroutine_die (stmt, context_die, depth);
12750 else
12751 gen_lexical_block_die (stmt, context_die, depth);
12753 else
12754 decls_for_scope (stmt, context_die, depth);
12757 /* Generate all of the decls declared within a given scope and (recursively)
12758 all of its sub-blocks. */
12760 static void
12761 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
12763 tree decl;
12764 tree subblocks;
12766 /* Ignore NULL blocks. */
12767 if (stmt == NULL_TREE)
12768 return;
12770 if (TREE_USED (stmt))
12772 /* Output the DIEs to represent all of the data objects and typedefs
12773 declared directly within this block but not within any nested
12774 sub-blocks. Also, nested function and tag DIEs have been
12775 generated with a parent of NULL; fix that up now. */
12776 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
12778 dw_die_ref die;
12780 if (TREE_CODE (decl) == FUNCTION_DECL)
12781 die = lookup_decl_die (decl);
12782 else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
12783 die = lookup_type_die (TREE_TYPE (decl));
12784 else
12785 die = NULL;
12787 if (die != NULL && die->die_parent == NULL)
12788 add_child_die (context_die, die);
12789 /* Do not produce debug information for static variables since
12790 these might be optimized out. We are called for these later
12791 in cgraph_varpool_analyze_pending_decls. */
12792 if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
12794 else
12795 gen_decl_die (decl, context_die);
12799 /* If we're at -g1, we're not interested in subblocks. */
12800 if (debug_info_level <= DINFO_LEVEL_TERSE)
12801 return;
12803 /* Output the DIEs to represent all sub-blocks (and the items declared
12804 therein) of this block. */
12805 for (subblocks = BLOCK_SUBBLOCKS (stmt);
12806 subblocks != NULL;
12807 subblocks = BLOCK_CHAIN (subblocks))
12808 gen_block_die (subblocks, context_die, depth + 1);
12811 /* Is this a typedef we can avoid emitting? */
12813 static inline int
12814 is_redundant_typedef (tree decl)
12816 if (TYPE_DECL_IS_STUB (decl))
12817 return 1;
12819 if (DECL_ARTIFICIAL (decl)
12820 && DECL_CONTEXT (decl)
12821 && is_tagged_type (DECL_CONTEXT (decl))
12822 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
12823 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
12824 /* Also ignore the artificial member typedef for the class name. */
12825 return 1;
12827 return 0;
12830 /* Returns the DIE for decl. A DIE will always be returned. */
12832 static dw_die_ref
12833 force_decl_die (tree decl)
12835 dw_die_ref decl_die;
12836 unsigned saved_external_flag;
12837 tree save_fn = NULL_TREE;
12838 decl_die = lookup_decl_die (decl);
12839 if (!decl_die)
12841 dw_die_ref context_die;
12842 tree decl_context = DECL_CONTEXT (decl);
12843 if (decl_context)
12845 /* Find die that represents this context. */
12846 if (TYPE_P (decl_context))
12847 context_die = force_type_die (decl_context);
12848 else
12849 context_die = force_decl_die (decl_context);
12851 else
12852 context_die = comp_unit_die;
12854 decl_die = lookup_decl_die (decl);
12855 if (decl_die)
12856 return decl_die;
12858 switch (TREE_CODE (decl))
12860 case FUNCTION_DECL:
12861 /* Clear current_function_decl, so that gen_subprogram_die thinks
12862 that this is a declaration. At this point, we just want to force
12863 declaration die. */
12864 save_fn = current_function_decl;
12865 current_function_decl = NULL_TREE;
12866 gen_subprogram_die (decl, context_die);
12867 current_function_decl = save_fn;
12868 break;
12870 case VAR_DECL:
12871 /* Set external flag to force declaration die. Restore it after
12872 gen_decl_die() call. */
12873 saved_external_flag = DECL_EXTERNAL (decl);
12874 DECL_EXTERNAL (decl) = 1;
12875 gen_decl_die (decl, context_die);
12876 DECL_EXTERNAL (decl) = saved_external_flag;
12877 break;
12879 case NAMESPACE_DECL:
12880 dwarf2out_decl (decl);
12881 break;
12883 default:
12884 gcc_unreachable ();
12887 /* We should be able to find the DIE now. */
12888 if (!decl_die)
12889 decl_die = lookup_decl_die (decl);
12890 gcc_assert (decl_die);
12893 return decl_die;
12896 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
12897 always returned. */
12899 static dw_die_ref
12900 force_type_die (tree type)
12902 dw_die_ref type_die;
12904 type_die = lookup_type_die (type);
12905 if (!type_die)
12907 dw_die_ref context_die;
12908 if (TYPE_CONTEXT (type))
12910 if (TYPE_P (TYPE_CONTEXT (type)))
12911 context_die = force_type_die (TYPE_CONTEXT (type));
12912 else
12913 context_die = force_decl_die (TYPE_CONTEXT (type));
12915 else
12916 context_die = comp_unit_die;
12918 type_die = lookup_type_die (type);
12919 if (type_die)
12920 return type_die;
12921 gen_type_die (type, context_die);
12922 type_die = lookup_type_die (type);
12923 gcc_assert (type_die);
12925 return type_die;
12928 /* Force out any required namespaces to be able to output DECL,
12929 and return the new context_die for it, if it's changed. */
12931 static dw_die_ref
12932 setup_namespace_context (tree thing, dw_die_ref context_die)
12934 tree context = (DECL_P (thing)
12935 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
12936 if (context && TREE_CODE (context) == NAMESPACE_DECL)
12937 /* Force out the namespace. */
12938 context_die = force_decl_die (context);
12940 return context_die;
12943 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
12944 type) within its namespace, if appropriate.
12946 For compatibility with older debuggers, namespace DIEs only contain
12947 declarations; all definitions are emitted at CU scope. */
12949 static void
12950 declare_in_namespace (tree thing, dw_die_ref context_die)
12952 dw_die_ref ns_context;
12954 if (debug_info_level <= DINFO_LEVEL_TERSE)
12955 return;
12957 /* If this decl is from an inlined function, then don't try to emit it in its
12958 namespace, as we will get confused. It would have already been emitted
12959 when the abstract instance of the inline function was emitted anyways. */
12960 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
12961 return;
12963 ns_context = setup_namespace_context (thing, context_die);
12965 if (ns_context != context_die)
12967 if (DECL_P (thing))
12968 gen_decl_die (thing, ns_context);
12969 else
12970 gen_type_die (thing, ns_context);
12974 /* Generate a DIE for a namespace or namespace alias. */
12976 static void
12977 gen_namespace_die (tree decl)
12979 dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
12981 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
12982 they are an alias of. */
12983 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
12985 /* Output a real namespace. */
12986 dw_die_ref namespace_die
12987 = new_die (DW_TAG_namespace, context_die, decl);
12988 add_name_and_src_coords_attributes (namespace_die, decl);
12989 equate_decl_number_to_die (decl, namespace_die);
12991 else
12993 /* Output a namespace alias. */
12995 /* Force out the namespace we are an alias of, if necessary. */
12996 dw_die_ref origin_die
12997 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
12999 /* Now create the namespace alias DIE. */
13000 dw_die_ref namespace_die
13001 = new_die (DW_TAG_imported_declaration, context_die, decl);
13002 add_name_and_src_coords_attributes (namespace_die, decl);
13003 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
13004 equate_decl_number_to_die (decl, namespace_die);
13008 /* Generate Dwarf debug information for a decl described by DECL. */
13010 static void
13011 gen_decl_die (tree decl, dw_die_ref context_die)
13013 tree origin;
13015 if (DECL_P (decl) && DECL_IGNORED_P (decl))
13016 return;
13018 switch (TREE_CODE (decl))
13020 case ERROR_MARK:
13021 break;
13023 case CONST_DECL:
13024 /* The individual enumerators of an enum type get output when we output
13025 the Dwarf representation of the relevant enum type itself. */
13026 break;
13028 case FUNCTION_DECL:
13029 /* Don't output any DIEs to represent mere function declarations,
13030 unless they are class members or explicit block externs. */
13031 if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
13032 && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
13033 break;
13035 #if 0
13036 /* FIXME */
13037 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
13038 on local redeclarations of global functions. That seems broken. */
13039 if (current_function_decl != decl)
13040 /* This is only a declaration. */;
13041 #endif
13043 /* If we're emitting a clone, emit info for the abstract instance. */
13044 if (DECL_ORIGIN (decl) != decl)
13045 dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
13047 /* If we're emitting an out-of-line copy of an inline function,
13048 emit info for the abstract instance and set up to refer to it. */
13049 else if (cgraph_function_possibly_inlined_p (decl)
13050 && ! DECL_ABSTRACT (decl)
13051 && ! class_or_namespace_scope_p (context_die)
13052 /* dwarf2out_abstract_function won't emit a die if this is just
13053 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
13054 that case, because that works only if we have a die. */
13055 && DECL_INITIAL (decl) != NULL_TREE)
13057 dwarf2out_abstract_function (decl);
13058 set_decl_origin_self (decl);
13061 /* Otherwise we're emitting the primary DIE for this decl. */
13062 else if (debug_info_level > DINFO_LEVEL_TERSE)
13064 /* Before we describe the FUNCTION_DECL itself, make sure that we
13065 have described its return type. */
13066 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
13068 /* And its virtual context. */
13069 if (DECL_VINDEX (decl) != NULL_TREE)
13070 gen_type_die (DECL_CONTEXT (decl), context_die);
13072 /* And its containing type. */
13073 origin = decl_class_context (decl);
13074 if (origin != NULL_TREE)
13075 gen_type_die_for_member (origin, decl, context_die);
13077 /* And its containing namespace. */
13078 declare_in_namespace (decl, context_die);
13081 /* Now output a DIE to represent the function itself. */
13082 gen_subprogram_die (decl, context_die);
13083 break;
13085 case TYPE_DECL:
13086 /* If we are in terse mode, don't generate any DIEs to represent any
13087 actual typedefs. */
13088 if (debug_info_level <= DINFO_LEVEL_TERSE)
13089 break;
13091 /* In the special case of a TYPE_DECL node representing the declaration
13092 of some type tag, if the given TYPE_DECL is marked as having been
13093 instantiated from some other (original) TYPE_DECL node (e.g. one which
13094 was generated within the original definition of an inline function) we
13095 have to generate a special (abbreviated) DW_TAG_structure_type,
13096 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. */
13097 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
13099 gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
13100 break;
13103 if (is_redundant_typedef (decl))
13104 gen_type_die (TREE_TYPE (decl), context_die);
13105 else
13106 /* Output a DIE to represent the typedef itself. */
13107 gen_typedef_die (decl, context_die);
13108 break;
13110 case LABEL_DECL:
13111 if (debug_info_level >= DINFO_LEVEL_NORMAL)
13112 gen_label_die (decl, context_die);
13113 break;
13115 case VAR_DECL:
13116 case RESULT_DECL:
13117 /* If we are in terse mode, don't generate any DIEs to represent any
13118 variable declarations or definitions. */
13119 if (debug_info_level <= DINFO_LEVEL_TERSE)
13120 break;
13122 /* Output any DIEs that are needed to specify the type of this data
13123 object. */
13124 gen_type_die (TREE_TYPE (decl), context_die);
13126 /* And its containing type. */
13127 origin = decl_class_context (decl);
13128 if (origin != NULL_TREE)
13129 gen_type_die_for_member (origin, decl, context_die);
13131 /* And its containing namespace. */
13132 declare_in_namespace (decl, context_die);
13134 /* Now output the DIE to represent the data object itself. This gets
13135 complicated because of the possibility that the VAR_DECL really
13136 represents an inlined instance of a formal parameter for an inline
13137 function. */
13138 origin = decl_ultimate_origin (decl);
13139 if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
13140 gen_formal_parameter_die (decl, context_die);
13141 else
13142 gen_variable_die (decl, context_die);
13143 break;
13145 case FIELD_DECL:
13146 /* Ignore the nameless fields that are used to skip bits but handle C++
13147 anonymous unions and structs. */
13148 if (DECL_NAME (decl) != NULL_TREE
13149 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
13150 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
13152 gen_type_die (member_declared_type (decl), context_die);
13153 gen_field_die (decl, context_die);
13155 break;
13157 case PARM_DECL:
13158 gen_type_die (TREE_TYPE (decl), context_die);
13159 gen_formal_parameter_die (decl, context_die);
13160 break;
13162 case NAMESPACE_DECL:
13163 gen_namespace_die (decl);
13164 break;
13166 default:
13167 /* Probably some frontend-internal decl. Assume we don't care. */
13168 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
13169 break;
13173 /* Add Ada "use" clause information for SGI Workshop debugger. */
13175 void
13176 dwarf2out_add_library_unit_info (const char *filename, const char *context_list)
13178 unsigned int file_index;
13180 if (filename != NULL)
13182 dw_die_ref unit_die = new_die (DW_TAG_module, comp_unit_die, NULL);
13183 tree context_list_decl
13184 = build_decl (LABEL_DECL, get_identifier (context_list),
13185 void_type_node);
13187 TREE_PUBLIC (context_list_decl) = TRUE;
13188 add_name_attribute (unit_die, context_list);
13189 file_index = lookup_filename (filename);
13190 add_AT_unsigned (unit_die, DW_AT_decl_file, file_index);
13191 add_pubname (context_list_decl, unit_die);
13195 /* Output debug information for global decl DECL. Called from toplev.c after
13196 compilation proper has finished. */
13198 static void
13199 dwarf2out_global_decl (tree decl)
13201 /* Output DWARF2 information for file-scope tentative data object
13202 declarations, file-scope (extern) function declarations (which had no
13203 corresponding body) and file-scope tagged type declarations and
13204 definitions which have not yet been forced out. */
13205 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
13206 dwarf2out_decl (decl);
13209 /* Output debug information for type decl DECL. Called from toplev.c
13210 and from language front ends (to record built-in types). */
13211 static void
13212 dwarf2out_type_decl (tree decl, int local)
13214 if (!local)
13215 dwarf2out_decl (decl);
13218 /* Output debug information for imported module or decl. */
13220 static void
13221 dwarf2out_imported_module_or_decl (tree decl, tree context)
13223 dw_die_ref imported_die, at_import_die;
13224 dw_die_ref scope_die;
13225 unsigned file_index;
13226 expanded_location xloc;
13228 if (debug_info_level <= DINFO_LEVEL_TERSE)
13229 return;
13231 gcc_assert (decl);
13233 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
13234 We need decl DIE for reference and scope die. First, get DIE for the decl
13235 itself. */
13237 /* Get the scope die for decl context. Use comp_unit_die for global module
13238 or decl. If die is not found for non globals, force new die. */
13239 if (!context)
13240 scope_die = comp_unit_die;
13241 else if (TYPE_P (context))
13242 scope_die = force_type_die (context);
13243 else
13244 scope_die = force_decl_die (context);
13246 /* For TYPE_DECL or CONST_DECL, lookup TREE_TYPE. */
13247 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
13249 if (is_base_type (TREE_TYPE (decl)))
13250 at_import_die = base_type_die (TREE_TYPE (decl));
13251 else
13252 at_import_die = force_type_die (TREE_TYPE (decl));
13254 else
13256 at_import_die = lookup_decl_die (decl);
13257 if (!at_import_die)
13259 /* If we're trying to avoid duplicate debug info, we may not have
13260 emitted the member decl for this field. Emit it now. */
13261 if (TREE_CODE (decl) == FIELD_DECL)
13263 tree type = DECL_CONTEXT (decl);
13264 dw_die_ref type_context_die;
13266 if (TYPE_CONTEXT (type))
13267 if (TYPE_P (TYPE_CONTEXT (type)))
13268 type_context_die = force_type_die (TYPE_CONTEXT (type));
13269 else
13270 type_context_die = force_decl_die (TYPE_CONTEXT (type));
13271 else
13272 type_context_die = comp_unit_die;
13273 gen_type_die_for_member (type, decl, type_context_die);
13275 at_import_die = force_decl_die (decl);
13279 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
13280 if (TREE_CODE (decl) == NAMESPACE_DECL)
13281 imported_die = new_die (DW_TAG_imported_module, scope_die, context);
13282 else
13283 imported_die = new_die (DW_TAG_imported_declaration, scope_die, context);
13285 xloc = expand_location (input_location);
13286 file_index = lookup_filename (xloc.file);
13287 add_AT_unsigned (imported_die, DW_AT_decl_file, file_index);
13288 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
13289 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
13292 /* Write the debugging output for DECL. */
13294 void
13295 dwarf2out_decl (tree decl)
13297 dw_die_ref context_die = comp_unit_die;
13299 switch (TREE_CODE (decl))
13301 case ERROR_MARK:
13302 return;
13304 case FUNCTION_DECL:
13305 /* What we would really like to do here is to filter out all mere
13306 file-scope declarations of file-scope functions which are never
13307 referenced later within this translation unit (and keep all of ones
13308 that *are* referenced later on) but we aren't clairvoyant, so we have
13309 no idea which functions will be referenced in the future (i.e. later
13310 on within the current translation unit). So here we just ignore all
13311 file-scope function declarations which are not also definitions. If
13312 and when the debugger needs to know something about these functions,
13313 it will have to hunt around and find the DWARF information associated
13314 with the definition of the function.
13316 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
13317 nodes represent definitions and which ones represent mere
13318 declarations. We have to check DECL_INITIAL instead. That's because
13319 the C front-end supports some weird semantics for "extern inline"
13320 function definitions. These can get inlined within the current
13321 translation unit (and thus, we need to generate Dwarf info for their
13322 abstract instances so that the Dwarf info for the concrete inlined
13323 instances can have something to refer to) but the compiler never
13324 generates any out-of-lines instances of such things (despite the fact
13325 that they *are* definitions).
13327 The important point is that the C front-end marks these "extern
13328 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
13329 them anyway. Note that the C++ front-end also plays some similar games
13330 for inline function definitions appearing within include files which
13331 also contain `#pragma interface' pragmas. */
13332 if (DECL_INITIAL (decl) == NULL_TREE)
13333 return;
13335 /* If we're a nested function, initially use a parent of NULL; if we're
13336 a plain function, this will be fixed up in decls_for_scope. If
13337 we're a method, it will be ignored, since we already have a DIE. */
13338 if (decl_function_context (decl)
13339 /* But if we're in terse mode, we don't care about scope. */
13340 && debug_info_level > DINFO_LEVEL_TERSE)
13341 context_die = NULL;
13342 break;
13344 case VAR_DECL:
13345 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
13346 declaration and if the declaration was never even referenced from
13347 within this entire compilation unit. We suppress these DIEs in
13348 order to save space in the .debug section (by eliminating entries
13349 which are probably useless). Note that we must not suppress
13350 block-local extern declarations (whether used or not) because that
13351 would screw-up the debugger's name lookup mechanism and cause it to
13352 miss things which really ought to be in scope at a given point. */
13353 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
13354 return;
13356 /* For local statics lookup proper context die. */
13357 if (TREE_STATIC (decl) && decl_function_context (decl))
13358 context_die = lookup_decl_die (DECL_CONTEXT (decl));
13360 /* If we are in terse mode, don't generate any DIEs to represent any
13361 variable declarations or definitions. */
13362 if (debug_info_level <= DINFO_LEVEL_TERSE)
13363 return;
13364 break;
13366 case NAMESPACE_DECL:
13367 if (debug_info_level <= DINFO_LEVEL_TERSE)
13368 return;
13369 if (lookup_decl_die (decl) != NULL)
13370 return;
13371 break;
13373 case TYPE_DECL:
13374 /* Don't emit stubs for types unless they are needed by other DIEs. */
13375 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
13376 return;
13378 /* Don't bother trying to generate any DIEs to represent any of the
13379 normal built-in types for the language we are compiling. */
13380 if (DECL_IS_BUILTIN (decl))
13382 /* OK, we need to generate one for `bool' so GDB knows what type
13383 comparisons have. */
13384 if ((get_AT_unsigned (comp_unit_die, DW_AT_language)
13385 == DW_LANG_C_plus_plus)
13386 && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
13387 && ! DECL_IGNORED_P (decl))
13388 modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
13390 return;
13393 /* If we are in terse mode, don't generate any DIEs for types. */
13394 if (debug_info_level <= DINFO_LEVEL_TERSE)
13395 return;
13397 /* If we're a function-scope tag, initially use a parent of NULL;
13398 this will be fixed up in decls_for_scope. */
13399 if (decl_function_context (decl))
13400 context_die = NULL;
13402 break;
13404 default:
13405 return;
13408 gen_decl_die (decl, context_die);
13411 /* Output a marker (i.e. a label) for the beginning of the generated code for
13412 a lexical block. */
13414 static void
13415 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
13416 unsigned int blocknum)
13418 current_function_section (current_function_decl);
13419 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
13422 /* Output a marker (i.e. a label) for the end of the generated code for a
13423 lexical block. */
13425 static void
13426 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
13428 current_function_section (current_function_decl);
13429 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
13432 /* Returns nonzero if it is appropriate not to emit any debugging
13433 information for BLOCK, because it doesn't contain any instructions.
13435 Don't allow this for blocks with nested functions or local classes
13436 as we would end up with orphans, and in the presence of scheduling
13437 we may end up calling them anyway. */
13439 static bool
13440 dwarf2out_ignore_block (tree block)
13442 tree decl;
13444 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
13445 if (TREE_CODE (decl) == FUNCTION_DECL
13446 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
13447 return 0;
13449 return 1;
13452 /* Lookup FILE_NAME (in the list of filenames that we know about here in
13453 dwarf2out.c) and return its "index". The index of each (known) filename is
13454 just a unique number which is associated with only that one filename. We
13455 need such numbers for the sake of generating labels (in the .debug_sfnames
13456 section) and references to those files numbers (in the .debug_srcinfo
13457 and.debug_macinfo sections). If the filename given as an argument is not
13458 found in our current list, add it to the list and assign it the next
13459 available unique index number. In order to speed up searches, we remember
13460 the index of the filename was looked up last. This handles the majority of
13461 all searches. */
13463 static unsigned
13464 lookup_filename (const char *file_name)
13466 size_t i, n;
13467 char *save_file_name;
13469 /* Check to see if the file name that was searched on the previous
13470 call matches this file name. If so, return the index. */
13471 if (file_table_last_lookup_index != 0)
13473 const char *last
13474 = VARRAY_CHAR_PTR (file_table, file_table_last_lookup_index);
13475 if (strcmp (file_name, last) == 0)
13476 return file_table_last_lookup_index;
13479 /* Didn't match the previous lookup, search the table. */
13480 n = VARRAY_ACTIVE_SIZE (file_table);
13481 for (i = 1; i < n; i++)
13482 if (strcmp (file_name, VARRAY_CHAR_PTR (file_table, i)) == 0)
13484 file_table_last_lookup_index = i;
13485 return i;
13488 /* Add the new entry to the end of the filename table. */
13489 file_table_last_lookup_index = n;
13490 save_file_name = (char *) ggc_strdup (file_name);
13491 VARRAY_PUSH_CHAR_PTR (file_table, save_file_name);
13492 VARRAY_PUSH_UINT (file_table_emitted, 0);
13494 /* If the assembler is emitting the file table, and we aren't eliminating
13495 unused debug types, then we must emit .file here. If we are eliminating
13496 unused debug types, then this will be done by the maybe_emit_file call in
13497 prune_unused_types_walk_attribs. */
13499 if (DWARF2_ASM_LINE_DEBUG_INFO && ! flag_eliminate_unused_debug_types)
13500 return maybe_emit_file (i);
13502 return i;
13505 /* If the assembler will construct the file table, then translate the compiler
13506 internal file table number into the assembler file table number, and emit
13507 a .file directive if we haven't already emitted one yet. The file table
13508 numbers are different because we prune debug info for unused variables and
13509 types, which may include filenames. */
13511 static int
13512 maybe_emit_file (int fileno)
13514 if (DWARF2_ASM_LINE_DEBUG_INFO && fileno > 0)
13516 if (!VARRAY_UINT (file_table_emitted, fileno))
13518 VARRAY_UINT (file_table_emitted, fileno) = ++emitcount;
13519 fprintf (asm_out_file, "\t.file %u ",
13520 VARRAY_UINT (file_table_emitted, fileno));
13521 output_quoted_string (asm_out_file,
13522 VARRAY_CHAR_PTR (file_table, fileno));
13523 fputc ('\n', asm_out_file);
13525 return VARRAY_UINT (file_table_emitted, fileno);
13527 else
13528 return fileno;
13531 /* Initialize the compiler internal file table. */
13533 static void
13534 init_file_table (void)
13536 /* Allocate the initial hunk of the file_table. */
13537 VARRAY_CHAR_PTR_INIT (file_table, 64, "file_table");
13538 VARRAY_UINT_INIT (file_table_emitted, 64, "file_table_emitted");
13540 /* Skip the first entry - file numbers begin at 1. */
13541 VARRAY_PUSH_CHAR_PTR (file_table, NULL);
13542 VARRAY_PUSH_UINT (file_table_emitted, 0);
13543 file_table_last_lookup_index = 0;
13546 /* Called by the final INSN scan whenever we see a var location. We
13547 use it to drop labels in the right places, and throw the location in
13548 our lookup table. */
13550 static void
13551 dwarf2out_var_location (rtx loc_note)
13553 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
13554 struct var_loc_node *newloc;
13555 rtx prev_insn;
13556 static rtx last_insn;
13557 static const char *last_label;
13558 tree decl;
13560 if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
13561 return;
13562 prev_insn = PREV_INSN (loc_note);
13564 newloc = ggc_alloc_cleared (sizeof (struct var_loc_node));
13565 /* If the insn we processed last time is the previous insn
13566 and it is also a var location note, use the label we emitted
13567 last time. */
13568 if (last_insn != NULL_RTX
13569 && last_insn == prev_insn
13570 && NOTE_P (prev_insn)
13571 && NOTE_LINE_NUMBER (prev_insn) == NOTE_INSN_VAR_LOCATION)
13573 newloc->label = last_label;
13575 else
13577 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
13578 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
13579 loclabel_num++;
13580 newloc->label = ggc_strdup (loclabel);
13582 newloc->var_loc_note = loc_note;
13583 newloc->next = NULL;
13585 if (cfun
13586 && (last_text_section == in_unlikely_executed_text
13587 || (last_text_section == in_named
13588 && last_text_section_name == cfun->unlikely_text_section_name)))
13589 newloc->section_label = cfun->cold_section_label;
13590 else
13591 newloc->section_label = text_section_label;
13593 last_insn = loc_note;
13594 last_label = newloc->label;
13595 decl = NOTE_VAR_LOCATION_DECL (loc_note);
13596 if (DECL_DEBUG_EXPR_IS_FROM (decl) && DECL_DEBUG_EXPR (decl)
13597 && DECL_P (DECL_DEBUG_EXPR (decl)))
13598 decl = DECL_DEBUG_EXPR (decl);
13599 add_var_loc_to_decl (decl, newloc);
13602 /* We need to reset the locations at the beginning of each
13603 function. We can't do this in the end_function hook, because the
13604 declarations that use the locations won't have been outputted when
13605 that hook is called. */
13607 static void
13608 dwarf2out_begin_function (tree unused ATTRIBUTE_UNUSED)
13610 htab_empty (decl_loc_table);
13613 /* Output a label to mark the beginning of a source code line entry
13614 and record information relating to this source line, in
13615 'line_info_table' for later output of the .debug_line section. */
13617 static void
13618 dwarf2out_source_line (unsigned int line, const char *filename)
13620 if (debug_info_level >= DINFO_LEVEL_NORMAL
13621 && line != 0)
13623 current_function_section (current_function_decl);
13625 /* If requested, emit something human-readable. */
13626 if (flag_debug_asm)
13627 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
13628 filename, line);
13630 if (DWARF2_ASM_LINE_DEBUG_INFO)
13632 unsigned file_num = lookup_filename (filename);
13634 file_num = maybe_emit_file (file_num);
13636 /* Emit the .loc directive understood by GNU as. */
13637 fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
13639 /* Indicate that line number info exists. */
13640 line_info_table_in_use++;
13642 /* Indicate that multiple line number tables exist. */
13643 if (DECL_SECTION_NAME (current_function_decl))
13644 separate_line_info_table_in_use++;
13646 else if (DECL_SECTION_NAME (current_function_decl))
13648 dw_separate_line_info_ref line_info;
13649 targetm.asm_out.internal_label (asm_out_file, SEPARATE_LINE_CODE_LABEL,
13650 separate_line_info_table_in_use);
13652 /* Expand the line info table if necessary. */
13653 if (separate_line_info_table_in_use
13654 == separate_line_info_table_allocated)
13656 separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
13657 separate_line_info_table
13658 = ggc_realloc (separate_line_info_table,
13659 separate_line_info_table_allocated
13660 * sizeof (dw_separate_line_info_entry));
13661 memset (separate_line_info_table
13662 + separate_line_info_table_in_use,
13664 (LINE_INFO_TABLE_INCREMENT
13665 * sizeof (dw_separate_line_info_entry)));
13668 /* Add the new entry at the end of the line_info_table. */
13669 line_info
13670 = &separate_line_info_table[separate_line_info_table_in_use++];
13671 line_info->dw_file_num = lookup_filename (filename);
13672 line_info->dw_line_num = line;
13673 line_info->function = current_function_funcdef_no;
13675 else
13677 dw_line_info_ref line_info;
13679 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
13680 line_info_table_in_use);
13682 /* Expand the line info table if necessary. */
13683 if (line_info_table_in_use == line_info_table_allocated)
13685 line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
13686 line_info_table
13687 = ggc_realloc (line_info_table,
13688 (line_info_table_allocated
13689 * sizeof (dw_line_info_entry)));
13690 memset (line_info_table + line_info_table_in_use, 0,
13691 LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
13694 /* Add the new entry at the end of the line_info_table. */
13695 line_info = &line_info_table[line_info_table_in_use++];
13696 line_info->dw_file_num = lookup_filename (filename);
13697 line_info->dw_line_num = line;
13702 /* Record the beginning of a new source file. */
13704 static void
13705 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
13707 if (flag_eliminate_dwarf2_dups)
13709 /* Record the beginning of the file for break_out_includes. */
13710 dw_die_ref bincl_die;
13712 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
13713 add_AT_string (bincl_die, DW_AT_name, filename);
13716 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13718 int fileno;
13720 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13721 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
13722 dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
13723 lineno);
13725 fileno = maybe_emit_file (lookup_filename (filename));
13726 dw2_asm_output_data_uleb128 (fileno, "Filename we just started");
13730 /* Record the end of a source file. */
13732 static void
13733 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
13735 if (flag_eliminate_dwarf2_dups)
13736 /* Record the end of the file for break_out_includes. */
13737 new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
13739 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13741 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13742 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
13746 /* Called from debug_define in toplev.c. The `buffer' parameter contains
13747 the tail part of the directive line, i.e. the part which is past the
13748 initial whitespace, #, whitespace, directive-name, whitespace part. */
13750 static void
13751 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
13752 const char *buffer ATTRIBUTE_UNUSED)
13754 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13756 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13757 dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
13758 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
13759 dw2_asm_output_nstring (buffer, -1, "The macro");
13763 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
13764 the tail part of the directive line, i.e. the part which is past the
13765 initial whitespace, #, whitespace, directive-name, whitespace part. */
13767 static void
13768 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
13769 const char *buffer ATTRIBUTE_UNUSED)
13771 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13773 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13774 dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
13775 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
13776 dw2_asm_output_nstring (buffer, -1, "The macro");
13780 /* Set up for Dwarf output at the start of compilation. */
13782 static void
13783 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
13785 init_file_table ();
13787 /* Allocate the decl_die_table. */
13788 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
13789 decl_die_table_eq, NULL);
13791 /* Allocate the decl_loc_table. */
13792 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
13793 decl_loc_table_eq, NULL);
13795 /* Allocate the initial hunk of the decl_scope_table. */
13796 decl_scope_table = VEC_alloc (tree, gc, 256);
13798 /* Allocate the initial hunk of the abbrev_die_table. */
13799 abbrev_die_table = ggc_alloc_cleared (ABBREV_DIE_TABLE_INCREMENT
13800 * sizeof (dw_die_ref));
13801 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
13802 /* Zero-th entry is allocated, but unused. */
13803 abbrev_die_table_in_use = 1;
13805 /* Allocate the initial hunk of the line_info_table. */
13806 line_info_table = ggc_alloc_cleared (LINE_INFO_TABLE_INCREMENT
13807 * sizeof (dw_line_info_entry));
13808 line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
13810 /* Zero-th entry is allocated, but unused. */
13811 line_info_table_in_use = 1;
13813 /* Generate the initial DIE for the .debug section. Note that the (string)
13814 value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
13815 will (typically) be a relative pathname and that this pathname should be
13816 taken as being relative to the directory from which the compiler was
13817 invoked when the given (base) source file was compiled. We will fill
13818 in this value in dwarf2out_finish. */
13819 comp_unit_die = gen_compile_unit_die (NULL);
13821 incomplete_types = VEC_alloc (tree, gc, 64);
13823 used_rtx_array = VEC_alloc (rtx, gc, 32);
13825 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
13826 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
13827 DEBUG_ABBREV_SECTION_LABEL, 0);
13828 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
13829 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
13830 COLD_TEXT_SECTION_LABEL, 0);
13831 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
13833 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
13834 DEBUG_INFO_SECTION_LABEL, 0);
13835 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
13836 DEBUG_LINE_SECTION_LABEL, 0);
13837 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
13838 DEBUG_RANGES_SECTION_LABEL, 0);
13839 named_section_flags (DEBUG_ABBREV_SECTION, SECTION_DEBUG);
13840 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
13841 named_section_flags (DEBUG_INFO_SECTION, SECTION_DEBUG);
13842 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
13843 named_section_flags (DEBUG_LINE_SECTION, SECTION_DEBUG);
13844 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
13846 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13848 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13849 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
13850 DEBUG_MACINFO_SECTION_LABEL, 0);
13851 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
13854 text_section ();
13855 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
13856 if (flag_reorder_blocks_and_partition)
13858 unlikely_text_section ();
13859 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
13863 /* A helper function for dwarf2out_finish called through
13864 ht_forall. Emit one queued .debug_str string. */
13866 static int
13867 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
13869 struct indirect_string_node *node = (struct indirect_string_node *) *h;
13871 if (node->form == DW_FORM_strp)
13873 named_section_flags (DEBUG_STR_SECTION, DEBUG_STR_SECTION_FLAGS);
13874 ASM_OUTPUT_LABEL (asm_out_file, node->label);
13875 assemble_string (node->str, strlen (node->str) + 1);
13878 return 1;
13883 /* Clear the marks for a die and its children.
13884 Be cool if the mark isn't set. */
13886 static void
13887 prune_unmark_dies (dw_die_ref die)
13889 dw_die_ref c;
13890 die->die_mark = 0;
13891 for (c = die->die_child; c; c = c->die_sib)
13892 prune_unmark_dies (c);
13896 /* Given DIE that we're marking as used, find any other dies
13897 it references as attributes and mark them as used. */
13899 static void
13900 prune_unused_types_walk_attribs (dw_die_ref die)
13902 dw_attr_ref a;
13904 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
13906 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
13908 /* A reference to another DIE.
13909 Make sure that it will get emitted. */
13910 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
13912 else if (a->dw_attr == DW_AT_decl_file || a->dw_attr == DW_AT_call_file)
13914 /* A reference to a file. Make sure the file name is emitted. */
13915 a->dw_attr_val.v.val_unsigned =
13916 maybe_emit_file (a->dw_attr_val.v.val_unsigned);
13922 /* Mark DIE as being used. If DOKIDS is true, then walk down
13923 to DIE's children. */
13925 static void
13926 prune_unused_types_mark (dw_die_ref die, int dokids)
13928 dw_die_ref c;
13930 if (die->die_mark == 0)
13932 /* We haven't done this node yet. Mark it as used. */
13933 die->die_mark = 1;
13935 /* We also have to mark its parents as used.
13936 (But we don't want to mark our parents' kids due to this.) */
13937 if (die->die_parent)
13938 prune_unused_types_mark (die->die_parent, 0);
13940 /* Mark any referenced nodes. */
13941 prune_unused_types_walk_attribs (die);
13943 /* If this node is a specification,
13944 also mark the definition, if it exists. */
13945 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
13946 prune_unused_types_mark (die->die_definition, 1);
13949 if (dokids && die->die_mark != 2)
13951 /* We need to walk the children, but haven't done so yet.
13952 Remember that we've walked the kids. */
13953 die->die_mark = 2;
13955 /* Walk them. */
13956 for (c = die->die_child; c; c = c->die_sib)
13958 /* If this is an array type, we need to make sure our
13959 kids get marked, even if they're types. */
13960 if (die->die_tag == DW_TAG_array_type)
13961 prune_unused_types_mark (c, 1);
13962 else
13963 prune_unused_types_walk (c);
13969 /* Walk the tree DIE and mark types that we actually use. */
13971 static void
13972 prune_unused_types_walk (dw_die_ref die)
13974 dw_die_ref c;
13976 /* Don't do anything if this node is already marked. */
13977 if (die->die_mark)
13978 return;
13980 switch (die->die_tag) {
13981 case DW_TAG_const_type:
13982 case DW_TAG_packed_type:
13983 case DW_TAG_pointer_type:
13984 case DW_TAG_reference_type:
13985 case DW_TAG_volatile_type:
13986 case DW_TAG_typedef:
13987 case DW_TAG_array_type:
13988 case DW_TAG_structure_type:
13989 case DW_TAG_union_type:
13990 case DW_TAG_class_type:
13991 case DW_TAG_friend:
13992 case DW_TAG_variant_part:
13993 case DW_TAG_enumeration_type:
13994 case DW_TAG_subroutine_type:
13995 case DW_TAG_string_type:
13996 case DW_TAG_set_type:
13997 case DW_TAG_subrange_type:
13998 case DW_TAG_ptr_to_member_type:
13999 case DW_TAG_file_type:
14000 /* It's a type node --- don't mark it. */
14001 return;
14003 default:
14004 /* Mark everything else. */
14005 break;
14008 die->die_mark = 1;
14010 /* Now, mark any dies referenced from here. */
14011 prune_unused_types_walk_attribs (die);
14013 /* Mark children. */
14014 for (c = die->die_child; c; c = c->die_sib)
14015 prune_unused_types_walk (c);
14019 /* Remove from the tree DIE any dies that aren't marked. */
14021 static void
14022 prune_unused_types_prune (dw_die_ref die)
14024 dw_die_ref c, p, n;
14026 gcc_assert (die->die_mark);
14028 p = NULL;
14029 for (c = die->die_child; c; c = n)
14031 n = c->die_sib;
14032 if (c->die_mark)
14034 prune_unused_types_prune (c);
14035 p = c;
14037 else
14039 if (p)
14040 p->die_sib = n;
14041 else
14042 die->die_child = n;
14043 free_die (c);
14049 /* Remove dies representing declarations that we never use. */
14051 static void
14052 prune_unused_types (void)
14054 unsigned int i;
14055 limbo_die_node *node;
14057 /* Clear all the marks. */
14058 prune_unmark_dies (comp_unit_die);
14059 for (node = limbo_die_list; node; node = node->next)
14060 prune_unmark_dies (node->die);
14062 /* Set the mark on nodes that are actually used. */
14063 prune_unused_types_walk (comp_unit_die);
14064 for (node = limbo_die_list; node; node = node->next)
14065 prune_unused_types_walk (node->die);
14067 /* Also set the mark on nodes referenced from the
14068 pubname_table or arange_table. */
14069 for (i = 0; i < pubname_table_in_use; i++)
14070 prune_unused_types_mark (pubname_table[i].die, 1);
14071 for (i = 0; i < arange_table_in_use; i++)
14072 prune_unused_types_mark (arange_table[i], 1);
14074 /* Get rid of nodes that aren't marked. */
14075 prune_unused_types_prune (comp_unit_die);
14076 for (node = limbo_die_list; node; node = node->next)
14077 prune_unused_types_prune (node->die);
14079 /* Leave the marks clear. */
14080 prune_unmark_dies (comp_unit_die);
14081 for (node = limbo_die_list; node; node = node->next)
14082 prune_unmark_dies (node->die);
14085 /* Output stuff that dwarf requires at the end of every file,
14086 and generate the DWARF-2 debugging info. */
14088 static void
14089 dwarf2out_finish (const char *filename)
14091 limbo_die_node *node, *next_node;
14092 dw_die_ref die = 0;
14094 /* Add the name for the main input file now. We delayed this from
14095 dwarf2out_init to avoid complications with PCH. */
14096 add_name_attribute (comp_unit_die, filename);
14097 if (filename[0] != DIR_SEPARATOR)
14098 add_comp_dir_attribute (comp_unit_die);
14099 else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
14101 size_t i;
14102 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
14103 if (VARRAY_CHAR_PTR (file_table, i)[0] != DIR_SEPARATOR
14104 /* Don't add cwd for <built-in>. */
14105 && VARRAY_CHAR_PTR (file_table, i)[0] != '<')
14107 add_comp_dir_attribute (comp_unit_die);
14108 break;
14112 /* Traverse the limbo die list, and add parent/child links. The only
14113 dies without parents that should be here are concrete instances of
14114 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
14115 For concrete instances, we can get the parent die from the abstract
14116 instance. */
14117 for (node = limbo_die_list; node; node = next_node)
14119 next_node = node->next;
14120 die = node->die;
14122 if (die->die_parent == NULL)
14124 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
14126 if (origin)
14127 add_child_die (origin->die_parent, die);
14128 else if (die == comp_unit_die)
14130 else if (errorcount > 0 || sorrycount > 0)
14131 /* It's OK to be confused by errors in the input. */
14132 add_child_die (comp_unit_die, die);
14133 else
14135 /* In certain situations, the lexical block containing a
14136 nested function can be optimized away, which results
14137 in the nested function die being orphaned. Likewise
14138 with the return type of that nested function. Force
14139 this to be a child of the containing function.
14141 It may happen that even the containing function got fully
14142 inlined and optimized out. In that case we are lost and
14143 assign the empty child. This should not be big issue as
14144 the function is likely unreachable too. */
14145 tree context = NULL_TREE;
14147 gcc_assert (node->created_for);
14149 if (DECL_P (node->created_for))
14150 context = DECL_CONTEXT (node->created_for);
14151 else if (TYPE_P (node->created_for))
14152 context = TYPE_CONTEXT (node->created_for);
14154 gcc_assert (context
14155 && (TREE_CODE (context) == FUNCTION_DECL
14156 || TREE_CODE (context) == NAMESPACE_DECL));
14158 origin = lookup_decl_die (context);
14159 if (origin)
14160 add_child_die (origin, die);
14161 else
14162 add_child_die (comp_unit_die, die);
14167 limbo_die_list = NULL;
14169 /* Walk through the list of incomplete types again, trying once more to
14170 emit full debugging info for them. */
14171 retry_incomplete_types ();
14173 /* We need to reverse all the dies before break_out_includes, or
14174 we'll see the end of an include file before the beginning. */
14175 reverse_all_dies (comp_unit_die);
14177 if (flag_eliminate_unused_debug_types)
14178 prune_unused_types ();
14180 /* Generate separate CUs for each of the include files we've seen.
14181 They will go into limbo_die_list. */
14182 if (flag_eliminate_dwarf2_dups)
14183 break_out_includes (comp_unit_die);
14185 /* Traverse the DIE's and add add sibling attributes to those DIE's
14186 that have children. */
14187 add_sibling_attributes (comp_unit_die);
14188 for (node = limbo_die_list; node; node = node->next)
14189 add_sibling_attributes (node->die);
14191 /* Output a terminator label for the .text section. */
14192 text_section ();
14193 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
14194 if (flag_reorder_blocks_and_partition)
14196 unlikely_text_section ();
14197 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
14200 /* Output the source line correspondence table. We must do this
14201 even if there is no line information. Otherwise, on an empty
14202 translation unit, we will generate a present, but empty,
14203 .debug_info section. IRIX 6.5 `nm' will then complain when
14204 examining the file. */
14205 if (! DWARF2_ASM_LINE_DEBUG_INFO)
14207 named_section_flags (DEBUG_LINE_SECTION, SECTION_DEBUG);
14208 output_line_info ();
14211 /* Output location list section if necessary. */
14212 if (have_location_lists)
14214 /* Output the location lists info. */
14215 named_section_flags (DEBUG_LOC_SECTION, SECTION_DEBUG);
14216 ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
14217 DEBUG_LOC_SECTION_LABEL, 0);
14218 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
14219 output_location_lists (die);
14220 have_location_lists = 0;
14223 /* We can only use the low/high_pc attributes if all of the code was
14224 in .text. */
14225 if (!separate_line_info_table_in_use && !have_switched_text_section)
14227 add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
14228 add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
14231 /* If it wasn't, we need to give .debug_loc and .debug_ranges an appropriate
14232 "base address". Use zero so that these addresses become absolute. */
14233 else if (have_location_lists || ranges_table_in_use)
14234 add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
14236 if (debug_info_level >= DINFO_LEVEL_NORMAL)
14237 add_AT_lbl_offset (comp_unit_die, DW_AT_stmt_list,
14238 debug_line_section_label);
14240 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14241 add_AT_lbl_offset (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
14243 /* Output all of the compilation units. We put the main one last so that
14244 the offsets are available to output_pubnames. */
14245 for (node = limbo_die_list; node; node = node->next)
14246 output_comp_unit (node->die, 0);
14248 output_comp_unit (comp_unit_die, 0);
14250 /* Output the abbreviation table. */
14251 named_section_flags (DEBUG_ABBREV_SECTION, SECTION_DEBUG);
14252 output_abbrev_section ();
14254 /* Output public names table if necessary. */
14255 if (pubname_table_in_use)
14257 named_section_flags (DEBUG_PUBNAMES_SECTION, SECTION_DEBUG);
14258 output_pubnames ();
14261 /* Output the address range information. We only put functions in the arange
14262 table, so don't write it out if we don't have any. */
14263 if (fde_table_in_use)
14265 named_section_flags (DEBUG_ARANGES_SECTION, SECTION_DEBUG);
14266 output_aranges ();
14269 /* Output ranges section if necessary. */
14270 if (ranges_table_in_use)
14272 named_section_flags (DEBUG_RANGES_SECTION, SECTION_DEBUG);
14273 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
14274 output_ranges ();
14277 /* Have to end the macro section. */
14278 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14280 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
14281 dw2_asm_output_data (1, 0, "End compilation unit");
14284 /* If we emitted any DW_FORM_strp form attribute, output the string
14285 table too. */
14286 if (debug_str_hash)
14287 htab_traverse (debug_str_hash, output_indirect_string, NULL);
14289 #else
14291 /* This should never be used, but its address is needed for comparisons. */
14292 const struct gcc_debug_hooks dwarf2_debug_hooks;
14294 #endif /* DWARF2_DEBUGGING_INFO */
14296 #include "gt-dwarf2out.h"