* dwarf2out.c (premark_used_types): Remove problematic prototype.
[official-gcc.git] / gcc / dwarf2out.c
blobe3cb00932e7fc100c1c1ee7876938971227a00e7
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
4 Contributed by Gary Funck (gary@intrepid.com).
5 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
6 Extensively modified by Jason Merrill (jason@cygnus.com).
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 2, or (at your option) any later
13 version.
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING. If not, write to the Free
22 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
23 02110-1301, USA. */
25 /* TODO: Emit .debug_line header even when there are no functions, since
26 the file numbers are used by .debug_info. Alternately, leave
27 out locations for types and decls.
28 Avoid talking about ctors and op= for PODs.
29 Factor out common prologue sequences into multiple CIEs. */
31 /* The first part of this file deals with the DWARF 2 frame unwind
32 information, which is also used by the GCC efficient exception handling
33 mechanism. The second part, controlled only by an #ifdef
34 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
35 information. */
37 #include "config.h"
38 #include "system.h"
39 #include "coretypes.h"
40 #include "tm.h"
41 #include "tree.h"
42 #include "version.h"
43 #include "flags.h"
44 #include "real.h"
45 #include "rtl.h"
46 #include "hard-reg-set.h"
47 #include "regs.h"
48 #include "insn-config.h"
49 #include "reload.h"
50 #include "function.h"
51 #include "output.h"
52 #include "expr.h"
53 #include "libfuncs.h"
54 #include "except.h"
55 #include "dwarf2.h"
56 #include "dwarf2out.h"
57 #include "dwarf2asm.h"
58 #include "toplev.h"
59 #include "varray.h"
60 #include "ggc.h"
61 #include "md5.h"
62 #include "tm_p.h"
63 #include "diagnostic.h"
64 #include "debug.h"
65 #include "target.h"
66 #include "langhooks.h"
67 #include "hashtab.h"
68 #include "cgraph.h"
69 #include "input.h"
71 #ifdef DWARF2_DEBUGGING_INFO
72 static void dwarf2out_source_line (unsigned int, const char *);
73 #endif
75 /* DWARF2 Abbreviation Glossary:
76 CFA = Canonical Frame Address
77 a fixed address on the stack which identifies a call frame.
78 We define it to be the value of SP just before the call insn.
79 The CFA register and offset, which may change during the course
80 of the function, are used to calculate its value at runtime.
81 CFI = Call Frame Instruction
82 an instruction for the DWARF2 abstract machine
83 CIE = Common Information Entry
84 information describing information common to one or more FDEs
85 DIE = Debugging Information Entry
86 FDE = Frame Description Entry
87 information describing the stack call frame, in particular,
88 how to restore registers
90 DW_CFA_... = DWARF2 CFA call frame instruction
91 DW_TAG_... = DWARF2 DIE tag */
93 #ifndef DWARF2_FRAME_INFO
94 # ifdef DWARF2_DEBUGGING_INFO
95 # define DWARF2_FRAME_INFO \
96 (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
97 # else
98 # define DWARF2_FRAME_INFO 0
99 # endif
100 #endif
102 /* Map register numbers held in the call frame info that gcc has
103 collected using DWARF_FRAME_REGNUM to those that should be output in
104 .debug_frame and .eh_frame. */
105 #ifndef DWARF2_FRAME_REG_OUT
106 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
107 #endif
109 /* Decide whether we want to emit frame unwind information for the current
110 translation unit. */
113 dwarf2out_do_frame (void)
115 /* We want to emit correct CFA location expressions or lists, so we
116 have to return true if we're going to output debug info, even if
117 we're not going to output frame or unwind info. */
118 return (write_symbols == DWARF2_DEBUG
119 || write_symbols == VMS_AND_DWARF2_DEBUG
120 || DWARF2_FRAME_INFO
121 #ifdef DWARF2_UNWIND_INFO
122 || (DWARF2_UNWIND_INFO
123 && (flag_unwind_tables
124 || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)))
125 #endif
129 /* The size of the target's pointer type. */
130 #ifndef PTR_SIZE
131 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
132 #endif
134 /* Array of RTXes referenced by the debugging information, which therefore
135 must be kept around forever. */
136 static GTY(()) VEC(rtx,gc) *used_rtx_array;
138 /* A pointer to the base of a list of incomplete types which might be
139 completed at some later time. incomplete_types_list needs to be a
140 VEC(tree,gc) because we want to tell the garbage collector about
141 it. */
142 static GTY(()) VEC(tree,gc) *incomplete_types;
144 /* A pointer to the base of a table of references to declaration
145 scopes. This table is a display which tracks the nesting
146 of declaration scopes at the current scope and containing
147 scopes. This table is used to find the proper place to
148 define type declaration DIE's. */
149 static GTY(()) VEC(tree,gc) *decl_scope_table;
151 /* Pointers to various DWARF2 sections. */
152 static GTY(()) section *debug_info_section;
153 static GTY(()) section *debug_abbrev_section;
154 static GTY(()) section *debug_aranges_section;
155 static GTY(()) section *debug_macinfo_section;
156 static GTY(()) section *debug_line_section;
157 static GTY(()) section *debug_loc_section;
158 static GTY(()) section *debug_pubnames_section;
159 static GTY(()) section *debug_str_section;
160 static GTY(()) section *debug_ranges_section;
161 static GTY(()) section *debug_frame_section;
163 /* How to start an assembler comment. */
164 #ifndef ASM_COMMENT_START
165 #define ASM_COMMENT_START ";#"
166 #endif
168 typedef struct dw_cfi_struct *dw_cfi_ref;
169 typedef struct dw_fde_struct *dw_fde_ref;
170 typedef union dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
172 /* Call frames are described using a sequence of Call Frame
173 Information instructions. The register number, offset
174 and address fields are provided as possible operands;
175 their use is selected by the opcode field. */
177 enum dw_cfi_oprnd_type {
178 dw_cfi_oprnd_unused,
179 dw_cfi_oprnd_reg_num,
180 dw_cfi_oprnd_offset,
181 dw_cfi_oprnd_addr,
182 dw_cfi_oprnd_loc
185 typedef union dw_cfi_oprnd_struct GTY(())
187 unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
188 HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
189 const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
190 struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
192 dw_cfi_oprnd;
194 typedef struct dw_cfi_struct GTY(())
196 dw_cfi_ref dw_cfi_next;
197 enum dwarf_call_frame_info dw_cfi_opc;
198 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
199 dw_cfi_oprnd1;
200 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
201 dw_cfi_oprnd2;
203 dw_cfi_node;
205 /* This is how we define the location of the CFA. We use to handle it
206 as REG + OFFSET all the time, but now it can be more complex.
207 It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
208 Instead of passing around REG and OFFSET, we pass a copy
209 of this structure. */
210 typedef struct cfa_loc GTY(())
212 HOST_WIDE_INT offset;
213 HOST_WIDE_INT base_offset;
214 unsigned int reg;
215 int indirect; /* 1 if CFA is accessed via a dereference. */
216 } dw_cfa_location;
218 /* All call frame descriptions (FDE's) in the GCC generated DWARF
219 refer to a single Common Information Entry (CIE), defined at
220 the beginning of the .debug_frame section. This use of a single
221 CIE obviates the need to keep track of multiple CIE's
222 in the DWARF generation routines below. */
224 typedef struct dw_fde_struct GTY(())
226 tree decl;
227 const char *dw_fde_begin;
228 const char *dw_fde_current_label;
229 const char *dw_fde_end;
230 const char *dw_fde_hot_section_label;
231 const char *dw_fde_hot_section_end_label;
232 const char *dw_fde_unlikely_section_label;
233 const char *dw_fde_unlikely_section_end_label;
234 bool dw_fde_switched_sections;
235 dw_cfi_ref dw_fde_cfi;
236 unsigned funcdef_number;
237 unsigned all_throwers_are_sibcalls : 1;
238 unsigned nothrow : 1;
239 unsigned uses_eh_lsda : 1;
241 dw_fde_node;
243 /* Maximum size (in bytes) of an artificially generated label. */
244 #define MAX_ARTIFICIAL_LABEL_BYTES 30
246 /* The size of addresses as they appear in the Dwarf 2 data.
247 Some architectures use word addresses to refer to code locations,
248 but Dwarf 2 info always uses byte addresses. On such machines,
249 Dwarf 2 addresses need to be larger than the architecture's
250 pointers. */
251 #ifndef DWARF2_ADDR_SIZE
252 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
253 #endif
255 /* The size in bytes of a DWARF field indicating an offset or length
256 relative to a debug info section, specified to be 4 bytes in the
257 DWARF-2 specification. The SGI/MIPS ABI defines it to be the same
258 as PTR_SIZE. */
260 #ifndef DWARF_OFFSET_SIZE
261 #define DWARF_OFFSET_SIZE 4
262 #endif
264 /* According to the (draft) DWARF 3 specification, the initial length
265 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
266 bytes are 0xffffffff, followed by the length stored in the next 8
267 bytes.
269 However, the SGI/MIPS ABI uses an initial length which is equal to
270 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
272 #ifndef DWARF_INITIAL_LENGTH_SIZE
273 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
274 #endif
276 #define DWARF_VERSION 2
278 /* Round SIZE up to the nearest BOUNDARY. */
279 #define DWARF_ROUND(SIZE,BOUNDARY) \
280 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
282 /* Offsets recorded in opcodes are a multiple of this alignment factor. */
283 #ifndef DWARF_CIE_DATA_ALIGNMENT
284 #ifdef STACK_GROWS_DOWNWARD
285 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
286 #else
287 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
288 #endif
289 #endif
291 /* CIE identifier. */
292 #if HOST_BITS_PER_WIDE_INT >= 64
293 #define DWARF_CIE_ID \
294 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
295 #else
296 #define DWARF_CIE_ID DW_CIE_ID
297 #endif
299 /* A pointer to the base of a table that contains frame description
300 information for each routine. */
301 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
303 /* Number of elements currently allocated for fde_table. */
304 static GTY(()) unsigned fde_table_allocated;
306 /* Number of elements in fde_table currently in use. */
307 static GTY(()) unsigned fde_table_in_use;
309 /* Size (in elements) of increments by which we may expand the
310 fde_table. */
311 #define FDE_TABLE_INCREMENT 256
313 /* A list of call frame insns for the CIE. */
314 static GTY(()) dw_cfi_ref cie_cfi_head;
316 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
317 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
318 attribute that accelerates the lookup of the FDE associated
319 with the subprogram. This variable holds the table index of the FDE
320 associated with the current function (body) definition. */
321 static unsigned current_funcdef_fde;
322 #endif
324 struct indirect_string_node GTY(())
326 const char *str;
327 unsigned int refcount;
328 unsigned int form;
329 char *label;
332 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
334 static GTY(()) int dw2_string_counter;
335 static GTY(()) unsigned long dwarf2out_cfi_label_num;
337 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
339 /* Forward declarations for functions defined in this file. */
341 static char *stripattributes (const char *);
342 static const char *dwarf_cfi_name (unsigned);
343 static dw_cfi_ref new_cfi (void);
344 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
345 static void add_fde_cfi (const char *, dw_cfi_ref);
346 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *);
347 static void lookup_cfa (dw_cfa_location *);
348 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
349 static void initial_return_save (rtx);
350 static HOST_WIDE_INT stack_adjust_offset (rtx);
351 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
352 static void output_call_frame_info (int);
353 static void dwarf2out_stack_adjust (rtx, bool);
354 static void flush_queued_reg_saves (void);
355 static bool clobbers_queued_reg_save (rtx);
356 static void dwarf2out_frame_debug_expr (rtx, const char *);
358 /* Support for complex CFA locations. */
359 static void output_cfa_loc (dw_cfi_ref);
360 static void get_cfa_from_loc_descr (dw_cfa_location *,
361 struct dw_loc_descr_struct *);
362 static struct dw_loc_descr_struct *build_cfa_loc
363 (dw_cfa_location *, HOST_WIDE_INT);
364 static void def_cfa_1 (const char *, dw_cfa_location *);
366 /* How to start an assembler comment. */
367 #ifndef ASM_COMMENT_START
368 #define ASM_COMMENT_START ";#"
369 #endif
371 /* Data and reference forms for relocatable data. */
372 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
373 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
375 #ifndef DEBUG_FRAME_SECTION
376 #define DEBUG_FRAME_SECTION ".debug_frame"
377 #endif
379 #ifndef FUNC_BEGIN_LABEL
380 #define FUNC_BEGIN_LABEL "LFB"
381 #endif
383 #ifndef FUNC_END_LABEL
384 #define FUNC_END_LABEL "LFE"
385 #endif
387 #ifndef FRAME_BEGIN_LABEL
388 #define FRAME_BEGIN_LABEL "Lframe"
389 #endif
390 #define CIE_AFTER_SIZE_LABEL "LSCIE"
391 #define CIE_END_LABEL "LECIE"
392 #define FDE_LABEL "LSFDE"
393 #define FDE_AFTER_SIZE_LABEL "LASFDE"
394 #define FDE_END_LABEL "LEFDE"
395 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
396 #define LINE_NUMBER_END_LABEL "LELT"
397 #define LN_PROLOG_AS_LABEL "LASLTP"
398 #define LN_PROLOG_END_LABEL "LELTP"
399 #define DIE_LABEL_PREFIX "DW"
401 /* The DWARF 2 CFA column which tracks the return address. Normally this
402 is the column for PC, or the first column after all of the hard
403 registers. */
404 #ifndef DWARF_FRAME_RETURN_COLUMN
405 #ifdef PC_REGNUM
406 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGNUM (PC_REGNUM)
407 #else
408 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGISTERS
409 #endif
410 #endif
412 /* The mapping from gcc register number to DWARF 2 CFA column number. By
413 default, we just provide columns for all registers. */
414 #ifndef DWARF_FRAME_REGNUM
415 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
416 #endif
418 /* Hook used by __throw. */
421 expand_builtin_dwarf_sp_column (void)
423 unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
424 return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
427 /* Return a pointer to a copy of the section string name S with all
428 attributes stripped off, and an asterisk prepended (for assemble_name). */
430 static inline char *
431 stripattributes (const char *s)
433 char *stripped = XNEWVEC (char, strlen (s) + 2);
434 char *p = stripped;
436 *p++ = '*';
438 while (*s && *s != ',')
439 *p++ = *s++;
441 *p = '\0';
442 return stripped;
445 /* Generate code to initialize the register size table. */
447 void
448 expand_builtin_init_dwarf_reg_sizes (tree address)
450 unsigned int i;
451 enum machine_mode mode = TYPE_MODE (char_type_node);
452 rtx addr = expand_normal (address);
453 rtx mem = gen_rtx_MEM (BLKmode, addr);
454 bool wrote_return_column = false;
456 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
458 int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
460 if (rnum < DWARF_FRAME_REGISTERS)
462 HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
463 enum machine_mode save_mode = reg_raw_mode[i];
464 HOST_WIDE_INT size;
466 if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
467 save_mode = choose_hard_reg_mode (i, 1, true);
468 if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
470 if (save_mode == VOIDmode)
471 continue;
472 wrote_return_column = true;
474 size = GET_MODE_SIZE (save_mode);
475 if (offset < 0)
476 continue;
478 emit_move_insn (adjust_address (mem, mode, offset),
479 gen_int_mode (size, mode));
483 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
484 gcc_assert (wrote_return_column);
485 i = DWARF_ALT_FRAME_RETURN_COLUMN;
486 wrote_return_column = false;
487 #else
488 i = DWARF_FRAME_RETURN_COLUMN;
489 #endif
491 if (! wrote_return_column)
493 enum machine_mode save_mode = Pmode;
494 HOST_WIDE_INT offset = i * GET_MODE_SIZE (mode);
495 HOST_WIDE_INT size = GET_MODE_SIZE (save_mode);
496 emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
500 /* Convert a DWARF call frame info. operation to its string name */
502 static const char *
503 dwarf_cfi_name (unsigned int cfi_opc)
505 switch (cfi_opc)
507 case DW_CFA_advance_loc:
508 return "DW_CFA_advance_loc";
509 case DW_CFA_offset:
510 return "DW_CFA_offset";
511 case DW_CFA_restore:
512 return "DW_CFA_restore";
513 case DW_CFA_nop:
514 return "DW_CFA_nop";
515 case DW_CFA_set_loc:
516 return "DW_CFA_set_loc";
517 case DW_CFA_advance_loc1:
518 return "DW_CFA_advance_loc1";
519 case DW_CFA_advance_loc2:
520 return "DW_CFA_advance_loc2";
521 case DW_CFA_advance_loc4:
522 return "DW_CFA_advance_loc4";
523 case DW_CFA_offset_extended:
524 return "DW_CFA_offset_extended";
525 case DW_CFA_restore_extended:
526 return "DW_CFA_restore_extended";
527 case DW_CFA_undefined:
528 return "DW_CFA_undefined";
529 case DW_CFA_same_value:
530 return "DW_CFA_same_value";
531 case DW_CFA_register:
532 return "DW_CFA_register";
533 case DW_CFA_remember_state:
534 return "DW_CFA_remember_state";
535 case DW_CFA_restore_state:
536 return "DW_CFA_restore_state";
537 case DW_CFA_def_cfa:
538 return "DW_CFA_def_cfa";
539 case DW_CFA_def_cfa_register:
540 return "DW_CFA_def_cfa_register";
541 case DW_CFA_def_cfa_offset:
542 return "DW_CFA_def_cfa_offset";
544 /* DWARF 3 */
545 case DW_CFA_def_cfa_expression:
546 return "DW_CFA_def_cfa_expression";
547 case DW_CFA_expression:
548 return "DW_CFA_expression";
549 case DW_CFA_offset_extended_sf:
550 return "DW_CFA_offset_extended_sf";
551 case DW_CFA_def_cfa_sf:
552 return "DW_CFA_def_cfa_sf";
553 case DW_CFA_def_cfa_offset_sf:
554 return "DW_CFA_def_cfa_offset_sf";
556 /* SGI/MIPS specific */
557 case DW_CFA_MIPS_advance_loc8:
558 return "DW_CFA_MIPS_advance_loc8";
560 /* GNU extensions */
561 case DW_CFA_GNU_window_save:
562 return "DW_CFA_GNU_window_save";
563 case DW_CFA_GNU_args_size:
564 return "DW_CFA_GNU_args_size";
565 case DW_CFA_GNU_negative_offset_extended:
566 return "DW_CFA_GNU_negative_offset_extended";
568 default:
569 return "DW_CFA_<unknown>";
573 /* Return a pointer to a newly allocated Call Frame Instruction. */
575 static inline dw_cfi_ref
576 new_cfi (void)
578 dw_cfi_ref cfi = ggc_alloc (sizeof (dw_cfi_node));
580 cfi->dw_cfi_next = NULL;
581 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
582 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
584 return cfi;
587 /* Add a Call Frame Instruction to list of instructions. */
589 static inline void
590 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
592 dw_cfi_ref *p;
594 /* Find the end of the chain. */
595 for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
598 *p = cfi;
601 /* Generate a new label for the CFI info to refer to. */
603 char *
604 dwarf2out_cfi_label (void)
606 static char label[20];
608 ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
609 ASM_OUTPUT_LABEL (asm_out_file, label);
610 return label;
613 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
614 or to the CIE if LABEL is NULL. */
616 static void
617 add_fde_cfi (const char *label, dw_cfi_ref cfi)
619 if (label)
621 dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
623 if (*label == 0)
624 label = dwarf2out_cfi_label ();
626 if (fde->dw_fde_current_label == NULL
627 || strcmp (label, fde->dw_fde_current_label) != 0)
629 dw_cfi_ref xcfi;
631 fde->dw_fde_current_label = label = xstrdup (label);
633 /* Set the location counter to the new label. */
634 xcfi = new_cfi ();
635 xcfi->dw_cfi_opc = DW_CFA_advance_loc4;
636 xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
637 add_cfi (&fde->dw_fde_cfi, xcfi);
640 add_cfi (&fde->dw_fde_cfi, cfi);
643 else
644 add_cfi (&cie_cfi_head, cfi);
647 /* Subroutine of lookup_cfa. */
649 static void
650 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc)
652 switch (cfi->dw_cfi_opc)
654 case DW_CFA_def_cfa_offset:
655 loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
656 break;
657 case DW_CFA_def_cfa_offset_sf:
658 loc->offset
659 = cfi->dw_cfi_oprnd1.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
660 break;
661 case DW_CFA_def_cfa_register:
662 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
663 break;
664 case DW_CFA_def_cfa:
665 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
666 loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
667 break;
668 case DW_CFA_def_cfa_sf:
669 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
670 loc->offset
671 = cfi->dw_cfi_oprnd2.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
672 break;
673 case DW_CFA_def_cfa_expression:
674 get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
675 break;
676 default:
677 break;
681 /* Find the previous value for the CFA. */
683 static void
684 lookup_cfa (dw_cfa_location *loc)
686 dw_cfi_ref cfi;
688 loc->reg = INVALID_REGNUM;
689 loc->offset = 0;
690 loc->indirect = 0;
691 loc->base_offset = 0;
693 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
694 lookup_cfa_1 (cfi, loc);
696 if (fde_table_in_use)
698 dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
699 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
700 lookup_cfa_1 (cfi, loc);
704 /* The current rule for calculating the DWARF2 canonical frame address. */
705 static dw_cfa_location cfa;
707 /* The register used for saving registers to the stack, and its offset
708 from the CFA. */
709 static dw_cfa_location cfa_store;
711 /* The running total of the size of arguments pushed onto the stack. */
712 static HOST_WIDE_INT args_size;
714 /* The last args_size we actually output. */
715 static HOST_WIDE_INT old_args_size;
717 /* Entry point to update the canonical frame address (CFA).
718 LABEL is passed to add_fde_cfi. The value of CFA is now to be
719 calculated from REG+OFFSET. */
721 void
722 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
724 dw_cfa_location loc;
725 loc.indirect = 0;
726 loc.base_offset = 0;
727 loc.reg = reg;
728 loc.offset = offset;
729 def_cfa_1 (label, &loc);
732 /* Determine if two dw_cfa_location structures define the same data. */
734 static bool
735 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
737 return (loc1->reg == loc2->reg
738 && loc1->offset == loc2->offset
739 && loc1->indirect == loc2->indirect
740 && (loc1->indirect == 0
741 || loc1->base_offset == loc2->base_offset));
744 /* This routine does the actual work. The CFA is now calculated from
745 the dw_cfa_location structure. */
747 static void
748 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
750 dw_cfi_ref cfi;
751 dw_cfa_location old_cfa, loc;
753 cfa = *loc_p;
754 loc = *loc_p;
756 if (cfa_store.reg == loc.reg && loc.indirect == 0)
757 cfa_store.offset = loc.offset;
759 loc.reg = DWARF_FRAME_REGNUM (loc.reg);
760 lookup_cfa (&old_cfa);
762 /* If nothing changed, no need to issue any call frame instructions. */
763 if (cfa_equal_p (&loc, &old_cfa))
764 return;
766 cfi = new_cfi ();
768 if (loc.reg == old_cfa.reg && !loc.indirect)
770 /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
771 the CFA register did not change but the offset did. */
772 if (loc.offset < 0)
774 HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
775 gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
777 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
778 cfi->dw_cfi_oprnd1.dw_cfi_offset = f_offset;
780 else
782 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
783 cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
787 #ifndef MIPS_DEBUGGING_INFO /* SGI dbx thinks this means no offset. */
788 else if (loc.offset == old_cfa.offset
789 && old_cfa.reg != INVALID_REGNUM
790 && !loc.indirect)
792 /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
793 indicating the CFA register has changed to <register> but the
794 offset has not changed. */
795 cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
796 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
798 #endif
800 else if (loc.indirect == 0)
802 /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
803 indicating the CFA register has changed to <register> with
804 the specified offset. */
805 if (loc.offset < 0)
807 HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
808 gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
810 cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
811 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
812 cfi->dw_cfi_oprnd2.dw_cfi_offset = f_offset;
814 else
816 cfi->dw_cfi_opc = DW_CFA_def_cfa;
817 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
818 cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
821 else
823 /* Construct a DW_CFA_def_cfa_expression instruction to
824 calculate the CFA using a full location expression since no
825 register-offset pair is available. */
826 struct dw_loc_descr_struct *loc_list;
828 cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
829 loc_list = build_cfa_loc (&loc, 0);
830 cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
833 add_fde_cfi (label, cfi);
836 /* Add the CFI for saving a register. REG is the CFA column number.
837 LABEL is passed to add_fde_cfi.
838 If SREG is -1, the register is saved at OFFSET from the CFA;
839 otherwise it is saved in SREG. */
841 static void
842 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
844 dw_cfi_ref cfi = new_cfi ();
846 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
848 if (sreg == INVALID_REGNUM)
850 if (reg & ~0x3f)
851 /* The register number won't fit in 6 bits, so we have to use
852 the long form. */
853 cfi->dw_cfi_opc = DW_CFA_offset_extended;
854 else
855 cfi->dw_cfi_opc = DW_CFA_offset;
857 #ifdef ENABLE_CHECKING
859 /* If we get an offset that is not a multiple of
860 DWARF_CIE_DATA_ALIGNMENT, there is either a bug in the
861 definition of DWARF_CIE_DATA_ALIGNMENT, or a bug in the machine
862 description. */
863 HOST_WIDE_INT check_offset = offset / DWARF_CIE_DATA_ALIGNMENT;
865 gcc_assert (check_offset * DWARF_CIE_DATA_ALIGNMENT == offset);
867 #endif
868 offset /= DWARF_CIE_DATA_ALIGNMENT;
869 if (offset < 0)
870 cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
872 cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
874 else if (sreg == reg)
875 cfi->dw_cfi_opc = DW_CFA_same_value;
876 else
878 cfi->dw_cfi_opc = DW_CFA_register;
879 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
882 add_fde_cfi (label, cfi);
885 /* Add the CFI for saving a register window. LABEL is passed to reg_save.
886 This CFI tells the unwinder that it needs to restore the window registers
887 from the previous frame's window save area.
889 ??? Perhaps we should note in the CIE where windows are saved (instead of
890 assuming 0(cfa)) and what registers are in the window. */
892 void
893 dwarf2out_window_save (const char *label)
895 dw_cfi_ref cfi = new_cfi ();
897 cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
898 add_fde_cfi (label, cfi);
901 /* Add a CFI to update the running total of the size of arguments
902 pushed onto the stack. */
904 void
905 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
907 dw_cfi_ref cfi;
909 if (size == old_args_size)
910 return;
912 old_args_size = size;
914 cfi = new_cfi ();
915 cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
916 cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
917 add_fde_cfi (label, cfi);
920 /* Entry point for saving a register to the stack. REG is the GCC register
921 number. LABEL and OFFSET are passed to reg_save. */
923 void
924 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
926 reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
929 /* Entry point for saving the return address in the stack.
930 LABEL and OFFSET are passed to reg_save. */
932 void
933 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
935 reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
938 /* Entry point for saving the return address in a register.
939 LABEL and SREG are passed to reg_save. */
941 void
942 dwarf2out_return_reg (const char *label, unsigned int sreg)
944 reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
947 /* Record the initial position of the return address. RTL is
948 INCOMING_RETURN_ADDR_RTX. */
950 static void
951 initial_return_save (rtx rtl)
953 unsigned int reg = INVALID_REGNUM;
954 HOST_WIDE_INT offset = 0;
956 switch (GET_CODE (rtl))
958 case REG:
959 /* RA is in a register. */
960 reg = DWARF_FRAME_REGNUM (REGNO (rtl));
961 break;
963 case MEM:
964 /* RA is on the stack. */
965 rtl = XEXP (rtl, 0);
966 switch (GET_CODE (rtl))
968 case REG:
969 gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
970 offset = 0;
971 break;
973 case PLUS:
974 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
975 offset = INTVAL (XEXP (rtl, 1));
976 break;
978 case MINUS:
979 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
980 offset = -INTVAL (XEXP (rtl, 1));
981 break;
983 default:
984 gcc_unreachable ();
987 break;
989 case PLUS:
990 /* The return address is at some offset from any value we can
991 actually load. For instance, on the SPARC it is in %i7+8. Just
992 ignore the offset for now; it doesn't matter for unwinding frames. */
993 gcc_assert (GET_CODE (XEXP (rtl, 1)) == CONST_INT);
994 initial_return_save (XEXP (rtl, 0));
995 return;
997 default:
998 gcc_unreachable ();
1001 if (reg != DWARF_FRAME_RETURN_COLUMN)
1002 reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1005 /* Given a SET, calculate the amount of stack adjustment it
1006 contains. */
1008 static HOST_WIDE_INT
1009 stack_adjust_offset (rtx pattern)
1011 rtx src = SET_SRC (pattern);
1012 rtx dest = SET_DEST (pattern);
1013 HOST_WIDE_INT offset = 0;
1014 enum rtx_code code;
1016 if (dest == stack_pointer_rtx)
1018 /* (set (reg sp) (plus (reg sp) (const_int))) */
1019 code = GET_CODE (src);
1020 if (! (code == PLUS || code == MINUS)
1021 || XEXP (src, 0) != stack_pointer_rtx
1022 || GET_CODE (XEXP (src, 1)) != CONST_INT)
1023 return 0;
1025 offset = INTVAL (XEXP (src, 1));
1026 if (code == PLUS)
1027 offset = -offset;
1029 else if (MEM_P (dest))
1031 /* (set (mem (pre_dec (reg sp))) (foo)) */
1032 src = XEXP (dest, 0);
1033 code = GET_CODE (src);
1035 switch (code)
1037 case PRE_MODIFY:
1038 case POST_MODIFY:
1039 if (XEXP (src, 0) == stack_pointer_rtx)
1041 rtx val = XEXP (XEXP (src, 1), 1);
1042 /* We handle only adjustments by constant amount. */
1043 gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1044 && GET_CODE (val) == CONST_INT);
1045 offset = -INTVAL (val);
1046 break;
1048 return 0;
1050 case PRE_DEC:
1051 case POST_DEC:
1052 if (XEXP (src, 0) == stack_pointer_rtx)
1054 offset = GET_MODE_SIZE (GET_MODE (dest));
1055 break;
1057 return 0;
1059 case PRE_INC:
1060 case POST_INC:
1061 if (XEXP (src, 0) == stack_pointer_rtx)
1063 offset = -GET_MODE_SIZE (GET_MODE (dest));
1064 break;
1066 return 0;
1068 default:
1069 return 0;
1072 else
1073 return 0;
1075 return offset;
1078 /* Check INSN to see if it looks like a push or a stack adjustment, and
1079 make a note of it if it does. EH uses this information to find out how
1080 much extra space it needs to pop off the stack. */
1082 static void
1083 dwarf2out_stack_adjust (rtx insn, bool after_p ATTRIBUTE_UNUSED)
1085 HOST_WIDE_INT offset;
1086 const char *label;
1087 int i;
1089 /* Don't handle epilogues at all. Certainly it would be wrong to do so
1090 with this function. Proper support would require all frame-related
1091 insns to be marked, and to be able to handle saving state around
1092 epilogues textually in the middle of the function. */
1093 if (prologue_epilogue_contains (insn) || sibcall_epilogue_contains (insn))
1094 return;
1096 if (BARRIER_P (insn))
1098 /* When we see a BARRIER, we know to reset args_size to 0. Usually
1099 the compiler will have already emitted a stack adjustment, but
1100 doesn't bother for calls to noreturn functions. */
1101 #ifdef STACK_GROWS_DOWNWARD
1102 offset = -args_size;
1103 #else
1104 offset = args_size;
1105 #endif
1107 else if (GET_CODE (PATTERN (insn)) == SET)
1108 offset = stack_adjust_offset (PATTERN (insn));
1109 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1110 || GET_CODE (PATTERN (insn)) == SEQUENCE)
1112 /* There may be stack adjustments inside compound insns. Search
1113 for them. */
1114 for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1115 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1116 offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i));
1118 else if (GET_CODE (insn) == CALL_INSN)
1119 offset = 0;
1120 else
1121 return;
1123 /* We handle this separately because we want stack adjustments in a
1124 CALL_INSN to be handled. */;
1125 if (GET_CODE (insn) == CALL_INSN)
1127 /* If only calls can throw, adjust args_size only at call sites. */
1128 if (!flag_asynchronous_unwind_tables)
1129 dwarf2out_args_size ("", args_size);
1132 if (offset == 0)
1133 return;
1135 if (cfa.reg == STACK_POINTER_REGNUM)
1136 cfa.offset += offset;
1138 #ifndef STACK_GROWS_DOWNWARD
1139 offset = -offset;
1140 #endif
1142 args_size += offset;
1143 if (args_size < 0)
1144 args_size = 0;
1146 /* If only calls can throw and we have a frame pointer, we'll save
1147 up adjustments until we see the CALL_INSN. We used to return
1148 early and derive args_size from NARGS in the CALL_INSN itself,
1149 but that doesn't compute the right value if we have nested call
1150 expansions, e.g., stack adjustments for a call have already been
1151 emitted, and then we issue another call to compute an argument
1152 for the enclosing call (i.e., bar (foo ())). */
1153 if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1154 return;
1156 label = dwarf2out_cfi_label ();
1157 def_cfa_1 (label, &cfa);
1158 if (flag_asynchronous_unwind_tables)
1159 dwarf2out_args_size (label, args_size);
1162 #endif
1164 /* We delay emitting a register save until either (a) we reach the end
1165 of the prologue or (b) the register is clobbered. This clusters
1166 register saves so that there are fewer pc advances. */
1168 struct queued_reg_save GTY(())
1170 struct queued_reg_save *next;
1171 rtx reg;
1172 HOST_WIDE_INT cfa_offset;
1173 rtx saved_reg;
1176 static GTY(()) struct queued_reg_save *queued_reg_saves;
1178 /* The caller's ORIG_REG is saved in SAVED_IN_REG. */
1179 struct reg_saved_in_data GTY(()) {
1180 rtx orig_reg;
1181 rtx saved_in_reg;
1184 /* A list of registers saved in other registers.
1185 The list intentionally has a small maximum capacity of 4; if your
1186 port needs more than that, you might consider implementing a
1187 more efficient data structure. */
1188 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1189 static GTY(()) size_t num_regs_saved_in_regs;
1191 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1192 static const char *last_reg_save_label;
1194 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1195 SREG, or if SREG is NULL then it is saved at OFFSET to the CFA. */
1197 static void
1198 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1200 struct queued_reg_save *q;
1202 /* Duplicates waste space, but it's also necessary to remove them
1203 for correctness, since the queue gets output in reverse
1204 order. */
1205 for (q = queued_reg_saves; q != NULL; q = q->next)
1206 if (REGNO (q->reg) == REGNO (reg))
1207 break;
1209 if (q == NULL)
1211 q = ggc_alloc (sizeof (*q));
1212 q->next = queued_reg_saves;
1213 queued_reg_saves = q;
1216 q->reg = reg;
1217 q->cfa_offset = offset;
1218 q->saved_reg = sreg;
1220 last_reg_save_label = label;
1223 /* Output all the entries in QUEUED_REG_SAVES. */
1225 static void
1226 flush_queued_reg_saves (void)
1228 struct queued_reg_save *q;
1230 for (q = queued_reg_saves; q; q = q->next)
1232 size_t i;
1233 unsigned int reg, sreg;
1235 for (i = 0; i < num_regs_saved_in_regs; i++)
1236 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1237 break;
1238 if (q->saved_reg && i == num_regs_saved_in_regs)
1240 gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1241 num_regs_saved_in_regs++;
1243 if (i != num_regs_saved_in_regs)
1245 regs_saved_in_regs[i].orig_reg = q->reg;
1246 regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1249 reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1250 if (q->saved_reg)
1251 sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1252 else
1253 sreg = INVALID_REGNUM;
1254 reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1257 queued_reg_saves = NULL;
1258 last_reg_save_label = NULL;
1261 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1262 location for? Or, does it clobber a register which we've previously
1263 said that some other register is saved in, and for which we now
1264 have a new location for? */
1266 static bool
1267 clobbers_queued_reg_save (rtx insn)
1269 struct queued_reg_save *q;
1271 for (q = queued_reg_saves; q; q = q->next)
1273 size_t i;
1274 if (modified_in_p (q->reg, insn))
1275 return true;
1276 for (i = 0; i < num_regs_saved_in_regs; i++)
1277 if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1278 && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1279 return true;
1282 return false;
1285 /* Entry point for saving the first register into the second. */
1287 void
1288 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1290 size_t i;
1291 unsigned int regno, sregno;
1293 for (i = 0; i < num_regs_saved_in_regs; i++)
1294 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1295 break;
1296 if (i == num_regs_saved_in_regs)
1298 gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1299 num_regs_saved_in_regs++;
1301 regs_saved_in_regs[i].orig_reg = reg;
1302 regs_saved_in_regs[i].saved_in_reg = sreg;
1304 regno = DWARF_FRAME_REGNUM (REGNO (reg));
1305 sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1306 reg_save (label, regno, sregno, 0);
1309 /* What register, if any, is currently saved in REG? */
1311 static rtx
1312 reg_saved_in (rtx reg)
1314 unsigned int regn = REGNO (reg);
1315 size_t i;
1316 struct queued_reg_save *q;
1318 for (q = queued_reg_saves; q; q = q->next)
1319 if (q->saved_reg && regn == REGNO (q->saved_reg))
1320 return q->reg;
1322 for (i = 0; i < num_regs_saved_in_regs; i++)
1323 if (regs_saved_in_regs[i].saved_in_reg
1324 && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1325 return regs_saved_in_regs[i].orig_reg;
1327 return NULL_RTX;
1331 /* A temporary register holding an integral value used in adjusting SP
1332 or setting up the store_reg. The "offset" field holds the integer
1333 value, not an offset. */
1334 static dw_cfa_location cfa_temp;
1336 /* Record call frame debugging information for an expression EXPR,
1337 which either sets SP or FP (adjusting how we calculate the frame
1338 address) or saves a register to the stack or another register.
1339 LABEL indicates the address of EXPR.
1341 This function encodes a state machine mapping rtxes to actions on
1342 cfa, cfa_store, and cfa_temp.reg. We describe these rules so
1343 users need not read the source code.
1345 The High-Level Picture
1347 Changes in the register we use to calculate the CFA: Currently we
1348 assume that if you copy the CFA register into another register, we
1349 should take the other one as the new CFA register; this seems to
1350 work pretty well. If it's wrong for some target, it's simple
1351 enough not to set RTX_FRAME_RELATED_P on the insn in question.
1353 Changes in the register we use for saving registers to the stack:
1354 This is usually SP, but not always. Again, we deduce that if you
1355 copy SP into another register (and SP is not the CFA register),
1356 then the new register is the one we will be using for register
1357 saves. This also seems to work.
1359 Register saves: There's not much guesswork about this one; if
1360 RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1361 register save, and the register used to calculate the destination
1362 had better be the one we think we're using for this purpose.
1363 It's also assumed that a copy from a call-saved register to another
1364 register is saving that register if RTX_FRAME_RELATED_P is set on
1365 that instruction. If the copy is from a call-saved register to
1366 the *same* register, that means that the register is now the same
1367 value as in the caller.
1369 Except: If the register being saved is the CFA register, and the
1370 offset is nonzero, we are saving the CFA, so we assume we have to
1371 use DW_CFA_def_cfa_expression. If the offset is 0, we assume that
1372 the intent is to save the value of SP from the previous frame.
1374 In addition, if a register has previously been saved to a different
1375 register,
1377 Invariants / Summaries of Rules
1379 cfa current rule for calculating the CFA. It usually
1380 consists of a register and an offset.
1381 cfa_store register used by prologue code to save things to the stack
1382 cfa_store.offset is the offset from the value of
1383 cfa_store.reg to the actual CFA
1384 cfa_temp register holding an integral value. cfa_temp.offset
1385 stores the value, which will be used to adjust the
1386 stack pointer. cfa_temp is also used like cfa_store,
1387 to track stores to the stack via fp or a temp reg.
1389 Rules 1- 4: Setting a register's value to cfa.reg or an expression
1390 with cfa.reg as the first operand changes the cfa.reg and its
1391 cfa.offset. Rule 1 and 4 also set cfa_temp.reg and
1392 cfa_temp.offset.
1394 Rules 6- 9: Set a non-cfa.reg register value to a constant or an
1395 expression yielding a constant. This sets cfa_temp.reg
1396 and cfa_temp.offset.
1398 Rule 5: Create a new register cfa_store used to save items to the
1399 stack.
1401 Rules 10-14: Save a register to the stack. Define offset as the
1402 difference of the original location and cfa_store's
1403 location (or cfa_temp's location if cfa_temp is used).
1405 The Rules
1407 "{a,b}" indicates a choice of a xor b.
1408 "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1410 Rule 1:
1411 (set <reg1> <reg2>:cfa.reg)
1412 effects: cfa.reg = <reg1>
1413 cfa.offset unchanged
1414 cfa_temp.reg = <reg1>
1415 cfa_temp.offset = cfa.offset
1417 Rule 2:
1418 (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1419 {<const_int>,<reg>:cfa_temp.reg}))
1420 effects: cfa.reg = sp if fp used
1421 cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
1422 cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1423 if cfa_store.reg==sp
1425 Rule 3:
1426 (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
1427 effects: cfa.reg = fp
1428 cfa_offset += +/- <const_int>
1430 Rule 4:
1431 (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
1432 constraints: <reg1> != fp
1433 <reg1> != sp
1434 effects: cfa.reg = <reg1>
1435 cfa_temp.reg = <reg1>
1436 cfa_temp.offset = cfa.offset
1438 Rule 5:
1439 (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1440 constraints: <reg1> != fp
1441 <reg1> != sp
1442 effects: cfa_store.reg = <reg1>
1443 cfa_store.offset = cfa.offset - cfa_temp.offset
1445 Rule 6:
1446 (set <reg> <const_int>)
1447 effects: cfa_temp.reg = <reg>
1448 cfa_temp.offset = <const_int>
1450 Rule 7:
1451 (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1452 effects: cfa_temp.reg = <reg1>
1453 cfa_temp.offset |= <const_int>
1455 Rule 8:
1456 (set <reg> (high <exp>))
1457 effects: none
1459 Rule 9:
1460 (set <reg> (lo_sum <exp> <const_int>))
1461 effects: cfa_temp.reg = <reg>
1462 cfa_temp.offset = <const_int>
1464 Rule 10:
1465 (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1466 effects: cfa_store.offset -= <const_int>
1467 cfa.offset = cfa_store.offset if cfa.reg == sp
1468 cfa.reg = sp
1469 cfa.base_offset = -cfa_store.offset
1471 Rule 11:
1472 (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1473 effects: cfa_store.offset += -/+ mode_size(mem)
1474 cfa.offset = cfa_store.offset if cfa.reg == sp
1475 cfa.reg = sp
1476 cfa.base_offset = -cfa_store.offset
1478 Rule 12:
1479 (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1481 <reg2>)
1482 effects: cfa.reg = <reg1>
1483 cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
1485 Rule 13:
1486 (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1487 effects: cfa.reg = <reg1>
1488 cfa.base_offset = -{cfa_store,cfa_temp}.offset
1490 Rule 14:
1491 (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
1492 effects: cfa.reg = <reg1>
1493 cfa.base_offset = -cfa_temp.offset
1494 cfa_temp.offset -= mode_size(mem)
1496   Rule 15:
1497   (set <reg> {unspec, unspec_volatile})
1498   effects: target-dependent */
1500 static void
1501 dwarf2out_frame_debug_expr (rtx expr, const char *label)
1503 rtx src, dest;
1504 HOST_WIDE_INT offset;
1506 /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1507 the PARALLEL independently. The first element is always processed if
1508 it is a SET. This is for backward compatibility. Other elements
1509 are processed only if they are SETs and the RTX_FRAME_RELATED_P
1510 flag is set in them. */
1511 if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
1513 int par_index;
1514 int limit = XVECLEN (expr, 0);
1516 for (par_index = 0; par_index < limit; par_index++)
1517 if (GET_CODE (XVECEXP (expr, 0, par_index)) == SET
1518 && (RTX_FRAME_RELATED_P (XVECEXP (expr, 0, par_index))
1519 || par_index == 0))
1520 dwarf2out_frame_debug_expr (XVECEXP (expr, 0, par_index), label);
1522 return;
1525 gcc_assert (GET_CODE (expr) == SET);
1527 src = SET_SRC (expr);
1528 dest = SET_DEST (expr);
1530 if (REG_P (src))
1532 rtx rsi = reg_saved_in (src);
1533 if (rsi)
1534 src = rsi;
1537 switch (GET_CODE (dest))
1539 case REG:
1540 switch (GET_CODE (src))
1542 /* Setting FP from SP. */
1543 case REG:
1544 if (cfa.reg == (unsigned) REGNO (src))
1546 /* Rule 1 */
1547 /* Update the CFA rule wrt SP or FP. Make sure src is
1548 relative to the current CFA register.
1550 We used to require that dest be either SP or FP, but the
1551 ARM copies SP to a temporary register, and from there to
1552 FP. So we just rely on the backends to only set
1553 RTX_FRAME_RELATED_P on appropriate insns. */
1554 cfa.reg = REGNO (dest);
1555 cfa_temp.reg = cfa.reg;
1556 cfa_temp.offset = cfa.offset;
1558 else
1560 /* Saving a register in a register. */
1561 gcc_assert (!fixed_regs [REGNO (dest)]
1562 /* For the SPARC and its register window. */
1563 || (DWARF_FRAME_REGNUM (REGNO (src))
1564 == DWARF_FRAME_RETURN_COLUMN));
1565 queue_reg_save (label, src, dest, 0);
1567 break;
1569 case PLUS:
1570 case MINUS:
1571 case LO_SUM:
1572 if (dest == stack_pointer_rtx)
1574 /* Rule 2 */
1575 /* Adjusting SP. */
1576 switch (GET_CODE (XEXP (src, 1)))
1578 case CONST_INT:
1579 offset = INTVAL (XEXP (src, 1));
1580 break;
1581 case REG:
1582 gcc_assert ((unsigned) REGNO (XEXP (src, 1))
1583 == cfa_temp.reg);
1584 offset = cfa_temp.offset;
1585 break;
1586 default:
1587 gcc_unreachable ();
1590 if (XEXP (src, 0) == hard_frame_pointer_rtx)
1592 /* Restoring SP from FP in the epilogue. */
1593 gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
1594 cfa.reg = STACK_POINTER_REGNUM;
1596 else if (GET_CODE (src) == LO_SUM)
1597 /* Assume we've set the source reg of the LO_SUM from sp. */
1599 else
1600 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
1602 if (GET_CODE (src) != MINUS)
1603 offset = -offset;
1604 if (cfa.reg == STACK_POINTER_REGNUM)
1605 cfa.offset += offset;
1606 if (cfa_store.reg == STACK_POINTER_REGNUM)
1607 cfa_store.offset += offset;
1609 else if (dest == hard_frame_pointer_rtx)
1611 /* Rule 3 */
1612 /* Either setting the FP from an offset of the SP,
1613 or adjusting the FP */
1614 gcc_assert (frame_pointer_needed);
1616 gcc_assert (REG_P (XEXP (src, 0))
1617 && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
1618 && GET_CODE (XEXP (src, 1)) == CONST_INT);
1619 offset = INTVAL (XEXP (src, 1));
1620 if (GET_CODE (src) != MINUS)
1621 offset = -offset;
1622 cfa.offset += offset;
1623 cfa.reg = HARD_FRAME_POINTER_REGNUM;
1625 else
1627 gcc_assert (GET_CODE (src) != MINUS);
1629 /* Rule 4 */
1630 if (REG_P (XEXP (src, 0))
1631 && REGNO (XEXP (src, 0)) == cfa.reg
1632 && GET_CODE (XEXP (src, 1)) == CONST_INT)
1634 /* Setting a temporary CFA register that will be copied
1635 into the FP later on. */
1636 offset = - INTVAL (XEXP (src, 1));
1637 cfa.offset += offset;
1638 cfa.reg = REGNO (dest);
1639 /* Or used to save regs to the stack. */
1640 cfa_temp.reg = cfa.reg;
1641 cfa_temp.offset = cfa.offset;
1644 /* Rule 5 */
1645 else if (REG_P (XEXP (src, 0))
1646 && REGNO (XEXP (src, 0)) == cfa_temp.reg
1647 && XEXP (src, 1) == stack_pointer_rtx)
1649 /* Setting a scratch register that we will use instead
1650 of SP for saving registers to the stack. */
1651 gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
1652 cfa_store.reg = REGNO (dest);
1653 cfa_store.offset = cfa.offset - cfa_temp.offset;
1656 /* Rule 9 */
1657 else if (GET_CODE (src) == LO_SUM
1658 && GET_CODE (XEXP (src, 1)) == CONST_INT)
1660 cfa_temp.reg = REGNO (dest);
1661 cfa_temp.offset = INTVAL (XEXP (src, 1));
1663 else
1664 gcc_unreachable ();
1666 break;
1668 /* Rule 6 */
1669 case CONST_INT:
1670 cfa_temp.reg = REGNO (dest);
1671 cfa_temp.offset = INTVAL (src);
1672 break;
1674 /* Rule 7 */
1675 case IOR:
1676 gcc_assert (REG_P (XEXP (src, 0))
1677 && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
1678 && GET_CODE (XEXP (src, 1)) == CONST_INT);
1680 if ((unsigned) REGNO (dest) != cfa_temp.reg)
1681 cfa_temp.reg = REGNO (dest);
1682 cfa_temp.offset |= INTVAL (XEXP (src, 1));
1683 break;
1685 /* Skip over HIGH, assuming it will be followed by a LO_SUM,
1686 which will fill in all of the bits. */
1687 /* Rule 8 */
1688 case HIGH:
1689 break;
1691 /* Rule 15 */
1692 case UNSPEC:
1693 case UNSPEC_VOLATILE:
1694 gcc_assert (targetm.dwarf_handle_frame_unspec);
1695 targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
1696 return;
1698 default:
1699 gcc_unreachable ();
1702 def_cfa_1 (label, &cfa);
1703 break;
1705 case MEM:
1706 gcc_assert (REG_P (src));
1708 /* Saving a register to the stack. Make sure dest is relative to the
1709 CFA register. */
1710 switch (GET_CODE (XEXP (dest, 0)))
1712 /* Rule 10 */
1713 /* With a push. */
1714 case PRE_MODIFY:
1715 /* We can't handle variable size modifications. */
1716 gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
1717 == CONST_INT);
1718 offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
1720 gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1721 && cfa_store.reg == STACK_POINTER_REGNUM);
1723 cfa_store.offset += offset;
1724 if (cfa.reg == STACK_POINTER_REGNUM)
1725 cfa.offset = cfa_store.offset;
1727 offset = -cfa_store.offset;
1728 break;
1730 /* Rule 11 */
1731 case PRE_INC:
1732 case PRE_DEC:
1733 offset = GET_MODE_SIZE (GET_MODE (dest));
1734 if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
1735 offset = -offset;
1737 gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1738 && cfa_store.reg == STACK_POINTER_REGNUM);
1740 cfa_store.offset += offset;
1741 if (cfa.reg == STACK_POINTER_REGNUM)
1742 cfa.offset = cfa_store.offset;
1744 offset = -cfa_store.offset;
1745 break;
1747 /* Rule 12 */
1748 /* With an offset. */
1749 case PLUS:
1750 case MINUS:
1751 case LO_SUM:
1753 int regno;
1755 gcc_assert (GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT
1756 && REG_P (XEXP (XEXP (dest, 0), 0)));
1757 offset = INTVAL (XEXP (XEXP (dest, 0), 1));
1758 if (GET_CODE (XEXP (dest, 0)) == MINUS)
1759 offset = -offset;
1761 regno = REGNO (XEXP (XEXP (dest, 0), 0));
1763 if (cfa_store.reg == (unsigned) regno)
1764 offset -= cfa_store.offset;
1765 else
1767 gcc_assert (cfa_temp.reg == (unsigned) regno);
1768 offset -= cfa_temp.offset;
1771 break;
1773 /* Rule 13 */
1774 /* Without an offset. */
1775 case REG:
1777 int regno = REGNO (XEXP (dest, 0));
1779 if (cfa_store.reg == (unsigned) regno)
1780 offset = -cfa_store.offset;
1781 else
1783 gcc_assert (cfa_temp.reg == (unsigned) regno);
1784 offset = -cfa_temp.offset;
1787 break;
1789 /* Rule 14 */
1790 case POST_INC:
1791 gcc_assert (cfa_temp.reg
1792 == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
1793 offset = -cfa_temp.offset;
1794 cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
1795 break;
1797 default:
1798 gcc_unreachable ();
1801 if (REGNO (src) != STACK_POINTER_REGNUM
1802 && REGNO (src) != HARD_FRAME_POINTER_REGNUM
1803 && (unsigned) REGNO (src) == cfa.reg)
1805 /* We're storing the current CFA reg into the stack. */
1807 if (cfa.offset == 0)
1809 /* If the source register is exactly the CFA, assume
1810 we're saving SP like any other register; this happens
1811 on the ARM. */
1812 def_cfa_1 (label, &cfa);
1813 queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
1814 break;
1816 else
1818 /* Otherwise, we'll need to look in the stack to
1819 calculate the CFA. */
1820 rtx x = XEXP (dest, 0);
1822 if (!REG_P (x))
1823 x = XEXP (x, 0);
1824 gcc_assert (REG_P (x));
1826 cfa.reg = REGNO (x);
1827 cfa.base_offset = offset;
1828 cfa.indirect = 1;
1829 def_cfa_1 (label, &cfa);
1830 break;
1834 def_cfa_1 (label, &cfa);
1835 queue_reg_save (label, src, NULL_RTX, offset);
1836 break;
1838 default:
1839 gcc_unreachable ();
1843 /* Record call frame debugging information for INSN, which either
1844 sets SP or FP (adjusting how we calculate the frame address) or saves a
1845 register to the stack. If INSN is NULL_RTX, initialize our state.
1847 If AFTER_P is false, we're being called before the insn is emitted,
1848 otherwise after. Call instructions get invoked twice. */
1850 void
1851 dwarf2out_frame_debug (rtx insn, bool after_p)
1853 const char *label;
1854 rtx src;
1856 if (insn == NULL_RTX)
1858 size_t i;
1860 /* Flush any queued register saves. */
1861 flush_queued_reg_saves ();
1863 /* Set up state for generating call frame debug info. */
1864 lookup_cfa (&cfa);
1865 gcc_assert (cfa.reg
1866 == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
1868 cfa.reg = STACK_POINTER_REGNUM;
1869 cfa_store = cfa;
1870 cfa_temp.reg = -1;
1871 cfa_temp.offset = 0;
1873 for (i = 0; i < num_regs_saved_in_regs; i++)
1875 regs_saved_in_regs[i].orig_reg = NULL_RTX;
1876 regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
1878 num_regs_saved_in_regs = 0;
1879 return;
1882 if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
1883 flush_queued_reg_saves ();
1885 if (! RTX_FRAME_RELATED_P (insn))
1887 if (!ACCUMULATE_OUTGOING_ARGS)
1888 dwarf2out_stack_adjust (insn, after_p);
1889 return;
1892 label = dwarf2out_cfi_label ();
1893 src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1894 if (src)
1895 insn = XEXP (src, 0);
1896 else
1897 insn = PATTERN (insn);
1899 dwarf2out_frame_debug_expr (insn, label);
1902 #endif
1904 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
1905 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
1906 (enum dwarf_call_frame_info cfi);
1908 static enum dw_cfi_oprnd_type
1909 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
1911 switch (cfi)
1913 case DW_CFA_nop:
1914 case DW_CFA_GNU_window_save:
1915 return dw_cfi_oprnd_unused;
1917 case DW_CFA_set_loc:
1918 case DW_CFA_advance_loc1:
1919 case DW_CFA_advance_loc2:
1920 case DW_CFA_advance_loc4:
1921 case DW_CFA_MIPS_advance_loc8:
1922 return dw_cfi_oprnd_addr;
1924 case DW_CFA_offset:
1925 case DW_CFA_offset_extended:
1926 case DW_CFA_def_cfa:
1927 case DW_CFA_offset_extended_sf:
1928 case DW_CFA_def_cfa_sf:
1929 case DW_CFA_restore_extended:
1930 case DW_CFA_undefined:
1931 case DW_CFA_same_value:
1932 case DW_CFA_def_cfa_register:
1933 case DW_CFA_register:
1934 return dw_cfi_oprnd_reg_num;
1936 case DW_CFA_def_cfa_offset:
1937 case DW_CFA_GNU_args_size:
1938 case DW_CFA_def_cfa_offset_sf:
1939 return dw_cfi_oprnd_offset;
1941 case DW_CFA_def_cfa_expression:
1942 case DW_CFA_expression:
1943 return dw_cfi_oprnd_loc;
1945 default:
1946 gcc_unreachable ();
1950 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
1951 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
1952 (enum dwarf_call_frame_info cfi);
1954 static enum dw_cfi_oprnd_type
1955 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
1957 switch (cfi)
1959 case DW_CFA_def_cfa:
1960 case DW_CFA_def_cfa_sf:
1961 case DW_CFA_offset:
1962 case DW_CFA_offset_extended_sf:
1963 case DW_CFA_offset_extended:
1964 return dw_cfi_oprnd_offset;
1966 case DW_CFA_register:
1967 return dw_cfi_oprnd_reg_num;
1969 default:
1970 return dw_cfi_oprnd_unused;
1974 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1976 /* Switch to eh_frame_section. If we don't have an eh_frame_section,
1977 switch to the data section instead, and write out a synthetic label
1978 for collect2. */
1980 static void
1981 switch_to_eh_frame_section (void)
1983 tree label;
1985 #ifdef EH_FRAME_SECTION_NAME
1986 if (eh_frame_section == 0)
1988 int flags;
1990 if (EH_TABLES_CAN_BE_READ_ONLY)
1992 int fde_encoding;
1993 int per_encoding;
1994 int lsda_encoding;
1996 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
1997 /*global=*/0);
1998 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
1999 /*global=*/1);
2000 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2001 /*global=*/0);
2002 flags = ((! flag_pic
2003 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2004 && (fde_encoding & 0x70) != DW_EH_PE_aligned
2005 && (per_encoding & 0x70) != DW_EH_PE_absptr
2006 && (per_encoding & 0x70) != DW_EH_PE_aligned
2007 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2008 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2009 ? 0 : SECTION_WRITE);
2011 else
2012 flags = SECTION_WRITE;
2013 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2015 #endif
2017 if (eh_frame_section)
2018 switch_to_section (eh_frame_section);
2019 else
2021 /* We have no special eh_frame section. Put the information in
2022 the data section and emit special labels to guide collect2. */
2023 switch_to_section (data_section);
2024 label = get_file_function_name ('F');
2025 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2026 targetm.asm_out.globalize_label (asm_out_file,
2027 IDENTIFIER_POINTER (label));
2028 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
2032 /* Output a Call Frame Information opcode and its operand(s). */
2034 static void
2035 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
2037 unsigned long r;
2038 if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
2039 dw2_asm_output_data (1, (cfi->dw_cfi_opc
2040 | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
2041 "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
2042 cfi->dw_cfi_oprnd1.dw_cfi_offset);
2043 else if (cfi->dw_cfi_opc == DW_CFA_offset)
2045 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2046 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2047 "DW_CFA_offset, column 0x%lx", r);
2048 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2050 else if (cfi->dw_cfi_opc == DW_CFA_restore)
2052 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2053 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2054 "DW_CFA_restore, column 0x%lx", r);
2056 else
2058 dw2_asm_output_data (1, cfi->dw_cfi_opc,
2059 "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
2061 switch (cfi->dw_cfi_opc)
2063 case DW_CFA_set_loc:
2064 if (for_eh)
2065 dw2_asm_output_encoded_addr_rtx (
2066 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
2067 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
2068 false, NULL);
2069 else
2070 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2071 cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
2072 break;
2074 case DW_CFA_advance_loc1:
2075 dw2_asm_output_delta (1, 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_advance_loc2:
2081 dw2_asm_output_delta (2, 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_advance_loc4:
2087 dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2088 fde->dw_fde_current_label, NULL);
2089 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2090 break;
2092 case DW_CFA_MIPS_advance_loc8:
2093 dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2094 fde->dw_fde_current_label, NULL);
2095 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2096 break;
2098 case DW_CFA_offset_extended:
2099 case DW_CFA_def_cfa:
2100 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2101 dw2_asm_output_data_uleb128 (r, NULL);
2102 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2103 break;
2105 case DW_CFA_offset_extended_sf:
2106 case DW_CFA_def_cfa_sf:
2107 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2108 dw2_asm_output_data_uleb128 (r, NULL);
2109 dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2110 break;
2112 case DW_CFA_restore_extended:
2113 case DW_CFA_undefined:
2114 case DW_CFA_same_value:
2115 case DW_CFA_def_cfa_register:
2116 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2117 dw2_asm_output_data_uleb128 (r, NULL);
2118 break;
2120 case DW_CFA_register:
2121 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2122 dw2_asm_output_data_uleb128 (r, NULL);
2123 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
2124 dw2_asm_output_data_uleb128 (r, NULL);
2125 break;
2127 case DW_CFA_def_cfa_offset:
2128 case DW_CFA_GNU_args_size:
2129 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2130 break;
2132 case DW_CFA_def_cfa_offset_sf:
2133 dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2134 break;
2136 case DW_CFA_GNU_window_save:
2137 break;
2139 case DW_CFA_def_cfa_expression:
2140 case DW_CFA_expression:
2141 output_cfa_loc (cfi);
2142 break;
2144 case DW_CFA_GNU_negative_offset_extended:
2145 /* Obsoleted by DW_CFA_offset_extended_sf. */
2146 gcc_unreachable ();
2148 default:
2149 break;
2154 /* Output the call frame information used to record information
2155 that relates to calculating the frame pointer, and records the
2156 location of saved registers. */
2158 static void
2159 output_call_frame_info (int for_eh)
2161 unsigned int i;
2162 dw_fde_ref fde;
2163 dw_cfi_ref cfi;
2164 char l1[20], l2[20], section_start_label[20];
2165 bool any_lsda_needed = false;
2166 char augmentation[6];
2167 int augmentation_size;
2168 int fde_encoding = DW_EH_PE_absptr;
2169 int per_encoding = DW_EH_PE_absptr;
2170 int lsda_encoding = DW_EH_PE_absptr;
2171 int return_reg;
2173 /* Don't emit a CIE if there won't be any FDEs. */
2174 if (fde_table_in_use == 0)
2175 return;
2177 /* If we make FDEs linkonce, we may have to emit an empty label for
2178 an FDE that wouldn't otherwise be emitted. We want to avoid
2179 having an FDE kept around when the function it refers to is
2180 discarded. Example where this matters: a primary function
2181 template in C++ requires EH information, but an explicit
2182 specialization doesn't. */
2183 if (TARGET_USES_WEAK_UNWIND_INFO
2184 && ! flag_asynchronous_unwind_tables
2185 && for_eh)
2186 for (i = 0; i < fde_table_in_use; i++)
2187 if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
2188 && !fde_table[i].uses_eh_lsda
2189 && ! DECL_WEAK (fde_table[i].decl))
2190 targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
2191 for_eh, /* empty */ 1);
2193 /* If we don't have any functions we'll want to unwind out of, don't
2194 emit any EH unwind information. Note that if exceptions aren't
2195 enabled, we won't have collected nothrow information, and if we
2196 asked for asynchronous tables, we always want this info. */
2197 if (for_eh)
2199 bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
2201 for (i = 0; i < fde_table_in_use; i++)
2202 if (fde_table[i].uses_eh_lsda)
2203 any_eh_needed = any_lsda_needed = true;
2204 else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2205 any_eh_needed = true;
2206 else if (! fde_table[i].nothrow
2207 && ! fde_table[i].all_throwers_are_sibcalls)
2208 any_eh_needed = true;
2210 if (! any_eh_needed)
2211 return;
2214 /* We're going to be generating comments, so turn on app. */
2215 if (flag_debug_asm)
2216 app_enable ();
2218 if (for_eh)
2219 switch_to_eh_frame_section ();
2220 else
2221 switch_to_section (debug_frame_section);
2223 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
2224 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
2226 /* Output the CIE. */
2227 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
2228 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
2229 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2230 dw2_asm_output_data (4, 0xffffffff,
2231 "Initial length escape value indicating 64-bit DWARF extension");
2232 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2233 "Length of Common Information Entry");
2234 ASM_OUTPUT_LABEL (asm_out_file, l1);
2236 /* Now that the CIE pointer is PC-relative for EH,
2237 use 0 to identify the CIE. */
2238 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
2239 (for_eh ? 0 : DWARF_CIE_ID),
2240 "CIE Identifier Tag");
2242 dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
2244 augmentation[0] = 0;
2245 augmentation_size = 0;
2246 if (for_eh)
2248 char *p;
2250 /* Augmentation:
2251 z Indicates that a uleb128 is present to size the
2252 augmentation section.
2253 L Indicates the encoding (and thus presence) of
2254 an LSDA pointer in the FDE augmentation.
2255 R Indicates a non-default pointer encoding for
2256 FDE code pointers.
2257 P Indicates the presence of an encoding + language
2258 personality routine in the CIE augmentation. */
2260 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
2261 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
2262 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
2264 p = augmentation + 1;
2265 if (eh_personality_libfunc)
2267 *p++ = 'P';
2268 augmentation_size += 1 + size_of_encoded_value (per_encoding);
2270 if (any_lsda_needed)
2272 *p++ = 'L';
2273 augmentation_size += 1;
2275 if (fde_encoding != DW_EH_PE_absptr)
2277 *p++ = 'R';
2278 augmentation_size += 1;
2280 if (p > augmentation + 1)
2282 augmentation[0] = 'z';
2283 *p = '\0';
2286 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
2287 if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
2289 int offset = ( 4 /* Length */
2290 + 4 /* CIE Id */
2291 + 1 /* CIE version */
2292 + strlen (augmentation) + 1 /* Augmentation */
2293 + size_of_uleb128 (1) /* Code alignment */
2294 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
2295 + 1 /* RA column */
2296 + 1 /* Augmentation size */
2297 + 1 /* Personality encoding */ );
2298 int pad = -offset & (PTR_SIZE - 1);
2300 augmentation_size += pad;
2302 /* Augmentations should be small, so there's scarce need to
2303 iterate for a solution. Die if we exceed one uleb128 byte. */
2304 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
2308 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
2309 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
2310 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
2311 "CIE Data Alignment Factor");
2313 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
2314 if (DW_CIE_VERSION == 1)
2315 dw2_asm_output_data (1, return_reg, "CIE RA Column");
2316 else
2317 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
2319 if (augmentation[0])
2321 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
2322 if (eh_personality_libfunc)
2324 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
2325 eh_data_format_name (per_encoding));
2326 dw2_asm_output_encoded_addr_rtx (per_encoding,
2327 eh_personality_libfunc,
2328 true, NULL);
2331 if (any_lsda_needed)
2332 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
2333 eh_data_format_name (lsda_encoding));
2335 if (fde_encoding != DW_EH_PE_absptr)
2336 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
2337 eh_data_format_name (fde_encoding));
2340 for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
2341 output_cfi (cfi, NULL, for_eh);
2343 /* Pad the CIE out to an address sized boundary. */
2344 ASM_OUTPUT_ALIGN (asm_out_file,
2345 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
2346 ASM_OUTPUT_LABEL (asm_out_file, l2);
2348 /* Loop through all of the FDE's. */
2349 for (i = 0; i < fde_table_in_use; i++)
2351 fde = &fde_table[i];
2353 /* Don't emit EH unwind info for leaf functions that don't need it. */
2354 if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
2355 && (fde->nothrow || fde->all_throwers_are_sibcalls)
2356 && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2357 && !fde->uses_eh_lsda)
2358 continue;
2360 targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
2361 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
2362 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
2363 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
2364 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2365 dw2_asm_output_data (4, 0xffffffff,
2366 "Initial length escape value indicating 64-bit DWARF extension");
2367 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2368 "FDE Length");
2369 ASM_OUTPUT_LABEL (asm_out_file, l1);
2371 if (for_eh)
2372 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
2373 else
2374 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
2375 debug_frame_section, "FDE CIE offset");
2377 if (for_eh)
2379 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
2380 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
2381 dw2_asm_output_encoded_addr_rtx (fde_encoding,
2382 sym_ref,
2383 false,
2384 "FDE initial location");
2385 if (fde->dw_fde_switched_sections)
2387 rtx sym_ref2 = gen_rtx_SYMBOL_REF (Pmode,
2388 fde->dw_fde_unlikely_section_label);
2389 rtx sym_ref3= gen_rtx_SYMBOL_REF (Pmode,
2390 fde->dw_fde_hot_section_label);
2391 SYMBOL_REF_FLAGS (sym_ref2) |= SYMBOL_FLAG_LOCAL;
2392 SYMBOL_REF_FLAGS (sym_ref3) |= SYMBOL_FLAG_LOCAL;
2393 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref3, false,
2394 "FDE initial location");
2395 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2396 fde->dw_fde_hot_section_end_label,
2397 fde->dw_fde_hot_section_label,
2398 "FDE address range");
2399 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref2, false,
2400 "FDE initial location");
2401 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2402 fde->dw_fde_unlikely_section_end_label,
2403 fde->dw_fde_unlikely_section_label,
2404 "FDE address range");
2406 else
2407 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2408 fde->dw_fde_end, fde->dw_fde_begin,
2409 "FDE address range");
2411 else
2413 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
2414 "FDE initial location");
2415 if (fde->dw_fde_switched_sections)
2417 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2418 fde->dw_fde_hot_section_label,
2419 "FDE initial location");
2420 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2421 fde->dw_fde_hot_section_end_label,
2422 fde->dw_fde_hot_section_label,
2423 "FDE address range");
2424 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2425 fde->dw_fde_unlikely_section_label,
2426 "FDE initial location");
2427 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2428 fde->dw_fde_unlikely_section_end_label,
2429 fde->dw_fde_unlikely_section_label,
2430 "FDE address range");
2432 else
2433 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2434 fde->dw_fde_end, fde->dw_fde_begin,
2435 "FDE address range");
2438 if (augmentation[0])
2440 if (any_lsda_needed)
2442 int size = size_of_encoded_value (lsda_encoding);
2444 if (lsda_encoding == DW_EH_PE_aligned)
2446 int offset = ( 4 /* Length */
2447 + 4 /* CIE offset */
2448 + 2 * size_of_encoded_value (fde_encoding)
2449 + 1 /* Augmentation size */ );
2450 int pad = -offset & (PTR_SIZE - 1);
2452 size += pad;
2453 gcc_assert (size_of_uleb128 (size) == 1);
2456 dw2_asm_output_data_uleb128 (size, "Augmentation size");
2458 if (fde->uses_eh_lsda)
2460 ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
2461 fde->funcdef_number);
2462 dw2_asm_output_encoded_addr_rtx (
2463 lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
2464 false, "Language Specific Data Area");
2466 else
2468 if (lsda_encoding == DW_EH_PE_aligned)
2469 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2470 dw2_asm_output_data
2471 (size_of_encoded_value (lsda_encoding), 0,
2472 "Language Specific Data Area (none)");
2475 else
2476 dw2_asm_output_data_uleb128 (0, "Augmentation size");
2479 /* Loop through the Call Frame Instructions associated with
2480 this FDE. */
2481 fde->dw_fde_current_label = fde->dw_fde_begin;
2482 for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
2483 output_cfi (cfi, fde, for_eh);
2485 /* Pad the FDE out to an address sized boundary. */
2486 ASM_OUTPUT_ALIGN (asm_out_file,
2487 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
2488 ASM_OUTPUT_LABEL (asm_out_file, l2);
2491 if (for_eh && targetm.terminate_dw2_eh_frame_info)
2492 dw2_asm_output_data (4, 0, "End of Table");
2493 #ifdef MIPS_DEBUGGING_INFO
2494 /* Work around Irix 6 assembler bug whereby labels at the end of a section
2495 get a value of 0. Putting .align 0 after the label fixes it. */
2496 ASM_OUTPUT_ALIGN (asm_out_file, 0);
2497 #endif
2499 /* Turn off app to make assembly quicker. */
2500 if (flag_debug_asm)
2501 app_disable ();
2504 /* Output a marker (i.e. a label) for the beginning of a function, before
2505 the prologue. */
2507 void
2508 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
2509 const char *file ATTRIBUTE_UNUSED)
2511 char label[MAX_ARTIFICIAL_LABEL_BYTES];
2512 char * dup_label;
2513 dw_fde_ref fde;
2515 current_function_func_begin_label = NULL;
2517 #ifdef TARGET_UNWIND_INFO
2518 /* ??? current_function_func_begin_label is also used by except.c
2519 for call-site information. We must emit this label if it might
2520 be used. */
2521 if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
2522 && ! dwarf2out_do_frame ())
2523 return;
2524 #else
2525 if (! dwarf2out_do_frame ())
2526 return;
2527 #endif
2529 switch_to_section (function_section (current_function_decl));
2530 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
2531 current_function_funcdef_no);
2532 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
2533 current_function_funcdef_no);
2534 dup_label = xstrdup (label);
2535 current_function_func_begin_label = dup_label;
2537 #ifdef TARGET_UNWIND_INFO
2538 /* We can elide the fde allocation if we're not emitting debug info. */
2539 if (! dwarf2out_do_frame ())
2540 return;
2541 #endif
2543 /* Expand the fde table if necessary. */
2544 if (fde_table_in_use == fde_table_allocated)
2546 fde_table_allocated += FDE_TABLE_INCREMENT;
2547 fde_table = ggc_realloc (fde_table,
2548 fde_table_allocated * sizeof (dw_fde_node));
2549 memset (fde_table + fde_table_in_use, 0,
2550 FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2553 /* Record the FDE associated with this function. */
2554 current_funcdef_fde = fde_table_in_use;
2556 /* Add the new FDE at the end of the fde_table. */
2557 fde = &fde_table[fde_table_in_use++];
2558 fde->decl = current_function_decl;
2559 fde->dw_fde_begin = dup_label;
2560 fde->dw_fde_current_label = NULL;
2561 fde->dw_fde_hot_section_label = NULL;
2562 fde->dw_fde_hot_section_end_label = NULL;
2563 fde->dw_fde_unlikely_section_label = NULL;
2564 fde->dw_fde_unlikely_section_end_label = NULL;
2565 fde->dw_fde_switched_sections = false;
2566 fde->dw_fde_end = NULL;
2567 fde->dw_fde_cfi = NULL;
2568 fde->funcdef_number = current_function_funcdef_no;
2569 fde->nothrow = TREE_NOTHROW (current_function_decl);
2570 fde->uses_eh_lsda = cfun->uses_eh_lsda;
2571 fde->all_throwers_are_sibcalls = cfun->all_throwers_are_sibcalls;
2573 args_size = old_args_size = 0;
2575 /* We only want to output line number information for the genuine dwarf2
2576 prologue case, not the eh frame case. */
2577 #ifdef DWARF2_DEBUGGING_INFO
2578 if (file)
2579 dwarf2out_source_line (line, file);
2580 #endif
2583 /* Output a marker (i.e. a label) for the absolute end of the generated code
2584 for a function definition. This gets called *after* the epilogue code has
2585 been generated. */
2587 void
2588 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
2589 const char *file ATTRIBUTE_UNUSED)
2591 dw_fde_ref fde;
2592 char label[MAX_ARTIFICIAL_LABEL_BYTES];
2594 /* Output a label to mark the endpoint of the code generated for this
2595 function. */
2596 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
2597 current_function_funcdef_no);
2598 ASM_OUTPUT_LABEL (asm_out_file, label);
2599 fde = &fde_table[fde_table_in_use - 1];
2600 fde->dw_fde_end = xstrdup (label);
2603 void
2604 dwarf2out_frame_init (void)
2606 /* Allocate the initial hunk of the fde_table. */
2607 fde_table = ggc_alloc_cleared (FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2608 fde_table_allocated = FDE_TABLE_INCREMENT;
2609 fde_table_in_use = 0;
2611 /* Generate the CFA instructions common to all FDE's. Do it now for the
2612 sake of lookup_cfa. */
2614 /* On entry, the Canonical Frame Address is at SP. */
2615 dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
2617 #ifdef DWARF2_UNWIND_INFO
2618 if (DWARF2_UNWIND_INFO)
2619 initial_return_save (INCOMING_RETURN_ADDR_RTX);
2620 #endif
2623 void
2624 dwarf2out_frame_finish (void)
2626 /* Output call frame information. */
2627 if (DWARF2_FRAME_INFO)
2628 output_call_frame_info (0);
2630 #ifndef TARGET_UNWIND_INFO
2631 /* Output another copy for the unwinder. */
2632 if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
2633 output_call_frame_info (1);
2634 #endif
2636 #endif
2638 /* And now, the subset of the debugging information support code necessary
2639 for emitting location expressions. */
2641 /* We need some way to distinguish DW_OP_addr with a direct symbol
2642 relocation from DW_OP_addr with a dtp-relative symbol relocation. */
2643 #define INTERNAL_DW_OP_tls_addr (0x100 + DW_OP_addr)
2646 typedef struct dw_val_struct *dw_val_ref;
2647 typedef struct die_struct *dw_die_ref;
2648 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
2649 typedef struct dw_loc_list_struct *dw_loc_list_ref;
2651 /* Each DIE may have a series of attribute/value pairs. Values
2652 can take on several forms. The forms that are used in this
2653 implementation are listed below. */
2655 enum dw_val_class
2657 dw_val_class_addr,
2658 dw_val_class_offset,
2659 dw_val_class_loc,
2660 dw_val_class_loc_list,
2661 dw_val_class_range_list,
2662 dw_val_class_const,
2663 dw_val_class_unsigned_const,
2664 dw_val_class_long_long,
2665 dw_val_class_vec,
2666 dw_val_class_flag,
2667 dw_val_class_die_ref,
2668 dw_val_class_fde_ref,
2669 dw_val_class_lbl_id,
2670 dw_val_class_lineptr,
2671 dw_val_class_str,
2672 dw_val_class_macptr
2675 /* Describe a double word constant value. */
2676 /* ??? Every instance of long_long in the code really means CONST_DOUBLE. */
2678 typedef struct dw_long_long_struct GTY(())
2680 unsigned long hi;
2681 unsigned long low;
2683 dw_long_long_const;
2685 /* Describe a floating point constant value, or a vector constant value. */
2687 typedef struct dw_vec_struct GTY(())
2689 unsigned char * GTY((length ("%h.length"))) array;
2690 unsigned length;
2691 unsigned elt_size;
2693 dw_vec_const;
2695 /* The dw_val_node describes an attribute's value, as it is
2696 represented internally. */
2698 typedef struct dw_val_struct GTY(())
2700 enum dw_val_class val_class;
2701 union dw_val_struct_union
2703 rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
2704 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
2705 dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
2706 dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
2707 HOST_WIDE_INT GTY ((default)) val_int;
2708 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
2709 dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
2710 dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
2711 struct dw_val_die_union
2713 dw_die_ref die;
2714 int external;
2715 } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
2716 unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
2717 struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
2718 char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
2719 unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
2721 GTY ((desc ("%1.val_class"))) v;
2723 dw_val_node;
2725 /* Locations in memory are described using a sequence of stack machine
2726 operations. */
2728 typedef struct dw_loc_descr_struct GTY(())
2730 dw_loc_descr_ref dw_loc_next;
2731 enum dwarf_location_atom dw_loc_opc;
2732 dw_val_node dw_loc_oprnd1;
2733 dw_val_node dw_loc_oprnd2;
2734 int dw_loc_addr;
2736 dw_loc_descr_node;
2738 /* Location lists are ranges + location descriptions for that range,
2739 so you can track variables that are in different places over
2740 their entire life. */
2741 typedef struct dw_loc_list_struct GTY(())
2743 dw_loc_list_ref dw_loc_next;
2744 const char *begin; /* Label for begin address of range */
2745 const char *end; /* Label for end address of range */
2746 char *ll_symbol; /* Label for beginning of location list.
2747 Only on head of list */
2748 const char *section; /* Section this loclist is relative to */
2749 dw_loc_descr_ref expr;
2750 } dw_loc_list_node;
2752 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2754 static const char *dwarf_stack_op_name (unsigned);
2755 static dw_loc_descr_ref new_loc_descr (enum dwarf_location_atom,
2756 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2757 static void add_loc_descr (dw_loc_descr_ref *, dw_loc_descr_ref);
2758 static unsigned long size_of_loc_descr (dw_loc_descr_ref);
2759 static unsigned long size_of_locs (dw_loc_descr_ref);
2760 static void output_loc_operands (dw_loc_descr_ref);
2761 static void output_loc_sequence (dw_loc_descr_ref);
2763 /* Convert a DWARF stack opcode into its string name. */
2765 static const char *
2766 dwarf_stack_op_name (unsigned int op)
2768 switch (op)
2770 case DW_OP_addr:
2771 case INTERNAL_DW_OP_tls_addr:
2772 return "DW_OP_addr";
2773 case DW_OP_deref:
2774 return "DW_OP_deref";
2775 case DW_OP_const1u:
2776 return "DW_OP_const1u";
2777 case DW_OP_const1s:
2778 return "DW_OP_const1s";
2779 case DW_OP_const2u:
2780 return "DW_OP_const2u";
2781 case DW_OP_const2s:
2782 return "DW_OP_const2s";
2783 case DW_OP_const4u:
2784 return "DW_OP_const4u";
2785 case DW_OP_const4s:
2786 return "DW_OP_const4s";
2787 case DW_OP_const8u:
2788 return "DW_OP_const8u";
2789 case DW_OP_const8s:
2790 return "DW_OP_const8s";
2791 case DW_OP_constu:
2792 return "DW_OP_constu";
2793 case DW_OP_consts:
2794 return "DW_OP_consts";
2795 case DW_OP_dup:
2796 return "DW_OP_dup";
2797 case DW_OP_drop:
2798 return "DW_OP_drop";
2799 case DW_OP_over:
2800 return "DW_OP_over";
2801 case DW_OP_pick:
2802 return "DW_OP_pick";
2803 case DW_OP_swap:
2804 return "DW_OP_swap";
2805 case DW_OP_rot:
2806 return "DW_OP_rot";
2807 case DW_OP_xderef:
2808 return "DW_OP_xderef";
2809 case DW_OP_abs:
2810 return "DW_OP_abs";
2811 case DW_OP_and:
2812 return "DW_OP_and";
2813 case DW_OP_div:
2814 return "DW_OP_div";
2815 case DW_OP_minus:
2816 return "DW_OP_minus";
2817 case DW_OP_mod:
2818 return "DW_OP_mod";
2819 case DW_OP_mul:
2820 return "DW_OP_mul";
2821 case DW_OP_neg:
2822 return "DW_OP_neg";
2823 case DW_OP_not:
2824 return "DW_OP_not";
2825 case DW_OP_or:
2826 return "DW_OP_or";
2827 case DW_OP_plus:
2828 return "DW_OP_plus";
2829 case DW_OP_plus_uconst:
2830 return "DW_OP_plus_uconst";
2831 case DW_OP_shl:
2832 return "DW_OP_shl";
2833 case DW_OP_shr:
2834 return "DW_OP_shr";
2835 case DW_OP_shra:
2836 return "DW_OP_shra";
2837 case DW_OP_xor:
2838 return "DW_OP_xor";
2839 case DW_OP_bra:
2840 return "DW_OP_bra";
2841 case DW_OP_eq:
2842 return "DW_OP_eq";
2843 case DW_OP_ge:
2844 return "DW_OP_ge";
2845 case DW_OP_gt:
2846 return "DW_OP_gt";
2847 case DW_OP_le:
2848 return "DW_OP_le";
2849 case DW_OP_lt:
2850 return "DW_OP_lt";
2851 case DW_OP_ne:
2852 return "DW_OP_ne";
2853 case DW_OP_skip:
2854 return "DW_OP_skip";
2855 case DW_OP_lit0:
2856 return "DW_OP_lit0";
2857 case DW_OP_lit1:
2858 return "DW_OP_lit1";
2859 case DW_OP_lit2:
2860 return "DW_OP_lit2";
2861 case DW_OP_lit3:
2862 return "DW_OP_lit3";
2863 case DW_OP_lit4:
2864 return "DW_OP_lit4";
2865 case DW_OP_lit5:
2866 return "DW_OP_lit5";
2867 case DW_OP_lit6:
2868 return "DW_OP_lit6";
2869 case DW_OP_lit7:
2870 return "DW_OP_lit7";
2871 case DW_OP_lit8:
2872 return "DW_OP_lit8";
2873 case DW_OP_lit9:
2874 return "DW_OP_lit9";
2875 case DW_OP_lit10:
2876 return "DW_OP_lit10";
2877 case DW_OP_lit11:
2878 return "DW_OP_lit11";
2879 case DW_OP_lit12:
2880 return "DW_OP_lit12";
2881 case DW_OP_lit13:
2882 return "DW_OP_lit13";
2883 case DW_OP_lit14:
2884 return "DW_OP_lit14";
2885 case DW_OP_lit15:
2886 return "DW_OP_lit15";
2887 case DW_OP_lit16:
2888 return "DW_OP_lit16";
2889 case DW_OP_lit17:
2890 return "DW_OP_lit17";
2891 case DW_OP_lit18:
2892 return "DW_OP_lit18";
2893 case DW_OP_lit19:
2894 return "DW_OP_lit19";
2895 case DW_OP_lit20:
2896 return "DW_OP_lit20";
2897 case DW_OP_lit21:
2898 return "DW_OP_lit21";
2899 case DW_OP_lit22:
2900 return "DW_OP_lit22";
2901 case DW_OP_lit23:
2902 return "DW_OP_lit23";
2903 case DW_OP_lit24:
2904 return "DW_OP_lit24";
2905 case DW_OP_lit25:
2906 return "DW_OP_lit25";
2907 case DW_OP_lit26:
2908 return "DW_OP_lit26";
2909 case DW_OP_lit27:
2910 return "DW_OP_lit27";
2911 case DW_OP_lit28:
2912 return "DW_OP_lit28";
2913 case DW_OP_lit29:
2914 return "DW_OP_lit29";
2915 case DW_OP_lit30:
2916 return "DW_OP_lit30";
2917 case DW_OP_lit31:
2918 return "DW_OP_lit31";
2919 case DW_OP_reg0:
2920 return "DW_OP_reg0";
2921 case DW_OP_reg1:
2922 return "DW_OP_reg1";
2923 case DW_OP_reg2:
2924 return "DW_OP_reg2";
2925 case DW_OP_reg3:
2926 return "DW_OP_reg3";
2927 case DW_OP_reg4:
2928 return "DW_OP_reg4";
2929 case DW_OP_reg5:
2930 return "DW_OP_reg5";
2931 case DW_OP_reg6:
2932 return "DW_OP_reg6";
2933 case DW_OP_reg7:
2934 return "DW_OP_reg7";
2935 case DW_OP_reg8:
2936 return "DW_OP_reg8";
2937 case DW_OP_reg9:
2938 return "DW_OP_reg9";
2939 case DW_OP_reg10:
2940 return "DW_OP_reg10";
2941 case DW_OP_reg11:
2942 return "DW_OP_reg11";
2943 case DW_OP_reg12:
2944 return "DW_OP_reg12";
2945 case DW_OP_reg13:
2946 return "DW_OP_reg13";
2947 case DW_OP_reg14:
2948 return "DW_OP_reg14";
2949 case DW_OP_reg15:
2950 return "DW_OP_reg15";
2951 case DW_OP_reg16:
2952 return "DW_OP_reg16";
2953 case DW_OP_reg17:
2954 return "DW_OP_reg17";
2955 case DW_OP_reg18:
2956 return "DW_OP_reg18";
2957 case DW_OP_reg19:
2958 return "DW_OP_reg19";
2959 case DW_OP_reg20:
2960 return "DW_OP_reg20";
2961 case DW_OP_reg21:
2962 return "DW_OP_reg21";
2963 case DW_OP_reg22:
2964 return "DW_OP_reg22";
2965 case DW_OP_reg23:
2966 return "DW_OP_reg23";
2967 case DW_OP_reg24:
2968 return "DW_OP_reg24";
2969 case DW_OP_reg25:
2970 return "DW_OP_reg25";
2971 case DW_OP_reg26:
2972 return "DW_OP_reg26";
2973 case DW_OP_reg27:
2974 return "DW_OP_reg27";
2975 case DW_OP_reg28:
2976 return "DW_OP_reg28";
2977 case DW_OP_reg29:
2978 return "DW_OP_reg29";
2979 case DW_OP_reg30:
2980 return "DW_OP_reg30";
2981 case DW_OP_reg31:
2982 return "DW_OP_reg31";
2983 case DW_OP_breg0:
2984 return "DW_OP_breg0";
2985 case DW_OP_breg1:
2986 return "DW_OP_breg1";
2987 case DW_OP_breg2:
2988 return "DW_OP_breg2";
2989 case DW_OP_breg3:
2990 return "DW_OP_breg3";
2991 case DW_OP_breg4:
2992 return "DW_OP_breg4";
2993 case DW_OP_breg5:
2994 return "DW_OP_breg5";
2995 case DW_OP_breg6:
2996 return "DW_OP_breg6";
2997 case DW_OP_breg7:
2998 return "DW_OP_breg7";
2999 case DW_OP_breg8:
3000 return "DW_OP_breg8";
3001 case DW_OP_breg9:
3002 return "DW_OP_breg9";
3003 case DW_OP_breg10:
3004 return "DW_OP_breg10";
3005 case DW_OP_breg11:
3006 return "DW_OP_breg11";
3007 case DW_OP_breg12:
3008 return "DW_OP_breg12";
3009 case DW_OP_breg13:
3010 return "DW_OP_breg13";
3011 case DW_OP_breg14:
3012 return "DW_OP_breg14";
3013 case DW_OP_breg15:
3014 return "DW_OP_breg15";
3015 case DW_OP_breg16:
3016 return "DW_OP_breg16";
3017 case DW_OP_breg17:
3018 return "DW_OP_breg17";
3019 case DW_OP_breg18:
3020 return "DW_OP_breg18";
3021 case DW_OP_breg19:
3022 return "DW_OP_breg19";
3023 case DW_OP_breg20:
3024 return "DW_OP_breg20";
3025 case DW_OP_breg21:
3026 return "DW_OP_breg21";
3027 case DW_OP_breg22:
3028 return "DW_OP_breg22";
3029 case DW_OP_breg23:
3030 return "DW_OP_breg23";
3031 case DW_OP_breg24:
3032 return "DW_OP_breg24";
3033 case DW_OP_breg25:
3034 return "DW_OP_breg25";
3035 case DW_OP_breg26:
3036 return "DW_OP_breg26";
3037 case DW_OP_breg27:
3038 return "DW_OP_breg27";
3039 case DW_OP_breg28:
3040 return "DW_OP_breg28";
3041 case DW_OP_breg29:
3042 return "DW_OP_breg29";
3043 case DW_OP_breg30:
3044 return "DW_OP_breg30";
3045 case DW_OP_breg31:
3046 return "DW_OP_breg31";
3047 case DW_OP_regx:
3048 return "DW_OP_regx";
3049 case DW_OP_fbreg:
3050 return "DW_OP_fbreg";
3051 case DW_OP_bregx:
3052 return "DW_OP_bregx";
3053 case DW_OP_piece:
3054 return "DW_OP_piece";
3055 case DW_OP_deref_size:
3056 return "DW_OP_deref_size";
3057 case DW_OP_xderef_size:
3058 return "DW_OP_xderef_size";
3059 case DW_OP_nop:
3060 return "DW_OP_nop";
3061 case DW_OP_push_object_address:
3062 return "DW_OP_push_object_address";
3063 case DW_OP_call2:
3064 return "DW_OP_call2";
3065 case DW_OP_call4:
3066 return "DW_OP_call4";
3067 case DW_OP_call_ref:
3068 return "DW_OP_call_ref";
3069 case DW_OP_GNU_push_tls_address:
3070 return "DW_OP_GNU_push_tls_address";
3071 default:
3072 return "OP_<unknown>";
3076 /* Return a pointer to a newly allocated location description. Location
3077 descriptions are simple expression terms that can be strung
3078 together to form more complicated location (address) descriptions. */
3080 static inline dw_loc_descr_ref
3081 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
3082 unsigned HOST_WIDE_INT oprnd2)
3084 dw_loc_descr_ref descr = ggc_alloc_cleared (sizeof (dw_loc_descr_node));
3086 descr->dw_loc_opc = op;
3087 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
3088 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
3089 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
3090 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
3092 return descr;
3095 /* Add a location description term to a location description expression. */
3097 static inline void
3098 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
3100 dw_loc_descr_ref *d;
3102 /* Find the end of the chain. */
3103 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
3106 *d = descr;
3109 /* Return the size of a location descriptor. */
3111 static unsigned long
3112 size_of_loc_descr (dw_loc_descr_ref loc)
3114 unsigned long size = 1;
3116 switch (loc->dw_loc_opc)
3118 case DW_OP_addr:
3119 case INTERNAL_DW_OP_tls_addr:
3120 size += DWARF2_ADDR_SIZE;
3121 break;
3122 case DW_OP_const1u:
3123 case DW_OP_const1s:
3124 size += 1;
3125 break;
3126 case DW_OP_const2u:
3127 case DW_OP_const2s:
3128 size += 2;
3129 break;
3130 case DW_OP_const4u:
3131 case DW_OP_const4s:
3132 size += 4;
3133 break;
3134 case DW_OP_const8u:
3135 case DW_OP_const8s:
3136 size += 8;
3137 break;
3138 case DW_OP_constu:
3139 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3140 break;
3141 case DW_OP_consts:
3142 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3143 break;
3144 case DW_OP_pick:
3145 size += 1;
3146 break;
3147 case DW_OP_plus_uconst:
3148 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3149 break;
3150 case DW_OP_skip:
3151 case DW_OP_bra:
3152 size += 2;
3153 break;
3154 case DW_OP_breg0:
3155 case DW_OP_breg1:
3156 case DW_OP_breg2:
3157 case DW_OP_breg3:
3158 case DW_OP_breg4:
3159 case DW_OP_breg5:
3160 case DW_OP_breg6:
3161 case DW_OP_breg7:
3162 case DW_OP_breg8:
3163 case DW_OP_breg9:
3164 case DW_OP_breg10:
3165 case DW_OP_breg11:
3166 case DW_OP_breg12:
3167 case DW_OP_breg13:
3168 case DW_OP_breg14:
3169 case DW_OP_breg15:
3170 case DW_OP_breg16:
3171 case DW_OP_breg17:
3172 case DW_OP_breg18:
3173 case DW_OP_breg19:
3174 case DW_OP_breg20:
3175 case DW_OP_breg21:
3176 case DW_OP_breg22:
3177 case DW_OP_breg23:
3178 case DW_OP_breg24:
3179 case DW_OP_breg25:
3180 case DW_OP_breg26:
3181 case DW_OP_breg27:
3182 case DW_OP_breg28:
3183 case DW_OP_breg29:
3184 case DW_OP_breg30:
3185 case DW_OP_breg31:
3186 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3187 break;
3188 case DW_OP_regx:
3189 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3190 break;
3191 case DW_OP_fbreg:
3192 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3193 break;
3194 case DW_OP_bregx:
3195 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3196 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
3197 break;
3198 case DW_OP_piece:
3199 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3200 break;
3201 case DW_OP_deref_size:
3202 case DW_OP_xderef_size:
3203 size += 1;
3204 break;
3205 case DW_OP_call2:
3206 size += 2;
3207 break;
3208 case DW_OP_call4:
3209 size += 4;
3210 break;
3211 case DW_OP_call_ref:
3212 size += DWARF2_ADDR_SIZE;
3213 break;
3214 default:
3215 break;
3218 return size;
3221 /* Return the size of a series of location descriptors. */
3223 static unsigned long
3224 size_of_locs (dw_loc_descr_ref loc)
3226 unsigned long size;
3228 for (size = 0; loc != NULL; loc = loc->dw_loc_next)
3230 loc->dw_loc_addr = size;
3231 size += size_of_loc_descr (loc);
3234 return size;
3237 /* Output location description stack opcode's operands (if any). */
3239 static void
3240 output_loc_operands (dw_loc_descr_ref loc)
3242 dw_val_ref val1 = &loc->dw_loc_oprnd1;
3243 dw_val_ref val2 = &loc->dw_loc_oprnd2;
3245 switch (loc->dw_loc_opc)
3247 #ifdef DWARF2_DEBUGGING_INFO
3248 case DW_OP_addr:
3249 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
3250 break;
3251 case DW_OP_const2u:
3252 case DW_OP_const2s:
3253 dw2_asm_output_data (2, val1->v.val_int, NULL);
3254 break;
3255 case DW_OP_const4u:
3256 case DW_OP_const4s:
3257 dw2_asm_output_data (4, val1->v.val_int, NULL);
3258 break;
3259 case DW_OP_const8u:
3260 case DW_OP_const8s:
3261 gcc_assert (HOST_BITS_PER_LONG >= 64);
3262 dw2_asm_output_data (8, val1->v.val_int, NULL);
3263 break;
3264 case DW_OP_skip:
3265 case DW_OP_bra:
3267 int offset;
3269 gcc_assert (val1->val_class == dw_val_class_loc);
3270 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
3272 dw2_asm_output_data (2, offset, NULL);
3274 break;
3275 #else
3276 case DW_OP_addr:
3277 case DW_OP_const2u:
3278 case DW_OP_const2s:
3279 case DW_OP_const4u:
3280 case DW_OP_const4s:
3281 case DW_OP_const8u:
3282 case DW_OP_const8s:
3283 case DW_OP_skip:
3284 case DW_OP_bra:
3285 /* We currently don't make any attempt to make sure these are
3286 aligned properly like we do for the main unwind info, so
3287 don't support emitting things larger than a byte if we're
3288 only doing unwinding. */
3289 gcc_unreachable ();
3290 #endif
3291 case DW_OP_const1u:
3292 case DW_OP_const1s:
3293 dw2_asm_output_data (1, val1->v.val_int, NULL);
3294 break;
3295 case DW_OP_constu:
3296 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3297 break;
3298 case DW_OP_consts:
3299 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3300 break;
3301 case DW_OP_pick:
3302 dw2_asm_output_data (1, val1->v.val_int, NULL);
3303 break;
3304 case DW_OP_plus_uconst:
3305 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3306 break;
3307 case DW_OP_breg0:
3308 case DW_OP_breg1:
3309 case DW_OP_breg2:
3310 case DW_OP_breg3:
3311 case DW_OP_breg4:
3312 case DW_OP_breg5:
3313 case DW_OP_breg6:
3314 case DW_OP_breg7:
3315 case DW_OP_breg8:
3316 case DW_OP_breg9:
3317 case DW_OP_breg10:
3318 case DW_OP_breg11:
3319 case DW_OP_breg12:
3320 case DW_OP_breg13:
3321 case DW_OP_breg14:
3322 case DW_OP_breg15:
3323 case DW_OP_breg16:
3324 case DW_OP_breg17:
3325 case DW_OP_breg18:
3326 case DW_OP_breg19:
3327 case DW_OP_breg20:
3328 case DW_OP_breg21:
3329 case DW_OP_breg22:
3330 case DW_OP_breg23:
3331 case DW_OP_breg24:
3332 case DW_OP_breg25:
3333 case DW_OP_breg26:
3334 case DW_OP_breg27:
3335 case DW_OP_breg28:
3336 case DW_OP_breg29:
3337 case DW_OP_breg30:
3338 case DW_OP_breg31:
3339 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3340 break;
3341 case DW_OP_regx:
3342 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3343 break;
3344 case DW_OP_fbreg:
3345 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3346 break;
3347 case DW_OP_bregx:
3348 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3349 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
3350 break;
3351 case DW_OP_piece:
3352 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3353 break;
3354 case DW_OP_deref_size:
3355 case DW_OP_xderef_size:
3356 dw2_asm_output_data (1, val1->v.val_int, NULL);
3357 break;
3359 case INTERNAL_DW_OP_tls_addr:
3360 if (targetm.asm_out.output_dwarf_dtprel)
3362 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
3363 DWARF2_ADDR_SIZE,
3364 val1->v.val_addr);
3365 fputc ('\n', asm_out_file);
3367 else
3368 gcc_unreachable ();
3369 break;
3371 default:
3372 /* Other codes have no operands. */
3373 break;
3377 /* Output a sequence of location operations. */
3379 static void
3380 output_loc_sequence (dw_loc_descr_ref loc)
3382 for (; loc != NULL; loc = loc->dw_loc_next)
3384 /* Output the opcode. */
3385 dw2_asm_output_data (1, loc->dw_loc_opc,
3386 "%s", dwarf_stack_op_name (loc->dw_loc_opc));
3388 /* Output the operand(s) (if any). */
3389 output_loc_operands (loc);
3393 /* This routine will generate the correct assembly data for a location
3394 description based on a cfi entry with a complex address. */
3396 static void
3397 output_cfa_loc (dw_cfi_ref cfi)
3399 dw_loc_descr_ref loc;
3400 unsigned long size;
3402 /* Output the size of the block. */
3403 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
3404 size = size_of_locs (loc);
3405 dw2_asm_output_data_uleb128 (size, NULL);
3407 /* Now output the operations themselves. */
3408 output_loc_sequence (loc);
3411 /* This function builds a dwarf location descriptor sequence from a
3412 dw_cfa_location, adding the given OFFSET to the result of the
3413 expression. */
3415 static struct dw_loc_descr_struct *
3416 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
3418 struct dw_loc_descr_struct *head, *tmp;
3420 offset += cfa->offset;
3422 if (cfa->indirect)
3424 if (cfa->base_offset)
3426 if (cfa->reg <= 31)
3427 head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->base_offset, 0);
3428 else
3429 head = new_loc_descr (DW_OP_bregx, cfa->reg, cfa->base_offset);
3431 else if (cfa->reg <= 31)
3432 head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3433 else
3434 head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3436 head->dw_loc_oprnd1.val_class = dw_val_class_const;
3437 tmp = new_loc_descr (DW_OP_deref, 0, 0);
3438 add_loc_descr (&head, tmp);
3439 if (offset != 0)
3441 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
3442 add_loc_descr (&head, tmp);
3445 else
3447 if (offset == 0)
3448 if (cfa->reg <= 31)
3449 head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3450 else
3451 head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3452 else if (cfa->reg <= 31)
3453 head = new_loc_descr (DW_OP_breg0 + cfa->reg, offset, 0);
3454 else
3455 head = new_loc_descr (DW_OP_bregx, cfa->reg, offset);
3458 return head;
3461 /* This function fills in aa dw_cfa_location structure from a dwarf location
3462 descriptor sequence. */
3464 static void
3465 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
3467 struct dw_loc_descr_struct *ptr;
3468 cfa->offset = 0;
3469 cfa->base_offset = 0;
3470 cfa->indirect = 0;
3471 cfa->reg = -1;
3473 for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
3475 enum dwarf_location_atom op = ptr->dw_loc_opc;
3477 switch (op)
3479 case DW_OP_reg0:
3480 case DW_OP_reg1:
3481 case DW_OP_reg2:
3482 case DW_OP_reg3:
3483 case DW_OP_reg4:
3484 case DW_OP_reg5:
3485 case DW_OP_reg6:
3486 case DW_OP_reg7:
3487 case DW_OP_reg8:
3488 case DW_OP_reg9:
3489 case DW_OP_reg10:
3490 case DW_OP_reg11:
3491 case DW_OP_reg12:
3492 case DW_OP_reg13:
3493 case DW_OP_reg14:
3494 case DW_OP_reg15:
3495 case DW_OP_reg16:
3496 case DW_OP_reg17:
3497 case DW_OP_reg18:
3498 case DW_OP_reg19:
3499 case DW_OP_reg20:
3500 case DW_OP_reg21:
3501 case DW_OP_reg22:
3502 case DW_OP_reg23:
3503 case DW_OP_reg24:
3504 case DW_OP_reg25:
3505 case DW_OP_reg26:
3506 case DW_OP_reg27:
3507 case DW_OP_reg28:
3508 case DW_OP_reg29:
3509 case DW_OP_reg30:
3510 case DW_OP_reg31:
3511 cfa->reg = op - DW_OP_reg0;
3512 break;
3513 case DW_OP_regx:
3514 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3515 break;
3516 case DW_OP_breg0:
3517 case DW_OP_breg1:
3518 case DW_OP_breg2:
3519 case DW_OP_breg3:
3520 case DW_OP_breg4:
3521 case DW_OP_breg5:
3522 case DW_OP_breg6:
3523 case DW_OP_breg7:
3524 case DW_OP_breg8:
3525 case DW_OP_breg9:
3526 case DW_OP_breg10:
3527 case DW_OP_breg11:
3528 case DW_OP_breg12:
3529 case DW_OP_breg13:
3530 case DW_OP_breg14:
3531 case DW_OP_breg15:
3532 case DW_OP_breg16:
3533 case DW_OP_breg17:
3534 case DW_OP_breg18:
3535 case DW_OP_breg19:
3536 case DW_OP_breg20:
3537 case DW_OP_breg21:
3538 case DW_OP_breg22:
3539 case DW_OP_breg23:
3540 case DW_OP_breg24:
3541 case DW_OP_breg25:
3542 case DW_OP_breg26:
3543 case DW_OP_breg27:
3544 case DW_OP_breg28:
3545 case DW_OP_breg29:
3546 case DW_OP_breg30:
3547 case DW_OP_breg31:
3548 cfa->reg = op - DW_OP_breg0;
3549 cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
3550 break;
3551 case DW_OP_bregx:
3552 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3553 cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
3554 break;
3555 case DW_OP_deref:
3556 cfa->indirect = 1;
3557 break;
3558 case DW_OP_plus_uconst:
3559 cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
3560 break;
3561 default:
3562 internal_error ("DW_LOC_OP %s not implemented",
3563 dwarf_stack_op_name (ptr->dw_loc_opc));
3567 #endif /* .debug_frame support */
3569 /* And now, the support for symbolic debugging information. */
3570 #ifdef DWARF2_DEBUGGING_INFO
3572 /* .debug_str support. */
3573 static int output_indirect_string (void **, void *);
3575 static void dwarf2out_init (const char *);
3576 static void dwarf2out_finish (const char *);
3577 static void dwarf2out_define (unsigned int, const char *);
3578 static void dwarf2out_undef (unsigned int, const char *);
3579 static void dwarf2out_start_source_file (unsigned, const char *);
3580 static void dwarf2out_end_source_file (unsigned);
3581 static void dwarf2out_begin_block (unsigned, unsigned);
3582 static void dwarf2out_end_block (unsigned, unsigned);
3583 static bool dwarf2out_ignore_block (tree);
3584 static void dwarf2out_global_decl (tree);
3585 static void dwarf2out_type_decl (tree, int);
3586 static void dwarf2out_imported_module_or_decl (tree, tree);
3587 static void dwarf2out_abstract_function (tree);
3588 static void dwarf2out_var_location (rtx);
3589 static void dwarf2out_begin_function (tree);
3590 static void dwarf2out_switch_text_section (void);
3592 /* The debug hooks structure. */
3594 const struct gcc_debug_hooks dwarf2_debug_hooks =
3596 dwarf2out_init,
3597 dwarf2out_finish,
3598 dwarf2out_define,
3599 dwarf2out_undef,
3600 dwarf2out_start_source_file,
3601 dwarf2out_end_source_file,
3602 dwarf2out_begin_block,
3603 dwarf2out_end_block,
3604 dwarf2out_ignore_block,
3605 dwarf2out_source_line,
3606 dwarf2out_begin_prologue,
3607 debug_nothing_int_charstar, /* end_prologue */
3608 dwarf2out_end_epilogue,
3609 dwarf2out_begin_function,
3610 debug_nothing_int, /* end_function */
3611 dwarf2out_decl, /* function_decl */
3612 dwarf2out_global_decl,
3613 dwarf2out_type_decl, /* type_decl */
3614 dwarf2out_imported_module_or_decl,
3615 debug_nothing_tree, /* deferred_inline_function */
3616 /* The DWARF 2 backend tries to reduce debugging bloat by not
3617 emitting the abstract description of inline functions until
3618 something tries to reference them. */
3619 dwarf2out_abstract_function, /* outlining_inline_function */
3620 debug_nothing_rtx, /* label */
3621 debug_nothing_int, /* handle_pch */
3622 dwarf2out_var_location,
3623 dwarf2out_switch_text_section,
3624 1 /* start_end_main_source_file */
3626 #endif
3628 /* NOTE: In the comments in this file, many references are made to
3629 "Debugging Information Entries". This term is abbreviated as `DIE'
3630 throughout the remainder of this file. */
3632 /* An internal representation of the DWARF output is built, and then
3633 walked to generate the DWARF debugging info. The walk of the internal
3634 representation is done after the entire program has been compiled.
3635 The types below are used to describe the internal representation. */
3637 /* Various DIE's use offsets relative to the beginning of the
3638 .debug_info section to refer to each other. */
3640 typedef long int dw_offset;
3642 /* Define typedefs here to avoid circular dependencies. */
3644 typedef struct dw_attr_struct *dw_attr_ref;
3645 typedef struct dw_line_info_struct *dw_line_info_ref;
3646 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
3647 typedef struct pubname_struct *pubname_ref;
3648 typedef struct dw_ranges_struct *dw_ranges_ref;
3650 /* Each entry in the line_info_table maintains the file and
3651 line number associated with the label generated for that
3652 entry. The label gives the PC value associated with
3653 the line number entry. */
3655 typedef struct dw_line_info_struct GTY(())
3657 unsigned long dw_file_num;
3658 unsigned long dw_line_num;
3660 dw_line_info_entry;
3662 /* Line information for functions in separate sections; each one gets its
3663 own sequence. */
3664 typedef struct dw_separate_line_info_struct GTY(())
3666 unsigned long dw_file_num;
3667 unsigned long dw_line_num;
3668 unsigned long function;
3670 dw_separate_line_info_entry;
3672 /* Each DIE attribute has a field specifying the attribute kind,
3673 a link to the next attribute in the chain, and an attribute value.
3674 Attributes are typically linked below the DIE they modify. */
3676 typedef struct dw_attr_struct GTY(())
3678 enum dwarf_attribute dw_attr;
3679 dw_val_node dw_attr_val;
3681 dw_attr_node;
3683 DEF_VEC_O(dw_attr_node);
3684 DEF_VEC_ALLOC_O(dw_attr_node,gc);
3686 /* The Debugging Information Entry (DIE) structure */
3688 typedef struct die_struct GTY(())
3690 enum dwarf_tag die_tag;
3691 char *die_symbol;
3692 VEC(dw_attr_node,gc) * die_attr;
3693 dw_die_ref die_parent;
3694 dw_die_ref die_child;
3695 dw_die_ref die_sib;
3696 dw_die_ref die_definition; /* ref from a specification to its definition */
3697 dw_offset die_offset;
3698 unsigned long die_abbrev;
3699 int die_mark;
3700 /* Die is used and must not be pruned as unused. */
3701 int die_perennial_p;
3702 unsigned int decl_id;
3704 die_node;
3706 /* The pubname structure */
3708 typedef struct pubname_struct GTY(())
3710 dw_die_ref die;
3711 char *name;
3713 pubname_entry;
3715 struct dw_ranges_struct GTY(())
3717 int block_num;
3720 /* The limbo die list structure. */
3721 typedef struct limbo_die_struct GTY(())
3723 dw_die_ref die;
3724 tree created_for;
3725 struct limbo_die_struct *next;
3727 limbo_die_node;
3729 /* How to start an assembler comment. */
3730 #ifndef ASM_COMMENT_START
3731 #define ASM_COMMENT_START ";#"
3732 #endif
3734 /* Define a macro which returns nonzero for a TYPE_DECL which was
3735 implicitly generated for a tagged type.
3737 Note that unlike the gcc front end (which generates a NULL named
3738 TYPE_DECL node for each complete tagged type, each array type, and
3739 each function type node created) the g++ front end generates a
3740 _named_ TYPE_DECL node for each tagged type node created.
3741 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3742 generate a DW_TAG_typedef DIE for them. */
3744 #define TYPE_DECL_IS_STUB(decl) \
3745 (DECL_NAME (decl) == NULL_TREE \
3746 || (DECL_ARTIFICIAL (decl) \
3747 && is_tagged_type (TREE_TYPE (decl)) \
3748 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3749 /* This is necessary for stub decls that \
3750 appear in nested inline functions. */ \
3751 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3752 && (decl_ultimate_origin (decl) \
3753 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3755 /* Information concerning the compilation unit's programming
3756 language, and compiler version. */
3758 /* Fixed size portion of the DWARF compilation unit header. */
3759 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3760 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
3762 /* Fixed size portion of public names info. */
3763 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3765 /* Fixed size portion of the address range info. */
3766 #define DWARF_ARANGES_HEADER_SIZE \
3767 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3768 DWARF2_ADDR_SIZE * 2) \
3769 - DWARF_INITIAL_LENGTH_SIZE)
3771 /* Size of padding portion in the address range info. It must be
3772 aligned to twice the pointer size. */
3773 #define DWARF_ARANGES_PAD_SIZE \
3774 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3775 DWARF2_ADDR_SIZE * 2) \
3776 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3778 /* Use assembler line directives if available. */
3779 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3780 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3781 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3782 #else
3783 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3784 #endif
3785 #endif
3787 /* Minimum line offset in a special line info. opcode.
3788 This value was chosen to give a reasonable range of values. */
3789 #define DWARF_LINE_BASE -10
3791 /* First special line opcode - leave room for the standard opcodes. */
3792 #define DWARF_LINE_OPCODE_BASE 10
3794 /* Range of line offsets in a special line info. opcode. */
3795 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3797 /* Flag that indicates the initial value of the is_stmt_start flag.
3798 In the present implementation, we do not mark any lines as
3799 the beginning of a source statement, because that information
3800 is not made available by the GCC front-end. */
3801 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3803 #ifdef DWARF2_DEBUGGING_INFO
3804 /* This location is used by calc_die_sizes() to keep track
3805 the offset of each DIE within the .debug_info section. */
3806 static unsigned long next_die_offset;
3807 #endif
3809 /* Record the root of the DIE's built for the current compilation unit. */
3810 static GTY(()) dw_die_ref comp_unit_die;
3812 /* A list of DIEs with a NULL parent waiting to be relocated. */
3813 static GTY(()) limbo_die_node *limbo_die_list;
3815 /* Filenames referenced by this compilation unit. */
3816 static GTY(()) varray_type file_table;
3817 static GTY(()) varray_type file_table_emitted;
3818 static GTY(()) size_t file_table_last_lookup_index;
3820 /* A hash table of references to DIE's that describe declarations.
3821 The key is a DECL_UID() which is a unique number identifying each decl. */
3822 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
3824 /* Node of the variable location list. */
3825 struct var_loc_node GTY ((chain_next ("%h.next")))
3827 rtx GTY (()) var_loc_note;
3828 const char * GTY (()) label;
3829 const char * GTY (()) section_label;
3830 struct var_loc_node * GTY (()) next;
3833 /* Variable location list. */
3834 struct var_loc_list_def GTY (())
3836 struct var_loc_node * GTY (()) first;
3838 /* Do not mark the last element of the chained list because
3839 it is marked through the chain. */
3840 struct var_loc_node * GTY ((skip ("%h"))) last;
3842 /* DECL_UID of the variable decl. */
3843 unsigned int decl_id;
3845 typedef struct var_loc_list_def var_loc_list;
3848 /* Table of decl location linked lists. */
3849 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
3851 /* A pointer to the base of a list of references to DIE's that
3852 are uniquely identified by their tag, presence/absence of
3853 children DIE's, and list of attribute/value pairs. */
3854 static GTY((length ("abbrev_die_table_allocated")))
3855 dw_die_ref *abbrev_die_table;
3857 /* Number of elements currently allocated for abbrev_die_table. */
3858 static GTY(()) unsigned abbrev_die_table_allocated;
3860 /* Number of elements in type_die_table currently in use. */
3861 static GTY(()) unsigned abbrev_die_table_in_use;
3863 /* Size (in elements) of increments by which we may expand the
3864 abbrev_die_table. */
3865 #define ABBREV_DIE_TABLE_INCREMENT 256
3867 /* A pointer to the base of a table that contains line information
3868 for each source code line in .text in the compilation unit. */
3869 static GTY((length ("line_info_table_allocated")))
3870 dw_line_info_ref line_info_table;
3872 /* Number of elements currently allocated for line_info_table. */
3873 static GTY(()) unsigned line_info_table_allocated;
3875 /* Number of elements in line_info_table currently in use. */
3876 static GTY(()) unsigned line_info_table_in_use;
3878 /* True if the compilation unit places functions in more than one section. */
3879 static GTY(()) bool have_multiple_function_sections = false;
3881 /* A pointer to the base of a table that contains line information
3882 for each source code line outside of .text in the compilation unit. */
3883 static GTY ((length ("separate_line_info_table_allocated")))
3884 dw_separate_line_info_ref separate_line_info_table;
3886 /* Number of elements currently allocated for separate_line_info_table. */
3887 static GTY(()) unsigned separate_line_info_table_allocated;
3889 /* Number of elements in separate_line_info_table currently in use. */
3890 static GTY(()) unsigned separate_line_info_table_in_use;
3892 /* Size (in elements) of increments by which we may expand the
3893 line_info_table. */
3894 #define LINE_INFO_TABLE_INCREMENT 1024
3896 /* A pointer to the base of a table that contains a list of publicly
3897 accessible names. */
3898 static GTY ((length ("pubname_table_allocated"))) pubname_ref pubname_table;
3900 /* Number of elements currently allocated for pubname_table. */
3901 static GTY(()) unsigned pubname_table_allocated;
3903 /* Number of elements in pubname_table currently in use. */
3904 static GTY(()) unsigned pubname_table_in_use;
3906 /* Size (in elements) of increments by which we may expand the
3907 pubname_table. */
3908 #define PUBNAME_TABLE_INCREMENT 64
3910 /* Array of dies for which we should generate .debug_arange info. */
3911 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
3913 /* Number of elements currently allocated for arange_table. */
3914 static GTY(()) unsigned arange_table_allocated;
3916 /* Number of elements in arange_table currently in use. */
3917 static GTY(()) unsigned arange_table_in_use;
3919 /* Size (in elements) of increments by which we may expand the
3920 arange_table. */
3921 #define ARANGE_TABLE_INCREMENT 64
3923 /* Array of dies for which we should generate .debug_ranges info. */
3924 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
3926 /* Number of elements currently allocated for ranges_table. */
3927 static GTY(()) unsigned ranges_table_allocated;
3929 /* Number of elements in ranges_table currently in use. */
3930 static GTY(()) unsigned ranges_table_in_use;
3932 /* Size (in elements) of increments by which we may expand the
3933 ranges_table. */
3934 #define RANGES_TABLE_INCREMENT 64
3936 /* Whether we have location lists that need outputting */
3937 static GTY(()) bool have_location_lists;
3939 /* Unique label counter. */
3940 static GTY(()) unsigned int loclabel_num;
3942 #ifdef DWARF2_DEBUGGING_INFO
3943 /* Record whether the function being analyzed contains inlined functions. */
3944 static int current_function_has_inlines;
3945 #endif
3946 #if 0 && defined (MIPS_DEBUGGING_INFO)
3947 static int comp_unit_has_inlines;
3948 #endif
3950 /* Number of file tables emitted in maybe_emit_file(). */
3951 static GTY(()) int emitcount = 0;
3953 /* Number of internal labels generated by gen_internal_sym(). */
3954 static GTY(()) int label_num;
3956 #ifdef DWARF2_DEBUGGING_INFO
3958 /* Offset from the "steady-state frame pointer" to the frame base,
3959 within the current function. */
3960 static HOST_WIDE_INT frame_pointer_fb_offset;
3962 /* Forward declarations for functions defined in this file. */
3964 static int is_pseudo_reg (rtx);
3965 static tree type_main_variant (tree);
3966 static int is_tagged_type (tree);
3967 static const char *dwarf_tag_name (unsigned);
3968 static const char *dwarf_attr_name (unsigned);
3969 static const char *dwarf_form_name (unsigned);
3970 static tree decl_ultimate_origin (tree);
3971 static tree block_ultimate_origin (tree);
3972 static tree decl_class_context (tree);
3973 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
3974 static inline enum dw_val_class AT_class (dw_attr_ref);
3975 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3976 static inline unsigned AT_flag (dw_attr_ref);
3977 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3978 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3979 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3980 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3981 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
3982 unsigned long);
3983 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3984 unsigned int, unsigned char *);
3985 static hashval_t debug_str_do_hash (const void *);
3986 static int debug_str_eq (const void *, const void *);
3987 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3988 static inline const char *AT_string (dw_attr_ref);
3989 static int AT_string_form (dw_attr_ref);
3990 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3991 static void add_AT_specification (dw_die_ref, dw_die_ref);
3992 static inline dw_die_ref AT_ref (dw_attr_ref);
3993 static inline int AT_ref_external (dw_attr_ref);
3994 static inline void set_AT_ref_external (dw_attr_ref, int);
3995 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3996 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3997 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3998 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3999 dw_loc_list_ref);
4000 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
4001 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
4002 static inline rtx AT_addr (dw_attr_ref);
4003 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
4004 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
4005 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
4006 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
4007 unsigned HOST_WIDE_INT);
4008 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
4009 unsigned long);
4010 static inline const char *AT_lbl (dw_attr_ref);
4011 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
4012 static const char *get_AT_low_pc (dw_die_ref);
4013 static const char *get_AT_hi_pc (dw_die_ref);
4014 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
4015 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
4016 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
4017 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
4018 static bool is_c_family (void);
4019 static bool is_cxx (void);
4020 static bool is_java (void);
4021 static bool is_fortran (void);
4022 static bool is_ada (void);
4023 static void remove_AT (dw_die_ref, enum dwarf_attribute);
4024 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
4025 static inline void free_die (dw_die_ref);
4026 static void add_child_die (dw_die_ref, dw_die_ref);
4027 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
4028 static dw_die_ref lookup_type_die (tree);
4029 static void equate_type_number_to_die (tree, dw_die_ref);
4030 static hashval_t decl_die_table_hash (const void *);
4031 static int decl_die_table_eq (const void *, const void *);
4032 static dw_die_ref lookup_decl_die (tree);
4033 static hashval_t decl_loc_table_hash (const void *);
4034 static int decl_loc_table_eq (const void *, const void *);
4035 static var_loc_list *lookup_decl_loc (tree);
4036 static void equate_decl_number_to_die (tree, dw_die_ref);
4037 static void add_var_loc_to_decl (tree, struct var_loc_node *);
4038 static void print_spaces (FILE *);
4039 static void print_die (dw_die_ref, FILE *);
4040 static void print_dwarf_line_table (FILE *);
4041 static void reverse_die_lists (dw_die_ref);
4042 static void reverse_all_dies (dw_die_ref);
4043 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
4044 static dw_die_ref pop_compile_unit (dw_die_ref);
4045 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
4046 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
4047 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
4048 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
4049 static int same_dw_val_p (dw_val_node *, dw_val_node *, int *);
4050 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
4051 static int same_die_p (dw_die_ref, dw_die_ref, int *);
4052 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
4053 static void compute_section_prefix (dw_die_ref);
4054 static int is_type_die (dw_die_ref);
4055 static int is_comdat_die (dw_die_ref);
4056 static int is_symbol_die (dw_die_ref);
4057 static void assign_symbol_names (dw_die_ref);
4058 static void break_out_includes (dw_die_ref);
4059 static hashval_t htab_cu_hash (const void *);
4060 static int htab_cu_eq (const void *, const void *);
4061 static void htab_cu_del (void *);
4062 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
4063 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
4064 static void add_sibling_attributes (dw_die_ref);
4065 static void build_abbrev_table (dw_die_ref);
4066 static void output_location_lists (dw_die_ref);
4067 static int constant_size (long unsigned);
4068 static unsigned long size_of_die (dw_die_ref);
4069 static void calc_die_sizes (dw_die_ref);
4070 static void mark_dies (dw_die_ref);
4071 static void unmark_dies (dw_die_ref);
4072 static void unmark_all_dies (dw_die_ref);
4073 static unsigned long size_of_pubnames (void);
4074 static unsigned long size_of_aranges (void);
4075 static enum dwarf_form value_format (dw_attr_ref);
4076 static void output_value_format (dw_attr_ref);
4077 static void output_abbrev_section (void);
4078 static void output_die_symbol (dw_die_ref);
4079 static void output_die (dw_die_ref);
4080 static void output_compilation_unit_header (void);
4081 static void output_comp_unit (dw_die_ref, int);
4082 static const char *dwarf2_name (tree, int);
4083 static void add_pubname (tree, dw_die_ref);
4084 static void output_pubnames (void);
4085 static void add_arange (tree, dw_die_ref);
4086 static void output_aranges (void);
4087 static unsigned int add_ranges (tree);
4088 static void output_ranges (void);
4089 static void output_line_info (void);
4090 static void output_file_names (void);
4091 static dw_die_ref base_type_die (tree);
4092 static tree root_type (tree);
4093 static int is_base_type (tree);
4094 static bool is_subrange_type (tree);
4095 static dw_die_ref subrange_type_die (tree, dw_die_ref);
4096 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
4097 static int type_is_enum (tree);
4098 static unsigned int dbx_reg_number (rtx);
4099 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
4100 static dw_loc_descr_ref reg_loc_descriptor (rtx);
4101 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int);
4102 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx);
4103 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4104 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT);
4105 static int is_based_loc (rtx);
4106 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode);
4107 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx);
4108 static dw_loc_descr_ref loc_descriptor (rtx);
4109 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
4110 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
4111 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
4112 static tree field_type (tree);
4113 static unsigned int simple_type_align_in_bits (tree);
4114 static unsigned int simple_decl_align_in_bits (tree);
4115 static unsigned HOST_WIDE_INT simple_type_size_in_bits (tree);
4116 static HOST_WIDE_INT field_byte_offset (tree);
4117 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
4118 dw_loc_descr_ref);
4119 static void add_data_member_location_attribute (dw_die_ref, tree);
4120 static void add_const_value_attribute (dw_die_ref, rtx);
4121 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
4122 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4123 static void insert_float (rtx, unsigned char *);
4124 static rtx rtl_for_decl_location (tree);
4125 static void add_location_or_const_value_attribute (dw_die_ref, tree,
4126 enum dwarf_attribute);
4127 static void tree_add_const_value_attribute (dw_die_ref, tree);
4128 static void add_name_attribute (dw_die_ref, const char *);
4129 static void add_comp_dir_attribute (dw_die_ref);
4130 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
4131 static void add_subscript_info (dw_die_ref, tree);
4132 static void add_byte_size_attribute (dw_die_ref, tree);
4133 static void add_bit_offset_attribute (dw_die_ref, tree);
4134 static void add_bit_size_attribute (dw_die_ref, tree);
4135 static void add_prototyped_attribute (dw_die_ref, tree);
4136 static void add_abstract_origin_attribute (dw_die_ref, tree);
4137 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
4138 static void add_src_coords_attributes (dw_die_ref, tree);
4139 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
4140 static void push_decl_scope (tree);
4141 static void pop_decl_scope (void);
4142 static dw_die_ref scope_die_for (tree, dw_die_ref);
4143 static inline int local_scope_p (dw_die_ref);
4144 static inline int class_or_namespace_scope_p (dw_die_ref);
4145 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
4146 static void add_calling_convention_attribute (dw_die_ref, tree);
4147 static const char *type_tag (tree);
4148 static tree member_declared_type (tree);
4149 #if 0
4150 static const char *decl_start_label (tree);
4151 #endif
4152 static void gen_array_type_die (tree, dw_die_ref);
4153 #if 0
4154 static void gen_entry_point_die (tree, dw_die_ref);
4155 #endif
4156 static void gen_inlined_enumeration_type_die (tree, dw_die_ref);
4157 static void gen_inlined_structure_type_die (tree, dw_die_ref);
4158 static void gen_inlined_union_type_die (tree, dw_die_ref);
4159 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
4160 static dw_die_ref gen_formal_parameter_die (tree, dw_die_ref);
4161 static void gen_unspecified_parameters_die (tree, dw_die_ref);
4162 static void gen_formal_types_die (tree, dw_die_ref);
4163 static void gen_subprogram_die (tree, dw_die_ref);
4164 static void gen_variable_die (tree, dw_die_ref);
4165 static void gen_label_die (tree, dw_die_ref);
4166 static void gen_lexical_block_die (tree, dw_die_ref, int);
4167 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
4168 static void gen_field_die (tree, dw_die_ref);
4169 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
4170 static dw_die_ref gen_compile_unit_die (const char *);
4171 static void gen_inheritance_die (tree, tree, dw_die_ref);
4172 static void gen_member_die (tree, dw_die_ref);
4173 static void gen_struct_or_union_type_die (tree, dw_die_ref);
4174 static void gen_subroutine_type_die (tree, dw_die_ref);
4175 static void gen_typedef_die (tree, dw_die_ref);
4176 static void gen_type_die (tree, dw_die_ref);
4177 static void gen_tagged_type_instantiation_die (tree, dw_die_ref);
4178 static void gen_block_die (tree, dw_die_ref, int);
4179 static void decls_for_scope (tree, dw_die_ref, int);
4180 static int is_redundant_typedef (tree);
4181 static void gen_namespace_die (tree);
4182 static void gen_decl_die (tree, dw_die_ref);
4183 static dw_die_ref force_decl_die (tree);
4184 static dw_die_ref force_type_die (tree);
4185 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
4186 static void declare_in_namespace (tree, dw_die_ref);
4187 static unsigned lookup_filename (const char *);
4188 static void init_file_table (void);
4189 static void retry_incomplete_types (void);
4190 static void gen_type_die_for_member (tree, tree, dw_die_ref);
4191 static void splice_child_die (dw_die_ref, dw_die_ref);
4192 static int file_info_cmp (const void *, const void *);
4193 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
4194 const char *, const char *, unsigned);
4195 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
4196 const char *, const char *,
4197 const char *);
4198 static void output_loc_list (dw_loc_list_ref);
4199 static char *gen_internal_sym (const char *);
4201 static void prune_unmark_dies (dw_die_ref);
4202 static void prune_unused_types_mark (dw_die_ref, int);
4203 static void prune_unused_types_walk (dw_die_ref);
4204 static void prune_unused_types_walk_attribs (dw_die_ref);
4205 static void prune_unused_types_prune (dw_die_ref);
4206 static void prune_unused_types (void);
4207 static int maybe_emit_file (int);
4209 /* Section names used to hold DWARF debugging information. */
4210 #ifndef DEBUG_INFO_SECTION
4211 #define DEBUG_INFO_SECTION ".debug_info"
4212 #endif
4213 #ifndef DEBUG_ABBREV_SECTION
4214 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
4215 #endif
4216 #ifndef DEBUG_ARANGES_SECTION
4217 #define DEBUG_ARANGES_SECTION ".debug_aranges"
4218 #endif
4219 #ifndef DEBUG_MACINFO_SECTION
4220 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
4221 #endif
4222 #ifndef DEBUG_LINE_SECTION
4223 #define DEBUG_LINE_SECTION ".debug_line"
4224 #endif
4225 #ifndef DEBUG_LOC_SECTION
4226 #define DEBUG_LOC_SECTION ".debug_loc"
4227 #endif
4228 #ifndef DEBUG_PUBNAMES_SECTION
4229 #define DEBUG_PUBNAMES_SECTION ".debug_pubnames"
4230 #endif
4231 #ifndef DEBUG_STR_SECTION
4232 #define DEBUG_STR_SECTION ".debug_str"
4233 #endif
4234 #ifndef DEBUG_RANGES_SECTION
4235 #define DEBUG_RANGES_SECTION ".debug_ranges"
4236 #endif
4238 /* Standard ELF section names for compiled code and data. */
4239 #ifndef TEXT_SECTION_NAME
4240 #define TEXT_SECTION_NAME ".text"
4241 #endif
4243 /* Section flags for .debug_str section. */
4244 #define DEBUG_STR_SECTION_FLAGS \
4245 (HAVE_GAS_SHF_MERGE && flag_merge_constants \
4246 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
4247 : SECTION_DEBUG)
4249 /* Labels we insert at beginning sections we can reference instead of
4250 the section names themselves. */
4252 #ifndef TEXT_SECTION_LABEL
4253 #define TEXT_SECTION_LABEL "Ltext"
4254 #endif
4255 #ifndef COLD_TEXT_SECTION_LABEL
4256 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
4257 #endif
4258 #ifndef DEBUG_LINE_SECTION_LABEL
4259 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
4260 #endif
4261 #ifndef DEBUG_INFO_SECTION_LABEL
4262 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
4263 #endif
4264 #ifndef DEBUG_ABBREV_SECTION_LABEL
4265 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
4266 #endif
4267 #ifndef DEBUG_LOC_SECTION_LABEL
4268 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
4269 #endif
4270 #ifndef DEBUG_RANGES_SECTION_LABEL
4271 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
4272 #endif
4273 #ifndef DEBUG_MACINFO_SECTION_LABEL
4274 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
4275 #endif
4277 /* Definitions of defaults for formats and names of various special
4278 (artificial) labels which may be generated within this file (when the -g
4279 options is used and DWARF2_DEBUGGING_INFO is in effect.
4280 If necessary, these may be overridden from within the tm.h file, but
4281 typically, overriding these defaults is unnecessary. */
4283 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4284 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4285 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4286 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4287 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4288 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4289 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4290 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4291 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4292 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4294 #ifndef TEXT_END_LABEL
4295 #define TEXT_END_LABEL "Letext"
4296 #endif
4297 #ifndef COLD_END_LABEL
4298 #define COLD_END_LABEL "Letext_cold"
4299 #endif
4300 #ifndef BLOCK_BEGIN_LABEL
4301 #define BLOCK_BEGIN_LABEL "LBB"
4302 #endif
4303 #ifndef BLOCK_END_LABEL
4304 #define BLOCK_END_LABEL "LBE"
4305 #endif
4306 #ifndef LINE_CODE_LABEL
4307 #define LINE_CODE_LABEL "LM"
4308 #endif
4309 #ifndef SEPARATE_LINE_CODE_LABEL
4310 #define SEPARATE_LINE_CODE_LABEL "LSM"
4311 #endif
4313 /* We allow a language front-end to designate a function that is to be
4314 called to "demangle" any name before it is put into a DIE. */
4316 static const char *(*demangle_name_func) (const char *);
4318 void
4319 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4321 demangle_name_func = func;
4324 /* Test if rtl node points to a pseudo register. */
4326 static inline int
4327 is_pseudo_reg (rtx rtl)
4329 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4330 || (GET_CODE (rtl) == SUBREG
4331 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4334 /* Return a reference to a type, with its const and volatile qualifiers
4335 removed. */
4337 static inline tree
4338 type_main_variant (tree type)
4340 type = TYPE_MAIN_VARIANT (type);
4342 /* ??? There really should be only one main variant among any group of
4343 variants of a given type (and all of the MAIN_VARIANT values for all
4344 members of the group should point to that one type) but sometimes the C
4345 front-end messes this up for array types, so we work around that bug
4346 here. */
4347 if (TREE_CODE (type) == ARRAY_TYPE)
4348 while (type != TYPE_MAIN_VARIANT (type))
4349 type = TYPE_MAIN_VARIANT (type);
4351 return type;
4354 /* Return nonzero if the given type node represents a tagged type. */
4356 static inline int
4357 is_tagged_type (tree type)
4359 enum tree_code code = TREE_CODE (type);
4361 return (code == RECORD_TYPE || code == UNION_TYPE
4362 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4365 /* Convert a DIE tag into its string name. */
4367 static const char *
4368 dwarf_tag_name (unsigned int tag)
4370 switch (tag)
4372 case DW_TAG_padding:
4373 return "DW_TAG_padding";
4374 case DW_TAG_array_type:
4375 return "DW_TAG_array_type";
4376 case DW_TAG_class_type:
4377 return "DW_TAG_class_type";
4378 case DW_TAG_entry_point:
4379 return "DW_TAG_entry_point";
4380 case DW_TAG_enumeration_type:
4381 return "DW_TAG_enumeration_type";
4382 case DW_TAG_formal_parameter:
4383 return "DW_TAG_formal_parameter";
4384 case DW_TAG_imported_declaration:
4385 return "DW_TAG_imported_declaration";
4386 case DW_TAG_label:
4387 return "DW_TAG_label";
4388 case DW_TAG_lexical_block:
4389 return "DW_TAG_lexical_block";
4390 case DW_TAG_member:
4391 return "DW_TAG_member";
4392 case DW_TAG_pointer_type:
4393 return "DW_TAG_pointer_type";
4394 case DW_TAG_reference_type:
4395 return "DW_TAG_reference_type";
4396 case DW_TAG_compile_unit:
4397 return "DW_TAG_compile_unit";
4398 case DW_TAG_string_type:
4399 return "DW_TAG_string_type";
4400 case DW_TAG_structure_type:
4401 return "DW_TAG_structure_type";
4402 case DW_TAG_subroutine_type:
4403 return "DW_TAG_subroutine_type";
4404 case DW_TAG_typedef:
4405 return "DW_TAG_typedef";
4406 case DW_TAG_union_type:
4407 return "DW_TAG_union_type";
4408 case DW_TAG_unspecified_parameters:
4409 return "DW_TAG_unspecified_parameters";
4410 case DW_TAG_variant:
4411 return "DW_TAG_variant";
4412 case DW_TAG_common_block:
4413 return "DW_TAG_common_block";
4414 case DW_TAG_common_inclusion:
4415 return "DW_TAG_common_inclusion";
4416 case DW_TAG_inheritance:
4417 return "DW_TAG_inheritance";
4418 case DW_TAG_inlined_subroutine:
4419 return "DW_TAG_inlined_subroutine";
4420 case DW_TAG_module:
4421 return "DW_TAG_module";
4422 case DW_TAG_ptr_to_member_type:
4423 return "DW_TAG_ptr_to_member_type";
4424 case DW_TAG_set_type:
4425 return "DW_TAG_set_type";
4426 case DW_TAG_subrange_type:
4427 return "DW_TAG_subrange_type";
4428 case DW_TAG_with_stmt:
4429 return "DW_TAG_with_stmt";
4430 case DW_TAG_access_declaration:
4431 return "DW_TAG_access_declaration";
4432 case DW_TAG_base_type:
4433 return "DW_TAG_base_type";
4434 case DW_TAG_catch_block:
4435 return "DW_TAG_catch_block";
4436 case DW_TAG_const_type:
4437 return "DW_TAG_const_type";
4438 case DW_TAG_constant:
4439 return "DW_TAG_constant";
4440 case DW_TAG_enumerator:
4441 return "DW_TAG_enumerator";
4442 case DW_TAG_file_type:
4443 return "DW_TAG_file_type";
4444 case DW_TAG_friend:
4445 return "DW_TAG_friend";
4446 case DW_TAG_namelist:
4447 return "DW_TAG_namelist";
4448 case DW_TAG_namelist_item:
4449 return "DW_TAG_namelist_item";
4450 case DW_TAG_namespace:
4451 return "DW_TAG_namespace";
4452 case DW_TAG_packed_type:
4453 return "DW_TAG_packed_type";
4454 case DW_TAG_subprogram:
4455 return "DW_TAG_subprogram";
4456 case DW_TAG_template_type_param:
4457 return "DW_TAG_template_type_param";
4458 case DW_TAG_template_value_param:
4459 return "DW_TAG_template_value_param";
4460 case DW_TAG_thrown_type:
4461 return "DW_TAG_thrown_type";
4462 case DW_TAG_try_block:
4463 return "DW_TAG_try_block";
4464 case DW_TAG_variant_part:
4465 return "DW_TAG_variant_part";
4466 case DW_TAG_variable:
4467 return "DW_TAG_variable";
4468 case DW_TAG_volatile_type:
4469 return "DW_TAG_volatile_type";
4470 case DW_TAG_imported_module:
4471 return "DW_TAG_imported_module";
4472 case DW_TAG_MIPS_loop:
4473 return "DW_TAG_MIPS_loop";
4474 case DW_TAG_format_label:
4475 return "DW_TAG_format_label";
4476 case DW_TAG_function_template:
4477 return "DW_TAG_function_template";
4478 case DW_TAG_class_template:
4479 return "DW_TAG_class_template";
4480 case DW_TAG_GNU_BINCL:
4481 return "DW_TAG_GNU_BINCL";
4482 case DW_TAG_GNU_EINCL:
4483 return "DW_TAG_GNU_EINCL";
4484 default:
4485 return "DW_TAG_<unknown>";
4489 /* Convert a DWARF attribute code into its string name. */
4491 static const char *
4492 dwarf_attr_name (unsigned int attr)
4494 switch (attr)
4496 case DW_AT_sibling:
4497 return "DW_AT_sibling";
4498 case DW_AT_location:
4499 return "DW_AT_location";
4500 case DW_AT_name:
4501 return "DW_AT_name";
4502 case DW_AT_ordering:
4503 return "DW_AT_ordering";
4504 case DW_AT_subscr_data:
4505 return "DW_AT_subscr_data";
4506 case DW_AT_byte_size:
4507 return "DW_AT_byte_size";
4508 case DW_AT_bit_offset:
4509 return "DW_AT_bit_offset";
4510 case DW_AT_bit_size:
4511 return "DW_AT_bit_size";
4512 case DW_AT_element_list:
4513 return "DW_AT_element_list";
4514 case DW_AT_stmt_list:
4515 return "DW_AT_stmt_list";
4516 case DW_AT_low_pc:
4517 return "DW_AT_low_pc";
4518 case DW_AT_high_pc:
4519 return "DW_AT_high_pc";
4520 case DW_AT_language:
4521 return "DW_AT_language";
4522 case DW_AT_member:
4523 return "DW_AT_member";
4524 case DW_AT_discr:
4525 return "DW_AT_discr";
4526 case DW_AT_discr_value:
4527 return "DW_AT_discr_value";
4528 case DW_AT_visibility:
4529 return "DW_AT_visibility";
4530 case DW_AT_import:
4531 return "DW_AT_import";
4532 case DW_AT_string_length:
4533 return "DW_AT_string_length";
4534 case DW_AT_common_reference:
4535 return "DW_AT_common_reference";
4536 case DW_AT_comp_dir:
4537 return "DW_AT_comp_dir";
4538 case DW_AT_const_value:
4539 return "DW_AT_const_value";
4540 case DW_AT_containing_type:
4541 return "DW_AT_containing_type";
4542 case DW_AT_default_value:
4543 return "DW_AT_default_value";
4544 case DW_AT_inline:
4545 return "DW_AT_inline";
4546 case DW_AT_is_optional:
4547 return "DW_AT_is_optional";
4548 case DW_AT_lower_bound:
4549 return "DW_AT_lower_bound";
4550 case DW_AT_producer:
4551 return "DW_AT_producer";
4552 case DW_AT_prototyped:
4553 return "DW_AT_prototyped";
4554 case DW_AT_return_addr:
4555 return "DW_AT_return_addr";
4556 case DW_AT_start_scope:
4557 return "DW_AT_start_scope";
4558 case DW_AT_stride_size:
4559 return "DW_AT_stride_size";
4560 case DW_AT_upper_bound:
4561 return "DW_AT_upper_bound";
4562 case DW_AT_abstract_origin:
4563 return "DW_AT_abstract_origin";
4564 case DW_AT_accessibility:
4565 return "DW_AT_accessibility";
4566 case DW_AT_address_class:
4567 return "DW_AT_address_class";
4568 case DW_AT_artificial:
4569 return "DW_AT_artificial";
4570 case DW_AT_base_types:
4571 return "DW_AT_base_types";
4572 case DW_AT_calling_convention:
4573 return "DW_AT_calling_convention";
4574 case DW_AT_count:
4575 return "DW_AT_count";
4576 case DW_AT_data_member_location:
4577 return "DW_AT_data_member_location";
4578 case DW_AT_decl_column:
4579 return "DW_AT_decl_column";
4580 case DW_AT_decl_file:
4581 return "DW_AT_decl_file";
4582 case DW_AT_decl_line:
4583 return "DW_AT_decl_line";
4584 case DW_AT_declaration:
4585 return "DW_AT_declaration";
4586 case DW_AT_discr_list:
4587 return "DW_AT_discr_list";
4588 case DW_AT_encoding:
4589 return "DW_AT_encoding";
4590 case DW_AT_external:
4591 return "DW_AT_external";
4592 case DW_AT_frame_base:
4593 return "DW_AT_frame_base";
4594 case DW_AT_friend:
4595 return "DW_AT_friend";
4596 case DW_AT_identifier_case:
4597 return "DW_AT_identifier_case";
4598 case DW_AT_macro_info:
4599 return "DW_AT_macro_info";
4600 case DW_AT_namelist_items:
4601 return "DW_AT_namelist_items";
4602 case DW_AT_priority:
4603 return "DW_AT_priority";
4604 case DW_AT_segment:
4605 return "DW_AT_segment";
4606 case DW_AT_specification:
4607 return "DW_AT_specification";
4608 case DW_AT_static_link:
4609 return "DW_AT_static_link";
4610 case DW_AT_type:
4611 return "DW_AT_type";
4612 case DW_AT_use_location:
4613 return "DW_AT_use_location";
4614 case DW_AT_variable_parameter:
4615 return "DW_AT_variable_parameter";
4616 case DW_AT_virtuality:
4617 return "DW_AT_virtuality";
4618 case DW_AT_vtable_elem_location:
4619 return "DW_AT_vtable_elem_location";
4621 case DW_AT_allocated:
4622 return "DW_AT_allocated";
4623 case DW_AT_associated:
4624 return "DW_AT_associated";
4625 case DW_AT_data_location:
4626 return "DW_AT_data_location";
4627 case DW_AT_stride:
4628 return "DW_AT_stride";
4629 case DW_AT_entry_pc:
4630 return "DW_AT_entry_pc";
4631 case DW_AT_use_UTF8:
4632 return "DW_AT_use_UTF8";
4633 case DW_AT_extension:
4634 return "DW_AT_extension";
4635 case DW_AT_ranges:
4636 return "DW_AT_ranges";
4637 case DW_AT_trampoline:
4638 return "DW_AT_trampoline";
4639 case DW_AT_call_column:
4640 return "DW_AT_call_column";
4641 case DW_AT_call_file:
4642 return "DW_AT_call_file";
4643 case DW_AT_call_line:
4644 return "DW_AT_call_line";
4646 case DW_AT_MIPS_fde:
4647 return "DW_AT_MIPS_fde";
4648 case DW_AT_MIPS_loop_begin:
4649 return "DW_AT_MIPS_loop_begin";
4650 case DW_AT_MIPS_tail_loop_begin:
4651 return "DW_AT_MIPS_tail_loop_begin";
4652 case DW_AT_MIPS_epilog_begin:
4653 return "DW_AT_MIPS_epilog_begin";
4654 case DW_AT_MIPS_loop_unroll_factor:
4655 return "DW_AT_MIPS_loop_unroll_factor";
4656 case DW_AT_MIPS_software_pipeline_depth:
4657 return "DW_AT_MIPS_software_pipeline_depth";
4658 case DW_AT_MIPS_linkage_name:
4659 return "DW_AT_MIPS_linkage_name";
4660 case DW_AT_MIPS_stride:
4661 return "DW_AT_MIPS_stride";
4662 case DW_AT_MIPS_abstract_name:
4663 return "DW_AT_MIPS_abstract_name";
4664 case DW_AT_MIPS_clone_origin:
4665 return "DW_AT_MIPS_clone_origin";
4666 case DW_AT_MIPS_has_inlines:
4667 return "DW_AT_MIPS_has_inlines";
4669 case DW_AT_sf_names:
4670 return "DW_AT_sf_names";
4671 case DW_AT_src_info:
4672 return "DW_AT_src_info";
4673 case DW_AT_mac_info:
4674 return "DW_AT_mac_info";
4675 case DW_AT_src_coords:
4676 return "DW_AT_src_coords";
4677 case DW_AT_body_begin:
4678 return "DW_AT_body_begin";
4679 case DW_AT_body_end:
4680 return "DW_AT_body_end";
4681 case DW_AT_GNU_vector:
4682 return "DW_AT_GNU_vector";
4684 case DW_AT_VMS_rtnbeg_pd_address:
4685 return "DW_AT_VMS_rtnbeg_pd_address";
4687 default:
4688 return "DW_AT_<unknown>";
4692 /* Convert a DWARF value form code into its string name. */
4694 static const char *
4695 dwarf_form_name (unsigned int form)
4697 switch (form)
4699 case DW_FORM_addr:
4700 return "DW_FORM_addr";
4701 case DW_FORM_block2:
4702 return "DW_FORM_block2";
4703 case DW_FORM_block4:
4704 return "DW_FORM_block4";
4705 case DW_FORM_data2:
4706 return "DW_FORM_data2";
4707 case DW_FORM_data4:
4708 return "DW_FORM_data4";
4709 case DW_FORM_data8:
4710 return "DW_FORM_data8";
4711 case DW_FORM_string:
4712 return "DW_FORM_string";
4713 case DW_FORM_block:
4714 return "DW_FORM_block";
4715 case DW_FORM_block1:
4716 return "DW_FORM_block1";
4717 case DW_FORM_data1:
4718 return "DW_FORM_data1";
4719 case DW_FORM_flag:
4720 return "DW_FORM_flag";
4721 case DW_FORM_sdata:
4722 return "DW_FORM_sdata";
4723 case DW_FORM_strp:
4724 return "DW_FORM_strp";
4725 case DW_FORM_udata:
4726 return "DW_FORM_udata";
4727 case DW_FORM_ref_addr:
4728 return "DW_FORM_ref_addr";
4729 case DW_FORM_ref1:
4730 return "DW_FORM_ref1";
4731 case DW_FORM_ref2:
4732 return "DW_FORM_ref2";
4733 case DW_FORM_ref4:
4734 return "DW_FORM_ref4";
4735 case DW_FORM_ref8:
4736 return "DW_FORM_ref8";
4737 case DW_FORM_ref_udata:
4738 return "DW_FORM_ref_udata";
4739 case DW_FORM_indirect:
4740 return "DW_FORM_indirect";
4741 default:
4742 return "DW_FORM_<unknown>";
4746 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4747 instance of an inlined instance of a decl which is local to an inline
4748 function, so we have to trace all of the way back through the origin chain
4749 to find out what sort of node actually served as the original seed for the
4750 given block. */
4752 static tree
4753 decl_ultimate_origin (tree decl)
4755 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4756 return NULL_TREE;
4758 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4759 nodes in the function to point to themselves; ignore that if
4760 we're trying to output the abstract instance of this function. */
4761 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4762 return NULL_TREE;
4764 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4765 most distant ancestor, this should never happen. */
4766 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4768 return DECL_ABSTRACT_ORIGIN (decl);
4771 /* Determine the "ultimate origin" of a block. The block may be an inlined
4772 instance of an inlined instance of a block which is local to an inline
4773 function, so we have to trace all of the way back through the origin chain
4774 to find out what sort of node actually served as the original seed for the
4775 given block. */
4777 static tree
4778 block_ultimate_origin (tree block)
4780 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
4782 /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
4783 nodes in the function to point to themselves; ignore that if
4784 we're trying to output the abstract instance of this function. */
4785 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
4786 return NULL_TREE;
4788 if (immediate_origin == NULL_TREE)
4789 return NULL_TREE;
4790 else
4792 tree ret_val;
4793 tree lookahead = immediate_origin;
4797 ret_val = lookahead;
4798 lookahead = (TREE_CODE (ret_val) == BLOCK
4799 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
4801 while (lookahead != NULL && lookahead != ret_val);
4803 /* The block's abstract origin chain may not be the *ultimate* origin of
4804 the block. It could lead to a DECL that has an abstract origin set.
4805 If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
4806 will give us if it has one). Note that DECL's abstract origins are
4807 supposed to be the most distant ancestor (or so decl_ultimate_origin
4808 claims), so we don't need to loop following the DECL origins. */
4809 if (DECL_P (ret_val))
4810 return DECL_ORIGIN (ret_val);
4812 return ret_val;
4816 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4817 of a virtual function may refer to a base class, so we check the 'this'
4818 parameter. */
4820 static tree
4821 decl_class_context (tree decl)
4823 tree context = NULL_TREE;
4825 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4826 context = DECL_CONTEXT (decl);
4827 else
4828 context = TYPE_MAIN_VARIANT
4829 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4831 if (context && !TYPE_P (context))
4832 context = NULL_TREE;
4834 return context;
4837 /* Add an attribute/value pair to a DIE. We build the lists up in reverse
4838 addition order, and correct that in reverse_all_dies. */
4840 static inline void
4841 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
4843 /* Maybe this should be an assert? */
4844 if (die == NULL)
4845 return;
4847 if (die->die_attr == NULL)
4848 die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
4849 VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
4852 static inline enum dw_val_class
4853 AT_class (dw_attr_ref a)
4855 return a->dw_attr_val.val_class;
4858 /* Add a flag value attribute to a DIE. */
4860 static inline void
4861 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4863 dw_attr_node attr;
4865 attr.dw_attr = attr_kind;
4866 attr.dw_attr_val.val_class = dw_val_class_flag;
4867 attr.dw_attr_val.v.val_flag = flag;
4868 add_dwarf_attr (die, &attr);
4871 static inline unsigned
4872 AT_flag (dw_attr_ref a)
4874 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4875 return a->dw_attr_val.v.val_flag;
4878 /* Add a signed integer attribute value to a DIE. */
4880 static inline void
4881 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4883 dw_attr_node attr;
4885 attr.dw_attr = attr_kind;
4886 attr.dw_attr_val.val_class = dw_val_class_const;
4887 attr.dw_attr_val.v.val_int = int_val;
4888 add_dwarf_attr (die, &attr);
4891 static inline HOST_WIDE_INT
4892 AT_int (dw_attr_ref a)
4894 gcc_assert (a && AT_class (a) == dw_val_class_const);
4895 return a->dw_attr_val.v.val_int;
4898 /* Add an unsigned integer attribute value to a DIE. */
4900 static inline void
4901 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4902 unsigned HOST_WIDE_INT unsigned_val)
4904 dw_attr_node attr;
4906 attr.dw_attr = attr_kind;
4907 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4908 attr.dw_attr_val.v.val_unsigned = unsigned_val;
4909 add_dwarf_attr (die, &attr);
4912 static inline unsigned HOST_WIDE_INT
4913 AT_unsigned (dw_attr_ref a)
4915 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
4916 return a->dw_attr_val.v.val_unsigned;
4919 /* Add an unsigned double integer attribute value to a DIE. */
4921 static inline void
4922 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
4923 long unsigned int val_hi, long unsigned int val_low)
4925 dw_attr_node attr;
4927 attr.dw_attr = attr_kind;
4928 attr.dw_attr_val.val_class = dw_val_class_long_long;
4929 attr.dw_attr_val.v.val_long_long.hi = val_hi;
4930 attr.dw_attr_val.v.val_long_long.low = val_low;
4931 add_dwarf_attr (die, &attr);
4934 /* Add a floating point attribute value to a DIE and return it. */
4936 static inline void
4937 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4938 unsigned int length, unsigned int elt_size, unsigned char *array)
4940 dw_attr_node attr;
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_node attr;
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 = attr_kind;
4987 attr.dw_attr_val.val_class = dw_val_class_str;
4988 attr.dw_attr_val.v.val_str = node;
4989 add_dwarf_attr (die, &attr);
4992 static inline const char *
4993 AT_string (dw_attr_ref a)
4995 gcc_assert (a && AT_class (a) == dw_val_class_str);
4996 return a->dw_attr_val.v.val_str->str;
4999 /* Find out whether a string should be output inline in DIE
5000 or out-of-line in .debug_str section. */
5002 static int
5003 AT_string_form (dw_attr_ref a)
5005 struct indirect_string_node *node;
5006 unsigned int len;
5007 char label[32];
5009 gcc_assert (a && AT_class (a) == dw_val_class_str);
5011 node = a->dw_attr_val.v.val_str;
5012 if (node->form)
5013 return node->form;
5015 len = strlen (node->str) + 1;
5017 /* If the string is shorter or equal to the size of the reference, it is
5018 always better to put it inline. */
5019 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
5020 return node->form = DW_FORM_string;
5022 /* If we cannot expect the linker to merge strings in .debug_str
5023 section, only put it into .debug_str if it is worth even in this
5024 single module. */
5025 if ((debug_str_section->common.flags & SECTION_MERGE) == 0
5026 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
5027 return node->form = DW_FORM_string;
5029 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
5030 ++dw2_string_counter;
5031 node->label = xstrdup (label);
5033 return node->form = DW_FORM_strp;
5036 /* Add a DIE reference attribute value to a DIE. */
5038 static inline void
5039 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
5041 dw_attr_node attr;
5043 attr.dw_attr = attr_kind;
5044 attr.dw_attr_val.val_class = dw_val_class_die_ref;
5045 attr.dw_attr_val.v.val_die_ref.die = targ_die;
5046 attr.dw_attr_val.v.val_die_ref.external = 0;
5047 add_dwarf_attr (die, &attr);
5050 /* Add an AT_specification attribute to a DIE, and also make the back
5051 pointer from the specification to the definition. */
5053 static inline void
5054 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
5056 add_AT_die_ref (die, DW_AT_specification, targ_die);
5057 gcc_assert (!targ_die->die_definition);
5058 targ_die->die_definition = die;
5061 static inline dw_die_ref
5062 AT_ref (dw_attr_ref a)
5064 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5065 return a->dw_attr_val.v.val_die_ref.die;
5068 static inline int
5069 AT_ref_external (dw_attr_ref a)
5071 if (a && AT_class (a) == dw_val_class_die_ref)
5072 return a->dw_attr_val.v.val_die_ref.external;
5074 return 0;
5077 static inline void
5078 set_AT_ref_external (dw_attr_ref a, int i)
5080 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5081 a->dw_attr_val.v.val_die_ref.external = i;
5084 /* Add an FDE reference attribute value to a DIE. */
5086 static inline void
5087 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
5089 dw_attr_node attr;
5091 attr.dw_attr = attr_kind;
5092 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
5093 attr.dw_attr_val.v.val_fde_index = targ_fde;
5094 add_dwarf_attr (die, &attr);
5097 /* Add a location description attribute value to a DIE. */
5099 static inline void
5100 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
5102 dw_attr_node attr;
5104 attr.dw_attr = attr_kind;
5105 attr.dw_attr_val.val_class = dw_val_class_loc;
5106 attr.dw_attr_val.v.val_loc = loc;
5107 add_dwarf_attr (die, &attr);
5110 static inline dw_loc_descr_ref
5111 AT_loc (dw_attr_ref a)
5113 gcc_assert (a && AT_class (a) == dw_val_class_loc);
5114 return a->dw_attr_val.v.val_loc;
5117 static inline void
5118 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
5120 dw_attr_node attr;
5122 attr.dw_attr = attr_kind;
5123 attr.dw_attr_val.val_class = dw_val_class_loc_list;
5124 attr.dw_attr_val.v.val_loc_list = loc_list;
5125 add_dwarf_attr (die, &attr);
5126 have_location_lists = true;
5129 static inline dw_loc_list_ref
5130 AT_loc_list (dw_attr_ref a)
5132 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
5133 return a->dw_attr_val.v.val_loc_list;
5136 /* Add an address constant attribute value to a DIE. */
5138 static inline void
5139 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
5141 dw_attr_node attr;
5143 attr.dw_attr = attr_kind;
5144 attr.dw_attr_val.val_class = dw_val_class_addr;
5145 attr.dw_attr_val.v.val_addr = addr;
5146 add_dwarf_attr (die, &attr);
5149 static inline rtx
5150 AT_addr (dw_attr_ref a)
5152 gcc_assert (a && AT_class (a) == dw_val_class_addr);
5153 return a->dw_attr_val.v.val_addr;
5156 /* Add a label identifier attribute value to a DIE. */
5158 static inline void
5159 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
5161 dw_attr_node attr;
5163 attr.dw_attr = attr_kind;
5164 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
5165 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5166 add_dwarf_attr (die, &attr);
5169 /* Add a section offset attribute value to a DIE, an offset into the
5170 debug_line section. */
5172 static inline void
5173 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5174 const char *label)
5176 dw_attr_node attr;
5178 attr.dw_attr = attr_kind;
5179 attr.dw_attr_val.val_class = dw_val_class_lineptr;
5180 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5181 add_dwarf_attr (die, &attr);
5184 /* Add a section offset attribute value to a DIE, an offset into the
5185 debug_macinfo section. */
5187 static inline void
5188 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5189 const char *label)
5191 dw_attr_node attr;
5193 attr.dw_attr = attr_kind;
5194 attr.dw_attr_val.val_class = dw_val_class_macptr;
5195 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5196 add_dwarf_attr (die, &attr);
5199 /* Add an offset attribute value to a DIE. */
5201 static inline void
5202 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
5203 unsigned HOST_WIDE_INT offset)
5205 dw_attr_node attr;
5207 attr.dw_attr = attr_kind;
5208 attr.dw_attr_val.val_class = dw_val_class_offset;
5209 attr.dw_attr_val.v.val_offset = offset;
5210 add_dwarf_attr (die, &attr);
5213 /* Add an range_list attribute value to a DIE. */
5215 static void
5216 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5217 long unsigned int offset)
5219 dw_attr_node attr;
5221 attr.dw_attr = attr_kind;
5222 attr.dw_attr_val.val_class = dw_val_class_range_list;
5223 attr.dw_attr_val.v.val_offset = offset;
5224 add_dwarf_attr (die, &attr);
5227 static inline const char *
5228 AT_lbl (dw_attr_ref a)
5230 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5231 || AT_class (a) == dw_val_class_lineptr
5232 || AT_class (a) == dw_val_class_macptr));
5233 return a->dw_attr_val.v.val_lbl_id;
5236 /* Get the attribute of type attr_kind. */
5238 static dw_attr_ref
5239 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5241 dw_attr_ref a;
5242 unsigned ix;
5243 dw_die_ref spec = NULL;
5245 if (! die)
5246 return NULL;
5248 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5249 if (a->dw_attr == attr_kind)
5250 return a;
5251 else if (a->dw_attr == DW_AT_specification
5252 || a->dw_attr == DW_AT_abstract_origin)
5253 spec = AT_ref (a);
5255 if (spec)
5256 return get_AT (spec, attr_kind);
5258 return NULL;
5261 /* Return the "low pc" attribute value, typically associated with a subprogram
5262 DIE. Return null if the "low pc" attribute is either not present, or if it
5263 cannot be represented as an assembler label identifier. */
5265 static inline const char *
5266 get_AT_low_pc (dw_die_ref die)
5268 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
5270 return a ? AT_lbl (a) : NULL;
5273 /* Return the "high pc" attribute value, typically associated with a subprogram
5274 DIE. Return null if the "high pc" attribute is either not present, or if it
5275 cannot be represented as an assembler label identifier. */
5277 static inline const char *
5278 get_AT_hi_pc (dw_die_ref die)
5280 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
5282 return a ? AT_lbl (a) : NULL;
5285 /* Return the value of the string attribute designated by ATTR_KIND, or
5286 NULL if it is not present. */
5288 static inline const char *
5289 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5291 dw_attr_ref a = get_AT (die, attr_kind);
5293 return a ? AT_string (a) : NULL;
5296 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5297 if it is not present. */
5299 static inline int
5300 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5302 dw_attr_ref a = get_AT (die, attr_kind);
5304 return a ? AT_flag (a) : 0;
5307 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5308 if it is not present. */
5310 static inline unsigned
5311 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5313 dw_attr_ref a = get_AT (die, attr_kind);
5315 return a ? AT_unsigned (a) : 0;
5318 static inline dw_die_ref
5319 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5321 dw_attr_ref a = get_AT (die, attr_kind);
5323 return a ? AT_ref (a) : NULL;
5326 /* Return TRUE if the language is C or C++. */
5328 static inline bool
5329 is_c_family (void)
5331 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5333 return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
5334 || lang == DW_LANG_C99
5335 || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
5338 /* Return TRUE if the language is C++. */
5340 static inline bool
5341 is_cxx (void)
5343 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5345 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
5348 /* Return TRUE if the language is Fortran. */
5350 static inline bool
5351 is_fortran (void)
5353 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5355 return (lang == DW_LANG_Fortran77
5356 || lang == DW_LANG_Fortran90
5357 || lang == DW_LANG_Fortran95);
5360 /* Return TRUE if the language is Java. */
5362 static inline bool
5363 is_java (void)
5365 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5367 return lang == DW_LANG_Java;
5370 /* Return TRUE if the language is Ada. */
5372 static inline bool
5373 is_ada (void)
5375 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5377 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5380 /* Free up the memory used by A. */
5382 static inline void free_AT (dw_attr_ref);
5383 static inline void
5384 free_AT (dw_attr_ref a)
5386 if (AT_class (a) == dw_val_class_str)
5387 if (a->dw_attr_val.v.val_str->refcount)
5388 a->dw_attr_val.v.val_str->refcount--;
5391 /* Remove the specified attribute if present. */
5393 static void
5394 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5396 dw_attr_ref a;
5397 unsigned ix;
5399 if (! die)
5400 return;
5402 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5403 if (a->dw_attr == attr_kind)
5405 free_AT (a);
5406 /* VEC_ordered_remove should help reduce the number of abbrevs
5407 that are needed. */
5408 VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
5409 return;
5413 /* Remove child die whose die_tag is specified tag. */
5415 static void
5416 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5418 dw_die_ref current, prev, next;
5419 current = die->die_child;
5420 prev = NULL;
5421 while (current != NULL)
5423 if (current->die_tag == tag)
5425 next = current->die_sib;
5426 if (prev == NULL)
5427 die->die_child = next;
5428 else
5429 prev->die_sib = next;
5430 free_die (current);
5431 current = next;
5433 else
5435 prev = current;
5436 current = current->die_sib;
5441 /* Free up the memory used by DIE, by removing its children and
5442 anything associated with its attributes. DIEs are garbage
5443 collected, so there is no actual freeing to do; the only real work is
5444 to decrease string reference counts. */
5446 static void
5447 free_die (dw_die_ref die)
5449 dw_die_ref child_die = die->die_child;
5451 die->die_child = NULL;
5453 while (child_die != NULL)
5455 dw_die_ref tmp_die = child_die;
5456 dw_attr_ref a;
5457 unsigned ix;
5459 child_die = child_die->die_sib;
5461 for (ix = 0; VEC_iterate (dw_attr_node, tmp_die->die_attr, ix, a); ix++)
5462 free_AT (a);
5464 free_die (tmp_die);
5468 /* Add a child DIE below its parent. We build the lists up in reverse
5469 addition order, and correct that in reverse_all_dies. */
5471 static inline void
5472 add_child_die (dw_die_ref die, dw_die_ref child_die)
5474 if (die != NULL && child_die != NULL)
5476 gcc_assert (die != child_die);
5478 child_die->die_parent = die;
5479 child_die->die_sib = die->die_child;
5480 die->die_child = child_die;
5484 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5485 is the specification, to the front of PARENT's list of children. */
5487 static void
5488 splice_child_die (dw_die_ref parent, dw_die_ref child)
5490 dw_die_ref *p;
5492 /* We want the declaration DIE from inside the class, not the
5493 specification DIE at toplevel. */
5494 if (child->die_parent != parent)
5496 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5498 if (tmp)
5499 child = tmp;
5502 gcc_assert (child->die_parent == parent
5503 || (child->die_parent
5504 == get_AT_ref (parent, DW_AT_specification)));
5506 for (p = &(child->die_parent->die_child); *p; p = &((*p)->die_sib))
5507 if (*p == child)
5509 *p = child->die_sib;
5510 break;
5513 child->die_parent = parent;
5514 child->die_sib = parent->die_child;
5515 parent->die_child = child;
5518 /* Return a pointer to a newly created DIE node. */
5520 static inline dw_die_ref
5521 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5523 dw_die_ref die = ggc_alloc_cleared (sizeof (die_node));
5525 die->die_tag = tag_value;
5527 if (parent_die != NULL)
5528 add_child_die (parent_die, die);
5529 else
5531 limbo_die_node *limbo_node;
5533 limbo_node = ggc_alloc_cleared (sizeof (limbo_die_node));
5534 limbo_node->die = die;
5535 limbo_node->created_for = t;
5536 limbo_node->next = limbo_die_list;
5537 limbo_die_list = limbo_node;
5540 return die;
5543 /* Return the DIE associated with the given type specifier. */
5545 static inline dw_die_ref
5546 lookup_type_die (tree type)
5548 return TYPE_SYMTAB_DIE (type);
5551 /* Equate a DIE to a given type specifier. */
5553 static inline void
5554 equate_type_number_to_die (tree type, dw_die_ref type_die)
5556 TYPE_SYMTAB_DIE (type) = type_die;
5559 /* Returns a hash value for X (which really is a die_struct). */
5561 static hashval_t
5562 decl_die_table_hash (const void *x)
5564 return (hashval_t) ((const dw_die_ref) x)->decl_id;
5567 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5569 static int
5570 decl_die_table_eq (const void *x, const void *y)
5572 return (((const dw_die_ref) x)->decl_id == DECL_UID ((const tree) y));
5575 /* Return the DIE associated with a given declaration. */
5577 static inline dw_die_ref
5578 lookup_decl_die (tree decl)
5580 return htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
5583 /* Returns a hash value for X (which really is a var_loc_list). */
5585 static hashval_t
5586 decl_loc_table_hash (const void *x)
5588 return (hashval_t) ((const var_loc_list *) x)->decl_id;
5591 /* Return nonzero if decl_id of var_loc_list X is the same as
5592 UID of decl *Y. */
5594 static int
5595 decl_loc_table_eq (const void *x, const void *y)
5597 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const tree) y));
5600 /* Return the var_loc list associated with a given declaration. */
5602 static inline var_loc_list *
5603 lookup_decl_loc (tree decl)
5605 return htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5608 /* Equate a DIE to a particular declaration. */
5610 static void
5611 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5613 unsigned int decl_id = DECL_UID (decl);
5614 void **slot;
5616 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5617 *slot = decl_die;
5618 decl_die->decl_id = decl_id;
5621 /* Add a variable location node to the linked list for DECL. */
5623 static void
5624 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
5626 unsigned int decl_id = DECL_UID (decl);
5627 var_loc_list *temp;
5628 void **slot;
5630 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5631 if (*slot == NULL)
5633 temp = ggc_alloc_cleared (sizeof (var_loc_list));
5634 temp->decl_id = decl_id;
5635 *slot = temp;
5637 else
5638 temp = *slot;
5640 if (temp->last)
5642 /* If the current location is the same as the end of the list,
5643 we have nothing to do. */
5644 if (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
5645 NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
5647 /* Add LOC to the end of list and update LAST. */
5648 temp->last->next = loc;
5649 temp->last = loc;
5652 /* Do not add empty location to the beginning of the list. */
5653 else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
5655 temp->first = loc;
5656 temp->last = loc;
5660 /* Keep track of the number of spaces used to indent the
5661 output of the debugging routines that print the structure of
5662 the DIE internal representation. */
5663 static int print_indent;
5665 /* Indent the line the number of spaces given by print_indent. */
5667 static inline void
5668 print_spaces (FILE *outfile)
5670 fprintf (outfile, "%*s", print_indent, "");
5673 /* Print the information associated with a given DIE, and its children.
5674 This routine is a debugging aid only. */
5676 static void
5677 print_die (dw_die_ref die, FILE *outfile)
5679 dw_attr_ref a;
5680 dw_die_ref c;
5681 unsigned ix;
5683 print_spaces (outfile);
5684 fprintf (outfile, "DIE %4lu: %s\n",
5685 die->die_offset, dwarf_tag_name (die->die_tag));
5686 print_spaces (outfile);
5687 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5688 fprintf (outfile, " offset: %lu\n", die->die_offset);
5690 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5692 print_spaces (outfile);
5693 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5695 switch (AT_class (a))
5697 case dw_val_class_addr:
5698 fprintf (outfile, "address");
5699 break;
5700 case dw_val_class_offset:
5701 fprintf (outfile, "offset");
5702 break;
5703 case dw_val_class_loc:
5704 fprintf (outfile, "location descriptor");
5705 break;
5706 case dw_val_class_loc_list:
5707 fprintf (outfile, "location list -> label:%s",
5708 AT_loc_list (a)->ll_symbol);
5709 break;
5710 case dw_val_class_range_list:
5711 fprintf (outfile, "range list");
5712 break;
5713 case dw_val_class_const:
5714 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5715 break;
5716 case dw_val_class_unsigned_const:
5717 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5718 break;
5719 case dw_val_class_long_long:
5720 fprintf (outfile, "constant (%lu,%lu)",
5721 a->dw_attr_val.v.val_long_long.hi,
5722 a->dw_attr_val.v.val_long_long.low);
5723 break;
5724 case dw_val_class_vec:
5725 fprintf (outfile, "floating-point or vector constant");
5726 break;
5727 case dw_val_class_flag:
5728 fprintf (outfile, "%u", AT_flag (a));
5729 break;
5730 case dw_val_class_die_ref:
5731 if (AT_ref (a) != NULL)
5733 if (AT_ref (a)->die_symbol)
5734 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
5735 else
5736 fprintf (outfile, "die -> %lu", AT_ref (a)->die_offset);
5738 else
5739 fprintf (outfile, "die -> <null>");
5740 break;
5741 case dw_val_class_lbl_id:
5742 case dw_val_class_lineptr:
5743 case dw_val_class_macptr:
5744 fprintf (outfile, "label: %s", AT_lbl (a));
5745 break;
5746 case dw_val_class_str:
5747 if (AT_string (a) != NULL)
5748 fprintf (outfile, "\"%s\"", AT_string (a));
5749 else
5750 fprintf (outfile, "<null>");
5751 break;
5752 default:
5753 break;
5756 fprintf (outfile, "\n");
5759 if (die->die_child != NULL)
5761 print_indent += 4;
5762 for (c = die->die_child; c != NULL; c = c->die_sib)
5763 print_die (c, outfile);
5765 print_indent -= 4;
5767 if (print_indent == 0)
5768 fprintf (outfile, "\n");
5771 /* Print the contents of the source code line number correspondence table.
5772 This routine is a debugging aid only. */
5774 static void
5775 print_dwarf_line_table (FILE *outfile)
5777 unsigned i;
5778 dw_line_info_ref line_info;
5780 fprintf (outfile, "\n\nDWARF source line information\n");
5781 for (i = 1; i < line_info_table_in_use; i++)
5783 line_info = &line_info_table[i];
5784 fprintf (outfile, "%5d: ", i);
5785 fprintf (outfile, "%-20s",
5786 VARRAY_CHAR_PTR (file_table, line_info->dw_file_num));
5787 fprintf (outfile, "%6ld", line_info->dw_line_num);
5788 fprintf (outfile, "\n");
5791 fprintf (outfile, "\n\n");
5794 /* Print the information collected for a given DIE. */
5796 void
5797 debug_dwarf_die (dw_die_ref die)
5799 print_die (die, stderr);
5802 /* Print all DWARF information collected for the compilation unit.
5803 This routine is a debugging aid only. */
5805 void
5806 debug_dwarf (void)
5808 print_indent = 0;
5809 print_die (comp_unit_die, stderr);
5810 if (! DWARF2_ASM_LINE_DEBUG_INFO)
5811 print_dwarf_line_table (stderr);
5814 /* We build up the lists of children and attributes by pushing new ones
5815 onto the beginning of the list. Reverse the lists for DIE so that
5816 they are in order of addition. */
5818 static void
5819 reverse_die_lists (dw_die_ref die)
5821 dw_die_ref c, cp, cn;
5823 for (c = die->die_child, cp = 0; c; c = cn)
5825 cn = c->die_sib;
5826 c->die_sib = cp;
5827 cp = c;
5830 die->die_child = cp;
5833 /* reverse_die_lists only reverses the single die you pass it. Since we used to
5834 reverse all dies in add_sibling_attributes, which runs through all the dies,
5835 it would reverse all the dies. Now, however, since we don't call
5836 reverse_die_lists in add_sibling_attributes, we need a routine to
5837 recursively reverse all the dies. This is that routine. */
5839 static void
5840 reverse_all_dies (dw_die_ref die)
5842 dw_die_ref c;
5844 reverse_die_lists (die);
5846 for (c = die->die_child; c; c = c->die_sib)
5847 reverse_all_dies (c);
5850 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5851 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5852 DIE that marks the start of the DIEs for this include file. */
5854 static dw_die_ref
5855 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5857 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5858 dw_die_ref new_unit = gen_compile_unit_die (filename);
5860 new_unit->die_sib = old_unit;
5861 return new_unit;
5864 /* Close an include-file CU and reopen the enclosing one. */
5866 static dw_die_ref
5867 pop_compile_unit (dw_die_ref old_unit)
5869 dw_die_ref new_unit = old_unit->die_sib;
5871 old_unit->die_sib = NULL;
5872 return new_unit;
5875 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5876 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5878 /* Calculate the checksum of a location expression. */
5880 static inline void
5881 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5883 CHECKSUM (loc->dw_loc_opc);
5884 CHECKSUM (loc->dw_loc_oprnd1);
5885 CHECKSUM (loc->dw_loc_oprnd2);
5888 /* Calculate the checksum of an attribute. */
5890 static void
5891 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5893 dw_loc_descr_ref loc;
5894 rtx r;
5896 CHECKSUM (at->dw_attr);
5898 /* We don't care about differences in file numbering. */
5899 if (at->dw_attr == DW_AT_decl_file
5900 /* Or that this was compiled with a different compiler snapshot; if
5901 the output is the same, that's what matters. */
5902 || at->dw_attr == DW_AT_producer)
5903 return;
5905 switch (AT_class (at))
5907 case dw_val_class_const:
5908 CHECKSUM (at->dw_attr_val.v.val_int);
5909 break;
5910 case dw_val_class_unsigned_const:
5911 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5912 break;
5913 case dw_val_class_long_long:
5914 CHECKSUM (at->dw_attr_val.v.val_long_long);
5915 break;
5916 case dw_val_class_vec:
5917 CHECKSUM (at->dw_attr_val.v.val_vec);
5918 break;
5919 case dw_val_class_flag:
5920 CHECKSUM (at->dw_attr_val.v.val_flag);
5921 break;
5922 case dw_val_class_str:
5923 CHECKSUM_STRING (AT_string (at));
5924 break;
5926 case dw_val_class_addr:
5927 r = AT_addr (at);
5928 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5929 CHECKSUM_STRING (XSTR (r, 0));
5930 break;
5932 case dw_val_class_offset:
5933 CHECKSUM (at->dw_attr_val.v.val_offset);
5934 break;
5936 case dw_val_class_loc:
5937 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5938 loc_checksum (loc, ctx);
5939 break;
5941 case dw_val_class_die_ref:
5942 die_checksum (AT_ref (at), ctx, mark);
5943 break;
5945 case dw_val_class_fde_ref:
5946 case dw_val_class_lbl_id:
5947 case dw_val_class_lineptr:
5948 case dw_val_class_macptr:
5949 break;
5951 default:
5952 break;
5956 /* Calculate the checksum of a DIE. */
5958 static void
5959 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5961 dw_die_ref c;
5962 dw_attr_ref a;
5963 unsigned ix;
5965 /* To avoid infinite recursion. */
5966 if (die->die_mark)
5968 CHECKSUM (die->die_mark);
5969 return;
5971 die->die_mark = ++(*mark);
5973 CHECKSUM (die->die_tag);
5975 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5976 attr_checksum (a, ctx, mark);
5978 for (c = die->die_child; c; c = c->die_sib)
5979 die_checksum (c, ctx, mark);
5982 #undef CHECKSUM
5983 #undef CHECKSUM_STRING
5985 /* Do the location expressions look same? */
5986 static inline int
5987 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
5989 return loc1->dw_loc_opc == loc2->dw_loc_opc
5990 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
5991 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
5994 /* Do the values look the same? */
5995 static int
5996 same_dw_val_p (dw_val_node *v1, dw_val_node *v2, int *mark)
5998 dw_loc_descr_ref loc1, loc2;
5999 rtx r1, r2;
6001 if (v1->val_class != v2->val_class)
6002 return 0;
6004 switch (v1->val_class)
6006 case dw_val_class_const:
6007 return v1->v.val_int == v2->v.val_int;
6008 case dw_val_class_unsigned_const:
6009 return v1->v.val_unsigned == v2->v.val_unsigned;
6010 case dw_val_class_long_long:
6011 return v1->v.val_long_long.hi == v2->v.val_long_long.hi
6012 && v1->v.val_long_long.low == v2->v.val_long_long.low;
6013 case dw_val_class_vec:
6014 if (v1->v.val_vec.length != v2->v.val_vec.length
6015 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6016 return 0;
6017 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6018 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6019 return 0;
6020 return 1;
6021 case dw_val_class_flag:
6022 return v1->v.val_flag == v2->v.val_flag;
6023 case dw_val_class_str:
6024 return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6026 case dw_val_class_addr:
6027 r1 = v1->v.val_addr;
6028 r2 = v2->v.val_addr;
6029 if (GET_CODE (r1) != GET_CODE (r2))
6030 return 0;
6031 gcc_assert (GET_CODE (r1) == SYMBOL_REF);
6032 return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
6034 case dw_val_class_offset:
6035 return v1->v.val_offset == v2->v.val_offset;
6037 case dw_val_class_loc:
6038 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6039 loc1 && loc2;
6040 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6041 if (!same_loc_p (loc1, loc2, mark))
6042 return 0;
6043 return !loc1 && !loc2;
6045 case dw_val_class_die_ref:
6046 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6048 case dw_val_class_fde_ref:
6049 case dw_val_class_lbl_id:
6050 case dw_val_class_lineptr:
6051 case dw_val_class_macptr:
6052 return 1;
6054 default:
6055 return 1;
6059 /* Do the attributes look the same? */
6061 static int
6062 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6064 if (at1->dw_attr != at2->dw_attr)
6065 return 0;
6067 /* We don't care about differences in file numbering. */
6068 if (at1->dw_attr == DW_AT_decl_file
6069 /* Or that this was compiled with a different compiler snapshot; if
6070 the output is the same, that's what matters. */
6071 || at1->dw_attr == DW_AT_producer)
6072 return 1;
6074 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6077 /* Do the dies look the same? */
6079 static int
6080 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6082 dw_die_ref c1, c2;
6083 dw_attr_ref a1;
6084 unsigned ix;
6086 /* To avoid infinite recursion. */
6087 if (die1->die_mark)
6088 return die1->die_mark == die2->die_mark;
6089 die1->die_mark = die2->die_mark = ++(*mark);
6091 if (die1->die_tag != die2->die_tag)
6092 return 0;
6094 if (VEC_length (dw_attr_node, die1->die_attr)
6095 != VEC_length (dw_attr_node, die2->die_attr))
6096 return 0;
6098 for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
6099 if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
6100 return 0;
6102 for (c1 = die1->die_child, c2 = die2->die_child;
6103 c1 && c2;
6104 c1 = c1->die_sib, c2 = c2->die_sib)
6105 if (!same_die_p (c1, c2, mark))
6106 return 0;
6107 if (c1 || c2)
6108 return 0;
6110 return 1;
6113 /* Do the dies look the same? Wrapper around same_die_p. */
6115 static int
6116 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6118 int mark = 0;
6119 int ret = same_die_p (die1, die2, &mark);
6121 unmark_all_dies (die1);
6122 unmark_all_dies (die2);
6124 return ret;
6127 /* The prefix to attach to symbols on DIEs in the current comdat debug
6128 info section. */
6129 static char *comdat_symbol_id;
6131 /* The index of the current symbol within the current comdat CU. */
6132 static unsigned int comdat_symbol_number;
6134 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6135 children, and set comdat_symbol_id accordingly. */
6137 static void
6138 compute_section_prefix (dw_die_ref unit_die)
6140 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6141 const char *base = die_name ? lbasename (die_name) : "anonymous";
6142 char *name = alloca (strlen (base) + 64);
6143 char *p;
6144 int i, mark;
6145 unsigned char checksum[16];
6146 struct md5_ctx ctx;
6148 /* Compute the checksum of the DIE, then append part of it as hex digits to
6149 the name filename of the unit. */
6151 md5_init_ctx (&ctx);
6152 mark = 0;
6153 die_checksum (unit_die, &ctx, &mark);
6154 unmark_all_dies (unit_die);
6155 md5_finish_ctx (&ctx, checksum);
6157 sprintf (name, "%s.", base);
6158 clean_symbol_name (name);
6160 p = name + strlen (name);
6161 for (i = 0; i < 4; i++)
6163 sprintf (p, "%.2x", checksum[i]);
6164 p += 2;
6167 comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
6168 comdat_symbol_number = 0;
6171 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6173 static int
6174 is_type_die (dw_die_ref die)
6176 switch (die->die_tag)
6178 case DW_TAG_array_type:
6179 case DW_TAG_class_type:
6180 case DW_TAG_enumeration_type:
6181 case DW_TAG_pointer_type:
6182 case DW_TAG_reference_type:
6183 case DW_TAG_string_type:
6184 case DW_TAG_structure_type:
6185 case DW_TAG_subroutine_type:
6186 case DW_TAG_union_type:
6187 case DW_TAG_ptr_to_member_type:
6188 case DW_TAG_set_type:
6189 case DW_TAG_subrange_type:
6190 case DW_TAG_base_type:
6191 case DW_TAG_const_type:
6192 case DW_TAG_file_type:
6193 case DW_TAG_packed_type:
6194 case DW_TAG_volatile_type:
6195 case DW_TAG_typedef:
6196 return 1;
6197 default:
6198 return 0;
6202 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6203 Basically, we want to choose the bits that are likely to be shared between
6204 compilations (types) and leave out the bits that are specific to individual
6205 compilations (functions). */
6207 static int
6208 is_comdat_die (dw_die_ref c)
6210 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6211 we do for stabs. The advantage is a greater likelihood of sharing between
6212 objects that don't include headers in the same order (and therefore would
6213 put the base types in a different comdat). jason 8/28/00 */
6215 if (c->die_tag == DW_TAG_base_type)
6216 return 0;
6218 if (c->die_tag == DW_TAG_pointer_type
6219 || c->die_tag == DW_TAG_reference_type
6220 || c->die_tag == DW_TAG_const_type
6221 || c->die_tag == DW_TAG_volatile_type)
6223 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6225 return t ? is_comdat_die (t) : 0;
6228 return is_type_die (c);
6231 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6232 compilation unit. */
6234 static int
6235 is_symbol_die (dw_die_ref c)
6237 return (is_type_die (c)
6238 || (get_AT (c, DW_AT_declaration)
6239 && !get_AT (c, DW_AT_specification))
6240 || c->die_tag == DW_TAG_namespace);
6243 static char *
6244 gen_internal_sym (const char *prefix)
6246 char buf[256];
6248 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6249 return xstrdup (buf);
6252 /* Assign symbols to all worthy DIEs under DIE. */
6254 static void
6255 assign_symbol_names (dw_die_ref die)
6257 dw_die_ref c;
6259 if (is_symbol_die (die))
6261 if (comdat_symbol_id)
6263 char *p = alloca (strlen (comdat_symbol_id) + 64);
6265 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6266 comdat_symbol_id, comdat_symbol_number++);
6267 die->die_symbol = xstrdup (p);
6269 else
6270 die->die_symbol = gen_internal_sym ("LDIE");
6273 for (c = die->die_child; c != NULL; c = c->die_sib)
6274 assign_symbol_names (c);
6277 struct cu_hash_table_entry
6279 dw_die_ref cu;
6280 unsigned min_comdat_num, max_comdat_num;
6281 struct cu_hash_table_entry *next;
6284 /* Routines to manipulate hash table of CUs. */
6285 static hashval_t
6286 htab_cu_hash (const void *of)
6288 const struct cu_hash_table_entry *entry = of;
6290 return htab_hash_string (entry->cu->die_symbol);
6293 static int
6294 htab_cu_eq (const void *of1, const void *of2)
6296 const struct cu_hash_table_entry *entry1 = of1;
6297 const struct die_struct *entry2 = of2;
6299 return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
6302 static void
6303 htab_cu_del (void *what)
6305 struct cu_hash_table_entry *next, *entry = what;
6307 while (entry)
6309 next = entry->next;
6310 free (entry);
6311 entry = next;
6315 /* Check whether we have already seen this CU and set up SYM_NUM
6316 accordingly. */
6317 static int
6318 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6320 struct cu_hash_table_entry dummy;
6321 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6323 dummy.max_comdat_num = 0;
6325 slot = (struct cu_hash_table_entry **)
6326 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6327 INSERT);
6328 entry = *slot;
6330 for (; entry; last = entry, entry = entry->next)
6332 if (same_die_p_wrap (cu, entry->cu))
6333 break;
6336 if (entry)
6338 *sym_num = entry->min_comdat_num;
6339 return 1;
6342 entry = XCNEW (struct cu_hash_table_entry);
6343 entry->cu = cu;
6344 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6345 entry->next = *slot;
6346 *slot = entry;
6348 return 0;
6351 /* Record SYM_NUM to record of CU in HTABLE. */
6352 static void
6353 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6355 struct cu_hash_table_entry **slot, *entry;
6357 slot = (struct cu_hash_table_entry **)
6358 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6359 NO_INSERT);
6360 entry = *slot;
6362 entry->max_comdat_num = sym_num;
6365 /* Traverse the DIE (which is always comp_unit_die), and set up
6366 additional compilation units for each of the include files we see
6367 bracketed by BINCL/EINCL. */
6369 static void
6370 break_out_includes (dw_die_ref die)
6372 dw_die_ref *ptr;
6373 dw_die_ref unit = NULL;
6374 limbo_die_node *node, **pnode;
6375 htab_t cu_hash_table;
6377 for (ptr = &(die->die_child); *ptr;)
6379 dw_die_ref c = *ptr;
6381 if (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6382 || (unit && is_comdat_die (c)))
6384 /* This DIE is for a secondary CU; remove it from the main one. */
6385 *ptr = c->die_sib;
6387 if (c->die_tag == DW_TAG_GNU_BINCL)
6389 unit = push_new_compile_unit (unit, c);
6390 free_die (c);
6392 else if (c->die_tag == DW_TAG_GNU_EINCL)
6394 unit = pop_compile_unit (unit);
6395 free_die (c);
6397 else
6398 add_child_die (unit, c);
6400 else
6402 /* Leave this DIE in the main CU. */
6403 ptr = &(c->die_sib);
6404 continue;
6408 #if 0
6409 /* We can only use this in debugging, since the frontend doesn't check
6410 to make sure that we leave every include file we enter. */
6411 gcc_assert (!unit);
6412 #endif
6414 assign_symbol_names (die);
6415 cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6416 for (node = limbo_die_list, pnode = &limbo_die_list;
6417 node;
6418 node = node->next)
6420 int is_dupl;
6422 compute_section_prefix (node->die);
6423 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6424 &comdat_symbol_number);
6425 assign_symbol_names (node->die);
6426 if (is_dupl)
6427 *pnode = node->next;
6428 else
6430 pnode = &node->next;
6431 record_comdat_symbol_number (node->die, cu_hash_table,
6432 comdat_symbol_number);
6435 htab_delete (cu_hash_table);
6438 /* Traverse the DIE and add a sibling attribute if it may have the
6439 effect of speeding up access to siblings. To save some space,
6440 avoid generating sibling attributes for DIE's without children. */
6442 static void
6443 add_sibling_attributes (dw_die_ref die)
6445 dw_die_ref c;
6447 if (die->die_tag != DW_TAG_compile_unit
6448 && die->die_sib && die->die_child != NULL)
6449 /* Add the sibling link to the front of the attribute list. */
6450 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
6452 for (c = die->die_child; c != NULL; c = c->die_sib)
6453 add_sibling_attributes (c);
6456 /* Output all location lists for the DIE and its children. */
6458 static void
6459 output_location_lists (dw_die_ref die)
6461 dw_die_ref c;
6462 dw_attr_ref a;
6463 unsigned ix;
6465 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6466 if (AT_class (a) == dw_val_class_loc_list)
6467 output_loc_list (AT_loc_list (a));
6469 for (c = die->die_child; c != NULL; c = c->die_sib)
6470 output_location_lists (c);
6474 /* The format of each DIE (and its attribute value pairs) is encoded in an
6475 abbreviation table. This routine builds the abbreviation table and assigns
6476 a unique abbreviation id for each abbreviation entry. The children of each
6477 die are visited recursively. */
6479 static void
6480 build_abbrev_table (dw_die_ref die)
6482 unsigned long abbrev_id;
6483 unsigned int n_alloc;
6484 dw_die_ref c;
6485 dw_attr_ref a;
6486 unsigned ix;
6488 /* Scan the DIE references, and mark as external any that refer to
6489 DIEs from other CUs (i.e. those which are not marked). */
6490 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6491 if (AT_class (a) == dw_val_class_die_ref
6492 && AT_ref (a)->die_mark == 0)
6494 gcc_assert (AT_ref (a)->die_symbol);
6496 set_AT_ref_external (a, 1);
6499 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6501 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6502 dw_attr_ref die_a, abbrev_a;
6503 unsigned ix;
6504 bool ok = true;
6506 if (abbrev->die_tag != die->die_tag)
6507 continue;
6508 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
6509 continue;
6511 if (VEC_length (dw_attr_node, abbrev->die_attr)
6512 != VEC_length (dw_attr_node, die->die_attr))
6513 continue;
6515 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
6517 abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
6518 if ((abbrev_a->dw_attr != die_a->dw_attr)
6519 || (value_format (abbrev_a) != value_format (die_a)))
6521 ok = false;
6522 break;
6525 if (ok)
6526 break;
6529 if (abbrev_id >= abbrev_die_table_in_use)
6531 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
6533 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
6534 abbrev_die_table = ggc_realloc (abbrev_die_table,
6535 sizeof (dw_die_ref) * n_alloc);
6537 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
6538 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
6539 abbrev_die_table_allocated = n_alloc;
6542 ++abbrev_die_table_in_use;
6543 abbrev_die_table[abbrev_id] = die;
6546 die->die_abbrev = abbrev_id;
6547 for (c = die->die_child; c != NULL; c = c->die_sib)
6548 build_abbrev_table (c);
6551 /* Return the power-of-two number of bytes necessary to represent VALUE. */
6553 static int
6554 constant_size (long unsigned int value)
6556 int log;
6558 if (value == 0)
6559 log = 0;
6560 else
6561 log = floor_log2 (value);
6563 log = log / 8;
6564 log = 1 << (floor_log2 (log) + 1);
6566 return log;
6569 /* Return the size of a DIE as it is represented in the
6570 .debug_info section. */
6572 static unsigned long
6573 size_of_die (dw_die_ref die)
6575 unsigned long size = 0;
6576 dw_attr_ref a;
6577 unsigned ix;
6579 size += size_of_uleb128 (die->die_abbrev);
6580 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6582 switch (AT_class (a))
6584 case dw_val_class_addr:
6585 size += DWARF2_ADDR_SIZE;
6586 break;
6587 case dw_val_class_offset:
6588 size += DWARF_OFFSET_SIZE;
6589 break;
6590 case dw_val_class_loc:
6592 unsigned long lsize = size_of_locs (AT_loc (a));
6594 /* Block length. */
6595 size += constant_size (lsize);
6596 size += lsize;
6598 break;
6599 case dw_val_class_loc_list:
6600 size += DWARF_OFFSET_SIZE;
6601 break;
6602 case dw_val_class_range_list:
6603 size += DWARF_OFFSET_SIZE;
6604 break;
6605 case dw_val_class_const:
6606 size += size_of_sleb128 (AT_int (a));
6607 break;
6608 case dw_val_class_unsigned_const:
6609 size += constant_size (AT_unsigned (a));
6610 break;
6611 case dw_val_class_long_long:
6612 size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
6613 break;
6614 case dw_val_class_vec:
6615 size += 1 + (a->dw_attr_val.v.val_vec.length
6616 * a->dw_attr_val.v.val_vec.elt_size); /* block */
6617 break;
6618 case dw_val_class_flag:
6619 size += 1;
6620 break;
6621 case dw_val_class_die_ref:
6622 if (AT_ref_external (a))
6623 size += DWARF2_ADDR_SIZE;
6624 else
6625 size += DWARF_OFFSET_SIZE;
6626 break;
6627 case dw_val_class_fde_ref:
6628 size += DWARF_OFFSET_SIZE;
6629 break;
6630 case dw_val_class_lbl_id:
6631 size += DWARF2_ADDR_SIZE;
6632 break;
6633 case dw_val_class_lineptr:
6634 case dw_val_class_macptr:
6635 size += DWARF_OFFSET_SIZE;
6636 break;
6637 case dw_val_class_str:
6638 if (AT_string_form (a) == DW_FORM_strp)
6639 size += DWARF_OFFSET_SIZE;
6640 else
6641 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
6642 break;
6643 default:
6644 gcc_unreachable ();
6648 return size;
6651 /* Size the debugging information associated with a given DIE. Visits the
6652 DIE's children recursively. Updates the global variable next_die_offset, on
6653 each time through. Uses the current value of next_die_offset to update the
6654 die_offset field in each DIE. */
6656 static void
6657 calc_die_sizes (dw_die_ref die)
6659 dw_die_ref c;
6661 die->die_offset = next_die_offset;
6662 next_die_offset += size_of_die (die);
6664 for (c = die->die_child; c != NULL; c = c->die_sib)
6665 calc_die_sizes (c);
6667 if (die->die_child != NULL)
6668 /* Count the null byte used to terminate sibling lists. */
6669 next_die_offset += 1;
6672 /* Set the marks for a die and its children. We do this so
6673 that we know whether or not a reference needs to use FORM_ref_addr; only
6674 DIEs in the same CU will be marked. We used to clear out the offset
6675 and use that as the flag, but ran into ordering problems. */
6677 static void
6678 mark_dies (dw_die_ref die)
6680 dw_die_ref c;
6682 gcc_assert (!die->die_mark);
6684 die->die_mark = 1;
6685 for (c = die->die_child; c; c = c->die_sib)
6686 mark_dies (c);
6689 /* Clear the marks for a die and its children. */
6691 static void
6692 unmark_dies (dw_die_ref die)
6694 dw_die_ref c;
6696 gcc_assert (die->die_mark);
6698 die->die_mark = 0;
6699 for (c = die->die_child; c; c = c->die_sib)
6700 unmark_dies (c);
6703 /* Clear the marks for a die, its children and referred dies. */
6705 static void
6706 unmark_all_dies (dw_die_ref die)
6708 dw_die_ref c;
6709 dw_attr_ref a;
6710 unsigned ix;
6712 if (!die->die_mark)
6713 return;
6714 die->die_mark = 0;
6716 for (c = die->die_child; c; c = c->die_sib)
6717 unmark_all_dies (c);
6719 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6720 if (AT_class (a) == dw_val_class_die_ref)
6721 unmark_all_dies (AT_ref (a));
6724 /* Return the size of the .debug_pubnames table generated for the
6725 compilation unit. */
6727 static unsigned long
6728 size_of_pubnames (void)
6730 unsigned long size;
6731 unsigned i;
6733 size = DWARF_PUBNAMES_HEADER_SIZE;
6734 for (i = 0; i < pubname_table_in_use; i++)
6736 pubname_ref p = &pubname_table[i];
6737 size += DWARF_OFFSET_SIZE + strlen (p->name) + 1;
6740 size += DWARF_OFFSET_SIZE;
6741 return size;
6744 /* Return the size of the information in the .debug_aranges section. */
6746 static unsigned long
6747 size_of_aranges (void)
6749 unsigned long size;
6751 size = DWARF_ARANGES_HEADER_SIZE;
6753 /* Count the address/length pair for this compilation unit. */
6754 size += 2 * DWARF2_ADDR_SIZE;
6755 size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
6757 /* Count the two zero words used to terminated the address range table. */
6758 size += 2 * DWARF2_ADDR_SIZE;
6759 return size;
6762 /* Select the encoding of an attribute value. */
6764 static enum dwarf_form
6765 value_format (dw_attr_ref a)
6767 switch (a->dw_attr_val.val_class)
6769 case dw_val_class_addr:
6770 return DW_FORM_addr;
6771 case dw_val_class_range_list:
6772 case dw_val_class_offset:
6773 case dw_val_class_loc_list:
6774 switch (DWARF_OFFSET_SIZE)
6776 case 4:
6777 return DW_FORM_data4;
6778 case 8:
6779 return DW_FORM_data8;
6780 default:
6781 gcc_unreachable ();
6783 case dw_val_class_loc:
6784 switch (constant_size (size_of_locs (AT_loc (a))))
6786 case 1:
6787 return DW_FORM_block1;
6788 case 2:
6789 return DW_FORM_block2;
6790 default:
6791 gcc_unreachable ();
6793 case dw_val_class_const:
6794 return DW_FORM_sdata;
6795 case dw_val_class_unsigned_const:
6796 switch (constant_size (AT_unsigned (a)))
6798 case 1:
6799 return DW_FORM_data1;
6800 case 2:
6801 return DW_FORM_data2;
6802 case 4:
6803 return DW_FORM_data4;
6804 case 8:
6805 return DW_FORM_data8;
6806 default:
6807 gcc_unreachable ();
6809 case dw_val_class_long_long:
6810 return DW_FORM_block1;
6811 case dw_val_class_vec:
6812 return DW_FORM_block1;
6813 case dw_val_class_flag:
6814 return DW_FORM_flag;
6815 case dw_val_class_die_ref:
6816 if (AT_ref_external (a))
6817 return DW_FORM_ref_addr;
6818 else
6819 return DW_FORM_ref;
6820 case dw_val_class_fde_ref:
6821 return DW_FORM_data;
6822 case dw_val_class_lbl_id:
6823 return DW_FORM_addr;
6824 case dw_val_class_lineptr:
6825 case dw_val_class_macptr:
6826 return DW_FORM_data;
6827 case dw_val_class_str:
6828 return AT_string_form (a);
6830 default:
6831 gcc_unreachable ();
6835 /* Output the encoding of an attribute value. */
6837 static void
6838 output_value_format (dw_attr_ref a)
6840 enum dwarf_form form = value_format (a);
6842 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
6845 /* Output the .debug_abbrev section which defines the DIE abbreviation
6846 table. */
6848 static void
6849 output_abbrev_section (void)
6851 unsigned long abbrev_id;
6853 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6855 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6856 unsigned ix;
6857 dw_attr_ref a_attr;
6859 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
6860 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
6861 dwarf_tag_name (abbrev->die_tag));
6863 if (abbrev->die_child != NULL)
6864 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
6865 else
6866 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
6868 for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
6869 ix++)
6871 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
6872 dwarf_attr_name (a_attr->dw_attr));
6873 output_value_format (a_attr);
6876 dw2_asm_output_data (1, 0, NULL);
6877 dw2_asm_output_data (1, 0, NULL);
6880 /* Terminate the table. */
6881 dw2_asm_output_data (1, 0, NULL);
6884 /* Output a symbol we can use to refer to this DIE from another CU. */
6886 static inline void
6887 output_die_symbol (dw_die_ref die)
6889 char *sym = die->die_symbol;
6891 if (sym == 0)
6892 return;
6894 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
6895 /* We make these global, not weak; if the target doesn't support
6896 .linkonce, it doesn't support combining the sections, so debugging
6897 will break. */
6898 targetm.asm_out.globalize_label (asm_out_file, sym);
6900 ASM_OUTPUT_LABEL (asm_out_file, sym);
6903 /* Return a new location list, given the begin and end range, and the
6904 expression. gensym tells us whether to generate a new internal symbol for
6905 this location list node, which is done for the head of the list only. */
6907 static inline dw_loc_list_ref
6908 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
6909 const char *section, unsigned int gensym)
6911 dw_loc_list_ref retlist = ggc_alloc_cleared (sizeof (dw_loc_list_node));
6913 retlist->begin = begin;
6914 retlist->end = end;
6915 retlist->expr = expr;
6916 retlist->section = section;
6917 if (gensym)
6918 retlist->ll_symbol = gen_internal_sym ("LLST");
6920 return retlist;
6923 /* Add a location description expression to a location list. */
6925 static inline void
6926 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
6927 const char *begin, const char *end,
6928 const char *section)
6930 dw_loc_list_ref *d;
6932 /* Find the end of the chain. */
6933 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
6936 /* Add a new location list node to the list. */
6937 *d = new_loc_list (descr, begin, end, section, 0);
6940 static void
6941 dwarf2out_switch_text_section (void)
6943 dw_fde_ref fde;
6945 gcc_assert (cfun);
6947 fde = &fde_table[fde_table_in_use - 1];
6948 fde->dw_fde_switched_sections = true;
6949 fde->dw_fde_hot_section_label = cfun->hot_section_label;
6950 fde->dw_fde_hot_section_end_label = cfun->hot_section_end_label;
6951 fde->dw_fde_unlikely_section_label = cfun->cold_section_label;
6952 fde->dw_fde_unlikely_section_end_label = cfun->cold_section_end_label;
6953 have_multiple_function_sections = true;
6956 /* Output the location list given to us. */
6958 static void
6959 output_loc_list (dw_loc_list_ref list_head)
6961 dw_loc_list_ref curr = list_head;
6963 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
6965 /* Walk the location list, and output each range + expression. */
6966 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
6968 unsigned long size;
6969 if (!have_multiple_function_sections)
6971 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
6972 "Location list begin address (%s)",
6973 list_head->ll_symbol);
6974 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
6975 "Location list end address (%s)",
6976 list_head->ll_symbol);
6978 else
6980 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
6981 "Location list begin address (%s)",
6982 list_head->ll_symbol);
6983 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
6984 "Location list end address (%s)",
6985 list_head->ll_symbol);
6987 size = size_of_locs (curr->expr);
6989 /* Output the block length for this list of location operations. */
6990 gcc_assert (size <= 0xffff);
6991 dw2_asm_output_data (2, size, "%s", "Location expression size");
6993 output_loc_sequence (curr->expr);
6996 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
6997 "Location list terminator begin (%s)",
6998 list_head->ll_symbol);
6999 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7000 "Location list terminator end (%s)",
7001 list_head->ll_symbol);
7004 /* Output the DIE and its attributes. Called recursively to generate
7005 the definitions of each child DIE. */
7007 static void
7008 output_die (dw_die_ref die)
7010 dw_attr_ref a;
7011 dw_die_ref c;
7012 unsigned long size;
7013 unsigned ix;
7015 /* If someone in another CU might refer to us, set up a symbol for
7016 them to point to. */
7017 if (die->die_symbol)
7018 output_die_symbol (die);
7020 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
7021 die->die_offset, dwarf_tag_name (die->die_tag));
7023 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7025 const char *name = dwarf_attr_name (a->dw_attr);
7027 switch (AT_class (a))
7029 case dw_val_class_addr:
7030 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
7031 break;
7033 case dw_val_class_offset:
7034 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
7035 "%s", name);
7036 break;
7038 case dw_val_class_range_list:
7040 char *p = strchr (ranges_section_label, '\0');
7042 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
7043 a->dw_attr_val.v.val_offset);
7044 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
7045 debug_ranges_section, "%s", name);
7046 *p = '\0';
7048 break;
7050 case dw_val_class_loc:
7051 size = size_of_locs (AT_loc (a));
7053 /* Output the block length for this list of location operations. */
7054 dw2_asm_output_data (constant_size (size), size, "%s", name);
7056 output_loc_sequence (AT_loc (a));
7057 break;
7059 case dw_val_class_const:
7060 /* ??? It would be slightly more efficient to use a scheme like is
7061 used for unsigned constants below, but gdb 4.x does not sign
7062 extend. Gdb 5.x does sign extend. */
7063 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
7064 break;
7066 case dw_val_class_unsigned_const:
7067 dw2_asm_output_data (constant_size (AT_unsigned (a)),
7068 AT_unsigned (a), "%s", name);
7069 break;
7071 case dw_val_class_long_long:
7073 unsigned HOST_WIDE_INT first, second;
7075 dw2_asm_output_data (1,
7076 2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7077 "%s", name);
7079 if (WORDS_BIG_ENDIAN)
7081 first = a->dw_attr_val.v.val_long_long.hi;
7082 second = a->dw_attr_val.v.val_long_long.low;
7084 else
7086 first = a->dw_attr_val.v.val_long_long.low;
7087 second = a->dw_attr_val.v.val_long_long.hi;
7090 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7091 first, "long long constant");
7092 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7093 second, NULL);
7095 break;
7097 case dw_val_class_vec:
7099 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
7100 unsigned int len = a->dw_attr_val.v.val_vec.length;
7101 unsigned int i;
7102 unsigned char *p;
7104 dw2_asm_output_data (1, len * elt_size, "%s", name);
7105 if (elt_size > sizeof (HOST_WIDE_INT))
7107 elt_size /= 2;
7108 len *= 2;
7110 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
7111 i < len;
7112 i++, p += elt_size)
7113 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
7114 "fp or vector constant word %u", i);
7115 break;
7118 case dw_val_class_flag:
7119 dw2_asm_output_data (1, AT_flag (a), "%s", name);
7120 break;
7122 case dw_val_class_loc_list:
7124 char *sym = AT_loc_list (a)->ll_symbol;
7126 gcc_assert (sym);
7127 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
7128 "%s", name);
7130 break;
7132 case dw_val_class_die_ref:
7133 if (AT_ref_external (a))
7135 char *sym = AT_ref (a)->die_symbol;
7137 gcc_assert (sym);
7138 dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, debug_info_section,
7139 "%s", name);
7141 else
7143 gcc_assert (AT_ref (a)->die_offset);
7144 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
7145 "%s", name);
7147 break;
7149 case dw_val_class_fde_ref:
7151 char l1[20];
7153 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
7154 a->dw_attr_val.v.val_fde_index * 2);
7155 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
7156 "%s", name);
7158 break;
7160 case dw_val_class_lbl_id:
7161 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
7162 break;
7164 case dw_val_class_lineptr:
7165 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7166 debug_line_section, "%s", name);
7167 break;
7169 case dw_val_class_macptr:
7170 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7171 debug_macinfo_section, "%s", name);
7172 break;
7174 case dw_val_class_str:
7175 if (AT_string_form (a) == DW_FORM_strp)
7176 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
7177 a->dw_attr_val.v.val_str->label,
7178 debug_str_section,
7179 "%s: \"%s\"", name, AT_string (a));
7180 else
7181 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
7182 break;
7184 default:
7185 gcc_unreachable ();
7189 for (c = die->die_child; c != NULL; c = c->die_sib)
7190 output_die (c);
7192 /* Add null byte to terminate sibling list. */
7193 if (die->die_child != NULL)
7194 dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
7195 die->die_offset);
7198 /* Output the compilation unit that appears at the beginning of the
7199 .debug_info section, and precedes the DIE descriptions. */
7201 static void
7202 output_compilation_unit_header (void)
7204 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7205 dw2_asm_output_data (4, 0xffffffff,
7206 "Initial length escape value indicating 64-bit DWARF extension");
7207 dw2_asm_output_data (DWARF_OFFSET_SIZE,
7208 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
7209 "Length of Compilation Unit Info");
7210 dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
7211 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
7212 debug_abbrev_section,
7213 "Offset Into Abbrev. Section");
7214 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
7217 /* Output the compilation unit DIE and its children. */
7219 static void
7220 output_comp_unit (dw_die_ref die, int output_if_empty)
7222 const char *secname;
7223 char *oldsym, *tmp;
7225 /* Unless we are outputting main CU, we may throw away empty ones. */
7226 if (!output_if_empty && die->die_child == NULL)
7227 return;
7229 /* Even if there are no children of this DIE, we must output the information
7230 about the compilation unit. Otherwise, on an empty translation unit, we
7231 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
7232 will then complain when examining the file. First mark all the DIEs in
7233 this CU so we know which get local refs. */
7234 mark_dies (die);
7236 build_abbrev_table (die);
7238 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
7239 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7240 calc_die_sizes (die);
7242 oldsym = die->die_symbol;
7243 if (oldsym)
7245 tmp = alloca (strlen (oldsym) + 24);
7247 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
7248 secname = tmp;
7249 die->die_symbol = NULL;
7250 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
7252 else
7253 switch_to_section (debug_info_section);
7255 /* Output debugging information. */
7256 output_compilation_unit_header ();
7257 output_die (die);
7259 /* Leave the marks on the main CU, so we can check them in
7260 output_pubnames. */
7261 if (oldsym)
7263 unmark_dies (die);
7264 die->die_symbol = oldsym;
7268 /* The DWARF2 pubname for a nested thingy looks like "A::f". The
7269 output of lang_hooks.decl_printable_name for C++ looks like
7270 "A::f(int)". Let's drop the argument list, and maybe the scope. */
7272 static const char *
7273 dwarf2_name (tree decl, int scope)
7275 return lang_hooks.decl_printable_name (decl, scope ? 1 : 0);
7278 /* Add a new entry to .debug_pubnames if appropriate. */
7280 static void
7281 add_pubname (tree decl, dw_die_ref die)
7283 pubname_ref p;
7285 if (! TREE_PUBLIC (decl))
7286 return;
7288 if (pubname_table_in_use == pubname_table_allocated)
7290 pubname_table_allocated += PUBNAME_TABLE_INCREMENT;
7291 pubname_table
7292 = ggc_realloc (pubname_table,
7293 (pubname_table_allocated * sizeof (pubname_entry)));
7294 memset (pubname_table + pubname_table_in_use, 0,
7295 PUBNAME_TABLE_INCREMENT * sizeof (pubname_entry));
7298 p = &pubname_table[pubname_table_in_use++];
7299 p->die = die;
7300 p->name = xstrdup (dwarf2_name (decl, 1));
7303 /* Output the public names table used to speed up access to externally
7304 visible names. For now, only generate entries for externally
7305 visible procedures. */
7307 static void
7308 output_pubnames (void)
7310 unsigned i;
7311 unsigned long pubnames_length = size_of_pubnames ();
7313 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7314 dw2_asm_output_data (4, 0xffffffff,
7315 "Initial length escape value indicating 64-bit DWARF extension");
7316 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7317 "Length of Public Names Info");
7318 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7319 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7320 debug_info_section,
7321 "Offset of Compilation Unit Info");
7322 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
7323 "Compilation Unit Length");
7325 for (i = 0; i < pubname_table_in_use; i++)
7327 pubname_ref pub = &pubname_table[i];
7329 /* We shouldn't see pubnames for DIEs outside of the main CU. */
7330 gcc_assert (pub->die->die_mark);
7332 dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
7333 "DIE offset");
7335 dw2_asm_output_nstring (pub->name, -1, "external name");
7338 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
7341 /* Add a new entry to .debug_aranges if appropriate. */
7343 static void
7344 add_arange (tree decl, dw_die_ref die)
7346 if (! DECL_SECTION_NAME (decl))
7347 return;
7349 if (arange_table_in_use == arange_table_allocated)
7351 arange_table_allocated += ARANGE_TABLE_INCREMENT;
7352 arange_table = ggc_realloc (arange_table,
7353 (arange_table_allocated
7354 * sizeof (dw_die_ref)));
7355 memset (arange_table + arange_table_in_use, 0,
7356 ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
7359 arange_table[arange_table_in_use++] = die;
7362 /* Output the information that goes into the .debug_aranges table.
7363 Namely, define the beginning and ending address range of the
7364 text section generated for this compilation unit. */
7366 static void
7367 output_aranges (void)
7369 unsigned i;
7370 unsigned long aranges_length = size_of_aranges ();
7372 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7373 dw2_asm_output_data (4, 0xffffffff,
7374 "Initial length escape value indicating 64-bit DWARF extension");
7375 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
7376 "Length of Address Ranges Info");
7377 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7378 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7379 debug_info_section,
7380 "Offset of Compilation Unit Info");
7381 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
7382 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
7384 /* We need to align to twice the pointer size here. */
7385 if (DWARF_ARANGES_PAD_SIZE)
7387 /* Pad using a 2 byte words so that padding is correct for any
7388 pointer size. */
7389 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
7390 2 * DWARF2_ADDR_SIZE);
7391 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
7392 dw2_asm_output_data (2, 0, NULL);
7395 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
7396 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
7397 text_section_label, "Length");
7398 if (flag_reorder_blocks_and_partition)
7400 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
7401 "Address");
7402 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
7403 cold_text_section_label, "Length");
7406 for (i = 0; i < arange_table_in_use; i++)
7408 dw_die_ref die = arange_table[i];
7410 /* We shouldn't see aranges for DIEs outside of the main CU. */
7411 gcc_assert (die->die_mark);
7413 if (die->die_tag == DW_TAG_subprogram)
7415 dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
7416 "Address");
7417 dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
7418 get_AT_low_pc (die), "Length");
7420 else
7422 /* A static variable; extract the symbol from DW_AT_location.
7423 Note that this code isn't currently hit, as we only emit
7424 aranges for functions (jason 9/23/99). */
7425 dw_attr_ref a = get_AT (die, DW_AT_location);
7426 dw_loc_descr_ref loc;
7428 gcc_assert (a && AT_class (a) == dw_val_class_loc);
7430 loc = AT_loc (a);
7431 gcc_assert (loc->dw_loc_opc == DW_OP_addr);
7433 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
7434 loc->dw_loc_oprnd1.v.val_addr, "Address");
7435 dw2_asm_output_data (DWARF2_ADDR_SIZE,
7436 get_AT_unsigned (die, DW_AT_byte_size),
7437 "Length");
7441 /* Output the terminator words. */
7442 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7443 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7446 /* Add a new entry to .debug_ranges. Return the offset at which it
7447 was placed. */
7449 static unsigned int
7450 add_ranges (tree block)
7452 unsigned int in_use = ranges_table_in_use;
7454 if (in_use == ranges_table_allocated)
7456 ranges_table_allocated += RANGES_TABLE_INCREMENT;
7457 ranges_table
7458 = ggc_realloc (ranges_table, (ranges_table_allocated
7459 * sizeof (struct dw_ranges_struct)));
7460 memset (ranges_table + ranges_table_in_use, 0,
7461 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
7464 ranges_table[in_use].block_num = (block ? BLOCK_NUMBER (block) : 0);
7465 ranges_table_in_use = in_use + 1;
7467 return in_use * 2 * DWARF2_ADDR_SIZE;
7470 static void
7471 output_ranges (void)
7473 unsigned i;
7474 static const char *const start_fmt = "Offset 0x%x";
7475 const char *fmt = start_fmt;
7477 for (i = 0; i < ranges_table_in_use; i++)
7479 int block_num = ranges_table[i].block_num;
7481 if (block_num)
7483 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
7484 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
7486 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
7487 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
7489 /* If all code is in the text section, then the compilation
7490 unit base address defaults to DW_AT_low_pc, which is the
7491 base of the text section. */
7492 if (!have_multiple_function_sections)
7494 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
7495 text_section_label,
7496 fmt, i * 2 * DWARF2_ADDR_SIZE);
7497 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
7498 text_section_label, NULL);
7501 /* Otherwise, we add a DW_AT_entry_pc attribute to force the
7502 compilation unit base address to zero, which allows us to
7503 use absolute addresses, and not worry about whether the
7504 target supports cross-section arithmetic. */
7505 else
7507 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
7508 fmt, i * 2 * DWARF2_ADDR_SIZE);
7509 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
7512 fmt = NULL;
7514 else
7516 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7517 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7518 fmt = start_fmt;
7523 /* Data structure containing information about input files. */
7524 struct file_info
7526 char *path; /* Complete file name. */
7527 char *fname; /* File name part. */
7528 int length; /* Length of entire string. */
7529 int file_idx; /* Index in input file table. */
7530 int dir_idx; /* Index in directory table. */
7533 /* Data structure containing information about directories with source
7534 files. */
7535 struct dir_info
7537 char *path; /* Path including directory name. */
7538 int length; /* Path length. */
7539 int prefix; /* Index of directory entry which is a prefix. */
7540 int count; /* Number of files in this directory. */
7541 int dir_idx; /* Index of directory used as base. */
7542 int used; /* Used in the end? */
7545 /* Callback function for file_info comparison. We sort by looking at
7546 the directories in the path. */
7548 static int
7549 file_info_cmp (const void *p1, const void *p2)
7551 const struct file_info *s1 = p1;
7552 const struct file_info *s2 = p2;
7553 unsigned char *cp1;
7554 unsigned char *cp2;
7556 /* Take care of file names without directories. We need to make sure that
7557 we return consistent values to qsort since some will get confused if
7558 we return the same value when identical operands are passed in opposite
7559 orders. So if neither has a directory, return 0 and otherwise return
7560 1 or -1 depending on which one has the directory. */
7561 if ((s1->path == s1->fname || s2->path == s2->fname))
7562 return (s2->path == s2->fname) - (s1->path == s1->fname);
7564 cp1 = (unsigned char *) s1->path;
7565 cp2 = (unsigned char *) s2->path;
7567 while (1)
7569 ++cp1;
7570 ++cp2;
7571 /* Reached the end of the first path? If so, handle like above. */
7572 if ((cp1 == (unsigned char *) s1->fname)
7573 || (cp2 == (unsigned char *) s2->fname))
7574 return ((cp2 == (unsigned char *) s2->fname)
7575 - (cp1 == (unsigned char *) s1->fname));
7577 /* Character of current path component the same? */
7578 else if (*cp1 != *cp2)
7579 return *cp1 - *cp2;
7583 /* Output the directory table and the file name table. We try to minimize
7584 the total amount of memory needed. A heuristic is used to avoid large
7585 slowdowns with many input files. */
7587 static void
7588 output_file_names (void)
7590 struct file_info *files;
7591 struct dir_info *dirs;
7592 int *saved;
7593 int *savehere;
7594 int *backmap;
7595 size_t ndirs;
7596 int idx_offset;
7597 size_t i;
7598 int idx;
7600 /* Handle the case where file_table is empty. */
7601 if (VARRAY_ACTIVE_SIZE (file_table) <= 1)
7603 dw2_asm_output_data (1, 0, "End directory table");
7604 dw2_asm_output_data (1, 0, "End file name table");
7605 return;
7608 /* Allocate the various arrays we need. */
7609 files = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (struct file_info));
7610 dirs = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (struct dir_info));
7612 /* Sort the file names. */
7613 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7615 char *f;
7617 /* Skip all leading "./". */
7618 f = VARRAY_CHAR_PTR (file_table, i);
7619 while (f[0] == '.' && f[1] == '/')
7620 f += 2;
7622 /* Create a new array entry. */
7623 files[i].path = f;
7624 files[i].length = strlen (f);
7625 files[i].file_idx = i;
7627 /* Search for the file name part. */
7628 f = strrchr (f, '/');
7629 files[i].fname = f == NULL ? files[i].path : f + 1;
7632 qsort (files + 1, VARRAY_ACTIVE_SIZE (file_table) - 1,
7633 sizeof (files[0]), file_info_cmp);
7635 /* Find all the different directories used. */
7636 dirs[0].path = files[1].path;
7637 dirs[0].length = files[1].fname - files[1].path;
7638 dirs[0].prefix = -1;
7639 dirs[0].count = 1;
7640 dirs[0].dir_idx = 0;
7641 dirs[0].used = 0;
7642 files[1].dir_idx = 0;
7643 ndirs = 1;
7645 for (i = 2; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7646 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
7647 && memcmp (dirs[ndirs - 1].path, files[i].path,
7648 dirs[ndirs - 1].length) == 0)
7650 /* Same directory as last entry. */
7651 files[i].dir_idx = ndirs - 1;
7652 ++dirs[ndirs - 1].count;
7654 else
7656 size_t j;
7658 /* This is a new directory. */
7659 dirs[ndirs].path = files[i].path;
7660 dirs[ndirs].length = files[i].fname - files[i].path;
7661 dirs[ndirs].count = 1;
7662 dirs[ndirs].dir_idx = ndirs;
7663 dirs[ndirs].used = 0;
7664 files[i].dir_idx = ndirs;
7666 /* Search for a prefix. */
7667 dirs[ndirs].prefix = -1;
7668 for (j = 0; j < ndirs; j++)
7669 if (dirs[j].length < dirs[ndirs].length
7670 && dirs[j].length > 1
7671 && (dirs[ndirs].prefix == -1
7672 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
7673 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
7674 dirs[ndirs].prefix = j;
7676 ++ndirs;
7679 /* Now to the actual work. We have to find a subset of the directories which
7680 allow expressing the file name using references to the directory table
7681 with the least amount of characters. We do not do an exhaustive search
7682 where we would have to check out every combination of every single
7683 possible prefix. Instead we use a heuristic which provides nearly optimal
7684 results in most cases and never is much off. */
7685 saved = alloca (ndirs * sizeof (int));
7686 savehere = alloca (ndirs * sizeof (int));
7688 memset (saved, '\0', ndirs * sizeof (saved[0]));
7689 for (i = 0; i < ndirs; i++)
7691 size_t j;
7692 int total;
7694 /* We can always save some space for the current directory. But this
7695 does not mean it will be enough to justify adding the directory. */
7696 savehere[i] = dirs[i].length;
7697 total = (savehere[i] - saved[i]) * dirs[i].count;
7699 for (j = i + 1; j < ndirs; j++)
7701 savehere[j] = 0;
7702 if (saved[j] < dirs[i].length)
7704 /* Determine whether the dirs[i] path is a prefix of the
7705 dirs[j] path. */
7706 int k;
7708 k = dirs[j].prefix;
7709 while (k != -1 && k != (int) i)
7710 k = dirs[k].prefix;
7712 if (k == (int) i)
7714 /* Yes it is. We can possibly safe some memory but
7715 writing the filenames in dirs[j] relative to
7716 dirs[i]. */
7717 savehere[j] = dirs[i].length;
7718 total += (savehere[j] - saved[j]) * dirs[j].count;
7723 /* Check whether we can safe enough to justify adding the dirs[i]
7724 directory. */
7725 if (total > dirs[i].length + 1)
7727 /* It's worthwhile adding. */
7728 for (j = i; j < ndirs; j++)
7729 if (savehere[j] > 0)
7731 /* Remember how much we saved for this directory so far. */
7732 saved[j] = savehere[j];
7734 /* Remember the prefix directory. */
7735 dirs[j].dir_idx = i;
7740 /* We have to emit them in the order they appear in the file_table array
7741 since the index is used in the debug info generation. To do this
7742 efficiently we generate a back-mapping of the indices first. */
7743 backmap = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (int));
7744 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7746 backmap[files[i].file_idx] = i;
7748 /* Mark this directory as used. */
7749 dirs[dirs[files[i].dir_idx].dir_idx].used = 1;
7752 /* That was it. We are ready to emit the information. First emit the
7753 directory name table. We have to make sure the first actually emitted
7754 directory name has index one; zero is reserved for the current working
7755 directory. Make sure we do not confuse these indices with the one for the
7756 constructed table (even though most of the time they are identical). */
7757 idx = 1;
7758 idx_offset = dirs[0].length > 0 ? 1 : 0;
7759 for (i = 1 - idx_offset; i < ndirs; i++)
7760 if (dirs[i].used != 0)
7762 dirs[i].used = idx++;
7763 dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
7764 "Directory Entry: 0x%x", dirs[i].used);
7767 dw2_asm_output_data (1, 0, "End directory table");
7769 /* Correct the index for the current working directory entry if it
7770 exists. */
7771 if (idx_offset == 0)
7772 dirs[0].used = 0;
7774 /* Now write all the file names. */
7775 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7777 int file_idx = backmap[i];
7778 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
7780 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
7781 "File Entry: 0x%lx", (unsigned long) i);
7783 /* Include directory index. */
7784 dw2_asm_output_data_uleb128 (dirs[dir_idx].used, NULL);
7786 /* Modification time. */
7787 dw2_asm_output_data_uleb128 (0, NULL);
7789 /* File length in bytes. */
7790 dw2_asm_output_data_uleb128 (0, NULL);
7793 dw2_asm_output_data (1, 0, "End file name table");
7797 /* Output the source line number correspondence information. This
7798 information goes into the .debug_line section. */
7800 static void
7801 output_line_info (void)
7803 char l1[20], l2[20], p1[20], p2[20];
7804 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
7805 char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
7806 unsigned opc;
7807 unsigned n_op_args;
7808 unsigned long lt_index;
7809 unsigned long current_line;
7810 long line_offset;
7811 long line_delta;
7812 unsigned long current_file;
7813 unsigned long function;
7815 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
7816 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
7817 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
7818 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
7820 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7821 dw2_asm_output_data (4, 0xffffffff,
7822 "Initial length escape value indicating 64-bit DWARF extension");
7823 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
7824 "Length of Source Line Info");
7825 ASM_OUTPUT_LABEL (asm_out_file, l1);
7827 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7828 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
7829 ASM_OUTPUT_LABEL (asm_out_file, p1);
7831 /* Define the architecture-dependent minimum instruction length (in
7832 bytes). In this implementation of DWARF, this field is used for
7833 information purposes only. Since GCC generates assembly language,
7834 we have no a priori knowledge of how many instruction bytes are
7835 generated for each source line, and therefore can use only the
7836 DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
7837 commands. Accordingly, we fix this as `1', which is "correct
7838 enough" for all architectures, and don't let the target override. */
7839 dw2_asm_output_data (1, 1,
7840 "Minimum Instruction Length");
7842 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
7843 "Default is_stmt_start flag");
7844 dw2_asm_output_data (1, DWARF_LINE_BASE,
7845 "Line Base Value (Special Opcodes)");
7846 dw2_asm_output_data (1, DWARF_LINE_RANGE,
7847 "Line Range Value (Special Opcodes)");
7848 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
7849 "Special Opcode Base");
7851 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
7853 switch (opc)
7855 case DW_LNS_advance_pc:
7856 case DW_LNS_advance_line:
7857 case DW_LNS_set_file:
7858 case DW_LNS_set_column:
7859 case DW_LNS_fixed_advance_pc:
7860 n_op_args = 1;
7861 break;
7862 default:
7863 n_op_args = 0;
7864 break;
7867 dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
7868 opc, n_op_args);
7871 /* Write out the information about the files we use. */
7872 output_file_names ();
7873 ASM_OUTPUT_LABEL (asm_out_file, p2);
7875 /* We used to set the address register to the first location in the text
7876 section here, but that didn't accomplish anything since we already
7877 have a line note for the opening brace of the first function. */
7879 /* Generate the line number to PC correspondence table, encoded as
7880 a series of state machine operations. */
7881 current_file = 1;
7882 current_line = 1;
7884 if (cfun && in_cold_section_p)
7885 strcpy (prev_line_label, cfun->cold_section_label);
7886 else
7887 strcpy (prev_line_label, text_section_label);
7888 for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
7890 dw_line_info_ref line_info = &line_info_table[lt_index];
7892 #if 0
7893 /* Disable this optimization for now; GDB wants to see two line notes
7894 at the beginning of a function so it can find the end of the
7895 prologue. */
7897 /* Don't emit anything for redundant notes. Just updating the
7898 address doesn't accomplish anything, because we already assume
7899 that anything after the last address is this line. */
7900 if (line_info->dw_line_num == current_line
7901 && line_info->dw_file_num == current_file)
7902 continue;
7903 #endif
7905 /* Emit debug info for the address of the current line.
7907 Unfortunately, we have little choice here currently, and must always
7908 use the most general form. GCC does not know the address delta
7909 itself, so we can't use DW_LNS_advance_pc. Many ports do have length
7910 attributes which will give an upper bound on the address range. We
7911 could perhaps use length attributes to determine when it is safe to
7912 use DW_LNS_fixed_advance_pc. */
7914 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
7915 if (0)
7917 /* This can handle deltas up to 0xffff. This takes 3 bytes. */
7918 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7919 "DW_LNS_fixed_advance_pc");
7920 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
7922 else
7924 /* This can handle any delta. This takes
7925 4+DWARF2_ADDR_SIZE bytes. */
7926 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7927 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7928 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7929 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7932 strcpy (prev_line_label, line_label);
7934 /* Emit debug info for the source file of the current line, if
7935 different from the previous line. */
7936 if (line_info->dw_file_num != current_file)
7938 current_file = line_info->dw_file_num;
7939 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
7940 dw2_asm_output_data_uleb128 (current_file, "(\"%s\")",
7941 VARRAY_CHAR_PTR (file_table,
7942 current_file));
7945 /* Emit debug info for the current line number, choosing the encoding
7946 that uses the least amount of space. */
7947 if (line_info->dw_line_num != current_line)
7949 line_offset = line_info->dw_line_num - current_line;
7950 line_delta = line_offset - DWARF_LINE_BASE;
7951 current_line = line_info->dw_line_num;
7952 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
7953 /* This can handle deltas from -10 to 234, using the current
7954 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE. This
7955 takes 1 byte. */
7956 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
7957 "line %lu", current_line);
7958 else
7960 /* This can handle any delta. This takes at least 4 bytes,
7961 depending on the value being encoded. */
7962 dw2_asm_output_data (1, DW_LNS_advance_line,
7963 "advance to line %lu", current_line);
7964 dw2_asm_output_data_sleb128 (line_offset, NULL);
7965 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7968 else
7969 /* We still need to start a new row, so output a copy insn. */
7970 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7973 /* Emit debug info for the address of the end of the function. */
7974 if (0)
7976 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7977 "DW_LNS_fixed_advance_pc");
7978 dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
7980 else
7982 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7983 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7984 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7985 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
7988 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
7989 dw2_asm_output_data_uleb128 (1, NULL);
7990 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
7992 function = 0;
7993 current_file = 1;
7994 current_line = 1;
7995 for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
7997 dw_separate_line_info_ref line_info
7998 = &separate_line_info_table[lt_index];
8000 #if 0
8001 /* Don't emit anything for redundant notes. */
8002 if (line_info->dw_line_num == current_line
8003 && line_info->dw_file_num == current_file
8004 && line_info->function == function)
8005 goto cont;
8006 #endif
8008 /* Emit debug info for the address of the current line. If this is
8009 a new function, or the first line of a function, then we need
8010 to handle it differently. */
8011 ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
8012 lt_index);
8013 if (function != line_info->function)
8015 function = line_info->function;
8017 /* Set the address register to the first line in the function. */
8018 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8019 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8020 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8021 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8023 else
8025 /* ??? See the DW_LNS_advance_pc comment above. */
8026 if (0)
8028 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8029 "DW_LNS_fixed_advance_pc");
8030 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8032 else
8034 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8035 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8036 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8037 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8041 strcpy (prev_line_label, line_label);
8043 /* Emit debug info for the source file of the current line, if
8044 different from the previous line. */
8045 if (line_info->dw_file_num != current_file)
8047 current_file = line_info->dw_file_num;
8048 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8049 dw2_asm_output_data_uleb128 (current_file, "(\"%s\")",
8050 VARRAY_CHAR_PTR (file_table,
8051 current_file));
8054 /* Emit debug info for the current line number, choosing the encoding
8055 that uses the least amount of space. */
8056 if (line_info->dw_line_num != current_line)
8058 line_offset = line_info->dw_line_num - current_line;
8059 line_delta = line_offset - DWARF_LINE_BASE;
8060 current_line = line_info->dw_line_num;
8061 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
8062 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
8063 "line %lu", current_line);
8064 else
8066 dw2_asm_output_data (1, DW_LNS_advance_line,
8067 "advance to line %lu", current_line);
8068 dw2_asm_output_data_sleb128 (line_offset, NULL);
8069 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8072 else
8073 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8075 #if 0
8076 cont:
8077 #endif
8079 lt_index++;
8081 /* If we're done with a function, end its sequence. */
8082 if (lt_index == separate_line_info_table_in_use
8083 || separate_line_info_table[lt_index].function != function)
8085 current_file = 1;
8086 current_line = 1;
8088 /* Emit debug info for the address of the end of the function. */
8089 ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
8090 if (0)
8092 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8093 "DW_LNS_fixed_advance_pc");
8094 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8096 else
8098 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8099 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8100 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8101 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8104 /* Output the marker for the end of this sequence. */
8105 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
8106 dw2_asm_output_data_uleb128 (1, NULL);
8107 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
8111 /* Output the marker for the end of the line number info. */
8112 ASM_OUTPUT_LABEL (asm_out_file, l2);
8115 /* Given a pointer to a tree node for some base type, return a pointer to
8116 a DIE that describes the given type.
8118 This routine must only be called for GCC type nodes that correspond to
8119 Dwarf base (fundamental) types. */
8121 static dw_die_ref
8122 base_type_die (tree type)
8124 dw_die_ref base_type_result;
8125 enum dwarf_type encoding;
8127 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
8128 return 0;
8130 switch (TREE_CODE (type))
8132 case INTEGER_TYPE:
8133 if (TYPE_STRING_FLAG (type))
8135 if (TYPE_UNSIGNED (type))
8136 encoding = DW_ATE_unsigned_char;
8137 else
8138 encoding = DW_ATE_signed_char;
8140 else if (TYPE_UNSIGNED (type))
8141 encoding = DW_ATE_unsigned;
8142 else
8143 encoding = DW_ATE_signed;
8144 break;
8146 case REAL_TYPE:
8147 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
8148 encoding = DW_ATE_decimal_float;
8149 else
8150 encoding = DW_ATE_float;
8151 break;
8153 /* Dwarf2 doesn't know anything about complex ints, so use
8154 a user defined type for it. */
8155 case COMPLEX_TYPE:
8156 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
8157 encoding = DW_ATE_complex_float;
8158 else
8159 encoding = DW_ATE_lo_user;
8160 break;
8162 case BOOLEAN_TYPE:
8163 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
8164 encoding = DW_ATE_boolean;
8165 break;
8167 default:
8168 /* No other TREE_CODEs are Dwarf fundamental types. */
8169 gcc_unreachable ();
8172 base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
8174 /* This probably indicates a bug. */
8175 if (! TYPE_NAME (type))
8176 add_name_attribute (base_type_result, "__unknown__");
8178 add_AT_unsigned (base_type_result, DW_AT_byte_size,
8179 int_size_in_bytes (type));
8180 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
8182 return base_type_result;
8185 /* Given a pointer to an arbitrary ..._TYPE tree node, return a pointer to
8186 the Dwarf "root" type for the given input type. The Dwarf "root" type of
8187 a given type is generally the same as the given type, except that if the
8188 given type is a pointer or reference type, then the root type of the given
8189 type is the root type of the "basis" type for the pointer or reference
8190 type. (This definition of the "root" type is recursive.) Also, the root
8191 type of a `const' qualified type or a `volatile' qualified type is the
8192 root type of the given type without the qualifiers. */
8194 static tree
8195 root_type (tree type)
8197 if (TREE_CODE (type) == ERROR_MARK)
8198 return error_mark_node;
8200 switch (TREE_CODE (type))
8202 case ERROR_MARK:
8203 return error_mark_node;
8205 case POINTER_TYPE:
8206 case REFERENCE_TYPE:
8207 return type_main_variant (root_type (TREE_TYPE (type)));
8209 default:
8210 return type_main_variant (type);
8214 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
8215 given input type is a Dwarf "fundamental" type. Otherwise return null. */
8217 static inline int
8218 is_base_type (tree type)
8220 switch (TREE_CODE (type))
8222 case ERROR_MARK:
8223 case VOID_TYPE:
8224 case INTEGER_TYPE:
8225 case REAL_TYPE:
8226 case COMPLEX_TYPE:
8227 case BOOLEAN_TYPE:
8228 return 1;
8230 case ARRAY_TYPE:
8231 case RECORD_TYPE:
8232 case UNION_TYPE:
8233 case QUAL_UNION_TYPE:
8234 case ENUMERAL_TYPE:
8235 case FUNCTION_TYPE:
8236 case METHOD_TYPE:
8237 case POINTER_TYPE:
8238 case REFERENCE_TYPE:
8239 case OFFSET_TYPE:
8240 case LANG_TYPE:
8241 case VECTOR_TYPE:
8242 return 0;
8244 default:
8245 gcc_unreachable ();
8248 return 0;
8251 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
8252 node, return the size in bits for the type if it is a constant, or else
8253 return the alignment for the type if the type's size is not constant, or
8254 else return BITS_PER_WORD if the type actually turns out to be an
8255 ERROR_MARK node. */
8257 static inline unsigned HOST_WIDE_INT
8258 simple_type_size_in_bits (tree type)
8260 if (TREE_CODE (type) == ERROR_MARK)
8261 return BITS_PER_WORD;
8262 else if (TYPE_SIZE (type) == NULL_TREE)
8263 return 0;
8264 else if (host_integerp (TYPE_SIZE (type), 1))
8265 return tree_low_cst (TYPE_SIZE (type), 1);
8266 else
8267 return TYPE_ALIGN (type);
8270 /* Return true if the debug information for the given type should be
8271 emitted as a subrange type. */
8273 static inline bool
8274 is_subrange_type (tree type)
8276 tree subtype = TREE_TYPE (type);
8278 /* Subrange types are identified by the fact that they are integer
8279 types, and that they have a subtype which is either an integer type
8280 or an enumeral type. */
8282 if (TREE_CODE (type) != INTEGER_TYPE
8283 || subtype == NULL_TREE)
8284 return false;
8286 if (TREE_CODE (subtype) != INTEGER_TYPE
8287 && TREE_CODE (subtype) != ENUMERAL_TYPE)
8288 return false;
8290 if (TREE_CODE (type) == TREE_CODE (subtype)
8291 && int_size_in_bytes (type) == int_size_in_bytes (subtype)
8292 && TYPE_MIN_VALUE (type) != NULL
8293 && TYPE_MIN_VALUE (subtype) != NULL
8294 && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
8295 && TYPE_MAX_VALUE (type) != NULL
8296 && TYPE_MAX_VALUE (subtype) != NULL
8297 && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
8299 /* The type and its subtype have the same representation. If in
8300 addition the two types also have the same name, then the given
8301 type is not a subrange type, but rather a plain base type. */
8302 /* FIXME: brobecker/2004-03-22:
8303 Sizetype INTEGER_CSTs nodes are canonicalized. It should
8304 therefore be sufficient to check the TYPE_SIZE node pointers
8305 rather than checking the actual size. Unfortunately, we have
8306 found some cases, such as in the Ada "integer" type, where
8307 this is not the case. Until this problem is solved, we need to
8308 keep checking the actual size. */
8309 tree type_name = TYPE_NAME (type);
8310 tree subtype_name = TYPE_NAME (subtype);
8312 if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
8313 type_name = DECL_NAME (type_name);
8315 if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
8316 subtype_name = DECL_NAME (subtype_name);
8318 if (type_name == subtype_name)
8319 return false;
8322 return true;
8325 /* Given a pointer to a tree node for a subrange type, return a pointer
8326 to a DIE that describes the given type. */
8328 static dw_die_ref
8329 subrange_type_die (tree type, dw_die_ref context_die)
8331 dw_die_ref subrange_die;
8332 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
8334 if (context_die == NULL)
8335 context_die = comp_unit_die;
8337 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
8339 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
8341 /* The size of the subrange type and its base type do not match,
8342 so we need to generate a size attribute for the subrange type. */
8343 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
8346 if (TYPE_MIN_VALUE (type) != NULL)
8347 add_bound_info (subrange_die, DW_AT_lower_bound,
8348 TYPE_MIN_VALUE (type));
8349 if (TYPE_MAX_VALUE (type) != NULL)
8350 add_bound_info (subrange_die, DW_AT_upper_bound,
8351 TYPE_MAX_VALUE (type));
8353 return subrange_die;
8356 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
8357 entry that chains various modifiers in front of the given type. */
8359 static dw_die_ref
8360 modified_type_die (tree type, int is_const_type, int is_volatile_type,
8361 dw_die_ref context_die)
8363 enum tree_code code = TREE_CODE (type);
8364 dw_die_ref mod_type_die;
8365 dw_die_ref sub_die = NULL;
8366 tree item_type = NULL;
8367 tree qualified_type;
8368 tree name;
8370 if (code == ERROR_MARK)
8371 return NULL;
8373 /* See if we already have the appropriately qualified variant of
8374 this type. */
8375 qualified_type
8376 = get_qualified_type (type,
8377 ((is_const_type ? TYPE_QUAL_CONST : 0)
8378 | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
8380 /* If we do, then we can just use its DIE, if it exists. */
8381 if (qualified_type)
8383 mod_type_die = lookup_type_die (qualified_type);
8384 if (mod_type_die)
8385 return mod_type_die;
8388 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
8390 /* Handle C typedef types. */
8391 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
8393 tree dtype = TREE_TYPE (name);
8395 if (qualified_type == dtype)
8397 /* For a named type, use the typedef. */
8398 gen_type_die (qualified_type, context_die);
8399 return lookup_type_die (qualified_type);
8401 else if (DECL_ORIGINAL_TYPE (name)
8402 && (is_const_type < TYPE_READONLY (dtype)
8403 || is_volatile_type < TYPE_VOLATILE (dtype)))
8404 /* cv-unqualified version of named type. Just use the unnamed
8405 type to which it refers. */
8406 return modified_type_die (DECL_ORIGINAL_TYPE (name),
8407 is_const_type, is_volatile_type,
8408 context_die);
8409 /* Else cv-qualified version of named type; fall through. */
8412 if (is_const_type)
8414 mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
8415 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
8417 else if (is_volatile_type)
8419 mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
8420 sub_die = modified_type_die (type, 0, 0, context_die);
8422 else if (code == POINTER_TYPE)
8424 mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
8425 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8426 simple_type_size_in_bits (type) / BITS_PER_UNIT);
8427 item_type = TREE_TYPE (type);
8429 else if (code == REFERENCE_TYPE)
8431 mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
8432 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8433 simple_type_size_in_bits (type) / BITS_PER_UNIT);
8434 item_type = TREE_TYPE (type);
8436 else if (is_subrange_type (type))
8438 mod_type_die = subrange_type_die (type, context_die);
8439 item_type = TREE_TYPE (type);
8441 else if (is_base_type (type))
8442 mod_type_die = base_type_die (type);
8443 else
8445 gen_type_die (type, context_die);
8447 /* We have to get the type_main_variant here (and pass that to the
8448 `lookup_type_die' routine) because the ..._TYPE node we have
8449 might simply be a *copy* of some original type node (where the
8450 copy was created to help us keep track of typedef names) and
8451 that copy might have a different TYPE_UID from the original
8452 ..._TYPE node. */
8453 if (TREE_CODE (type) != VECTOR_TYPE)
8454 return lookup_type_die (type_main_variant (type));
8455 else
8456 /* Vectors have the debugging information in the type,
8457 not the main variant. */
8458 return lookup_type_die (type);
8461 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
8462 don't output a DW_TAG_typedef, since there isn't one in the
8463 user's program; just attach a DW_AT_name to the type. */
8464 if (name
8465 && (TREE_CODE (name) != TYPE_DECL || TREE_TYPE (name) == qualified_type))
8467 if (TREE_CODE (name) == TYPE_DECL)
8468 /* Could just call add_name_and_src_coords_attributes here,
8469 but since this is a builtin type it doesn't have any
8470 useful source coordinates anyway. */
8471 name = DECL_NAME (name);
8472 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
8475 if (qualified_type)
8476 equate_type_number_to_die (qualified_type, mod_type_die);
8478 if (item_type)
8479 /* We must do this after the equate_type_number_to_die call, in case
8480 this is a recursive type. This ensures that the modified_type_die
8481 recursion will terminate even if the type is recursive. Recursive
8482 types are possible in Ada. */
8483 sub_die = modified_type_die (item_type,
8484 TYPE_READONLY (item_type),
8485 TYPE_VOLATILE (item_type),
8486 context_die);
8488 if (sub_die != NULL)
8489 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
8491 return mod_type_die;
8494 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
8495 an enumerated type. */
8497 static inline int
8498 type_is_enum (tree type)
8500 return TREE_CODE (type) == ENUMERAL_TYPE;
8503 /* Return the DBX register number described by a given RTL node. */
8505 static unsigned int
8506 dbx_reg_number (rtx rtl)
8508 unsigned regno = REGNO (rtl);
8510 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
8512 #ifdef LEAF_REG_REMAP
8513 regno = LEAF_REG_REMAP (regno);
8514 #endif
8516 return DBX_REGISTER_NUMBER (regno);
8519 /* Optionally add a DW_OP_piece term to a location description expression.
8520 DW_OP_piece is only added if the location description expression already
8521 doesn't end with DW_OP_piece. */
8523 static void
8524 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
8526 dw_loc_descr_ref loc;
8528 if (*list_head != NULL)
8530 /* Find the end of the chain. */
8531 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
8534 if (loc->dw_loc_opc != DW_OP_piece)
8535 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
8539 /* Return a location descriptor that designates a machine register or
8540 zero if there is none. */
8542 static dw_loc_descr_ref
8543 reg_loc_descriptor (rtx rtl)
8545 rtx regs;
8547 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
8548 return 0;
8550 regs = targetm.dwarf_register_span (rtl);
8552 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
8553 return multiple_reg_loc_descriptor (rtl, regs);
8554 else
8555 return one_reg_loc_descriptor (dbx_reg_number (rtl));
8558 /* Return a location descriptor that designates a machine register for
8559 a given hard register number. */
8561 static dw_loc_descr_ref
8562 one_reg_loc_descriptor (unsigned int regno)
8564 if (regno <= 31)
8565 return new_loc_descr (DW_OP_reg0 + regno, 0, 0);
8566 else
8567 return new_loc_descr (DW_OP_regx, regno, 0);
8570 /* Given an RTL of a register, return a location descriptor that
8571 designates a value that spans more than one register. */
8573 static dw_loc_descr_ref
8574 multiple_reg_loc_descriptor (rtx rtl, rtx regs)
8576 int nregs, size, i;
8577 unsigned reg;
8578 dw_loc_descr_ref loc_result = NULL;
8580 reg = REGNO (rtl);
8581 #ifdef LEAF_REG_REMAP
8582 reg = LEAF_REG_REMAP (reg);
8583 #endif
8584 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
8585 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
8587 /* Simple, contiguous registers. */
8588 if (regs == NULL_RTX)
8590 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
8592 loc_result = NULL;
8593 while (nregs--)
8595 dw_loc_descr_ref t;
8597 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg));
8598 add_loc_descr (&loc_result, t);
8599 add_loc_descr_op_piece (&loc_result, size);
8600 ++reg;
8602 return loc_result;
8605 /* Now onto stupid register sets in non contiguous locations. */
8607 gcc_assert (GET_CODE (regs) == PARALLEL);
8609 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8610 loc_result = NULL;
8612 for (i = 0; i < XVECLEN (regs, 0); ++i)
8614 dw_loc_descr_ref t;
8616 t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)));
8617 add_loc_descr (&loc_result, t);
8618 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8619 add_loc_descr_op_piece (&loc_result, size);
8621 return loc_result;
8624 /* Return a location descriptor that designates a constant. */
8626 static dw_loc_descr_ref
8627 int_loc_descriptor (HOST_WIDE_INT i)
8629 enum dwarf_location_atom op;
8631 /* Pick the smallest representation of a constant, rather than just
8632 defaulting to the LEB encoding. */
8633 if (i >= 0)
8635 if (i <= 31)
8636 op = DW_OP_lit0 + i;
8637 else if (i <= 0xff)
8638 op = DW_OP_const1u;
8639 else if (i <= 0xffff)
8640 op = DW_OP_const2u;
8641 else if (HOST_BITS_PER_WIDE_INT == 32
8642 || i <= 0xffffffff)
8643 op = DW_OP_const4u;
8644 else
8645 op = DW_OP_constu;
8647 else
8649 if (i >= -0x80)
8650 op = DW_OP_const1s;
8651 else if (i >= -0x8000)
8652 op = DW_OP_const2s;
8653 else if (HOST_BITS_PER_WIDE_INT == 32
8654 || i >= -0x80000000)
8655 op = DW_OP_const4s;
8656 else
8657 op = DW_OP_consts;
8660 return new_loc_descr (op, i, 0);
8663 /* Return a location descriptor that designates a base+offset location. */
8665 static dw_loc_descr_ref
8666 based_loc_descr (rtx reg, HOST_WIDE_INT offset)
8668 unsigned int regno;
8670 /* We only use "frame base" when we're sure we're talking about the
8671 post-prologue local stack frame. We do this by *not* running
8672 register elimination until this point, and recognizing the special
8673 argument pointer and soft frame pointer rtx's. */
8674 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
8676 rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
8678 if (elim != reg)
8680 if (GET_CODE (elim) == PLUS)
8682 offset += INTVAL (XEXP (elim, 1));
8683 elim = XEXP (elim, 0);
8685 gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
8686 : stack_pointer_rtx));
8687 offset += frame_pointer_fb_offset;
8689 return new_loc_descr (DW_OP_fbreg, offset, 0);
8693 regno = dbx_reg_number (reg);
8694 if (regno <= 31)
8695 return new_loc_descr (DW_OP_breg0 + regno, offset, 0);
8696 else
8697 return new_loc_descr (DW_OP_bregx, regno, offset);
8700 /* Return true if this RTL expression describes a base+offset calculation. */
8702 static inline int
8703 is_based_loc (rtx rtl)
8705 return (GET_CODE (rtl) == PLUS
8706 && ((REG_P (XEXP (rtl, 0))
8707 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
8708 && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
8711 /* The following routine converts the RTL for a variable or parameter
8712 (resident in memory) into an equivalent Dwarf representation of a
8713 mechanism for getting the address of that same variable onto the top of a
8714 hypothetical "address evaluation" stack.
8716 When creating memory location descriptors, we are effectively transforming
8717 the RTL for a memory-resident object into its Dwarf postfix expression
8718 equivalent. This routine recursively descends an RTL tree, turning
8719 it into Dwarf postfix code as it goes.
8721 MODE is the mode of the memory reference, needed to handle some
8722 autoincrement addressing modes.
8724 CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
8725 location list for RTL.
8727 Return 0 if we can't represent the location. */
8729 static dw_loc_descr_ref
8730 mem_loc_descriptor (rtx rtl, enum machine_mode mode)
8732 dw_loc_descr_ref mem_loc_result = NULL;
8733 enum dwarf_location_atom op;
8735 /* Note that for a dynamically sized array, the location we will generate a
8736 description of here will be the lowest numbered location which is
8737 actually within the array. That's *not* necessarily the same as the
8738 zeroth element of the array. */
8740 rtl = targetm.delegitimize_address (rtl);
8742 switch (GET_CODE (rtl))
8744 case POST_INC:
8745 case POST_DEC:
8746 case POST_MODIFY:
8747 /* POST_INC and POST_DEC can be handled just like a SUBREG. So we
8748 just fall into the SUBREG code. */
8750 /* ... fall through ... */
8752 case SUBREG:
8753 /* The case of a subreg may arise when we have a local (register)
8754 variable or a formal (register) parameter which doesn't quite fill
8755 up an entire register. For now, just assume that it is
8756 legitimate to make the Dwarf info refer to the whole register which
8757 contains the given subreg. */
8758 rtl = XEXP (rtl, 0);
8760 /* ... fall through ... */
8762 case REG:
8763 /* Whenever a register number forms a part of the description of the
8764 method for calculating the (dynamic) address of a memory resident
8765 object, DWARF rules require the register number be referred to as
8766 a "base register". This distinction is not based in any way upon
8767 what category of register the hardware believes the given register
8768 belongs to. This is strictly DWARF terminology we're dealing with
8769 here. Note that in cases where the location of a memory-resident
8770 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
8771 OP_CONST (0)) the actual DWARF location descriptor that we generate
8772 may just be OP_BASEREG (basereg). This may look deceptively like
8773 the object in question was allocated to a register (rather than in
8774 memory) so DWARF consumers need to be aware of the subtle
8775 distinction between OP_REG and OP_BASEREG. */
8776 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
8777 mem_loc_result = based_loc_descr (rtl, 0);
8778 break;
8780 case MEM:
8781 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl));
8782 if (mem_loc_result != 0)
8783 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
8784 break;
8786 case LO_SUM:
8787 rtl = XEXP (rtl, 1);
8789 /* ... fall through ... */
8791 case LABEL_REF:
8792 /* Some ports can transform a symbol ref into a label ref, because
8793 the symbol ref is too far away and has to be dumped into a constant
8794 pool. */
8795 case CONST:
8796 case SYMBOL_REF:
8797 /* Alternatively, the symbol in the constant pool might be referenced
8798 by a different symbol. */
8799 if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
8801 bool marked;
8802 rtx tmp = get_pool_constant_mark (rtl, &marked);
8804 if (GET_CODE (tmp) == SYMBOL_REF)
8806 rtl = tmp;
8807 if (CONSTANT_POOL_ADDRESS_P (tmp))
8808 get_pool_constant_mark (tmp, &marked);
8809 else
8810 marked = true;
8813 /* If all references to this pool constant were optimized away,
8814 it was not output and thus we can't represent it.
8815 FIXME: might try to use DW_OP_const_value here, though
8816 DW_OP_piece complicates it. */
8817 if (!marked)
8818 return 0;
8821 mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
8822 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
8823 mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
8824 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
8825 break;
8827 case PRE_MODIFY:
8828 /* Extract the PLUS expression nested inside and fall into
8829 PLUS code below. */
8830 rtl = XEXP (rtl, 1);
8831 goto plus;
8833 case PRE_INC:
8834 case PRE_DEC:
8835 /* Turn these into a PLUS expression and fall into the PLUS code
8836 below. */
8837 rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
8838 GEN_INT (GET_CODE (rtl) == PRE_INC
8839 ? GET_MODE_UNIT_SIZE (mode)
8840 : -GET_MODE_UNIT_SIZE (mode)));
8842 /* ... fall through ... */
8844 case PLUS:
8845 plus:
8846 if (is_based_loc (rtl))
8847 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
8848 INTVAL (XEXP (rtl, 1)));
8849 else
8851 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode);
8852 if (mem_loc_result == 0)
8853 break;
8855 if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
8856 && INTVAL (XEXP (rtl, 1)) >= 0)
8857 add_loc_descr (&mem_loc_result,
8858 new_loc_descr (DW_OP_plus_uconst,
8859 INTVAL (XEXP (rtl, 1)), 0));
8860 else
8862 add_loc_descr (&mem_loc_result,
8863 mem_loc_descriptor (XEXP (rtl, 1), mode));
8864 add_loc_descr (&mem_loc_result,
8865 new_loc_descr (DW_OP_plus, 0, 0));
8868 break;
8870 /* If a pseudo-reg is optimized away, it is possible for it to
8871 be replaced with a MEM containing a multiply or shift. */
8872 case MULT:
8873 op = DW_OP_mul;
8874 goto do_binop;
8876 case ASHIFT:
8877 op = DW_OP_shl;
8878 goto do_binop;
8880 case ASHIFTRT:
8881 op = DW_OP_shra;
8882 goto do_binop;
8884 case LSHIFTRT:
8885 op = DW_OP_shr;
8886 goto do_binop;
8888 do_binop:
8890 dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode);
8891 dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode);
8893 if (op0 == 0 || op1 == 0)
8894 break;
8896 mem_loc_result = op0;
8897 add_loc_descr (&mem_loc_result, op1);
8898 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
8899 break;
8902 case CONST_INT:
8903 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
8904 break;
8906 default:
8907 gcc_unreachable ();
8910 return mem_loc_result;
8913 /* Return a descriptor that describes the concatenation of two locations.
8914 This is typically a complex variable. */
8916 static dw_loc_descr_ref
8917 concat_loc_descriptor (rtx x0, rtx x1)
8919 dw_loc_descr_ref cc_loc_result = NULL;
8920 dw_loc_descr_ref x0_ref = loc_descriptor (x0);
8921 dw_loc_descr_ref x1_ref = loc_descriptor (x1);
8923 if (x0_ref == 0 || x1_ref == 0)
8924 return 0;
8926 cc_loc_result = x0_ref;
8927 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
8929 add_loc_descr (&cc_loc_result, x1_ref);
8930 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
8932 return cc_loc_result;
8935 /* Output a proper Dwarf location descriptor for a variable or parameter
8936 which is either allocated in a register or in a memory location. For a
8937 register, we just generate an OP_REG and the register number. For a
8938 memory location we provide a Dwarf postfix expression describing how to
8939 generate the (dynamic) address of the object onto the address stack.
8941 If we don't know how to describe it, return 0. */
8943 static dw_loc_descr_ref
8944 loc_descriptor (rtx rtl)
8946 dw_loc_descr_ref loc_result = NULL;
8948 switch (GET_CODE (rtl))
8950 case SUBREG:
8951 /* The case of a subreg may arise when we have a local (register)
8952 variable or a formal (register) parameter which doesn't quite fill
8953 up an entire register. For now, just assume that it is
8954 legitimate to make the Dwarf info refer to the whole register which
8955 contains the given subreg. */
8956 rtl = SUBREG_REG (rtl);
8958 /* ... fall through ... */
8960 case REG:
8961 loc_result = reg_loc_descriptor (rtl);
8962 break;
8964 case MEM:
8965 loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl));
8966 break;
8968 case CONCAT:
8969 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1));
8970 break;
8972 case VAR_LOCATION:
8973 /* Single part. */
8974 if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
8976 loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0));
8977 break;
8980 rtl = XEXP (rtl, 1);
8981 /* FALLTHRU */
8983 case PARALLEL:
8985 rtvec par_elems = XVEC (rtl, 0);
8986 int num_elem = GET_NUM_ELEM (par_elems);
8987 enum machine_mode mode;
8988 int i;
8990 /* Create the first one, so we have something to add to. */
8991 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0));
8992 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
8993 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
8994 for (i = 1; i < num_elem; i++)
8996 dw_loc_descr_ref temp;
8998 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0));
8999 add_loc_descr (&loc_result, temp);
9000 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
9001 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9004 break;
9006 default:
9007 gcc_unreachable ();
9010 return loc_result;
9013 /* Similar, but generate the descriptor from trees instead of rtl. This comes
9014 up particularly with variable length arrays. WANT_ADDRESS is 2 if this is
9015 a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
9016 top-level invocation, and we require the address of LOC; is 0 if we require
9017 the value of LOC. */
9019 static dw_loc_descr_ref
9020 loc_descriptor_from_tree_1 (tree loc, int want_address)
9022 dw_loc_descr_ref ret, ret1;
9023 int have_address = 0;
9024 enum dwarf_location_atom op;
9026 /* ??? Most of the time we do not take proper care for sign/zero
9027 extending the values properly. Hopefully this won't be a real
9028 problem... */
9030 switch (TREE_CODE (loc))
9032 case ERROR_MARK:
9033 return 0;
9035 case PLACEHOLDER_EXPR:
9036 /* This case involves extracting fields from an object to determine the
9037 position of other fields. We don't try to encode this here. The
9038 only user of this is Ada, which encodes the needed information using
9039 the names of types. */
9040 return 0;
9042 case CALL_EXPR:
9043 return 0;
9045 case PREINCREMENT_EXPR:
9046 case PREDECREMENT_EXPR:
9047 case POSTINCREMENT_EXPR:
9048 case POSTDECREMENT_EXPR:
9049 /* There are no opcodes for these operations. */
9050 return 0;
9052 case ADDR_EXPR:
9053 /* If we already want an address, there's nothing we can do. */
9054 if (want_address)
9055 return 0;
9057 /* Otherwise, process the argument and look for the address. */
9058 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
9060 case VAR_DECL:
9061 if (DECL_THREAD_LOCAL_P (loc))
9063 rtx rtl;
9065 /* If this is not defined, we have no way to emit the data. */
9066 if (!targetm.asm_out.output_dwarf_dtprel)
9067 return 0;
9069 /* The way DW_OP_GNU_push_tls_address is specified, we can only
9070 look up addresses of objects in the current module. */
9071 if (DECL_EXTERNAL (loc))
9072 return 0;
9074 rtl = rtl_for_decl_location (loc);
9075 if (rtl == NULL_RTX)
9076 return 0;
9078 if (!MEM_P (rtl))
9079 return 0;
9080 rtl = XEXP (rtl, 0);
9081 if (! CONSTANT_P (rtl))
9082 return 0;
9084 ret = new_loc_descr (INTERNAL_DW_OP_tls_addr, 0, 0);
9085 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9086 ret->dw_loc_oprnd1.v.val_addr = rtl;
9088 ret1 = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
9089 add_loc_descr (&ret, ret1);
9091 have_address = 1;
9092 break;
9094 /* FALLTHRU */
9096 case PARM_DECL:
9097 if (DECL_HAS_VALUE_EXPR_P (loc))
9098 return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
9099 want_address);
9100 /* FALLTHRU */
9102 case RESULT_DECL:
9104 rtx rtl = rtl_for_decl_location (loc);
9106 if (rtl == NULL_RTX)
9107 return 0;
9108 else if (GET_CODE (rtl) == CONST_INT)
9110 HOST_WIDE_INT val = INTVAL (rtl);
9111 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
9112 val &= GET_MODE_MASK (DECL_MODE (loc));
9113 ret = int_loc_descriptor (val);
9115 else if (GET_CODE (rtl) == CONST_STRING)
9116 return 0;
9117 else if (CONSTANT_P (rtl))
9119 ret = new_loc_descr (DW_OP_addr, 0, 0);
9120 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9121 ret->dw_loc_oprnd1.v.val_addr = rtl;
9123 else
9125 enum machine_mode mode;
9127 /* Certain constructs can only be represented at top-level. */
9128 if (want_address == 2)
9129 return loc_descriptor (rtl);
9131 mode = GET_MODE (rtl);
9132 if (MEM_P (rtl))
9134 rtl = XEXP (rtl, 0);
9135 have_address = 1;
9137 ret = mem_loc_descriptor (rtl, mode);
9140 break;
9142 case INDIRECT_REF:
9143 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9144 have_address = 1;
9145 break;
9147 case COMPOUND_EXPR:
9148 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
9150 case NOP_EXPR:
9151 case CONVERT_EXPR:
9152 case NON_LVALUE_EXPR:
9153 case VIEW_CONVERT_EXPR:
9154 case SAVE_EXPR:
9155 case MODIFY_EXPR:
9156 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), want_address);
9158 case COMPONENT_REF:
9159 case BIT_FIELD_REF:
9160 case ARRAY_REF:
9161 case ARRAY_RANGE_REF:
9163 tree obj, offset;
9164 HOST_WIDE_INT bitsize, bitpos, bytepos;
9165 enum machine_mode mode;
9166 int volatilep;
9167 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
9169 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
9170 &unsignedp, &volatilep, false);
9172 if (obj == loc)
9173 return 0;
9175 ret = loc_descriptor_from_tree_1 (obj, 1);
9176 if (ret == 0
9177 || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
9178 return 0;
9180 if (offset != NULL_TREE)
9182 /* Variable offset. */
9183 add_loc_descr (&ret, loc_descriptor_from_tree_1 (offset, 0));
9184 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9187 bytepos = bitpos / BITS_PER_UNIT;
9188 if (bytepos > 0)
9189 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
9190 else if (bytepos < 0)
9192 add_loc_descr (&ret, int_loc_descriptor (bytepos));
9193 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9196 have_address = 1;
9197 break;
9200 case INTEGER_CST:
9201 if (host_integerp (loc, 0))
9202 ret = int_loc_descriptor (tree_low_cst (loc, 0));
9203 else
9204 return 0;
9205 break;
9207 case CONSTRUCTOR:
9209 /* Get an RTL for this, if something has been emitted. */
9210 rtx rtl = lookup_constant_def (loc);
9211 enum machine_mode mode;
9213 if (!rtl || !MEM_P (rtl))
9214 return 0;
9215 mode = GET_MODE (rtl);
9216 rtl = XEXP (rtl, 0);
9217 ret = mem_loc_descriptor (rtl, mode);
9218 have_address = 1;
9219 break;
9222 case TRUTH_AND_EXPR:
9223 case TRUTH_ANDIF_EXPR:
9224 case BIT_AND_EXPR:
9225 op = DW_OP_and;
9226 goto do_binop;
9228 case TRUTH_XOR_EXPR:
9229 case BIT_XOR_EXPR:
9230 op = DW_OP_xor;
9231 goto do_binop;
9233 case TRUTH_OR_EXPR:
9234 case TRUTH_ORIF_EXPR:
9235 case BIT_IOR_EXPR:
9236 op = DW_OP_or;
9237 goto do_binop;
9239 case FLOOR_DIV_EXPR:
9240 case CEIL_DIV_EXPR:
9241 case ROUND_DIV_EXPR:
9242 case TRUNC_DIV_EXPR:
9243 op = DW_OP_div;
9244 goto do_binop;
9246 case MINUS_EXPR:
9247 op = DW_OP_minus;
9248 goto do_binop;
9250 case FLOOR_MOD_EXPR:
9251 case CEIL_MOD_EXPR:
9252 case ROUND_MOD_EXPR:
9253 case TRUNC_MOD_EXPR:
9254 op = DW_OP_mod;
9255 goto do_binop;
9257 case MULT_EXPR:
9258 op = DW_OP_mul;
9259 goto do_binop;
9261 case LSHIFT_EXPR:
9262 op = DW_OP_shl;
9263 goto do_binop;
9265 case RSHIFT_EXPR:
9266 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
9267 goto do_binop;
9269 case PLUS_EXPR:
9270 if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
9271 && host_integerp (TREE_OPERAND (loc, 1), 0))
9273 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9274 if (ret == 0)
9275 return 0;
9277 add_loc_descr (&ret,
9278 new_loc_descr (DW_OP_plus_uconst,
9279 tree_low_cst (TREE_OPERAND (loc, 1),
9281 0));
9282 break;
9285 op = DW_OP_plus;
9286 goto do_binop;
9288 case LE_EXPR:
9289 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9290 return 0;
9292 op = DW_OP_le;
9293 goto do_binop;
9295 case GE_EXPR:
9296 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9297 return 0;
9299 op = DW_OP_ge;
9300 goto do_binop;
9302 case LT_EXPR:
9303 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9304 return 0;
9306 op = DW_OP_lt;
9307 goto do_binop;
9309 case GT_EXPR:
9310 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9311 return 0;
9313 op = DW_OP_gt;
9314 goto do_binop;
9316 case EQ_EXPR:
9317 op = DW_OP_eq;
9318 goto do_binop;
9320 case NE_EXPR:
9321 op = DW_OP_ne;
9322 goto do_binop;
9324 do_binop:
9325 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9326 ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9327 if (ret == 0 || ret1 == 0)
9328 return 0;
9330 add_loc_descr (&ret, ret1);
9331 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9332 break;
9334 case TRUTH_NOT_EXPR:
9335 case BIT_NOT_EXPR:
9336 op = DW_OP_not;
9337 goto do_unop;
9339 case ABS_EXPR:
9340 op = DW_OP_abs;
9341 goto do_unop;
9343 case NEGATE_EXPR:
9344 op = DW_OP_neg;
9345 goto do_unop;
9347 do_unop:
9348 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9349 if (ret == 0)
9350 return 0;
9352 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9353 break;
9355 case MIN_EXPR:
9356 case MAX_EXPR:
9358 const enum tree_code code =
9359 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
9361 loc = build3 (COND_EXPR, TREE_TYPE (loc),
9362 build2 (code, integer_type_node,
9363 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
9364 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
9367 /* ... fall through ... */
9369 case COND_EXPR:
9371 dw_loc_descr_ref lhs
9372 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9373 dw_loc_descr_ref rhs
9374 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
9375 dw_loc_descr_ref bra_node, jump_node, tmp;
9377 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9378 if (ret == 0 || lhs == 0 || rhs == 0)
9379 return 0;
9381 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
9382 add_loc_descr (&ret, bra_node);
9384 add_loc_descr (&ret, rhs);
9385 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
9386 add_loc_descr (&ret, jump_node);
9388 add_loc_descr (&ret, lhs);
9389 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9390 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
9392 /* ??? Need a node to point the skip at. Use a nop. */
9393 tmp = new_loc_descr (DW_OP_nop, 0, 0);
9394 add_loc_descr (&ret, tmp);
9395 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9396 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
9398 break;
9400 case FIX_TRUNC_EXPR:
9401 case FIX_CEIL_EXPR:
9402 case FIX_FLOOR_EXPR:
9403 case FIX_ROUND_EXPR:
9404 return 0;
9406 default:
9407 /* Leave front-end specific codes as simply unknown. This comes
9408 up, for instance, with the C STMT_EXPR. */
9409 if ((unsigned int) TREE_CODE (loc)
9410 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
9411 return 0;
9413 #ifdef ENABLE_CHECKING
9414 /* Otherwise this is a generic code; we should just lists all of
9415 these explicitly. We forgot one. */
9416 gcc_unreachable ();
9417 #else
9418 /* In a release build, we want to degrade gracefully: better to
9419 generate incomplete debugging information than to crash. */
9420 return NULL;
9421 #endif
9424 /* Show if we can't fill the request for an address. */
9425 if (want_address && !have_address)
9426 return 0;
9428 /* If we've got an address and don't want one, dereference. */
9429 if (!want_address && have_address && ret)
9431 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
9433 if (size > DWARF2_ADDR_SIZE || size == -1)
9434 return 0;
9435 else if (size == DWARF2_ADDR_SIZE)
9436 op = DW_OP_deref;
9437 else
9438 op = DW_OP_deref_size;
9440 add_loc_descr (&ret, new_loc_descr (op, size, 0));
9443 return ret;
9446 static inline dw_loc_descr_ref
9447 loc_descriptor_from_tree (tree loc)
9449 return loc_descriptor_from_tree_1 (loc, 2);
9452 /* Given a value, round it up to the lowest multiple of `boundary'
9453 which is not less than the value itself. */
9455 static inline HOST_WIDE_INT
9456 ceiling (HOST_WIDE_INT value, unsigned int boundary)
9458 return (((value + boundary - 1) / boundary) * boundary);
9461 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
9462 pointer to the declared type for the relevant field variable, or return
9463 `integer_type_node' if the given node turns out to be an
9464 ERROR_MARK node. */
9466 static inline tree
9467 field_type (tree decl)
9469 tree type;
9471 if (TREE_CODE (decl) == ERROR_MARK)
9472 return integer_type_node;
9474 type = DECL_BIT_FIELD_TYPE (decl);
9475 if (type == NULL_TREE)
9476 type = TREE_TYPE (decl);
9478 return type;
9481 /* Given a pointer to a tree node, return the alignment in bits for
9482 it, or else return BITS_PER_WORD if the node actually turns out to
9483 be an ERROR_MARK node. */
9485 static inline unsigned
9486 simple_type_align_in_bits (tree type)
9488 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
9491 static inline unsigned
9492 simple_decl_align_in_bits (tree decl)
9494 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
9497 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
9498 lowest addressed byte of the "containing object" for the given FIELD_DECL,
9499 or return 0 if we are unable to determine what that offset is, either
9500 because the argument turns out to be a pointer to an ERROR_MARK node, or
9501 because the offset is actually variable. (We can't handle the latter case
9502 just yet). */
9504 static HOST_WIDE_INT
9505 field_byte_offset (tree decl)
9507 unsigned int type_align_in_bits;
9508 unsigned int decl_align_in_bits;
9509 unsigned HOST_WIDE_INT type_size_in_bits;
9510 HOST_WIDE_INT object_offset_in_bits;
9511 tree type;
9512 tree field_size_tree;
9513 HOST_WIDE_INT bitpos_int;
9514 HOST_WIDE_INT deepest_bitpos;
9515 unsigned HOST_WIDE_INT field_size_in_bits;
9517 if (TREE_CODE (decl) == ERROR_MARK)
9518 return 0;
9520 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
9522 type = field_type (decl);
9523 field_size_tree = DECL_SIZE (decl);
9525 /* The size could be unspecified if there was an error, or for
9526 a flexible array member. */
9527 if (! field_size_tree)
9528 field_size_tree = bitsize_zero_node;
9530 /* We cannot yet cope with fields whose positions are variable, so
9531 for now, when we see such things, we simply return 0. Someday, we may
9532 be able to handle such cases, but it will be damn difficult. */
9533 if (! host_integerp (bit_position (decl), 0))
9534 return 0;
9536 bitpos_int = int_bit_position (decl);
9538 /* If we don't know the size of the field, pretend it's a full word. */
9539 if (host_integerp (field_size_tree, 1))
9540 field_size_in_bits = tree_low_cst (field_size_tree, 1);
9541 else
9542 field_size_in_bits = BITS_PER_WORD;
9544 type_size_in_bits = simple_type_size_in_bits (type);
9545 type_align_in_bits = simple_type_align_in_bits (type);
9546 decl_align_in_bits = simple_decl_align_in_bits (decl);
9548 /* The GCC front-end doesn't make any attempt to keep track of the starting
9549 bit offset (relative to the start of the containing structure type) of the
9550 hypothetical "containing object" for a bit-field. Thus, when computing
9551 the byte offset value for the start of the "containing object" of a
9552 bit-field, we must deduce this information on our own. This can be rather
9553 tricky to do in some cases. For example, handling the following structure
9554 type definition when compiling for an i386/i486 target (which only aligns
9555 long long's to 32-bit boundaries) can be very tricky:
9557 struct S { int field1; long long field2:31; };
9559 Fortunately, there is a simple rule-of-thumb which can be used in such
9560 cases. When compiling for an i386/i486, GCC will allocate 8 bytes for the
9561 structure shown above. It decides to do this based upon one simple rule
9562 for bit-field allocation. GCC allocates each "containing object" for each
9563 bit-field at the first (i.e. lowest addressed) legitimate alignment
9564 boundary (based upon the required minimum alignment for the declared type
9565 of the field) which it can possibly use, subject to the condition that
9566 there is still enough available space remaining in the containing object
9567 (when allocated at the selected point) to fully accommodate all of the
9568 bits of the bit-field itself.
9570 This simple rule makes it obvious why GCC allocates 8 bytes for each
9571 object of the structure type shown above. When looking for a place to
9572 allocate the "containing object" for `field2', the compiler simply tries
9573 to allocate a 64-bit "containing object" at each successive 32-bit
9574 boundary (starting at zero) until it finds a place to allocate that 64-
9575 bit field such that at least 31 contiguous (and previously unallocated)
9576 bits remain within that selected 64 bit field. (As it turns out, for the
9577 example above, the compiler finds it is OK to allocate the "containing
9578 object" 64-bit field at bit-offset zero within the structure type.)
9580 Here we attempt to work backwards from the limited set of facts we're
9581 given, and we try to deduce from those facts, where GCC must have believed
9582 that the containing object started (within the structure type). The value
9583 we deduce is then used (by the callers of this routine) to generate
9584 DW_AT_location and DW_AT_bit_offset attributes for fields (both bit-fields
9585 and, in the case of DW_AT_location, regular fields as well). */
9587 /* Figure out the bit-distance from the start of the structure to the
9588 "deepest" bit of the bit-field. */
9589 deepest_bitpos = bitpos_int + field_size_in_bits;
9591 /* This is the tricky part. Use some fancy footwork to deduce where the
9592 lowest addressed bit of the containing object must be. */
9593 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9595 /* Round up to type_align by default. This works best for bitfields. */
9596 object_offset_in_bits += type_align_in_bits - 1;
9597 object_offset_in_bits /= type_align_in_bits;
9598 object_offset_in_bits *= type_align_in_bits;
9600 if (object_offset_in_bits > bitpos_int)
9602 /* Sigh, the decl must be packed. */
9603 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9605 /* Round up to decl_align instead. */
9606 object_offset_in_bits += decl_align_in_bits - 1;
9607 object_offset_in_bits /= decl_align_in_bits;
9608 object_offset_in_bits *= decl_align_in_bits;
9611 return object_offset_in_bits / BITS_PER_UNIT;
9614 /* The following routines define various Dwarf attributes and any data
9615 associated with them. */
9617 /* Add a location description attribute value to a DIE.
9619 This emits location attributes suitable for whole variables and
9620 whole parameters. Note that the location attributes for struct fields are
9621 generated by the routine `data_member_location_attribute' below. */
9623 static inline void
9624 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
9625 dw_loc_descr_ref descr)
9627 if (descr != 0)
9628 add_AT_loc (die, attr_kind, descr);
9631 /* Attach the specialized form of location attribute used for data members of
9632 struct and union types. In the special case of a FIELD_DECL node which
9633 represents a bit-field, the "offset" part of this special location
9634 descriptor must indicate the distance in bytes from the lowest-addressed
9635 byte of the containing struct or union type to the lowest-addressed byte of
9636 the "containing object" for the bit-field. (See the `field_byte_offset'
9637 function above).
9639 For any given bit-field, the "containing object" is a hypothetical object
9640 (of some integral or enum type) within which the given bit-field lives. The
9641 type of this hypothetical "containing object" is always the same as the
9642 declared type of the individual bit-field itself (for GCC anyway... the
9643 DWARF spec doesn't actually mandate this). Note that it is the size (in
9644 bytes) of the hypothetical "containing object" which will be given in the
9645 DW_AT_byte_size attribute for this bit-field. (See the
9646 `byte_size_attribute' function below.) It is also used when calculating the
9647 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
9648 function below.) */
9650 static void
9651 add_data_member_location_attribute (dw_die_ref die, tree decl)
9653 HOST_WIDE_INT offset;
9654 dw_loc_descr_ref loc_descr = 0;
9656 if (TREE_CODE (decl) == TREE_BINFO)
9658 /* We're working on the TAG_inheritance for a base class. */
9659 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
9661 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
9662 aren't at a fixed offset from all (sub)objects of the same
9663 type. We need to extract the appropriate offset from our
9664 vtable. The following dwarf expression means
9666 BaseAddr = ObAddr + *((*ObAddr) - Offset)
9668 This is specific to the V3 ABI, of course. */
9670 dw_loc_descr_ref tmp;
9672 /* Make a copy of the object address. */
9673 tmp = new_loc_descr (DW_OP_dup, 0, 0);
9674 add_loc_descr (&loc_descr, tmp);
9676 /* Extract the vtable address. */
9677 tmp = new_loc_descr (DW_OP_deref, 0, 0);
9678 add_loc_descr (&loc_descr, tmp);
9680 /* Calculate the address of the offset. */
9681 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
9682 gcc_assert (offset < 0);
9684 tmp = int_loc_descriptor (-offset);
9685 add_loc_descr (&loc_descr, tmp);
9686 tmp = new_loc_descr (DW_OP_minus, 0, 0);
9687 add_loc_descr (&loc_descr, tmp);
9689 /* Extract the offset. */
9690 tmp = new_loc_descr (DW_OP_deref, 0, 0);
9691 add_loc_descr (&loc_descr, tmp);
9693 /* Add it to the object address. */
9694 tmp = new_loc_descr (DW_OP_plus, 0, 0);
9695 add_loc_descr (&loc_descr, tmp);
9697 else
9698 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
9700 else
9701 offset = field_byte_offset (decl);
9703 if (! loc_descr)
9705 enum dwarf_location_atom op;
9707 /* The DWARF2 standard says that we should assume that the structure
9708 address is already on the stack, so we can specify a structure field
9709 address by using DW_OP_plus_uconst. */
9711 #ifdef MIPS_DEBUGGING_INFO
9712 /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
9713 operator correctly. It works only if we leave the offset on the
9714 stack. */
9715 op = DW_OP_constu;
9716 #else
9717 op = DW_OP_plus_uconst;
9718 #endif
9720 loc_descr = new_loc_descr (op, offset, 0);
9723 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
9726 /* Writes integer values to dw_vec_const array. */
9728 static void
9729 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
9731 while (size != 0)
9733 *dest++ = val & 0xff;
9734 val >>= 8;
9735 --size;
9739 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
9741 static HOST_WIDE_INT
9742 extract_int (const unsigned char *src, unsigned int size)
9744 HOST_WIDE_INT val = 0;
9746 src += size;
9747 while (size != 0)
9749 val <<= 8;
9750 val |= *--src & 0xff;
9751 --size;
9753 return val;
9756 /* Writes floating point values to dw_vec_const array. */
9758 static void
9759 insert_float (rtx rtl, unsigned char *array)
9761 REAL_VALUE_TYPE rv;
9762 long val[4];
9763 int i;
9765 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
9766 real_to_target (val, &rv, GET_MODE (rtl));
9768 /* real_to_target puts 32-bit pieces in each long. Pack them. */
9769 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
9771 insert_int (val[i], 4, array);
9772 array += 4;
9776 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
9777 does not have a "location" either in memory or in a register. These
9778 things can arise in GNU C when a constant is passed as an actual parameter
9779 to an inlined function. They can also arise in C++ where declared
9780 constants do not necessarily get memory "homes". */
9782 static void
9783 add_const_value_attribute (dw_die_ref die, rtx rtl)
9785 switch (GET_CODE (rtl))
9787 case CONST_INT:
9789 HOST_WIDE_INT val = INTVAL (rtl);
9791 if (val < 0)
9792 add_AT_int (die, DW_AT_const_value, val);
9793 else
9794 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
9796 break;
9798 case CONST_DOUBLE:
9799 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
9800 floating-point constant. A CONST_DOUBLE is used whenever the
9801 constant requires more than one word in order to be adequately
9802 represented. We output CONST_DOUBLEs as blocks. */
9804 enum machine_mode mode = GET_MODE (rtl);
9806 if (SCALAR_FLOAT_MODE_P (mode))
9808 unsigned int length = GET_MODE_SIZE (mode);
9809 unsigned char *array = ggc_alloc (length);
9811 insert_float (rtl, array);
9812 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
9814 else
9816 /* ??? We really should be using HOST_WIDE_INT throughout. */
9817 gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
9819 add_AT_long_long (die, DW_AT_const_value,
9820 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
9823 break;
9825 case CONST_VECTOR:
9827 enum machine_mode mode = GET_MODE (rtl);
9828 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
9829 unsigned int length = CONST_VECTOR_NUNITS (rtl);
9830 unsigned char *array = ggc_alloc (length * elt_size);
9831 unsigned int i;
9832 unsigned char *p;
9834 switch (GET_MODE_CLASS (mode))
9836 case MODE_VECTOR_INT:
9837 for (i = 0, p = array; i < length; i++, p += elt_size)
9839 rtx elt = CONST_VECTOR_ELT (rtl, i);
9840 HOST_WIDE_INT lo, hi;
9842 switch (GET_CODE (elt))
9844 case CONST_INT:
9845 lo = INTVAL (elt);
9846 hi = -(lo < 0);
9847 break;
9849 case CONST_DOUBLE:
9850 lo = CONST_DOUBLE_LOW (elt);
9851 hi = CONST_DOUBLE_HIGH (elt);
9852 break;
9854 default:
9855 gcc_unreachable ();
9858 if (elt_size <= sizeof (HOST_WIDE_INT))
9859 insert_int (lo, elt_size, p);
9860 else
9862 unsigned char *p0 = p;
9863 unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
9865 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
9866 if (WORDS_BIG_ENDIAN)
9868 p0 = p1;
9869 p1 = p;
9871 insert_int (lo, sizeof (HOST_WIDE_INT), p0);
9872 insert_int (hi, sizeof (HOST_WIDE_INT), p1);
9875 break;
9877 case MODE_VECTOR_FLOAT:
9878 for (i = 0, p = array; i < length; i++, p += elt_size)
9880 rtx elt = CONST_VECTOR_ELT (rtl, i);
9881 insert_float (elt, p);
9883 break;
9885 default:
9886 gcc_unreachable ();
9889 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
9891 break;
9893 case CONST_STRING:
9894 add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
9895 break;
9897 case SYMBOL_REF:
9898 case LABEL_REF:
9899 case CONST:
9900 add_AT_addr (die, DW_AT_const_value, rtl);
9901 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
9902 break;
9904 case PLUS:
9905 /* In cases where an inlined instance of an inline function is passed
9906 the address of an `auto' variable (which is local to the caller) we
9907 can get a situation where the DECL_RTL of the artificial local
9908 variable (for the inlining) which acts as a stand-in for the
9909 corresponding formal parameter (of the inline function) will look
9910 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
9911 exactly a compile-time constant expression, but it isn't the address
9912 of the (artificial) local variable either. Rather, it represents the
9913 *value* which the artificial local variable always has during its
9914 lifetime. We currently have no way to represent such quasi-constant
9915 values in Dwarf, so for now we just punt and generate nothing. */
9916 break;
9918 default:
9919 /* No other kinds of rtx should be possible here. */
9920 gcc_unreachable ();
9925 /* Determine whether the evaluation of EXPR references any variables
9926 or functions which aren't otherwise used (and therefore may not be
9927 output). */
9928 static tree
9929 reference_to_unused (tree * tp, int * walk_subtrees,
9930 void * data ATTRIBUTE_UNUSED)
9932 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
9933 *walk_subtrees = 0;
9935 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
9936 && ! TREE_ASM_WRITTEN (*tp))
9937 return *tp;
9938 else
9939 return NULL_TREE;
9942 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
9943 for use in a later add_const_value_attribute call. */
9945 static rtx
9946 rtl_for_decl_init (tree init, tree type)
9948 rtx rtl = NULL_RTX;
9950 /* If a variable is initialized with a string constant without embedded
9951 zeros, build CONST_STRING. */
9952 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
9954 tree enttype = TREE_TYPE (type);
9955 tree domain = TYPE_DOMAIN (type);
9956 enum machine_mode mode = TYPE_MODE (enttype);
9958 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
9959 && domain
9960 && integer_zerop (TYPE_MIN_VALUE (domain))
9961 && compare_tree_int (TYPE_MAX_VALUE (domain),
9962 TREE_STRING_LENGTH (init) - 1) == 0
9963 && ((size_t) TREE_STRING_LENGTH (init)
9964 == strlen (TREE_STRING_POINTER (init)) + 1))
9965 rtl = gen_rtx_CONST_STRING (VOIDmode,
9966 ggc_strdup (TREE_STRING_POINTER (init)));
9968 /* Although DWARF could easily handle other kinds of aggregates, we
9969 have no way to represent such values as RTL constants, so skip
9970 those. */
9971 else if (AGGREGATE_TYPE_P (type))
9973 /* If the initializer is something that we know will expand into an
9974 immediate RTL constant, expand it now. We must be careful not to
9975 reference variables which won't be output. */
9976 else if (initializer_constant_valid_p (init, type)
9977 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
9979 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
9981 /* If expand_expr returns a MEM, it wasn't immediate. */
9982 gcc_assert (!rtl || !MEM_P (rtl));
9985 return rtl;
9988 /* Generate RTL for the variable DECL to represent its location. */
9990 static rtx
9991 rtl_for_decl_location (tree decl)
9993 rtx rtl;
9995 /* Here we have to decide where we are going to say the parameter "lives"
9996 (as far as the debugger is concerned). We only have a couple of
9997 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
9999 DECL_RTL normally indicates where the parameter lives during most of the
10000 activation of the function. If optimization is enabled however, this
10001 could be either NULL or else a pseudo-reg. Both of those cases indicate
10002 that the parameter doesn't really live anywhere (as far as the code
10003 generation parts of GCC are concerned) during most of the function's
10004 activation. That will happen (for example) if the parameter is never
10005 referenced within the function.
10007 We could just generate a location descriptor here for all non-NULL
10008 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
10009 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
10010 where DECL_RTL is NULL or is a pseudo-reg.
10012 Note however that we can only get away with using DECL_INCOMING_RTL as
10013 a backup substitute for DECL_RTL in certain limited cases. In cases
10014 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
10015 we can be sure that the parameter was passed using the same type as it is
10016 declared to have within the function, and that its DECL_INCOMING_RTL
10017 points us to a place where a value of that type is passed.
10019 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
10020 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
10021 because in these cases DECL_INCOMING_RTL points us to a value of some
10022 type which is *different* from the type of the parameter itself. Thus,
10023 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
10024 such cases, the debugger would end up (for example) trying to fetch a
10025 `float' from a place which actually contains the first part of a
10026 `double'. That would lead to really incorrect and confusing
10027 output at debug-time.
10029 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
10030 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
10031 are a couple of exceptions however. On little-endian machines we can
10032 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
10033 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
10034 an integral type that is smaller than TREE_TYPE (decl). These cases arise
10035 when (on a little-endian machine) a non-prototyped function has a
10036 parameter declared to be of type `short' or `char'. In such cases,
10037 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
10038 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
10039 passed `int' value. If the debugger then uses that address to fetch
10040 a `short' or a `char' (on a little-endian machine) the result will be
10041 the correct data, so we allow for such exceptional cases below.
10043 Note that our goal here is to describe the place where the given formal
10044 parameter lives during most of the function's activation (i.e. between the
10045 end of the prologue and the start of the epilogue). We'll do that as best
10046 as we can. Note however that if the given formal parameter is modified
10047 sometime during the execution of the function, then a stack backtrace (at
10048 debug-time) will show the function as having been called with the *new*
10049 value rather than the value which was originally passed in. This happens
10050 rarely enough that it is not a major problem, but it *is* a problem, and
10051 I'd like to fix it.
10053 A future version of dwarf2out.c may generate two additional attributes for
10054 any given DW_TAG_formal_parameter DIE which will describe the "passed
10055 type" and the "passed location" for the given formal parameter in addition
10056 to the attributes we now generate to indicate the "declared type" and the
10057 "active location" for each parameter. This additional set of attributes
10058 could be used by debuggers for stack backtraces. Separately, note that
10059 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
10060 This happens (for example) for inlined-instances of inline function formal
10061 parameters which are never referenced. This really shouldn't be
10062 happening. All PARM_DECL nodes should get valid non-NULL
10063 DECL_INCOMING_RTL values. FIXME. */
10065 /* Use DECL_RTL as the "location" unless we find something better. */
10066 rtl = DECL_RTL_IF_SET (decl);
10068 /* When generating abstract instances, ignore everything except
10069 constants, symbols living in memory, and symbols living in
10070 fixed registers. */
10071 if (! reload_completed)
10073 if (rtl
10074 && (CONSTANT_P (rtl)
10075 || (MEM_P (rtl)
10076 && CONSTANT_P (XEXP (rtl, 0)))
10077 || (REG_P (rtl)
10078 && TREE_CODE (decl) == VAR_DECL
10079 && TREE_STATIC (decl))))
10081 rtl = targetm.delegitimize_address (rtl);
10082 return rtl;
10084 rtl = NULL_RTX;
10086 else if (TREE_CODE (decl) == PARM_DECL)
10088 if (rtl == NULL_RTX || is_pseudo_reg (rtl))
10090 tree declared_type = TREE_TYPE (decl);
10091 tree passed_type = DECL_ARG_TYPE (decl);
10092 enum machine_mode dmode = TYPE_MODE (declared_type);
10093 enum machine_mode pmode = TYPE_MODE (passed_type);
10095 /* This decl represents a formal parameter which was optimized out.
10096 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
10097 all cases where (rtl == NULL_RTX) just below. */
10098 if (dmode == pmode)
10099 rtl = DECL_INCOMING_RTL (decl);
10100 else if (SCALAR_INT_MODE_P (dmode)
10101 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
10102 && DECL_INCOMING_RTL (decl))
10104 rtx inc = DECL_INCOMING_RTL (decl);
10105 if (REG_P (inc))
10106 rtl = inc;
10107 else if (MEM_P (inc))
10109 if (BYTES_BIG_ENDIAN)
10110 rtl = adjust_address_nv (inc, dmode,
10111 GET_MODE_SIZE (pmode)
10112 - GET_MODE_SIZE (dmode));
10113 else
10114 rtl = inc;
10119 /* If the parm was passed in registers, but lives on the stack, then
10120 make a big endian correction if the mode of the type of the
10121 parameter is not the same as the mode of the rtl. */
10122 /* ??? This is the same series of checks that are made in dbxout.c before
10123 we reach the big endian correction code there. It isn't clear if all
10124 of these checks are necessary here, but keeping them all is the safe
10125 thing to do. */
10126 else if (MEM_P (rtl)
10127 && XEXP (rtl, 0) != const0_rtx
10128 && ! CONSTANT_P (XEXP (rtl, 0))
10129 /* Not passed in memory. */
10130 && !MEM_P (DECL_INCOMING_RTL (decl))
10131 /* Not passed by invisible reference. */
10132 && (!REG_P (XEXP (rtl, 0))
10133 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
10134 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
10135 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
10136 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
10137 #endif
10139 /* Big endian correction check. */
10140 && BYTES_BIG_ENDIAN
10141 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
10142 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
10143 < UNITS_PER_WORD))
10145 int offset = (UNITS_PER_WORD
10146 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
10148 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10149 plus_constant (XEXP (rtl, 0), offset));
10152 else if (TREE_CODE (decl) == VAR_DECL
10153 && rtl
10154 && MEM_P (rtl)
10155 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
10156 && BYTES_BIG_ENDIAN)
10158 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
10159 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
10161 /* If a variable is declared "register" yet is smaller than
10162 a register, then if we store the variable to memory, it
10163 looks like we're storing a register-sized value, when in
10164 fact we are not. We need to adjust the offset of the
10165 storage location to reflect the actual value's bytes,
10166 else gdb will not be able to display it. */
10167 if (rsize > dsize)
10168 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10169 plus_constant (XEXP (rtl, 0), rsize-dsize));
10172 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
10173 and will have been substituted directly into all expressions that use it.
10174 C does not have such a concept, but C++ and other languages do. */
10175 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
10176 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
10178 if (rtl)
10179 rtl = targetm.delegitimize_address (rtl);
10181 /* If we don't look past the constant pool, we risk emitting a
10182 reference to a constant pool entry that isn't referenced from
10183 code, and thus is not emitted. */
10184 if (rtl)
10185 rtl = avoid_constant_pool_reference (rtl);
10187 return rtl;
10190 /* We need to figure out what section we should use as the base for the
10191 address ranges where a given location is valid.
10192 1. If this particular DECL has a section associated with it, use that.
10193 2. If this function has a section associated with it, use that.
10194 3. Otherwise, use the text section.
10195 XXX: If you split a variable across multiple sections, we won't notice. */
10197 static const char *
10198 secname_for_decl (tree decl)
10200 const char *secname;
10202 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
10204 tree sectree = DECL_SECTION_NAME (decl);
10205 secname = TREE_STRING_POINTER (sectree);
10207 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
10209 tree sectree = DECL_SECTION_NAME (current_function_decl);
10210 secname = TREE_STRING_POINTER (sectree);
10212 else if (cfun && in_cold_section_p)
10213 secname = cfun->cold_section_label;
10214 else
10215 secname = text_section_label;
10217 return secname;
10220 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
10221 data attribute for a variable or a parameter. We generate the
10222 DW_AT_const_value attribute only in those cases where the given variable
10223 or parameter does not have a true "location" either in memory or in a
10224 register. This can happen (for example) when a constant is passed as an
10225 actual argument in a call to an inline function. (It's possible that
10226 these things can crop up in other ways also.) Note that one type of
10227 constant value which can be passed into an inlined function is a constant
10228 pointer. This can happen for example if an actual argument in an inlined
10229 function call evaluates to a compile-time constant address. */
10231 static void
10232 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
10233 enum dwarf_attribute attr)
10235 rtx rtl;
10236 dw_loc_descr_ref descr;
10237 var_loc_list *loc_list;
10238 struct var_loc_node *node;
10239 if (TREE_CODE (decl) == ERROR_MARK)
10240 return;
10242 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
10243 || TREE_CODE (decl) == RESULT_DECL);
10245 /* See if we possibly have multiple locations for this variable. */
10246 loc_list = lookup_decl_loc (decl);
10248 /* If it truly has multiple locations, the first and last node will
10249 differ. */
10250 if (loc_list && loc_list->first != loc_list->last)
10252 const char *endname, *secname;
10253 dw_loc_list_ref list;
10254 rtx varloc;
10256 /* Now that we know what section we are using for a base,
10257 actually construct the list of locations.
10258 The first location information is what is passed to the
10259 function that creates the location list, and the remaining
10260 locations just get added on to that list.
10261 Note that we only know the start address for a location
10262 (IE location changes), so to build the range, we use
10263 the range [current location start, next location start].
10264 This means we have to special case the last node, and generate
10265 a range of [last location start, end of function label]. */
10267 node = loc_list->first;
10268 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10269 secname = secname_for_decl (decl);
10271 list = new_loc_list (loc_descriptor (varloc),
10272 node->label, node->next->label, secname, 1);
10273 node = node->next;
10275 for (; node->next; node = node->next)
10276 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10278 /* The variable has a location between NODE->LABEL and
10279 NODE->NEXT->LABEL. */
10280 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10281 add_loc_descr_to_loc_list (&list, loc_descriptor (varloc),
10282 node->label, node->next->label, secname);
10285 /* If the variable has a location at the last label
10286 it keeps its location until the end of function. */
10287 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10289 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
10291 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10292 if (!current_function_decl)
10293 endname = text_end_label;
10294 else
10296 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
10297 current_function_funcdef_no);
10298 endname = ggc_strdup (label_id);
10300 add_loc_descr_to_loc_list (&list, loc_descriptor (varloc),
10301 node->label, endname, secname);
10304 /* Finally, add the location list to the DIE, and we are done. */
10305 add_AT_loc_list (die, attr, list);
10306 return;
10309 /* Try to get some constant RTL for this decl, and use that as the value of
10310 the location. */
10312 rtl = rtl_for_decl_location (decl);
10313 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
10315 add_const_value_attribute (die, rtl);
10316 return;
10319 /* If we have tried to generate the location otherwise, and it
10320 didn't work out (we wouldn't be here if we did), and we have a one entry
10321 location list, try generating a location from that. */
10322 if (loc_list && loc_list->first)
10324 node = loc_list->first;
10325 descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note));
10326 if (descr)
10328 add_AT_location_description (die, attr, descr);
10329 return;
10333 /* We couldn't get any rtl, so try directly generating the location
10334 description from the tree. */
10335 descr = loc_descriptor_from_tree (decl);
10336 if (descr)
10338 add_AT_location_description (die, attr, descr);
10339 return;
10341 /* None of that worked, so it must not really have a location;
10342 try adding a constant value attribute from the DECL_INITIAL. */
10343 tree_add_const_value_attribute (die, decl);
10346 /* If we don't have a copy of this variable in memory for some reason (such
10347 as a C++ member constant that doesn't have an out-of-line definition),
10348 we should tell the debugger about the constant value. */
10350 static void
10351 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
10353 tree init = DECL_INITIAL (decl);
10354 tree type = TREE_TYPE (decl);
10355 rtx rtl;
10357 if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
10358 /* OK */;
10359 else
10360 return;
10362 rtl = rtl_for_decl_init (init, type);
10363 if (rtl)
10364 add_const_value_attribute (var_die, rtl);
10367 /* Convert the CFI instructions for the current function into a
10368 location list. This is used for DW_AT_frame_base when we targeting
10369 a dwarf2 consumer that does not support the dwarf3
10370 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
10371 expressions. */
10373 static dw_loc_list_ref
10374 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
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_advance_loc1:
10407 case DW_CFA_advance_loc2:
10408 case DW_CFA_advance_loc4:
10409 if (!cfa_equal_p (&last_cfa, &next_cfa))
10411 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
10412 start_label, last_label, section,
10413 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, offset),
10438 start_label, last_label, section,
10439 list == NULL);
10440 list_tail = &(*list_tail)->dw_loc_next;
10441 start_label = last_label;
10443 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
10444 start_label, fde->dw_fde_end, section,
10445 list == NULL);
10447 return list;
10450 /* Compute a displacement from the "steady-state frame pointer" to the
10451 frame base (often the same as the CFA), and store it in
10452 frame_pointer_fb_offset. OFFSET is added to the displacement
10453 before the latter is negated. */
10455 static void
10456 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
10458 rtx reg, elim;
10460 #ifdef FRAME_POINTER_CFA_OFFSET
10461 reg = frame_pointer_rtx;
10462 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
10463 #else
10464 reg = arg_pointer_rtx;
10465 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
10466 #endif
10468 elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10469 if (GET_CODE (elim) == PLUS)
10471 offset += INTVAL (XEXP (elim, 1));
10472 elim = XEXP (elim, 0);
10474 gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
10475 : stack_pointer_rtx));
10477 frame_pointer_fb_offset = -offset;
10480 /* Generate a DW_AT_name attribute given some string value to be included as
10481 the value of the attribute. */
10483 static void
10484 add_name_attribute (dw_die_ref die, const char *name_string)
10486 if (name_string != NULL && *name_string != 0)
10488 if (demangle_name_func)
10489 name_string = (*demangle_name_func) (name_string);
10491 add_AT_string (die, DW_AT_name, name_string);
10495 /* Generate a DW_AT_comp_dir attribute for DIE. */
10497 static void
10498 add_comp_dir_attribute (dw_die_ref die)
10500 const char *wd = get_src_pwd ();
10501 if (wd != NULL)
10502 add_AT_string (die, DW_AT_comp_dir, wd);
10505 /* Given a tree node describing an array bound (either lower or upper) output
10506 a representation for that bound. */
10508 static void
10509 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
10511 switch (TREE_CODE (bound))
10513 case ERROR_MARK:
10514 return;
10516 /* All fixed-bounds are represented by INTEGER_CST nodes. */
10517 case INTEGER_CST:
10518 if (! host_integerp (bound, 0)
10519 || (bound_attr == DW_AT_lower_bound
10520 && (((is_c_family () || is_java ()) && integer_zerop (bound))
10521 || (is_fortran () && integer_onep (bound)))))
10522 /* Use the default. */
10524 else
10525 add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
10526 break;
10528 case CONVERT_EXPR:
10529 case NOP_EXPR:
10530 case NON_LVALUE_EXPR:
10531 case VIEW_CONVERT_EXPR:
10532 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
10533 break;
10535 case SAVE_EXPR:
10536 break;
10538 case VAR_DECL:
10539 case PARM_DECL:
10540 case RESULT_DECL:
10542 dw_die_ref decl_die = lookup_decl_die (bound);
10544 /* ??? Can this happen, or should the variable have been bound
10545 first? Probably it can, since I imagine that we try to create
10546 the types of parameters in the order in which they exist in
10547 the list, and won't have created a forward reference to a
10548 later parameter. */
10549 if (decl_die != NULL)
10550 add_AT_die_ref (subrange_die, bound_attr, decl_die);
10551 break;
10554 default:
10556 /* Otherwise try to create a stack operation procedure to
10557 evaluate the value of the array bound. */
10559 dw_die_ref ctx, decl_die;
10560 dw_loc_descr_ref loc;
10562 loc = loc_descriptor_from_tree (bound);
10563 if (loc == NULL)
10564 break;
10566 if (current_function_decl == 0)
10567 ctx = comp_unit_die;
10568 else
10569 ctx = lookup_decl_die (current_function_decl);
10571 decl_die = new_die (DW_TAG_variable, ctx, bound);
10572 add_AT_flag (decl_die, DW_AT_artificial, 1);
10573 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
10574 add_AT_loc (decl_die, DW_AT_location, loc);
10576 add_AT_die_ref (subrange_die, bound_attr, decl_die);
10577 break;
10582 /* Note that the block of subscript information for an array type also
10583 includes information about the element type of type given array type. */
10585 static void
10586 add_subscript_info (dw_die_ref type_die, tree type)
10588 #ifndef MIPS_DEBUGGING_INFO
10589 unsigned dimension_number;
10590 #endif
10591 tree lower, upper;
10592 dw_die_ref subrange_die;
10594 /* The GNU compilers represent multidimensional array types as sequences of
10595 one dimensional array types whose element types are themselves array
10596 types. Here we squish that down, so that each multidimensional array
10597 type gets only one array_type DIE in the Dwarf debugging info. The draft
10598 Dwarf specification say that we are allowed to do this kind of
10599 compression in C (because there is no difference between an array or
10600 arrays and a multidimensional array in C) but for other source languages
10601 (e.g. Ada) we probably shouldn't do this. */
10603 /* ??? The SGI dwarf reader fails for multidimensional arrays with a
10604 const enum type. E.g. const enum machine_mode insn_operand_mode[2][10].
10605 We work around this by disabling this feature. See also
10606 gen_array_type_die. */
10607 #ifndef MIPS_DEBUGGING_INFO
10608 for (dimension_number = 0;
10609 TREE_CODE (type) == ARRAY_TYPE;
10610 type = TREE_TYPE (type), dimension_number++)
10611 #endif
10613 tree domain = TYPE_DOMAIN (type);
10615 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
10616 and (in GNU C only) variable bounds. Handle all three forms
10617 here. */
10618 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
10619 if (domain)
10621 /* We have an array type with specified bounds. */
10622 lower = TYPE_MIN_VALUE (domain);
10623 upper = TYPE_MAX_VALUE (domain);
10625 /* Define the index type. */
10626 if (TREE_TYPE (domain))
10628 /* ??? This is probably an Ada unnamed subrange type. Ignore the
10629 TREE_TYPE field. We can't emit debug info for this
10630 because it is an unnamed integral type. */
10631 if (TREE_CODE (domain) == INTEGER_TYPE
10632 && TYPE_NAME (domain) == NULL_TREE
10633 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
10634 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
10636 else
10637 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
10638 type_die);
10641 /* ??? If upper is NULL, the array has unspecified length,
10642 but it does have a lower bound. This happens with Fortran
10643 dimension arr(N:*)
10644 Since the debugger is definitely going to need to know N
10645 to produce useful results, go ahead and output the lower
10646 bound solo, and hope the debugger can cope. */
10648 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
10649 if (upper)
10650 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
10653 /* Otherwise we have an array type with an unspecified length. The
10654 DWARF-2 spec does not say how to handle this; let's just leave out the
10655 bounds. */
10659 static void
10660 add_byte_size_attribute (dw_die_ref die, tree tree_node)
10662 unsigned size;
10664 switch (TREE_CODE (tree_node))
10666 case ERROR_MARK:
10667 size = 0;
10668 break;
10669 case ENUMERAL_TYPE:
10670 case RECORD_TYPE:
10671 case UNION_TYPE:
10672 case QUAL_UNION_TYPE:
10673 size = int_size_in_bytes (tree_node);
10674 break;
10675 case FIELD_DECL:
10676 /* For a data member of a struct or union, the DW_AT_byte_size is
10677 generally given as the number of bytes normally allocated for an
10678 object of the *declared* type of the member itself. This is true
10679 even for bit-fields. */
10680 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
10681 break;
10682 default:
10683 gcc_unreachable ();
10686 /* Note that `size' might be -1 when we get to this point. If it is, that
10687 indicates that the byte size of the entity in question is variable. We
10688 have no good way of expressing this fact in Dwarf at the present time,
10689 so just let the -1 pass on through. */
10690 add_AT_unsigned (die, DW_AT_byte_size, size);
10693 /* For a FIELD_DECL node which represents a bit-field, output an attribute
10694 which specifies the distance in bits from the highest order bit of the
10695 "containing object" for the bit-field to the highest order bit of the
10696 bit-field itself.
10698 For any given bit-field, the "containing object" is a hypothetical object
10699 (of some integral or enum type) within which the given bit-field lives. The
10700 type of this hypothetical "containing object" is always the same as the
10701 declared type of the individual bit-field itself. The determination of the
10702 exact location of the "containing object" for a bit-field is rather
10703 complicated. It's handled by the `field_byte_offset' function (above).
10705 Note that it is the size (in bytes) of the hypothetical "containing object"
10706 which will be given in the DW_AT_byte_size attribute for this bit-field.
10707 (See `byte_size_attribute' above). */
10709 static inline void
10710 add_bit_offset_attribute (dw_die_ref die, tree decl)
10712 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
10713 tree type = DECL_BIT_FIELD_TYPE (decl);
10714 HOST_WIDE_INT bitpos_int;
10715 HOST_WIDE_INT highest_order_object_bit_offset;
10716 HOST_WIDE_INT highest_order_field_bit_offset;
10717 HOST_WIDE_INT unsigned bit_offset;
10719 /* Must be a field and a bit field. */
10720 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
10722 /* We can't yet handle bit-fields whose offsets are variable, so if we
10723 encounter such things, just return without generating any attribute
10724 whatsoever. Likewise for variable or too large size. */
10725 if (! host_integerp (bit_position (decl), 0)
10726 || ! host_integerp (DECL_SIZE (decl), 1))
10727 return;
10729 bitpos_int = int_bit_position (decl);
10731 /* Note that the bit offset is always the distance (in bits) from the
10732 highest-order bit of the "containing object" to the highest-order bit of
10733 the bit-field itself. Since the "high-order end" of any object or field
10734 is different on big-endian and little-endian machines, the computation
10735 below must take account of these differences. */
10736 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
10737 highest_order_field_bit_offset = bitpos_int;
10739 if (! BYTES_BIG_ENDIAN)
10741 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
10742 highest_order_object_bit_offset += simple_type_size_in_bits (type);
10745 bit_offset
10746 = (! BYTES_BIG_ENDIAN
10747 ? highest_order_object_bit_offset - highest_order_field_bit_offset
10748 : highest_order_field_bit_offset - highest_order_object_bit_offset);
10750 add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
10753 /* For a FIELD_DECL node which represents a bit field, output an attribute
10754 which specifies the length in bits of the given field. */
10756 static inline void
10757 add_bit_size_attribute (dw_die_ref die, tree decl)
10759 /* Must be a field and a bit field. */
10760 gcc_assert (TREE_CODE (decl) == FIELD_DECL
10761 && DECL_BIT_FIELD_TYPE (decl));
10763 if (host_integerp (DECL_SIZE (decl), 1))
10764 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
10767 /* If the compiled language is ANSI C, then add a 'prototyped'
10768 attribute, if arg types are given for the parameters of a function. */
10770 static inline void
10771 add_prototyped_attribute (dw_die_ref die, tree func_type)
10773 if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
10774 && TYPE_ARG_TYPES (func_type) != NULL)
10775 add_AT_flag (die, DW_AT_prototyped, 1);
10778 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
10779 by looking in either the type declaration or object declaration
10780 equate table. */
10782 static inline void
10783 add_abstract_origin_attribute (dw_die_ref die, tree origin)
10785 dw_die_ref origin_die = NULL;
10787 if (TREE_CODE (origin) != FUNCTION_DECL)
10789 /* We may have gotten separated from the block for the inlined
10790 function, if we're in an exception handler or some such; make
10791 sure that the abstract function has been written out.
10793 Doing this for nested functions is wrong, however; functions are
10794 distinct units, and our context might not even be inline. */
10795 tree fn = origin;
10797 if (TYPE_P (fn))
10798 fn = TYPE_STUB_DECL (fn);
10800 fn = decl_function_context (fn);
10801 if (fn)
10802 dwarf2out_abstract_function (fn);
10805 if (DECL_P (origin))
10806 origin_die = lookup_decl_die (origin);
10807 else if (TYPE_P (origin))
10808 origin_die = lookup_type_die (origin);
10810 /* XXX: Functions that are never lowered don't always have correct block
10811 trees (in the case of java, they simply have no block tree, in some other
10812 languages). For these functions, there is nothing we can really do to
10813 output correct debug info for inlined functions in all cases. Rather
10814 than die, we'll just produce deficient debug info now, in that we will
10815 have variables without a proper abstract origin. In the future, when all
10816 functions are lowered, we should re-add a gcc_assert (origin_die)
10817 here. */
10819 if (origin_die)
10820 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
10823 /* We do not currently support the pure_virtual attribute. */
10825 static inline void
10826 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
10828 if (DECL_VINDEX (func_decl))
10830 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
10832 if (host_integerp (DECL_VINDEX (func_decl), 0))
10833 add_AT_loc (die, DW_AT_vtable_elem_location,
10834 new_loc_descr (DW_OP_constu,
10835 tree_low_cst (DECL_VINDEX (func_decl), 0),
10836 0));
10838 /* GNU extension: Record what type this method came from originally. */
10839 if (debug_info_level > DINFO_LEVEL_TERSE)
10840 add_AT_die_ref (die, DW_AT_containing_type,
10841 lookup_type_die (DECL_CONTEXT (func_decl)));
10845 /* Add source coordinate attributes for the given decl. */
10847 static void
10848 add_src_coords_attributes (dw_die_ref die, tree decl)
10850 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
10851 unsigned file_index = lookup_filename (s.file);
10853 add_AT_unsigned (die, DW_AT_decl_file, file_index);
10854 add_AT_unsigned (die, DW_AT_decl_line, s.line);
10857 /* Add a DW_AT_name attribute and source coordinate attribute for the
10858 given decl, but only if it actually has a name. */
10860 static void
10861 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
10863 tree decl_name;
10865 decl_name = DECL_NAME (decl);
10866 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
10868 add_name_attribute (die, dwarf2_name (decl, 0));
10869 if (! DECL_ARTIFICIAL (decl))
10870 add_src_coords_attributes (die, decl);
10872 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
10873 && TREE_PUBLIC (decl)
10874 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
10875 && !DECL_ABSTRACT (decl)
10876 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl)))
10877 add_AT_string (die, DW_AT_MIPS_linkage_name,
10878 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
10881 #ifdef VMS_DEBUGGING_INFO
10882 /* Get the function's name, as described by its RTL. This may be different
10883 from the DECL_NAME name used in the source file. */
10884 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
10886 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
10887 XEXP (DECL_RTL (decl), 0));
10888 VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
10890 #endif
10893 /* Push a new declaration scope. */
10895 static void
10896 push_decl_scope (tree scope)
10898 VEC_safe_push (tree, gc, decl_scope_table, scope);
10901 /* Pop a declaration scope. */
10903 static inline void
10904 pop_decl_scope (void)
10906 VEC_pop (tree, decl_scope_table);
10909 /* Return the DIE for the scope that immediately contains this type.
10910 Non-named types get global scope. Named types nested in other
10911 types get their containing scope if it's open, or global scope
10912 otherwise. All other types (i.e. function-local named types) get
10913 the current active scope. */
10915 static dw_die_ref
10916 scope_die_for (tree t, dw_die_ref context_die)
10918 dw_die_ref scope_die = NULL;
10919 tree containing_scope;
10920 int i;
10922 /* Non-types always go in the current scope. */
10923 gcc_assert (TYPE_P (t));
10925 containing_scope = TYPE_CONTEXT (t);
10927 /* Use the containing namespace if it was passed in (for a declaration). */
10928 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
10930 if (context_die == lookup_decl_die (containing_scope))
10931 /* OK */;
10932 else
10933 containing_scope = NULL_TREE;
10936 /* Ignore function type "scopes" from the C frontend. They mean that
10937 a tagged type is local to a parmlist of a function declarator, but
10938 that isn't useful to DWARF. */
10939 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
10940 containing_scope = NULL_TREE;
10942 if (containing_scope == NULL_TREE)
10943 scope_die = comp_unit_die;
10944 else if (TYPE_P (containing_scope))
10946 /* For types, we can just look up the appropriate DIE. But
10947 first we check to see if we're in the middle of emitting it
10948 so we know where the new DIE should go. */
10949 for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
10950 if (VEC_index (tree, decl_scope_table, i) == containing_scope)
10951 break;
10953 if (i < 0)
10955 gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
10956 || TREE_ASM_WRITTEN (containing_scope));
10958 /* If none of the current dies are suitable, we get file scope. */
10959 scope_die = comp_unit_die;
10961 else
10962 scope_die = lookup_type_die (containing_scope);
10964 else
10965 scope_die = context_die;
10967 return scope_die;
10970 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
10972 static inline int
10973 local_scope_p (dw_die_ref context_die)
10975 for (; context_die; context_die = context_die->die_parent)
10976 if (context_die->die_tag == DW_TAG_inlined_subroutine
10977 || context_die->die_tag == DW_TAG_subprogram)
10978 return 1;
10980 return 0;
10983 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
10984 whether or not to treat a DIE in this context as a declaration. */
10986 static inline int
10987 class_or_namespace_scope_p (dw_die_ref context_die)
10989 return (context_die
10990 && (context_die->die_tag == DW_TAG_structure_type
10991 || context_die->die_tag == DW_TAG_union_type
10992 || context_die->die_tag == DW_TAG_namespace));
10995 /* Many forms of DIEs require a "type description" attribute. This
10996 routine locates the proper "type descriptor" die for the type given
10997 by 'type', and adds a DW_AT_type attribute below the given die. */
10999 static void
11000 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
11001 int decl_volatile, dw_die_ref context_die)
11003 enum tree_code code = TREE_CODE (type);
11004 dw_die_ref type_die = NULL;
11006 /* ??? If this type is an unnamed subrange type of an integral or
11007 floating-point type, use the inner type. This is because we have no
11008 support for unnamed types in base_type_die. This can happen if this is
11009 an Ada subrange type. Correct solution is emit a subrange type die. */
11010 if ((code == INTEGER_TYPE || code == REAL_TYPE)
11011 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
11012 type = TREE_TYPE (type), code = TREE_CODE (type);
11014 if (code == ERROR_MARK
11015 /* Handle a special case. For functions whose return type is void, we
11016 generate *no* type attribute. (Note that no object may have type
11017 `void', so this only applies to function return types). */
11018 || code == VOID_TYPE)
11019 return;
11021 type_die = modified_type_die (type,
11022 decl_const || TYPE_READONLY (type),
11023 decl_volatile || TYPE_VOLATILE (type),
11024 context_die);
11026 if (type_die != NULL)
11027 add_AT_die_ref (object_die, DW_AT_type, type_die);
11030 /* Given an object die, add the calling convention attribute for the
11031 function call type. */
11032 static void
11033 add_calling_convention_attribute (dw_die_ref subr_die, tree type)
11035 enum dwarf_calling_convention value = DW_CC_normal;
11037 value = targetm.dwarf_calling_convention (type);
11039 /* Only add the attribute if the backend requests it, and
11040 is not DW_CC_normal. */
11041 if (value && (value != DW_CC_normal))
11042 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
11045 /* Given a tree pointer to a struct, class, union, or enum type node, return
11046 a pointer to the (string) tag name for the given type, or zero if the type
11047 was declared without a tag. */
11049 static const char *
11050 type_tag (tree type)
11052 const char *name = 0;
11054 if (TYPE_NAME (type) != 0)
11056 tree t = 0;
11058 /* Find the IDENTIFIER_NODE for the type name. */
11059 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
11060 t = TYPE_NAME (type);
11062 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
11063 a TYPE_DECL node, regardless of whether or not a `typedef' was
11064 involved. */
11065 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11066 && ! DECL_IGNORED_P (TYPE_NAME (type)))
11067 t = DECL_NAME (TYPE_NAME (type));
11069 /* Now get the name as a string, or invent one. */
11070 if (t != 0)
11071 name = IDENTIFIER_POINTER (t);
11074 return (name == 0 || *name == '\0') ? 0 : name;
11077 /* Return the type associated with a data member, make a special check
11078 for bit field types. */
11080 static inline tree
11081 member_declared_type (tree member)
11083 return (DECL_BIT_FIELD_TYPE (member)
11084 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
11087 /* Get the decl's label, as described by its RTL. This may be different
11088 from the DECL_NAME name used in the source file. */
11090 #if 0
11091 static const char *
11092 decl_start_label (tree decl)
11094 rtx x;
11095 const char *fnname;
11097 x = DECL_RTL (decl);
11098 gcc_assert (MEM_P (x));
11100 x = XEXP (x, 0);
11101 gcc_assert (GET_CODE (x) == SYMBOL_REF);
11103 fnname = XSTR (x, 0);
11104 return fnname;
11106 #endif
11108 /* These routines generate the internal representation of the DIE's for
11109 the compilation unit. Debugging information is collected by walking
11110 the declaration trees passed in from dwarf2out_decl(). */
11112 static void
11113 gen_array_type_die (tree type, dw_die_ref context_die)
11115 dw_die_ref scope_die = scope_die_for (type, context_die);
11116 dw_die_ref array_die;
11117 tree element_type;
11119 /* ??? The SGI dwarf reader fails for array of array of enum types unless
11120 the inner array type comes before the outer array type. Thus we must
11121 call gen_type_die before we call new_die. See below also. */
11122 #ifdef MIPS_DEBUGGING_INFO
11123 gen_type_die (TREE_TYPE (type), context_die);
11124 #endif
11126 array_die = new_die (DW_TAG_array_type, scope_die, type);
11127 add_name_attribute (array_die, type_tag (type));
11128 equate_type_number_to_die (type, array_die);
11130 if (TREE_CODE (type) == VECTOR_TYPE)
11132 /* The frontend feeds us a representation for the vector as a struct
11133 containing an array. Pull out the array type. */
11134 type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
11135 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
11138 #if 0
11139 /* We default the array ordering. SDB will probably do
11140 the right things even if DW_AT_ordering is not present. It's not even
11141 an issue until we start to get into multidimensional arrays anyway. If
11142 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
11143 then we'll have to put the DW_AT_ordering attribute back in. (But if
11144 and when we find out that we need to put these in, we will only do so
11145 for multidimensional arrays. */
11146 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
11147 #endif
11149 #ifdef MIPS_DEBUGGING_INFO
11150 /* The SGI compilers handle arrays of unknown bound by setting
11151 AT_declaration and not emitting any subrange DIEs. */
11152 if (! TYPE_DOMAIN (type))
11153 add_AT_flag (array_die, DW_AT_declaration, 1);
11154 else
11155 #endif
11156 add_subscript_info (array_die, type);
11158 /* Add representation of the type of the elements of this array type. */
11159 element_type = TREE_TYPE (type);
11161 /* ??? The SGI dwarf reader fails for multidimensional arrays with a
11162 const enum type. E.g. const enum machine_mode insn_operand_mode[2][10].
11163 We work around this by disabling this feature. See also
11164 add_subscript_info. */
11165 #ifndef MIPS_DEBUGGING_INFO
11166 while (TREE_CODE (element_type) == ARRAY_TYPE)
11167 element_type = TREE_TYPE (element_type);
11169 gen_type_die (element_type, context_die);
11170 #endif
11172 add_type_attribute (array_die, element_type, 0, 0, context_die);
11175 #if 0
11176 static void
11177 gen_entry_point_die (tree decl, dw_die_ref context_die)
11179 tree origin = decl_ultimate_origin (decl);
11180 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
11182 if (origin != NULL)
11183 add_abstract_origin_attribute (decl_die, origin);
11184 else
11186 add_name_and_src_coords_attributes (decl_die, decl);
11187 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
11188 0, 0, context_die);
11191 if (DECL_ABSTRACT (decl))
11192 equate_decl_number_to_die (decl, decl_die);
11193 else
11194 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
11196 #endif
11198 /* Walk through the list of incomplete types again, trying once more to
11199 emit full debugging info for them. */
11201 static void
11202 retry_incomplete_types (void)
11204 int i;
11206 for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
11207 gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
11210 /* Generate a DIE to represent an inlined instance of an enumeration type. */
11212 static void
11213 gen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
11215 dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
11217 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11218 be incomplete and such types are not marked. */
11219 add_abstract_origin_attribute (type_die, type);
11222 /* Generate a DIE to represent an inlined instance of a structure type. */
11224 static void
11225 gen_inlined_structure_type_die (tree type, dw_die_ref context_die)
11227 dw_die_ref type_die = new_die (DW_TAG_structure_type, context_die, type);
11229 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11230 be incomplete and such types are not marked. */
11231 add_abstract_origin_attribute (type_die, type);
11234 /* Generate a DIE to represent an inlined instance of a union type. */
11236 static void
11237 gen_inlined_union_type_die (tree type, dw_die_ref context_die)
11239 dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
11241 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11242 be incomplete and such types are not marked. */
11243 add_abstract_origin_attribute (type_die, type);
11246 /* Generate a DIE to represent an enumeration type. Note that these DIEs
11247 include all of the information about the enumeration values also. Each
11248 enumerated type name/value is listed as a child of the enumerated type
11249 DIE. */
11251 static dw_die_ref
11252 gen_enumeration_type_die (tree type, dw_die_ref context_die)
11254 dw_die_ref type_die = lookup_type_die (type);
11256 if (type_die == NULL)
11258 type_die = new_die (DW_TAG_enumeration_type,
11259 scope_die_for (type, context_die), type);
11260 equate_type_number_to_die (type, type_die);
11261 add_name_attribute (type_die, type_tag (type));
11263 else if (! TYPE_SIZE (type))
11264 return type_die;
11265 else
11266 remove_AT (type_die, DW_AT_declaration);
11268 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
11269 given enum type is incomplete, do not generate the DW_AT_byte_size
11270 attribute or the DW_AT_element_list attribute. */
11271 if (TYPE_SIZE (type))
11273 tree link;
11275 TREE_ASM_WRITTEN (type) = 1;
11276 add_byte_size_attribute (type_die, type);
11277 if (TYPE_STUB_DECL (type) != NULL_TREE)
11278 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
11280 /* If the first reference to this type was as the return type of an
11281 inline function, then it may not have a parent. Fix this now. */
11282 if (type_die->die_parent == NULL)
11283 add_child_die (scope_die_for (type, context_die), type_die);
11285 for (link = TYPE_VALUES (type);
11286 link != NULL; link = TREE_CHAIN (link))
11288 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
11289 tree value = TREE_VALUE (link);
11291 add_name_attribute (enum_die,
11292 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
11294 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
11295 /* DWARF2 does not provide a way of indicating whether or
11296 not enumeration constants are signed or unsigned. GDB
11297 always assumes the values are signed, so we output all
11298 values as if they were signed. That means that
11299 enumeration constants with very large unsigned values
11300 will appear to have negative values in the debugger. */
11301 add_AT_int (enum_die, DW_AT_const_value,
11302 tree_low_cst (value, tree_int_cst_sgn (value) > 0));
11305 else
11306 add_AT_flag (type_die, DW_AT_declaration, 1);
11308 return type_die;
11311 /* Generate a DIE to represent either a real live formal parameter decl or to
11312 represent just the type of some formal parameter position in some function
11313 type.
11315 Note that this routine is a bit unusual because its argument may be a
11316 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
11317 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
11318 node. If it's the former then this function is being called to output a
11319 DIE to represent a formal parameter object (or some inlining thereof). If
11320 it's the latter, then this function is only being called to output a
11321 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
11322 argument type of some subprogram type. */
11324 static dw_die_ref
11325 gen_formal_parameter_die (tree node, dw_die_ref context_die)
11327 dw_die_ref parm_die
11328 = new_die (DW_TAG_formal_parameter, context_die, node);
11329 tree origin;
11331 switch (TREE_CODE_CLASS (TREE_CODE (node)))
11333 case tcc_declaration:
11334 origin = decl_ultimate_origin (node);
11335 if (origin != NULL)
11336 add_abstract_origin_attribute (parm_die, origin);
11337 else
11339 add_name_and_src_coords_attributes (parm_die, node);
11340 add_type_attribute (parm_die, TREE_TYPE (node),
11341 TREE_READONLY (node),
11342 TREE_THIS_VOLATILE (node),
11343 context_die);
11344 if (DECL_ARTIFICIAL (node))
11345 add_AT_flag (parm_die, DW_AT_artificial, 1);
11348 equate_decl_number_to_die (node, parm_die);
11349 if (! DECL_ABSTRACT (node))
11350 add_location_or_const_value_attribute (parm_die, node, DW_AT_location);
11352 break;
11354 case tcc_type:
11355 /* We were called with some kind of a ..._TYPE node. */
11356 add_type_attribute (parm_die, node, 0, 0, context_die);
11357 break;
11359 default:
11360 gcc_unreachable ();
11363 return parm_die;
11366 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
11367 at the end of an (ANSI prototyped) formal parameters list. */
11369 static void
11370 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
11372 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
11375 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
11376 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
11377 parameters as specified in some function type specification (except for
11378 those which appear as part of a function *definition*). */
11380 static void
11381 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
11383 tree link;
11384 tree formal_type = NULL;
11385 tree first_parm_type;
11386 tree arg;
11388 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
11390 arg = DECL_ARGUMENTS (function_or_method_type);
11391 function_or_method_type = TREE_TYPE (function_or_method_type);
11393 else
11394 arg = NULL_TREE;
11396 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
11398 /* Make our first pass over the list of formal parameter types and output a
11399 DW_TAG_formal_parameter DIE for each one. */
11400 for (link = first_parm_type; link; )
11402 dw_die_ref parm_die;
11404 formal_type = TREE_VALUE (link);
11405 if (formal_type == void_type_node)
11406 break;
11408 /* Output a (nameless) DIE to represent the formal parameter itself. */
11409 parm_die = gen_formal_parameter_die (formal_type, context_die);
11410 if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
11411 && link == first_parm_type)
11412 || (arg && DECL_ARTIFICIAL (arg)))
11413 add_AT_flag (parm_die, DW_AT_artificial, 1);
11415 link = TREE_CHAIN (link);
11416 if (arg)
11417 arg = TREE_CHAIN (arg);
11420 /* If this function type has an ellipsis, add a
11421 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
11422 if (formal_type != void_type_node)
11423 gen_unspecified_parameters_die (function_or_method_type, context_die);
11425 /* Make our second (and final) pass over the list of formal parameter types
11426 and output DIEs to represent those types (as necessary). */
11427 for (link = TYPE_ARG_TYPES (function_or_method_type);
11428 link && TREE_VALUE (link);
11429 link = TREE_CHAIN (link))
11430 gen_type_die (TREE_VALUE (link), context_die);
11433 /* We want to generate the DIE for TYPE so that we can generate the
11434 die for MEMBER, which has been defined; we will need to refer back
11435 to the member declaration nested within TYPE. If we're trying to
11436 generate minimal debug info for TYPE, processing TYPE won't do the
11437 trick; we need to attach the member declaration by hand. */
11439 static void
11440 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
11442 gen_type_die (type, context_die);
11444 /* If we're trying to avoid duplicate debug info, we may not have
11445 emitted the member decl for this function. Emit it now. */
11446 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
11447 && ! lookup_decl_die (member))
11449 dw_die_ref type_die;
11450 gcc_assert (!decl_ultimate_origin (member));
11452 push_decl_scope (type);
11453 type_die = lookup_type_die (type);
11454 if (TREE_CODE (member) == FUNCTION_DECL)
11455 gen_subprogram_die (member, type_die);
11456 else if (TREE_CODE (member) == FIELD_DECL)
11458 /* Ignore the nameless fields that are used to skip bits but handle
11459 C++ anonymous unions and structs. */
11460 if (DECL_NAME (member) != NULL_TREE
11461 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
11462 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
11464 gen_type_die (member_declared_type (member), type_die);
11465 gen_field_die (member, type_die);
11468 else
11469 gen_variable_die (member, type_die);
11471 pop_decl_scope ();
11475 /* Generate the DWARF2 info for the "abstract" instance of a function which we
11476 may later generate inlined and/or out-of-line instances of. */
11478 static void
11479 dwarf2out_abstract_function (tree decl)
11481 dw_die_ref old_die;
11482 tree save_fn;
11483 tree context;
11484 int was_abstract = DECL_ABSTRACT (decl);
11486 /* Make sure we have the actual abstract inline, not a clone. */
11487 decl = DECL_ORIGIN (decl);
11489 old_die = lookup_decl_die (decl);
11490 if (old_die && get_AT (old_die, DW_AT_inline))
11491 /* We've already generated the abstract instance. */
11492 return;
11494 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
11495 we don't get confused by DECL_ABSTRACT. */
11496 if (debug_info_level > DINFO_LEVEL_TERSE)
11498 context = decl_class_context (decl);
11499 if (context)
11500 gen_type_die_for_member
11501 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
11504 /* Pretend we've just finished compiling this function. */
11505 save_fn = current_function_decl;
11506 current_function_decl = decl;
11508 set_decl_abstract_flags (decl, 1);
11509 dwarf2out_decl (decl);
11510 if (! was_abstract)
11511 set_decl_abstract_flags (decl, 0);
11513 current_function_decl = save_fn;
11516 /* Helper function of premark_used_types() which gets called through
11517 htab_traverse_resize().
11519 Marks the DIE of a given type in *SLOT as perennial, so it never gets
11520 marked as unused by prune_unused_types. */
11521 static int
11522 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
11524 tree type;
11525 dw_die_ref die;
11527 type = *slot;
11528 die = lookup_type_die (type);
11529 if (die != NULL)
11530 die->die_perennial_p = 1;
11531 return 1;
11534 /* Mark all members of used_types_hash as perennial. */
11535 static void
11536 premark_used_types (void)
11538 if (cfun && cfun->used_types_hash)
11539 htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
11542 /* Generate a DIE to represent a declared function (either file-scope or
11543 block-local). */
11545 static void
11546 gen_subprogram_die (tree decl, dw_die_ref context_die)
11548 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
11549 tree origin = decl_ultimate_origin (decl);
11550 dw_die_ref subr_die;
11551 tree fn_arg_types;
11552 tree outer_scope;
11553 dw_die_ref old_die = lookup_decl_die (decl);
11554 int declaration = (current_function_decl != decl
11555 || class_or_namespace_scope_p (context_die));
11557 premark_used_types();
11559 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
11560 started to generate the abstract instance of an inline, decided to output
11561 its containing class, and proceeded to emit the declaration of the inline
11562 from the member list for the class. If so, DECLARATION takes priority;
11563 we'll get back to the abstract instance when done with the class. */
11565 /* The class-scope declaration DIE must be the primary DIE. */
11566 if (origin && declaration && class_or_namespace_scope_p (context_die))
11568 origin = NULL;
11569 gcc_assert (!old_die);
11572 /* Now that the C++ front end lazily declares artificial member fns, we
11573 might need to retrofit the declaration into its class. */
11574 if (!declaration && !origin && !old_die
11575 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
11576 && !class_or_namespace_scope_p (context_die)
11577 && debug_info_level > DINFO_LEVEL_TERSE)
11578 old_die = force_decl_die (decl);
11580 if (origin != NULL)
11582 gcc_assert (!declaration || local_scope_p (context_die));
11584 /* Fixup die_parent for the abstract instance of a nested
11585 inline function. */
11586 if (old_die && old_die->die_parent == NULL)
11587 add_child_die (context_die, old_die);
11589 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11590 add_abstract_origin_attribute (subr_die, origin);
11592 else if (old_die)
11594 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11595 unsigned file_index = lookup_filename (s.file);
11597 if (!get_AT_flag (old_die, DW_AT_declaration)
11598 /* We can have a normal definition following an inline one in the
11599 case of redefinition of GNU C extern inlines.
11600 It seems reasonable to use AT_specification in this case. */
11601 && !get_AT (old_die, DW_AT_inline))
11603 /* Detect and ignore this case, where we are trying to output
11604 something we have already output. */
11605 return;
11608 /* If the definition comes from the same place as the declaration,
11609 maybe use the old DIE. We always want the DIE for this function
11610 that has the *_pc attributes to be under comp_unit_die so the
11611 debugger can find it. We also need to do this for abstract
11612 instances of inlines, since the spec requires the out-of-line copy
11613 to have the same parent. For local class methods, this doesn't
11614 apply; we just use the old DIE. */
11615 if ((old_die->die_parent == comp_unit_die || context_die == NULL)
11616 && (DECL_ARTIFICIAL (decl)
11617 || (get_AT_unsigned (old_die, DW_AT_decl_file) == file_index
11618 && (get_AT_unsigned (old_die, DW_AT_decl_line)
11619 == (unsigned) s.line))))
11621 subr_die = old_die;
11623 /* Clear out the declaration attribute and the formal parameters.
11624 Do not remove all children, because it is possible that this
11625 declaration die was forced using force_decl_die(). In such
11626 cases die that forced declaration die (e.g. TAG_imported_module)
11627 is one of the children that we do not want to remove. */
11628 remove_AT (subr_die, DW_AT_declaration);
11629 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
11631 else
11633 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11634 add_AT_specification (subr_die, old_die);
11635 if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
11636 add_AT_unsigned (subr_die, DW_AT_decl_file, file_index);
11637 if (get_AT_unsigned (old_die, DW_AT_decl_line)
11638 != (unsigned) s.line)
11639 add_AT_unsigned
11640 (subr_die, DW_AT_decl_line, s.line);
11643 else
11645 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11647 if (TREE_PUBLIC (decl))
11648 add_AT_flag (subr_die, DW_AT_external, 1);
11650 add_name_and_src_coords_attributes (subr_die, decl);
11651 if (debug_info_level > DINFO_LEVEL_TERSE)
11653 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
11654 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
11655 0, 0, context_die);
11658 add_pure_or_virtual_attribute (subr_die, decl);
11659 if (DECL_ARTIFICIAL (decl))
11660 add_AT_flag (subr_die, DW_AT_artificial, 1);
11662 if (TREE_PROTECTED (decl))
11663 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
11664 else if (TREE_PRIVATE (decl))
11665 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
11668 if (declaration)
11670 if (!old_die || !get_AT (old_die, DW_AT_inline))
11672 add_AT_flag (subr_die, DW_AT_declaration, 1);
11674 /* The first time we see a member function, it is in the context of
11675 the class to which it belongs. We make sure of this by emitting
11676 the class first. The next time is the definition, which is
11677 handled above. The two may come from the same source text.
11679 Note that force_decl_die() forces function declaration die. It is
11680 later reused to represent definition. */
11681 equate_decl_number_to_die (decl, subr_die);
11684 else if (DECL_ABSTRACT (decl))
11686 if (DECL_DECLARED_INLINE_P (decl))
11688 if (cgraph_function_possibly_inlined_p (decl))
11689 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
11690 else
11691 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
11693 else
11695 if (cgraph_function_possibly_inlined_p (decl))
11696 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
11697 else
11698 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
11701 equate_decl_number_to_die (decl, subr_die);
11703 else if (!DECL_EXTERNAL (decl))
11705 HOST_WIDE_INT cfa_fb_offset;
11707 if (!old_die || !get_AT (old_die, DW_AT_inline))
11708 equate_decl_number_to_die (decl, subr_die);
11710 if (!flag_reorder_blocks_and_partition)
11712 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
11713 current_function_funcdef_no);
11714 add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
11715 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
11716 current_function_funcdef_no);
11717 add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
11719 add_pubname (decl, subr_die);
11720 add_arange (decl, subr_die);
11722 else
11723 { /* Do nothing for now; maybe need to duplicate die, one for
11724 hot section and ond for cold section, then use the hot/cold
11725 section begin/end labels to generate the aranges... */
11727 add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
11728 add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
11729 add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
11730 add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
11732 add_pubname (decl, subr_die);
11733 add_arange (decl, subr_die);
11734 add_arange (decl, subr_die);
11738 #ifdef MIPS_DEBUGGING_INFO
11739 /* Add a reference to the FDE for this routine. */
11740 add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
11741 #endif
11743 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
11745 /* We define the "frame base" as the function's CFA. This is more
11746 convenient for several reasons: (1) It's stable across the prologue
11747 and epilogue, which makes it better than just a frame pointer,
11748 (2) With dwarf3, there exists a one-byte encoding that allows us
11749 to reference the .debug_frame data by proxy, but failing that,
11750 (3) We can at least reuse the code inspection and interpretation
11751 code that determines the CFA position at various points in the
11752 function. */
11753 /* ??? Use some command-line or configury switch to enable the use
11754 of dwarf3 DW_OP_call_frame_cfa. At present there are no dwarf
11755 consumers that understand it; fall back to "pure" dwarf2 and
11756 convert the CFA data into a location list. */
11758 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
11759 if (list->dw_loc_next)
11760 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
11761 else
11762 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
11765 /* Compute a displacement from the "steady-state frame pointer" to
11766 the CFA. The former is what all stack slots and argument slots
11767 will reference in the rtl; the later is what we've told the
11768 debugger about. We'll need to adjust all frame_base references
11769 by this displacement. */
11770 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
11772 if (cfun->static_chain_decl)
11773 add_AT_location_description (subr_die, DW_AT_static_link,
11774 loc_descriptor_from_tree (cfun->static_chain_decl));
11777 /* Now output descriptions of the arguments for this function. This gets
11778 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
11779 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
11780 `...' at the end of the formal parameter list. In order to find out if
11781 there was a trailing ellipsis or not, we must instead look at the type
11782 associated with the FUNCTION_DECL. This will be a node of type
11783 FUNCTION_TYPE. If the chain of type nodes hanging off of this
11784 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
11785 an ellipsis at the end. */
11787 /* In the case where we are describing a mere function declaration, all we
11788 need to do here (and all we *can* do here) is to describe the *types* of
11789 its formal parameters. */
11790 if (debug_info_level <= DINFO_LEVEL_TERSE)
11792 else if (declaration)
11793 gen_formal_types_die (decl, subr_die);
11794 else
11796 /* Generate DIEs to represent all known formal parameters. */
11797 tree arg_decls = DECL_ARGUMENTS (decl);
11798 tree parm;
11800 /* When generating DIEs, generate the unspecified_parameters DIE
11801 instead if we come across the arg "__builtin_va_alist" */
11802 for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
11803 if (TREE_CODE (parm) == PARM_DECL)
11805 if (DECL_NAME (parm)
11806 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
11807 "__builtin_va_alist"))
11808 gen_unspecified_parameters_die (parm, subr_die);
11809 else
11810 gen_decl_die (parm, subr_die);
11813 /* Decide whether we need an unspecified_parameters DIE at the end.
11814 There are 2 more cases to do this for: 1) the ansi ... declaration -
11815 this is detectable when the end of the arg list is not a
11816 void_type_node 2) an unprototyped function declaration (not a
11817 definition). This just means that we have no info about the
11818 parameters at all. */
11819 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
11820 if (fn_arg_types != NULL)
11822 /* This is the prototyped case, check for.... */
11823 if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
11824 gen_unspecified_parameters_die (decl, subr_die);
11826 else if (DECL_INITIAL (decl) == NULL_TREE)
11827 gen_unspecified_parameters_die (decl, subr_die);
11830 /* Output Dwarf info for all of the stuff within the body of the function
11831 (if it has one - it may be just a declaration). */
11832 outer_scope = DECL_INITIAL (decl);
11834 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
11835 a function. This BLOCK actually represents the outermost binding contour
11836 for the function, i.e. the contour in which the function's formal
11837 parameters and labels get declared. Curiously, it appears that the front
11838 end doesn't actually put the PARM_DECL nodes for the current function onto
11839 the BLOCK_VARS list for this outer scope, but are strung off of the
11840 DECL_ARGUMENTS list for the function instead.
11842 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
11843 the LABEL_DECL nodes for the function however, and we output DWARF info
11844 for those in decls_for_scope. Just within the `outer_scope' there will be
11845 a BLOCK node representing the function's outermost pair of curly braces,
11846 and any blocks used for the base and member initializers of a C++
11847 constructor function. */
11848 if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
11850 /* Emit a DW_TAG_variable DIE for a named return value. */
11851 if (DECL_NAME (DECL_RESULT (decl)))
11852 gen_decl_die (DECL_RESULT (decl), subr_die);
11854 current_function_has_inlines = 0;
11855 decls_for_scope (outer_scope, subr_die, 0);
11857 #if 0 && defined (MIPS_DEBUGGING_INFO)
11858 if (current_function_has_inlines)
11860 add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
11861 if (! comp_unit_has_inlines)
11863 add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
11864 comp_unit_has_inlines = 1;
11867 #endif
11869 /* Add the calling convention attribute if requested. */
11870 add_calling_convention_attribute (subr_die, TREE_TYPE (decl));
11874 /* Generate a DIE to represent a declared data object. */
11876 static void
11877 gen_variable_die (tree decl, dw_die_ref context_die)
11879 tree origin = decl_ultimate_origin (decl);
11880 dw_die_ref var_die = new_die (DW_TAG_variable, context_die, decl);
11882 dw_die_ref old_die = lookup_decl_die (decl);
11883 int declaration = (DECL_EXTERNAL (decl)
11884 /* If DECL is COMDAT and has not actually been
11885 emitted, we cannot take its address; there
11886 might end up being no definition anywhere in
11887 the program. For example, consider the C++
11888 test case:
11890 template <class T>
11891 struct S { static const int i = 7; };
11893 template <class T>
11894 const int S<T>::i;
11896 int f() { return S<int>::i; }
11898 Here, S<int>::i is not DECL_EXTERNAL, but no
11899 definition is required, so the compiler will
11900 not emit a definition. */
11901 || (TREE_CODE (decl) == VAR_DECL
11902 && DECL_COMDAT (decl) && !TREE_ASM_WRITTEN (decl))
11903 || class_or_namespace_scope_p (context_die));
11905 if (origin != NULL)
11906 add_abstract_origin_attribute (var_die, origin);
11908 /* Loop unrolling can create multiple blocks that refer to the same
11909 static variable, so we must test for the DW_AT_declaration flag.
11911 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
11912 copy decls and set the DECL_ABSTRACT flag on them instead of
11913 sharing them.
11915 ??? Duplicated blocks have been rewritten to use .debug_ranges.
11917 ??? The declare_in_namespace support causes us to get two DIEs for one
11918 variable, both of which are declarations. We want to avoid considering
11919 one to be a specification, so we must test that this DIE is not a
11920 declaration. */
11921 else if (old_die && TREE_STATIC (decl) && ! declaration
11922 && get_AT_flag (old_die, DW_AT_declaration) == 1)
11924 /* This is a definition of a C++ class level static. */
11925 add_AT_specification (var_die, old_die);
11926 if (DECL_NAME (decl))
11928 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11929 unsigned file_index = lookup_filename (s.file);
11931 if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
11932 add_AT_unsigned (var_die, DW_AT_decl_file, file_index);
11934 if (get_AT_unsigned (old_die, DW_AT_decl_line)
11935 != (unsigned) s.line)
11937 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
11940 else
11942 add_name_and_src_coords_attributes (var_die, decl);
11943 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
11944 TREE_THIS_VOLATILE (decl), context_die);
11946 if (TREE_PUBLIC (decl))
11947 add_AT_flag (var_die, DW_AT_external, 1);
11949 if (DECL_ARTIFICIAL (decl))
11950 add_AT_flag (var_die, DW_AT_artificial, 1);
11952 if (TREE_PROTECTED (decl))
11953 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
11954 else if (TREE_PRIVATE (decl))
11955 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
11958 if (declaration)
11959 add_AT_flag (var_die, DW_AT_declaration, 1);
11961 if (DECL_ABSTRACT (decl) || declaration)
11962 equate_decl_number_to_die (decl, var_die);
11964 if (! declaration && ! DECL_ABSTRACT (decl))
11966 add_location_or_const_value_attribute (var_die, decl, DW_AT_location);
11967 add_pubname (decl, var_die);
11969 else
11970 tree_add_const_value_attribute (var_die, decl);
11973 /* Generate a DIE to represent a label identifier. */
11975 static void
11976 gen_label_die (tree decl, dw_die_ref context_die)
11978 tree origin = decl_ultimate_origin (decl);
11979 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
11980 rtx insn;
11981 char label[MAX_ARTIFICIAL_LABEL_BYTES];
11983 if (origin != NULL)
11984 add_abstract_origin_attribute (lbl_die, origin);
11985 else
11986 add_name_and_src_coords_attributes (lbl_die, decl);
11988 if (DECL_ABSTRACT (decl))
11989 equate_decl_number_to_die (decl, lbl_die);
11990 else
11992 insn = DECL_RTL_IF_SET (decl);
11994 /* Deleted labels are programmer specified labels which have been
11995 eliminated because of various optimizations. We still emit them
11996 here so that it is possible to put breakpoints on them. */
11997 if (insn
11998 && (LABEL_P (insn)
11999 || ((NOTE_P (insn)
12000 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED_LABEL))))
12002 /* When optimization is enabled (via -O) some parts of the compiler
12003 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
12004 represent source-level labels which were explicitly declared by
12005 the user. This really shouldn't be happening though, so catch
12006 it if it ever does happen. */
12007 gcc_assert (!INSN_DELETED_P (insn));
12009 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
12010 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
12015 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
12016 attributes to the DIE for a block STMT, to describe where the inlined
12017 function was called from. This is similar to add_src_coords_attributes. */
12019 static inline void
12020 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
12022 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
12023 unsigned file_index = lookup_filename (s.file);
12025 add_AT_unsigned (die, DW_AT_call_file, file_index);
12026 add_AT_unsigned (die, DW_AT_call_line, s.line);
12029 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
12030 Add low_pc and high_pc attributes to the DIE for a block STMT. */
12032 static inline void
12033 add_high_low_attributes (tree stmt, dw_die_ref die)
12035 char label[MAX_ARTIFICIAL_LABEL_BYTES];
12037 if (BLOCK_FRAGMENT_CHAIN (stmt))
12039 tree chain;
12041 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
12043 chain = BLOCK_FRAGMENT_CHAIN (stmt);
12046 add_ranges (chain);
12047 chain = BLOCK_FRAGMENT_CHAIN (chain);
12049 while (chain);
12050 add_ranges (NULL);
12052 else
12054 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
12055 BLOCK_NUMBER (stmt));
12056 add_AT_lbl_id (die, DW_AT_low_pc, label);
12057 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
12058 BLOCK_NUMBER (stmt));
12059 add_AT_lbl_id (die, DW_AT_high_pc, label);
12063 /* Generate a DIE for a lexical block. */
12065 static void
12066 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
12068 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
12070 if (! BLOCK_ABSTRACT (stmt))
12071 add_high_low_attributes (stmt, stmt_die);
12073 decls_for_scope (stmt, stmt_die, depth);
12076 /* Generate a DIE for an inlined subprogram. */
12078 static void
12079 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
12081 tree decl = block_ultimate_origin (stmt);
12083 /* Emit info for the abstract instance first, if we haven't yet. We
12084 must emit this even if the block is abstract, otherwise when we
12085 emit the block below (or elsewhere), we may end up trying to emit
12086 a die whose origin die hasn't been emitted, and crashing. */
12087 dwarf2out_abstract_function (decl);
12089 if (! BLOCK_ABSTRACT (stmt))
12091 dw_die_ref subr_die
12092 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
12094 add_abstract_origin_attribute (subr_die, decl);
12095 add_high_low_attributes (stmt, subr_die);
12096 add_call_src_coords_attributes (stmt, subr_die);
12098 decls_for_scope (stmt, subr_die, depth);
12099 current_function_has_inlines = 1;
12101 else
12102 /* We may get here if we're the outer block of function A that was
12103 inlined into function B that was inlined into function C. When
12104 generating debugging info for C, dwarf2out_abstract_function(B)
12105 would mark all inlined blocks as abstract, including this one.
12106 So, we wouldn't (and shouldn't) expect labels to be generated
12107 for this one. Instead, just emit debugging info for
12108 declarations within the block. This is particularly important
12109 in the case of initializers of arguments passed from B to us:
12110 if they're statement expressions containing declarations, we
12111 wouldn't generate dies for their abstract variables, and then,
12112 when generating dies for the real variables, we'd die (pun
12113 intended :-) */
12114 gen_lexical_block_die (stmt, context_die, depth);
12117 /* Generate a DIE for a field in a record, or structure. */
12119 static void
12120 gen_field_die (tree decl, dw_die_ref context_die)
12122 dw_die_ref decl_die;
12124 if (TREE_TYPE (decl) == error_mark_node)
12125 return;
12127 decl_die = new_die (DW_TAG_member, context_die, decl);
12128 add_name_and_src_coords_attributes (decl_die, decl);
12129 add_type_attribute (decl_die, member_declared_type (decl),
12130 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
12131 context_die);
12133 if (DECL_BIT_FIELD_TYPE (decl))
12135 add_byte_size_attribute (decl_die, decl);
12136 add_bit_size_attribute (decl_die, decl);
12137 add_bit_offset_attribute (decl_die, decl);
12140 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
12141 add_data_member_location_attribute (decl_die, decl);
12143 if (DECL_ARTIFICIAL (decl))
12144 add_AT_flag (decl_die, DW_AT_artificial, 1);
12146 if (TREE_PROTECTED (decl))
12147 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
12148 else if (TREE_PRIVATE (decl))
12149 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
12151 /* Equate decl number to die, so that we can look up this decl later on. */
12152 equate_decl_number_to_die (decl, decl_die);
12155 #if 0
12156 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
12157 Use modified_type_die instead.
12158 We keep this code here just in case these types of DIEs may be needed to
12159 represent certain things in other languages (e.g. Pascal) someday. */
12161 static void
12162 gen_pointer_type_die (tree type, dw_die_ref context_die)
12164 dw_die_ref ptr_die
12165 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
12167 equate_type_number_to_die (type, ptr_die);
12168 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
12169 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
12172 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
12173 Use modified_type_die instead.
12174 We keep this code here just in case these types of DIEs may be needed to
12175 represent certain things in other languages (e.g. Pascal) someday. */
12177 static void
12178 gen_reference_type_die (tree type, dw_die_ref context_die)
12180 dw_die_ref ref_die
12181 = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
12183 equate_type_number_to_die (type, ref_die);
12184 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
12185 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
12187 #endif
12189 /* Generate a DIE for a pointer to a member type. */
12191 static void
12192 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
12194 dw_die_ref ptr_die
12195 = new_die (DW_TAG_ptr_to_member_type,
12196 scope_die_for (type, context_die), type);
12198 equate_type_number_to_die (type, ptr_die);
12199 add_AT_die_ref (ptr_die, DW_AT_containing_type,
12200 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
12201 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
12204 /* Generate the DIE for the compilation unit. */
12206 static dw_die_ref
12207 gen_compile_unit_die (const char *filename)
12209 dw_die_ref die;
12210 char producer[250];
12211 const char *language_string = lang_hooks.name;
12212 int language;
12214 die = new_die (DW_TAG_compile_unit, NULL, NULL);
12216 if (filename)
12218 add_name_attribute (die, filename);
12219 /* Don't add cwd for <built-in>. */
12220 if (filename[0] != DIR_SEPARATOR && filename[0] != '<')
12221 add_comp_dir_attribute (die);
12224 sprintf (producer, "%s %s", language_string, version_string);
12226 #ifdef MIPS_DEBUGGING_INFO
12227 /* The MIPS/SGI compilers place the 'cc' command line options in the producer
12228 string. The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
12229 not appear in the producer string, the debugger reaches the conclusion
12230 that the object file is stripped and has no debugging information.
12231 To get the MIPS/SGI debugger to believe that there is debugging
12232 information in the object file, we add a -g to the producer string. */
12233 if (debug_info_level > DINFO_LEVEL_TERSE)
12234 strcat (producer, " -g");
12235 #endif
12237 add_AT_string (die, DW_AT_producer, producer);
12239 if (strcmp (language_string, "GNU C++") == 0)
12240 language = DW_LANG_C_plus_plus;
12241 else if (strcmp (language_string, "GNU Ada") == 0)
12242 language = DW_LANG_Ada95;
12243 else if (strcmp (language_string, "GNU F77") == 0)
12244 language = DW_LANG_Fortran77;
12245 else if (strcmp (language_string, "GNU F95") == 0)
12246 language = DW_LANG_Fortran95;
12247 else if (strcmp (language_string, "GNU Pascal") == 0)
12248 language = DW_LANG_Pascal83;
12249 else if (strcmp (language_string, "GNU Java") == 0)
12250 language = DW_LANG_Java;
12251 else if (strcmp (language_string, "GNU Objective-C") == 0)
12252 language = DW_LANG_ObjC;
12253 else if (strcmp (language_string, "GNU Objective-C++") == 0)
12254 language = DW_LANG_ObjC_plus_plus;
12255 else
12256 language = DW_LANG_C89;
12258 add_AT_unsigned (die, DW_AT_language, language);
12259 return die;
12262 /* Generate the DIE for a base class. */
12264 static void
12265 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
12267 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
12269 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
12270 add_data_member_location_attribute (die, binfo);
12272 if (BINFO_VIRTUAL_P (binfo))
12273 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
12275 if (access == access_public_node)
12276 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
12277 else if (access == access_protected_node)
12278 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
12281 /* Generate a DIE for a class member. */
12283 static void
12284 gen_member_die (tree type, dw_die_ref context_die)
12286 tree member;
12287 tree binfo = TYPE_BINFO (type);
12288 dw_die_ref child;
12290 /* If this is not an incomplete type, output descriptions of each of its
12291 members. Note that as we output the DIEs necessary to represent the
12292 members of this record or union type, we will also be trying to output
12293 DIEs to represent the *types* of those members. However the `type'
12294 function (above) will specifically avoid generating type DIEs for member
12295 types *within* the list of member DIEs for this (containing) type except
12296 for those types (of members) which are explicitly marked as also being
12297 members of this (containing) type themselves. The g++ front- end can
12298 force any given type to be treated as a member of some other (containing)
12299 type by setting the TYPE_CONTEXT of the given (member) type to point to
12300 the TREE node representing the appropriate (containing) type. */
12302 /* First output info about the base classes. */
12303 if (binfo)
12305 VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
12306 int i;
12307 tree base;
12309 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
12310 gen_inheritance_die (base,
12311 (accesses ? VEC_index (tree, accesses, i)
12312 : access_public_node), context_die);
12315 /* Now output info about the data members and type members. */
12316 for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
12318 /* If we thought we were generating minimal debug info for TYPE
12319 and then changed our minds, some of the member declarations
12320 may have already been defined. Don't define them again, but
12321 do put them in the right order. */
12323 child = lookup_decl_die (member);
12324 if (child)
12325 splice_child_die (context_die, child);
12326 else
12327 gen_decl_die (member, context_die);
12330 /* Now output info about the function members (if any). */
12331 for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
12333 /* Don't include clones in the member list. */
12334 if (DECL_ABSTRACT_ORIGIN (member))
12335 continue;
12337 child = lookup_decl_die (member);
12338 if (child)
12339 splice_child_die (context_die, child);
12340 else
12341 gen_decl_die (member, context_die);
12345 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
12346 is set, we pretend that the type was never defined, so we only get the
12347 member DIEs needed by later specification DIEs. */
12349 static void
12350 gen_struct_or_union_type_die (tree type, dw_die_ref context_die)
12352 dw_die_ref type_die = lookup_type_die (type);
12353 dw_die_ref scope_die = 0;
12354 int nested = 0;
12355 int complete = (TYPE_SIZE (type)
12356 && (! TYPE_STUB_DECL (type)
12357 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
12358 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
12360 if (type_die && ! complete)
12361 return;
12363 if (TYPE_CONTEXT (type) != NULL_TREE
12364 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
12365 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
12366 nested = 1;
12368 scope_die = scope_die_for (type, context_die);
12370 if (! type_die || (nested && scope_die == comp_unit_die))
12371 /* First occurrence of type or toplevel definition of nested class. */
12373 dw_die_ref old_die = type_die;
12375 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
12376 ? DW_TAG_structure_type : DW_TAG_union_type,
12377 scope_die, type);
12378 equate_type_number_to_die (type, type_die);
12379 if (old_die)
12380 add_AT_specification (type_die, old_die);
12381 else
12382 add_name_attribute (type_die, type_tag (type));
12384 else
12385 remove_AT (type_die, DW_AT_declaration);
12387 /* If this type has been completed, then give it a byte_size attribute and
12388 then give a list of members. */
12389 if (complete && !ns_decl)
12391 /* Prevent infinite recursion in cases where the type of some member of
12392 this type is expressed in terms of this type itself. */
12393 TREE_ASM_WRITTEN (type) = 1;
12394 add_byte_size_attribute (type_die, type);
12395 if (TYPE_STUB_DECL (type) != NULL_TREE)
12396 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
12398 /* If the first reference to this type was as the return type of an
12399 inline function, then it may not have a parent. Fix this now. */
12400 if (type_die->die_parent == NULL)
12401 add_child_die (scope_die, type_die);
12403 push_decl_scope (type);
12404 gen_member_die (type, type_die);
12405 pop_decl_scope ();
12407 /* GNU extension: Record what type our vtable lives in. */
12408 if (TYPE_VFIELD (type))
12410 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
12412 gen_type_die (vtype, context_die);
12413 add_AT_die_ref (type_die, DW_AT_containing_type,
12414 lookup_type_die (vtype));
12417 else
12419 add_AT_flag (type_die, DW_AT_declaration, 1);
12421 /* We don't need to do this for function-local types. */
12422 if (TYPE_STUB_DECL (type)
12423 && ! decl_function_context (TYPE_STUB_DECL (type)))
12424 VEC_safe_push (tree, gc, incomplete_types, type);
12428 /* Generate a DIE for a subroutine _type_. */
12430 static void
12431 gen_subroutine_type_die (tree type, dw_die_ref context_die)
12433 tree return_type = TREE_TYPE (type);
12434 dw_die_ref subr_die
12435 = new_die (DW_TAG_subroutine_type,
12436 scope_die_for (type, context_die), type);
12438 equate_type_number_to_die (type, subr_die);
12439 add_prototyped_attribute (subr_die, type);
12440 add_type_attribute (subr_die, return_type, 0, 0, context_die);
12441 gen_formal_types_die (type, subr_die);
12444 /* Generate a DIE for a type definition. */
12446 static void
12447 gen_typedef_die (tree decl, dw_die_ref context_die)
12449 dw_die_ref type_die;
12450 tree origin;
12452 if (TREE_ASM_WRITTEN (decl))
12453 return;
12455 TREE_ASM_WRITTEN (decl) = 1;
12456 type_die = new_die (DW_TAG_typedef, context_die, decl);
12457 origin = decl_ultimate_origin (decl);
12458 if (origin != NULL)
12459 add_abstract_origin_attribute (type_die, origin);
12460 else
12462 tree type;
12464 add_name_and_src_coords_attributes (type_die, decl);
12465 if (DECL_ORIGINAL_TYPE (decl))
12467 type = DECL_ORIGINAL_TYPE (decl);
12469 gcc_assert (type != TREE_TYPE (decl));
12470 equate_type_number_to_die (TREE_TYPE (decl), type_die);
12472 else
12473 type = TREE_TYPE (decl);
12475 add_type_attribute (type_die, type, TREE_READONLY (decl),
12476 TREE_THIS_VOLATILE (decl), context_die);
12479 if (DECL_ABSTRACT (decl))
12480 equate_decl_number_to_die (decl, type_die);
12483 /* Generate a type description DIE. */
12485 static void
12486 gen_type_die (tree type, dw_die_ref context_die)
12488 int need_pop;
12490 if (type == NULL_TREE || type == error_mark_node)
12491 return;
12493 if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12494 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
12496 if (TREE_ASM_WRITTEN (type))
12497 return;
12499 /* Prevent broken recursion; we can't hand off to the same type. */
12500 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
12502 TREE_ASM_WRITTEN (type) = 1;
12503 gen_decl_die (TYPE_NAME (type), context_die);
12504 return;
12507 /* We are going to output a DIE to represent the unqualified version
12508 of this type (i.e. without any const or volatile qualifiers) so
12509 get the main variant (i.e. the unqualified version) of this type
12510 now. (Vectors are special because the debugging info is in the
12511 cloned type itself). */
12512 if (TREE_CODE (type) != VECTOR_TYPE)
12513 type = type_main_variant (type);
12515 if (TREE_ASM_WRITTEN (type))
12516 return;
12518 switch (TREE_CODE (type))
12520 case ERROR_MARK:
12521 break;
12523 case POINTER_TYPE:
12524 case REFERENCE_TYPE:
12525 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
12526 ensures that the gen_type_die recursion will terminate even if the
12527 type is recursive. Recursive types are possible in Ada. */
12528 /* ??? We could perhaps do this for all types before the switch
12529 statement. */
12530 TREE_ASM_WRITTEN (type) = 1;
12532 /* For these types, all that is required is that we output a DIE (or a
12533 set of DIEs) to represent the "basis" type. */
12534 gen_type_die (TREE_TYPE (type), context_die);
12535 break;
12537 case OFFSET_TYPE:
12538 /* This code is used for C++ pointer-to-data-member types.
12539 Output a description of the relevant class type. */
12540 gen_type_die (TYPE_OFFSET_BASETYPE (type), context_die);
12542 /* Output a description of the type of the object pointed to. */
12543 gen_type_die (TREE_TYPE (type), context_die);
12545 /* Now output a DIE to represent this pointer-to-data-member type
12546 itself. */
12547 gen_ptr_to_mbr_type_die (type, context_die);
12548 break;
12550 case FUNCTION_TYPE:
12551 /* Force out return type (in case it wasn't forced out already). */
12552 gen_type_die (TREE_TYPE (type), context_die);
12553 gen_subroutine_type_die (type, context_die);
12554 break;
12556 case METHOD_TYPE:
12557 /* Force out return type (in case it wasn't forced out already). */
12558 gen_type_die (TREE_TYPE (type), context_die);
12559 gen_subroutine_type_die (type, context_die);
12560 break;
12562 case ARRAY_TYPE:
12563 gen_array_type_die (type, context_die);
12564 break;
12566 case VECTOR_TYPE:
12567 gen_array_type_die (type, context_die);
12568 break;
12570 case ENUMERAL_TYPE:
12571 case RECORD_TYPE:
12572 case UNION_TYPE:
12573 case QUAL_UNION_TYPE:
12574 /* If this is a nested type whose containing class hasn't been written
12575 out yet, writing it out will cover this one, too. This does not apply
12576 to instantiations of member class templates; they need to be added to
12577 the containing class as they are generated. FIXME: This hurts the
12578 idea of combining type decls from multiple TUs, since we can't predict
12579 what set of template instantiations we'll get. */
12580 if (TYPE_CONTEXT (type)
12581 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
12582 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
12584 gen_type_die (TYPE_CONTEXT (type), context_die);
12586 if (TREE_ASM_WRITTEN (type))
12587 return;
12589 /* If that failed, attach ourselves to the stub. */
12590 push_decl_scope (TYPE_CONTEXT (type));
12591 context_die = lookup_type_die (TYPE_CONTEXT (type));
12592 need_pop = 1;
12594 else
12596 declare_in_namespace (type, context_die);
12597 need_pop = 0;
12600 if (TREE_CODE (type) == ENUMERAL_TYPE)
12601 gen_enumeration_type_die (type, context_die);
12602 else
12603 gen_struct_or_union_type_die (type, context_die);
12605 if (need_pop)
12606 pop_decl_scope ();
12608 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
12609 it up if it is ever completed. gen_*_type_die will set it for us
12610 when appropriate. */
12611 return;
12613 case VOID_TYPE:
12614 case INTEGER_TYPE:
12615 case REAL_TYPE:
12616 case COMPLEX_TYPE:
12617 case BOOLEAN_TYPE:
12618 /* No DIEs needed for fundamental types. */
12619 break;
12621 case LANG_TYPE:
12622 /* No Dwarf representation currently defined. */
12623 break;
12625 default:
12626 gcc_unreachable ();
12629 TREE_ASM_WRITTEN (type) = 1;
12632 /* Generate a DIE for a tagged type instantiation. */
12634 static void
12635 gen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
12637 if (type == NULL_TREE || type == error_mark_node)
12638 return;
12640 /* We are going to output a DIE to represent the unqualified version of
12641 this type (i.e. without any const or volatile qualifiers) so make sure
12642 that we have the main variant (i.e. the unqualified version) of this
12643 type now. */
12644 gcc_assert (type == type_main_variant (type));
12646 /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
12647 an instance of an unresolved type. */
12649 switch (TREE_CODE (type))
12651 case ERROR_MARK:
12652 break;
12654 case ENUMERAL_TYPE:
12655 gen_inlined_enumeration_type_die (type, context_die);
12656 break;
12658 case RECORD_TYPE:
12659 gen_inlined_structure_type_die (type, context_die);
12660 break;
12662 case UNION_TYPE:
12663 case QUAL_UNION_TYPE:
12664 gen_inlined_union_type_die (type, context_die);
12665 break;
12667 default:
12668 gcc_unreachable ();
12672 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
12673 things which are local to the given block. */
12675 static void
12676 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
12678 int must_output_die = 0;
12679 tree origin;
12680 tree decl;
12681 enum tree_code origin_code;
12683 /* Ignore blocks that are NULL. */
12684 if (stmt == NULL_TREE)
12685 return;
12687 /* If the block is one fragment of a non-contiguous block, do not
12688 process the variables, since they will have been done by the
12689 origin block. Do process subblocks. */
12690 if (BLOCK_FRAGMENT_ORIGIN (stmt))
12692 tree sub;
12694 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
12695 gen_block_die (sub, context_die, depth + 1);
12697 return;
12700 /* Determine the "ultimate origin" of this block. This block may be an
12701 inlined instance of an inlined instance of inline function, so we have
12702 to trace all of the way back through the origin chain to find out what
12703 sort of node actually served as the original seed for the creation of
12704 the current block. */
12705 origin = block_ultimate_origin (stmt);
12706 origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
12708 /* Determine if we need to output any Dwarf DIEs at all to represent this
12709 block. */
12710 if (origin_code == FUNCTION_DECL)
12711 /* The outer scopes for inlinings *must* always be represented. We
12712 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
12713 must_output_die = 1;
12714 else
12716 /* In the case where the current block represents an inlining of the
12717 "body block" of an inline function, we must *NOT* output any DIE for
12718 this block because we have already output a DIE to represent the whole
12719 inlined function scope and the "body block" of any function doesn't
12720 really represent a different scope according to ANSI C rules. So we
12721 check here to make sure that this block does not represent a "body
12722 block inlining" before trying to set the MUST_OUTPUT_DIE flag. */
12723 if (! is_body_block (origin ? origin : stmt))
12725 /* Determine if this block directly contains any "significant"
12726 local declarations which we will need to output DIEs for. */
12727 if (debug_info_level > DINFO_LEVEL_TERSE)
12728 /* We are not in terse mode so *any* local declaration counts
12729 as being a "significant" one. */
12730 must_output_die = (BLOCK_VARS (stmt) != NULL
12731 && (TREE_USED (stmt)
12732 || TREE_ASM_WRITTEN (stmt)
12733 || BLOCK_ABSTRACT (stmt)));
12734 else
12735 /* We are in terse mode, so only local (nested) function
12736 definitions count as "significant" local declarations. */
12737 for (decl = BLOCK_VARS (stmt);
12738 decl != NULL; decl = TREE_CHAIN (decl))
12739 if (TREE_CODE (decl) == FUNCTION_DECL
12740 && DECL_INITIAL (decl))
12742 must_output_die = 1;
12743 break;
12748 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
12749 DIE for any block which contains no significant local declarations at
12750 all. Rather, in such cases we just call `decls_for_scope' so that any
12751 needed Dwarf info for any sub-blocks will get properly generated. Note
12752 that in terse mode, our definition of what constitutes a "significant"
12753 local declaration gets restricted to include only inlined function
12754 instances and local (nested) function definitions. */
12755 if (must_output_die)
12757 if (origin_code == FUNCTION_DECL)
12758 gen_inlined_subroutine_die (stmt, context_die, depth);
12759 else
12760 gen_lexical_block_die (stmt, context_die, depth);
12762 else
12763 decls_for_scope (stmt, context_die, depth);
12766 /* Generate all of the decls declared within a given scope and (recursively)
12767 all of its sub-blocks. */
12769 static void
12770 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
12772 tree decl;
12773 tree subblocks;
12775 /* Ignore NULL blocks. */
12776 if (stmt == NULL_TREE)
12777 return;
12779 if (TREE_USED (stmt))
12781 /* Output the DIEs to represent all of the data objects and typedefs
12782 declared directly within this block but not within any nested
12783 sub-blocks. Also, nested function and tag DIEs have been
12784 generated with a parent of NULL; fix that up now. */
12785 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
12787 dw_die_ref die;
12789 if (TREE_CODE (decl) == FUNCTION_DECL)
12790 die = lookup_decl_die (decl);
12791 else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
12792 die = lookup_type_die (TREE_TYPE (decl));
12793 else
12794 die = NULL;
12796 if (die != NULL && die->die_parent == NULL)
12797 add_child_die (context_die, die);
12798 /* Do not produce debug information for static variables since
12799 these might be optimized out. We are called for these later
12800 in cgraph_varpool_analyze_pending_decls. */
12801 if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
12803 else
12804 gen_decl_die (decl, context_die);
12808 /* If we're at -g1, we're not interested in subblocks. */
12809 if (debug_info_level <= DINFO_LEVEL_TERSE)
12810 return;
12812 /* Output the DIEs to represent all sub-blocks (and the items declared
12813 therein) of this block. */
12814 for (subblocks = BLOCK_SUBBLOCKS (stmt);
12815 subblocks != NULL;
12816 subblocks = BLOCK_CHAIN (subblocks))
12817 gen_block_die (subblocks, context_die, depth + 1);
12820 /* Is this a typedef we can avoid emitting? */
12822 static inline int
12823 is_redundant_typedef (tree decl)
12825 if (TYPE_DECL_IS_STUB (decl))
12826 return 1;
12828 if (DECL_ARTIFICIAL (decl)
12829 && DECL_CONTEXT (decl)
12830 && is_tagged_type (DECL_CONTEXT (decl))
12831 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
12832 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
12833 /* Also ignore the artificial member typedef for the class name. */
12834 return 1;
12836 return 0;
12839 /* Returns the DIE for decl. A DIE will always be returned. */
12841 static dw_die_ref
12842 force_decl_die (tree decl)
12844 dw_die_ref decl_die;
12845 unsigned saved_external_flag;
12846 tree save_fn = NULL_TREE;
12847 decl_die = lookup_decl_die (decl);
12848 if (!decl_die)
12850 dw_die_ref context_die;
12851 tree decl_context = DECL_CONTEXT (decl);
12852 if (decl_context)
12854 /* Find die that represents this context. */
12855 if (TYPE_P (decl_context))
12856 context_die = force_type_die (decl_context);
12857 else
12858 context_die = force_decl_die (decl_context);
12860 else
12861 context_die = comp_unit_die;
12863 decl_die = lookup_decl_die (decl);
12864 if (decl_die)
12865 return decl_die;
12867 switch (TREE_CODE (decl))
12869 case FUNCTION_DECL:
12870 /* Clear current_function_decl, so that gen_subprogram_die thinks
12871 that this is a declaration. At this point, we just want to force
12872 declaration die. */
12873 save_fn = current_function_decl;
12874 current_function_decl = NULL_TREE;
12875 gen_subprogram_die (decl, context_die);
12876 current_function_decl = save_fn;
12877 break;
12879 case VAR_DECL:
12880 /* Set external flag to force declaration die. Restore it after
12881 gen_decl_die() call. */
12882 saved_external_flag = DECL_EXTERNAL (decl);
12883 DECL_EXTERNAL (decl) = 1;
12884 gen_decl_die (decl, context_die);
12885 DECL_EXTERNAL (decl) = saved_external_flag;
12886 break;
12888 case NAMESPACE_DECL:
12889 dwarf2out_decl (decl);
12890 break;
12892 default:
12893 gcc_unreachable ();
12896 /* We should be able to find the DIE now. */
12897 if (!decl_die)
12898 decl_die = lookup_decl_die (decl);
12899 gcc_assert (decl_die);
12902 return decl_die;
12905 /* Returns the DIE for TYPE. A DIE is always returned. */
12907 static dw_die_ref
12908 force_type_die (tree type)
12910 dw_die_ref type_die;
12912 type_die = lookup_type_die (type);
12913 if (!type_die)
12915 dw_die_ref context_die;
12916 if (TYPE_CONTEXT (type))
12918 if (TYPE_P (TYPE_CONTEXT (type)))
12919 context_die = force_type_die (TYPE_CONTEXT (type));
12920 else
12921 context_die = force_decl_die (TYPE_CONTEXT (type));
12923 else
12924 context_die = comp_unit_die;
12926 type_die = lookup_type_die (type);
12927 if (type_die)
12928 return type_die;
12929 gen_type_die (type, context_die);
12930 type_die = lookup_type_die (type);
12931 gcc_assert (type_die);
12933 return type_die;
12936 /* Force out any required namespaces to be able to output DECL,
12937 and return the new context_die for it, if it's changed. */
12939 static dw_die_ref
12940 setup_namespace_context (tree thing, dw_die_ref context_die)
12942 tree context = (DECL_P (thing)
12943 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
12944 if (context && TREE_CODE (context) == NAMESPACE_DECL)
12945 /* Force out the namespace. */
12946 context_die = force_decl_die (context);
12948 return context_die;
12951 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
12952 type) within its namespace, if appropriate.
12954 For compatibility with older debuggers, namespace DIEs only contain
12955 declarations; all definitions are emitted at CU scope. */
12957 static void
12958 declare_in_namespace (tree thing, dw_die_ref context_die)
12960 dw_die_ref ns_context;
12962 if (debug_info_level <= DINFO_LEVEL_TERSE)
12963 return;
12965 /* If this decl is from an inlined function, then don't try to emit it in its
12966 namespace, as we will get confused. It would have already been emitted
12967 when the abstract instance of the inline function was emitted anyways. */
12968 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
12969 return;
12971 ns_context = setup_namespace_context (thing, context_die);
12973 if (ns_context != context_die)
12975 if (DECL_P (thing))
12976 gen_decl_die (thing, ns_context);
12977 else
12978 gen_type_die (thing, ns_context);
12982 /* Generate a DIE for a namespace or namespace alias. */
12984 static void
12985 gen_namespace_die (tree decl)
12987 dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
12989 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
12990 they are an alias of. */
12991 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
12993 /* Output a real namespace. */
12994 dw_die_ref namespace_die
12995 = new_die (DW_TAG_namespace, context_die, decl);
12996 add_name_and_src_coords_attributes (namespace_die, decl);
12997 equate_decl_number_to_die (decl, namespace_die);
12999 else
13001 /* Output a namespace alias. */
13003 /* Force out the namespace we are an alias of, if necessary. */
13004 dw_die_ref origin_die
13005 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
13007 /* Now create the namespace alias DIE. */
13008 dw_die_ref namespace_die
13009 = new_die (DW_TAG_imported_declaration, context_die, decl);
13010 add_name_and_src_coords_attributes (namespace_die, decl);
13011 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
13012 equate_decl_number_to_die (decl, namespace_die);
13016 /* Generate Dwarf debug information for a decl described by DECL. */
13018 static void
13019 gen_decl_die (tree decl, dw_die_ref context_die)
13021 tree origin;
13023 if (DECL_P (decl) && DECL_IGNORED_P (decl))
13024 return;
13026 switch (TREE_CODE (decl))
13028 case ERROR_MARK:
13029 break;
13031 case CONST_DECL:
13032 /* The individual enumerators of an enum type get output when we output
13033 the Dwarf representation of the relevant enum type itself. */
13034 break;
13036 case FUNCTION_DECL:
13037 /* Don't output any DIEs to represent mere function declarations,
13038 unless they are class members or explicit block externs. */
13039 if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
13040 && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
13041 break;
13043 #if 0
13044 /* FIXME */
13045 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
13046 on local redeclarations of global functions. That seems broken. */
13047 if (current_function_decl != decl)
13048 /* This is only a declaration. */;
13049 #endif
13051 /* If we're emitting a clone, emit info for the abstract instance. */
13052 if (DECL_ORIGIN (decl) != decl)
13053 dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
13055 /* If we're emitting an out-of-line copy of an inline function,
13056 emit info for the abstract instance and set up to refer to it. */
13057 else if (cgraph_function_possibly_inlined_p (decl)
13058 && ! DECL_ABSTRACT (decl)
13059 && ! class_or_namespace_scope_p (context_die)
13060 /* dwarf2out_abstract_function won't emit a die if this is just
13061 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
13062 that case, because that works only if we have a die. */
13063 && DECL_INITIAL (decl) != NULL_TREE)
13065 dwarf2out_abstract_function (decl);
13066 set_decl_origin_self (decl);
13069 /* Otherwise we're emitting the primary DIE for this decl. */
13070 else if (debug_info_level > DINFO_LEVEL_TERSE)
13072 /* Before we describe the FUNCTION_DECL itself, make sure that we
13073 have described its return type. */
13074 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
13076 /* And its virtual context. */
13077 if (DECL_VINDEX (decl) != NULL_TREE)
13078 gen_type_die (DECL_CONTEXT (decl), context_die);
13080 /* And its containing type. */
13081 origin = decl_class_context (decl);
13082 if (origin != NULL_TREE)
13083 gen_type_die_for_member (origin, decl, context_die);
13085 /* And its containing namespace. */
13086 declare_in_namespace (decl, context_die);
13089 /* Now output a DIE to represent the function itself. */
13090 gen_subprogram_die (decl, context_die);
13091 break;
13093 case TYPE_DECL:
13094 /* If we are in terse mode, don't generate any DIEs to represent any
13095 actual typedefs. */
13096 if (debug_info_level <= DINFO_LEVEL_TERSE)
13097 break;
13099 /* In the special case of a TYPE_DECL node representing the declaration
13100 of some type tag, if the given TYPE_DECL is marked as having been
13101 instantiated from some other (original) TYPE_DECL node (e.g. one which
13102 was generated within the original definition of an inline function) we
13103 have to generate a special (abbreviated) DW_TAG_structure_type,
13104 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. */
13105 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
13107 gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
13108 break;
13111 if (is_redundant_typedef (decl))
13112 gen_type_die (TREE_TYPE (decl), context_die);
13113 else
13114 /* Output a DIE to represent the typedef itself. */
13115 gen_typedef_die (decl, context_die);
13116 break;
13118 case LABEL_DECL:
13119 if (debug_info_level >= DINFO_LEVEL_NORMAL)
13120 gen_label_die (decl, context_die);
13121 break;
13123 case VAR_DECL:
13124 case RESULT_DECL:
13125 /* If we are in terse mode, don't generate any DIEs to represent any
13126 variable declarations or definitions. */
13127 if (debug_info_level <= DINFO_LEVEL_TERSE)
13128 break;
13130 /* Output any DIEs that are needed to specify the type of this data
13131 object. */
13132 gen_type_die (TREE_TYPE (decl), context_die);
13134 /* And its containing type. */
13135 origin = decl_class_context (decl);
13136 if (origin != NULL_TREE)
13137 gen_type_die_for_member (origin, decl, context_die);
13139 /* And its containing namespace. */
13140 declare_in_namespace (decl, context_die);
13142 /* Now output the DIE to represent the data object itself. This gets
13143 complicated because of the possibility that the VAR_DECL really
13144 represents an inlined instance of a formal parameter for an inline
13145 function. */
13146 origin = decl_ultimate_origin (decl);
13147 if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
13148 gen_formal_parameter_die (decl, context_die);
13149 else
13150 gen_variable_die (decl, context_die);
13151 break;
13153 case FIELD_DECL:
13154 /* Ignore the nameless fields that are used to skip bits but handle C++
13155 anonymous unions and structs. */
13156 if (DECL_NAME (decl) != NULL_TREE
13157 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
13158 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
13160 gen_type_die (member_declared_type (decl), context_die);
13161 gen_field_die (decl, context_die);
13163 break;
13165 case PARM_DECL:
13166 gen_type_die (TREE_TYPE (decl), context_die);
13167 gen_formal_parameter_die (decl, context_die);
13168 break;
13170 case NAMESPACE_DECL:
13171 gen_namespace_die (decl);
13172 break;
13174 default:
13175 /* Probably some frontend-internal decl. Assume we don't care. */
13176 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
13177 break;
13181 /* Output debug information for global decl DECL. Called from toplev.c after
13182 compilation proper has finished. */
13184 static void
13185 dwarf2out_global_decl (tree decl)
13187 /* Output DWARF2 information for file-scope tentative data object
13188 declarations, file-scope (extern) function declarations (which had no
13189 corresponding body) and file-scope tagged type declarations and
13190 definitions which have not yet been forced out. */
13191 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
13192 dwarf2out_decl (decl);
13195 /* Output debug information for type decl DECL. Called from toplev.c
13196 and from language front ends (to record built-in types). */
13197 static void
13198 dwarf2out_type_decl (tree decl, int local)
13200 if (!local)
13201 dwarf2out_decl (decl);
13204 /* Output debug information for imported module or decl. */
13206 static void
13207 dwarf2out_imported_module_or_decl (tree decl, tree context)
13209 dw_die_ref imported_die, at_import_die;
13210 dw_die_ref scope_die;
13211 unsigned file_index;
13212 expanded_location xloc;
13214 if (debug_info_level <= DINFO_LEVEL_TERSE)
13215 return;
13217 gcc_assert (decl);
13219 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
13220 We need decl DIE for reference and scope die. First, get DIE for the decl
13221 itself. */
13223 /* Get the scope die for decl context. Use comp_unit_die for global module
13224 or decl. If die is not found for non globals, force new die. */
13225 if (!context)
13226 scope_die = comp_unit_die;
13227 else if (TYPE_P (context))
13228 scope_die = force_type_die (context);
13229 else
13230 scope_die = force_decl_die (context);
13232 /* For TYPE_DECL or CONST_DECL, lookup TREE_TYPE. */
13233 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
13234 at_import_die = force_type_die (TREE_TYPE (decl));
13235 else
13237 at_import_die = lookup_decl_die (decl);
13238 if (!at_import_die)
13240 /* If we're trying to avoid duplicate debug info, we may not have
13241 emitted the member decl for this field. Emit it now. */
13242 if (TREE_CODE (decl) == FIELD_DECL)
13244 tree type = DECL_CONTEXT (decl);
13245 dw_die_ref type_context_die;
13247 if (TYPE_CONTEXT (type))
13248 if (TYPE_P (TYPE_CONTEXT (type)))
13249 type_context_die = force_type_die (TYPE_CONTEXT (type));
13250 else
13251 type_context_die = force_decl_die (TYPE_CONTEXT (type));
13252 else
13253 type_context_die = comp_unit_die;
13254 gen_type_die_for_member (type, decl, type_context_die);
13256 at_import_die = force_decl_die (decl);
13260 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
13261 if (TREE_CODE (decl) == NAMESPACE_DECL)
13262 imported_die = new_die (DW_TAG_imported_module, scope_die, context);
13263 else
13264 imported_die = new_die (DW_TAG_imported_declaration, scope_die, context);
13266 xloc = expand_location (input_location);
13267 file_index = lookup_filename (xloc.file);
13268 add_AT_unsigned (imported_die, DW_AT_decl_file, file_index);
13269 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
13270 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
13273 /* Write the debugging output for DECL. */
13275 void
13276 dwarf2out_decl (tree decl)
13278 dw_die_ref context_die = comp_unit_die;
13280 switch (TREE_CODE (decl))
13282 case ERROR_MARK:
13283 return;
13285 case FUNCTION_DECL:
13286 /* What we would really like to do here is to filter out all mere
13287 file-scope declarations of file-scope functions which are never
13288 referenced later within this translation unit (and keep all of ones
13289 that *are* referenced later on) but we aren't clairvoyant, so we have
13290 no idea which functions will be referenced in the future (i.e. later
13291 on within the current translation unit). So here we just ignore all
13292 file-scope function declarations which are not also definitions. If
13293 and when the debugger needs to know something about these functions,
13294 it will have to hunt around and find the DWARF information associated
13295 with the definition of the function.
13297 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
13298 nodes represent definitions and which ones represent mere
13299 declarations. We have to check DECL_INITIAL instead. That's because
13300 the C front-end supports some weird semantics for "extern inline"
13301 function definitions. These can get inlined within the current
13302 translation unit (and thus, we need to generate Dwarf info for their
13303 abstract instances so that the Dwarf info for the concrete inlined
13304 instances can have something to refer to) but the compiler never
13305 generates any out-of-lines instances of such things (despite the fact
13306 that they *are* definitions).
13308 The important point is that the C front-end marks these "extern
13309 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
13310 them anyway. Note that the C++ front-end also plays some similar games
13311 for inline function definitions appearing within include files which
13312 also contain `#pragma interface' pragmas. */
13313 if (DECL_INITIAL (decl) == NULL_TREE)
13314 return;
13316 /* If we're a nested function, initially use a parent of NULL; if we're
13317 a plain function, this will be fixed up in decls_for_scope. If
13318 we're a method, it will be ignored, since we already have a DIE. */
13319 if (decl_function_context (decl)
13320 /* But if we're in terse mode, we don't care about scope. */
13321 && debug_info_level > DINFO_LEVEL_TERSE)
13322 context_die = NULL;
13323 break;
13325 case VAR_DECL:
13326 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
13327 declaration and if the declaration was never even referenced from
13328 within this entire compilation unit. We suppress these DIEs in
13329 order to save space in the .debug section (by eliminating entries
13330 which are probably useless). Note that we must not suppress
13331 block-local extern declarations (whether used or not) because that
13332 would screw-up the debugger's name lookup mechanism and cause it to
13333 miss things which really ought to be in scope at a given point. */
13334 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
13335 return;
13337 /* For local statics lookup proper context die. */
13338 if (TREE_STATIC (decl) && decl_function_context (decl))
13339 context_die = lookup_decl_die (DECL_CONTEXT (decl));
13341 /* If we are in terse mode, don't generate any DIEs to represent any
13342 variable declarations or definitions. */
13343 if (debug_info_level <= DINFO_LEVEL_TERSE)
13344 return;
13345 break;
13347 case NAMESPACE_DECL:
13348 if (debug_info_level <= DINFO_LEVEL_TERSE)
13349 return;
13350 if (lookup_decl_die (decl) != NULL)
13351 return;
13352 break;
13354 case TYPE_DECL:
13355 /* Don't emit stubs for types unless they are needed by other DIEs. */
13356 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
13357 return;
13359 /* Don't bother trying to generate any DIEs to represent any of the
13360 normal built-in types for the language we are compiling. */
13361 if (DECL_IS_BUILTIN (decl))
13363 /* OK, we need to generate one for `bool' so GDB knows what type
13364 comparisons have. */
13365 if (is_cxx ()
13366 && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
13367 && ! DECL_IGNORED_P (decl))
13368 modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
13370 return;
13373 /* If we are in terse mode, don't generate any DIEs for types. */
13374 if (debug_info_level <= DINFO_LEVEL_TERSE)
13375 return;
13377 /* If we're a function-scope tag, initially use a parent of NULL;
13378 this will be fixed up in decls_for_scope. */
13379 if (decl_function_context (decl))
13380 context_die = NULL;
13382 break;
13384 default:
13385 return;
13388 gen_decl_die (decl, context_die);
13391 /* Output a marker (i.e. a label) for the beginning of the generated code for
13392 a lexical block. */
13394 static void
13395 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
13396 unsigned int blocknum)
13398 switch_to_section (current_function_section ());
13399 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
13402 /* Output a marker (i.e. a label) for the end of the generated code for a
13403 lexical block. */
13405 static void
13406 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
13408 switch_to_section (current_function_section ());
13409 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
13412 /* Returns nonzero if it is appropriate not to emit any debugging
13413 information for BLOCK, because it doesn't contain any instructions.
13415 Don't allow this for blocks with nested functions or local classes
13416 as we would end up with orphans, and in the presence of scheduling
13417 we may end up calling them anyway. */
13419 static bool
13420 dwarf2out_ignore_block (tree block)
13422 tree decl;
13424 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
13425 if (TREE_CODE (decl) == FUNCTION_DECL
13426 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
13427 return 0;
13429 return 1;
13432 /* Lookup FILE_NAME (in the list of filenames that we know about here in
13433 dwarf2out.c) and return its "index". The index of each (known) filename is
13434 just a unique number which is associated with only that one filename. We
13435 need such numbers for the sake of generating labels (in the .debug_sfnames
13436 section) and references to those files numbers (in the .debug_srcinfo
13437 and.debug_macinfo sections). If the filename given as an argument is not
13438 found in our current list, add it to the list and assign it the next
13439 available unique index number. In order to speed up searches, we remember
13440 the index of the filename was looked up last. This handles the majority of
13441 all searches. */
13443 static unsigned
13444 lookup_filename (const char *file_name)
13446 size_t i, n;
13447 char *save_file_name;
13449 /* Check to see if the file name that was searched on the previous
13450 call matches this file name. If so, return the index. */
13451 if (file_table_last_lookup_index != 0)
13453 const char *last
13454 = VARRAY_CHAR_PTR (file_table, file_table_last_lookup_index);
13455 if (strcmp (file_name, last) == 0)
13456 return file_table_last_lookup_index;
13459 /* Didn't match the previous lookup, search the table. */
13460 n = VARRAY_ACTIVE_SIZE (file_table);
13461 for (i = 1; i < n; i++)
13462 if (strcmp (file_name, VARRAY_CHAR_PTR (file_table, i)) == 0)
13464 file_table_last_lookup_index = i;
13465 return i;
13468 /* Add the new entry to the end of the filename table. */
13469 file_table_last_lookup_index = n;
13470 save_file_name = (char *) ggc_strdup (file_name);
13471 VARRAY_PUSH_CHAR_PTR (file_table, save_file_name);
13472 VARRAY_PUSH_UINT (file_table_emitted, 0);
13474 /* If the assembler is emitting the file table, and we aren't eliminating
13475 unused debug types, then we must emit .file here. If we are eliminating
13476 unused debug types, then this will be done by the maybe_emit_file call in
13477 prune_unused_types_walk_attribs. */
13479 if (DWARF2_ASM_LINE_DEBUG_INFO && ! flag_eliminate_unused_debug_types)
13480 return maybe_emit_file (i);
13482 return i;
13485 /* If the assembler will construct the file table, then translate the compiler
13486 internal file table number into the assembler file table number, and emit
13487 a .file directive if we haven't already emitted one yet. The file table
13488 numbers are different because we prune debug info for unused variables and
13489 types, which may include filenames. */
13491 static int
13492 maybe_emit_file (int fileno)
13494 if (DWARF2_ASM_LINE_DEBUG_INFO && fileno > 0)
13496 if (!VARRAY_UINT (file_table_emitted, fileno))
13498 VARRAY_UINT (file_table_emitted, fileno) = ++emitcount;
13499 fprintf (asm_out_file, "\t.file %u ",
13500 VARRAY_UINT (file_table_emitted, fileno));
13501 output_quoted_string (asm_out_file,
13502 VARRAY_CHAR_PTR (file_table, fileno));
13503 fputc ('\n', asm_out_file);
13505 return VARRAY_UINT (file_table_emitted, fileno);
13507 else
13508 return fileno;
13511 /* Initialize the compiler internal file table. */
13513 static void
13514 init_file_table (void)
13516 /* Allocate the initial hunk of the file_table. */
13517 VARRAY_CHAR_PTR_INIT (file_table, 64, "file_table");
13518 VARRAY_UINT_INIT (file_table_emitted, 64, "file_table_emitted");
13520 /* Skip the first entry - file numbers begin at 1. */
13521 VARRAY_PUSH_CHAR_PTR (file_table, NULL);
13522 VARRAY_PUSH_UINT (file_table_emitted, 0);
13523 file_table_last_lookup_index = 0;
13526 /* Called by the final INSN scan whenever we see a var location. We
13527 use it to drop labels in the right places, and throw the location in
13528 our lookup table. */
13530 static void
13531 dwarf2out_var_location (rtx loc_note)
13533 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
13534 struct var_loc_node *newloc;
13535 rtx prev_insn;
13536 static rtx last_insn;
13537 static const char *last_label;
13538 tree decl;
13540 if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
13541 return;
13542 prev_insn = PREV_INSN (loc_note);
13544 newloc = ggc_alloc_cleared (sizeof (struct var_loc_node));
13545 /* If the insn we processed last time is the previous insn
13546 and it is also a var location note, use the label we emitted
13547 last time. */
13548 if (last_insn != NULL_RTX
13549 && last_insn == prev_insn
13550 && NOTE_P (prev_insn)
13551 && NOTE_LINE_NUMBER (prev_insn) == NOTE_INSN_VAR_LOCATION)
13553 newloc->label = last_label;
13555 else
13557 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
13558 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
13559 loclabel_num++;
13560 newloc->label = ggc_strdup (loclabel);
13562 newloc->var_loc_note = loc_note;
13563 newloc->next = NULL;
13565 if (cfun && in_cold_section_p)
13566 newloc->section_label = cfun->cold_section_label;
13567 else
13568 newloc->section_label = text_section_label;
13570 last_insn = loc_note;
13571 last_label = newloc->label;
13572 decl = NOTE_VAR_LOCATION_DECL (loc_note);
13573 if (DECL_DEBUG_EXPR_IS_FROM (decl) && DECL_DEBUG_EXPR (decl)
13574 && DECL_P (DECL_DEBUG_EXPR (decl)))
13575 decl = DECL_DEBUG_EXPR (decl);
13576 add_var_loc_to_decl (decl, newloc);
13579 /* We need to reset the locations at the beginning of each
13580 function. We can't do this in the end_function hook, because the
13581 declarations that use the locations won't have been output when
13582 that hook is called. Also compute have_multiple_function_sections here. */
13584 static void
13585 dwarf2out_begin_function (tree fun)
13587 htab_empty (decl_loc_table);
13589 if (function_section (fun) != text_section)
13590 have_multiple_function_sections = true;
13593 /* Output a label to mark the beginning of a source code line entry
13594 and record information relating to this source line, in
13595 'line_info_table' for later output of the .debug_line section. */
13597 static void
13598 dwarf2out_source_line (unsigned int line, const char *filename)
13600 if (debug_info_level >= DINFO_LEVEL_NORMAL
13601 && line != 0)
13603 switch_to_section (current_function_section ());
13605 /* If requested, emit something human-readable. */
13606 if (flag_debug_asm)
13607 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
13608 filename, line);
13610 if (DWARF2_ASM_LINE_DEBUG_INFO)
13612 unsigned file_num = lookup_filename (filename);
13614 file_num = maybe_emit_file (file_num);
13616 /* Emit the .loc directive understood by GNU as. */
13617 fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
13619 /* Indicate that line number info exists. */
13620 line_info_table_in_use++;
13622 else if (function_section (current_function_decl) != text_section)
13624 dw_separate_line_info_ref line_info;
13625 targetm.asm_out.internal_label (asm_out_file, SEPARATE_LINE_CODE_LABEL,
13626 separate_line_info_table_in_use);
13628 /* Expand the line info table if necessary. */
13629 if (separate_line_info_table_in_use
13630 == separate_line_info_table_allocated)
13632 separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
13633 separate_line_info_table
13634 = ggc_realloc (separate_line_info_table,
13635 separate_line_info_table_allocated
13636 * sizeof (dw_separate_line_info_entry));
13637 memset (separate_line_info_table
13638 + separate_line_info_table_in_use,
13640 (LINE_INFO_TABLE_INCREMENT
13641 * sizeof (dw_separate_line_info_entry)));
13644 /* Add the new entry at the end of the line_info_table. */
13645 line_info
13646 = &separate_line_info_table[separate_line_info_table_in_use++];
13647 line_info->dw_file_num = lookup_filename (filename);
13648 line_info->dw_line_num = line;
13649 line_info->function = current_function_funcdef_no;
13651 else
13653 dw_line_info_ref line_info;
13655 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
13656 line_info_table_in_use);
13658 /* Expand the line info table if necessary. */
13659 if (line_info_table_in_use == line_info_table_allocated)
13661 line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
13662 line_info_table
13663 = ggc_realloc (line_info_table,
13664 (line_info_table_allocated
13665 * sizeof (dw_line_info_entry)));
13666 memset (line_info_table + line_info_table_in_use, 0,
13667 LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
13670 /* Add the new entry at the end of the line_info_table. */
13671 line_info = &line_info_table[line_info_table_in_use++];
13672 line_info->dw_file_num = lookup_filename (filename);
13673 line_info->dw_line_num = line;
13678 /* Record the beginning of a new source file. */
13680 static void
13681 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
13683 if (flag_eliminate_dwarf2_dups)
13685 /* Record the beginning of the file for break_out_includes. */
13686 dw_die_ref bincl_die;
13688 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
13689 add_AT_string (bincl_die, DW_AT_name, filename);
13692 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13694 int fileno;
13696 switch_to_section (debug_macinfo_section);
13697 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
13698 dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
13699 lineno);
13701 fileno = maybe_emit_file (lookup_filename (filename));
13702 dw2_asm_output_data_uleb128 (fileno, "Filename we just started");
13706 /* Record the end of a source file. */
13708 static void
13709 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
13711 if (flag_eliminate_dwarf2_dups)
13712 /* Record the end of the file for break_out_includes. */
13713 new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
13715 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13717 switch_to_section (debug_macinfo_section);
13718 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
13722 /* Called from debug_define in toplev.c. The `buffer' parameter contains
13723 the tail part of the directive line, i.e. the part which is past the
13724 initial whitespace, #, whitespace, directive-name, whitespace part. */
13726 static void
13727 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
13728 const char *buffer ATTRIBUTE_UNUSED)
13730 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13732 switch_to_section (debug_macinfo_section);
13733 dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
13734 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
13735 dw2_asm_output_nstring (buffer, -1, "The macro");
13739 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
13740 the tail part of the directive line, i.e. the part which is past the
13741 initial whitespace, #, whitespace, directive-name, whitespace part. */
13743 static void
13744 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
13745 const char *buffer ATTRIBUTE_UNUSED)
13747 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13749 switch_to_section (debug_macinfo_section);
13750 dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
13751 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
13752 dw2_asm_output_nstring (buffer, -1, "The macro");
13756 /* Set up for Dwarf output at the start of compilation. */
13758 static void
13759 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
13761 init_file_table ();
13763 /* Allocate the decl_die_table. */
13764 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
13765 decl_die_table_eq, NULL);
13767 /* Allocate the decl_loc_table. */
13768 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
13769 decl_loc_table_eq, NULL);
13771 /* Allocate the initial hunk of the decl_scope_table. */
13772 decl_scope_table = VEC_alloc (tree, gc, 256);
13774 /* Allocate the initial hunk of the abbrev_die_table. */
13775 abbrev_die_table = ggc_alloc_cleared (ABBREV_DIE_TABLE_INCREMENT
13776 * sizeof (dw_die_ref));
13777 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
13778 /* Zero-th entry is allocated, but unused. */
13779 abbrev_die_table_in_use = 1;
13781 /* Allocate the initial hunk of the line_info_table. */
13782 line_info_table = ggc_alloc_cleared (LINE_INFO_TABLE_INCREMENT
13783 * sizeof (dw_line_info_entry));
13784 line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
13786 /* Zero-th entry is allocated, but unused. */
13787 line_info_table_in_use = 1;
13789 /* Generate the initial DIE for the .debug section. Note that the (string)
13790 value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
13791 will (typically) be a relative pathname and that this pathname should be
13792 taken as being relative to the directory from which the compiler was
13793 invoked when the given (base) source file was compiled. We will fill
13794 in this value in dwarf2out_finish. */
13795 comp_unit_die = gen_compile_unit_die (NULL);
13797 incomplete_types = VEC_alloc (tree, gc, 64);
13799 used_rtx_array = VEC_alloc (rtx, gc, 32);
13801 debug_info_section = get_section (DEBUG_INFO_SECTION,
13802 SECTION_DEBUG, NULL);
13803 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
13804 SECTION_DEBUG, NULL);
13805 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
13806 SECTION_DEBUG, NULL);
13807 debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
13808 SECTION_DEBUG, NULL);
13809 debug_line_section = get_section (DEBUG_LINE_SECTION,
13810 SECTION_DEBUG, NULL);
13811 debug_loc_section = get_section (DEBUG_LOC_SECTION,
13812 SECTION_DEBUG, NULL);
13813 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
13814 SECTION_DEBUG, NULL);
13815 debug_str_section = get_section (DEBUG_STR_SECTION,
13816 DEBUG_STR_SECTION_FLAGS, NULL);
13817 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
13818 SECTION_DEBUG, NULL);
13819 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
13820 SECTION_DEBUG, NULL);
13822 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
13823 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
13824 DEBUG_ABBREV_SECTION_LABEL, 0);
13825 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
13826 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
13827 COLD_TEXT_SECTION_LABEL, 0);
13828 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
13830 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
13831 DEBUG_INFO_SECTION_LABEL, 0);
13832 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
13833 DEBUG_LINE_SECTION_LABEL, 0);
13834 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
13835 DEBUG_RANGES_SECTION_LABEL, 0);
13836 switch_to_section (debug_abbrev_section);
13837 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
13838 switch_to_section (debug_info_section);
13839 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
13840 switch_to_section (debug_line_section);
13841 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
13843 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13845 switch_to_section (debug_macinfo_section);
13846 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
13847 DEBUG_MACINFO_SECTION_LABEL, 0);
13848 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
13851 switch_to_section (text_section);
13852 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
13853 if (flag_reorder_blocks_and_partition)
13855 switch_to_section (unlikely_text_section ());
13856 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
13860 /* A helper function for dwarf2out_finish called through
13861 ht_forall. Emit one queued .debug_str string. */
13863 static int
13864 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
13866 struct indirect_string_node *node = (struct indirect_string_node *) *h;
13868 if (node->form == DW_FORM_strp)
13870 switch_to_section (debug_str_section);
13871 ASM_OUTPUT_LABEL (asm_out_file, node->label);
13872 assemble_string (node->str, strlen (node->str) + 1);
13875 return 1;
13880 /* Clear the marks for a die and its children.
13881 Be cool if the mark isn't set. */
13883 static void
13884 prune_unmark_dies (dw_die_ref die)
13886 dw_die_ref c;
13887 die->die_mark = 0;
13888 for (c = die->die_child; c; c = c->die_sib)
13889 prune_unmark_dies (c);
13893 /* Given DIE that we're marking as used, find any other dies
13894 it references as attributes and mark them as used. */
13896 static void
13897 prune_unused_types_walk_attribs (dw_die_ref die)
13899 dw_attr_ref a;
13900 unsigned ix;
13902 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
13904 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
13906 /* A reference to another DIE.
13907 Make sure that it will get emitted. */
13908 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
13910 else if (a->dw_attr == DW_AT_decl_file || a->dw_attr == DW_AT_call_file)
13912 /* A reference to a file. Make sure the file name is emitted. */
13913 a->dw_attr_val.v.val_unsigned =
13914 maybe_emit_file (a->dw_attr_val.v.val_unsigned);
13920 /* Mark DIE as being used. If DOKIDS is true, then walk down
13921 to DIE's children. */
13923 static void
13924 prune_unused_types_mark (dw_die_ref die, int dokids)
13926 dw_die_ref c;
13928 if (die->die_mark == 0)
13930 /* We haven't done this node yet. Mark it as used. */
13931 die->die_mark = 1;
13933 /* We also have to mark its parents as used.
13934 (But we don't want to mark our parents' kids due to this.) */
13935 if (die->die_parent)
13936 prune_unused_types_mark (die->die_parent, 0);
13938 /* Mark any referenced nodes. */
13939 prune_unused_types_walk_attribs (die);
13941 /* If this node is a specification,
13942 also mark the definition, if it exists. */
13943 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
13944 prune_unused_types_mark (die->die_definition, 1);
13947 if (dokids && die->die_mark != 2)
13949 /* We need to walk the children, but haven't done so yet.
13950 Remember that we've walked the kids. */
13951 die->die_mark = 2;
13953 /* Walk them. */
13954 for (c = die->die_child; c; c = c->die_sib)
13956 /* If this is an array type, we need to make sure our
13957 kids get marked, even if they're types. */
13958 if (die->die_tag == DW_TAG_array_type)
13959 prune_unused_types_mark (c, 1);
13960 else
13961 prune_unused_types_walk (c);
13967 /* Walk the tree DIE and mark types that we actually use. */
13969 static void
13970 prune_unused_types_walk (dw_die_ref die)
13972 dw_die_ref c;
13974 /* Don't do anything if this node is already marked. */
13975 if (die->die_mark)
13976 return;
13978 switch (die->die_tag) {
13979 case DW_TAG_const_type:
13980 case DW_TAG_packed_type:
13981 case DW_TAG_pointer_type:
13982 case DW_TAG_reference_type:
13983 case DW_TAG_volatile_type:
13984 case DW_TAG_typedef:
13985 case DW_TAG_array_type:
13986 case DW_TAG_structure_type:
13987 case DW_TAG_union_type:
13988 case DW_TAG_class_type:
13989 case DW_TAG_friend:
13990 case DW_TAG_variant_part:
13991 case DW_TAG_enumeration_type:
13992 case DW_TAG_subroutine_type:
13993 case DW_TAG_string_type:
13994 case DW_TAG_set_type:
13995 case DW_TAG_subrange_type:
13996 case DW_TAG_ptr_to_member_type:
13997 case DW_TAG_file_type:
13998 if (die->die_perennial_p)
13999 break;
14001 /* It's a type node --- don't mark it. */
14002 return;
14004 default:
14005 /* Mark everything else. */
14006 break;
14009 die->die_mark = 1;
14011 /* Now, mark any dies referenced from here. */
14012 prune_unused_types_walk_attribs (die);
14014 /* Mark children. */
14015 for (c = die->die_child; c; c = c->die_sib)
14016 prune_unused_types_walk (c);
14020 /* Remove from the tree DIE any dies that aren't marked. */
14022 static void
14023 prune_unused_types_prune (dw_die_ref die)
14025 dw_die_ref c, p, n;
14027 gcc_assert (die->die_mark);
14029 p = NULL;
14030 for (c = die->die_child; c; c = n)
14032 n = c->die_sib;
14033 if (c->die_mark)
14035 prune_unused_types_prune (c);
14036 p = c;
14038 else
14040 if (p)
14041 p->die_sib = n;
14042 else
14043 die->die_child = n;
14044 free_die (c);
14050 /* Remove dies representing declarations that we never use. */
14052 static void
14053 prune_unused_types (void)
14055 unsigned int i;
14056 limbo_die_node *node;
14058 /* Clear all the marks. */
14059 prune_unmark_dies (comp_unit_die);
14060 for (node = limbo_die_list; node; node = node->next)
14061 prune_unmark_dies (node->die);
14063 /* Set the mark on nodes that are actually used. */
14064 prune_unused_types_walk (comp_unit_die);
14065 for (node = limbo_die_list; node; node = node->next)
14066 prune_unused_types_walk (node->die);
14068 /* Also set the mark on nodes referenced from the
14069 pubname_table or arange_table. */
14070 for (i = 0; i < pubname_table_in_use; i++)
14071 prune_unused_types_mark (pubname_table[i].die, 1);
14072 for (i = 0; i < arange_table_in_use; i++)
14073 prune_unused_types_mark (arange_table[i], 1);
14075 /* Get rid of nodes that aren't marked. */
14076 prune_unused_types_prune (comp_unit_die);
14077 for (node = limbo_die_list; node; node = node->next)
14078 prune_unused_types_prune (node->die);
14080 /* Leave the marks clear. */
14081 prune_unmark_dies (comp_unit_die);
14082 for (node = limbo_die_list; node; node = node->next)
14083 prune_unmark_dies (node->die);
14086 /* Output stuff that dwarf requires at the end of every file,
14087 and generate the DWARF-2 debugging info. */
14089 static void
14090 dwarf2out_finish (const char *filename)
14092 limbo_die_node *node, *next_node;
14093 dw_die_ref die = 0;
14095 /* Add the name for the main input file now. We delayed this from
14096 dwarf2out_init to avoid complications with PCH. */
14097 add_name_attribute (comp_unit_die, filename);
14098 if (filename[0] != DIR_SEPARATOR)
14099 add_comp_dir_attribute (comp_unit_die);
14100 else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
14102 size_t i;
14103 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
14104 if (VARRAY_CHAR_PTR (file_table, i)[0] != DIR_SEPARATOR
14105 /* Don't add cwd for <built-in>. */
14106 && VARRAY_CHAR_PTR (file_table, i)[0] != '<')
14108 add_comp_dir_attribute (comp_unit_die);
14109 break;
14113 /* Traverse the limbo die list, and add parent/child links. The only
14114 dies without parents that should be here are concrete instances of
14115 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
14116 For concrete instances, we can get the parent die from the abstract
14117 instance. */
14118 for (node = limbo_die_list; node; node = next_node)
14120 next_node = node->next;
14121 die = node->die;
14123 if (die->die_parent == NULL)
14125 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
14127 if (origin)
14128 add_child_die (origin->die_parent, die);
14129 else if (die == comp_unit_die)
14131 else if (errorcount > 0 || sorrycount > 0)
14132 /* It's OK to be confused by errors in the input. */
14133 add_child_die (comp_unit_die, die);
14134 else
14136 /* In certain situations, the lexical block containing a
14137 nested function can be optimized away, which results
14138 in the nested function die being orphaned. Likewise
14139 with the return type of that nested function. Force
14140 this to be a child of the containing function.
14142 It may happen that even the containing function got fully
14143 inlined and optimized out. In that case we are lost and
14144 assign the empty child. This should not be big issue as
14145 the function is likely unreachable too. */
14146 tree context = NULL_TREE;
14148 gcc_assert (node->created_for);
14150 if (DECL_P (node->created_for))
14151 context = DECL_CONTEXT (node->created_for);
14152 else if (TYPE_P (node->created_for))
14153 context = TYPE_CONTEXT (node->created_for);
14155 gcc_assert (context && TREE_CODE (context) == FUNCTION_DECL);
14157 origin = lookup_decl_die (context);
14158 if (origin)
14159 add_child_die (origin, die);
14160 else
14161 add_child_die (comp_unit_die, die);
14166 limbo_die_list = NULL;
14168 /* Walk through the list of incomplete types again, trying once more to
14169 emit full debugging info for them. */
14170 retry_incomplete_types ();
14172 /* We need to reverse all the dies before break_out_includes, or
14173 we'll see the end of an include file before the beginning. */
14174 reverse_all_dies (comp_unit_die);
14176 if (flag_eliminate_unused_debug_types)
14177 prune_unused_types ();
14179 /* Generate separate CUs for each of the include files we've seen.
14180 They will go into limbo_die_list. */
14181 if (flag_eliminate_dwarf2_dups)
14182 break_out_includes (comp_unit_die);
14184 /* Traverse the DIE's and add add sibling attributes to those DIE's
14185 that have children. */
14186 add_sibling_attributes (comp_unit_die);
14187 for (node = limbo_die_list; node; node = node->next)
14188 add_sibling_attributes (node->die);
14190 /* Output a terminator label for the .text section. */
14191 switch_to_section (text_section);
14192 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
14193 if (flag_reorder_blocks_and_partition)
14195 switch_to_section (unlikely_text_section ());
14196 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
14199 /* Output the source line correspondence table. We must do this
14200 even if there is no line information. Otherwise, on an empty
14201 translation unit, we will generate a present, but empty,
14202 .debug_info section. IRIX 6.5 `nm' will then complain when
14203 examining the file. */
14204 if (! DWARF2_ASM_LINE_DEBUG_INFO)
14206 switch_to_section (debug_line_section);
14207 output_line_info ();
14210 /* We can only use the low/high_pc attributes if all of the code was
14211 in .text. */
14212 if (!have_multiple_function_sections)
14214 add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
14215 add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
14218 /* If it wasn't, we need to give .debug_loc and .debug_ranges an appropriate
14219 "base address". Use zero so that these addresses become absolute. */
14220 else if (have_location_lists || ranges_table_in_use)
14221 add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
14223 /* Output location list section if necessary. */
14224 if (have_location_lists)
14226 /* Output the location lists info. */
14227 switch_to_section (debug_loc_section);
14228 ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
14229 DEBUG_LOC_SECTION_LABEL, 0);
14230 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
14231 output_location_lists (die);
14234 if (debug_info_level >= DINFO_LEVEL_NORMAL)
14235 add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
14236 debug_line_section_label);
14238 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14239 add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
14241 /* Output all of the compilation units. We put the main one last so that
14242 the offsets are available to output_pubnames. */
14243 for (node = limbo_die_list; node; node = node->next)
14244 output_comp_unit (node->die, 0);
14246 output_comp_unit (comp_unit_die, 0);
14248 /* Output the abbreviation table. */
14249 switch_to_section (debug_abbrev_section);
14250 output_abbrev_section ();
14252 /* Output public names table if necessary. */
14253 if (pubname_table_in_use)
14255 switch_to_section (debug_pubnames_section);
14256 output_pubnames ();
14259 /* Output the address range information. We only put functions in the arange
14260 table, so don't write it out if we don't have any. */
14261 if (fde_table_in_use)
14263 switch_to_section (debug_aranges_section);
14264 output_aranges ();
14267 /* Output ranges section if necessary. */
14268 if (ranges_table_in_use)
14270 switch_to_section (debug_ranges_section);
14271 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
14272 output_ranges ();
14275 /* Have to end the macro section. */
14276 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14278 switch_to_section (debug_macinfo_section);
14279 dw2_asm_output_data (1, 0, "End compilation unit");
14282 /* If we emitted any DW_FORM_strp form attribute, output the string
14283 table too. */
14284 if (debug_str_hash)
14285 htab_traverse (debug_str_hash, output_indirect_string, NULL);
14287 #else
14289 /* This should never be used, but its address is needed for comparisons. */
14290 const struct gcc_debug_hooks dwarf2_debug_hooks;
14292 #endif /* DWARF2_DEBUGGING_INFO */
14294 #include "gt-dwarf2out.h"