Makefile.in: Add dummy "install-info" target.
[official-gcc.git] / gcc / dwarf2out.c
blob476f6ebf794c70bbdd531eae318397dc7b7214f1
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
4 Contributed by Gary Funck (gary@intrepid.com).
5 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
6 Extensively modified by Jason Merrill (jason@cygnus.com).
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 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_pubtypes_section;
160 static GTY(()) section *debug_str_section;
161 static GTY(()) section *debug_ranges_section;
162 static GTY(()) section *debug_frame_section;
164 /* How to start an assembler comment. */
165 #ifndef ASM_COMMENT_START
166 #define ASM_COMMENT_START ";#"
167 #endif
169 typedef struct dw_cfi_struct *dw_cfi_ref;
170 typedef struct dw_fde_struct *dw_fde_ref;
171 typedef union dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
173 /* Call frames are described using a sequence of Call Frame
174 Information instructions. The register number, offset
175 and address fields are provided as possible operands;
176 their use is selected by the opcode field. */
178 enum dw_cfi_oprnd_type {
179 dw_cfi_oprnd_unused,
180 dw_cfi_oprnd_reg_num,
181 dw_cfi_oprnd_offset,
182 dw_cfi_oprnd_addr,
183 dw_cfi_oprnd_loc
186 typedef union dw_cfi_oprnd_struct GTY(())
188 unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
189 HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
190 const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
191 struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
193 dw_cfi_oprnd;
195 typedef struct dw_cfi_struct GTY(())
197 dw_cfi_ref dw_cfi_next;
198 enum dwarf_call_frame_info dw_cfi_opc;
199 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
200 dw_cfi_oprnd1;
201 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
202 dw_cfi_oprnd2;
204 dw_cfi_node;
206 /* This is how we define the location of the CFA. We use to handle it
207 as REG + OFFSET all the time, but now it can be more complex.
208 It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
209 Instead of passing around REG and OFFSET, we pass a copy
210 of this structure. */
211 typedef struct cfa_loc GTY(())
213 HOST_WIDE_INT offset;
214 HOST_WIDE_INT base_offset;
215 unsigned int reg;
216 int indirect; /* 1 if CFA is accessed via a dereference. */
217 } dw_cfa_location;
219 /* All call frame descriptions (FDE's) in the GCC generated DWARF
220 refer to a single Common Information Entry (CIE), defined at
221 the beginning of the .debug_frame section. This use of a single
222 CIE obviates the need to keep track of multiple CIE's
223 in the DWARF generation routines below. */
225 typedef struct dw_fde_struct GTY(())
227 tree decl;
228 const char *dw_fde_begin;
229 const char *dw_fde_current_label;
230 const char *dw_fde_end;
231 const char *dw_fde_hot_section_label;
232 const char *dw_fde_hot_section_end_label;
233 const char *dw_fde_unlikely_section_label;
234 const char *dw_fde_unlikely_section_end_label;
235 bool dw_fde_switched_sections;
236 dw_cfi_ref dw_fde_cfi;
237 unsigned funcdef_number;
238 unsigned all_throwers_are_sibcalls : 1;
239 unsigned nothrow : 1;
240 unsigned uses_eh_lsda : 1;
242 dw_fde_node;
244 /* Maximum size (in bytes) of an artificially generated label. */
245 #define MAX_ARTIFICIAL_LABEL_BYTES 30
247 /* The size of addresses as they appear in the Dwarf 2 data.
248 Some architectures use word addresses to refer to code locations,
249 but Dwarf 2 info always uses byte addresses. On such machines,
250 Dwarf 2 addresses need to be larger than the architecture's
251 pointers. */
252 #ifndef DWARF2_ADDR_SIZE
253 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
254 #endif
256 /* The size in bytes of a DWARF field indicating an offset or length
257 relative to a debug info section, specified to be 4 bytes in the
258 DWARF-2 specification. The SGI/MIPS ABI defines it to be the same
259 as PTR_SIZE. */
261 #ifndef DWARF_OFFSET_SIZE
262 #define DWARF_OFFSET_SIZE 4
263 #endif
265 /* According to the (draft) DWARF 3 specification, the initial length
266 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
267 bytes are 0xffffffff, followed by the length stored in the next 8
268 bytes.
270 However, the SGI/MIPS ABI uses an initial length which is equal to
271 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
273 #ifndef DWARF_INITIAL_LENGTH_SIZE
274 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
275 #endif
277 #define DWARF_VERSION 2
279 /* Round SIZE up to the nearest BOUNDARY. */
280 #define DWARF_ROUND(SIZE,BOUNDARY) \
281 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
283 /* Offsets recorded in opcodes are a multiple of this alignment factor. */
284 #ifndef DWARF_CIE_DATA_ALIGNMENT
285 #ifdef STACK_GROWS_DOWNWARD
286 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
287 #else
288 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
289 #endif
290 #endif
292 /* CIE identifier. */
293 #if HOST_BITS_PER_WIDE_INT >= 64
294 #define DWARF_CIE_ID \
295 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
296 #else
297 #define DWARF_CIE_ID DW_CIE_ID
298 #endif
300 /* A pointer to the base of a table that contains frame description
301 information for each routine. */
302 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
304 /* Number of elements currently allocated for fde_table. */
305 static GTY(()) unsigned fde_table_allocated;
307 /* Number of elements in fde_table currently in use. */
308 static GTY(()) unsigned fde_table_in_use;
310 /* Size (in elements) of increments by which we may expand the
311 fde_table. */
312 #define FDE_TABLE_INCREMENT 256
314 /* A list of call frame insns for the CIE. */
315 static GTY(()) dw_cfi_ref cie_cfi_head;
317 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
318 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
319 attribute that accelerates the lookup of the FDE associated
320 with the subprogram. This variable holds the table index of the FDE
321 associated with the current function (body) definition. */
322 static unsigned current_funcdef_fde;
323 #endif
325 struct indirect_string_node GTY(())
327 const char *str;
328 unsigned int refcount;
329 unsigned int form;
330 char *label;
333 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
335 static GTY(()) int dw2_string_counter;
336 static GTY(()) unsigned long dwarf2out_cfi_label_num;
338 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
340 /* Forward declarations for functions defined in this file. */
342 static char *stripattributes (const char *);
343 static const char *dwarf_cfi_name (unsigned);
344 static dw_cfi_ref new_cfi (void);
345 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
346 static void add_fde_cfi (const char *, dw_cfi_ref);
347 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *);
348 static void lookup_cfa (dw_cfa_location *);
349 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
350 static void initial_return_save (rtx);
351 static HOST_WIDE_INT stack_adjust_offset (rtx);
352 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
353 static void output_call_frame_info (int);
354 static void dwarf2out_stack_adjust (rtx, bool);
355 static void flush_queued_reg_saves (void);
356 static bool clobbers_queued_reg_save (rtx);
357 static void dwarf2out_frame_debug_expr (rtx, const char *);
359 /* Support for complex CFA locations. */
360 static void output_cfa_loc (dw_cfi_ref);
361 static void get_cfa_from_loc_descr (dw_cfa_location *,
362 struct dw_loc_descr_struct *);
363 static struct dw_loc_descr_struct *build_cfa_loc
364 (dw_cfa_location *, HOST_WIDE_INT);
365 static void def_cfa_1 (const char *, dw_cfa_location *);
367 /* How to start an assembler comment. */
368 #ifndef ASM_COMMENT_START
369 #define ASM_COMMENT_START ";#"
370 #endif
372 /* Data and reference forms for relocatable data. */
373 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
374 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
376 #ifndef DEBUG_FRAME_SECTION
377 #define DEBUG_FRAME_SECTION ".debug_frame"
378 #endif
380 #ifndef FUNC_BEGIN_LABEL
381 #define FUNC_BEGIN_LABEL "LFB"
382 #endif
384 #ifndef FUNC_END_LABEL
385 #define FUNC_END_LABEL "LFE"
386 #endif
388 #ifndef FRAME_BEGIN_LABEL
389 #define FRAME_BEGIN_LABEL "Lframe"
390 #endif
391 #define CIE_AFTER_SIZE_LABEL "LSCIE"
392 #define CIE_END_LABEL "LECIE"
393 #define FDE_LABEL "LSFDE"
394 #define FDE_AFTER_SIZE_LABEL "LASFDE"
395 #define FDE_END_LABEL "LEFDE"
396 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
397 #define LINE_NUMBER_END_LABEL "LELT"
398 #define LN_PROLOG_AS_LABEL "LASLTP"
399 #define LN_PROLOG_END_LABEL "LELTP"
400 #define DIE_LABEL_PREFIX "DW"
402 /* The DWARF 2 CFA column which tracks the return address. Normally this
403 is the column for PC, or the first column after all of the hard
404 registers. */
405 #ifndef DWARF_FRAME_RETURN_COLUMN
406 #ifdef PC_REGNUM
407 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGNUM (PC_REGNUM)
408 #else
409 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGISTERS
410 #endif
411 #endif
413 /* The mapping from gcc register number to DWARF 2 CFA column number. By
414 default, we just provide columns for all registers. */
415 #ifndef DWARF_FRAME_REGNUM
416 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
417 #endif
419 /* Hook used by __throw. */
422 expand_builtin_dwarf_sp_column (void)
424 unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
425 return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
428 /* Return a pointer to a copy of the section string name S with all
429 attributes stripped off, and an asterisk prepended (for assemble_name). */
431 static inline char *
432 stripattributes (const char *s)
434 char *stripped = XNEWVEC (char, strlen (s) + 2);
435 char *p = stripped;
437 *p++ = '*';
439 while (*s && *s != ',')
440 *p++ = *s++;
442 *p = '\0';
443 return stripped;
446 /* MEM is a memory reference for the register size table, each element of
447 which has mode MODE. Initialize column C as a return address column. */
449 static void
450 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
452 HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
453 HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
454 emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
457 /* Generate code to initialize the register size table. */
459 void
460 expand_builtin_init_dwarf_reg_sizes (tree address)
462 unsigned int i;
463 enum machine_mode mode = TYPE_MODE (char_type_node);
464 rtx addr = expand_normal (address);
465 rtx mem = gen_rtx_MEM (BLKmode, addr);
466 bool wrote_return_column = false;
468 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
470 int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
472 if (rnum < DWARF_FRAME_REGISTERS)
474 HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
475 enum machine_mode save_mode = reg_raw_mode[i];
476 HOST_WIDE_INT size;
478 if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
479 save_mode = choose_hard_reg_mode (i, 1, true);
480 if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
482 if (save_mode == VOIDmode)
483 continue;
484 wrote_return_column = true;
486 size = GET_MODE_SIZE (save_mode);
487 if (offset < 0)
488 continue;
490 emit_move_insn (adjust_address (mem, mode, offset),
491 gen_int_mode (size, mode));
495 if (!wrote_return_column)
496 init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
498 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
499 init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
500 #endif
502 targetm.init_dwarf_reg_sizes_extra (address);
505 /* Convert a DWARF call frame info. operation to its string name */
507 static const char *
508 dwarf_cfi_name (unsigned int cfi_opc)
510 switch (cfi_opc)
512 case DW_CFA_advance_loc:
513 return "DW_CFA_advance_loc";
514 case DW_CFA_offset:
515 return "DW_CFA_offset";
516 case DW_CFA_restore:
517 return "DW_CFA_restore";
518 case DW_CFA_nop:
519 return "DW_CFA_nop";
520 case DW_CFA_set_loc:
521 return "DW_CFA_set_loc";
522 case DW_CFA_advance_loc1:
523 return "DW_CFA_advance_loc1";
524 case DW_CFA_advance_loc2:
525 return "DW_CFA_advance_loc2";
526 case DW_CFA_advance_loc4:
527 return "DW_CFA_advance_loc4";
528 case DW_CFA_offset_extended:
529 return "DW_CFA_offset_extended";
530 case DW_CFA_restore_extended:
531 return "DW_CFA_restore_extended";
532 case DW_CFA_undefined:
533 return "DW_CFA_undefined";
534 case DW_CFA_same_value:
535 return "DW_CFA_same_value";
536 case DW_CFA_register:
537 return "DW_CFA_register";
538 case DW_CFA_remember_state:
539 return "DW_CFA_remember_state";
540 case DW_CFA_restore_state:
541 return "DW_CFA_restore_state";
542 case DW_CFA_def_cfa:
543 return "DW_CFA_def_cfa";
544 case DW_CFA_def_cfa_register:
545 return "DW_CFA_def_cfa_register";
546 case DW_CFA_def_cfa_offset:
547 return "DW_CFA_def_cfa_offset";
549 /* DWARF 3 */
550 case DW_CFA_def_cfa_expression:
551 return "DW_CFA_def_cfa_expression";
552 case DW_CFA_expression:
553 return "DW_CFA_expression";
554 case DW_CFA_offset_extended_sf:
555 return "DW_CFA_offset_extended_sf";
556 case DW_CFA_def_cfa_sf:
557 return "DW_CFA_def_cfa_sf";
558 case DW_CFA_def_cfa_offset_sf:
559 return "DW_CFA_def_cfa_offset_sf";
561 /* SGI/MIPS specific */
562 case DW_CFA_MIPS_advance_loc8:
563 return "DW_CFA_MIPS_advance_loc8";
565 /* GNU extensions */
566 case DW_CFA_GNU_window_save:
567 return "DW_CFA_GNU_window_save";
568 case DW_CFA_GNU_args_size:
569 return "DW_CFA_GNU_args_size";
570 case DW_CFA_GNU_negative_offset_extended:
571 return "DW_CFA_GNU_negative_offset_extended";
573 default:
574 return "DW_CFA_<unknown>";
578 /* Return a pointer to a newly allocated Call Frame Instruction. */
580 static inline dw_cfi_ref
581 new_cfi (void)
583 dw_cfi_ref cfi = ggc_alloc (sizeof (dw_cfi_node));
585 cfi->dw_cfi_next = NULL;
586 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
587 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
589 return cfi;
592 /* Add a Call Frame Instruction to list of instructions. */
594 static inline void
595 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
597 dw_cfi_ref *p;
599 /* Find the end of the chain. */
600 for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
603 *p = cfi;
606 /* Generate a new label for the CFI info to refer to. */
608 char *
609 dwarf2out_cfi_label (void)
611 static char label[20];
613 ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
614 ASM_OUTPUT_LABEL (asm_out_file, label);
615 return label;
618 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
619 or to the CIE if LABEL is NULL. */
621 static void
622 add_fde_cfi (const char *label, dw_cfi_ref cfi)
624 if (label)
626 dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
628 if (*label == 0)
629 label = dwarf2out_cfi_label ();
631 if (fde->dw_fde_current_label == NULL
632 || strcmp (label, fde->dw_fde_current_label) != 0)
634 dw_cfi_ref xcfi;
636 label = xstrdup (label);
638 /* Set the location counter to the new label. */
639 xcfi = new_cfi ();
640 /* If we have a current label, advance from there, otherwise
641 set the location directly using set_loc. */
642 xcfi->dw_cfi_opc = fde->dw_fde_current_label
643 ? DW_CFA_advance_loc4
644 : DW_CFA_set_loc;
645 xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
646 add_cfi (&fde->dw_fde_cfi, xcfi);
648 fde->dw_fde_current_label = label;
651 add_cfi (&fde->dw_fde_cfi, cfi);
654 else
655 add_cfi (&cie_cfi_head, cfi);
658 /* Subroutine of lookup_cfa. */
660 static void
661 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc)
663 switch (cfi->dw_cfi_opc)
665 case DW_CFA_def_cfa_offset:
666 loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
667 break;
668 case DW_CFA_def_cfa_offset_sf:
669 loc->offset
670 = cfi->dw_cfi_oprnd1.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
671 break;
672 case DW_CFA_def_cfa_register:
673 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
674 break;
675 case DW_CFA_def_cfa:
676 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
677 loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
678 break;
679 case DW_CFA_def_cfa_sf:
680 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
681 loc->offset
682 = cfi->dw_cfi_oprnd2.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
683 break;
684 case DW_CFA_def_cfa_expression:
685 get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
686 break;
687 default:
688 break;
692 /* Find the previous value for the CFA. */
694 static void
695 lookup_cfa (dw_cfa_location *loc)
697 dw_cfi_ref cfi;
699 loc->reg = INVALID_REGNUM;
700 loc->offset = 0;
701 loc->indirect = 0;
702 loc->base_offset = 0;
704 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
705 lookup_cfa_1 (cfi, loc);
707 if (fde_table_in_use)
709 dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
710 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
711 lookup_cfa_1 (cfi, loc);
715 /* The current rule for calculating the DWARF2 canonical frame address. */
716 static dw_cfa_location cfa;
718 /* The register used for saving registers to the stack, and its offset
719 from the CFA. */
720 static dw_cfa_location cfa_store;
722 /* The running total of the size of arguments pushed onto the stack. */
723 static HOST_WIDE_INT args_size;
725 /* The last args_size we actually output. */
726 static HOST_WIDE_INT old_args_size;
728 /* Entry point to update the canonical frame address (CFA).
729 LABEL is passed to add_fde_cfi. The value of CFA is now to be
730 calculated from REG+OFFSET. */
732 void
733 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
735 dw_cfa_location loc;
736 loc.indirect = 0;
737 loc.base_offset = 0;
738 loc.reg = reg;
739 loc.offset = offset;
740 def_cfa_1 (label, &loc);
743 /* Determine if two dw_cfa_location structures define the same data. */
745 static bool
746 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
748 return (loc1->reg == loc2->reg
749 && loc1->offset == loc2->offset
750 && loc1->indirect == loc2->indirect
751 && (loc1->indirect == 0
752 || loc1->base_offset == loc2->base_offset));
755 /* This routine does the actual work. The CFA is now calculated from
756 the dw_cfa_location structure. */
758 static void
759 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
761 dw_cfi_ref cfi;
762 dw_cfa_location old_cfa, loc;
764 cfa = *loc_p;
765 loc = *loc_p;
767 if (cfa_store.reg == loc.reg && loc.indirect == 0)
768 cfa_store.offset = loc.offset;
770 loc.reg = DWARF_FRAME_REGNUM (loc.reg);
771 lookup_cfa (&old_cfa);
773 /* If nothing changed, no need to issue any call frame instructions. */
774 if (cfa_equal_p (&loc, &old_cfa))
775 return;
777 cfi = new_cfi ();
779 if (loc.reg == old_cfa.reg && !loc.indirect)
781 /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
782 the CFA register did not change but the offset did. */
783 if (loc.offset < 0)
785 HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
786 gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
788 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
789 cfi->dw_cfi_oprnd1.dw_cfi_offset = f_offset;
791 else
793 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
794 cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
798 #ifndef MIPS_DEBUGGING_INFO /* SGI dbx thinks this means no offset. */
799 else if (loc.offset == old_cfa.offset
800 && old_cfa.reg != INVALID_REGNUM
801 && !loc.indirect)
803 /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
804 indicating the CFA register has changed to <register> but the
805 offset has not changed. */
806 cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
807 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
809 #endif
811 else if (loc.indirect == 0)
813 /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
814 indicating the CFA register has changed to <register> with
815 the specified offset. */
816 if (loc.offset < 0)
818 HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
819 gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
821 cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
822 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
823 cfi->dw_cfi_oprnd2.dw_cfi_offset = f_offset;
825 else
827 cfi->dw_cfi_opc = DW_CFA_def_cfa;
828 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
829 cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
832 else
834 /* Construct a DW_CFA_def_cfa_expression instruction to
835 calculate the CFA using a full location expression since no
836 register-offset pair is available. */
837 struct dw_loc_descr_struct *loc_list;
839 cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
840 loc_list = build_cfa_loc (&loc, 0);
841 cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
844 add_fde_cfi (label, cfi);
847 /* Add the CFI for saving a register. REG is the CFA column number.
848 LABEL is passed to add_fde_cfi.
849 If SREG is -1, the register is saved at OFFSET from the CFA;
850 otherwise it is saved in SREG. */
852 static void
853 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
855 dw_cfi_ref cfi = new_cfi ();
857 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
859 if (sreg == INVALID_REGNUM)
861 if (reg & ~0x3f)
862 /* The register number won't fit in 6 bits, so we have to use
863 the long form. */
864 cfi->dw_cfi_opc = DW_CFA_offset_extended;
865 else
866 cfi->dw_cfi_opc = DW_CFA_offset;
868 #ifdef ENABLE_CHECKING
870 /* If we get an offset that is not a multiple of
871 DWARF_CIE_DATA_ALIGNMENT, there is either a bug in the
872 definition of DWARF_CIE_DATA_ALIGNMENT, or a bug in the machine
873 description. */
874 HOST_WIDE_INT check_offset = offset / DWARF_CIE_DATA_ALIGNMENT;
876 gcc_assert (check_offset * DWARF_CIE_DATA_ALIGNMENT == offset);
878 #endif
879 offset /= DWARF_CIE_DATA_ALIGNMENT;
880 if (offset < 0)
881 cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
883 cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
885 else if (sreg == reg)
886 cfi->dw_cfi_opc = DW_CFA_same_value;
887 else
889 cfi->dw_cfi_opc = DW_CFA_register;
890 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
893 add_fde_cfi (label, cfi);
896 /* Add the CFI for saving a register window. LABEL is passed to reg_save.
897 This CFI tells the unwinder that it needs to restore the window registers
898 from the previous frame's window save area.
900 ??? Perhaps we should note in the CIE where windows are saved (instead of
901 assuming 0(cfa)) and what registers are in the window. */
903 void
904 dwarf2out_window_save (const char *label)
906 dw_cfi_ref cfi = new_cfi ();
908 cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
909 add_fde_cfi (label, cfi);
912 /* Add a CFI to update the running total of the size of arguments
913 pushed onto the stack. */
915 void
916 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
918 dw_cfi_ref cfi;
920 if (size == old_args_size)
921 return;
923 old_args_size = size;
925 cfi = new_cfi ();
926 cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
927 cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
928 add_fde_cfi (label, cfi);
931 /* Entry point for saving a register to the stack. REG is the GCC register
932 number. LABEL and OFFSET are passed to reg_save. */
934 void
935 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
937 reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
940 /* Entry point for saving the return address in the stack.
941 LABEL and OFFSET are passed to reg_save. */
943 void
944 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
946 reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
949 /* Entry point for saving the return address in a register.
950 LABEL and SREG are passed to reg_save. */
952 void
953 dwarf2out_return_reg (const char *label, unsigned int sreg)
955 reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
958 /* Record the initial position of the return address. RTL is
959 INCOMING_RETURN_ADDR_RTX. */
961 static void
962 initial_return_save (rtx rtl)
964 unsigned int reg = INVALID_REGNUM;
965 HOST_WIDE_INT offset = 0;
967 switch (GET_CODE (rtl))
969 case REG:
970 /* RA is in a register. */
971 reg = DWARF_FRAME_REGNUM (REGNO (rtl));
972 break;
974 case MEM:
975 /* RA is on the stack. */
976 rtl = XEXP (rtl, 0);
977 switch (GET_CODE (rtl))
979 case REG:
980 gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
981 offset = 0;
982 break;
984 case PLUS:
985 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
986 offset = INTVAL (XEXP (rtl, 1));
987 break;
989 case MINUS:
990 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
991 offset = -INTVAL (XEXP (rtl, 1));
992 break;
994 default:
995 gcc_unreachable ();
998 break;
1000 case PLUS:
1001 /* The return address is at some offset from any value we can
1002 actually load. For instance, on the SPARC it is in %i7+8. Just
1003 ignore the offset for now; it doesn't matter for unwinding frames. */
1004 gcc_assert (GET_CODE (XEXP (rtl, 1)) == CONST_INT);
1005 initial_return_save (XEXP (rtl, 0));
1006 return;
1008 default:
1009 gcc_unreachable ();
1012 if (reg != DWARF_FRAME_RETURN_COLUMN)
1013 reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1016 /* Given a SET, calculate the amount of stack adjustment it
1017 contains. */
1019 static HOST_WIDE_INT
1020 stack_adjust_offset (rtx pattern)
1022 rtx src = SET_SRC (pattern);
1023 rtx dest = SET_DEST (pattern);
1024 HOST_WIDE_INT offset = 0;
1025 enum rtx_code code;
1027 if (dest == stack_pointer_rtx)
1029 /* (set (reg sp) (plus (reg sp) (const_int))) */
1030 code = GET_CODE (src);
1031 if (! (code == PLUS || code == MINUS)
1032 || XEXP (src, 0) != stack_pointer_rtx
1033 || GET_CODE (XEXP (src, 1)) != CONST_INT)
1034 return 0;
1036 offset = INTVAL (XEXP (src, 1));
1037 if (code == PLUS)
1038 offset = -offset;
1040 else if (MEM_P (dest))
1042 /* (set (mem (pre_dec (reg sp))) (foo)) */
1043 src = XEXP (dest, 0);
1044 code = GET_CODE (src);
1046 switch (code)
1048 case PRE_MODIFY:
1049 case POST_MODIFY:
1050 if (XEXP (src, 0) == stack_pointer_rtx)
1052 rtx val = XEXP (XEXP (src, 1), 1);
1053 /* We handle only adjustments by constant amount. */
1054 gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1055 && GET_CODE (val) == CONST_INT);
1056 offset = -INTVAL (val);
1057 break;
1059 return 0;
1061 case PRE_DEC:
1062 case POST_DEC:
1063 if (XEXP (src, 0) == stack_pointer_rtx)
1065 offset = GET_MODE_SIZE (GET_MODE (dest));
1066 break;
1068 return 0;
1070 case PRE_INC:
1071 case POST_INC:
1072 if (XEXP (src, 0) == stack_pointer_rtx)
1074 offset = -GET_MODE_SIZE (GET_MODE (dest));
1075 break;
1077 return 0;
1079 default:
1080 return 0;
1083 else
1084 return 0;
1086 return offset;
1089 /* Check INSN to see if it looks like a push or a stack adjustment, and
1090 make a note of it if it does. EH uses this information to find out how
1091 much extra space it needs to pop off the stack. */
1093 static void
1094 dwarf2out_stack_adjust (rtx insn, bool after_p)
1096 HOST_WIDE_INT offset;
1097 const char *label;
1098 int i;
1100 /* Don't handle epilogues at all. Certainly it would be wrong to do so
1101 with this function. Proper support would require all frame-related
1102 insns to be marked, and to be able to handle saving state around
1103 epilogues textually in the middle of the function. */
1104 if (prologue_epilogue_contains (insn) || sibcall_epilogue_contains (insn))
1105 return;
1107 /* If only calls can throw, and we have a frame pointer,
1108 save up adjustments until we see the CALL_INSN. */
1109 if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1111 if (CALL_P (insn) && !after_p)
1113 /* Extract the size of the args from the CALL rtx itself. */
1114 insn = PATTERN (insn);
1115 if (GET_CODE (insn) == PARALLEL)
1116 insn = XVECEXP (insn, 0, 0);
1117 if (GET_CODE (insn) == SET)
1118 insn = SET_SRC (insn);
1119 gcc_assert (GET_CODE (insn) == CALL);
1120 dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1122 return;
1125 if (CALL_P (insn) && !after_p)
1127 if (!flag_asynchronous_unwind_tables)
1128 dwarf2out_args_size ("", args_size);
1129 return;
1131 else if (BARRIER_P (insn))
1133 /* When we see a BARRIER, we know to reset args_size to 0. Usually
1134 the compiler will have already emitted a stack adjustment, but
1135 doesn't bother for calls to noreturn functions. */
1136 #ifdef STACK_GROWS_DOWNWARD
1137 offset = -args_size;
1138 #else
1139 offset = args_size;
1140 #endif
1142 else if (GET_CODE (PATTERN (insn)) == SET)
1143 offset = stack_adjust_offset (PATTERN (insn));
1144 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1145 || GET_CODE (PATTERN (insn)) == SEQUENCE)
1147 /* There may be stack adjustments inside compound insns. Search
1148 for them. */
1149 for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1150 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1151 offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i));
1153 else
1154 return;
1156 if (offset == 0)
1157 return;
1159 if (cfa.reg == STACK_POINTER_REGNUM)
1160 cfa.offset += offset;
1162 #ifndef STACK_GROWS_DOWNWARD
1163 offset = -offset;
1164 #endif
1166 args_size += offset;
1167 if (args_size < 0)
1168 args_size = 0;
1170 label = dwarf2out_cfi_label ();
1171 def_cfa_1 (label, &cfa);
1172 if (flag_asynchronous_unwind_tables)
1173 dwarf2out_args_size (label, args_size);
1176 #endif
1178 /* We delay emitting a register save until either (a) we reach the end
1179 of the prologue or (b) the register is clobbered. This clusters
1180 register saves so that there are fewer pc advances. */
1182 struct queued_reg_save GTY(())
1184 struct queued_reg_save *next;
1185 rtx reg;
1186 HOST_WIDE_INT cfa_offset;
1187 rtx saved_reg;
1190 static GTY(()) struct queued_reg_save *queued_reg_saves;
1192 /* The caller's ORIG_REG is saved in SAVED_IN_REG. */
1193 struct reg_saved_in_data GTY(()) {
1194 rtx orig_reg;
1195 rtx saved_in_reg;
1198 /* A list of registers saved in other registers.
1199 The list intentionally has a small maximum capacity of 4; if your
1200 port needs more than that, you might consider implementing a
1201 more efficient data structure. */
1202 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1203 static GTY(()) size_t num_regs_saved_in_regs;
1205 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1206 static const char *last_reg_save_label;
1208 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1209 SREG, or if SREG is NULL then it is saved at OFFSET to the CFA. */
1211 static void
1212 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1214 struct queued_reg_save *q;
1216 /* Duplicates waste space, but it's also necessary to remove them
1217 for correctness, since the queue gets output in reverse
1218 order. */
1219 for (q = queued_reg_saves; q != NULL; q = q->next)
1220 if (REGNO (q->reg) == REGNO (reg))
1221 break;
1223 if (q == NULL)
1225 q = ggc_alloc (sizeof (*q));
1226 q->next = queued_reg_saves;
1227 queued_reg_saves = q;
1230 q->reg = reg;
1231 q->cfa_offset = offset;
1232 q->saved_reg = sreg;
1234 last_reg_save_label = label;
1237 /* Output all the entries in QUEUED_REG_SAVES. */
1239 static void
1240 flush_queued_reg_saves (void)
1242 struct queued_reg_save *q;
1244 for (q = queued_reg_saves; q; q = q->next)
1246 size_t i;
1247 unsigned int reg, sreg;
1249 for (i = 0; i < num_regs_saved_in_regs; i++)
1250 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1251 break;
1252 if (q->saved_reg && i == num_regs_saved_in_regs)
1254 gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1255 num_regs_saved_in_regs++;
1257 if (i != num_regs_saved_in_regs)
1259 regs_saved_in_regs[i].orig_reg = q->reg;
1260 regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1263 reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1264 if (q->saved_reg)
1265 sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1266 else
1267 sreg = INVALID_REGNUM;
1268 reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1271 queued_reg_saves = NULL;
1272 last_reg_save_label = NULL;
1275 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1276 location for? Or, does it clobber a register which we've previously
1277 said that some other register is saved in, and for which we now
1278 have a new location for? */
1280 static bool
1281 clobbers_queued_reg_save (rtx insn)
1283 struct queued_reg_save *q;
1285 for (q = queued_reg_saves; q; q = q->next)
1287 size_t i;
1288 if (modified_in_p (q->reg, insn))
1289 return true;
1290 for (i = 0; i < num_regs_saved_in_regs; i++)
1291 if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1292 && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1293 return true;
1296 return false;
1299 /* Entry point for saving the first register into the second. */
1301 void
1302 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1304 size_t i;
1305 unsigned int regno, sregno;
1307 for (i = 0; i < num_regs_saved_in_regs; i++)
1308 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1309 break;
1310 if (i == num_regs_saved_in_regs)
1312 gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1313 num_regs_saved_in_regs++;
1315 regs_saved_in_regs[i].orig_reg = reg;
1316 regs_saved_in_regs[i].saved_in_reg = sreg;
1318 regno = DWARF_FRAME_REGNUM (REGNO (reg));
1319 sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1320 reg_save (label, regno, sregno, 0);
1323 /* What register, if any, is currently saved in REG? */
1325 static rtx
1326 reg_saved_in (rtx reg)
1328 unsigned int regn = REGNO (reg);
1329 size_t i;
1330 struct queued_reg_save *q;
1332 for (q = queued_reg_saves; q; q = q->next)
1333 if (q->saved_reg && regn == REGNO (q->saved_reg))
1334 return q->reg;
1336 for (i = 0; i < num_regs_saved_in_regs; i++)
1337 if (regs_saved_in_regs[i].saved_in_reg
1338 && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1339 return regs_saved_in_regs[i].orig_reg;
1341 return NULL_RTX;
1345 /* A temporary register holding an integral value used in adjusting SP
1346 or setting up the store_reg. The "offset" field holds the integer
1347 value, not an offset. */
1348 static dw_cfa_location cfa_temp;
1350 /* Record call frame debugging information for an expression EXPR,
1351 which either sets SP or FP (adjusting how we calculate the frame
1352 address) or saves a register to the stack or another register.
1353 LABEL indicates the address of EXPR.
1355 This function encodes a state machine mapping rtxes to actions on
1356 cfa, cfa_store, and cfa_temp.reg. We describe these rules so
1357 users need not read the source code.
1359 The High-Level Picture
1361 Changes in the register we use to calculate the CFA: Currently we
1362 assume that if you copy the CFA register into another register, we
1363 should take the other one as the new CFA register; this seems to
1364 work pretty well. If it's wrong for some target, it's simple
1365 enough not to set RTX_FRAME_RELATED_P on the insn in question.
1367 Changes in the register we use for saving registers to the stack:
1368 This is usually SP, but not always. Again, we deduce that if you
1369 copy SP into another register (and SP is not the CFA register),
1370 then the new register is the one we will be using for register
1371 saves. This also seems to work.
1373 Register saves: There's not much guesswork about this one; if
1374 RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1375 register save, and the register used to calculate the destination
1376 had better be the one we think we're using for this purpose.
1377 It's also assumed that a copy from a call-saved register to another
1378 register is saving that register if RTX_FRAME_RELATED_P is set on
1379 that instruction. If the copy is from a call-saved register to
1380 the *same* register, that means that the register is now the same
1381 value as in the caller.
1383 Except: If the register being saved is the CFA register, and the
1384 offset is nonzero, we are saving the CFA, so we assume we have to
1385 use DW_CFA_def_cfa_expression. If the offset is 0, we assume that
1386 the intent is to save the value of SP from the previous frame.
1388 In addition, if a register has previously been saved to a different
1389 register,
1391 Invariants / Summaries of Rules
1393 cfa current rule for calculating the CFA. It usually
1394 consists of a register and an offset.
1395 cfa_store register used by prologue code to save things to the stack
1396 cfa_store.offset is the offset from the value of
1397 cfa_store.reg to the actual CFA
1398 cfa_temp register holding an integral value. cfa_temp.offset
1399 stores the value, which will be used to adjust the
1400 stack pointer. cfa_temp is also used like cfa_store,
1401 to track stores to the stack via fp or a temp reg.
1403 Rules 1- 4: Setting a register's value to cfa.reg or an expression
1404 with cfa.reg as the first operand changes the cfa.reg and its
1405 cfa.offset. Rule 1 and 4 also set cfa_temp.reg and
1406 cfa_temp.offset.
1408 Rules 6- 9: Set a non-cfa.reg register value to a constant or an
1409 expression yielding a constant. This sets cfa_temp.reg
1410 and cfa_temp.offset.
1412 Rule 5: Create a new register cfa_store used to save items to the
1413 stack.
1415 Rules 10-14: Save a register to the stack. Define offset as the
1416 difference of the original location and cfa_store's
1417 location (or cfa_temp's location if cfa_temp is used).
1419 The Rules
1421 "{a,b}" indicates a choice of a xor b.
1422 "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1424 Rule 1:
1425 (set <reg1> <reg2>:cfa.reg)
1426 effects: cfa.reg = <reg1>
1427 cfa.offset unchanged
1428 cfa_temp.reg = <reg1>
1429 cfa_temp.offset = cfa.offset
1431 Rule 2:
1432 (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1433 {<const_int>,<reg>:cfa_temp.reg}))
1434 effects: cfa.reg = sp if fp used
1435 cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
1436 cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1437 if cfa_store.reg==sp
1439 Rule 3:
1440 (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
1441 effects: cfa.reg = fp
1442 cfa_offset += +/- <const_int>
1444 Rule 4:
1445 (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
1446 constraints: <reg1> != fp
1447 <reg1> != sp
1448 effects: cfa.reg = <reg1>
1449 cfa_temp.reg = <reg1>
1450 cfa_temp.offset = cfa.offset
1452 Rule 5:
1453 (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1454 constraints: <reg1> != fp
1455 <reg1> != sp
1456 effects: cfa_store.reg = <reg1>
1457 cfa_store.offset = cfa.offset - cfa_temp.offset
1459 Rule 6:
1460 (set <reg> <const_int>)
1461 effects: cfa_temp.reg = <reg>
1462 cfa_temp.offset = <const_int>
1464 Rule 7:
1465 (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1466 effects: cfa_temp.reg = <reg1>
1467 cfa_temp.offset |= <const_int>
1469 Rule 8:
1470 (set <reg> (high <exp>))
1471 effects: none
1473 Rule 9:
1474 (set <reg> (lo_sum <exp> <const_int>))
1475 effects: cfa_temp.reg = <reg>
1476 cfa_temp.offset = <const_int>
1478 Rule 10:
1479 (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1480 effects: cfa_store.offset -= <const_int>
1481 cfa.offset = cfa_store.offset if cfa.reg == sp
1482 cfa.reg = sp
1483 cfa.base_offset = -cfa_store.offset
1485 Rule 11:
1486 (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1487 effects: cfa_store.offset += -/+ mode_size(mem)
1488 cfa.offset = cfa_store.offset if cfa.reg == sp
1489 cfa.reg = sp
1490 cfa.base_offset = -cfa_store.offset
1492 Rule 12:
1493 (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1495 <reg2>)
1496 effects: cfa.reg = <reg1>
1497 cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
1499 Rule 13:
1500 (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1501 effects: cfa.reg = <reg1>
1502 cfa.base_offset = -{cfa_store,cfa_temp}.offset
1504 Rule 14:
1505 (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
1506 effects: cfa.reg = <reg1>
1507 cfa.base_offset = -cfa_temp.offset
1508 cfa_temp.offset -= mode_size(mem)
1510 Rule 15:
1511 (set <reg> {unspec, unspec_volatile})
1512 effects: target-dependent */
1514 static void
1515 dwarf2out_frame_debug_expr (rtx expr, const char *label)
1517 rtx src, dest;
1518 HOST_WIDE_INT offset;
1520 /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1521 the PARALLEL independently. The first element is always processed if
1522 it is a SET. This is for backward compatibility. Other elements
1523 are processed only if they are SETs and the RTX_FRAME_RELATED_P
1524 flag is set in them. */
1525 if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
1527 int par_index;
1528 int limit = XVECLEN (expr, 0);
1529 rtx elem;
1531 /* PARALLELs have strict read-modify-write semantics, so we
1532 ought to evaluate every rvalue before changing any lvalue.
1533 It's cumbersome to do that in general, but there's an
1534 easy approximation that is enough for all current users:
1535 handle register saves before register assignments. */
1536 if (GET_CODE (expr) == PARALLEL)
1537 for (par_index = 0; par_index < limit; par_index++)
1539 elem = XVECEXP (expr, 0, par_index);
1540 if (GET_CODE (elem) == SET
1541 && MEM_P (SET_DEST (elem))
1542 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1543 dwarf2out_frame_debug_expr (elem, label);
1546 for (par_index = 0; par_index < limit; par_index++)
1548 elem = XVECEXP (expr, 0, par_index);
1549 if (GET_CODE (elem) == SET
1550 && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
1551 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1552 dwarf2out_frame_debug_expr (elem, label);
1554 return;
1557 gcc_assert (GET_CODE (expr) == SET);
1559 src = SET_SRC (expr);
1560 dest = SET_DEST (expr);
1562 if (REG_P (src))
1564 rtx rsi = reg_saved_in (src);
1565 if (rsi)
1566 src = rsi;
1569 switch (GET_CODE (dest))
1571 case REG:
1572 switch (GET_CODE (src))
1574 /* Setting FP from SP. */
1575 case REG:
1576 if (cfa.reg == (unsigned) REGNO (src))
1578 /* Rule 1 */
1579 /* Update the CFA rule wrt SP or FP. Make sure src is
1580 relative to the current CFA register.
1582 We used to require that dest be either SP or FP, but the
1583 ARM copies SP to a temporary register, and from there to
1584 FP. So we just rely on the backends to only set
1585 RTX_FRAME_RELATED_P on appropriate insns. */
1586 cfa.reg = REGNO (dest);
1587 cfa_temp.reg = cfa.reg;
1588 cfa_temp.offset = cfa.offset;
1590 else
1592 /* Saving a register in a register. */
1593 gcc_assert (!fixed_regs [REGNO (dest)]
1594 /* For the SPARC and its register window. */
1595 || (DWARF_FRAME_REGNUM (REGNO (src))
1596 == DWARF_FRAME_RETURN_COLUMN));
1597 queue_reg_save (label, src, dest, 0);
1599 break;
1601 case PLUS:
1602 case MINUS:
1603 case LO_SUM:
1604 if (dest == stack_pointer_rtx)
1606 /* Rule 2 */
1607 /* Adjusting SP. */
1608 switch (GET_CODE (XEXP (src, 1)))
1610 case CONST_INT:
1611 offset = INTVAL (XEXP (src, 1));
1612 break;
1613 case REG:
1614 gcc_assert ((unsigned) REGNO (XEXP (src, 1))
1615 == cfa_temp.reg);
1616 offset = cfa_temp.offset;
1617 break;
1618 default:
1619 gcc_unreachable ();
1622 if (XEXP (src, 0) == hard_frame_pointer_rtx)
1624 /* Restoring SP from FP in the epilogue. */
1625 gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
1626 cfa.reg = STACK_POINTER_REGNUM;
1628 else if (GET_CODE (src) == LO_SUM)
1629 /* Assume we've set the source reg of the LO_SUM from sp. */
1631 else
1632 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
1634 if (GET_CODE (src) != MINUS)
1635 offset = -offset;
1636 if (cfa.reg == STACK_POINTER_REGNUM)
1637 cfa.offset += offset;
1638 if (cfa_store.reg == STACK_POINTER_REGNUM)
1639 cfa_store.offset += offset;
1641 else if (dest == hard_frame_pointer_rtx)
1643 /* Rule 3 */
1644 /* Either setting the FP from an offset of the SP,
1645 or adjusting the FP */
1646 gcc_assert (frame_pointer_needed);
1648 gcc_assert (REG_P (XEXP (src, 0))
1649 && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
1650 && GET_CODE (XEXP (src, 1)) == CONST_INT);
1651 offset = INTVAL (XEXP (src, 1));
1652 if (GET_CODE (src) != MINUS)
1653 offset = -offset;
1654 cfa.offset += offset;
1655 cfa.reg = HARD_FRAME_POINTER_REGNUM;
1657 else
1659 gcc_assert (GET_CODE (src) != MINUS);
1661 /* Rule 4 */
1662 if (REG_P (XEXP (src, 0))
1663 && REGNO (XEXP (src, 0)) == cfa.reg
1664 && GET_CODE (XEXP (src, 1)) == CONST_INT)
1666 /* Setting a temporary CFA register that will be copied
1667 into the FP later on. */
1668 offset = - INTVAL (XEXP (src, 1));
1669 cfa.offset += offset;
1670 cfa.reg = REGNO (dest);
1671 /* Or used to save regs to the stack. */
1672 cfa_temp.reg = cfa.reg;
1673 cfa_temp.offset = cfa.offset;
1676 /* Rule 5 */
1677 else if (REG_P (XEXP (src, 0))
1678 && REGNO (XEXP (src, 0)) == cfa_temp.reg
1679 && XEXP (src, 1) == stack_pointer_rtx)
1681 /* Setting a scratch register that we will use instead
1682 of SP for saving registers to the stack. */
1683 gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
1684 cfa_store.reg = REGNO (dest);
1685 cfa_store.offset = cfa.offset - cfa_temp.offset;
1688 /* Rule 9 */
1689 else if (GET_CODE (src) == LO_SUM
1690 && GET_CODE (XEXP (src, 1)) == CONST_INT)
1692 cfa_temp.reg = REGNO (dest);
1693 cfa_temp.offset = INTVAL (XEXP (src, 1));
1695 else
1696 gcc_unreachable ();
1698 break;
1700 /* Rule 6 */
1701 case CONST_INT:
1702 cfa_temp.reg = REGNO (dest);
1703 cfa_temp.offset = INTVAL (src);
1704 break;
1706 /* Rule 7 */
1707 case IOR:
1708 gcc_assert (REG_P (XEXP (src, 0))
1709 && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
1710 && GET_CODE (XEXP (src, 1)) == CONST_INT);
1712 if ((unsigned) REGNO (dest) != cfa_temp.reg)
1713 cfa_temp.reg = REGNO (dest);
1714 cfa_temp.offset |= INTVAL (XEXP (src, 1));
1715 break;
1717 /* Skip over HIGH, assuming it will be followed by a LO_SUM,
1718 which will fill in all of the bits. */
1719 /* Rule 8 */
1720 case HIGH:
1721 break;
1723 /* Rule 15 */
1724 case UNSPEC:
1725 case UNSPEC_VOLATILE:
1726 gcc_assert (targetm.dwarf_handle_frame_unspec);
1727 targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
1728 return;
1730 default:
1731 gcc_unreachable ();
1734 def_cfa_1 (label, &cfa);
1735 break;
1737 case MEM:
1738 gcc_assert (REG_P (src));
1740 /* Saving a register to the stack. Make sure dest is relative to the
1741 CFA register. */
1742 switch (GET_CODE (XEXP (dest, 0)))
1744 /* Rule 10 */
1745 /* With a push. */
1746 case PRE_MODIFY:
1747 /* We can't handle variable size modifications. */
1748 gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
1749 == CONST_INT);
1750 offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
1752 gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1753 && cfa_store.reg == STACK_POINTER_REGNUM);
1755 cfa_store.offset += offset;
1756 if (cfa.reg == STACK_POINTER_REGNUM)
1757 cfa.offset = cfa_store.offset;
1759 offset = -cfa_store.offset;
1760 break;
1762 /* Rule 11 */
1763 case PRE_INC:
1764 case PRE_DEC:
1765 offset = GET_MODE_SIZE (GET_MODE (dest));
1766 if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
1767 offset = -offset;
1769 gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1770 && cfa_store.reg == STACK_POINTER_REGNUM);
1772 cfa_store.offset += offset;
1773 if (cfa.reg == STACK_POINTER_REGNUM)
1774 cfa.offset = cfa_store.offset;
1776 offset = -cfa_store.offset;
1777 break;
1779 /* Rule 12 */
1780 /* With an offset. */
1781 case PLUS:
1782 case MINUS:
1783 case LO_SUM:
1785 int regno;
1787 gcc_assert (GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT
1788 && REG_P (XEXP (XEXP (dest, 0), 0)));
1789 offset = INTVAL (XEXP (XEXP (dest, 0), 1));
1790 if (GET_CODE (XEXP (dest, 0)) == MINUS)
1791 offset = -offset;
1793 regno = REGNO (XEXP (XEXP (dest, 0), 0));
1795 if (cfa_store.reg == (unsigned) regno)
1796 offset -= cfa_store.offset;
1797 else
1799 gcc_assert (cfa_temp.reg == (unsigned) regno);
1800 offset -= cfa_temp.offset;
1803 break;
1805 /* Rule 13 */
1806 /* Without an offset. */
1807 case REG:
1809 int regno = REGNO (XEXP (dest, 0));
1811 if (cfa_store.reg == (unsigned) regno)
1812 offset = -cfa_store.offset;
1813 else
1815 gcc_assert (cfa_temp.reg == (unsigned) regno);
1816 offset = -cfa_temp.offset;
1819 break;
1821 /* Rule 14 */
1822 case POST_INC:
1823 gcc_assert (cfa_temp.reg
1824 == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
1825 offset = -cfa_temp.offset;
1826 cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
1827 break;
1829 default:
1830 gcc_unreachable ();
1833 if (REGNO (src) != STACK_POINTER_REGNUM
1834 && REGNO (src) != HARD_FRAME_POINTER_REGNUM
1835 && (unsigned) REGNO (src) == cfa.reg)
1837 /* We're storing the current CFA reg into the stack. */
1839 if (cfa.offset == 0)
1841 /* If the source register is exactly the CFA, assume
1842 we're saving SP like any other register; this happens
1843 on the ARM. */
1844 def_cfa_1 (label, &cfa);
1845 queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
1846 break;
1848 else
1850 /* Otherwise, we'll need to look in the stack to
1851 calculate the CFA. */
1852 rtx x = XEXP (dest, 0);
1854 if (!REG_P (x))
1855 x = XEXP (x, 0);
1856 gcc_assert (REG_P (x));
1858 cfa.reg = REGNO (x);
1859 cfa.base_offset = offset;
1860 cfa.indirect = 1;
1861 def_cfa_1 (label, &cfa);
1862 break;
1866 def_cfa_1 (label, &cfa);
1867 queue_reg_save (label, src, NULL_RTX, offset);
1868 break;
1870 default:
1871 gcc_unreachable ();
1875 /* Record call frame debugging information for INSN, which either
1876 sets SP or FP (adjusting how we calculate the frame address) or saves a
1877 register to the stack. If INSN is NULL_RTX, initialize our state.
1879 If AFTER_P is false, we're being called before the insn is emitted,
1880 otherwise after. Call instructions get invoked twice. */
1882 void
1883 dwarf2out_frame_debug (rtx insn, bool after_p)
1885 const char *label;
1886 rtx src;
1888 if (insn == NULL_RTX)
1890 size_t i;
1892 /* Flush any queued register saves. */
1893 flush_queued_reg_saves ();
1895 /* Set up state for generating call frame debug info. */
1896 lookup_cfa (&cfa);
1897 gcc_assert (cfa.reg
1898 == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
1900 cfa.reg = STACK_POINTER_REGNUM;
1901 cfa_store = cfa;
1902 cfa_temp.reg = -1;
1903 cfa_temp.offset = 0;
1905 for (i = 0; i < num_regs_saved_in_regs; i++)
1907 regs_saved_in_regs[i].orig_reg = NULL_RTX;
1908 regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
1910 num_regs_saved_in_regs = 0;
1911 return;
1914 if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
1915 flush_queued_reg_saves ();
1917 if (! RTX_FRAME_RELATED_P (insn))
1919 if (!ACCUMULATE_OUTGOING_ARGS)
1920 dwarf2out_stack_adjust (insn, after_p);
1921 return;
1924 label = dwarf2out_cfi_label ();
1925 src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1926 if (src)
1927 insn = XEXP (src, 0);
1928 else
1929 insn = PATTERN (insn);
1931 dwarf2out_frame_debug_expr (insn, label);
1934 #endif
1936 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
1937 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
1938 (enum dwarf_call_frame_info cfi);
1940 static enum dw_cfi_oprnd_type
1941 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
1943 switch (cfi)
1945 case DW_CFA_nop:
1946 case DW_CFA_GNU_window_save:
1947 return dw_cfi_oprnd_unused;
1949 case DW_CFA_set_loc:
1950 case DW_CFA_advance_loc1:
1951 case DW_CFA_advance_loc2:
1952 case DW_CFA_advance_loc4:
1953 case DW_CFA_MIPS_advance_loc8:
1954 return dw_cfi_oprnd_addr;
1956 case DW_CFA_offset:
1957 case DW_CFA_offset_extended:
1958 case DW_CFA_def_cfa:
1959 case DW_CFA_offset_extended_sf:
1960 case DW_CFA_def_cfa_sf:
1961 case DW_CFA_restore_extended:
1962 case DW_CFA_undefined:
1963 case DW_CFA_same_value:
1964 case DW_CFA_def_cfa_register:
1965 case DW_CFA_register:
1966 return dw_cfi_oprnd_reg_num;
1968 case DW_CFA_def_cfa_offset:
1969 case DW_CFA_GNU_args_size:
1970 case DW_CFA_def_cfa_offset_sf:
1971 return dw_cfi_oprnd_offset;
1973 case DW_CFA_def_cfa_expression:
1974 case DW_CFA_expression:
1975 return dw_cfi_oprnd_loc;
1977 default:
1978 gcc_unreachable ();
1982 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
1983 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
1984 (enum dwarf_call_frame_info cfi);
1986 static enum dw_cfi_oprnd_type
1987 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
1989 switch (cfi)
1991 case DW_CFA_def_cfa:
1992 case DW_CFA_def_cfa_sf:
1993 case DW_CFA_offset:
1994 case DW_CFA_offset_extended_sf:
1995 case DW_CFA_offset_extended:
1996 return dw_cfi_oprnd_offset;
1998 case DW_CFA_register:
1999 return dw_cfi_oprnd_reg_num;
2001 default:
2002 return dw_cfi_oprnd_unused;
2006 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2008 /* Switch to eh_frame_section. If we don't have an eh_frame_section,
2009 switch to the data section instead, and write out a synthetic label
2010 for collect2. */
2012 static void
2013 switch_to_eh_frame_section (void)
2015 tree label;
2017 #ifdef EH_FRAME_SECTION_NAME
2018 if (eh_frame_section == 0)
2020 int flags;
2022 if (EH_TABLES_CAN_BE_READ_ONLY)
2024 int fde_encoding;
2025 int per_encoding;
2026 int lsda_encoding;
2028 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2029 /*global=*/0);
2030 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2031 /*global=*/1);
2032 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2033 /*global=*/0);
2034 flags = ((! flag_pic
2035 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2036 && (fde_encoding & 0x70) != DW_EH_PE_aligned
2037 && (per_encoding & 0x70) != DW_EH_PE_absptr
2038 && (per_encoding & 0x70) != DW_EH_PE_aligned
2039 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2040 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2041 ? 0 : SECTION_WRITE);
2043 else
2044 flags = SECTION_WRITE;
2045 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2047 #endif
2049 if (eh_frame_section)
2050 switch_to_section (eh_frame_section);
2051 else
2053 /* We have no special eh_frame section. Put the information in
2054 the data section and emit special labels to guide collect2. */
2055 switch_to_section (data_section);
2056 label = get_file_function_name ("F");
2057 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2058 targetm.asm_out.globalize_label (asm_out_file,
2059 IDENTIFIER_POINTER (label));
2060 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
2064 /* Output a Call Frame Information opcode and its operand(s). */
2066 static void
2067 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
2069 unsigned long r;
2070 if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
2071 dw2_asm_output_data (1, (cfi->dw_cfi_opc
2072 | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
2073 "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
2074 cfi->dw_cfi_oprnd1.dw_cfi_offset);
2075 else if (cfi->dw_cfi_opc == DW_CFA_offset)
2077 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2078 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2079 "DW_CFA_offset, column 0x%lx", r);
2080 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2082 else if (cfi->dw_cfi_opc == DW_CFA_restore)
2084 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2085 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2086 "DW_CFA_restore, column 0x%lx", r);
2088 else
2090 dw2_asm_output_data (1, cfi->dw_cfi_opc,
2091 "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
2093 switch (cfi->dw_cfi_opc)
2095 case DW_CFA_set_loc:
2096 if (for_eh)
2097 dw2_asm_output_encoded_addr_rtx (
2098 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
2099 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
2100 false, NULL);
2101 else
2102 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2103 cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
2104 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2105 break;
2107 case DW_CFA_advance_loc1:
2108 dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2109 fde->dw_fde_current_label, NULL);
2110 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2111 break;
2113 case DW_CFA_advance_loc2:
2114 dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2115 fde->dw_fde_current_label, NULL);
2116 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2117 break;
2119 case DW_CFA_advance_loc4:
2120 dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2121 fde->dw_fde_current_label, NULL);
2122 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2123 break;
2125 case DW_CFA_MIPS_advance_loc8:
2126 dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2127 fde->dw_fde_current_label, NULL);
2128 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2129 break;
2131 case DW_CFA_offset_extended:
2132 case DW_CFA_def_cfa:
2133 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2134 dw2_asm_output_data_uleb128 (r, NULL);
2135 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2136 break;
2138 case DW_CFA_offset_extended_sf:
2139 case DW_CFA_def_cfa_sf:
2140 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2141 dw2_asm_output_data_uleb128 (r, NULL);
2142 dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2143 break;
2145 case DW_CFA_restore_extended:
2146 case DW_CFA_undefined:
2147 case DW_CFA_same_value:
2148 case DW_CFA_def_cfa_register:
2149 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2150 dw2_asm_output_data_uleb128 (r, NULL);
2151 break;
2153 case DW_CFA_register:
2154 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2155 dw2_asm_output_data_uleb128 (r, NULL);
2156 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
2157 dw2_asm_output_data_uleb128 (r, NULL);
2158 break;
2160 case DW_CFA_def_cfa_offset:
2161 case DW_CFA_GNU_args_size:
2162 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2163 break;
2165 case DW_CFA_def_cfa_offset_sf:
2166 dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2167 break;
2169 case DW_CFA_GNU_window_save:
2170 break;
2172 case DW_CFA_def_cfa_expression:
2173 case DW_CFA_expression:
2174 output_cfa_loc (cfi);
2175 break;
2177 case DW_CFA_GNU_negative_offset_extended:
2178 /* Obsoleted by DW_CFA_offset_extended_sf. */
2179 gcc_unreachable ();
2181 default:
2182 break;
2187 /* Output the call frame information used to record information
2188 that relates to calculating the frame pointer, and records the
2189 location of saved registers. */
2191 static void
2192 output_call_frame_info (int for_eh)
2194 unsigned int i;
2195 dw_fde_ref fde;
2196 dw_cfi_ref cfi;
2197 char l1[20], l2[20], section_start_label[20];
2198 bool any_lsda_needed = false;
2199 char augmentation[6];
2200 int augmentation_size;
2201 int fde_encoding = DW_EH_PE_absptr;
2202 int per_encoding = DW_EH_PE_absptr;
2203 int lsda_encoding = DW_EH_PE_absptr;
2204 int return_reg;
2206 /* Don't emit a CIE if there won't be any FDEs. */
2207 if (fde_table_in_use == 0)
2208 return;
2210 /* If we make FDEs linkonce, we may have to emit an empty label for
2211 an FDE that wouldn't otherwise be emitted. We want to avoid
2212 having an FDE kept around when the function it refers to is
2213 discarded. Example where this matters: a primary function
2214 template in C++ requires EH information, but an explicit
2215 specialization doesn't. */
2216 if (TARGET_USES_WEAK_UNWIND_INFO
2217 && ! flag_asynchronous_unwind_tables
2218 && for_eh)
2219 for (i = 0; i < fde_table_in_use; i++)
2220 if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
2221 && !fde_table[i].uses_eh_lsda
2222 && ! DECL_WEAK (fde_table[i].decl))
2223 targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
2224 for_eh, /* empty */ 1);
2226 /* If we don't have any functions we'll want to unwind out of, don't
2227 emit any EH unwind information. Note that if exceptions aren't
2228 enabled, we won't have collected nothrow information, and if we
2229 asked for asynchronous tables, we always want this info. */
2230 if (for_eh)
2232 bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
2234 for (i = 0; i < fde_table_in_use; i++)
2235 if (fde_table[i].uses_eh_lsda)
2236 any_eh_needed = any_lsda_needed = true;
2237 else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2238 any_eh_needed = true;
2239 else if (! fde_table[i].nothrow
2240 && ! fde_table[i].all_throwers_are_sibcalls)
2241 any_eh_needed = true;
2243 if (! any_eh_needed)
2244 return;
2247 /* We're going to be generating comments, so turn on app. */
2248 if (flag_debug_asm)
2249 app_enable ();
2251 if (for_eh)
2252 switch_to_eh_frame_section ();
2253 else
2255 if (!debug_frame_section)
2256 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
2257 SECTION_DEBUG, NULL);
2258 switch_to_section (debug_frame_section);
2261 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
2262 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
2264 /* Output the CIE. */
2265 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
2266 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
2267 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2268 dw2_asm_output_data (4, 0xffffffff,
2269 "Initial length escape value indicating 64-bit DWARF extension");
2270 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2271 "Length of Common Information Entry");
2272 ASM_OUTPUT_LABEL (asm_out_file, l1);
2274 /* Now that the CIE pointer is PC-relative for EH,
2275 use 0 to identify the CIE. */
2276 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
2277 (for_eh ? 0 : DWARF_CIE_ID),
2278 "CIE Identifier Tag");
2280 dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
2282 augmentation[0] = 0;
2283 augmentation_size = 0;
2284 if (for_eh)
2286 char *p;
2288 /* Augmentation:
2289 z Indicates that a uleb128 is present to size the
2290 augmentation section.
2291 L Indicates the encoding (and thus presence) of
2292 an LSDA pointer in the FDE augmentation.
2293 R Indicates a non-default pointer encoding for
2294 FDE code pointers.
2295 P Indicates the presence of an encoding + language
2296 personality routine in the CIE augmentation. */
2298 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
2299 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
2300 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
2302 p = augmentation + 1;
2303 if (eh_personality_libfunc)
2305 *p++ = 'P';
2306 augmentation_size += 1 + size_of_encoded_value (per_encoding);
2308 if (any_lsda_needed)
2310 *p++ = 'L';
2311 augmentation_size += 1;
2313 if (fde_encoding != DW_EH_PE_absptr)
2315 *p++ = 'R';
2316 augmentation_size += 1;
2318 if (p > augmentation + 1)
2320 augmentation[0] = 'z';
2321 *p = '\0';
2324 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
2325 if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
2327 int offset = ( 4 /* Length */
2328 + 4 /* CIE Id */
2329 + 1 /* CIE version */
2330 + strlen (augmentation) + 1 /* Augmentation */
2331 + size_of_uleb128 (1) /* Code alignment */
2332 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
2333 + 1 /* RA column */
2334 + 1 /* Augmentation size */
2335 + 1 /* Personality encoding */ );
2336 int pad = -offset & (PTR_SIZE - 1);
2338 augmentation_size += pad;
2340 /* Augmentations should be small, so there's scarce need to
2341 iterate for a solution. Die if we exceed one uleb128 byte. */
2342 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
2346 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
2347 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
2348 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
2349 "CIE Data Alignment Factor");
2351 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
2352 if (DW_CIE_VERSION == 1)
2353 dw2_asm_output_data (1, return_reg, "CIE RA Column");
2354 else
2355 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
2357 if (augmentation[0])
2359 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
2360 if (eh_personality_libfunc)
2362 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
2363 eh_data_format_name (per_encoding));
2364 dw2_asm_output_encoded_addr_rtx (per_encoding,
2365 eh_personality_libfunc,
2366 true, NULL);
2369 if (any_lsda_needed)
2370 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
2371 eh_data_format_name (lsda_encoding));
2373 if (fde_encoding != DW_EH_PE_absptr)
2374 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
2375 eh_data_format_name (fde_encoding));
2378 for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
2379 output_cfi (cfi, NULL, for_eh);
2381 /* Pad the CIE out to an address sized boundary. */
2382 ASM_OUTPUT_ALIGN (asm_out_file,
2383 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
2384 ASM_OUTPUT_LABEL (asm_out_file, l2);
2386 /* Loop through all of the FDE's. */
2387 for (i = 0; i < fde_table_in_use; i++)
2389 fde = &fde_table[i];
2391 /* Don't emit EH unwind info for leaf functions that don't need it. */
2392 if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
2393 && (fde->nothrow || fde->all_throwers_are_sibcalls)
2394 && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2395 && !fde->uses_eh_lsda)
2396 continue;
2398 targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
2399 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
2400 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
2401 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
2402 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2403 dw2_asm_output_data (4, 0xffffffff,
2404 "Initial length escape value indicating 64-bit DWARF extension");
2405 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2406 "FDE Length");
2407 ASM_OUTPUT_LABEL (asm_out_file, l1);
2409 if (for_eh)
2410 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
2411 else
2412 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
2413 debug_frame_section, "FDE CIE offset");
2415 if (for_eh)
2417 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
2418 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
2419 dw2_asm_output_encoded_addr_rtx (fde_encoding,
2420 sym_ref,
2421 false,
2422 "FDE initial location");
2423 if (fde->dw_fde_switched_sections)
2425 rtx sym_ref2 = gen_rtx_SYMBOL_REF (Pmode,
2426 fde->dw_fde_unlikely_section_label);
2427 rtx sym_ref3= gen_rtx_SYMBOL_REF (Pmode,
2428 fde->dw_fde_hot_section_label);
2429 SYMBOL_REF_FLAGS (sym_ref2) |= SYMBOL_FLAG_LOCAL;
2430 SYMBOL_REF_FLAGS (sym_ref3) |= SYMBOL_FLAG_LOCAL;
2431 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref3, false,
2432 "FDE initial location");
2433 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2434 fde->dw_fde_hot_section_end_label,
2435 fde->dw_fde_hot_section_label,
2436 "FDE address range");
2437 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref2, false,
2438 "FDE initial location");
2439 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2440 fde->dw_fde_unlikely_section_end_label,
2441 fde->dw_fde_unlikely_section_label,
2442 "FDE address range");
2444 else
2445 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2446 fde->dw_fde_end, fde->dw_fde_begin,
2447 "FDE address range");
2449 else
2451 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
2452 "FDE initial location");
2453 if (fde->dw_fde_switched_sections)
2455 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2456 fde->dw_fde_hot_section_label,
2457 "FDE initial location");
2458 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2459 fde->dw_fde_hot_section_end_label,
2460 fde->dw_fde_hot_section_label,
2461 "FDE address range");
2462 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2463 fde->dw_fde_unlikely_section_label,
2464 "FDE initial location");
2465 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2466 fde->dw_fde_unlikely_section_end_label,
2467 fde->dw_fde_unlikely_section_label,
2468 "FDE address range");
2470 else
2471 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2472 fde->dw_fde_end, fde->dw_fde_begin,
2473 "FDE address range");
2476 if (augmentation[0])
2478 if (any_lsda_needed)
2480 int size = size_of_encoded_value (lsda_encoding);
2482 if (lsda_encoding == DW_EH_PE_aligned)
2484 int offset = ( 4 /* Length */
2485 + 4 /* CIE offset */
2486 + 2 * size_of_encoded_value (fde_encoding)
2487 + 1 /* Augmentation size */ );
2488 int pad = -offset & (PTR_SIZE - 1);
2490 size += pad;
2491 gcc_assert (size_of_uleb128 (size) == 1);
2494 dw2_asm_output_data_uleb128 (size, "Augmentation size");
2496 if (fde->uses_eh_lsda)
2498 ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
2499 fde->funcdef_number);
2500 dw2_asm_output_encoded_addr_rtx (
2501 lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
2502 false, "Language Specific Data Area");
2504 else
2506 if (lsda_encoding == DW_EH_PE_aligned)
2507 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2508 dw2_asm_output_data
2509 (size_of_encoded_value (lsda_encoding), 0,
2510 "Language Specific Data Area (none)");
2513 else
2514 dw2_asm_output_data_uleb128 (0, "Augmentation size");
2517 /* Loop through the Call Frame Instructions associated with
2518 this FDE. */
2519 fde->dw_fde_current_label = fde->dw_fde_begin;
2520 for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
2521 output_cfi (cfi, fde, for_eh);
2523 /* Pad the FDE out to an address sized boundary. */
2524 ASM_OUTPUT_ALIGN (asm_out_file,
2525 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
2526 ASM_OUTPUT_LABEL (asm_out_file, l2);
2529 if (for_eh && targetm.terminate_dw2_eh_frame_info)
2530 dw2_asm_output_data (4, 0, "End of Table");
2531 #ifdef MIPS_DEBUGGING_INFO
2532 /* Work around Irix 6 assembler bug whereby labels at the end of a section
2533 get a value of 0. Putting .align 0 after the label fixes it. */
2534 ASM_OUTPUT_ALIGN (asm_out_file, 0);
2535 #endif
2537 /* Turn off app to make assembly quicker. */
2538 if (flag_debug_asm)
2539 app_disable ();
2542 /* Output a marker (i.e. a label) for the beginning of a function, before
2543 the prologue. */
2545 void
2546 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
2547 const char *file ATTRIBUTE_UNUSED)
2549 char label[MAX_ARTIFICIAL_LABEL_BYTES];
2550 char * dup_label;
2551 dw_fde_ref fde;
2553 current_function_func_begin_label = NULL;
2555 #ifdef TARGET_UNWIND_INFO
2556 /* ??? current_function_func_begin_label is also used by except.c
2557 for call-site information. We must emit this label if it might
2558 be used. */
2559 if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
2560 && ! dwarf2out_do_frame ())
2561 return;
2562 #else
2563 if (! dwarf2out_do_frame ())
2564 return;
2565 #endif
2567 switch_to_section (function_section (current_function_decl));
2568 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
2569 current_function_funcdef_no);
2570 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
2571 current_function_funcdef_no);
2572 dup_label = xstrdup (label);
2573 current_function_func_begin_label = dup_label;
2575 #ifdef TARGET_UNWIND_INFO
2576 /* We can elide the fde allocation if we're not emitting debug info. */
2577 if (! dwarf2out_do_frame ())
2578 return;
2579 #endif
2581 /* Expand the fde table if necessary. */
2582 if (fde_table_in_use == fde_table_allocated)
2584 fde_table_allocated += FDE_TABLE_INCREMENT;
2585 fde_table = ggc_realloc (fde_table,
2586 fde_table_allocated * sizeof (dw_fde_node));
2587 memset (fde_table + fde_table_in_use, 0,
2588 FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2591 /* Record the FDE associated with this function. */
2592 current_funcdef_fde = fde_table_in_use;
2594 /* Add the new FDE at the end of the fde_table. */
2595 fde = &fde_table[fde_table_in_use++];
2596 fde->decl = current_function_decl;
2597 fde->dw_fde_begin = dup_label;
2598 fde->dw_fde_current_label = dup_label;
2599 fde->dw_fde_hot_section_label = NULL;
2600 fde->dw_fde_hot_section_end_label = NULL;
2601 fde->dw_fde_unlikely_section_label = NULL;
2602 fde->dw_fde_unlikely_section_end_label = NULL;
2603 fde->dw_fde_switched_sections = false;
2604 fde->dw_fde_end = NULL;
2605 fde->dw_fde_cfi = NULL;
2606 fde->funcdef_number = current_function_funcdef_no;
2607 fde->nothrow = TREE_NOTHROW (current_function_decl);
2608 fde->uses_eh_lsda = cfun->uses_eh_lsda;
2609 fde->all_throwers_are_sibcalls = cfun->all_throwers_are_sibcalls;
2611 args_size = old_args_size = 0;
2613 /* We only want to output line number information for the genuine dwarf2
2614 prologue case, not the eh frame case. */
2615 #ifdef DWARF2_DEBUGGING_INFO
2616 if (file)
2617 dwarf2out_source_line (line, file);
2618 #endif
2621 /* Output a marker (i.e. a label) for the absolute end of the generated code
2622 for a function definition. This gets called *after* the epilogue code has
2623 been generated. */
2625 void
2626 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
2627 const char *file ATTRIBUTE_UNUSED)
2629 dw_fde_ref fde;
2630 char label[MAX_ARTIFICIAL_LABEL_BYTES];
2632 /* Output a label to mark the endpoint of the code generated for this
2633 function. */
2634 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
2635 current_function_funcdef_no);
2636 ASM_OUTPUT_LABEL (asm_out_file, label);
2637 fde = &fde_table[fde_table_in_use - 1];
2638 fde->dw_fde_end = xstrdup (label);
2641 void
2642 dwarf2out_frame_init (void)
2644 /* Allocate the initial hunk of the fde_table. */
2645 fde_table = ggc_alloc_cleared (FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2646 fde_table_allocated = FDE_TABLE_INCREMENT;
2647 fde_table_in_use = 0;
2649 /* Generate the CFA instructions common to all FDE's. Do it now for the
2650 sake of lookup_cfa. */
2652 /* On entry, the Canonical Frame Address is at SP. */
2653 dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
2655 #ifdef DWARF2_UNWIND_INFO
2656 if (DWARF2_UNWIND_INFO)
2657 initial_return_save (INCOMING_RETURN_ADDR_RTX);
2658 #endif
2661 void
2662 dwarf2out_frame_finish (void)
2664 /* Output call frame information. */
2665 if (DWARF2_FRAME_INFO)
2666 output_call_frame_info (0);
2668 #ifndef TARGET_UNWIND_INFO
2669 /* Output another copy for the unwinder. */
2670 if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
2671 output_call_frame_info (1);
2672 #endif
2674 #endif
2676 /* And now, the subset of the debugging information support code necessary
2677 for emitting location expressions. */
2679 /* Data about a single source file. */
2680 struct dwarf_file_data GTY(())
2682 const char * filename;
2683 int emitted_number;
2686 /* We need some way to distinguish DW_OP_addr with a direct symbol
2687 relocation from DW_OP_addr with a dtp-relative symbol relocation. */
2688 #define INTERNAL_DW_OP_tls_addr (0x100 + DW_OP_addr)
2691 typedef struct dw_val_struct *dw_val_ref;
2692 typedef struct die_struct *dw_die_ref;
2693 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
2694 typedef struct dw_loc_list_struct *dw_loc_list_ref;
2696 /* Each DIE may have a series of attribute/value pairs. Values
2697 can take on several forms. The forms that are used in this
2698 implementation are listed below. */
2700 enum dw_val_class
2702 dw_val_class_addr,
2703 dw_val_class_offset,
2704 dw_val_class_loc,
2705 dw_val_class_loc_list,
2706 dw_val_class_range_list,
2707 dw_val_class_const,
2708 dw_val_class_unsigned_const,
2709 dw_val_class_long_long,
2710 dw_val_class_vec,
2711 dw_val_class_flag,
2712 dw_val_class_die_ref,
2713 dw_val_class_fde_ref,
2714 dw_val_class_lbl_id,
2715 dw_val_class_lineptr,
2716 dw_val_class_str,
2717 dw_val_class_macptr,
2718 dw_val_class_file
2721 /* Describe a double word constant value. */
2722 /* ??? Every instance of long_long in the code really means CONST_DOUBLE. */
2724 typedef struct dw_long_long_struct GTY(())
2726 unsigned long hi;
2727 unsigned long low;
2729 dw_long_long_const;
2731 /* Describe a floating point constant value, or a vector constant value. */
2733 typedef struct dw_vec_struct GTY(())
2735 unsigned char * GTY((length ("%h.length"))) array;
2736 unsigned length;
2737 unsigned elt_size;
2739 dw_vec_const;
2741 /* The dw_val_node describes an attribute's value, as it is
2742 represented internally. */
2744 typedef struct dw_val_struct GTY(())
2746 enum dw_val_class val_class;
2747 union dw_val_struct_union
2749 rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
2750 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
2751 dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
2752 dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
2753 HOST_WIDE_INT GTY ((default)) val_int;
2754 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
2755 dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
2756 dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
2757 struct dw_val_die_union
2759 dw_die_ref die;
2760 int external;
2761 } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
2762 unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
2763 struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
2764 char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
2765 unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
2766 struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
2768 GTY ((desc ("%1.val_class"))) v;
2770 dw_val_node;
2772 /* Locations in memory are described using a sequence of stack machine
2773 operations. */
2775 typedef struct dw_loc_descr_struct GTY(())
2777 dw_loc_descr_ref dw_loc_next;
2778 enum dwarf_location_atom dw_loc_opc;
2779 dw_val_node dw_loc_oprnd1;
2780 dw_val_node dw_loc_oprnd2;
2781 int dw_loc_addr;
2783 dw_loc_descr_node;
2785 /* Location lists are ranges + location descriptions for that range,
2786 so you can track variables that are in different places over
2787 their entire life. */
2788 typedef struct dw_loc_list_struct GTY(())
2790 dw_loc_list_ref dw_loc_next;
2791 const char *begin; /* Label for begin address of range */
2792 const char *end; /* Label for end address of range */
2793 char *ll_symbol; /* Label for beginning of location list.
2794 Only on head of list */
2795 const char *section; /* Section this loclist is relative to */
2796 dw_loc_descr_ref expr;
2797 } dw_loc_list_node;
2799 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2801 static const char *dwarf_stack_op_name (unsigned);
2802 static dw_loc_descr_ref new_loc_descr (enum dwarf_location_atom,
2803 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2804 static void add_loc_descr (dw_loc_descr_ref *, dw_loc_descr_ref);
2805 static unsigned long size_of_loc_descr (dw_loc_descr_ref);
2806 static unsigned long size_of_locs (dw_loc_descr_ref);
2807 static void output_loc_operands (dw_loc_descr_ref);
2808 static void output_loc_sequence (dw_loc_descr_ref);
2810 /* Convert a DWARF stack opcode into its string name. */
2812 static const char *
2813 dwarf_stack_op_name (unsigned int op)
2815 switch (op)
2817 case DW_OP_addr:
2818 case INTERNAL_DW_OP_tls_addr:
2819 return "DW_OP_addr";
2820 case DW_OP_deref:
2821 return "DW_OP_deref";
2822 case DW_OP_const1u:
2823 return "DW_OP_const1u";
2824 case DW_OP_const1s:
2825 return "DW_OP_const1s";
2826 case DW_OP_const2u:
2827 return "DW_OP_const2u";
2828 case DW_OP_const2s:
2829 return "DW_OP_const2s";
2830 case DW_OP_const4u:
2831 return "DW_OP_const4u";
2832 case DW_OP_const4s:
2833 return "DW_OP_const4s";
2834 case DW_OP_const8u:
2835 return "DW_OP_const8u";
2836 case DW_OP_const8s:
2837 return "DW_OP_const8s";
2838 case DW_OP_constu:
2839 return "DW_OP_constu";
2840 case DW_OP_consts:
2841 return "DW_OP_consts";
2842 case DW_OP_dup:
2843 return "DW_OP_dup";
2844 case DW_OP_drop:
2845 return "DW_OP_drop";
2846 case DW_OP_over:
2847 return "DW_OP_over";
2848 case DW_OP_pick:
2849 return "DW_OP_pick";
2850 case DW_OP_swap:
2851 return "DW_OP_swap";
2852 case DW_OP_rot:
2853 return "DW_OP_rot";
2854 case DW_OP_xderef:
2855 return "DW_OP_xderef";
2856 case DW_OP_abs:
2857 return "DW_OP_abs";
2858 case DW_OP_and:
2859 return "DW_OP_and";
2860 case DW_OP_div:
2861 return "DW_OP_div";
2862 case DW_OP_minus:
2863 return "DW_OP_minus";
2864 case DW_OP_mod:
2865 return "DW_OP_mod";
2866 case DW_OP_mul:
2867 return "DW_OP_mul";
2868 case DW_OP_neg:
2869 return "DW_OP_neg";
2870 case DW_OP_not:
2871 return "DW_OP_not";
2872 case DW_OP_or:
2873 return "DW_OP_or";
2874 case DW_OP_plus:
2875 return "DW_OP_plus";
2876 case DW_OP_plus_uconst:
2877 return "DW_OP_plus_uconst";
2878 case DW_OP_shl:
2879 return "DW_OP_shl";
2880 case DW_OP_shr:
2881 return "DW_OP_shr";
2882 case DW_OP_shra:
2883 return "DW_OP_shra";
2884 case DW_OP_xor:
2885 return "DW_OP_xor";
2886 case DW_OP_bra:
2887 return "DW_OP_bra";
2888 case DW_OP_eq:
2889 return "DW_OP_eq";
2890 case DW_OP_ge:
2891 return "DW_OP_ge";
2892 case DW_OP_gt:
2893 return "DW_OP_gt";
2894 case DW_OP_le:
2895 return "DW_OP_le";
2896 case DW_OP_lt:
2897 return "DW_OP_lt";
2898 case DW_OP_ne:
2899 return "DW_OP_ne";
2900 case DW_OP_skip:
2901 return "DW_OP_skip";
2902 case DW_OP_lit0:
2903 return "DW_OP_lit0";
2904 case DW_OP_lit1:
2905 return "DW_OP_lit1";
2906 case DW_OP_lit2:
2907 return "DW_OP_lit2";
2908 case DW_OP_lit3:
2909 return "DW_OP_lit3";
2910 case DW_OP_lit4:
2911 return "DW_OP_lit4";
2912 case DW_OP_lit5:
2913 return "DW_OP_lit5";
2914 case DW_OP_lit6:
2915 return "DW_OP_lit6";
2916 case DW_OP_lit7:
2917 return "DW_OP_lit7";
2918 case DW_OP_lit8:
2919 return "DW_OP_lit8";
2920 case DW_OP_lit9:
2921 return "DW_OP_lit9";
2922 case DW_OP_lit10:
2923 return "DW_OP_lit10";
2924 case DW_OP_lit11:
2925 return "DW_OP_lit11";
2926 case DW_OP_lit12:
2927 return "DW_OP_lit12";
2928 case DW_OP_lit13:
2929 return "DW_OP_lit13";
2930 case DW_OP_lit14:
2931 return "DW_OP_lit14";
2932 case DW_OP_lit15:
2933 return "DW_OP_lit15";
2934 case DW_OP_lit16:
2935 return "DW_OP_lit16";
2936 case DW_OP_lit17:
2937 return "DW_OP_lit17";
2938 case DW_OP_lit18:
2939 return "DW_OP_lit18";
2940 case DW_OP_lit19:
2941 return "DW_OP_lit19";
2942 case DW_OP_lit20:
2943 return "DW_OP_lit20";
2944 case DW_OP_lit21:
2945 return "DW_OP_lit21";
2946 case DW_OP_lit22:
2947 return "DW_OP_lit22";
2948 case DW_OP_lit23:
2949 return "DW_OP_lit23";
2950 case DW_OP_lit24:
2951 return "DW_OP_lit24";
2952 case DW_OP_lit25:
2953 return "DW_OP_lit25";
2954 case DW_OP_lit26:
2955 return "DW_OP_lit26";
2956 case DW_OP_lit27:
2957 return "DW_OP_lit27";
2958 case DW_OP_lit28:
2959 return "DW_OP_lit28";
2960 case DW_OP_lit29:
2961 return "DW_OP_lit29";
2962 case DW_OP_lit30:
2963 return "DW_OP_lit30";
2964 case DW_OP_lit31:
2965 return "DW_OP_lit31";
2966 case DW_OP_reg0:
2967 return "DW_OP_reg0";
2968 case DW_OP_reg1:
2969 return "DW_OP_reg1";
2970 case DW_OP_reg2:
2971 return "DW_OP_reg2";
2972 case DW_OP_reg3:
2973 return "DW_OP_reg3";
2974 case DW_OP_reg4:
2975 return "DW_OP_reg4";
2976 case DW_OP_reg5:
2977 return "DW_OP_reg5";
2978 case DW_OP_reg6:
2979 return "DW_OP_reg6";
2980 case DW_OP_reg7:
2981 return "DW_OP_reg7";
2982 case DW_OP_reg8:
2983 return "DW_OP_reg8";
2984 case DW_OP_reg9:
2985 return "DW_OP_reg9";
2986 case DW_OP_reg10:
2987 return "DW_OP_reg10";
2988 case DW_OP_reg11:
2989 return "DW_OP_reg11";
2990 case DW_OP_reg12:
2991 return "DW_OP_reg12";
2992 case DW_OP_reg13:
2993 return "DW_OP_reg13";
2994 case DW_OP_reg14:
2995 return "DW_OP_reg14";
2996 case DW_OP_reg15:
2997 return "DW_OP_reg15";
2998 case DW_OP_reg16:
2999 return "DW_OP_reg16";
3000 case DW_OP_reg17:
3001 return "DW_OP_reg17";
3002 case DW_OP_reg18:
3003 return "DW_OP_reg18";
3004 case DW_OP_reg19:
3005 return "DW_OP_reg19";
3006 case DW_OP_reg20:
3007 return "DW_OP_reg20";
3008 case DW_OP_reg21:
3009 return "DW_OP_reg21";
3010 case DW_OP_reg22:
3011 return "DW_OP_reg22";
3012 case DW_OP_reg23:
3013 return "DW_OP_reg23";
3014 case DW_OP_reg24:
3015 return "DW_OP_reg24";
3016 case DW_OP_reg25:
3017 return "DW_OP_reg25";
3018 case DW_OP_reg26:
3019 return "DW_OP_reg26";
3020 case DW_OP_reg27:
3021 return "DW_OP_reg27";
3022 case DW_OP_reg28:
3023 return "DW_OP_reg28";
3024 case DW_OP_reg29:
3025 return "DW_OP_reg29";
3026 case DW_OP_reg30:
3027 return "DW_OP_reg30";
3028 case DW_OP_reg31:
3029 return "DW_OP_reg31";
3030 case DW_OP_breg0:
3031 return "DW_OP_breg0";
3032 case DW_OP_breg1:
3033 return "DW_OP_breg1";
3034 case DW_OP_breg2:
3035 return "DW_OP_breg2";
3036 case DW_OP_breg3:
3037 return "DW_OP_breg3";
3038 case DW_OP_breg4:
3039 return "DW_OP_breg4";
3040 case DW_OP_breg5:
3041 return "DW_OP_breg5";
3042 case DW_OP_breg6:
3043 return "DW_OP_breg6";
3044 case DW_OP_breg7:
3045 return "DW_OP_breg7";
3046 case DW_OP_breg8:
3047 return "DW_OP_breg8";
3048 case DW_OP_breg9:
3049 return "DW_OP_breg9";
3050 case DW_OP_breg10:
3051 return "DW_OP_breg10";
3052 case DW_OP_breg11:
3053 return "DW_OP_breg11";
3054 case DW_OP_breg12:
3055 return "DW_OP_breg12";
3056 case DW_OP_breg13:
3057 return "DW_OP_breg13";
3058 case DW_OP_breg14:
3059 return "DW_OP_breg14";
3060 case DW_OP_breg15:
3061 return "DW_OP_breg15";
3062 case DW_OP_breg16:
3063 return "DW_OP_breg16";
3064 case DW_OP_breg17:
3065 return "DW_OP_breg17";
3066 case DW_OP_breg18:
3067 return "DW_OP_breg18";
3068 case DW_OP_breg19:
3069 return "DW_OP_breg19";
3070 case DW_OP_breg20:
3071 return "DW_OP_breg20";
3072 case DW_OP_breg21:
3073 return "DW_OP_breg21";
3074 case DW_OP_breg22:
3075 return "DW_OP_breg22";
3076 case DW_OP_breg23:
3077 return "DW_OP_breg23";
3078 case DW_OP_breg24:
3079 return "DW_OP_breg24";
3080 case DW_OP_breg25:
3081 return "DW_OP_breg25";
3082 case DW_OP_breg26:
3083 return "DW_OP_breg26";
3084 case DW_OP_breg27:
3085 return "DW_OP_breg27";
3086 case DW_OP_breg28:
3087 return "DW_OP_breg28";
3088 case DW_OP_breg29:
3089 return "DW_OP_breg29";
3090 case DW_OP_breg30:
3091 return "DW_OP_breg30";
3092 case DW_OP_breg31:
3093 return "DW_OP_breg31";
3094 case DW_OP_regx:
3095 return "DW_OP_regx";
3096 case DW_OP_fbreg:
3097 return "DW_OP_fbreg";
3098 case DW_OP_bregx:
3099 return "DW_OP_bregx";
3100 case DW_OP_piece:
3101 return "DW_OP_piece";
3102 case DW_OP_deref_size:
3103 return "DW_OP_deref_size";
3104 case DW_OP_xderef_size:
3105 return "DW_OP_xderef_size";
3106 case DW_OP_nop:
3107 return "DW_OP_nop";
3108 case DW_OP_push_object_address:
3109 return "DW_OP_push_object_address";
3110 case DW_OP_call2:
3111 return "DW_OP_call2";
3112 case DW_OP_call4:
3113 return "DW_OP_call4";
3114 case DW_OP_call_ref:
3115 return "DW_OP_call_ref";
3116 case DW_OP_GNU_push_tls_address:
3117 return "DW_OP_GNU_push_tls_address";
3118 default:
3119 return "OP_<unknown>";
3123 /* Return a pointer to a newly allocated location description. Location
3124 descriptions are simple expression terms that can be strung
3125 together to form more complicated location (address) descriptions. */
3127 static inline dw_loc_descr_ref
3128 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
3129 unsigned HOST_WIDE_INT oprnd2)
3131 dw_loc_descr_ref descr = ggc_alloc_cleared (sizeof (dw_loc_descr_node));
3133 descr->dw_loc_opc = op;
3134 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
3135 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
3136 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
3137 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
3139 return descr;
3142 /* Add a location description term to a location description expression. */
3144 static inline void
3145 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
3147 dw_loc_descr_ref *d;
3149 /* Find the end of the chain. */
3150 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
3153 *d = descr;
3156 /* Return the size of a location descriptor. */
3158 static unsigned long
3159 size_of_loc_descr (dw_loc_descr_ref loc)
3161 unsigned long size = 1;
3163 switch (loc->dw_loc_opc)
3165 case DW_OP_addr:
3166 case INTERNAL_DW_OP_tls_addr:
3167 size += DWARF2_ADDR_SIZE;
3168 break;
3169 case DW_OP_const1u:
3170 case DW_OP_const1s:
3171 size += 1;
3172 break;
3173 case DW_OP_const2u:
3174 case DW_OP_const2s:
3175 size += 2;
3176 break;
3177 case DW_OP_const4u:
3178 case DW_OP_const4s:
3179 size += 4;
3180 break;
3181 case DW_OP_const8u:
3182 case DW_OP_const8s:
3183 size += 8;
3184 break;
3185 case DW_OP_constu:
3186 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3187 break;
3188 case DW_OP_consts:
3189 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3190 break;
3191 case DW_OP_pick:
3192 size += 1;
3193 break;
3194 case DW_OP_plus_uconst:
3195 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3196 break;
3197 case DW_OP_skip:
3198 case DW_OP_bra:
3199 size += 2;
3200 break;
3201 case DW_OP_breg0:
3202 case DW_OP_breg1:
3203 case DW_OP_breg2:
3204 case DW_OP_breg3:
3205 case DW_OP_breg4:
3206 case DW_OP_breg5:
3207 case DW_OP_breg6:
3208 case DW_OP_breg7:
3209 case DW_OP_breg8:
3210 case DW_OP_breg9:
3211 case DW_OP_breg10:
3212 case DW_OP_breg11:
3213 case DW_OP_breg12:
3214 case DW_OP_breg13:
3215 case DW_OP_breg14:
3216 case DW_OP_breg15:
3217 case DW_OP_breg16:
3218 case DW_OP_breg17:
3219 case DW_OP_breg18:
3220 case DW_OP_breg19:
3221 case DW_OP_breg20:
3222 case DW_OP_breg21:
3223 case DW_OP_breg22:
3224 case DW_OP_breg23:
3225 case DW_OP_breg24:
3226 case DW_OP_breg25:
3227 case DW_OP_breg26:
3228 case DW_OP_breg27:
3229 case DW_OP_breg28:
3230 case DW_OP_breg29:
3231 case DW_OP_breg30:
3232 case DW_OP_breg31:
3233 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3234 break;
3235 case DW_OP_regx:
3236 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3237 break;
3238 case DW_OP_fbreg:
3239 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3240 break;
3241 case DW_OP_bregx:
3242 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3243 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
3244 break;
3245 case DW_OP_piece:
3246 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3247 break;
3248 case DW_OP_deref_size:
3249 case DW_OP_xderef_size:
3250 size += 1;
3251 break;
3252 case DW_OP_call2:
3253 size += 2;
3254 break;
3255 case DW_OP_call4:
3256 size += 4;
3257 break;
3258 case DW_OP_call_ref:
3259 size += DWARF2_ADDR_SIZE;
3260 break;
3261 default:
3262 break;
3265 return size;
3268 /* Return the size of a series of location descriptors. */
3270 static unsigned long
3271 size_of_locs (dw_loc_descr_ref loc)
3273 dw_loc_descr_ref l;
3274 unsigned long size;
3276 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
3277 field, to avoid writing to a PCH file. */
3278 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3280 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
3281 break;
3282 size += size_of_loc_descr (l);
3284 if (! l)
3285 return size;
3287 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3289 l->dw_loc_addr = size;
3290 size += size_of_loc_descr (l);
3293 return size;
3296 /* Output location description stack opcode's operands (if any). */
3298 static void
3299 output_loc_operands (dw_loc_descr_ref loc)
3301 dw_val_ref val1 = &loc->dw_loc_oprnd1;
3302 dw_val_ref val2 = &loc->dw_loc_oprnd2;
3304 switch (loc->dw_loc_opc)
3306 #ifdef DWARF2_DEBUGGING_INFO
3307 case DW_OP_addr:
3308 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
3309 break;
3310 case DW_OP_const2u:
3311 case DW_OP_const2s:
3312 dw2_asm_output_data (2, val1->v.val_int, NULL);
3313 break;
3314 case DW_OP_const4u:
3315 case DW_OP_const4s:
3316 dw2_asm_output_data (4, val1->v.val_int, NULL);
3317 break;
3318 case DW_OP_const8u:
3319 case DW_OP_const8s:
3320 gcc_assert (HOST_BITS_PER_LONG >= 64);
3321 dw2_asm_output_data (8, val1->v.val_int, NULL);
3322 break;
3323 case DW_OP_skip:
3324 case DW_OP_bra:
3326 int offset;
3328 gcc_assert (val1->val_class == dw_val_class_loc);
3329 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
3331 dw2_asm_output_data (2, offset, NULL);
3333 break;
3334 #else
3335 case DW_OP_addr:
3336 case DW_OP_const2u:
3337 case DW_OP_const2s:
3338 case DW_OP_const4u:
3339 case DW_OP_const4s:
3340 case DW_OP_const8u:
3341 case DW_OP_const8s:
3342 case DW_OP_skip:
3343 case DW_OP_bra:
3344 /* We currently don't make any attempt to make sure these are
3345 aligned properly like we do for the main unwind info, so
3346 don't support emitting things larger than a byte if we're
3347 only doing unwinding. */
3348 gcc_unreachable ();
3349 #endif
3350 case DW_OP_const1u:
3351 case DW_OP_const1s:
3352 dw2_asm_output_data (1, val1->v.val_int, NULL);
3353 break;
3354 case DW_OP_constu:
3355 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3356 break;
3357 case DW_OP_consts:
3358 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3359 break;
3360 case DW_OP_pick:
3361 dw2_asm_output_data (1, val1->v.val_int, NULL);
3362 break;
3363 case DW_OP_plus_uconst:
3364 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3365 break;
3366 case DW_OP_breg0:
3367 case DW_OP_breg1:
3368 case DW_OP_breg2:
3369 case DW_OP_breg3:
3370 case DW_OP_breg4:
3371 case DW_OP_breg5:
3372 case DW_OP_breg6:
3373 case DW_OP_breg7:
3374 case DW_OP_breg8:
3375 case DW_OP_breg9:
3376 case DW_OP_breg10:
3377 case DW_OP_breg11:
3378 case DW_OP_breg12:
3379 case DW_OP_breg13:
3380 case DW_OP_breg14:
3381 case DW_OP_breg15:
3382 case DW_OP_breg16:
3383 case DW_OP_breg17:
3384 case DW_OP_breg18:
3385 case DW_OP_breg19:
3386 case DW_OP_breg20:
3387 case DW_OP_breg21:
3388 case DW_OP_breg22:
3389 case DW_OP_breg23:
3390 case DW_OP_breg24:
3391 case DW_OP_breg25:
3392 case DW_OP_breg26:
3393 case DW_OP_breg27:
3394 case DW_OP_breg28:
3395 case DW_OP_breg29:
3396 case DW_OP_breg30:
3397 case DW_OP_breg31:
3398 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3399 break;
3400 case DW_OP_regx:
3401 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3402 break;
3403 case DW_OP_fbreg:
3404 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3405 break;
3406 case DW_OP_bregx:
3407 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3408 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
3409 break;
3410 case DW_OP_piece:
3411 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3412 break;
3413 case DW_OP_deref_size:
3414 case DW_OP_xderef_size:
3415 dw2_asm_output_data (1, val1->v.val_int, NULL);
3416 break;
3418 case INTERNAL_DW_OP_tls_addr:
3419 if (targetm.asm_out.output_dwarf_dtprel)
3421 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
3422 DWARF2_ADDR_SIZE,
3423 val1->v.val_addr);
3424 fputc ('\n', asm_out_file);
3426 else
3427 gcc_unreachable ();
3428 break;
3430 default:
3431 /* Other codes have no operands. */
3432 break;
3436 /* Output a sequence of location operations. */
3438 static void
3439 output_loc_sequence (dw_loc_descr_ref loc)
3441 for (; loc != NULL; loc = loc->dw_loc_next)
3443 /* Output the opcode. */
3444 dw2_asm_output_data (1, loc->dw_loc_opc,
3445 "%s", dwarf_stack_op_name (loc->dw_loc_opc));
3447 /* Output the operand(s) (if any). */
3448 output_loc_operands (loc);
3452 /* This routine will generate the correct assembly data for a location
3453 description based on a cfi entry with a complex address. */
3455 static void
3456 output_cfa_loc (dw_cfi_ref cfi)
3458 dw_loc_descr_ref loc;
3459 unsigned long size;
3461 /* Output the size of the block. */
3462 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
3463 size = size_of_locs (loc);
3464 dw2_asm_output_data_uleb128 (size, NULL);
3466 /* Now output the operations themselves. */
3467 output_loc_sequence (loc);
3470 /* This function builds a dwarf location descriptor sequence from a
3471 dw_cfa_location, adding the given OFFSET to the result of the
3472 expression. */
3474 static struct dw_loc_descr_struct *
3475 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
3477 struct dw_loc_descr_struct *head, *tmp;
3479 offset += cfa->offset;
3481 if (cfa->indirect)
3483 if (cfa->base_offset)
3485 if (cfa->reg <= 31)
3486 head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->base_offset, 0);
3487 else
3488 head = new_loc_descr (DW_OP_bregx, cfa->reg, cfa->base_offset);
3490 else if (cfa->reg <= 31)
3491 head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3492 else
3493 head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3495 head->dw_loc_oprnd1.val_class = dw_val_class_const;
3496 tmp = new_loc_descr (DW_OP_deref, 0, 0);
3497 add_loc_descr (&head, tmp);
3498 if (offset != 0)
3500 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
3501 add_loc_descr (&head, tmp);
3504 else
3506 if (offset == 0)
3507 if (cfa->reg <= 31)
3508 head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3509 else
3510 head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3511 else if (cfa->reg <= 31)
3512 head = new_loc_descr (DW_OP_breg0 + cfa->reg, offset, 0);
3513 else
3514 head = new_loc_descr (DW_OP_bregx, cfa->reg, offset);
3517 return head;
3520 /* This function fills in aa dw_cfa_location structure from a dwarf location
3521 descriptor sequence. */
3523 static void
3524 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
3526 struct dw_loc_descr_struct *ptr;
3527 cfa->offset = 0;
3528 cfa->base_offset = 0;
3529 cfa->indirect = 0;
3530 cfa->reg = -1;
3532 for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
3534 enum dwarf_location_atom op = ptr->dw_loc_opc;
3536 switch (op)
3538 case DW_OP_reg0:
3539 case DW_OP_reg1:
3540 case DW_OP_reg2:
3541 case DW_OP_reg3:
3542 case DW_OP_reg4:
3543 case DW_OP_reg5:
3544 case DW_OP_reg6:
3545 case DW_OP_reg7:
3546 case DW_OP_reg8:
3547 case DW_OP_reg9:
3548 case DW_OP_reg10:
3549 case DW_OP_reg11:
3550 case DW_OP_reg12:
3551 case DW_OP_reg13:
3552 case DW_OP_reg14:
3553 case DW_OP_reg15:
3554 case DW_OP_reg16:
3555 case DW_OP_reg17:
3556 case DW_OP_reg18:
3557 case DW_OP_reg19:
3558 case DW_OP_reg20:
3559 case DW_OP_reg21:
3560 case DW_OP_reg22:
3561 case DW_OP_reg23:
3562 case DW_OP_reg24:
3563 case DW_OP_reg25:
3564 case DW_OP_reg26:
3565 case DW_OP_reg27:
3566 case DW_OP_reg28:
3567 case DW_OP_reg29:
3568 case DW_OP_reg30:
3569 case DW_OP_reg31:
3570 cfa->reg = op - DW_OP_reg0;
3571 break;
3572 case DW_OP_regx:
3573 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3574 break;
3575 case DW_OP_breg0:
3576 case DW_OP_breg1:
3577 case DW_OP_breg2:
3578 case DW_OP_breg3:
3579 case DW_OP_breg4:
3580 case DW_OP_breg5:
3581 case DW_OP_breg6:
3582 case DW_OP_breg7:
3583 case DW_OP_breg8:
3584 case DW_OP_breg9:
3585 case DW_OP_breg10:
3586 case DW_OP_breg11:
3587 case DW_OP_breg12:
3588 case DW_OP_breg13:
3589 case DW_OP_breg14:
3590 case DW_OP_breg15:
3591 case DW_OP_breg16:
3592 case DW_OP_breg17:
3593 case DW_OP_breg18:
3594 case DW_OP_breg19:
3595 case DW_OP_breg20:
3596 case DW_OP_breg21:
3597 case DW_OP_breg22:
3598 case DW_OP_breg23:
3599 case DW_OP_breg24:
3600 case DW_OP_breg25:
3601 case DW_OP_breg26:
3602 case DW_OP_breg27:
3603 case DW_OP_breg28:
3604 case DW_OP_breg29:
3605 case DW_OP_breg30:
3606 case DW_OP_breg31:
3607 cfa->reg = op - DW_OP_breg0;
3608 cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
3609 break;
3610 case DW_OP_bregx:
3611 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3612 cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
3613 break;
3614 case DW_OP_deref:
3615 cfa->indirect = 1;
3616 break;
3617 case DW_OP_plus_uconst:
3618 cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
3619 break;
3620 default:
3621 internal_error ("DW_LOC_OP %s not implemented",
3622 dwarf_stack_op_name (ptr->dw_loc_opc));
3626 #endif /* .debug_frame support */
3628 /* And now, the support for symbolic debugging information. */
3629 #ifdef DWARF2_DEBUGGING_INFO
3631 /* .debug_str support. */
3632 static int output_indirect_string (void **, void *);
3634 static void dwarf2out_init (const char *);
3635 static void dwarf2out_finish (const char *);
3636 static void dwarf2out_define (unsigned int, const char *);
3637 static void dwarf2out_undef (unsigned int, const char *);
3638 static void dwarf2out_start_source_file (unsigned, const char *);
3639 static void dwarf2out_end_source_file (unsigned);
3640 static void dwarf2out_begin_block (unsigned, unsigned);
3641 static void dwarf2out_end_block (unsigned, unsigned);
3642 static bool dwarf2out_ignore_block (tree);
3643 static void dwarf2out_global_decl (tree);
3644 static void dwarf2out_type_decl (tree, int);
3645 static void dwarf2out_imported_module_or_decl (tree, tree);
3646 static void dwarf2out_abstract_function (tree);
3647 static void dwarf2out_var_location (rtx);
3648 static void dwarf2out_begin_function (tree);
3649 static void dwarf2out_switch_text_section (void);
3651 /* The debug hooks structure. */
3653 const struct gcc_debug_hooks dwarf2_debug_hooks =
3655 dwarf2out_init,
3656 dwarf2out_finish,
3657 dwarf2out_define,
3658 dwarf2out_undef,
3659 dwarf2out_start_source_file,
3660 dwarf2out_end_source_file,
3661 dwarf2out_begin_block,
3662 dwarf2out_end_block,
3663 dwarf2out_ignore_block,
3664 dwarf2out_source_line,
3665 dwarf2out_begin_prologue,
3666 debug_nothing_int_charstar, /* end_prologue */
3667 dwarf2out_end_epilogue,
3668 dwarf2out_begin_function,
3669 debug_nothing_int, /* end_function */
3670 dwarf2out_decl, /* function_decl */
3671 dwarf2out_global_decl,
3672 dwarf2out_type_decl, /* type_decl */
3673 dwarf2out_imported_module_or_decl,
3674 debug_nothing_tree, /* deferred_inline_function */
3675 /* The DWARF 2 backend tries to reduce debugging bloat by not
3676 emitting the abstract description of inline functions until
3677 something tries to reference them. */
3678 dwarf2out_abstract_function, /* outlining_inline_function */
3679 debug_nothing_rtx, /* label */
3680 debug_nothing_int, /* handle_pch */
3681 dwarf2out_var_location,
3682 dwarf2out_switch_text_section,
3683 1 /* start_end_main_source_file */
3685 #endif
3687 /* NOTE: In the comments in this file, many references are made to
3688 "Debugging Information Entries". This term is abbreviated as `DIE'
3689 throughout the remainder of this file. */
3691 /* An internal representation of the DWARF output is built, and then
3692 walked to generate the DWARF debugging info. The walk of the internal
3693 representation is done after the entire program has been compiled.
3694 The types below are used to describe the internal representation. */
3696 /* Various DIE's use offsets relative to the beginning of the
3697 .debug_info section to refer to each other. */
3699 typedef long int dw_offset;
3701 /* Define typedefs here to avoid circular dependencies. */
3703 typedef struct dw_attr_struct *dw_attr_ref;
3704 typedef struct dw_line_info_struct *dw_line_info_ref;
3705 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
3706 typedef struct pubname_struct *pubname_ref;
3707 typedef struct dw_ranges_struct *dw_ranges_ref;
3709 /* Each entry in the line_info_table maintains the file and
3710 line number associated with the label generated for that
3711 entry. The label gives the PC value associated with
3712 the line number entry. */
3714 typedef struct dw_line_info_struct GTY(())
3716 unsigned long dw_file_num;
3717 unsigned long dw_line_num;
3719 dw_line_info_entry;
3721 /* Line information for functions in separate sections; each one gets its
3722 own sequence. */
3723 typedef struct dw_separate_line_info_struct GTY(())
3725 unsigned long dw_file_num;
3726 unsigned long dw_line_num;
3727 unsigned long function;
3729 dw_separate_line_info_entry;
3731 /* Each DIE attribute has a field specifying the attribute kind,
3732 a link to the next attribute in the chain, and an attribute value.
3733 Attributes are typically linked below the DIE they modify. */
3735 typedef struct dw_attr_struct GTY(())
3737 enum dwarf_attribute dw_attr;
3738 dw_val_node dw_attr_val;
3740 dw_attr_node;
3742 DEF_VEC_O(dw_attr_node);
3743 DEF_VEC_ALLOC_O(dw_attr_node,gc);
3745 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
3746 The children of each node form a circular list linked by
3747 die_sib. die_child points to the node *before* the "first" child node. */
3749 typedef struct die_struct GTY(())
3751 enum dwarf_tag die_tag;
3752 char *die_symbol;
3753 VEC(dw_attr_node,gc) * die_attr;
3754 dw_die_ref die_parent;
3755 dw_die_ref die_child;
3756 dw_die_ref die_sib;
3757 dw_die_ref die_definition; /* ref from a specification to its definition */
3758 dw_offset die_offset;
3759 unsigned long die_abbrev;
3760 int die_mark;
3761 /* Die is used and must not be pruned as unused. */
3762 int die_perennial_p;
3763 unsigned int decl_id;
3765 die_node;
3767 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
3768 #define FOR_EACH_CHILD(die, c, expr) do { \
3769 c = die->die_child; \
3770 if (c) do { \
3771 c = c->die_sib; \
3772 expr; \
3773 } while (c != die->die_child); \
3774 } while (0)
3776 /* The pubname structure */
3778 typedef struct pubname_struct GTY(())
3780 dw_die_ref die;
3781 const char *name;
3783 pubname_entry;
3785 DEF_VEC_O(pubname_entry);
3786 DEF_VEC_ALLOC_O(pubname_entry, gc);
3788 struct dw_ranges_struct GTY(())
3790 int block_num;
3793 /* The limbo die list structure. */
3794 typedef struct limbo_die_struct GTY(())
3796 dw_die_ref die;
3797 tree created_for;
3798 struct limbo_die_struct *next;
3800 limbo_die_node;
3802 /* How to start an assembler comment. */
3803 #ifndef ASM_COMMENT_START
3804 #define ASM_COMMENT_START ";#"
3805 #endif
3807 /* Define a macro which returns nonzero for a TYPE_DECL which was
3808 implicitly generated for a tagged type.
3810 Note that unlike the gcc front end (which generates a NULL named
3811 TYPE_DECL node for each complete tagged type, each array type, and
3812 each function type node created) the g++ front end generates a
3813 _named_ TYPE_DECL node for each tagged type node created.
3814 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3815 generate a DW_TAG_typedef DIE for them. */
3817 #define TYPE_DECL_IS_STUB(decl) \
3818 (DECL_NAME (decl) == NULL_TREE \
3819 || (DECL_ARTIFICIAL (decl) \
3820 && is_tagged_type (TREE_TYPE (decl)) \
3821 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3822 /* This is necessary for stub decls that \
3823 appear in nested inline functions. */ \
3824 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3825 && (decl_ultimate_origin (decl) \
3826 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3828 /* Information concerning the compilation unit's programming
3829 language, and compiler version. */
3831 /* Fixed size portion of the DWARF compilation unit header. */
3832 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3833 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
3835 /* Fixed size portion of public names info. */
3836 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3838 /* Fixed size portion of the address range info. */
3839 #define DWARF_ARANGES_HEADER_SIZE \
3840 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3841 DWARF2_ADDR_SIZE * 2) \
3842 - DWARF_INITIAL_LENGTH_SIZE)
3844 /* Size of padding portion in the address range info. It must be
3845 aligned to twice the pointer size. */
3846 #define DWARF_ARANGES_PAD_SIZE \
3847 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3848 DWARF2_ADDR_SIZE * 2) \
3849 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3851 /* Use assembler line directives if available. */
3852 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3853 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3854 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3855 #else
3856 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3857 #endif
3858 #endif
3860 /* Minimum line offset in a special line info. opcode.
3861 This value was chosen to give a reasonable range of values. */
3862 #define DWARF_LINE_BASE -10
3864 /* First special line opcode - leave room for the standard opcodes. */
3865 #define DWARF_LINE_OPCODE_BASE 10
3867 /* Range of line offsets in a special line info. opcode. */
3868 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3870 /* Flag that indicates the initial value of the is_stmt_start flag.
3871 In the present implementation, we do not mark any lines as
3872 the beginning of a source statement, because that information
3873 is not made available by the GCC front-end. */
3874 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3876 #ifdef DWARF2_DEBUGGING_INFO
3877 /* This location is used by calc_die_sizes() to keep track
3878 the offset of each DIE within the .debug_info section. */
3879 static unsigned long next_die_offset;
3880 #endif
3882 /* Record the root of the DIE's built for the current compilation unit. */
3883 static GTY(()) dw_die_ref comp_unit_die;
3885 /* A list of DIEs with a NULL parent waiting to be relocated. */
3886 static GTY(()) limbo_die_node *limbo_die_list;
3888 /* Filenames referenced by this compilation unit. */
3889 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
3891 /* A hash table of references to DIE's that describe declarations.
3892 The key is a DECL_UID() which is a unique number identifying each decl. */
3893 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
3895 /* Node of the variable location list. */
3896 struct var_loc_node GTY ((chain_next ("%h.next")))
3898 rtx GTY (()) var_loc_note;
3899 const char * GTY (()) label;
3900 const char * GTY (()) section_label;
3901 struct var_loc_node * GTY (()) next;
3904 /* Variable location list. */
3905 struct var_loc_list_def GTY (())
3907 struct var_loc_node * GTY (()) first;
3909 /* Do not mark the last element of the chained list because
3910 it is marked through the chain. */
3911 struct var_loc_node * GTY ((skip ("%h"))) last;
3913 /* DECL_UID of the variable decl. */
3914 unsigned int decl_id;
3916 typedef struct var_loc_list_def var_loc_list;
3919 /* Table of decl location linked lists. */
3920 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
3922 /* A pointer to the base of a list of references to DIE's that
3923 are uniquely identified by their tag, presence/absence of
3924 children DIE's, and list of attribute/value pairs. */
3925 static GTY((length ("abbrev_die_table_allocated")))
3926 dw_die_ref *abbrev_die_table;
3928 /* Number of elements currently allocated for abbrev_die_table. */
3929 static GTY(()) unsigned abbrev_die_table_allocated;
3931 /* Number of elements in type_die_table currently in use. */
3932 static GTY(()) unsigned abbrev_die_table_in_use;
3934 /* Size (in elements) of increments by which we may expand the
3935 abbrev_die_table. */
3936 #define ABBREV_DIE_TABLE_INCREMENT 256
3938 /* A pointer to the base of a table that contains line information
3939 for each source code line in .text in the compilation unit. */
3940 static GTY((length ("line_info_table_allocated")))
3941 dw_line_info_ref line_info_table;
3943 /* Number of elements currently allocated for line_info_table. */
3944 static GTY(()) unsigned line_info_table_allocated;
3946 /* Number of elements in line_info_table currently in use. */
3947 static GTY(()) unsigned line_info_table_in_use;
3949 /* True if the compilation unit places functions in more than one section. */
3950 static GTY(()) bool have_multiple_function_sections = false;
3952 /* A pointer to the base of a table that contains line information
3953 for each source code line outside of .text in the compilation unit. */
3954 static GTY ((length ("separate_line_info_table_allocated")))
3955 dw_separate_line_info_ref separate_line_info_table;
3957 /* Number of elements currently allocated for separate_line_info_table. */
3958 static GTY(()) unsigned separate_line_info_table_allocated;
3960 /* Number of elements in separate_line_info_table currently in use. */
3961 static GTY(()) unsigned separate_line_info_table_in_use;
3963 /* Size (in elements) of increments by which we may expand the
3964 line_info_table. */
3965 #define LINE_INFO_TABLE_INCREMENT 1024
3967 /* A pointer to the base of a table that contains a list of publicly
3968 accessible names. */
3969 static GTY (()) VEC (pubname_entry, gc) * pubname_table;
3971 /* A pointer to the base of a table that contains a list of publicly
3972 accessible types. */
3973 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
3975 /* Array of dies for which we should generate .debug_arange info. */
3976 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
3978 /* Number of elements currently allocated for arange_table. */
3979 static GTY(()) unsigned arange_table_allocated;
3981 /* Number of elements in arange_table currently in use. */
3982 static GTY(()) unsigned arange_table_in_use;
3984 /* Size (in elements) of increments by which we may expand the
3985 arange_table. */
3986 #define ARANGE_TABLE_INCREMENT 64
3988 /* Array of dies for which we should generate .debug_ranges info. */
3989 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
3991 /* Number of elements currently allocated for ranges_table. */
3992 static GTY(()) unsigned ranges_table_allocated;
3994 /* Number of elements in ranges_table currently in use. */
3995 static GTY(()) unsigned ranges_table_in_use;
3997 /* Size (in elements) of increments by which we may expand the
3998 ranges_table. */
3999 #define RANGES_TABLE_INCREMENT 64
4001 /* Whether we have location lists that need outputting */
4002 static GTY(()) bool have_location_lists;
4004 /* Unique label counter. */
4005 static GTY(()) unsigned int loclabel_num;
4007 #ifdef DWARF2_DEBUGGING_INFO
4008 /* Record whether the function being analyzed contains inlined functions. */
4009 static int current_function_has_inlines;
4010 #endif
4011 #if 0 && defined (MIPS_DEBUGGING_INFO)
4012 static int comp_unit_has_inlines;
4013 #endif
4015 /* The last file entry emitted by maybe_emit_file(). */
4016 static GTY(()) struct dwarf_file_data * last_emitted_file;
4018 /* Number of internal labels generated by gen_internal_sym(). */
4019 static GTY(()) int label_num;
4021 /* Cached result of previous call to lookup_filename. */
4022 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
4024 #ifdef DWARF2_DEBUGGING_INFO
4026 /* Offset from the "steady-state frame pointer" to the frame base,
4027 within the current function. */
4028 static HOST_WIDE_INT frame_pointer_fb_offset;
4030 /* Forward declarations for functions defined in this file. */
4032 static int is_pseudo_reg (rtx);
4033 static tree type_main_variant (tree);
4034 static int is_tagged_type (tree);
4035 static const char *dwarf_tag_name (unsigned);
4036 static const char *dwarf_attr_name (unsigned);
4037 static const char *dwarf_form_name (unsigned);
4038 static tree decl_ultimate_origin (tree);
4039 static tree block_ultimate_origin (tree);
4040 static tree decl_class_context (tree);
4041 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
4042 static inline enum dw_val_class AT_class (dw_attr_ref);
4043 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
4044 static inline unsigned AT_flag (dw_attr_ref);
4045 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
4046 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
4047 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
4048 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
4049 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
4050 unsigned long);
4051 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
4052 unsigned int, unsigned char *);
4053 static hashval_t debug_str_do_hash (const void *);
4054 static int debug_str_eq (const void *, const void *);
4055 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
4056 static inline const char *AT_string (dw_attr_ref);
4057 static int AT_string_form (dw_attr_ref);
4058 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
4059 static void add_AT_specification (dw_die_ref, dw_die_ref);
4060 static inline dw_die_ref AT_ref (dw_attr_ref);
4061 static inline int AT_ref_external (dw_attr_ref);
4062 static inline void set_AT_ref_external (dw_attr_ref, int);
4063 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
4064 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
4065 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
4066 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
4067 dw_loc_list_ref);
4068 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
4069 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
4070 static inline rtx AT_addr (dw_attr_ref);
4071 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
4072 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
4073 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
4074 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
4075 unsigned HOST_WIDE_INT);
4076 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
4077 unsigned long);
4078 static inline const char *AT_lbl (dw_attr_ref);
4079 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
4080 static const char *get_AT_low_pc (dw_die_ref);
4081 static const char *get_AT_hi_pc (dw_die_ref);
4082 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
4083 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
4084 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
4085 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
4086 static bool is_c_family (void);
4087 static bool is_cxx (void);
4088 static bool is_java (void);
4089 static bool is_fortran (void);
4090 static bool is_ada (void);
4091 static void remove_AT (dw_die_ref, enum dwarf_attribute);
4092 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
4093 static void add_child_die (dw_die_ref, dw_die_ref);
4094 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
4095 static dw_die_ref lookup_type_die (tree);
4096 static void equate_type_number_to_die (tree, dw_die_ref);
4097 static hashval_t decl_die_table_hash (const void *);
4098 static int decl_die_table_eq (const void *, const void *);
4099 static dw_die_ref lookup_decl_die (tree);
4100 static hashval_t decl_loc_table_hash (const void *);
4101 static int decl_loc_table_eq (const void *, const void *);
4102 static var_loc_list *lookup_decl_loc (tree);
4103 static void equate_decl_number_to_die (tree, dw_die_ref);
4104 static void add_var_loc_to_decl (tree, struct var_loc_node *);
4105 static void print_spaces (FILE *);
4106 static void print_die (dw_die_ref, FILE *);
4107 static void print_dwarf_line_table (FILE *);
4108 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
4109 static dw_die_ref pop_compile_unit (dw_die_ref);
4110 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
4111 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
4112 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
4113 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
4114 static int same_dw_val_p (dw_val_node *, dw_val_node *, int *);
4115 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
4116 static int same_die_p (dw_die_ref, dw_die_ref, int *);
4117 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
4118 static void compute_section_prefix (dw_die_ref);
4119 static int is_type_die (dw_die_ref);
4120 static int is_comdat_die (dw_die_ref);
4121 static int is_symbol_die (dw_die_ref);
4122 static void assign_symbol_names (dw_die_ref);
4123 static void break_out_includes (dw_die_ref);
4124 static hashval_t htab_cu_hash (const void *);
4125 static int htab_cu_eq (const void *, const void *);
4126 static void htab_cu_del (void *);
4127 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
4128 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
4129 static void add_sibling_attributes (dw_die_ref);
4130 static void build_abbrev_table (dw_die_ref);
4131 static void output_location_lists (dw_die_ref);
4132 static int constant_size (long unsigned);
4133 static unsigned long size_of_die (dw_die_ref);
4134 static void calc_die_sizes (dw_die_ref);
4135 static void mark_dies (dw_die_ref);
4136 static void unmark_dies (dw_die_ref);
4137 static void unmark_all_dies (dw_die_ref);
4138 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
4139 static unsigned long size_of_aranges (void);
4140 static enum dwarf_form value_format (dw_attr_ref);
4141 static void output_value_format (dw_attr_ref);
4142 static void output_abbrev_section (void);
4143 static void output_die_symbol (dw_die_ref);
4144 static void output_die (dw_die_ref);
4145 static void output_compilation_unit_header (void);
4146 static void output_comp_unit (dw_die_ref, int);
4147 static const char *dwarf2_name (tree, int);
4148 static void add_pubname (tree, dw_die_ref);
4149 static void add_pubtype (tree, dw_die_ref);
4150 static void output_pubnames (VEC (pubname_entry,gc) *);
4151 static void add_arange (tree, dw_die_ref);
4152 static void output_aranges (void);
4153 static unsigned int add_ranges (tree);
4154 static void output_ranges (void);
4155 static void output_line_info (void);
4156 static void output_file_names (void);
4157 static dw_die_ref base_type_die (tree);
4158 static int is_base_type (tree);
4159 static bool is_subrange_type (tree);
4160 static dw_die_ref subrange_type_die (tree, dw_die_ref);
4161 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
4162 static int type_is_enum (tree);
4163 static unsigned int dbx_reg_number (rtx);
4164 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
4165 static dw_loc_descr_ref reg_loc_descriptor (rtx);
4166 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int);
4167 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx);
4168 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4169 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT);
4170 static int is_based_loc (rtx);
4171 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode);
4172 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx);
4173 static dw_loc_descr_ref loc_descriptor (rtx);
4174 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
4175 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
4176 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
4177 static tree field_type (tree);
4178 static unsigned int simple_type_align_in_bits (tree);
4179 static unsigned int simple_decl_align_in_bits (tree);
4180 static unsigned HOST_WIDE_INT simple_type_size_in_bits (tree);
4181 static HOST_WIDE_INT field_byte_offset (tree);
4182 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
4183 dw_loc_descr_ref);
4184 static void add_data_member_location_attribute (dw_die_ref, tree);
4185 static void add_const_value_attribute (dw_die_ref, rtx);
4186 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
4187 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4188 static void insert_float (rtx, unsigned char *);
4189 static rtx rtl_for_decl_location (tree);
4190 static void add_location_or_const_value_attribute (dw_die_ref, tree,
4191 enum dwarf_attribute);
4192 static void tree_add_const_value_attribute (dw_die_ref, tree);
4193 static void add_name_attribute (dw_die_ref, const char *);
4194 static void add_comp_dir_attribute (dw_die_ref);
4195 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
4196 static void add_subscript_info (dw_die_ref, tree);
4197 static void add_byte_size_attribute (dw_die_ref, tree);
4198 static void add_bit_offset_attribute (dw_die_ref, tree);
4199 static void add_bit_size_attribute (dw_die_ref, tree);
4200 static void add_prototyped_attribute (dw_die_ref, tree);
4201 static void add_abstract_origin_attribute (dw_die_ref, tree);
4202 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
4203 static void add_src_coords_attributes (dw_die_ref, tree);
4204 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
4205 static void push_decl_scope (tree);
4206 static void pop_decl_scope (void);
4207 static dw_die_ref scope_die_for (tree, dw_die_ref);
4208 static inline int local_scope_p (dw_die_ref);
4209 static inline int class_or_namespace_scope_p (dw_die_ref);
4210 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
4211 static void add_calling_convention_attribute (dw_die_ref, tree);
4212 static const char *type_tag (tree);
4213 static tree member_declared_type (tree);
4214 #if 0
4215 static const char *decl_start_label (tree);
4216 #endif
4217 static void gen_array_type_die (tree, dw_die_ref);
4218 #if 0
4219 static void gen_entry_point_die (tree, dw_die_ref);
4220 #endif
4221 static void gen_inlined_enumeration_type_die (tree, dw_die_ref);
4222 static void gen_inlined_structure_type_die (tree, dw_die_ref);
4223 static void gen_inlined_union_type_die (tree, dw_die_ref);
4224 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
4225 static dw_die_ref gen_formal_parameter_die (tree, dw_die_ref);
4226 static void gen_unspecified_parameters_die (tree, dw_die_ref);
4227 static void gen_formal_types_die (tree, dw_die_ref);
4228 static void gen_subprogram_die (tree, dw_die_ref);
4229 static void gen_variable_die (tree, dw_die_ref);
4230 static void gen_label_die (tree, dw_die_ref);
4231 static void gen_lexical_block_die (tree, dw_die_ref, int);
4232 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
4233 static void gen_field_die (tree, dw_die_ref);
4234 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
4235 static dw_die_ref gen_compile_unit_die (const char *);
4236 static void gen_inheritance_die (tree, tree, dw_die_ref);
4237 static void gen_member_die (tree, dw_die_ref);
4238 static void gen_struct_or_union_type_die (tree, dw_die_ref);
4239 static void gen_subroutine_type_die (tree, dw_die_ref);
4240 static void gen_typedef_die (tree, dw_die_ref);
4241 static void gen_type_die (tree, dw_die_ref);
4242 static void gen_tagged_type_instantiation_die (tree, dw_die_ref);
4243 static void gen_block_die (tree, dw_die_ref, int);
4244 static void decls_for_scope (tree, dw_die_ref, int);
4245 static int is_redundant_typedef (tree);
4246 static void gen_namespace_die (tree);
4247 static void gen_decl_die (tree, dw_die_ref);
4248 static dw_die_ref force_decl_die (tree);
4249 static dw_die_ref force_type_die (tree);
4250 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
4251 static void declare_in_namespace (tree, dw_die_ref);
4252 static struct dwarf_file_data * lookup_filename (const char *);
4253 static void retry_incomplete_types (void);
4254 static void gen_type_die_for_member (tree, tree, dw_die_ref);
4255 static void splice_child_die (dw_die_ref, dw_die_ref);
4256 static int file_info_cmp (const void *, const void *);
4257 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
4258 const char *, const char *, unsigned);
4259 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
4260 const char *, const char *,
4261 const char *);
4262 static void output_loc_list (dw_loc_list_ref);
4263 static char *gen_internal_sym (const char *);
4265 static void prune_unmark_dies (dw_die_ref);
4266 static void prune_unused_types_mark (dw_die_ref, int);
4267 static void prune_unused_types_walk (dw_die_ref);
4268 static void prune_unused_types_walk_attribs (dw_die_ref);
4269 static void prune_unused_types_prune (dw_die_ref);
4270 static void prune_unused_types (void);
4271 static int maybe_emit_file (struct dwarf_file_data *fd);
4273 /* Section names used to hold DWARF debugging information. */
4274 #ifndef DEBUG_INFO_SECTION
4275 #define DEBUG_INFO_SECTION ".debug_info"
4276 #endif
4277 #ifndef DEBUG_ABBREV_SECTION
4278 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
4279 #endif
4280 #ifndef DEBUG_ARANGES_SECTION
4281 #define DEBUG_ARANGES_SECTION ".debug_aranges"
4282 #endif
4283 #ifndef DEBUG_MACINFO_SECTION
4284 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
4285 #endif
4286 #ifndef DEBUG_LINE_SECTION
4287 #define DEBUG_LINE_SECTION ".debug_line"
4288 #endif
4289 #ifndef DEBUG_LOC_SECTION
4290 #define DEBUG_LOC_SECTION ".debug_loc"
4291 #endif
4292 #ifndef DEBUG_PUBNAMES_SECTION
4293 #define DEBUG_PUBNAMES_SECTION ".debug_pubnames"
4294 #endif
4295 #ifndef DEBUG_STR_SECTION
4296 #define DEBUG_STR_SECTION ".debug_str"
4297 #endif
4298 #ifndef DEBUG_RANGES_SECTION
4299 #define DEBUG_RANGES_SECTION ".debug_ranges"
4300 #endif
4302 /* Standard ELF section names for compiled code and data. */
4303 #ifndef TEXT_SECTION_NAME
4304 #define TEXT_SECTION_NAME ".text"
4305 #endif
4307 /* Section flags for .debug_str section. */
4308 #define DEBUG_STR_SECTION_FLAGS \
4309 (HAVE_GAS_SHF_MERGE && flag_merge_constants \
4310 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
4311 : SECTION_DEBUG)
4313 /* Labels we insert at beginning sections we can reference instead of
4314 the section names themselves. */
4316 #ifndef TEXT_SECTION_LABEL
4317 #define TEXT_SECTION_LABEL "Ltext"
4318 #endif
4319 #ifndef COLD_TEXT_SECTION_LABEL
4320 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
4321 #endif
4322 #ifndef DEBUG_LINE_SECTION_LABEL
4323 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
4324 #endif
4325 #ifndef DEBUG_INFO_SECTION_LABEL
4326 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
4327 #endif
4328 #ifndef DEBUG_ABBREV_SECTION_LABEL
4329 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
4330 #endif
4331 #ifndef DEBUG_LOC_SECTION_LABEL
4332 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
4333 #endif
4334 #ifndef DEBUG_RANGES_SECTION_LABEL
4335 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
4336 #endif
4337 #ifndef DEBUG_MACINFO_SECTION_LABEL
4338 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
4339 #endif
4341 /* Definitions of defaults for formats and names of various special
4342 (artificial) labels which may be generated within this file (when the -g
4343 options is used and DWARF2_DEBUGGING_INFO is in effect.
4344 If necessary, these may be overridden from within the tm.h file, but
4345 typically, overriding these defaults is unnecessary. */
4347 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4348 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4349 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4350 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4351 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4352 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4353 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4354 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4355 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4356 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4358 #ifndef TEXT_END_LABEL
4359 #define TEXT_END_LABEL "Letext"
4360 #endif
4361 #ifndef COLD_END_LABEL
4362 #define COLD_END_LABEL "Letext_cold"
4363 #endif
4364 #ifndef BLOCK_BEGIN_LABEL
4365 #define BLOCK_BEGIN_LABEL "LBB"
4366 #endif
4367 #ifndef BLOCK_END_LABEL
4368 #define BLOCK_END_LABEL "LBE"
4369 #endif
4370 #ifndef LINE_CODE_LABEL
4371 #define LINE_CODE_LABEL "LM"
4372 #endif
4373 #ifndef SEPARATE_LINE_CODE_LABEL
4374 #define SEPARATE_LINE_CODE_LABEL "LSM"
4375 #endif
4377 /* We allow a language front-end to designate a function that is to be
4378 called to "demangle" any name before it is put into a DIE. */
4380 static const char *(*demangle_name_func) (const char *);
4382 void
4383 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4385 demangle_name_func = func;
4388 /* Test if rtl node points to a pseudo register. */
4390 static inline int
4391 is_pseudo_reg (rtx rtl)
4393 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4394 || (GET_CODE (rtl) == SUBREG
4395 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4398 /* Return a reference to a type, with its const and volatile qualifiers
4399 removed. */
4401 static inline tree
4402 type_main_variant (tree type)
4404 type = TYPE_MAIN_VARIANT (type);
4406 /* ??? There really should be only one main variant among any group of
4407 variants of a given type (and all of the MAIN_VARIANT values for all
4408 members of the group should point to that one type) but sometimes the C
4409 front-end messes this up for array types, so we work around that bug
4410 here. */
4411 if (TREE_CODE (type) == ARRAY_TYPE)
4412 while (type != TYPE_MAIN_VARIANT (type))
4413 type = TYPE_MAIN_VARIANT (type);
4415 return type;
4418 /* Return nonzero if the given type node represents a tagged type. */
4420 static inline int
4421 is_tagged_type (tree type)
4423 enum tree_code code = TREE_CODE (type);
4425 return (code == RECORD_TYPE || code == UNION_TYPE
4426 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4429 /* Convert a DIE tag into its string name. */
4431 static const char *
4432 dwarf_tag_name (unsigned int tag)
4434 switch (tag)
4436 case DW_TAG_padding:
4437 return "DW_TAG_padding";
4438 case DW_TAG_array_type:
4439 return "DW_TAG_array_type";
4440 case DW_TAG_class_type:
4441 return "DW_TAG_class_type";
4442 case DW_TAG_entry_point:
4443 return "DW_TAG_entry_point";
4444 case DW_TAG_enumeration_type:
4445 return "DW_TAG_enumeration_type";
4446 case DW_TAG_formal_parameter:
4447 return "DW_TAG_formal_parameter";
4448 case DW_TAG_imported_declaration:
4449 return "DW_TAG_imported_declaration";
4450 case DW_TAG_label:
4451 return "DW_TAG_label";
4452 case DW_TAG_lexical_block:
4453 return "DW_TAG_lexical_block";
4454 case DW_TAG_member:
4455 return "DW_TAG_member";
4456 case DW_TAG_pointer_type:
4457 return "DW_TAG_pointer_type";
4458 case DW_TAG_reference_type:
4459 return "DW_TAG_reference_type";
4460 case DW_TAG_compile_unit:
4461 return "DW_TAG_compile_unit";
4462 case DW_TAG_string_type:
4463 return "DW_TAG_string_type";
4464 case DW_TAG_structure_type:
4465 return "DW_TAG_structure_type";
4466 case DW_TAG_subroutine_type:
4467 return "DW_TAG_subroutine_type";
4468 case DW_TAG_typedef:
4469 return "DW_TAG_typedef";
4470 case DW_TAG_union_type:
4471 return "DW_TAG_union_type";
4472 case DW_TAG_unspecified_parameters:
4473 return "DW_TAG_unspecified_parameters";
4474 case DW_TAG_variant:
4475 return "DW_TAG_variant";
4476 case DW_TAG_common_block:
4477 return "DW_TAG_common_block";
4478 case DW_TAG_common_inclusion:
4479 return "DW_TAG_common_inclusion";
4480 case DW_TAG_inheritance:
4481 return "DW_TAG_inheritance";
4482 case DW_TAG_inlined_subroutine:
4483 return "DW_TAG_inlined_subroutine";
4484 case DW_TAG_module:
4485 return "DW_TAG_module";
4486 case DW_TAG_ptr_to_member_type:
4487 return "DW_TAG_ptr_to_member_type";
4488 case DW_TAG_set_type:
4489 return "DW_TAG_set_type";
4490 case DW_TAG_subrange_type:
4491 return "DW_TAG_subrange_type";
4492 case DW_TAG_with_stmt:
4493 return "DW_TAG_with_stmt";
4494 case DW_TAG_access_declaration:
4495 return "DW_TAG_access_declaration";
4496 case DW_TAG_base_type:
4497 return "DW_TAG_base_type";
4498 case DW_TAG_catch_block:
4499 return "DW_TAG_catch_block";
4500 case DW_TAG_const_type:
4501 return "DW_TAG_const_type";
4502 case DW_TAG_constant:
4503 return "DW_TAG_constant";
4504 case DW_TAG_enumerator:
4505 return "DW_TAG_enumerator";
4506 case DW_TAG_file_type:
4507 return "DW_TAG_file_type";
4508 case DW_TAG_friend:
4509 return "DW_TAG_friend";
4510 case DW_TAG_namelist:
4511 return "DW_TAG_namelist";
4512 case DW_TAG_namelist_item:
4513 return "DW_TAG_namelist_item";
4514 case DW_TAG_namespace:
4515 return "DW_TAG_namespace";
4516 case DW_TAG_packed_type:
4517 return "DW_TAG_packed_type";
4518 case DW_TAG_subprogram:
4519 return "DW_TAG_subprogram";
4520 case DW_TAG_template_type_param:
4521 return "DW_TAG_template_type_param";
4522 case DW_TAG_template_value_param:
4523 return "DW_TAG_template_value_param";
4524 case DW_TAG_thrown_type:
4525 return "DW_TAG_thrown_type";
4526 case DW_TAG_try_block:
4527 return "DW_TAG_try_block";
4528 case DW_TAG_variant_part:
4529 return "DW_TAG_variant_part";
4530 case DW_TAG_variable:
4531 return "DW_TAG_variable";
4532 case DW_TAG_volatile_type:
4533 return "DW_TAG_volatile_type";
4534 case DW_TAG_imported_module:
4535 return "DW_TAG_imported_module";
4536 case DW_TAG_MIPS_loop:
4537 return "DW_TAG_MIPS_loop";
4538 case DW_TAG_format_label:
4539 return "DW_TAG_format_label";
4540 case DW_TAG_function_template:
4541 return "DW_TAG_function_template";
4542 case DW_TAG_class_template:
4543 return "DW_TAG_class_template";
4544 case DW_TAG_GNU_BINCL:
4545 return "DW_TAG_GNU_BINCL";
4546 case DW_TAG_GNU_EINCL:
4547 return "DW_TAG_GNU_EINCL";
4548 default:
4549 return "DW_TAG_<unknown>";
4553 /* Convert a DWARF attribute code into its string name. */
4555 static const char *
4556 dwarf_attr_name (unsigned int attr)
4558 switch (attr)
4560 case DW_AT_sibling:
4561 return "DW_AT_sibling";
4562 case DW_AT_location:
4563 return "DW_AT_location";
4564 case DW_AT_name:
4565 return "DW_AT_name";
4566 case DW_AT_ordering:
4567 return "DW_AT_ordering";
4568 case DW_AT_subscr_data:
4569 return "DW_AT_subscr_data";
4570 case DW_AT_byte_size:
4571 return "DW_AT_byte_size";
4572 case DW_AT_bit_offset:
4573 return "DW_AT_bit_offset";
4574 case DW_AT_bit_size:
4575 return "DW_AT_bit_size";
4576 case DW_AT_element_list:
4577 return "DW_AT_element_list";
4578 case DW_AT_stmt_list:
4579 return "DW_AT_stmt_list";
4580 case DW_AT_low_pc:
4581 return "DW_AT_low_pc";
4582 case DW_AT_high_pc:
4583 return "DW_AT_high_pc";
4584 case DW_AT_language:
4585 return "DW_AT_language";
4586 case DW_AT_member:
4587 return "DW_AT_member";
4588 case DW_AT_discr:
4589 return "DW_AT_discr";
4590 case DW_AT_discr_value:
4591 return "DW_AT_discr_value";
4592 case DW_AT_visibility:
4593 return "DW_AT_visibility";
4594 case DW_AT_import:
4595 return "DW_AT_import";
4596 case DW_AT_string_length:
4597 return "DW_AT_string_length";
4598 case DW_AT_common_reference:
4599 return "DW_AT_common_reference";
4600 case DW_AT_comp_dir:
4601 return "DW_AT_comp_dir";
4602 case DW_AT_const_value:
4603 return "DW_AT_const_value";
4604 case DW_AT_containing_type:
4605 return "DW_AT_containing_type";
4606 case DW_AT_default_value:
4607 return "DW_AT_default_value";
4608 case DW_AT_inline:
4609 return "DW_AT_inline";
4610 case DW_AT_is_optional:
4611 return "DW_AT_is_optional";
4612 case DW_AT_lower_bound:
4613 return "DW_AT_lower_bound";
4614 case DW_AT_producer:
4615 return "DW_AT_producer";
4616 case DW_AT_prototyped:
4617 return "DW_AT_prototyped";
4618 case DW_AT_return_addr:
4619 return "DW_AT_return_addr";
4620 case DW_AT_start_scope:
4621 return "DW_AT_start_scope";
4622 case DW_AT_stride_size:
4623 return "DW_AT_stride_size";
4624 case DW_AT_upper_bound:
4625 return "DW_AT_upper_bound";
4626 case DW_AT_abstract_origin:
4627 return "DW_AT_abstract_origin";
4628 case DW_AT_accessibility:
4629 return "DW_AT_accessibility";
4630 case DW_AT_address_class:
4631 return "DW_AT_address_class";
4632 case DW_AT_artificial:
4633 return "DW_AT_artificial";
4634 case DW_AT_base_types:
4635 return "DW_AT_base_types";
4636 case DW_AT_calling_convention:
4637 return "DW_AT_calling_convention";
4638 case DW_AT_count:
4639 return "DW_AT_count";
4640 case DW_AT_data_member_location:
4641 return "DW_AT_data_member_location";
4642 case DW_AT_decl_column:
4643 return "DW_AT_decl_column";
4644 case DW_AT_decl_file:
4645 return "DW_AT_decl_file";
4646 case DW_AT_decl_line:
4647 return "DW_AT_decl_line";
4648 case DW_AT_declaration:
4649 return "DW_AT_declaration";
4650 case DW_AT_discr_list:
4651 return "DW_AT_discr_list";
4652 case DW_AT_encoding:
4653 return "DW_AT_encoding";
4654 case DW_AT_external:
4655 return "DW_AT_external";
4656 case DW_AT_frame_base:
4657 return "DW_AT_frame_base";
4658 case DW_AT_friend:
4659 return "DW_AT_friend";
4660 case DW_AT_identifier_case:
4661 return "DW_AT_identifier_case";
4662 case DW_AT_macro_info:
4663 return "DW_AT_macro_info";
4664 case DW_AT_namelist_items:
4665 return "DW_AT_namelist_items";
4666 case DW_AT_priority:
4667 return "DW_AT_priority";
4668 case DW_AT_segment:
4669 return "DW_AT_segment";
4670 case DW_AT_specification:
4671 return "DW_AT_specification";
4672 case DW_AT_static_link:
4673 return "DW_AT_static_link";
4674 case DW_AT_type:
4675 return "DW_AT_type";
4676 case DW_AT_use_location:
4677 return "DW_AT_use_location";
4678 case DW_AT_variable_parameter:
4679 return "DW_AT_variable_parameter";
4680 case DW_AT_virtuality:
4681 return "DW_AT_virtuality";
4682 case DW_AT_vtable_elem_location:
4683 return "DW_AT_vtable_elem_location";
4685 case DW_AT_allocated:
4686 return "DW_AT_allocated";
4687 case DW_AT_associated:
4688 return "DW_AT_associated";
4689 case DW_AT_data_location:
4690 return "DW_AT_data_location";
4691 case DW_AT_stride:
4692 return "DW_AT_stride";
4693 case DW_AT_entry_pc:
4694 return "DW_AT_entry_pc";
4695 case DW_AT_use_UTF8:
4696 return "DW_AT_use_UTF8";
4697 case DW_AT_extension:
4698 return "DW_AT_extension";
4699 case DW_AT_ranges:
4700 return "DW_AT_ranges";
4701 case DW_AT_trampoline:
4702 return "DW_AT_trampoline";
4703 case DW_AT_call_column:
4704 return "DW_AT_call_column";
4705 case DW_AT_call_file:
4706 return "DW_AT_call_file";
4707 case DW_AT_call_line:
4708 return "DW_AT_call_line";
4710 case DW_AT_MIPS_fde:
4711 return "DW_AT_MIPS_fde";
4712 case DW_AT_MIPS_loop_begin:
4713 return "DW_AT_MIPS_loop_begin";
4714 case DW_AT_MIPS_tail_loop_begin:
4715 return "DW_AT_MIPS_tail_loop_begin";
4716 case DW_AT_MIPS_epilog_begin:
4717 return "DW_AT_MIPS_epilog_begin";
4718 case DW_AT_MIPS_loop_unroll_factor:
4719 return "DW_AT_MIPS_loop_unroll_factor";
4720 case DW_AT_MIPS_software_pipeline_depth:
4721 return "DW_AT_MIPS_software_pipeline_depth";
4722 case DW_AT_MIPS_linkage_name:
4723 return "DW_AT_MIPS_linkage_name";
4724 case DW_AT_MIPS_stride:
4725 return "DW_AT_MIPS_stride";
4726 case DW_AT_MIPS_abstract_name:
4727 return "DW_AT_MIPS_abstract_name";
4728 case DW_AT_MIPS_clone_origin:
4729 return "DW_AT_MIPS_clone_origin";
4730 case DW_AT_MIPS_has_inlines:
4731 return "DW_AT_MIPS_has_inlines";
4733 case DW_AT_sf_names:
4734 return "DW_AT_sf_names";
4735 case DW_AT_src_info:
4736 return "DW_AT_src_info";
4737 case DW_AT_mac_info:
4738 return "DW_AT_mac_info";
4739 case DW_AT_src_coords:
4740 return "DW_AT_src_coords";
4741 case DW_AT_body_begin:
4742 return "DW_AT_body_begin";
4743 case DW_AT_body_end:
4744 return "DW_AT_body_end";
4745 case DW_AT_GNU_vector:
4746 return "DW_AT_GNU_vector";
4748 case DW_AT_VMS_rtnbeg_pd_address:
4749 return "DW_AT_VMS_rtnbeg_pd_address";
4751 default:
4752 return "DW_AT_<unknown>";
4756 /* Convert a DWARF value form code into its string name. */
4758 static const char *
4759 dwarf_form_name (unsigned int form)
4761 switch (form)
4763 case DW_FORM_addr:
4764 return "DW_FORM_addr";
4765 case DW_FORM_block2:
4766 return "DW_FORM_block2";
4767 case DW_FORM_block4:
4768 return "DW_FORM_block4";
4769 case DW_FORM_data2:
4770 return "DW_FORM_data2";
4771 case DW_FORM_data4:
4772 return "DW_FORM_data4";
4773 case DW_FORM_data8:
4774 return "DW_FORM_data8";
4775 case DW_FORM_string:
4776 return "DW_FORM_string";
4777 case DW_FORM_block:
4778 return "DW_FORM_block";
4779 case DW_FORM_block1:
4780 return "DW_FORM_block1";
4781 case DW_FORM_data1:
4782 return "DW_FORM_data1";
4783 case DW_FORM_flag:
4784 return "DW_FORM_flag";
4785 case DW_FORM_sdata:
4786 return "DW_FORM_sdata";
4787 case DW_FORM_strp:
4788 return "DW_FORM_strp";
4789 case DW_FORM_udata:
4790 return "DW_FORM_udata";
4791 case DW_FORM_ref_addr:
4792 return "DW_FORM_ref_addr";
4793 case DW_FORM_ref1:
4794 return "DW_FORM_ref1";
4795 case DW_FORM_ref2:
4796 return "DW_FORM_ref2";
4797 case DW_FORM_ref4:
4798 return "DW_FORM_ref4";
4799 case DW_FORM_ref8:
4800 return "DW_FORM_ref8";
4801 case DW_FORM_ref_udata:
4802 return "DW_FORM_ref_udata";
4803 case DW_FORM_indirect:
4804 return "DW_FORM_indirect";
4805 default:
4806 return "DW_FORM_<unknown>";
4810 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4811 instance of an inlined instance of a decl which is local to an inline
4812 function, so we have to trace all of the way back through the origin chain
4813 to find out what sort of node actually served as the original seed for the
4814 given block. */
4816 static tree
4817 decl_ultimate_origin (tree decl)
4819 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4820 return NULL_TREE;
4822 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4823 nodes in the function to point to themselves; ignore that if
4824 we're trying to output the abstract instance of this function. */
4825 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4826 return NULL_TREE;
4828 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4829 most distant ancestor, this should never happen. */
4830 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4832 return DECL_ABSTRACT_ORIGIN (decl);
4835 /* Determine the "ultimate origin" of a block. The block may be an inlined
4836 instance of an inlined instance of a block which is local to an inline
4837 function, so we have to trace all of the way back through the origin chain
4838 to find out what sort of node actually served as the original seed for the
4839 given block. */
4841 static tree
4842 block_ultimate_origin (tree block)
4844 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
4846 /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
4847 nodes in the function to point to themselves; ignore that if
4848 we're trying to output the abstract instance of this function. */
4849 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
4850 return NULL_TREE;
4852 if (immediate_origin == NULL_TREE)
4853 return NULL_TREE;
4854 else
4856 tree ret_val;
4857 tree lookahead = immediate_origin;
4861 ret_val = lookahead;
4862 lookahead = (TREE_CODE (ret_val) == BLOCK
4863 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
4865 while (lookahead != NULL && lookahead != ret_val);
4867 /* The block's abstract origin chain may not be the *ultimate* origin of
4868 the block. It could lead to a DECL that has an abstract origin set.
4869 If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
4870 will give us if it has one). Note that DECL's abstract origins are
4871 supposed to be the most distant ancestor (or so decl_ultimate_origin
4872 claims), so we don't need to loop following the DECL origins. */
4873 if (DECL_P (ret_val))
4874 return DECL_ORIGIN (ret_val);
4876 return ret_val;
4880 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4881 of a virtual function may refer to a base class, so we check the 'this'
4882 parameter. */
4884 static tree
4885 decl_class_context (tree decl)
4887 tree context = NULL_TREE;
4889 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4890 context = DECL_CONTEXT (decl);
4891 else
4892 context = TYPE_MAIN_VARIANT
4893 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4895 if (context && !TYPE_P (context))
4896 context = NULL_TREE;
4898 return context;
4901 /* Add an attribute/value pair to a DIE. */
4903 static inline void
4904 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
4906 /* Maybe this should be an assert? */
4907 if (die == NULL)
4908 return;
4910 if (die->die_attr == NULL)
4911 die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
4912 VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
4915 static inline enum dw_val_class
4916 AT_class (dw_attr_ref a)
4918 return a->dw_attr_val.val_class;
4921 /* Add a flag value attribute to a DIE. */
4923 static inline void
4924 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4926 dw_attr_node attr;
4928 attr.dw_attr = attr_kind;
4929 attr.dw_attr_val.val_class = dw_val_class_flag;
4930 attr.dw_attr_val.v.val_flag = flag;
4931 add_dwarf_attr (die, &attr);
4934 static inline unsigned
4935 AT_flag (dw_attr_ref a)
4937 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4938 return a->dw_attr_val.v.val_flag;
4941 /* Add a signed integer attribute value to a DIE. */
4943 static inline void
4944 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4946 dw_attr_node attr;
4948 attr.dw_attr = attr_kind;
4949 attr.dw_attr_val.val_class = dw_val_class_const;
4950 attr.dw_attr_val.v.val_int = int_val;
4951 add_dwarf_attr (die, &attr);
4954 static inline HOST_WIDE_INT
4955 AT_int (dw_attr_ref a)
4957 gcc_assert (a && AT_class (a) == dw_val_class_const);
4958 return a->dw_attr_val.v.val_int;
4961 /* Add an unsigned integer attribute value to a DIE. */
4963 static inline void
4964 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4965 unsigned HOST_WIDE_INT unsigned_val)
4967 dw_attr_node attr;
4969 attr.dw_attr = attr_kind;
4970 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4971 attr.dw_attr_val.v.val_unsigned = unsigned_val;
4972 add_dwarf_attr (die, &attr);
4975 static inline unsigned HOST_WIDE_INT
4976 AT_unsigned (dw_attr_ref a)
4978 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
4979 return a->dw_attr_val.v.val_unsigned;
4982 /* Add an unsigned double integer attribute value to a DIE. */
4984 static inline void
4985 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
4986 long unsigned int val_hi, long unsigned int val_low)
4988 dw_attr_node attr;
4990 attr.dw_attr = attr_kind;
4991 attr.dw_attr_val.val_class = dw_val_class_long_long;
4992 attr.dw_attr_val.v.val_long_long.hi = val_hi;
4993 attr.dw_attr_val.v.val_long_long.low = val_low;
4994 add_dwarf_attr (die, &attr);
4997 /* Add a floating point attribute value to a DIE and return it. */
4999 static inline void
5000 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
5001 unsigned int length, unsigned int elt_size, unsigned char *array)
5003 dw_attr_node attr;
5005 attr.dw_attr = attr_kind;
5006 attr.dw_attr_val.val_class = dw_val_class_vec;
5007 attr.dw_attr_val.v.val_vec.length = length;
5008 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
5009 attr.dw_attr_val.v.val_vec.array = array;
5010 add_dwarf_attr (die, &attr);
5013 /* Hash and equality functions for debug_str_hash. */
5015 static hashval_t
5016 debug_str_do_hash (const void *x)
5018 return htab_hash_string (((const struct indirect_string_node *)x)->str);
5021 static int
5022 debug_str_eq (const void *x1, const void *x2)
5024 return strcmp ((((const struct indirect_string_node *)x1)->str),
5025 (const char *)x2) == 0;
5028 /* Add a string attribute value to a DIE. */
5030 static inline void
5031 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
5033 dw_attr_node attr;
5034 struct indirect_string_node *node;
5035 void **slot;
5037 if (! debug_str_hash)
5038 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
5039 debug_str_eq, NULL);
5041 slot = htab_find_slot_with_hash (debug_str_hash, str,
5042 htab_hash_string (str), INSERT);
5043 if (*slot == NULL)
5045 node = (struct indirect_string_node *)
5046 ggc_alloc_cleared (sizeof (struct indirect_string_node));
5047 node->str = ggc_strdup (str);
5048 *slot = node;
5050 else
5051 node = (struct indirect_string_node *) *slot;
5053 node->refcount++;
5055 attr.dw_attr = attr_kind;
5056 attr.dw_attr_val.val_class = dw_val_class_str;
5057 attr.dw_attr_val.v.val_str = node;
5058 add_dwarf_attr (die, &attr);
5061 static inline const char *
5062 AT_string (dw_attr_ref a)
5064 gcc_assert (a && AT_class (a) == dw_val_class_str);
5065 return a->dw_attr_val.v.val_str->str;
5068 /* Find out whether a string should be output inline in DIE
5069 or out-of-line in .debug_str section. */
5071 static int
5072 AT_string_form (dw_attr_ref a)
5074 struct indirect_string_node *node;
5075 unsigned int len;
5076 char label[32];
5078 gcc_assert (a && AT_class (a) == dw_val_class_str);
5080 node = a->dw_attr_val.v.val_str;
5081 if (node->form)
5082 return node->form;
5084 len = strlen (node->str) + 1;
5086 /* If the string is shorter or equal to the size of the reference, it is
5087 always better to put it inline. */
5088 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
5089 return node->form = DW_FORM_string;
5091 /* If we cannot expect the linker to merge strings in .debug_str
5092 section, only put it into .debug_str if it is worth even in this
5093 single module. */
5094 if ((debug_str_section->common.flags & SECTION_MERGE) == 0
5095 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
5096 return node->form = DW_FORM_string;
5098 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
5099 ++dw2_string_counter;
5100 node->label = xstrdup (label);
5102 return node->form = DW_FORM_strp;
5105 /* Add a DIE reference attribute value to a DIE. */
5107 static inline void
5108 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
5110 dw_attr_node attr;
5112 attr.dw_attr = attr_kind;
5113 attr.dw_attr_val.val_class = dw_val_class_die_ref;
5114 attr.dw_attr_val.v.val_die_ref.die = targ_die;
5115 attr.dw_attr_val.v.val_die_ref.external = 0;
5116 add_dwarf_attr (die, &attr);
5119 /* Add an AT_specification attribute to a DIE, and also make the back
5120 pointer from the specification to the definition. */
5122 static inline void
5123 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
5125 add_AT_die_ref (die, DW_AT_specification, targ_die);
5126 gcc_assert (!targ_die->die_definition);
5127 targ_die->die_definition = die;
5130 static inline dw_die_ref
5131 AT_ref (dw_attr_ref a)
5133 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5134 return a->dw_attr_val.v.val_die_ref.die;
5137 static inline int
5138 AT_ref_external (dw_attr_ref a)
5140 if (a && AT_class (a) == dw_val_class_die_ref)
5141 return a->dw_attr_val.v.val_die_ref.external;
5143 return 0;
5146 static inline void
5147 set_AT_ref_external (dw_attr_ref a, int i)
5149 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5150 a->dw_attr_val.v.val_die_ref.external = i;
5153 /* Add an FDE reference attribute value to a DIE. */
5155 static inline void
5156 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
5158 dw_attr_node attr;
5160 attr.dw_attr = attr_kind;
5161 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
5162 attr.dw_attr_val.v.val_fde_index = targ_fde;
5163 add_dwarf_attr (die, &attr);
5166 /* Add a location description attribute value to a DIE. */
5168 static inline void
5169 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
5171 dw_attr_node attr;
5173 attr.dw_attr = attr_kind;
5174 attr.dw_attr_val.val_class = dw_val_class_loc;
5175 attr.dw_attr_val.v.val_loc = loc;
5176 add_dwarf_attr (die, &attr);
5179 static inline dw_loc_descr_ref
5180 AT_loc (dw_attr_ref a)
5182 gcc_assert (a && AT_class (a) == dw_val_class_loc);
5183 return a->dw_attr_val.v.val_loc;
5186 static inline void
5187 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
5189 dw_attr_node attr;
5191 attr.dw_attr = attr_kind;
5192 attr.dw_attr_val.val_class = dw_val_class_loc_list;
5193 attr.dw_attr_val.v.val_loc_list = loc_list;
5194 add_dwarf_attr (die, &attr);
5195 have_location_lists = true;
5198 static inline dw_loc_list_ref
5199 AT_loc_list (dw_attr_ref a)
5201 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
5202 return a->dw_attr_val.v.val_loc_list;
5205 /* Add an address constant attribute value to a DIE. */
5207 static inline void
5208 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
5210 dw_attr_node attr;
5212 attr.dw_attr = attr_kind;
5213 attr.dw_attr_val.val_class = dw_val_class_addr;
5214 attr.dw_attr_val.v.val_addr = addr;
5215 add_dwarf_attr (die, &attr);
5218 /* Get the RTX from to an address DIE attribute. */
5220 static inline rtx
5221 AT_addr (dw_attr_ref a)
5223 gcc_assert (a && AT_class (a) == dw_val_class_addr);
5224 return a->dw_attr_val.v.val_addr;
5227 /* Add a file attribute value to a DIE. */
5229 static inline void
5230 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
5231 struct dwarf_file_data *fd)
5233 dw_attr_node attr;
5235 attr.dw_attr = attr_kind;
5236 attr.dw_attr_val.val_class = dw_val_class_file;
5237 attr.dw_attr_val.v.val_file = fd;
5238 add_dwarf_attr (die, &attr);
5241 /* Get the dwarf_file_data from a file DIE attribute. */
5243 static inline struct dwarf_file_data *
5244 AT_file (dw_attr_ref a)
5246 gcc_assert (a && AT_class (a) == dw_val_class_file);
5247 return a->dw_attr_val.v.val_file;
5250 /* Add a label identifier attribute value to a DIE. */
5252 static inline void
5253 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
5255 dw_attr_node attr;
5257 attr.dw_attr = attr_kind;
5258 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
5259 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5260 add_dwarf_attr (die, &attr);
5263 /* Add a section offset attribute value to a DIE, an offset into the
5264 debug_line section. */
5266 static inline void
5267 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5268 const char *label)
5270 dw_attr_node attr;
5272 attr.dw_attr = attr_kind;
5273 attr.dw_attr_val.val_class = dw_val_class_lineptr;
5274 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5275 add_dwarf_attr (die, &attr);
5278 /* Add a section offset attribute value to a DIE, an offset into the
5279 debug_macinfo section. */
5281 static inline void
5282 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5283 const char *label)
5285 dw_attr_node attr;
5287 attr.dw_attr = attr_kind;
5288 attr.dw_attr_val.val_class = dw_val_class_macptr;
5289 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5290 add_dwarf_attr (die, &attr);
5293 /* Add an offset attribute value to a DIE. */
5295 static inline void
5296 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
5297 unsigned HOST_WIDE_INT offset)
5299 dw_attr_node attr;
5301 attr.dw_attr = attr_kind;
5302 attr.dw_attr_val.val_class = dw_val_class_offset;
5303 attr.dw_attr_val.v.val_offset = offset;
5304 add_dwarf_attr (die, &attr);
5307 /* Add an range_list attribute value to a DIE. */
5309 static void
5310 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5311 long unsigned int offset)
5313 dw_attr_node attr;
5315 attr.dw_attr = attr_kind;
5316 attr.dw_attr_val.val_class = dw_val_class_range_list;
5317 attr.dw_attr_val.v.val_offset = offset;
5318 add_dwarf_attr (die, &attr);
5321 static inline const char *
5322 AT_lbl (dw_attr_ref a)
5324 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5325 || AT_class (a) == dw_val_class_lineptr
5326 || AT_class (a) == dw_val_class_macptr));
5327 return a->dw_attr_val.v.val_lbl_id;
5330 /* Get the attribute of type attr_kind. */
5332 static dw_attr_ref
5333 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5335 dw_attr_ref a;
5336 unsigned ix;
5337 dw_die_ref spec = NULL;
5339 if (! die)
5340 return NULL;
5342 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5343 if (a->dw_attr == attr_kind)
5344 return a;
5345 else if (a->dw_attr == DW_AT_specification
5346 || a->dw_attr == DW_AT_abstract_origin)
5347 spec = AT_ref (a);
5349 if (spec)
5350 return get_AT (spec, attr_kind);
5352 return NULL;
5355 /* Return the "low pc" attribute value, typically associated with a subprogram
5356 DIE. Return null if the "low pc" attribute is either not present, or if it
5357 cannot be represented as an assembler label identifier. */
5359 static inline const char *
5360 get_AT_low_pc (dw_die_ref die)
5362 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
5364 return a ? AT_lbl (a) : NULL;
5367 /* Return the "high pc" attribute value, typically associated with a subprogram
5368 DIE. Return null if the "high pc" attribute is either not present, or if it
5369 cannot be represented as an assembler label identifier. */
5371 static inline const char *
5372 get_AT_hi_pc (dw_die_ref die)
5374 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
5376 return a ? AT_lbl (a) : NULL;
5379 /* Return the value of the string attribute designated by ATTR_KIND, or
5380 NULL if it is not present. */
5382 static inline const char *
5383 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5385 dw_attr_ref a = get_AT (die, attr_kind);
5387 return a ? AT_string (a) : NULL;
5390 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5391 if it is not present. */
5393 static inline int
5394 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5396 dw_attr_ref a = get_AT (die, attr_kind);
5398 return a ? AT_flag (a) : 0;
5401 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5402 if it is not present. */
5404 static inline unsigned
5405 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5407 dw_attr_ref a = get_AT (die, attr_kind);
5409 return a ? AT_unsigned (a) : 0;
5412 static inline dw_die_ref
5413 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5415 dw_attr_ref a = get_AT (die, attr_kind);
5417 return a ? AT_ref (a) : NULL;
5420 static inline struct dwarf_file_data *
5421 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5423 dw_attr_ref a = get_AT (die, attr_kind);
5425 return a ? AT_file (a) : NULL;
5428 /* Return TRUE if the language is C or C++. */
5430 static inline bool
5431 is_c_family (void)
5433 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5435 return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
5436 || lang == DW_LANG_C99
5437 || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
5440 /* Return TRUE if the language is C++. */
5442 static inline bool
5443 is_cxx (void)
5445 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5447 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
5450 /* Return TRUE if the language is Fortran. */
5452 static inline bool
5453 is_fortran (void)
5455 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5457 return (lang == DW_LANG_Fortran77
5458 || lang == DW_LANG_Fortran90
5459 || lang == DW_LANG_Fortran95);
5462 /* Return TRUE if the language is Java. */
5464 static inline bool
5465 is_java (void)
5467 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5469 return lang == DW_LANG_Java;
5472 /* Return TRUE if the language is Ada. */
5474 static inline bool
5475 is_ada (void)
5477 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5479 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5482 /* Remove the specified attribute if present. */
5484 static void
5485 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5487 dw_attr_ref a;
5488 unsigned ix;
5490 if (! die)
5491 return;
5493 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5494 if (a->dw_attr == attr_kind)
5496 if (AT_class (a) == dw_val_class_str)
5497 if (a->dw_attr_val.v.val_str->refcount)
5498 a->dw_attr_val.v.val_str->refcount--;
5500 /* VEC_ordered_remove should help reduce the number of abbrevs
5501 that are needed. */
5502 VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
5503 return;
5507 /* Remove CHILD from its parent. PREV must have the property that
5508 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5510 static void
5511 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5513 gcc_assert (child->die_parent == prev->die_parent);
5514 gcc_assert (prev->die_sib == child);
5515 if (prev == child)
5517 gcc_assert (child->die_parent->die_child == child);
5518 prev = NULL;
5520 else
5521 prev->die_sib = child->die_sib;
5522 if (child->die_parent->die_child == child)
5523 child->die_parent->die_child = prev;
5526 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
5527 matches TAG. */
5529 static void
5530 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5532 dw_die_ref c;
5534 c = die->die_child;
5535 if (c) do {
5536 dw_die_ref prev = c;
5537 c = c->die_sib;
5538 while (c->die_tag == tag)
5540 remove_child_with_prev (c, prev);
5541 /* Might have removed every child. */
5542 if (c == c->die_sib)
5543 return;
5544 c = c->die_sib;
5546 } while (c != die->die_child);
5549 /* Add a CHILD_DIE as the last child of DIE. */
5551 static void
5552 add_child_die (dw_die_ref die, dw_die_ref child_die)
5554 /* FIXME this should probably be an assert. */
5555 if (! die || ! child_die)
5556 return;
5557 gcc_assert (die != child_die);
5559 child_die->die_parent = die;
5560 if (die->die_child)
5562 child_die->die_sib = die->die_child->die_sib;
5563 die->die_child->die_sib = child_die;
5565 else
5566 child_die->die_sib = child_die;
5567 die->die_child = child_die;
5570 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5571 is the specification, to the end of PARENT's list of children.
5572 This is done by removing and re-adding it. */
5574 static void
5575 splice_child_die (dw_die_ref parent, dw_die_ref child)
5577 dw_die_ref p;
5579 /* We want the declaration DIE from inside the class, not the
5580 specification DIE at toplevel. */
5581 if (child->die_parent != parent)
5583 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5585 if (tmp)
5586 child = tmp;
5589 gcc_assert (child->die_parent == parent
5590 || (child->die_parent
5591 == get_AT_ref (parent, DW_AT_specification)));
5593 for (p = child->die_parent->die_child; ; p = p->die_sib)
5594 if (p->die_sib == child)
5596 remove_child_with_prev (child, p);
5597 break;
5600 add_child_die (parent, child);
5603 /* Return a pointer to a newly created DIE node. */
5605 static inline dw_die_ref
5606 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5608 dw_die_ref die = ggc_alloc_cleared (sizeof (die_node));
5610 die->die_tag = tag_value;
5612 if (parent_die != NULL)
5613 add_child_die (parent_die, die);
5614 else
5616 limbo_die_node *limbo_node;
5618 limbo_node = ggc_alloc_cleared (sizeof (limbo_die_node));
5619 limbo_node->die = die;
5620 limbo_node->created_for = t;
5621 limbo_node->next = limbo_die_list;
5622 limbo_die_list = limbo_node;
5625 return die;
5628 /* Return the DIE associated with the given type specifier. */
5630 static inline dw_die_ref
5631 lookup_type_die (tree type)
5633 return TYPE_SYMTAB_DIE (type);
5636 /* Equate a DIE to a given type specifier. */
5638 static inline void
5639 equate_type_number_to_die (tree type, dw_die_ref type_die)
5641 TYPE_SYMTAB_DIE (type) = type_die;
5644 /* Returns a hash value for X (which really is a die_struct). */
5646 static hashval_t
5647 decl_die_table_hash (const void *x)
5649 return (hashval_t) ((const dw_die_ref) x)->decl_id;
5652 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5654 static int
5655 decl_die_table_eq (const void *x, const void *y)
5657 return (((const dw_die_ref) x)->decl_id == DECL_UID ((const tree) y));
5660 /* Return the DIE associated with a given declaration. */
5662 static inline dw_die_ref
5663 lookup_decl_die (tree decl)
5665 return htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
5668 /* Returns a hash value for X (which really is a var_loc_list). */
5670 static hashval_t
5671 decl_loc_table_hash (const void *x)
5673 return (hashval_t) ((const var_loc_list *) x)->decl_id;
5676 /* Return nonzero if decl_id of var_loc_list X is the same as
5677 UID of decl *Y. */
5679 static int
5680 decl_loc_table_eq (const void *x, const void *y)
5682 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const tree) y));
5685 /* Return the var_loc list associated with a given declaration. */
5687 static inline var_loc_list *
5688 lookup_decl_loc (tree decl)
5690 return htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5693 /* Equate a DIE to a particular declaration. */
5695 static void
5696 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5698 unsigned int decl_id = DECL_UID (decl);
5699 void **slot;
5701 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5702 *slot = decl_die;
5703 decl_die->decl_id = decl_id;
5706 /* Add a variable location node to the linked list for DECL. */
5708 static void
5709 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
5711 unsigned int decl_id = DECL_UID (decl);
5712 var_loc_list *temp;
5713 void **slot;
5715 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5716 if (*slot == NULL)
5718 temp = ggc_alloc_cleared (sizeof (var_loc_list));
5719 temp->decl_id = decl_id;
5720 *slot = temp;
5722 else
5723 temp = *slot;
5725 if (temp->last)
5727 /* If the current location is the same as the end of the list,
5728 we have nothing to do. */
5729 if (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
5730 NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
5732 /* Add LOC to the end of list and update LAST. */
5733 temp->last->next = loc;
5734 temp->last = loc;
5737 /* Do not add empty location to the beginning of the list. */
5738 else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
5740 temp->first = loc;
5741 temp->last = loc;
5745 /* Keep track of the number of spaces used to indent the
5746 output of the debugging routines that print the structure of
5747 the DIE internal representation. */
5748 static int print_indent;
5750 /* Indent the line the number of spaces given by print_indent. */
5752 static inline void
5753 print_spaces (FILE *outfile)
5755 fprintf (outfile, "%*s", print_indent, "");
5758 /* Print the information associated with a given DIE, and its children.
5759 This routine is a debugging aid only. */
5761 static void
5762 print_die (dw_die_ref die, FILE *outfile)
5764 dw_attr_ref a;
5765 dw_die_ref c;
5766 unsigned ix;
5768 print_spaces (outfile);
5769 fprintf (outfile, "DIE %4lu: %s\n",
5770 die->die_offset, dwarf_tag_name (die->die_tag));
5771 print_spaces (outfile);
5772 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5773 fprintf (outfile, " offset: %lu\n", die->die_offset);
5775 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5777 print_spaces (outfile);
5778 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5780 switch (AT_class (a))
5782 case dw_val_class_addr:
5783 fprintf (outfile, "address");
5784 break;
5785 case dw_val_class_offset:
5786 fprintf (outfile, "offset");
5787 break;
5788 case dw_val_class_loc:
5789 fprintf (outfile, "location descriptor");
5790 break;
5791 case dw_val_class_loc_list:
5792 fprintf (outfile, "location list -> label:%s",
5793 AT_loc_list (a)->ll_symbol);
5794 break;
5795 case dw_val_class_range_list:
5796 fprintf (outfile, "range list");
5797 break;
5798 case dw_val_class_const:
5799 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5800 break;
5801 case dw_val_class_unsigned_const:
5802 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5803 break;
5804 case dw_val_class_long_long:
5805 fprintf (outfile, "constant (%lu,%lu)",
5806 a->dw_attr_val.v.val_long_long.hi,
5807 a->dw_attr_val.v.val_long_long.low);
5808 break;
5809 case dw_val_class_vec:
5810 fprintf (outfile, "floating-point or vector constant");
5811 break;
5812 case dw_val_class_flag:
5813 fprintf (outfile, "%u", AT_flag (a));
5814 break;
5815 case dw_val_class_die_ref:
5816 if (AT_ref (a) != NULL)
5818 if (AT_ref (a)->die_symbol)
5819 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
5820 else
5821 fprintf (outfile, "die -> %lu", AT_ref (a)->die_offset);
5823 else
5824 fprintf (outfile, "die -> <null>");
5825 break;
5826 case dw_val_class_lbl_id:
5827 case dw_val_class_lineptr:
5828 case dw_val_class_macptr:
5829 fprintf (outfile, "label: %s", AT_lbl (a));
5830 break;
5831 case dw_val_class_str:
5832 if (AT_string (a) != NULL)
5833 fprintf (outfile, "\"%s\"", AT_string (a));
5834 else
5835 fprintf (outfile, "<null>");
5836 break;
5837 case dw_val_class_file:
5838 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5839 AT_file (a)->emitted_number);
5840 break;
5841 default:
5842 break;
5845 fprintf (outfile, "\n");
5848 if (die->die_child != NULL)
5850 print_indent += 4;
5851 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5852 print_indent -= 4;
5854 if (print_indent == 0)
5855 fprintf (outfile, "\n");
5858 /* Print the contents of the source code line number correspondence table.
5859 This routine is a debugging aid only. */
5861 static void
5862 print_dwarf_line_table (FILE *outfile)
5864 unsigned i;
5865 dw_line_info_ref line_info;
5867 fprintf (outfile, "\n\nDWARF source line information\n");
5868 for (i = 1; i < line_info_table_in_use; i++)
5870 line_info = &line_info_table[i];
5871 fprintf (outfile, "%5d: %4ld %6ld\n", i,
5872 line_info->dw_file_num,
5873 line_info->dw_line_num);
5876 fprintf (outfile, "\n\n");
5879 /* Print the information collected for a given DIE. */
5881 void
5882 debug_dwarf_die (dw_die_ref die)
5884 print_die (die, stderr);
5887 /* Print all DWARF information collected for the compilation unit.
5888 This routine is a debugging aid only. */
5890 void
5891 debug_dwarf (void)
5893 print_indent = 0;
5894 print_die (comp_unit_die, stderr);
5895 if (! DWARF2_ASM_LINE_DEBUG_INFO)
5896 print_dwarf_line_table (stderr);
5899 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5900 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5901 DIE that marks the start of the DIEs for this include file. */
5903 static dw_die_ref
5904 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5906 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5907 dw_die_ref new_unit = gen_compile_unit_die (filename);
5909 new_unit->die_sib = old_unit;
5910 return new_unit;
5913 /* Close an include-file CU and reopen the enclosing one. */
5915 static dw_die_ref
5916 pop_compile_unit (dw_die_ref old_unit)
5918 dw_die_ref new_unit = old_unit->die_sib;
5920 old_unit->die_sib = NULL;
5921 return new_unit;
5924 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5925 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5927 /* Calculate the checksum of a location expression. */
5929 static inline void
5930 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5932 CHECKSUM (loc->dw_loc_opc);
5933 CHECKSUM (loc->dw_loc_oprnd1);
5934 CHECKSUM (loc->dw_loc_oprnd2);
5937 /* Calculate the checksum of an attribute. */
5939 static void
5940 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5942 dw_loc_descr_ref loc;
5943 rtx r;
5945 CHECKSUM (at->dw_attr);
5947 /* We don't care that this was compiled with a different compiler
5948 snapshot; if the output is the same, that's what matters. */
5949 if (at->dw_attr == DW_AT_producer)
5950 return;
5952 switch (AT_class (at))
5954 case dw_val_class_const:
5955 CHECKSUM (at->dw_attr_val.v.val_int);
5956 break;
5957 case dw_val_class_unsigned_const:
5958 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5959 break;
5960 case dw_val_class_long_long:
5961 CHECKSUM (at->dw_attr_val.v.val_long_long);
5962 break;
5963 case dw_val_class_vec:
5964 CHECKSUM (at->dw_attr_val.v.val_vec);
5965 break;
5966 case dw_val_class_flag:
5967 CHECKSUM (at->dw_attr_val.v.val_flag);
5968 break;
5969 case dw_val_class_str:
5970 CHECKSUM_STRING (AT_string (at));
5971 break;
5973 case dw_val_class_addr:
5974 r = AT_addr (at);
5975 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5976 CHECKSUM_STRING (XSTR (r, 0));
5977 break;
5979 case dw_val_class_offset:
5980 CHECKSUM (at->dw_attr_val.v.val_offset);
5981 break;
5983 case dw_val_class_loc:
5984 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5985 loc_checksum (loc, ctx);
5986 break;
5988 case dw_val_class_die_ref:
5989 die_checksum (AT_ref (at), ctx, mark);
5990 break;
5992 case dw_val_class_fde_ref:
5993 case dw_val_class_lbl_id:
5994 case dw_val_class_lineptr:
5995 case dw_val_class_macptr:
5996 break;
5998 case dw_val_class_file:
5999 CHECKSUM_STRING (AT_file (at)->filename);
6000 break;
6002 default:
6003 break;
6007 /* Calculate the checksum of a DIE. */
6009 static void
6010 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6012 dw_die_ref c;
6013 dw_attr_ref a;
6014 unsigned ix;
6016 /* To avoid infinite recursion. */
6017 if (die->die_mark)
6019 CHECKSUM (die->die_mark);
6020 return;
6022 die->die_mark = ++(*mark);
6024 CHECKSUM (die->die_tag);
6026 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6027 attr_checksum (a, ctx, mark);
6029 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6032 #undef CHECKSUM
6033 #undef CHECKSUM_STRING
6035 /* Do the location expressions look same? */
6036 static inline int
6037 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6039 return loc1->dw_loc_opc == loc2->dw_loc_opc
6040 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6041 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6044 /* Do the values look the same? */
6045 static int
6046 same_dw_val_p (dw_val_node *v1, dw_val_node *v2, int *mark)
6048 dw_loc_descr_ref loc1, loc2;
6049 rtx r1, r2;
6051 if (v1->val_class != v2->val_class)
6052 return 0;
6054 switch (v1->val_class)
6056 case dw_val_class_const:
6057 return v1->v.val_int == v2->v.val_int;
6058 case dw_val_class_unsigned_const:
6059 return v1->v.val_unsigned == v2->v.val_unsigned;
6060 case dw_val_class_long_long:
6061 return v1->v.val_long_long.hi == v2->v.val_long_long.hi
6062 && v1->v.val_long_long.low == v2->v.val_long_long.low;
6063 case dw_val_class_vec:
6064 if (v1->v.val_vec.length != v2->v.val_vec.length
6065 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6066 return 0;
6067 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6068 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6069 return 0;
6070 return 1;
6071 case dw_val_class_flag:
6072 return v1->v.val_flag == v2->v.val_flag;
6073 case dw_val_class_str:
6074 return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6076 case dw_val_class_addr:
6077 r1 = v1->v.val_addr;
6078 r2 = v2->v.val_addr;
6079 if (GET_CODE (r1) != GET_CODE (r2))
6080 return 0;
6081 gcc_assert (GET_CODE (r1) == SYMBOL_REF);
6082 return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
6084 case dw_val_class_offset:
6085 return v1->v.val_offset == v2->v.val_offset;
6087 case dw_val_class_loc:
6088 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6089 loc1 && loc2;
6090 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6091 if (!same_loc_p (loc1, loc2, mark))
6092 return 0;
6093 return !loc1 && !loc2;
6095 case dw_val_class_die_ref:
6096 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6098 case dw_val_class_fde_ref:
6099 case dw_val_class_lbl_id:
6100 case dw_val_class_lineptr:
6101 case dw_val_class_macptr:
6102 return 1;
6104 case dw_val_class_file:
6105 return v1->v.val_file == v2->v.val_file;
6107 default:
6108 return 1;
6112 /* Do the attributes look the same? */
6114 static int
6115 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6117 if (at1->dw_attr != at2->dw_attr)
6118 return 0;
6120 /* We don't care that this was compiled with a different compiler
6121 snapshot; if the output is the same, that's what matters. */
6122 if (at1->dw_attr == DW_AT_producer)
6123 return 1;
6125 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6128 /* Do the dies look the same? */
6130 static int
6131 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6133 dw_die_ref c1, c2;
6134 dw_attr_ref a1;
6135 unsigned ix;
6137 /* To avoid infinite recursion. */
6138 if (die1->die_mark)
6139 return die1->die_mark == die2->die_mark;
6140 die1->die_mark = die2->die_mark = ++(*mark);
6142 if (die1->die_tag != die2->die_tag)
6143 return 0;
6145 if (VEC_length (dw_attr_node, die1->die_attr)
6146 != VEC_length (dw_attr_node, die2->die_attr))
6147 return 0;
6149 for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
6150 if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
6151 return 0;
6153 c1 = die1->die_child;
6154 c2 = die2->die_child;
6155 if (! c1)
6157 if (c2)
6158 return 0;
6160 else
6161 for (;;)
6163 if (!same_die_p (c1, c2, mark))
6164 return 0;
6165 c1 = c1->die_sib;
6166 c2 = c2->die_sib;
6167 if (c1 == die1->die_child)
6169 if (c2 == die2->die_child)
6170 break;
6171 else
6172 return 0;
6176 return 1;
6179 /* Do the dies look the same? Wrapper around same_die_p. */
6181 static int
6182 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6184 int mark = 0;
6185 int ret = same_die_p (die1, die2, &mark);
6187 unmark_all_dies (die1);
6188 unmark_all_dies (die2);
6190 return ret;
6193 /* The prefix to attach to symbols on DIEs in the current comdat debug
6194 info section. */
6195 static char *comdat_symbol_id;
6197 /* The index of the current symbol within the current comdat CU. */
6198 static unsigned int comdat_symbol_number;
6200 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6201 children, and set comdat_symbol_id accordingly. */
6203 static void
6204 compute_section_prefix (dw_die_ref unit_die)
6206 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6207 const char *base = die_name ? lbasename (die_name) : "anonymous";
6208 char *name = alloca (strlen (base) + 64);
6209 char *p;
6210 int i, mark;
6211 unsigned char checksum[16];
6212 struct md5_ctx ctx;
6214 /* Compute the checksum of the DIE, then append part of it as hex digits to
6215 the name filename of the unit. */
6217 md5_init_ctx (&ctx);
6218 mark = 0;
6219 die_checksum (unit_die, &ctx, &mark);
6220 unmark_all_dies (unit_die);
6221 md5_finish_ctx (&ctx, checksum);
6223 sprintf (name, "%s.", base);
6224 clean_symbol_name (name);
6226 p = name + strlen (name);
6227 for (i = 0; i < 4; i++)
6229 sprintf (p, "%.2x", checksum[i]);
6230 p += 2;
6233 comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
6234 comdat_symbol_number = 0;
6237 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6239 static int
6240 is_type_die (dw_die_ref die)
6242 switch (die->die_tag)
6244 case DW_TAG_array_type:
6245 case DW_TAG_class_type:
6246 case DW_TAG_enumeration_type:
6247 case DW_TAG_pointer_type:
6248 case DW_TAG_reference_type:
6249 case DW_TAG_string_type:
6250 case DW_TAG_structure_type:
6251 case DW_TAG_subroutine_type:
6252 case DW_TAG_union_type:
6253 case DW_TAG_ptr_to_member_type:
6254 case DW_TAG_set_type:
6255 case DW_TAG_subrange_type:
6256 case DW_TAG_base_type:
6257 case DW_TAG_const_type:
6258 case DW_TAG_file_type:
6259 case DW_TAG_packed_type:
6260 case DW_TAG_volatile_type:
6261 case DW_TAG_typedef:
6262 return 1;
6263 default:
6264 return 0;
6268 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6269 Basically, we want to choose the bits that are likely to be shared between
6270 compilations (types) and leave out the bits that are specific to individual
6271 compilations (functions). */
6273 static int
6274 is_comdat_die (dw_die_ref c)
6276 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6277 we do for stabs. The advantage is a greater likelihood of sharing between
6278 objects that don't include headers in the same order (and therefore would
6279 put the base types in a different comdat). jason 8/28/00 */
6281 if (c->die_tag == DW_TAG_base_type)
6282 return 0;
6284 if (c->die_tag == DW_TAG_pointer_type
6285 || c->die_tag == DW_TAG_reference_type
6286 || c->die_tag == DW_TAG_const_type
6287 || c->die_tag == DW_TAG_volatile_type)
6289 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6291 return t ? is_comdat_die (t) : 0;
6294 return is_type_die (c);
6297 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6298 compilation unit. */
6300 static int
6301 is_symbol_die (dw_die_ref c)
6303 return (is_type_die (c)
6304 || (get_AT (c, DW_AT_declaration)
6305 && !get_AT (c, DW_AT_specification))
6306 || c->die_tag == DW_TAG_namespace);
6309 static char *
6310 gen_internal_sym (const char *prefix)
6312 char buf[256];
6314 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6315 return xstrdup (buf);
6318 /* Assign symbols to all worthy DIEs under DIE. */
6320 static void
6321 assign_symbol_names (dw_die_ref die)
6323 dw_die_ref c;
6325 if (is_symbol_die (die))
6327 if (comdat_symbol_id)
6329 char *p = alloca (strlen (comdat_symbol_id) + 64);
6331 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6332 comdat_symbol_id, comdat_symbol_number++);
6333 die->die_symbol = xstrdup (p);
6335 else
6336 die->die_symbol = gen_internal_sym ("LDIE");
6339 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6342 struct cu_hash_table_entry
6344 dw_die_ref cu;
6345 unsigned min_comdat_num, max_comdat_num;
6346 struct cu_hash_table_entry *next;
6349 /* Routines to manipulate hash table of CUs. */
6350 static hashval_t
6351 htab_cu_hash (const void *of)
6353 const struct cu_hash_table_entry *entry = of;
6355 return htab_hash_string (entry->cu->die_symbol);
6358 static int
6359 htab_cu_eq (const void *of1, const void *of2)
6361 const struct cu_hash_table_entry *entry1 = of1;
6362 const struct die_struct *entry2 = of2;
6364 return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
6367 static void
6368 htab_cu_del (void *what)
6370 struct cu_hash_table_entry *next, *entry = what;
6372 while (entry)
6374 next = entry->next;
6375 free (entry);
6376 entry = next;
6380 /* Check whether we have already seen this CU and set up SYM_NUM
6381 accordingly. */
6382 static int
6383 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6385 struct cu_hash_table_entry dummy;
6386 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6388 dummy.max_comdat_num = 0;
6390 slot = (struct cu_hash_table_entry **)
6391 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6392 INSERT);
6393 entry = *slot;
6395 for (; entry; last = entry, entry = entry->next)
6397 if (same_die_p_wrap (cu, entry->cu))
6398 break;
6401 if (entry)
6403 *sym_num = entry->min_comdat_num;
6404 return 1;
6407 entry = XCNEW (struct cu_hash_table_entry);
6408 entry->cu = cu;
6409 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6410 entry->next = *slot;
6411 *slot = entry;
6413 return 0;
6416 /* Record SYM_NUM to record of CU in HTABLE. */
6417 static void
6418 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6420 struct cu_hash_table_entry **slot, *entry;
6422 slot = (struct cu_hash_table_entry **)
6423 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6424 NO_INSERT);
6425 entry = *slot;
6427 entry->max_comdat_num = sym_num;
6430 /* Traverse the DIE (which is always comp_unit_die), and set up
6431 additional compilation units for each of the include files we see
6432 bracketed by BINCL/EINCL. */
6434 static void
6435 break_out_includes (dw_die_ref die)
6437 dw_die_ref c;
6438 dw_die_ref unit = NULL;
6439 limbo_die_node *node, **pnode;
6440 htab_t cu_hash_table;
6442 c = die->die_child;
6443 if (c) do {
6444 dw_die_ref prev = c;
6445 c = c->die_sib;
6446 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6447 || (unit && is_comdat_die (c)))
6449 dw_die_ref next = c->die_sib;
6451 /* This DIE is for a secondary CU; remove it from the main one. */
6452 remove_child_with_prev (c, prev);
6454 if (c->die_tag == DW_TAG_GNU_BINCL)
6455 unit = push_new_compile_unit (unit, c);
6456 else if (c->die_tag == DW_TAG_GNU_EINCL)
6457 unit = pop_compile_unit (unit);
6458 else
6459 add_child_die (unit, c);
6460 c = next;
6461 if (c == die->die_child)
6462 break;
6464 } while (c != die->die_child);
6466 #if 0
6467 /* We can only use this in debugging, since the frontend doesn't check
6468 to make sure that we leave every include file we enter. */
6469 gcc_assert (!unit);
6470 #endif
6472 assign_symbol_names (die);
6473 cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6474 for (node = limbo_die_list, pnode = &limbo_die_list;
6475 node;
6476 node = node->next)
6478 int is_dupl;
6480 compute_section_prefix (node->die);
6481 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6482 &comdat_symbol_number);
6483 assign_symbol_names (node->die);
6484 if (is_dupl)
6485 *pnode = node->next;
6486 else
6488 pnode = &node->next;
6489 record_comdat_symbol_number (node->die, cu_hash_table,
6490 comdat_symbol_number);
6493 htab_delete (cu_hash_table);
6496 /* Traverse the DIE and add a sibling attribute if it may have the
6497 effect of speeding up access to siblings. To save some space,
6498 avoid generating sibling attributes for DIE's without children. */
6500 static void
6501 add_sibling_attributes (dw_die_ref die)
6503 dw_die_ref c;
6505 if (! die->die_child)
6506 return;
6508 if (die->die_parent && die != die->die_parent->die_child)
6509 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
6511 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
6514 /* Output all location lists for the DIE and its children. */
6516 static void
6517 output_location_lists (dw_die_ref die)
6519 dw_die_ref c;
6520 dw_attr_ref a;
6521 unsigned ix;
6523 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6524 if (AT_class (a) == dw_val_class_loc_list)
6525 output_loc_list (AT_loc_list (a));
6527 FOR_EACH_CHILD (die, c, output_location_lists (c));
6530 /* The format of each DIE (and its attribute value pairs) is encoded in an
6531 abbreviation table. This routine builds the abbreviation table and assigns
6532 a unique abbreviation id for each abbreviation entry. The children of each
6533 die are visited recursively. */
6535 static void
6536 build_abbrev_table (dw_die_ref die)
6538 unsigned long abbrev_id;
6539 unsigned int n_alloc;
6540 dw_die_ref c;
6541 dw_attr_ref a;
6542 unsigned ix;
6544 /* Scan the DIE references, and mark as external any that refer to
6545 DIEs from other CUs (i.e. those which are not marked). */
6546 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6547 if (AT_class (a) == dw_val_class_die_ref
6548 && AT_ref (a)->die_mark == 0)
6550 gcc_assert (AT_ref (a)->die_symbol);
6552 set_AT_ref_external (a, 1);
6555 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6557 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6558 dw_attr_ref die_a, abbrev_a;
6559 unsigned ix;
6560 bool ok = true;
6562 if (abbrev->die_tag != die->die_tag)
6563 continue;
6564 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
6565 continue;
6567 if (VEC_length (dw_attr_node, abbrev->die_attr)
6568 != VEC_length (dw_attr_node, die->die_attr))
6569 continue;
6571 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
6573 abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
6574 if ((abbrev_a->dw_attr != die_a->dw_attr)
6575 || (value_format (abbrev_a) != value_format (die_a)))
6577 ok = false;
6578 break;
6581 if (ok)
6582 break;
6585 if (abbrev_id >= abbrev_die_table_in_use)
6587 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
6589 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
6590 abbrev_die_table = ggc_realloc (abbrev_die_table,
6591 sizeof (dw_die_ref) * n_alloc);
6593 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
6594 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
6595 abbrev_die_table_allocated = n_alloc;
6598 ++abbrev_die_table_in_use;
6599 abbrev_die_table[abbrev_id] = die;
6602 die->die_abbrev = abbrev_id;
6603 FOR_EACH_CHILD (die, c, build_abbrev_table (c));
6606 /* Return the power-of-two number of bytes necessary to represent VALUE. */
6608 static int
6609 constant_size (long unsigned int value)
6611 int log;
6613 if (value == 0)
6614 log = 0;
6615 else
6616 log = floor_log2 (value);
6618 log = log / 8;
6619 log = 1 << (floor_log2 (log) + 1);
6621 return log;
6624 /* Return the size of a DIE as it is represented in the
6625 .debug_info section. */
6627 static unsigned long
6628 size_of_die (dw_die_ref die)
6630 unsigned long size = 0;
6631 dw_attr_ref a;
6632 unsigned ix;
6634 size += size_of_uleb128 (die->die_abbrev);
6635 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6637 switch (AT_class (a))
6639 case dw_val_class_addr:
6640 size += DWARF2_ADDR_SIZE;
6641 break;
6642 case dw_val_class_offset:
6643 size += DWARF_OFFSET_SIZE;
6644 break;
6645 case dw_val_class_loc:
6647 unsigned long lsize = size_of_locs (AT_loc (a));
6649 /* Block length. */
6650 size += constant_size (lsize);
6651 size += lsize;
6653 break;
6654 case dw_val_class_loc_list:
6655 size += DWARF_OFFSET_SIZE;
6656 break;
6657 case dw_val_class_range_list:
6658 size += DWARF_OFFSET_SIZE;
6659 break;
6660 case dw_val_class_const:
6661 size += size_of_sleb128 (AT_int (a));
6662 break;
6663 case dw_val_class_unsigned_const:
6664 size += constant_size (AT_unsigned (a));
6665 break;
6666 case dw_val_class_long_long:
6667 size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
6668 break;
6669 case dw_val_class_vec:
6670 size += 1 + (a->dw_attr_val.v.val_vec.length
6671 * a->dw_attr_val.v.val_vec.elt_size); /* block */
6672 break;
6673 case dw_val_class_flag:
6674 size += 1;
6675 break;
6676 case dw_val_class_die_ref:
6677 if (AT_ref_external (a))
6678 size += DWARF2_ADDR_SIZE;
6679 else
6680 size += DWARF_OFFSET_SIZE;
6681 break;
6682 case dw_val_class_fde_ref:
6683 size += DWARF_OFFSET_SIZE;
6684 break;
6685 case dw_val_class_lbl_id:
6686 size += DWARF2_ADDR_SIZE;
6687 break;
6688 case dw_val_class_lineptr:
6689 case dw_val_class_macptr:
6690 size += DWARF_OFFSET_SIZE;
6691 break;
6692 case dw_val_class_str:
6693 if (AT_string_form (a) == DW_FORM_strp)
6694 size += DWARF_OFFSET_SIZE;
6695 else
6696 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
6697 break;
6698 case dw_val_class_file:
6699 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
6700 break;
6701 default:
6702 gcc_unreachable ();
6706 return size;
6709 /* Size the debugging information associated with a given DIE. Visits the
6710 DIE's children recursively. Updates the global variable next_die_offset, on
6711 each time through. Uses the current value of next_die_offset to update the
6712 die_offset field in each DIE. */
6714 static void
6715 calc_die_sizes (dw_die_ref die)
6717 dw_die_ref c;
6719 die->die_offset = next_die_offset;
6720 next_die_offset += size_of_die (die);
6722 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
6724 if (die->die_child != NULL)
6725 /* Count the null byte used to terminate sibling lists. */
6726 next_die_offset += 1;
6729 /* Set the marks for a die and its children. We do this so
6730 that we know whether or not a reference needs to use FORM_ref_addr; only
6731 DIEs in the same CU will be marked. We used to clear out the offset
6732 and use that as the flag, but ran into ordering problems. */
6734 static void
6735 mark_dies (dw_die_ref die)
6737 dw_die_ref c;
6739 gcc_assert (!die->die_mark);
6741 die->die_mark = 1;
6742 FOR_EACH_CHILD (die, c, mark_dies (c));
6745 /* Clear the marks for a die and its children. */
6747 static void
6748 unmark_dies (dw_die_ref die)
6750 dw_die_ref c;
6752 gcc_assert (die->die_mark);
6754 die->die_mark = 0;
6755 FOR_EACH_CHILD (die, c, unmark_dies (c));
6758 /* Clear the marks for a die, its children and referred dies. */
6760 static void
6761 unmark_all_dies (dw_die_ref die)
6763 dw_die_ref c;
6764 dw_attr_ref a;
6765 unsigned ix;
6767 if (!die->die_mark)
6768 return;
6769 die->die_mark = 0;
6771 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
6773 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6774 if (AT_class (a) == dw_val_class_die_ref)
6775 unmark_all_dies (AT_ref (a));
6778 /* Return the size of the .debug_pubnames or .debug_pubtypes table
6779 generated for the compilation unit. */
6781 static unsigned long
6782 size_of_pubnames (VEC (pubname_entry, gc) * names)
6784 unsigned long size;
6785 unsigned i;
6786 pubname_ref p;
6788 size = DWARF_PUBNAMES_HEADER_SIZE;
6789 for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
6790 if (names != pubtype_table
6791 || p->die->die_offset != 0
6792 || !flag_eliminate_unused_debug_types)
6793 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
6795 size += DWARF_OFFSET_SIZE;
6796 return size;
6799 /* Return the size of the information in the .debug_aranges section. */
6801 static unsigned long
6802 size_of_aranges (void)
6804 unsigned long size;
6806 size = DWARF_ARANGES_HEADER_SIZE;
6808 /* Count the address/length pair for this compilation unit. */
6809 size += 2 * DWARF2_ADDR_SIZE;
6810 size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
6812 /* Count the two zero words used to terminated the address range table. */
6813 size += 2 * DWARF2_ADDR_SIZE;
6814 return size;
6817 /* Select the encoding of an attribute value. */
6819 static enum dwarf_form
6820 value_format (dw_attr_ref a)
6822 switch (a->dw_attr_val.val_class)
6824 case dw_val_class_addr:
6825 return DW_FORM_addr;
6826 case dw_val_class_range_list:
6827 case dw_val_class_offset:
6828 case dw_val_class_loc_list:
6829 switch (DWARF_OFFSET_SIZE)
6831 case 4:
6832 return DW_FORM_data4;
6833 case 8:
6834 return DW_FORM_data8;
6835 default:
6836 gcc_unreachable ();
6838 case dw_val_class_loc:
6839 switch (constant_size (size_of_locs (AT_loc (a))))
6841 case 1:
6842 return DW_FORM_block1;
6843 case 2:
6844 return DW_FORM_block2;
6845 default:
6846 gcc_unreachable ();
6848 case dw_val_class_const:
6849 return DW_FORM_sdata;
6850 case dw_val_class_unsigned_const:
6851 switch (constant_size (AT_unsigned (a)))
6853 case 1:
6854 return DW_FORM_data1;
6855 case 2:
6856 return DW_FORM_data2;
6857 case 4:
6858 return DW_FORM_data4;
6859 case 8:
6860 return DW_FORM_data8;
6861 default:
6862 gcc_unreachable ();
6864 case dw_val_class_long_long:
6865 return DW_FORM_block1;
6866 case dw_val_class_vec:
6867 return DW_FORM_block1;
6868 case dw_val_class_flag:
6869 return DW_FORM_flag;
6870 case dw_val_class_die_ref:
6871 if (AT_ref_external (a))
6872 return DW_FORM_ref_addr;
6873 else
6874 return DW_FORM_ref;
6875 case dw_val_class_fde_ref:
6876 return DW_FORM_data;
6877 case dw_val_class_lbl_id:
6878 return DW_FORM_addr;
6879 case dw_val_class_lineptr:
6880 case dw_val_class_macptr:
6881 return DW_FORM_data;
6882 case dw_val_class_str:
6883 return AT_string_form (a);
6884 case dw_val_class_file:
6885 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
6887 case 1:
6888 return DW_FORM_data1;
6889 case 2:
6890 return DW_FORM_data2;
6891 case 4:
6892 return DW_FORM_data4;
6893 default:
6894 gcc_unreachable ();
6897 default:
6898 gcc_unreachable ();
6902 /* Output the encoding of an attribute value. */
6904 static void
6905 output_value_format (dw_attr_ref a)
6907 enum dwarf_form form = value_format (a);
6909 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
6912 /* Output the .debug_abbrev section which defines the DIE abbreviation
6913 table. */
6915 static void
6916 output_abbrev_section (void)
6918 unsigned long abbrev_id;
6920 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6922 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6923 unsigned ix;
6924 dw_attr_ref a_attr;
6926 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
6927 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
6928 dwarf_tag_name (abbrev->die_tag));
6930 if (abbrev->die_child != NULL)
6931 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
6932 else
6933 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
6935 for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
6936 ix++)
6938 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
6939 dwarf_attr_name (a_attr->dw_attr));
6940 output_value_format (a_attr);
6943 dw2_asm_output_data (1, 0, NULL);
6944 dw2_asm_output_data (1, 0, NULL);
6947 /* Terminate the table. */
6948 dw2_asm_output_data (1, 0, NULL);
6951 /* Output a symbol we can use to refer to this DIE from another CU. */
6953 static inline void
6954 output_die_symbol (dw_die_ref die)
6956 char *sym = die->die_symbol;
6958 if (sym == 0)
6959 return;
6961 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
6962 /* We make these global, not weak; if the target doesn't support
6963 .linkonce, it doesn't support combining the sections, so debugging
6964 will break. */
6965 targetm.asm_out.globalize_label (asm_out_file, sym);
6967 ASM_OUTPUT_LABEL (asm_out_file, sym);
6970 /* Return a new location list, given the begin and end range, and the
6971 expression. gensym tells us whether to generate a new internal symbol for
6972 this location list node, which is done for the head of the list only. */
6974 static inline dw_loc_list_ref
6975 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
6976 const char *section, unsigned int gensym)
6978 dw_loc_list_ref retlist = ggc_alloc_cleared (sizeof (dw_loc_list_node));
6980 retlist->begin = begin;
6981 retlist->end = end;
6982 retlist->expr = expr;
6983 retlist->section = section;
6984 if (gensym)
6985 retlist->ll_symbol = gen_internal_sym ("LLST");
6987 return retlist;
6990 /* Add a location description expression to a location list. */
6992 static inline void
6993 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
6994 const char *begin, const char *end,
6995 const char *section)
6997 dw_loc_list_ref *d;
6999 /* Find the end of the chain. */
7000 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
7003 /* Add a new location list node to the list. */
7004 *d = new_loc_list (descr, begin, end, section, 0);
7007 static void
7008 dwarf2out_switch_text_section (void)
7010 dw_fde_ref fde;
7012 gcc_assert (cfun);
7014 fde = &fde_table[fde_table_in_use - 1];
7015 fde->dw_fde_switched_sections = true;
7016 fde->dw_fde_hot_section_label = cfun->hot_section_label;
7017 fde->dw_fde_hot_section_end_label = cfun->hot_section_end_label;
7018 fde->dw_fde_unlikely_section_label = cfun->cold_section_label;
7019 fde->dw_fde_unlikely_section_end_label = cfun->cold_section_end_label;
7020 have_multiple_function_sections = true;
7022 /* Reset the current label on switching text sections, so that we
7023 don't attempt to advance_loc4 between labels in different sections. */
7024 fde->dw_fde_current_label = NULL;
7027 /* Output the location list given to us. */
7029 static void
7030 output_loc_list (dw_loc_list_ref list_head)
7032 dw_loc_list_ref curr = list_head;
7034 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
7036 /* Walk the location list, and output each range + expression. */
7037 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
7039 unsigned long size;
7040 if (!have_multiple_function_sections)
7042 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
7043 "Location list begin address (%s)",
7044 list_head->ll_symbol);
7045 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
7046 "Location list end address (%s)",
7047 list_head->ll_symbol);
7049 else
7051 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
7052 "Location list begin address (%s)",
7053 list_head->ll_symbol);
7054 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
7055 "Location list end address (%s)",
7056 list_head->ll_symbol);
7058 size = size_of_locs (curr->expr);
7060 /* Output the block length for this list of location operations. */
7061 gcc_assert (size <= 0xffff);
7062 dw2_asm_output_data (2, size, "%s", "Location expression size");
7064 output_loc_sequence (curr->expr);
7067 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7068 "Location list terminator begin (%s)",
7069 list_head->ll_symbol);
7070 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7071 "Location list terminator end (%s)",
7072 list_head->ll_symbol);
7075 /* Output the DIE and its attributes. Called recursively to generate
7076 the definitions of each child DIE. */
7078 static void
7079 output_die (dw_die_ref die)
7081 dw_attr_ref a;
7082 dw_die_ref c;
7083 unsigned long size;
7084 unsigned ix;
7086 /* If someone in another CU might refer to us, set up a symbol for
7087 them to point to. */
7088 if (die->die_symbol)
7089 output_die_symbol (die);
7091 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
7092 die->die_offset, dwarf_tag_name (die->die_tag));
7094 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7096 const char *name = dwarf_attr_name (a->dw_attr);
7098 switch (AT_class (a))
7100 case dw_val_class_addr:
7101 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
7102 break;
7104 case dw_val_class_offset:
7105 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
7106 "%s", name);
7107 break;
7109 case dw_val_class_range_list:
7111 char *p = strchr (ranges_section_label, '\0');
7113 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
7114 a->dw_attr_val.v.val_offset);
7115 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
7116 debug_ranges_section, "%s", name);
7117 *p = '\0';
7119 break;
7121 case dw_val_class_loc:
7122 size = size_of_locs (AT_loc (a));
7124 /* Output the block length for this list of location operations. */
7125 dw2_asm_output_data (constant_size (size), size, "%s", name);
7127 output_loc_sequence (AT_loc (a));
7128 break;
7130 case dw_val_class_const:
7131 /* ??? It would be slightly more efficient to use a scheme like is
7132 used for unsigned constants below, but gdb 4.x does not sign
7133 extend. Gdb 5.x does sign extend. */
7134 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
7135 break;
7137 case dw_val_class_unsigned_const:
7138 dw2_asm_output_data (constant_size (AT_unsigned (a)),
7139 AT_unsigned (a), "%s", name);
7140 break;
7142 case dw_val_class_long_long:
7144 unsigned HOST_WIDE_INT first, second;
7146 dw2_asm_output_data (1,
7147 2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7148 "%s", name);
7150 if (WORDS_BIG_ENDIAN)
7152 first = a->dw_attr_val.v.val_long_long.hi;
7153 second = a->dw_attr_val.v.val_long_long.low;
7155 else
7157 first = a->dw_attr_val.v.val_long_long.low;
7158 second = a->dw_attr_val.v.val_long_long.hi;
7161 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7162 first, "long long constant");
7163 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7164 second, NULL);
7166 break;
7168 case dw_val_class_vec:
7170 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
7171 unsigned int len = a->dw_attr_val.v.val_vec.length;
7172 unsigned int i;
7173 unsigned char *p;
7175 dw2_asm_output_data (1, len * elt_size, "%s", name);
7176 if (elt_size > sizeof (HOST_WIDE_INT))
7178 elt_size /= 2;
7179 len *= 2;
7181 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
7182 i < len;
7183 i++, p += elt_size)
7184 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
7185 "fp or vector constant word %u", i);
7186 break;
7189 case dw_val_class_flag:
7190 dw2_asm_output_data (1, AT_flag (a), "%s", name);
7191 break;
7193 case dw_val_class_loc_list:
7195 char *sym = AT_loc_list (a)->ll_symbol;
7197 gcc_assert (sym);
7198 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
7199 "%s", name);
7201 break;
7203 case dw_val_class_die_ref:
7204 if (AT_ref_external (a))
7206 char *sym = AT_ref (a)->die_symbol;
7208 gcc_assert (sym);
7209 dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, debug_info_section,
7210 "%s", name);
7212 else
7214 gcc_assert (AT_ref (a)->die_offset);
7215 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
7216 "%s", name);
7218 break;
7220 case dw_val_class_fde_ref:
7222 char l1[20];
7224 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
7225 a->dw_attr_val.v.val_fde_index * 2);
7226 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
7227 "%s", name);
7229 break;
7231 case dw_val_class_lbl_id:
7232 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
7233 break;
7235 case dw_val_class_lineptr:
7236 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7237 debug_line_section, "%s", name);
7238 break;
7240 case dw_val_class_macptr:
7241 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7242 debug_macinfo_section, "%s", name);
7243 break;
7245 case dw_val_class_str:
7246 if (AT_string_form (a) == DW_FORM_strp)
7247 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
7248 a->dw_attr_val.v.val_str->label,
7249 debug_str_section,
7250 "%s: \"%s\"", name, AT_string (a));
7251 else
7252 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
7253 break;
7255 case dw_val_class_file:
7257 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
7259 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
7260 a->dw_attr_val.v.val_file->filename);
7261 break;
7264 default:
7265 gcc_unreachable ();
7269 FOR_EACH_CHILD (die, c, output_die (c));
7271 /* Add null byte to terminate sibling list. */
7272 if (die->die_child != NULL)
7273 dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
7274 die->die_offset);
7277 /* Output the compilation unit that appears at the beginning of the
7278 .debug_info section, and precedes the DIE descriptions. */
7280 static void
7281 output_compilation_unit_header (void)
7283 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7284 dw2_asm_output_data (4, 0xffffffff,
7285 "Initial length escape value indicating 64-bit DWARF extension");
7286 dw2_asm_output_data (DWARF_OFFSET_SIZE,
7287 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
7288 "Length of Compilation Unit Info");
7289 dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
7290 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
7291 debug_abbrev_section,
7292 "Offset Into Abbrev. Section");
7293 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
7296 /* Output the compilation unit DIE and its children. */
7298 static void
7299 output_comp_unit (dw_die_ref die, int output_if_empty)
7301 const char *secname;
7302 char *oldsym, *tmp;
7304 /* Unless we are outputting main CU, we may throw away empty ones. */
7305 if (!output_if_empty && die->die_child == NULL)
7306 return;
7308 /* Even if there are no children of this DIE, we must output the information
7309 about the compilation unit. Otherwise, on an empty translation unit, we
7310 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
7311 will then complain when examining the file. First mark all the DIEs in
7312 this CU so we know which get local refs. */
7313 mark_dies (die);
7315 build_abbrev_table (die);
7317 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
7318 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7319 calc_die_sizes (die);
7321 oldsym = die->die_symbol;
7322 if (oldsym)
7324 tmp = alloca (strlen (oldsym) + 24);
7326 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
7327 secname = tmp;
7328 die->die_symbol = NULL;
7329 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
7331 else
7332 switch_to_section (debug_info_section);
7334 /* Output debugging information. */
7335 output_compilation_unit_header ();
7336 output_die (die);
7338 /* Leave the marks on the main CU, so we can check them in
7339 output_pubnames. */
7340 if (oldsym)
7342 unmark_dies (die);
7343 die->die_symbol = oldsym;
7347 /* Return the DWARF2/3 pubname associated with a decl. */
7349 static const char *
7350 dwarf2_name (tree decl, int scope)
7352 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
7355 /* Add a new entry to .debug_pubnames if appropriate. */
7357 static void
7358 add_pubname (tree decl, dw_die_ref die)
7360 pubname_entry e;
7362 if (! TREE_PUBLIC (decl))
7363 return;
7365 e.die = die;
7366 e.name = xstrdup (dwarf2_name (decl, 1));
7367 VEC_safe_push (pubname_entry, gc, pubname_table, &e);
7370 /* Add a new entry to .debug_pubtypes if appropriate. */
7372 static void
7373 add_pubtype (tree decl, dw_die_ref die)
7375 pubname_entry e;
7377 e.name = NULL;
7378 if ((TREE_PUBLIC (decl)
7379 || die->die_parent == comp_unit_die)
7380 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
7382 e.die = die;
7383 if (TYPE_P (decl))
7385 if (TYPE_NAME (decl))
7387 if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
7388 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
7389 else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
7390 && DECL_NAME (TYPE_NAME (decl)))
7391 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
7392 else
7393 e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
7396 else
7397 e.name = xstrdup (dwarf2_name (decl, 1));
7399 /* If we don't have a name for the type, there's no point in adding
7400 it to the table. */
7401 if (e.name && e.name[0] != '\0')
7402 VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
7406 /* Output the public names table used to speed up access to externally
7407 visible names; or the public types table used to find type definitions. */
7409 static void
7410 output_pubnames (VEC (pubname_entry, gc) * names)
7412 unsigned i;
7413 unsigned long pubnames_length = size_of_pubnames (names);
7414 pubname_ref pub;
7416 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7417 dw2_asm_output_data (4, 0xffffffff,
7418 "Initial length escape value indicating 64-bit DWARF extension");
7419 if (names == pubname_table)
7420 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7421 "Length of Public Names Info");
7422 else
7423 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7424 "Length of Public Type Names Info");
7425 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7426 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7427 debug_info_section,
7428 "Offset of Compilation Unit Info");
7429 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
7430 "Compilation Unit Length");
7432 for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
7434 /* We shouldn't see pubnames for DIEs outside of the main CU. */
7435 if (names == pubname_table)
7436 gcc_assert (pub->die->die_mark);
7438 if (names != pubtype_table
7439 || pub->die->die_offset != 0
7440 || !flag_eliminate_unused_debug_types)
7442 dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
7443 "DIE offset");
7445 dw2_asm_output_nstring (pub->name, -1, "external name");
7449 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
7452 /* Add a new entry to .debug_aranges if appropriate. */
7454 static void
7455 add_arange (tree decl, dw_die_ref die)
7457 if (! DECL_SECTION_NAME (decl))
7458 return;
7460 if (arange_table_in_use == arange_table_allocated)
7462 arange_table_allocated += ARANGE_TABLE_INCREMENT;
7463 arange_table = ggc_realloc (arange_table,
7464 (arange_table_allocated
7465 * sizeof (dw_die_ref)));
7466 memset (arange_table + arange_table_in_use, 0,
7467 ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
7470 arange_table[arange_table_in_use++] = die;
7473 /* Output the information that goes into the .debug_aranges table.
7474 Namely, define the beginning and ending address range of the
7475 text section generated for this compilation unit. */
7477 static void
7478 output_aranges (void)
7480 unsigned i;
7481 unsigned long aranges_length = size_of_aranges ();
7483 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7484 dw2_asm_output_data (4, 0xffffffff,
7485 "Initial length escape value indicating 64-bit DWARF extension");
7486 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
7487 "Length of Address Ranges Info");
7488 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7489 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7490 debug_info_section,
7491 "Offset of Compilation Unit Info");
7492 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
7493 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
7495 /* We need to align to twice the pointer size here. */
7496 if (DWARF_ARANGES_PAD_SIZE)
7498 /* Pad using a 2 byte words so that padding is correct for any
7499 pointer size. */
7500 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
7501 2 * DWARF2_ADDR_SIZE);
7502 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
7503 dw2_asm_output_data (2, 0, NULL);
7506 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
7507 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
7508 text_section_label, "Length");
7509 if (flag_reorder_blocks_and_partition)
7511 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
7512 "Address");
7513 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
7514 cold_text_section_label, "Length");
7517 for (i = 0; i < arange_table_in_use; i++)
7519 dw_die_ref die = arange_table[i];
7521 /* We shouldn't see aranges for DIEs outside of the main CU. */
7522 gcc_assert (die->die_mark);
7524 if (die->die_tag == DW_TAG_subprogram)
7526 dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
7527 "Address");
7528 dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
7529 get_AT_low_pc (die), "Length");
7531 else
7533 /* A static variable; extract the symbol from DW_AT_location.
7534 Note that this code isn't currently hit, as we only emit
7535 aranges for functions (jason 9/23/99). */
7536 dw_attr_ref a = get_AT (die, DW_AT_location);
7537 dw_loc_descr_ref loc;
7539 gcc_assert (a && AT_class (a) == dw_val_class_loc);
7541 loc = AT_loc (a);
7542 gcc_assert (loc->dw_loc_opc == DW_OP_addr);
7544 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
7545 loc->dw_loc_oprnd1.v.val_addr, "Address");
7546 dw2_asm_output_data (DWARF2_ADDR_SIZE,
7547 get_AT_unsigned (die, DW_AT_byte_size),
7548 "Length");
7552 /* Output the terminator words. */
7553 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7554 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7557 /* Add a new entry to .debug_ranges. Return the offset at which it
7558 was placed. */
7560 static unsigned int
7561 add_ranges (tree block)
7563 unsigned int in_use = ranges_table_in_use;
7565 if (in_use == ranges_table_allocated)
7567 ranges_table_allocated += RANGES_TABLE_INCREMENT;
7568 ranges_table
7569 = ggc_realloc (ranges_table, (ranges_table_allocated
7570 * sizeof (struct dw_ranges_struct)));
7571 memset (ranges_table + ranges_table_in_use, 0,
7572 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
7575 ranges_table[in_use].block_num = (block ? BLOCK_NUMBER (block) : 0);
7576 ranges_table_in_use = in_use + 1;
7578 return in_use * 2 * DWARF2_ADDR_SIZE;
7581 static void
7582 output_ranges (void)
7584 unsigned i;
7585 static const char *const start_fmt = "Offset 0x%x";
7586 const char *fmt = start_fmt;
7588 for (i = 0; i < ranges_table_in_use; i++)
7590 int block_num = ranges_table[i].block_num;
7592 if (block_num)
7594 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
7595 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
7597 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
7598 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
7600 /* If all code is in the text section, then the compilation
7601 unit base address defaults to DW_AT_low_pc, which is the
7602 base of the text section. */
7603 if (!have_multiple_function_sections)
7605 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
7606 text_section_label,
7607 fmt, i * 2 * DWARF2_ADDR_SIZE);
7608 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
7609 text_section_label, NULL);
7612 /* Otherwise, we add a DW_AT_entry_pc attribute to force the
7613 compilation unit base address to zero, which allows us to
7614 use absolute addresses, and not worry about whether the
7615 target supports cross-section arithmetic. */
7616 else
7618 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
7619 fmt, i * 2 * DWARF2_ADDR_SIZE);
7620 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
7623 fmt = NULL;
7625 else
7627 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7628 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7629 fmt = start_fmt;
7634 /* Data structure containing information about input files. */
7635 struct file_info
7637 const char *path; /* Complete file name. */
7638 const char *fname; /* File name part. */
7639 int length; /* Length of entire string. */
7640 struct dwarf_file_data * file_idx; /* Index in input file table. */
7641 int dir_idx; /* Index in directory table. */
7644 /* Data structure containing information about directories with source
7645 files. */
7646 struct dir_info
7648 const char *path; /* Path including directory name. */
7649 int length; /* Path length. */
7650 int prefix; /* Index of directory entry which is a prefix. */
7651 int count; /* Number of files in this directory. */
7652 int dir_idx; /* Index of directory used as base. */
7655 /* Callback function for file_info comparison. We sort by looking at
7656 the directories in the path. */
7658 static int
7659 file_info_cmp (const void *p1, const void *p2)
7661 const struct file_info *s1 = p1;
7662 const struct file_info *s2 = p2;
7663 unsigned char *cp1;
7664 unsigned char *cp2;
7666 /* Take care of file names without directories. We need to make sure that
7667 we return consistent values to qsort since some will get confused if
7668 we return the same value when identical operands are passed in opposite
7669 orders. So if neither has a directory, return 0 and otherwise return
7670 1 or -1 depending on which one has the directory. */
7671 if ((s1->path == s1->fname || s2->path == s2->fname))
7672 return (s2->path == s2->fname) - (s1->path == s1->fname);
7674 cp1 = (unsigned char *) s1->path;
7675 cp2 = (unsigned char *) s2->path;
7677 while (1)
7679 ++cp1;
7680 ++cp2;
7681 /* Reached the end of the first path? If so, handle like above. */
7682 if ((cp1 == (unsigned char *) s1->fname)
7683 || (cp2 == (unsigned char *) s2->fname))
7684 return ((cp2 == (unsigned char *) s2->fname)
7685 - (cp1 == (unsigned char *) s1->fname));
7687 /* Character of current path component the same? */
7688 else if (*cp1 != *cp2)
7689 return *cp1 - *cp2;
7693 struct file_name_acquire_data
7695 struct file_info *files;
7696 int used_files;
7697 int max_files;
7700 /* Traversal function for the hash table. */
7702 static int
7703 file_name_acquire (void ** slot, void *data)
7705 struct file_name_acquire_data *fnad = data;
7706 struct dwarf_file_data *d = *slot;
7707 struct file_info *fi;
7708 const char *f;
7710 gcc_assert (fnad->max_files >= d->emitted_number);
7712 if (! d->emitted_number)
7713 return 1;
7715 gcc_assert (fnad->max_files != fnad->used_files);
7717 fi = fnad->files + fnad->used_files++;
7719 /* Skip all leading "./". */
7720 f = d->filename;
7721 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
7722 f += 2;
7724 /* Create a new array entry. */
7725 fi->path = f;
7726 fi->length = strlen (f);
7727 fi->file_idx = d;
7729 /* Search for the file name part. */
7730 f = strrchr (f, DIR_SEPARATOR);
7731 #if defined (DIR_SEPARATOR_2)
7733 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
7735 if (g != NULL)
7737 if (f == NULL || f < g)
7738 f = g;
7741 #endif
7743 fi->fname = f == NULL ? fi->path : f + 1;
7744 return 1;
7747 /* Output the directory table and the file name table. We try to minimize
7748 the total amount of memory needed. A heuristic is used to avoid large
7749 slowdowns with many input files. */
7751 static void
7752 output_file_names (void)
7754 struct file_name_acquire_data fnad;
7755 int numfiles;
7756 struct file_info *files;
7757 struct dir_info *dirs;
7758 int *saved;
7759 int *savehere;
7760 int *backmap;
7761 int ndirs;
7762 int idx_offset;
7763 int i;
7764 int idx;
7766 if (!last_emitted_file)
7768 dw2_asm_output_data (1, 0, "End directory table");
7769 dw2_asm_output_data (1, 0, "End file name table");
7770 return;
7773 numfiles = last_emitted_file->emitted_number;
7775 /* Allocate the various arrays we need. */
7776 files = alloca (numfiles * sizeof (struct file_info));
7777 dirs = alloca (numfiles * sizeof (struct dir_info));
7779 fnad.files = files;
7780 fnad.used_files = 0;
7781 fnad.max_files = numfiles;
7782 htab_traverse (file_table, file_name_acquire, &fnad);
7783 gcc_assert (fnad.used_files == fnad.max_files);
7785 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
7787 /* Find all the different directories used. */
7788 dirs[0].path = files[0].path;
7789 dirs[0].length = files[0].fname - files[0].path;
7790 dirs[0].prefix = -1;
7791 dirs[0].count = 1;
7792 dirs[0].dir_idx = 0;
7793 files[0].dir_idx = 0;
7794 ndirs = 1;
7796 for (i = 1; i < numfiles; i++)
7797 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
7798 && memcmp (dirs[ndirs - 1].path, files[i].path,
7799 dirs[ndirs - 1].length) == 0)
7801 /* Same directory as last entry. */
7802 files[i].dir_idx = ndirs - 1;
7803 ++dirs[ndirs - 1].count;
7805 else
7807 int j;
7809 /* This is a new directory. */
7810 dirs[ndirs].path = files[i].path;
7811 dirs[ndirs].length = files[i].fname - files[i].path;
7812 dirs[ndirs].count = 1;
7813 dirs[ndirs].dir_idx = ndirs;
7814 files[i].dir_idx = ndirs;
7816 /* Search for a prefix. */
7817 dirs[ndirs].prefix = -1;
7818 for (j = 0; j < ndirs; j++)
7819 if (dirs[j].length < dirs[ndirs].length
7820 && dirs[j].length > 1
7821 && (dirs[ndirs].prefix == -1
7822 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
7823 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
7824 dirs[ndirs].prefix = j;
7826 ++ndirs;
7829 /* Now to the actual work. We have to find a subset of the directories which
7830 allow expressing the file name using references to the directory table
7831 with the least amount of characters. We do not do an exhaustive search
7832 where we would have to check out every combination of every single
7833 possible prefix. Instead we use a heuristic which provides nearly optimal
7834 results in most cases and never is much off. */
7835 saved = alloca (ndirs * sizeof (int));
7836 savehere = alloca (ndirs * sizeof (int));
7838 memset (saved, '\0', ndirs * sizeof (saved[0]));
7839 for (i = 0; i < ndirs; i++)
7841 int j;
7842 int total;
7844 /* We can always save some space for the current directory. But this
7845 does not mean it will be enough to justify adding the directory. */
7846 savehere[i] = dirs[i].length;
7847 total = (savehere[i] - saved[i]) * dirs[i].count;
7849 for (j = i + 1; j < ndirs; j++)
7851 savehere[j] = 0;
7852 if (saved[j] < dirs[i].length)
7854 /* Determine whether the dirs[i] path is a prefix of the
7855 dirs[j] path. */
7856 int k;
7858 k = dirs[j].prefix;
7859 while (k != -1 && k != (int) i)
7860 k = dirs[k].prefix;
7862 if (k == (int) i)
7864 /* Yes it is. We can possibly save some memory by
7865 writing the filenames in dirs[j] relative to
7866 dirs[i]. */
7867 savehere[j] = dirs[i].length;
7868 total += (savehere[j] - saved[j]) * dirs[j].count;
7873 /* Check whether we can save enough to justify adding the dirs[i]
7874 directory. */
7875 if (total > dirs[i].length + 1)
7877 /* It's worthwhile adding. */
7878 for (j = i; j < ndirs; j++)
7879 if (savehere[j] > 0)
7881 /* Remember how much we saved for this directory so far. */
7882 saved[j] = savehere[j];
7884 /* Remember the prefix directory. */
7885 dirs[j].dir_idx = i;
7890 /* Emit the directory name table. */
7891 idx = 1;
7892 idx_offset = dirs[0].length > 0 ? 1 : 0;
7893 for (i = 1 - idx_offset; i < ndirs; i++)
7894 dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
7895 "Directory Entry: 0x%x", i + idx_offset);
7897 dw2_asm_output_data (1, 0, "End directory table");
7899 /* We have to emit them in the order of emitted_number since that's
7900 used in the debug info generation. To do this efficiently we
7901 generate a back-mapping of the indices first. */
7902 backmap = alloca (numfiles * sizeof (int));
7903 for (i = 0; i < numfiles; i++)
7904 backmap[files[i].file_idx->emitted_number - 1] = i;
7906 /* Now write all the file names. */
7907 for (i = 0; i < numfiles; i++)
7909 int file_idx = backmap[i];
7910 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
7912 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
7913 "File Entry: 0x%x", (unsigned) i + 1);
7915 /* Include directory index. */
7916 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
7918 /* Modification time. */
7919 dw2_asm_output_data_uleb128 (0, NULL);
7921 /* File length in bytes. */
7922 dw2_asm_output_data_uleb128 (0, NULL);
7925 dw2_asm_output_data (1, 0, "End file name table");
7929 /* Output the source line number correspondence information. This
7930 information goes into the .debug_line section. */
7932 static void
7933 output_line_info (void)
7935 char l1[20], l2[20], p1[20], p2[20];
7936 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
7937 char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
7938 unsigned opc;
7939 unsigned n_op_args;
7940 unsigned long lt_index;
7941 unsigned long current_line;
7942 long line_offset;
7943 long line_delta;
7944 unsigned long current_file;
7945 unsigned long function;
7947 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
7948 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
7949 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
7950 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
7952 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7953 dw2_asm_output_data (4, 0xffffffff,
7954 "Initial length escape value indicating 64-bit DWARF extension");
7955 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
7956 "Length of Source Line Info");
7957 ASM_OUTPUT_LABEL (asm_out_file, l1);
7959 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7960 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
7961 ASM_OUTPUT_LABEL (asm_out_file, p1);
7963 /* Define the architecture-dependent minimum instruction length (in
7964 bytes). In this implementation of DWARF, this field is used for
7965 information purposes only. Since GCC generates assembly language,
7966 we have no a priori knowledge of how many instruction bytes are
7967 generated for each source line, and therefore can use only the
7968 DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
7969 commands. Accordingly, we fix this as `1', which is "correct
7970 enough" for all architectures, and don't let the target override. */
7971 dw2_asm_output_data (1, 1,
7972 "Minimum Instruction Length");
7974 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
7975 "Default is_stmt_start flag");
7976 dw2_asm_output_data (1, DWARF_LINE_BASE,
7977 "Line Base Value (Special Opcodes)");
7978 dw2_asm_output_data (1, DWARF_LINE_RANGE,
7979 "Line Range Value (Special Opcodes)");
7980 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
7981 "Special Opcode Base");
7983 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
7985 switch (opc)
7987 case DW_LNS_advance_pc:
7988 case DW_LNS_advance_line:
7989 case DW_LNS_set_file:
7990 case DW_LNS_set_column:
7991 case DW_LNS_fixed_advance_pc:
7992 n_op_args = 1;
7993 break;
7994 default:
7995 n_op_args = 0;
7996 break;
7999 dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
8000 opc, n_op_args);
8003 /* Write out the information about the files we use. */
8004 output_file_names ();
8005 ASM_OUTPUT_LABEL (asm_out_file, p2);
8007 /* We used to set the address register to the first location in the text
8008 section here, but that didn't accomplish anything since we already
8009 have a line note for the opening brace of the first function. */
8011 /* Generate the line number to PC correspondence table, encoded as
8012 a series of state machine operations. */
8013 current_file = 1;
8014 current_line = 1;
8016 if (cfun && in_cold_section_p)
8017 strcpy (prev_line_label, cfun->cold_section_label);
8018 else
8019 strcpy (prev_line_label, text_section_label);
8020 for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
8022 dw_line_info_ref line_info = &line_info_table[lt_index];
8024 #if 0
8025 /* Disable this optimization for now; GDB wants to see two line notes
8026 at the beginning of a function so it can find the end of the
8027 prologue. */
8029 /* Don't emit anything for redundant notes. Just updating the
8030 address doesn't accomplish anything, because we already assume
8031 that anything after the last address is this line. */
8032 if (line_info->dw_line_num == current_line
8033 && line_info->dw_file_num == current_file)
8034 continue;
8035 #endif
8037 /* Emit debug info for the address of the current line.
8039 Unfortunately, we have little choice here currently, and must always
8040 use the most general form. GCC does not know the address delta
8041 itself, so we can't use DW_LNS_advance_pc. Many ports do have length
8042 attributes which will give an upper bound on the address range. We
8043 could perhaps use length attributes to determine when it is safe to
8044 use DW_LNS_fixed_advance_pc. */
8046 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
8047 if (0)
8049 /* This can handle deltas up to 0xffff. This takes 3 bytes. */
8050 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8051 "DW_LNS_fixed_advance_pc");
8052 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8054 else
8056 /* This can handle any delta. This takes
8057 4+DWARF2_ADDR_SIZE bytes. */
8058 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8059 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8060 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8061 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8064 strcpy (prev_line_label, line_label);
8066 /* Emit debug info for the source file of the current line, if
8067 different from the previous line. */
8068 if (line_info->dw_file_num != current_file)
8070 current_file = line_info->dw_file_num;
8071 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8072 dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
8075 /* Emit debug info for the current line number, choosing the encoding
8076 that uses the least amount of space. */
8077 if (line_info->dw_line_num != current_line)
8079 line_offset = line_info->dw_line_num - current_line;
8080 line_delta = line_offset - DWARF_LINE_BASE;
8081 current_line = line_info->dw_line_num;
8082 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
8083 /* This can handle deltas from -10 to 234, using the current
8084 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE. This
8085 takes 1 byte. */
8086 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
8087 "line %lu", current_line);
8088 else
8090 /* This can handle any delta. This takes at least 4 bytes,
8091 depending on the value being encoded. */
8092 dw2_asm_output_data (1, DW_LNS_advance_line,
8093 "advance to line %lu", current_line);
8094 dw2_asm_output_data_sleb128 (line_offset, NULL);
8095 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8098 else
8099 /* We still need to start a new row, so output a copy insn. */
8100 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8103 /* Emit debug info for the address of the end of the function. */
8104 if (0)
8106 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8107 "DW_LNS_fixed_advance_pc");
8108 dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
8110 else
8112 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8113 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8114 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8115 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
8118 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
8119 dw2_asm_output_data_uleb128 (1, NULL);
8120 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
8122 function = 0;
8123 current_file = 1;
8124 current_line = 1;
8125 for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
8127 dw_separate_line_info_ref line_info
8128 = &separate_line_info_table[lt_index];
8130 #if 0
8131 /* Don't emit anything for redundant notes. */
8132 if (line_info->dw_line_num == current_line
8133 && line_info->dw_file_num == current_file
8134 && line_info->function == function)
8135 goto cont;
8136 #endif
8138 /* Emit debug info for the address of the current line. If this is
8139 a new function, or the first line of a function, then we need
8140 to handle it differently. */
8141 ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
8142 lt_index);
8143 if (function != line_info->function)
8145 function = line_info->function;
8147 /* Set the address register to the first line in the function. */
8148 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8149 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8150 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8151 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8153 else
8155 /* ??? See the DW_LNS_advance_pc comment above. */
8156 if (0)
8158 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8159 "DW_LNS_fixed_advance_pc");
8160 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8162 else
8164 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8165 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8166 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8167 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8171 strcpy (prev_line_label, line_label);
8173 /* Emit debug info for the source file of the current line, if
8174 different from the previous line. */
8175 if (line_info->dw_file_num != current_file)
8177 current_file = line_info->dw_file_num;
8178 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8179 dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
8182 /* Emit debug info for the current line number, choosing the encoding
8183 that uses the least amount of space. */
8184 if (line_info->dw_line_num != current_line)
8186 line_offset = line_info->dw_line_num - current_line;
8187 line_delta = line_offset - DWARF_LINE_BASE;
8188 current_line = line_info->dw_line_num;
8189 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
8190 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
8191 "line %lu", current_line);
8192 else
8194 dw2_asm_output_data (1, DW_LNS_advance_line,
8195 "advance to line %lu", current_line);
8196 dw2_asm_output_data_sleb128 (line_offset, NULL);
8197 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8200 else
8201 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8203 #if 0
8204 cont:
8205 #endif
8207 lt_index++;
8209 /* If we're done with a function, end its sequence. */
8210 if (lt_index == separate_line_info_table_in_use
8211 || separate_line_info_table[lt_index].function != function)
8213 current_file = 1;
8214 current_line = 1;
8216 /* Emit debug info for the address of the end of the function. */
8217 ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
8218 if (0)
8220 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8221 "DW_LNS_fixed_advance_pc");
8222 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8224 else
8226 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8227 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8228 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8229 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8232 /* Output the marker for the end of this sequence. */
8233 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
8234 dw2_asm_output_data_uleb128 (1, NULL);
8235 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
8239 /* Output the marker for the end of the line number info. */
8240 ASM_OUTPUT_LABEL (asm_out_file, l2);
8243 /* Given a pointer to a tree node for some base type, return a pointer to
8244 a DIE that describes the given type.
8246 This routine must only be called for GCC type nodes that correspond to
8247 Dwarf base (fundamental) types. */
8249 static dw_die_ref
8250 base_type_die (tree type)
8252 dw_die_ref base_type_result;
8253 enum dwarf_type encoding;
8255 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
8256 return 0;
8258 switch (TREE_CODE (type))
8260 case INTEGER_TYPE:
8261 if (TYPE_STRING_FLAG (type))
8263 if (TYPE_UNSIGNED (type))
8264 encoding = DW_ATE_unsigned_char;
8265 else
8266 encoding = DW_ATE_signed_char;
8268 else if (TYPE_UNSIGNED (type))
8269 encoding = DW_ATE_unsigned;
8270 else
8271 encoding = DW_ATE_signed;
8272 break;
8274 case REAL_TYPE:
8275 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
8276 encoding = DW_ATE_decimal_float;
8277 else
8278 encoding = DW_ATE_float;
8279 break;
8281 /* Dwarf2 doesn't know anything about complex ints, so use
8282 a user defined type for it. */
8283 case COMPLEX_TYPE:
8284 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
8285 encoding = DW_ATE_complex_float;
8286 else
8287 encoding = DW_ATE_lo_user;
8288 break;
8290 case BOOLEAN_TYPE:
8291 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
8292 encoding = DW_ATE_boolean;
8293 break;
8295 default:
8296 /* No other TREE_CODEs are Dwarf fundamental types. */
8297 gcc_unreachable ();
8300 base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
8302 /* This probably indicates a bug. */
8303 if (! TYPE_NAME (type))
8304 add_name_attribute (base_type_result, "__unknown__");
8306 add_AT_unsigned (base_type_result, DW_AT_byte_size,
8307 int_size_in_bytes (type));
8308 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
8310 return base_type_result;
8313 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
8314 given input type is a Dwarf "fundamental" type. Otherwise return null. */
8316 static inline int
8317 is_base_type (tree type)
8319 switch (TREE_CODE (type))
8321 case ERROR_MARK:
8322 case VOID_TYPE:
8323 case INTEGER_TYPE:
8324 case REAL_TYPE:
8325 case COMPLEX_TYPE:
8326 case BOOLEAN_TYPE:
8327 return 1;
8329 case ARRAY_TYPE:
8330 case RECORD_TYPE:
8331 case UNION_TYPE:
8332 case QUAL_UNION_TYPE:
8333 case ENUMERAL_TYPE:
8334 case FUNCTION_TYPE:
8335 case METHOD_TYPE:
8336 case POINTER_TYPE:
8337 case REFERENCE_TYPE:
8338 case OFFSET_TYPE:
8339 case LANG_TYPE:
8340 case VECTOR_TYPE:
8341 return 0;
8343 default:
8344 gcc_unreachable ();
8347 return 0;
8350 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
8351 node, return the size in bits for the type if it is a constant, or else
8352 return the alignment for the type if the type's size is not constant, or
8353 else return BITS_PER_WORD if the type actually turns out to be an
8354 ERROR_MARK node. */
8356 static inline unsigned HOST_WIDE_INT
8357 simple_type_size_in_bits (tree type)
8359 if (TREE_CODE (type) == ERROR_MARK)
8360 return BITS_PER_WORD;
8361 else if (TYPE_SIZE (type) == NULL_TREE)
8362 return 0;
8363 else if (host_integerp (TYPE_SIZE (type), 1))
8364 return tree_low_cst (TYPE_SIZE (type), 1);
8365 else
8366 return TYPE_ALIGN (type);
8369 /* Return true if the debug information for the given type should be
8370 emitted as a subrange type. */
8372 static inline bool
8373 is_subrange_type (tree type)
8375 tree subtype = TREE_TYPE (type);
8377 /* Subrange types are identified by the fact that they are integer
8378 types, and that they have a subtype which is either an integer type
8379 or an enumeral type. */
8381 if (TREE_CODE (type) != INTEGER_TYPE
8382 || subtype == NULL_TREE)
8383 return false;
8385 if (TREE_CODE (subtype) != INTEGER_TYPE
8386 && TREE_CODE (subtype) != ENUMERAL_TYPE)
8387 return false;
8389 if (TREE_CODE (type) == TREE_CODE (subtype)
8390 && int_size_in_bytes (type) == int_size_in_bytes (subtype)
8391 && TYPE_MIN_VALUE (type) != NULL
8392 && TYPE_MIN_VALUE (subtype) != NULL
8393 && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
8394 && TYPE_MAX_VALUE (type) != NULL
8395 && TYPE_MAX_VALUE (subtype) != NULL
8396 && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
8398 /* The type and its subtype have the same representation. If in
8399 addition the two types also have the same name, then the given
8400 type is not a subrange type, but rather a plain base type. */
8401 /* FIXME: brobecker/2004-03-22:
8402 Sizetype INTEGER_CSTs nodes are canonicalized. It should
8403 therefore be sufficient to check the TYPE_SIZE node pointers
8404 rather than checking the actual size. Unfortunately, we have
8405 found some cases, such as in the Ada "integer" type, where
8406 this is not the case. Until this problem is solved, we need to
8407 keep checking the actual size. */
8408 tree type_name = TYPE_NAME (type);
8409 tree subtype_name = TYPE_NAME (subtype);
8411 if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
8412 type_name = DECL_NAME (type_name);
8414 if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
8415 subtype_name = DECL_NAME (subtype_name);
8417 if (type_name == subtype_name)
8418 return false;
8421 return true;
8424 /* Given a pointer to a tree node for a subrange type, return a pointer
8425 to a DIE that describes the given type. */
8427 static dw_die_ref
8428 subrange_type_die (tree type, dw_die_ref context_die)
8430 dw_die_ref subrange_die;
8431 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
8433 if (context_die == NULL)
8434 context_die = comp_unit_die;
8436 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
8438 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
8440 /* The size of the subrange type and its base type do not match,
8441 so we need to generate a size attribute for the subrange type. */
8442 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
8445 if (TYPE_MIN_VALUE (type) != NULL)
8446 add_bound_info (subrange_die, DW_AT_lower_bound,
8447 TYPE_MIN_VALUE (type));
8448 if (TYPE_MAX_VALUE (type) != NULL)
8449 add_bound_info (subrange_die, DW_AT_upper_bound,
8450 TYPE_MAX_VALUE (type));
8452 return subrange_die;
8455 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
8456 entry that chains various modifiers in front of the given type. */
8458 static dw_die_ref
8459 modified_type_die (tree type, int is_const_type, int is_volatile_type,
8460 dw_die_ref context_die)
8462 enum tree_code code = TREE_CODE (type);
8463 dw_die_ref mod_type_die;
8464 dw_die_ref sub_die = NULL;
8465 tree item_type = NULL;
8466 tree qualified_type;
8467 tree name;
8469 if (code == ERROR_MARK)
8470 return NULL;
8472 /* See if we already have the appropriately qualified variant of
8473 this type. */
8474 qualified_type
8475 = get_qualified_type (type,
8476 ((is_const_type ? TYPE_QUAL_CONST : 0)
8477 | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
8479 /* If we do, then we can just use its DIE, if it exists. */
8480 if (qualified_type)
8482 mod_type_die = lookup_type_die (qualified_type);
8483 if (mod_type_die)
8484 return mod_type_die;
8487 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
8489 /* Handle C typedef types. */
8490 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
8492 tree dtype = TREE_TYPE (name);
8494 if (qualified_type == dtype)
8496 /* For a named type, use the typedef. */
8497 gen_type_die (qualified_type, context_die);
8498 return lookup_type_die (qualified_type);
8500 else if (is_const_type < TYPE_READONLY (dtype)
8501 || is_volatile_type < TYPE_VOLATILE (dtype)
8502 || (is_const_type <= TYPE_READONLY (dtype)
8503 && is_volatile_type <= TYPE_VOLATILE (dtype)
8504 && DECL_ORIGINAL_TYPE (name) != type))
8505 /* cv-unqualified version of named type. Just use the unnamed
8506 type to which it refers. */
8507 return modified_type_die (DECL_ORIGINAL_TYPE (name),
8508 is_const_type, is_volatile_type,
8509 context_die);
8510 /* Else cv-qualified version of named type; fall through. */
8513 if (is_const_type)
8515 mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
8516 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
8518 else if (is_volatile_type)
8520 mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
8521 sub_die = modified_type_die (type, 0, 0, context_die);
8523 else if (code == POINTER_TYPE)
8525 mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
8526 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8527 simple_type_size_in_bits (type) / BITS_PER_UNIT);
8528 item_type = TREE_TYPE (type);
8530 else if (code == REFERENCE_TYPE)
8532 mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
8533 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8534 simple_type_size_in_bits (type) / BITS_PER_UNIT);
8535 item_type = TREE_TYPE (type);
8537 else if (is_subrange_type (type))
8539 mod_type_die = subrange_type_die (type, context_die);
8540 item_type = TREE_TYPE (type);
8542 else if (is_base_type (type))
8543 mod_type_die = base_type_die (type);
8544 else
8546 gen_type_die (type, context_die);
8548 /* We have to get the type_main_variant here (and pass that to the
8549 `lookup_type_die' routine) because the ..._TYPE node we have
8550 might simply be a *copy* of some original type node (where the
8551 copy was created to help us keep track of typedef names) and
8552 that copy might have a different TYPE_UID from the original
8553 ..._TYPE node. */
8554 if (TREE_CODE (type) != VECTOR_TYPE)
8555 return lookup_type_die (type_main_variant (type));
8556 else
8557 /* Vectors have the debugging information in the type,
8558 not the main variant. */
8559 return lookup_type_die (type);
8562 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
8563 don't output a DW_TAG_typedef, since there isn't one in the
8564 user's program; just attach a DW_AT_name to the type. */
8565 if (name
8566 && (TREE_CODE (name) != TYPE_DECL || TREE_TYPE (name) == qualified_type))
8568 if (TREE_CODE (name) == TYPE_DECL)
8569 /* Could just call add_name_and_src_coords_attributes here,
8570 but since this is a builtin type it doesn't have any
8571 useful source coordinates anyway. */
8572 name = DECL_NAME (name);
8573 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
8576 if (qualified_type)
8577 equate_type_number_to_die (qualified_type, mod_type_die);
8579 if (item_type)
8580 /* We must do this after the equate_type_number_to_die call, in case
8581 this is a recursive type. This ensures that the modified_type_die
8582 recursion will terminate even if the type is recursive. Recursive
8583 types are possible in Ada. */
8584 sub_die = modified_type_die (item_type,
8585 TYPE_READONLY (item_type),
8586 TYPE_VOLATILE (item_type),
8587 context_die);
8589 if (sub_die != NULL)
8590 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
8592 return mod_type_die;
8595 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
8596 an enumerated type. */
8598 static inline int
8599 type_is_enum (tree type)
8601 return TREE_CODE (type) == ENUMERAL_TYPE;
8604 /* Return the DBX register number described by a given RTL node. */
8606 static unsigned int
8607 dbx_reg_number (rtx rtl)
8609 unsigned regno = REGNO (rtl);
8611 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
8613 #ifdef LEAF_REG_REMAP
8614 if (current_function_uses_only_leaf_regs)
8616 int leaf_reg = LEAF_REG_REMAP (regno);
8617 if (leaf_reg != -1)
8618 regno = (unsigned) leaf_reg;
8620 #endif
8622 return DBX_REGISTER_NUMBER (regno);
8625 /* Optionally add a DW_OP_piece term to a location description expression.
8626 DW_OP_piece is only added if the location description expression already
8627 doesn't end with DW_OP_piece. */
8629 static void
8630 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
8632 dw_loc_descr_ref loc;
8634 if (*list_head != NULL)
8636 /* Find the end of the chain. */
8637 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
8640 if (loc->dw_loc_opc != DW_OP_piece)
8641 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
8645 /* Return a location descriptor that designates a machine register or
8646 zero if there is none. */
8648 static dw_loc_descr_ref
8649 reg_loc_descriptor (rtx rtl)
8651 rtx regs;
8653 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
8654 return 0;
8656 regs = targetm.dwarf_register_span (rtl);
8658 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
8659 return multiple_reg_loc_descriptor (rtl, regs);
8660 else
8661 return one_reg_loc_descriptor (dbx_reg_number (rtl));
8664 /* Return a location descriptor that designates a machine register for
8665 a given hard register number. */
8667 static dw_loc_descr_ref
8668 one_reg_loc_descriptor (unsigned int regno)
8670 if (regno <= 31)
8671 return new_loc_descr (DW_OP_reg0 + regno, 0, 0);
8672 else
8673 return new_loc_descr (DW_OP_regx, regno, 0);
8676 /* Given an RTL of a register, return a location descriptor that
8677 designates a value that spans more than one register. */
8679 static dw_loc_descr_ref
8680 multiple_reg_loc_descriptor (rtx rtl, rtx regs)
8682 int nregs, size, i;
8683 unsigned reg;
8684 dw_loc_descr_ref loc_result = NULL;
8686 reg = REGNO (rtl);
8687 #ifdef LEAF_REG_REMAP
8688 if (current_function_uses_only_leaf_regs)
8690 int leaf_reg = LEAF_REG_REMAP (reg);
8691 if (leaf_reg != -1)
8692 reg = (unsigned) leaf_reg;
8694 #endif
8695 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
8696 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
8698 /* Simple, contiguous registers. */
8699 if (regs == NULL_RTX)
8701 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
8703 loc_result = NULL;
8704 while (nregs--)
8706 dw_loc_descr_ref t;
8708 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg));
8709 add_loc_descr (&loc_result, t);
8710 add_loc_descr_op_piece (&loc_result, size);
8711 ++reg;
8713 return loc_result;
8716 /* Now onto stupid register sets in non contiguous locations. */
8718 gcc_assert (GET_CODE (regs) == PARALLEL);
8720 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8721 loc_result = NULL;
8723 for (i = 0; i < XVECLEN (regs, 0); ++i)
8725 dw_loc_descr_ref t;
8727 t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)));
8728 add_loc_descr (&loc_result, t);
8729 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8730 add_loc_descr_op_piece (&loc_result, size);
8732 return loc_result;
8735 /* Return a location descriptor that designates a constant. */
8737 static dw_loc_descr_ref
8738 int_loc_descriptor (HOST_WIDE_INT i)
8740 enum dwarf_location_atom op;
8742 /* Pick the smallest representation of a constant, rather than just
8743 defaulting to the LEB encoding. */
8744 if (i >= 0)
8746 if (i <= 31)
8747 op = DW_OP_lit0 + i;
8748 else if (i <= 0xff)
8749 op = DW_OP_const1u;
8750 else if (i <= 0xffff)
8751 op = DW_OP_const2u;
8752 else if (HOST_BITS_PER_WIDE_INT == 32
8753 || i <= 0xffffffff)
8754 op = DW_OP_const4u;
8755 else
8756 op = DW_OP_constu;
8758 else
8760 if (i >= -0x80)
8761 op = DW_OP_const1s;
8762 else if (i >= -0x8000)
8763 op = DW_OP_const2s;
8764 else if (HOST_BITS_PER_WIDE_INT == 32
8765 || i >= -0x80000000)
8766 op = DW_OP_const4s;
8767 else
8768 op = DW_OP_consts;
8771 return new_loc_descr (op, i, 0);
8774 /* Return a location descriptor that designates a base+offset location. */
8776 static dw_loc_descr_ref
8777 based_loc_descr (rtx reg, HOST_WIDE_INT offset)
8779 unsigned int regno;
8781 /* We only use "frame base" when we're sure we're talking about the
8782 post-prologue local stack frame. We do this by *not* running
8783 register elimination until this point, and recognizing the special
8784 argument pointer and soft frame pointer rtx's. */
8785 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
8787 rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
8789 if (elim != reg)
8791 if (GET_CODE (elim) == PLUS)
8793 offset += INTVAL (XEXP (elim, 1));
8794 elim = XEXP (elim, 0);
8796 gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
8797 : stack_pointer_rtx));
8798 offset += frame_pointer_fb_offset;
8800 return new_loc_descr (DW_OP_fbreg, offset, 0);
8804 regno = dbx_reg_number (reg);
8805 if (regno <= 31)
8806 return new_loc_descr (DW_OP_breg0 + regno, offset, 0);
8807 else
8808 return new_loc_descr (DW_OP_bregx, regno, offset);
8811 /* Return true if this RTL expression describes a base+offset calculation. */
8813 static inline int
8814 is_based_loc (rtx rtl)
8816 return (GET_CODE (rtl) == PLUS
8817 && ((REG_P (XEXP (rtl, 0))
8818 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
8819 && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
8822 /* Return a descriptor that describes the concatenation of N locations
8823 used to form the address of a memory location. */
8825 static dw_loc_descr_ref
8826 concatn_mem_loc_descriptor (rtx concatn, enum machine_mode mode)
8828 unsigned int i;
8829 dw_loc_descr_ref cc_loc_result = NULL;
8830 unsigned int n = XVECLEN (concatn, 0);
8832 for (i = 0; i < n; ++i)
8834 dw_loc_descr_ref ref;
8835 rtx x = XVECEXP (concatn, 0, i);
8837 ref = mem_loc_descriptor (x, mode);
8838 if (ref == NULL)
8839 return NULL;
8841 add_loc_descr (&cc_loc_result, ref);
8842 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
8845 return cc_loc_result;
8848 /* The following routine converts the RTL for a variable or parameter
8849 (resident in memory) into an equivalent Dwarf representation of a
8850 mechanism for getting the address of that same variable onto the top of a
8851 hypothetical "address evaluation" stack.
8853 When creating memory location descriptors, we are effectively transforming
8854 the RTL for a memory-resident object into its Dwarf postfix expression
8855 equivalent. This routine recursively descends an RTL tree, turning
8856 it into Dwarf postfix code as it goes.
8858 MODE is the mode of the memory reference, needed to handle some
8859 autoincrement addressing modes.
8861 CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
8862 location list for RTL.
8864 Return 0 if we can't represent the location. */
8866 static dw_loc_descr_ref
8867 mem_loc_descriptor (rtx rtl, enum machine_mode mode)
8869 dw_loc_descr_ref mem_loc_result = NULL;
8870 enum dwarf_location_atom op;
8872 /* Note that for a dynamically sized array, the location we will generate a
8873 description of here will be the lowest numbered location which is
8874 actually within the array. That's *not* necessarily the same as the
8875 zeroth element of the array. */
8877 rtl = targetm.delegitimize_address (rtl);
8879 switch (GET_CODE (rtl))
8881 case POST_INC:
8882 case POST_DEC:
8883 case POST_MODIFY:
8884 /* POST_INC and POST_DEC can be handled just like a SUBREG. So we
8885 just fall into the SUBREG code. */
8887 /* ... fall through ... */
8889 case SUBREG:
8890 /* The case of a subreg may arise when we have a local (register)
8891 variable or a formal (register) parameter which doesn't quite fill
8892 up an entire register. For now, just assume that it is
8893 legitimate to make the Dwarf info refer to the whole register which
8894 contains the given subreg. */
8895 rtl = XEXP (rtl, 0);
8897 /* ... fall through ... */
8899 case REG:
8900 /* Whenever a register number forms a part of the description of the
8901 method for calculating the (dynamic) address of a memory resident
8902 object, DWARF rules require the register number be referred to as
8903 a "base register". This distinction is not based in any way upon
8904 what category of register the hardware believes the given register
8905 belongs to. This is strictly DWARF terminology we're dealing with
8906 here. Note that in cases where the location of a memory-resident
8907 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
8908 OP_CONST (0)) the actual DWARF location descriptor that we generate
8909 may just be OP_BASEREG (basereg). This may look deceptively like
8910 the object in question was allocated to a register (rather than in
8911 memory) so DWARF consumers need to be aware of the subtle
8912 distinction between OP_REG and OP_BASEREG. */
8913 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
8914 mem_loc_result = based_loc_descr (rtl, 0);
8915 break;
8917 case MEM:
8918 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl));
8919 if (mem_loc_result != 0)
8920 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
8921 break;
8923 case LO_SUM:
8924 rtl = XEXP (rtl, 1);
8926 /* ... fall through ... */
8928 case LABEL_REF:
8929 /* Some ports can transform a symbol ref into a label ref, because
8930 the symbol ref is too far away and has to be dumped into a constant
8931 pool. */
8932 case CONST:
8933 case SYMBOL_REF:
8934 /* Alternatively, the symbol in the constant pool might be referenced
8935 by a different symbol. */
8936 if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
8938 bool marked;
8939 rtx tmp = get_pool_constant_mark (rtl, &marked);
8941 if (GET_CODE (tmp) == SYMBOL_REF)
8943 rtl = tmp;
8944 if (CONSTANT_POOL_ADDRESS_P (tmp))
8945 get_pool_constant_mark (tmp, &marked);
8946 else
8947 marked = true;
8950 /* If all references to this pool constant were optimized away,
8951 it was not output and thus we can't represent it.
8952 FIXME: might try to use DW_OP_const_value here, though
8953 DW_OP_piece complicates it. */
8954 if (!marked)
8955 return 0;
8958 mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
8959 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
8960 mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
8961 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
8962 break;
8964 case PRE_MODIFY:
8965 /* Extract the PLUS expression nested inside and fall into
8966 PLUS code below. */
8967 rtl = XEXP (rtl, 1);
8968 goto plus;
8970 case PRE_INC:
8971 case PRE_DEC:
8972 /* Turn these into a PLUS expression and fall into the PLUS code
8973 below. */
8974 rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
8975 GEN_INT (GET_CODE (rtl) == PRE_INC
8976 ? GET_MODE_UNIT_SIZE (mode)
8977 : -GET_MODE_UNIT_SIZE (mode)));
8979 /* ... fall through ... */
8981 case PLUS:
8982 plus:
8983 if (is_based_loc (rtl))
8984 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
8985 INTVAL (XEXP (rtl, 1)));
8986 else
8988 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode);
8989 if (mem_loc_result == 0)
8990 break;
8992 if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
8993 && INTVAL (XEXP (rtl, 1)) >= 0)
8994 add_loc_descr (&mem_loc_result,
8995 new_loc_descr (DW_OP_plus_uconst,
8996 INTVAL (XEXP (rtl, 1)), 0));
8997 else
8999 add_loc_descr (&mem_loc_result,
9000 mem_loc_descriptor (XEXP (rtl, 1), mode));
9001 add_loc_descr (&mem_loc_result,
9002 new_loc_descr (DW_OP_plus, 0, 0));
9005 break;
9007 /* If a pseudo-reg is optimized away, it is possible for it to
9008 be replaced with a MEM containing a multiply or shift. */
9009 case MULT:
9010 op = DW_OP_mul;
9011 goto do_binop;
9013 case ASHIFT:
9014 op = DW_OP_shl;
9015 goto do_binop;
9017 case ASHIFTRT:
9018 op = DW_OP_shra;
9019 goto do_binop;
9021 case LSHIFTRT:
9022 op = DW_OP_shr;
9023 goto do_binop;
9025 do_binop:
9027 dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode);
9028 dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode);
9030 if (op0 == 0 || op1 == 0)
9031 break;
9033 mem_loc_result = op0;
9034 add_loc_descr (&mem_loc_result, op1);
9035 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
9036 break;
9039 case CONST_INT:
9040 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
9041 break;
9043 case CONCATN:
9044 mem_loc_result = concatn_mem_loc_descriptor (rtl, mode);
9045 break;
9047 default:
9048 gcc_unreachable ();
9051 return mem_loc_result;
9054 /* Return a descriptor that describes the concatenation of two locations.
9055 This is typically a complex variable. */
9057 static dw_loc_descr_ref
9058 concat_loc_descriptor (rtx x0, rtx x1)
9060 dw_loc_descr_ref cc_loc_result = NULL;
9061 dw_loc_descr_ref x0_ref = loc_descriptor (x0);
9062 dw_loc_descr_ref x1_ref = loc_descriptor (x1);
9064 if (x0_ref == 0 || x1_ref == 0)
9065 return 0;
9067 cc_loc_result = x0_ref;
9068 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
9070 add_loc_descr (&cc_loc_result, x1_ref);
9071 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
9073 return cc_loc_result;
9076 /* Return a descriptor that describes the concatenation of N
9077 locations. */
9079 static dw_loc_descr_ref
9080 concatn_loc_descriptor (rtx concatn)
9082 unsigned int i;
9083 dw_loc_descr_ref cc_loc_result = NULL;
9084 unsigned int n = XVECLEN (concatn, 0);
9086 for (i = 0; i < n; ++i)
9088 dw_loc_descr_ref ref;
9089 rtx x = XVECEXP (concatn, 0, i);
9091 ref = loc_descriptor (x);
9092 if (ref == NULL)
9093 return NULL;
9095 add_loc_descr (&cc_loc_result, ref);
9096 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
9099 return cc_loc_result;
9102 /* Output a proper Dwarf location descriptor for a variable or parameter
9103 which is either allocated in a register or in a memory location. For a
9104 register, we just generate an OP_REG and the register number. For a
9105 memory location we provide a Dwarf postfix expression describing how to
9106 generate the (dynamic) address of the object onto the address stack.
9108 If we don't know how to describe it, return 0. */
9110 static dw_loc_descr_ref
9111 loc_descriptor (rtx rtl)
9113 dw_loc_descr_ref loc_result = NULL;
9115 switch (GET_CODE (rtl))
9117 case SUBREG:
9118 /* The case of a subreg may arise when we have a local (register)
9119 variable or a formal (register) parameter which doesn't quite fill
9120 up an entire register. For now, just assume that it is
9121 legitimate to make the Dwarf info refer to the whole register which
9122 contains the given subreg. */
9123 rtl = SUBREG_REG (rtl);
9125 /* ... fall through ... */
9127 case REG:
9128 loc_result = reg_loc_descriptor (rtl);
9129 break;
9131 case MEM:
9132 loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl));
9133 break;
9135 case CONCAT:
9136 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1));
9137 break;
9139 case CONCATN:
9140 loc_result = concatn_loc_descriptor (rtl);
9141 break;
9143 case VAR_LOCATION:
9144 /* Single part. */
9145 if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
9147 loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0));
9148 break;
9151 rtl = XEXP (rtl, 1);
9152 /* FALLTHRU */
9154 case PARALLEL:
9156 rtvec par_elems = XVEC (rtl, 0);
9157 int num_elem = GET_NUM_ELEM (par_elems);
9158 enum machine_mode mode;
9159 int i;
9161 /* Create the first one, so we have something to add to. */
9162 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0));
9163 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
9164 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9165 for (i = 1; i < num_elem; i++)
9167 dw_loc_descr_ref temp;
9169 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0));
9170 add_loc_descr (&loc_result, temp);
9171 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
9172 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9175 break;
9177 default:
9178 gcc_unreachable ();
9181 return loc_result;
9184 /* Similar, but generate the descriptor from trees instead of rtl. This comes
9185 up particularly with variable length arrays. WANT_ADDRESS is 2 if this is
9186 a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
9187 top-level invocation, and we require the address of LOC; is 0 if we require
9188 the value of LOC. */
9190 static dw_loc_descr_ref
9191 loc_descriptor_from_tree_1 (tree loc, int want_address)
9193 dw_loc_descr_ref ret, ret1;
9194 int have_address = 0;
9195 enum dwarf_location_atom op;
9197 /* ??? Most of the time we do not take proper care for sign/zero
9198 extending the values properly. Hopefully this won't be a real
9199 problem... */
9201 switch (TREE_CODE (loc))
9203 case ERROR_MARK:
9204 return 0;
9206 case PLACEHOLDER_EXPR:
9207 /* This case involves extracting fields from an object to determine the
9208 position of other fields. We don't try to encode this here. The
9209 only user of this is Ada, which encodes the needed information using
9210 the names of types. */
9211 return 0;
9213 case CALL_EXPR:
9214 return 0;
9216 case PREINCREMENT_EXPR:
9217 case PREDECREMENT_EXPR:
9218 case POSTINCREMENT_EXPR:
9219 case POSTDECREMENT_EXPR:
9220 /* There are no opcodes for these operations. */
9221 return 0;
9223 case ADDR_EXPR:
9224 /* If we already want an address, there's nothing we can do. */
9225 if (want_address)
9226 return 0;
9228 /* Otherwise, process the argument and look for the address. */
9229 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
9231 case VAR_DECL:
9232 if (DECL_THREAD_LOCAL_P (loc))
9234 rtx rtl;
9236 /* If this is not defined, we have no way to emit the data. */
9237 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
9238 return 0;
9240 /* The way DW_OP_GNU_push_tls_address is specified, we can only
9241 look up addresses of objects in the current module. */
9242 if (DECL_EXTERNAL (loc))
9243 return 0;
9245 rtl = rtl_for_decl_location (loc);
9246 if (rtl == NULL_RTX)
9247 return 0;
9249 if (!MEM_P (rtl))
9250 return 0;
9251 rtl = XEXP (rtl, 0);
9252 if (! CONSTANT_P (rtl))
9253 return 0;
9255 ret = new_loc_descr (INTERNAL_DW_OP_tls_addr, 0, 0);
9256 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9257 ret->dw_loc_oprnd1.v.val_addr = rtl;
9259 ret1 = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
9260 add_loc_descr (&ret, ret1);
9262 have_address = 1;
9263 break;
9265 /* FALLTHRU */
9267 case PARM_DECL:
9268 if (DECL_HAS_VALUE_EXPR_P (loc))
9269 return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
9270 want_address);
9271 /* FALLTHRU */
9273 case RESULT_DECL:
9274 case FUNCTION_DECL:
9276 rtx rtl = rtl_for_decl_location (loc);
9278 if (rtl == NULL_RTX)
9279 return 0;
9280 else if (GET_CODE (rtl) == CONST_INT)
9282 HOST_WIDE_INT val = INTVAL (rtl);
9283 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
9284 val &= GET_MODE_MASK (DECL_MODE (loc));
9285 ret = int_loc_descriptor (val);
9287 else if (GET_CODE (rtl) == CONST_STRING)
9288 return 0;
9289 else if (CONSTANT_P (rtl))
9291 ret = new_loc_descr (DW_OP_addr, 0, 0);
9292 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9293 ret->dw_loc_oprnd1.v.val_addr = rtl;
9295 else
9297 enum machine_mode mode;
9299 /* Certain constructs can only be represented at top-level. */
9300 if (want_address == 2)
9301 return loc_descriptor (rtl);
9303 mode = GET_MODE (rtl);
9304 if (MEM_P (rtl))
9306 rtl = XEXP (rtl, 0);
9307 have_address = 1;
9309 ret = mem_loc_descriptor (rtl, mode);
9312 break;
9314 case INDIRECT_REF:
9315 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9316 have_address = 1;
9317 break;
9319 case COMPOUND_EXPR:
9320 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
9322 case NOP_EXPR:
9323 case CONVERT_EXPR:
9324 case NON_LVALUE_EXPR:
9325 case VIEW_CONVERT_EXPR:
9326 case SAVE_EXPR:
9327 case GIMPLE_MODIFY_STMT:
9328 return loc_descriptor_from_tree_1 (GENERIC_TREE_OPERAND (loc, 0),
9329 want_address);
9331 case COMPONENT_REF:
9332 case BIT_FIELD_REF:
9333 case ARRAY_REF:
9334 case ARRAY_RANGE_REF:
9336 tree obj, offset;
9337 HOST_WIDE_INT bitsize, bitpos, bytepos;
9338 enum machine_mode mode;
9339 int volatilep;
9340 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
9342 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
9343 &unsignedp, &volatilep, false);
9345 if (obj == loc)
9346 return 0;
9348 ret = loc_descriptor_from_tree_1 (obj, 1);
9349 if (ret == 0
9350 || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
9351 return 0;
9353 if (offset != NULL_TREE)
9355 /* Variable offset. */
9356 add_loc_descr (&ret, loc_descriptor_from_tree_1 (offset, 0));
9357 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9360 bytepos = bitpos / BITS_PER_UNIT;
9361 if (bytepos > 0)
9362 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
9363 else if (bytepos < 0)
9365 add_loc_descr (&ret, int_loc_descriptor (bytepos));
9366 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9369 have_address = 1;
9370 break;
9373 case INTEGER_CST:
9374 if (host_integerp (loc, 0))
9375 ret = int_loc_descriptor (tree_low_cst (loc, 0));
9376 else
9377 return 0;
9378 break;
9380 case CONSTRUCTOR:
9382 /* Get an RTL for this, if something has been emitted. */
9383 rtx rtl = lookup_constant_def (loc);
9384 enum machine_mode mode;
9386 if (!rtl || !MEM_P (rtl))
9387 return 0;
9388 mode = GET_MODE (rtl);
9389 rtl = XEXP (rtl, 0);
9390 ret = mem_loc_descriptor (rtl, mode);
9391 have_address = 1;
9392 break;
9395 case TRUTH_AND_EXPR:
9396 case TRUTH_ANDIF_EXPR:
9397 case BIT_AND_EXPR:
9398 op = DW_OP_and;
9399 goto do_binop;
9401 case TRUTH_XOR_EXPR:
9402 case BIT_XOR_EXPR:
9403 op = DW_OP_xor;
9404 goto do_binop;
9406 case TRUTH_OR_EXPR:
9407 case TRUTH_ORIF_EXPR:
9408 case BIT_IOR_EXPR:
9409 op = DW_OP_or;
9410 goto do_binop;
9412 case FLOOR_DIV_EXPR:
9413 case CEIL_DIV_EXPR:
9414 case ROUND_DIV_EXPR:
9415 case TRUNC_DIV_EXPR:
9416 op = DW_OP_div;
9417 goto do_binop;
9419 case MINUS_EXPR:
9420 op = DW_OP_minus;
9421 goto do_binop;
9423 case FLOOR_MOD_EXPR:
9424 case CEIL_MOD_EXPR:
9425 case ROUND_MOD_EXPR:
9426 case TRUNC_MOD_EXPR:
9427 op = DW_OP_mod;
9428 goto do_binop;
9430 case MULT_EXPR:
9431 op = DW_OP_mul;
9432 goto do_binop;
9434 case LSHIFT_EXPR:
9435 op = DW_OP_shl;
9436 goto do_binop;
9438 case RSHIFT_EXPR:
9439 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
9440 goto do_binop;
9442 case PLUS_EXPR:
9443 if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
9444 && host_integerp (TREE_OPERAND (loc, 1), 0))
9446 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9447 if (ret == 0)
9448 return 0;
9450 add_loc_descr (&ret,
9451 new_loc_descr (DW_OP_plus_uconst,
9452 tree_low_cst (TREE_OPERAND (loc, 1),
9454 0));
9455 break;
9458 op = DW_OP_plus;
9459 goto do_binop;
9461 case LE_EXPR:
9462 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9463 return 0;
9465 op = DW_OP_le;
9466 goto do_binop;
9468 case GE_EXPR:
9469 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9470 return 0;
9472 op = DW_OP_ge;
9473 goto do_binop;
9475 case LT_EXPR:
9476 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9477 return 0;
9479 op = DW_OP_lt;
9480 goto do_binop;
9482 case GT_EXPR:
9483 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9484 return 0;
9486 op = DW_OP_gt;
9487 goto do_binop;
9489 case EQ_EXPR:
9490 op = DW_OP_eq;
9491 goto do_binop;
9493 case NE_EXPR:
9494 op = DW_OP_ne;
9495 goto do_binop;
9497 do_binop:
9498 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9499 ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9500 if (ret == 0 || ret1 == 0)
9501 return 0;
9503 add_loc_descr (&ret, ret1);
9504 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9505 break;
9507 case TRUTH_NOT_EXPR:
9508 case BIT_NOT_EXPR:
9509 op = DW_OP_not;
9510 goto do_unop;
9512 case ABS_EXPR:
9513 op = DW_OP_abs;
9514 goto do_unop;
9516 case NEGATE_EXPR:
9517 op = DW_OP_neg;
9518 goto do_unop;
9520 do_unop:
9521 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9522 if (ret == 0)
9523 return 0;
9525 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9526 break;
9528 case MIN_EXPR:
9529 case MAX_EXPR:
9531 const enum tree_code code =
9532 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
9534 loc = build3 (COND_EXPR, TREE_TYPE (loc),
9535 build2 (code, integer_type_node,
9536 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
9537 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
9540 /* ... fall through ... */
9542 case COND_EXPR:
9544 dw_loc_descr_ref lhs
9545 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9546 dw_loc_descr_ref rhs
9547 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
9548 dw_loc_descr_ref bra_node, jump_node, tmp;
9550 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9551 if (ret == 0 || lhs == 0 || rhs == 0)
9552 return 0;
9554 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
9555 add_loc_descr (&ret, bra_node);
9557 add_loc_descr (&ret, rhs);
9558 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
9559 add_loc_descr (&ret, jump_node);
9561 add_loc_descr (&ret, lhs);
9562 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9563 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
9565 /* ??? Need a node to point the skip at. Use a nop. */
9566 tmp = new_loc_descr (DW_OP_nop, 0, 0);
9567 add_loc_descr (&ret, tmp);
9568 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9569 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
9571 break;
9573 case FIX_TRUNC_EXPR:
9574 return 0;
9576 default:
9577 /* Leave front-end specific codes as simply unknown. This comes
9578 up, for instance, with the C STMT_EXPR. */
9579 if ((unsigned int) TREE_CODE (loc)
9580 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
9581 return 0;
9583 #ifdef ENABLE_CHECKING
9584 /* Otherwise this is a generic code; we should just lists all of
9585 these explicitly. We forgot one. */
9586 gcc_unreachable ();
9587 #else
9588 /* In a release build, we want to degrade gracefully: better to
9589 generate incomplete debugging information than to crash. */
9590 return NULL;
9591 #endif
9594 /* Show if we can't fill the request for an address. */
9595 if (want_address && !have_address)
9596 return 0;
9598 /* If we've got an address and don't want one, dereference. */
9599 if (!want_address && have_address && ret)
9601 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
9603 if (size > DWARF2_ADDR_SIZE || size == -1)
9604 return 0;
9605 else if (size == DWARF2_ADDR_SIZE)
9606 op = DW_OP_deref;
9607 else
9608 op = DW_OP_deref_size;
9610 add_loc_descr (&ret, new_loc_descr (op, size, 0));
9613 return ret;
9616 static inline dw_loc_descr_ref
9617 loc_descriptor_from_tree (tree loc)
9619 return loc_descriptor_from_tree_1 (loc, 2);
9622 /* Given a value, round it up to the lowest multiple of `boundary'
9623 which is not less than the value itself. */
9625 static inline HOST_WIDE_INT
9626 ceiling (HOST_WIDE_INT value, unsigned int boundary)
9628 return (((value + boundary - 1) / boundary) * boundary);
9631 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
9632 pointer to the declared type for the relevant field variable, or return
9633 `integer_type_node' if the given node turns out to be an
9634 ERROR_MARK node. */
9636 static inline tree
9637 field_type (tree decl)
9639 tree type;
9641 if (TREE_CODE (decl) == ERROR_MARK)
9642 return integer_type_node;
9644 type = DECL_BIT_FIELD_TYPE (decl);
9645 if (type == NULL_TREE)
9646 type = TREE_TYPE (decl);
9648 return type;
9651 /* Given a pointer to a tree node, return the alignment in bits for
9652 it, or else return BITS_PER_WORD if the node actually turns out to
9653 be an ERROR_MARK node. */
9655 static inline unsigned
9656 simple_type_align_in_bits (tree type)
9658 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
9661 static inline unsigned
9662 simple_decl_align_in_bits (tree decl)
9664 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
9667 /* Return the result of rounding T up to ALIGN. */
9669 static inline HOST_WIDE_INT
9670 round_up_to_align (HOST_WIDE_INT t, unsigned int align)
9672 /* We must be careful if T is negative because HOST_WIDE_INT can be
9673 either "above" or "below" unsigned int as per the C promotion
9674 rules, depending on the host, thus making the signedness of the
9675 direct multiplication and division unpredictable. */
9676 unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
9678 u += align - 1;
9679 u /= align;
9680 u *= align;
9682 return (HOST_WIDE_INT) u;
9685 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
9686 lowest addressed byte of the "containing object" for the given FIELD_DECL,
9687 or return 0 if we are unable to determine what that offset is, either
9688 because the argument turns out to be a pointer to an ERROR_MARK node, or
9689 because the offset is actually variable. (We can't handle the latter case
9690 just yet). */
9692 static HOST_WIDE_INT
9693 field_byte_offset (tree decl)
9695 unsigned int type_align_in_bits;
9696 unsigned int decl_align_in_bits;
9697 unsigned HOST_WIDE_INT type_size_in_bits;
9698 HOST_WIDE_INT object_offset_in_bits;
9699 tree type;
9700 tree field_size_tree;
9701 HOST_WIDE_INT bitpos_int;
9702 HOST_WIDE_INT deepest_bitpos;
9703 unsigned HOST_WIDE_INT field_size_in_bits;
9705 if (TREE_CODE (decl) == ERROR_MARK)
9706 return 0;
9708 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
9710 type = field_type (decl);
9711 field_size_tree = DECL_SIZE (decl);
9713 /* The size could be unspecified if there was an error, or for
9714 a flexible array member. */
9715 if (! field_size_tree)
9716 field_size_tree = bitsize_zero_node;
9718 /* We cannot yet cope with fields whose positions are variable, so
9719 for now, when we see such things, we simply return 0. Someday, we may
9720 be able to handle such cases, but it will be damn difficult. */
9721 if (! host_integerp (bit_position (decl), 0))
9722 return 0;
9724 bitpos_int = int_bit_position (decl);
9726 /* If we don't know the size of the field, pretend it's a full word. */
9727 if (host_integerp (field_size_tree, 1))
9728 field_size_in_bits = tree_low_cst (field_size_tree, 1);
9729 else
9730 field_size_in_bits = BITS_PER_WORD;
9732 type_size_in_bits = simple_type_size_in_bits (type);
9733 type_align_in_bits = simple_type_align_in_bits (type);
9734 decl_align_in_bits = simple_decl_align_in_bits (decl);
9736 /* The GCC front-end doesn't make any attempt to keep track of the starting
9737 bit offset (relative to the start of the containing structure type) of the
9738 hypothetical "containing object" for a bit-field. Thus, when computing
9739 the byte offset value for the start of the "containing object" of a
9740 bit-field, we must deduce this information on our own. This can be rather
9741 tricky to do in some cases. For example, handling the following structure
9742 type definition when compiling for an i386/i486 target (which only aligns
9743 long long's to 32-bit boundaries) can be very tricky:
9745 struct S { int field1; long long field2:31; };
9747 Fortunately, there is a simple rule-of-thumb which can be used in such
9748 cases. When compiling for an i386/i486, GCC will allocate 8 bytes for the
9749 structure shown above. It decides to do this based upon one simple rule
9750 for bit-field allocation. GCC allocates each "containing object" for each
9751 bit-field at the first (i.e. lowest addressed) legitimate alignment
9752 boundary (based upon the required minimum alignment for the declared type
9753 of the field) which it can possibly use, subject to the condition that
9754 there is still enough available space remaining in the containing object
9755 (when allocated at the selected point) to fully accommodate all of the
9756 bits of the bit-field itself.
9758 This simple rule makes it obvious why GCC allocates 8 bytes for each
9759 object of the structure type shown above. When looking for a place to
9760 allocate the "containing object" for `field2', the compiler simply tries
9761 to allocate a 64-bit "containing object" at each successive 32-bit
9762 boundary (starting at zero) until it finds a place to allocate that 64-
9763 bit field such that at least 31 contiguous (and previously unallocated)
9764 bits remain within that selected 64 bit field. (As it turns out, for the
9765 example above, the compiler finds it is OK to allocate the "containing
9766 object" 64-bit field at bit-offset zero within the structure type.)
9768 Here we attempt to work backwards from the limited set of facts we're
9769 given, and we try to deduce from those facts, where GCC must have believed
9770 that the containing object started (within the structure type). The value
9771 we deduce is then used (by the callers of this routine) to generate
9772 DW_AT_location and DW_AT_bit_offset attributes for fields (both bit-fields
9773 and, in the case of DW_AT_location, regular fields as well). */
9775 /* Figure out the bit-distance from the start of the structure to the
9776 "deepest" bit of the bit-field. */
9777 deepest_bitpos = bitpos_int + field_size_in_bits;
9779 /* This is the tricky part. Use some fancy footwork to deduce where the
9780 lowest addressed bit of the containing object must be. */
9781 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9783 /* Round up to type_align by default. This works best for bitfields. */
9784 object_offset_in_bits
9785 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
9787 if (object_offset_in_bits > bitpos_int)
9789 /* Sigh, the decl must be packed. */
9790 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9792 /* Round up to decl_align instead. */
9793 object_offset_in_bits
9794 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
9797 return object_offset_in_bits / BITS_PER_UNIT;
9800 /* The following routines define various Dwarf attributes and any data
9801 associated with them. */
9803 /* Add a location description attribute value to a DIE.
9805 This emits location attributes suitable for whole variables and
9806 whole parameters. Note that the location attributes for struct fields are
9807 generated by the routine `data_member_location_attribute' below. */
9809 static inline void
9810 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
9811 dw_loc_descr_ref descr)
9813 if (descr != 0)
9814 add_AT_loc (die, attr_kind, descr);
9817 /* Attach the specialized form of location attribute used for data members of
9818 struct and union types. In the special case of a FIELD_DECL node which
9819 represents a bit-field, the "offset" part of this special location
9820 descriptor must indicate the distance in bytes from the lowest-addressed
9821 byte of the containing struct or union type to the lowest-addressed byte of
9822 the "containing object" for the bit-field. (See the `field_byte_offset'
9823 function above).
9825 For any given bit-field, the "containing object" is a hypothetical object
9826 (of some integral or enum type) within which the given bit-field lives. The
9827 type of this hypothetical "containing object" is always the same as the
9828 declared type of the individual bit-field itself (for GCC anyway... the
9829 DWARF spec doesn't actually mandate this). Note that it is the size (in
9830 bytes) of the hypothetical "containing object" which will be given in the
9831 DW_AT_byte_size attribute for this bit-field. (See the
9832 `byte_size_attribute' function below.) It is also used when calculating the
9833 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
9834 function below.) */
9836 static void
9837 add_data_member_location_attribute (dw_die_ref die, tree decl)
9839 HOST_WIDE_INT offset;
9840 dw_loc_descr_ref loc_descr = 0;
9842 if (TREE_CODE (decl) == TREE_BINFO)
9844 /* We're working on the TAG_inheritance for a base class. */
9845 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
9847 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
9848 aren't at a fixed offset from all (sub)objects of the same
9849 type. We need to extract the appropriate offset from our
9850 vtable. The following dwarf expression means
9852 BaseAddr = ObAddr + *((*ObAddr) - Offset)
9854 This is specific to the V3 ABI, of course. */
9856 dw_loc_descr_ref tmp;
9858 /* Make a copy of the object address. */
9859 tmp = new_loc_descr (DW_OP_dup, 0, 0);
9860 add_loc_descr (&loc_descr, tmp);
9862 /* Extract the vtable address. */
9863 tmp = new_loc_descr (DW_OP_deref, 0, 0);
9864 add_loc_descr (&loc_descr, tmp);
9866 /* Calculate the address of the offset. */
9867 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
9868 gcc_assert (offset < 0);
9870 tmp = int_loc_descriptor (-offset);
9871 add_loc_descr (&loc_descr, tmp);
9872 tmp = new_loc_descr (DW_OP_minus, 0, 0);
9873 add_loc_descr (&loc_descr, tmp);
9875 /* Extract the offset. */
9876 tmp = new_loc_descr (DW_OP_deref, 0, 0);
9877 add_loc_descr (&loc_descr, tmp);
9879 /* Add it to the object address. */
9880 tmp = new_loc_descr (DW_OP_plus, 0, 0);
9881 add_loc_descr (&loc_descr, tmp);
9883 else
9884 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
9886 else
9887 offset = field_byte_offset (decl);
9889 if (! loc_descr)
9891 enum dwarf_location_atom op;
9893 /* The DWARF2 standard says that we should assume that the structure
9894 address is already on the stack, so we can specify a structure field
9895 address by using DW_OP_plus_uconst. */
9897 #ifdef MIPS_DEBUGGING_INFO
9898 /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
9899 operator correctly. It works only if we leave the offset on the
9900 stack. */
9901 op = DW_OP_constu;
9902 #else
9903 op = DW_OP_plus_uconst;
9904 #endif
9906 loc_descr = new_loc_descr (op, offset, 0);
9909 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
9912 /* Writes integer values to dw_vec_const array. */
9914 static void
9915 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
9917 while (size != 0)
9919 *dest++ = val & 0xff;
9920 val >>= 8;
9921 --size;
9925 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
9927 static HOST_WIDE_INT
9928 extract_int (const unsigned char *src, unsigned int size)
9930 HOST_WIDE_INT val = 0;
9932 src += size;
9933 while (size != 0)
9935 val <<= 8;
9936 val |= *--src & 0xff;
9937 --size;
9939 return val;
9942 /* Writes floating point values to dw_vec_const array. */
9944 static void
9945 insert_float (rtx rtl, unsigned char *array)
9947 REAL_VALUE_TYPE rv;
9948 long val[4];
9949 int i;
9951 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
9952 real_to_target (val, &rv, GET_MODE (rtl));
9954 /* real_to_target puts 32-bit pieces in each long. Pack them. */
9955 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
9957 insert_int (val[i], 4, array);
9958 array += 4;
9962 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
9963 does not have a "location" either in memory or in a register. These
9964 things can arise in GNU C when a constant is passed as an actual parameter
9965 to an inlined function. They can also arise in C++ where declared
9966 constants do not necessarily get memory "homes". */
9968 static void
9969 add_const_value_attribute (dw_die_ref die, rtx rtl)
9971 switch (GET_CODE (rtl))
9973 case CONST_INT:
9975 HOST_WIDE_INT val = INTVAL (rtl);
9977 if (val < 0)
9978 add_AT_int (die, DW_AT_const_value, val);
9979 else
9980 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
9982 break;
9984 case CONST_DOUBLE:
9985 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
9986 floating-point constant. A CONST_DOUBLE is used whenever the
9987 constant requires more than one word in order to be adequately
9988 represented. We output CONST_DOUBLEs as blocks. */
9990 enum machine_mode mode = GET_MODE (rtl);
9992 if (SCALAR_FLOAT_MODE_P (mode))
9994 unsigned int length = GET_MODE_SIZE (mode);
9995 unsigned char *array = ggc_alloc (length);
9997 insert_float (rtl, array);
9998 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
10000 else
10002 /* ??? We really should be using HOST_WIDE_INT throughout. */
10003 gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
10005 add_AT_long_long (die, DW_AT_const_value,
10006 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
10009 break;
10011 case CONST_VECTOR:
10013 enum machine_mode mode = GET_MODE (rtl);
10014 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
10015 unsigned int length = CONST_VECTOR_NUNITS (rtl);
10016 unsigned char *array = ggc_alloc (length * elt_size);
10017 unsigned int i;
10018 unsigned char *p;
10020 switch (GET_MODE_CLASS (mode))
10022 case MODE_VECTOR_INT:
10023 for (i = 0, p = array; i < length; i++, p += elt_size)
10025 rtx elt = CONST_VECTOR_ELT (rtl, i);
10026 HOST_WIDE_INT lo, hi;
10028 switch (GET_CODE (elt))
10030 case CONST_INT:
10031 lo = INTVAL (elt);
10032 hi = -(lo < 0);
10033 break;
10035 case CONST_DOUBLE:
10036 lo = CONST_DOUBLE_LOW (elt);
10037 hi = CONST_DOUBLE_HIGH (elt);
10038 break;
10040 default:
10041 gcc_unreachable ();
10044 if (elt_size <= sizeof (HOST_WIDE_INT))
10045 insert_int (lo, elt_size, p);
10046 else
10048 unsigned char *p0 = p;
10049 unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
10051 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
10052 if (WORDS_BIG_ENDIAN)
10054 p0 = p1;
10055 p1 = p;
10057 insert_int (lo, sizeof (HOST_WIDE_INT), p0);
10058 insert_int (hi, sizeof (HOST_WIDE_INT), p1);
10061 break;
10063 case MODE_VECTOR_FLOAT:
10064 for (i = 0, p = array; i < length; i++, p += elt_size)
10066 rtx elt = CONST_VECTOR_ELT (rtl, i);
10067 insert_float (elt, p);
10069 break;
10071 default:
10072 gcc_unreachable ();
10075 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
10077 break;
10079 case CONST_STRING:
10080 add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
10081 break;
10083 case SYMBOL_REF:
10084 case LABEL_REF:
10085 case CONST:
10086 add_AT_addr (die, DW_AT_const_value, rtl);
10087 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
10088 break;
10090 case PLUS:
10091 /* In cases where an inlined instance of an inline function is passed
10092 the address of an `auto' variable (which is local to the caller) we
10093 can get a situation where the DECL_RTL of the artificial local
10094 variable (for the inlining) which acts as a stand-in for the
10095 corresponding formal parameter (of the inline function) will look
10096 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
10097 exactly a compile-time constant expression, but it isn't the address
10098 of the (artificial) local variable either. Rather, it represents the
10099 *value* which the artificial local variable always has during its
10100 lifetime. We currently have no way to represent such quasi-constant
10101 values in Dwarf, so for now we just punt and generate nothing. */
10102 break;
10104 default:
10105 /* No other kinds of rtx should be possible here. */
10106 gcc_unreachable ();
10111 /* Determine whether the evaluation of EXPR references any variables
10112 or functions which aren't otherwise used (and therefore may not be
10113 output). */
10114 static tree
10115 reference_to_unused (tree * tp, int * walk_subtrees,
10116 void * data ATTRIBUTE_UNUSED)
10118 if (! EXPR_P (*tp) && ! GIMPLE_STMT_P (*tp) && ! CONSTANT_CLASS_P (*tp))
10119 *walk_subtrees = 0;
10121 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
10122 && ! TREE_ASM_WRITTEN (*tp))
10123 return *tp;
10124 else if (DECL_P (*tp) && TREE_CODE (*tp) != FUNCTION_DECL)
10126 struct varpool_node *node = varpool_node (*tp);
10127 if (!node->needed)
10128 return *tp;
10131 return NULL_TREE;
10134 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
10135 for use in a later add_const_value_attribute call. */
10137 static rtx
10138 rtl_for_decl_init (tree init, tree type)
10140 rtx rtl = NULL_RTX;
10142 /* If a variable is initialized with a string constant without embedded
10143 zeros, build CONST_STRING. */
10144 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
10146 tree enttype = TREE_TYPE (type);
10147 tree domain = TYPE_DOMAIN (type);
10148 enum machine_mode mode = TYPE_MODE (enttype);
10150 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
10151 && domain
10152 && integer_zerop (TYPE_MIN_VALUE (domain))
10153 && compare_tree_int (TYPE_MAX_VALUE (domain),
10154 TREE_STRING_LENGTH (init) - 1) == 0
10155 && ((size_t) TREE_STRING_LENGTH (init)
10156 == strlen (TREE_STRING_POINTER (init)) + 1))
10157 rtl = gen_rtx_CONST_STRING (VOIDmode,
10158 ggc_strdup (TREE_STRING_POINTER (init)));
10160 /* Other aggregates, and complex values, could be represented using
10161 CONCAT: FIXME! */
10162 else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
10164 /* Vectors only work if their mode is supported by the target.
10165 FIXME: generic vectors ought to work too. */
10166 else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
10168 /* If the initializer is something that we know will expand into an
10169 immediate RTL constant, expand it now. We must be careful not to
10170 reference variables which won't be output. */
10171 else if (initializer_constant_valid_p (init, type)
10172 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
10174 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
10176 /* If expand_expr returns a MEM, it wasn't immediate. */
10177 gcc_assert (!rtl || !MEM_P (rtl));
10180 return rtl;
10183 /* Generate RTL for the variable DECL to represent its location. */
10185 static rtx
10186 rtl_for_decl_location (tree decl)
10188 rtx rtl;
10190 /* Here we have to decide where we are going to say the parameter "lives"
10191 (as far as the debugger is concerned). We only have a couple of
10192 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
10194 DECL_RTL normally indicates where the parameter lives during most of the
10195 activation of the function. If optimization is enabled however, this
10196 could be either NULL or else a pseudo-reg. Both of those cases indicate
10197 that the parameter doesn't really live anywhere (as far as the code
10198 generation parts of GCC are concerned) during most of the function's
10199 activation. That will happen (for example) if the parameter is never
10200 referenced within the function.
10202 We could just generate a location descriptor here for all non-NULL
10203 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
10204 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
10205 where DECL_RTL is NULL or is a pseudo-reg.
10207 Note however that we can only get away with using DECL_INCOMING_RTL as
10208 a backup substitute for DECL_RTL in certain limited cases. In cases
10209 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
10210 we can be sure that the parameter was passed using the same type as it is
10211 declared to have within the function, and that its DECL_INCOMING_RTL
10212 points us to a place where a value of that type is passed.
10214 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
10215 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
10216 because in these cases DECL_INCOMING_RTL points us to a value of some
10217 type which is *different* from the type of the parameter itself. Thus,
10218 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
10219 such cases, the debugger would end up (for example) trying to fetch a
10220 `float' from a place which actually contains the first part of a
10221 `double'. That would lead to really incorrect and confusing
10222 output at debug-time.
10224 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
10225 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
10226 are a couple of exceptions however. On little-endian machines we can
10227 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
10228 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
10229 an integral type that is smaller than TREE_TYPE (decl). These cases arise
10230 when (on a little-endian machine) a non-prototyped function has a
10231 parameter declared to be of type `short' or `char'. In such cases,
10232 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
10233 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
10234 passed `int' value. If the debugger then uses that address to fetch
10235 a `short' or a `char' (on a little-endian machine) the result will be
10236 the correct data, so we allow for such exceptional cases below.
10238 Note that our goal here is to describe the place where the given formal
10239 parameter lives during most of the function's activation (i.e. between the
10240 end of the prologue and the start of the epilogue). We'll do that as best
10241 as we can. Note however that if the given formal parameter is modified
10242 sometime during the execution of the function, then a stack backtrace (at
10243 debug-time) will show the function as having been called with the *new*
10244 value rather than the value which was originally passed in. This happens
10245 rarely enough that it is not a major problem, but it *is* a problem, and
10246 I'd like to fix it.
10248 A future version of dwarf2out.c may generate two additional attributes for
10249 any given DW_TAG_formal_parameter DIE which will describe the "passed
10250 type" and the "passed location" for the given formal parameter in addition
10251 to the attributes we now generate to indicate the "declared type" and the
10252 "active location" for each parameter. This additional set of attributes
10253 could be used by debuggers for stack backtraces. Separately, note that
10254 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
10255 This happens (for example) for inlined-instances of inline function formal
10256 parameters which are never referenced. This really shouldn't be
10257 happening. All PARM_DECL nodes should get valid non-NULL
10258 DECL_INCOMING_RTL values. FIXME. */
10260 /* Use DECL_RTL as the "location" unless we find something better. */
10261 rtl = DECL_RTL_IF_SET (decl);
10263 /* When generating abstract instances, ignore everything except
10264 constants, symbols living in memory, and symbols living in
10265 fixed registers. */
10266 if (! reload_completed)
10268 if (rtl
10269 && (CONSTANT_P (rtl)
10270 || (MEM_P (rtl)
10271 && CONSTANT_P (XEXP (rtl, 0)))
10272 || (REG_P (rtl)
10273 && TREE_CODE (decl) == VAR_DECL
10274 && TREE_STATIC (decl))))
10276 rtl = targetm.delegitimize_address (rtl);
10277 return rtl;
10279 rtl = NULL_RTX;
10281 else if (TREE_CODE (decl) == PARM_DECL)
10283 if (rtl == NULL_RTX || is_pseudo_reg (rtl))
10285 tree declared_type = TREE_TYPE (decl);
10286 tree passed_type = DECL_ARG_TYPE (decl);
10287 enum machine_mode dmode = TYPE_MODE (declared_type);
10288 enum machine_mode pmode = TYPE_MODE (passed_type);
10290 /* This decl represents a formal parameter which was optimized out.
10291 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
10292 all cases where (rtl == NULL_RTX) just below. */
10293 if (dmode == pmode)
10294 rtl = DECL_INCOMING_RTL (decl);
10295 else if (SCALAR_INT_MODE_P (dmode)
10296 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
10297 && DECL_INCOMING_RTL (decl))
10299 rtx inc = DECL_INCOMING_RTL (decl);
10300 if (REG_P (inc))
10301 rtl = inc;
10302 else if (MEM_P (inc))
10304 if (BYTES_BIG_ENDIAN)
10305 rtl = adjust_address_nv (inc, dmode,
10306 GET_MODE_SIZE (pmode)
10307 - GET_MODE_SIZE (dmode));
10308 else
10309 rtl = inc;
10314 /* If the parm was passed in registers, but lives on the stack, then
10315 make a big endian correction if the mode of the type of the
10316 parameter is not the same as the mode of the rtl. */
10317 /* ??? This is the same series of checks that are made in dbxout.c before
10318 we reach the big endian correction code there. It isn't clear if all
10319 of these checks are necessary here, but keeping them all is the safe
10320 thing to do. */
10321 else if (MEM_P (rtl)
10322 && XEXP (rtl, 0) != const0_rtx
10323 && ! CONSTANT_P (XEXP (rtl, 0))
10324 /* Not passed in memory. */
10325 && !MEM_P (DECL_INCOMING_RTL (decl))
10326 /* Not passed by invisible reference. */
10327 && (!REG_P (XEXP (rtl, 0))
10328 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
10329 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
10330 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
10331 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
10332 #endif
10334 /* Big endian correction check. */
10335 && BYTES_BIG_ENDIAN
10336 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
10337 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
10338 < UNITS_PER_WORD))
10340 int offset = (UNITS_PER_WORD
10341 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
10343 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10344 plus_constant (XEXP (rtl, 0), offset));
10347 else if (TREE_CODE (decl) == VAR_DECL
10348 && rtl
10349 && MEM_P (rtl)
10350 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
10351 && BYTES_BIG_ENDIAN)
10353 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
10354 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
10356 /* If a variable is declared "register" yet is smaller than
10357 a register, then if we store the variable to memory, it
10358 looks like we're storing a register-sized value, when in
10359 fact we are not. We need to adjust the offset of the
10360 storage location to reflect the actual value's bytes,
10361 else gdb will not be able to display it. */
10362 if (rsize > dsize)
10363 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10364 plus_constant (XEXP (rtl, 0), rsize-dsize));
10367 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
10368 and will have been substituted directly into all expressions that use it.
10369 C does not have such a concept, but C++ and other languages do. */
10370 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
10371 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
10373 if (rtl)
10374 rtl = targetm.delegitimize_address (rtl);
10376 /* If we don't look past the constant pool, we risk emitting a
10377 reference to a constant pool entry that isn't referenced from
10378 code, and thus is not emitted. */
10379 if (rtl)
10380 rtl = avoid_constant_pool_reference (rtl);
10382 return rtl;
10385 /* We need to figure out what section we should use as the base for the
10386 address ranges where a given location is valid.
10387 1. If this particular DECL has a section associated with it, use that.
10388 2. If this function has a section associated with it, use that.
10389 3. Otherwise, use the text section.
10390 XXX: If you split a variable across multiple sections, we won't notice. */
10392 static const char *
10393 secname_for_decl (tree decl)
10395 const char *secname;
10397 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
10399 tree sectree = DECL_SECTION_NAME (decl);
10400 secname = TREE_STRING_POINTER (sectree);
10402 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
10404 tree sectree = DECL_SECTION_NAME (current_function_decl);
10405 secname = TREE_STRING_POINTER (sectree);
10407 else if (cfun && in_cold_section_p)
10408 secname = cfun->cold_section_label;
10409 else
10410 secname = text_section_label;
10412 return secname;
10415 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
10416 data attribute for a variable or a parameter. We generate the
10417 DW_AT_const_value attribute only in those cases where the given variable
10418 or parameter does not have a true "location" either in memory or in a
10419 register. This can happen (for example) when a constant is passed as an
10420 actual argument in a call to an inline function. (It's possible that
10421 these things can crop up in other ways also.) Note that one type of
10422 constant value which can be passed into an inlined function is a constant
10423 pointer. This can happen for example if an actual argument in an inlined
10424 function call evaluates to a compile-time constant address. */
10426 static void
10427 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
10428 enum dwarf_attribute attr)
10430 rtx rtl;
10431 dw_loc_descr_ref descr;
10432 var_loc_list *loc_list;
10433 struct var_loc_node *node;
10434 if (TREE_CODE (decl) == ERROR_MARK)
10435 return;
10437 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
10438 || TREE_CODE (decl) == RESULT_DECL);
10440 /* See if we possibly have multiple locations for this variable. */
10441 loc_list = lookup_decl_loc (decl);
10443 /* If it truly has multiple locations, the first and last node will
10444 differ. */
10445 if (loc_list && loc_list->first != loc_list->last)
10447 const char *endname, *secname;
10448 dw_loc_list_ref list;
10449 rtx varloc;
10451 /* Now that we know what section we are using for a base,
10452 actually construct the list of locations.
10453 The first location information is what is passed to the
10454 function that creates the location list, and the remaining
10455 locations just get added on to that list.
10456 Note that we only know the start address for a location
10457 (IE location changes), so to build the range, we use
10458 the range [current location start, next location start].
10459 This means we have to special case the last node, and generate
10460 a range of [last location start, end of function label]. */
10462 node = loc_list->first;
10463 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10464 secname = secname_for_decl (decl);
10466 list = new_loc_list (loc_descriptor (varloc),
10467 node->label, node->next->label, secname, 1);
10468 node = node->next;
10470 for (; node->next; node = node->next)
10471 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10473 /* The variable has a location between NODE->LABEL and
10474 NODE->NEXT->LABEL. */
10475 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10476 add_loc_descr_to_loc_list (&list, loc_descriptor (varloc),
10477 node->label, node->next->label, secname);
10480 /* If the variable has a location at the last label
10481 it keeps its location until the end of function. */
10482 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10484 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
10486 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10487 if (!current_function_decl)
10488 endname = text_end_label;
10489 else
10491 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
10492 current_function_funcdef_no);
10493 endname = ggc_strdup (label_id);
10495 add_loc_descr_to_loc_list (&list, loc_descriptor (varloc),
10496 node->label, endname, secname);
10499 /* Finally, add the location list to the DIE, and we are done. */
10500 add_AT_loc_list (die, attr, list);
10501 return;
10504 /* Try to get some constant RTL for this decl, and use that as the value of
10505 the location. */
10507 rtl = rtl_for_decl_location (decl);
10508 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
10510 add_const_value_attribute (die, rtl);
10511 return;
10514 /* If we have tried to generate the location otherwise, and it
10515 didn't work out (we wouldn't be here if we did), and we have a one entry
10516 location list, try generating a location from that. */
10517 if (loc_list && loc_list->first)
10519 node = loc_list->first;
10520 descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note));
10521 if (descr)
10523 add_AT_location_description (die, attr, descr);
10524 return;
10528 /* We couldn't get any rtl, so try directly generating the location
10529 description from the tree. */
10530 descr = loc_descriptor_from_tree (decl);
10531 if (descr)
10533 add_AT_location_description (die, attr, descr);
10534 return;
10536 /* None of that worked, so it must not really have a location;
10537 try adding a constant value attribute from the DECL_INITIAL. */
10538 tree_add_const_value_attribute (die, decl);
10541 /* If we don't have a copy of this variable in memory for some reason (such
10542 as a C++ member constant that doesn't have an out-of-line definition),
10543 we should tell the debugger about the constant value. */
10545 static void
10546 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
10548 tree init = DECL_INITIAL (decl);
10549 tree type = TREE_TYPE (decl);
10550 rtx rtl;
10552 if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
10553 /* OK */;
10554 else
10555 return;
10557 rtl = rtl_for_decl_init (init, type);
10558 if (rtl)
10559 add_const_value_attribute (var_die, rtl);
10562 /* Convert the CFI instructions for the current function into a
10563 location list. This is used for DW_AT_frame_base when we targeting
10564 a dwarf2 consumer that does not support the dwarf3
10565 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
10566 expressions. */
10568 static dw_loc_list_ref
10569 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
10571 dw_fde_ref fde;
10572 dw_loc_list_ref list, *list_tail;
10573 dw_cfi_ref cfi;
10574 dw_cfa_location last_cfa, next_cfa;
10575 const char *start_label, *last_label, *section;
10577 fde = &fde_table[fde_table_in_use - 1];
10579 section = secname_for_decl (current_function_decl);
10580 list_tail = &list;
10581 list = NULL;
10583 next_cfa.reg = INVALID_REGNUM;
10584 next_cfa.offset = 0;
10585 next_cfa.indirect = 0;
10586 next_cfa.base_offset = 0;
10588 start_label = fde->dw_fde_begin;
10590 /* ??? Bald assumption that the CIE opcode list does not contain
10591 advance opcodes. */
10592 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
10593 lookup_cfa_1 (cfi, &next_cfa);
10595 last_cfa = next_cfa;
10596 last_label = start_label;
10598 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
10599 switch (cfi->dw_cfi_opc)
10601 case DW_CFA_set_loc:
10602 case DW_CFA_advance_loc1:
10603 case DW_CFA_advance_loc2:
10604 case DW_CFA_advance_loc4:
10605 if (!cfa_equal_p (&last_cfa, &next_cfa))
10607 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
10608 start_label, last_label, section,
10609 list == NULL);
10611 list_tail = &(*list_tail)->dw_loc_next;
10612 last_cfa = next_cfa;
10613 start_label = last_label;
10615 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
10616 break;
10618 case DW_CFA_advance_loc:
10619 /* The encoding is complex enough that we should never emit this. */
10620 case DW_CFA_remember_state:
10621 case DW_CFA_restore_state:
10622 /* We don't handle these two in this function. It would be possible
10623 if it were to be required. */
10624 gcc_unreachable ();
10626 default:
10627 lookup_cfa_1 (cfi, &next_cfa);
10628 break;
10631 if (!cfa_equal_p (&last_cfa, &next_cfa))
10633 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
10634 start_label, last_label, section,
10635 list == NULL);
10636 list_tail = &(*list_tail)->dw_loc_next;
10637 start_label = last_label;
10639 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
10640 start_label, fde->dw_fde_end, section,
10641 list == NULL);
10643 return list;
10646 /* Compute a displacement from the "steady-state frame pointer" to the
10647 frame base (often the same as the CFA), and store it in
10648 frame_pointer_fb_offset. OFFSET is added to the displacement
10649 before the latter is negated. */
10651 static void
10652 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
10654 rtx reg, elim;
10656 #ifdef FRAME_POINTER_CFA_OFFSET
10657 reg = frame_pointer_rtx;
10658 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
10659 #else
10660 reg = arg_pointer_rtx;
10661 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
10662 #endif
10664 elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10665 if (GET_CODE (elim) == PLUS)
10667 offset += INTVAL (XEXP (elim, 1));
10668 elim = XEXP (elim, 0);
10670 gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
10671 : stack_pointer_rtx));
10673 frame_pointer_fb_offset = -offset;
10676 /* Generate a DW_AT_name attribute given some string value to be included as
10677 the value of the attribute. */
10679 static void
10680 add_name_attribute (dw_die_ref die, const char *name_string)
10682 if (name_string != NULL && *name_string != 0)
10684 if (demangle_name_func)
10685 name_string = (*demangle_name_func) (name_string);
10687 add_AT_string (die, DW_AT_name, name_string);
10691 /* Generate a DW_AT_comp_dir attribute for DIE. */
10693 static void
10694 add_comp_dir_attribute (dw_die_ref die)
10696 const char *wd = get_src_pwd ();
10697 if (wd != NULL)
10698 add_AT_string (die, DW_AT_comp_dir, wd);
10701 /* Given a tree node describing an array bound (either lower or upper) output
10702 a representation for that bound. */
10704 static void
10705 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
10707 switch (TREE_CODE (bound))
10709 case ERROR_MARK:
10710 return;
10712 /* All fixed-bounds are represented by INTEGER_CST nodes. */
10713 case INTEGER_CST:
10714 if (! host_integerp (bound, 0)
10715 || (bound_attr == DW_AT_lower_bound
10716 && (((is_c_family () || is_java ()) && integer_zerop (bound))
10717 || (is_fortran () && integer_onep (bound)))))
10718 /* Use the default. */
10720 else
10721 add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
10722 break;
10724 case CONVERT_EXPR:
10725 case NOP_EXPR:
10726 case NON_LVALUE_EXPR:
10727 case VIEW_CONVERT_EXPR:
10728 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
10729 break;
10731 case SAVE_EXPR:
10732 break;
10734 case VAR_DECL:
10735 case PARM_DECL:
10736 case RESULT_DECL:
10738 dw_die_ref decl_die = lookup_decl_die (bound);
10740 /* ??? Can this happen, or should the variable have been bound
10741 first? Probably it can, since I imagine that we try to create
10742 the types of parameters in the order in which they exist in
10743 the list, and won't have created a forward reference to a
10744 later parameter. */
10745 if (decl_die != NULL)
10746 add_AT_die_ref (subrange_die, bound_attr, decl_die);
10747 break;
10750 default:
10752 /* Otherwise try to create a stack operation procedure to
10753 evaluate the value of the array bound. */
10755 dw_die_ref ctx, decl_die;
10756 dw_loc_descr_ref loc;
10758 loc = loc_descriptor_from_tree (bound);
10759 if (loc == NULL)
10760 break;
10762 if (current_function_decl == 0)
10763 ctx = comp_unit_die;
10764 else
10765 ctx = lookup_decl_die (current_function_decl);
10767 decl_die = new_die (DW_TAG_variable, ctx, bound);
10768 add_AT_flag (decl_die, DW_AT_artificial, 1);
10769 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
10770 add_AT_loc (decl_die, DW_AT_location, loc);
10772 add_AT_die_ref (subrange_die, bound_attr, decl_die);
10773 break;
10778 /* Note that the block of subscript information for an array type also
10779 includes information about the element type of type given array type. */
10781 static void
10782 add_subscript_info (dw_die_ref type_die, tree type)
10784 #ifndef MIPS_DEBUGGING_INFO
10785 unsigned dimension_number;
10786 #endif
10787 tree lower, upper;
10788 dw_die_ref subrange_die;
10790 /* The GNU compilers represent multidimensional array types as sequences of
10791 one dimensional array types whose element types are themselves array
10792 types. Here we squish that down, so that each multidimensional array
10793 type gets only one array_type DIE in the Dwarf debugging info. The draft
10794 Dwarf specification say that we are allowed to do this kind of
10795 compression in C (because there is no difference between an array or
10796 arrays and a multidimensional array in C) but for other source languages
10797 (e.g. Ada) we probably shouldn't do this. */
10799 /* ??? The SGI dwarf reader fails for multidimensional arrays with a
10800 const enum type. E.g. const enum machine_mode insn_operand_mode[2][10].
10801 We work around this by disabling this feature. See also
10802 gen_array_type_die. */
10803 #ifndef MIPS_DEBUGGING_INFO
10804 for (dimension_number = 0;
10805 TREE_CODE (type) == ARRAY_TYPE;
10806 type = TREE_TYPE (type), dimension_number++)
10807 #endif
10809 tree domain = TYPE_DOMAIN (type);
10811 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
10812 and (in GNU C only) variable bounds. Handle all three forms
10813 here. */
10814 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
10815 if (domain)
10817 /* We have an array type with specified bounds. */
10818 lower = TYPE_MIN_VALUE (domain);
10819 upper = TYPE_MAX_VALUE (domain);
10821 /* Define the index type. */
10822 if (TREE_TYPE (domain))
10824 /* ??? This is probably an Ada unnamed subrange type. Ignore the
10825 TREE_TYPE field. We can't emit debug info for this
10826 because it is an unnamed integral type. */
10827 if (TREE_CODE (domain) == INTEGER_TYPE
10828 && TYPE_NAME (domain) == NULL_TREE
10829 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
10830 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
10832 else
10833 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
10834 type_die);
10837 /* ??? If upper is NULL, the array has unspecified length,
10838 but it does have a lower bound. This happens with Fortran
10839 dimension arr(N:*)
10840 Since the debugger is definitely going to need to know N
10841 to produce useful results, go ahead and output the lower
10842 bound solo, and hope the debugger can cope. */
10844 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
10845 if (upper)
10846 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
10849 /* Otherwise we have an array type with an unspecified length. The
10850 DWARF-2 spec does not say how to handle this; let's just leave out the
10851 bounds. */
10855 static void
10856 add_byte_size_attribute (dw_die_ref die, tree tree_node)
10858 unsigned size;
10860 switch (TREE_CODE (tree_node))
10862 case ERROR_MARK:
10863 size = 0;
10864 break;
10865 case ENUMERAL_TYPE:
10866 case RECORD_TYPE:
10867 case UNION_TYPE:
10868 case QUAL_UNION_TYPE:
10869 size = int_size_in_bytes (tree_node);
10870 break;
10871 case FIELD_DECL:
10872 /* For a data member of a struct or union, the DW_AT_byte_size is
10873 generally given as the number of bytes normally allocated for an
10874 object of the *declared* type of the member itself. This is true
10875 even for bit-fields. */
10876 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
10877 break;
10878 default:
10879 gcc_unreachable ();
10882 /* Note that `size' might be -1 when we get to this point. If it is, that
10883 indicates that the byte size of the entity in question is variable. We
10884 have no good way of expressing this fact in Dwarf at the present time,
10885 so just let the -1 pass on through. */
10886 add_AT_unsigned (die, DW_AT_byte_size, size);
10889 /* For a FIELD_DECL node which represents a bit-field, output an attribute
10890 which specifies the distance in bits from the highest order bit of the
10891 "containing object" for the bit-field to the highest order bit of the
10892 bit-field itself.
10894 For any given bit-field, the "containing object" is a hypothetical object
10895 (of some integral or enum type) within which the given bit-field lives. The
10896 type of this hypothetical "containing object" is always the same as the
10897 declared type of the individual bit-field itself. The determination of the
10898 exact location of the "containing object" for a bit-field is rather
10899 complicated. It's handled by the `field_byte_offset' function (above).
10901 Note that it is the size (in bytes) of the hypothetical "containing object"
10902 which will be given in the DW_AT_byte_size attribute for this bit-field.
10903 (See `byte_size_attribute' above). */
10905 static inline void
10906 add_bit_offset_attribute (dw_die_ref die, tree decl)
10908 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
10909 tree type = DECL_BIT_FIELD_TYPE (decl);
10910 HOST_WIDE_INT bitpos_int;
10911 HOST_WIDE_INT highest_order_object_bit_offset;
10912 HOST_WIDE_INT highest_order_field_bit_offset;
10913 HOST_WIDE_INT unsigned bit_offset;
10915 /* Must be a field and a bit field. */
10916 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
10918 /* We can't yet handle bit-fields whose offsets are variable, so if we
10919 encounter such things, just return without generating any attribute
10920 whatsoever. Likewise for variable or too large size. */
10921 if (! host_integerp (bit_position (decl), 0)
10922 || ! host_integerp (DECL_SIZE (decl), 1))
10923 return;
10925 bitpos_int = int_bit_position (decl);
10927 /* Note that the bit offset is always the distance (in bits) from the
10928 highest-order bit of the "containing object" to the highest-order bit of
10929 the bit-field itself. Since the "high-order end" of any object or field
10930 is different on big-endian and little-endian machines, the computation
10931 below must take account of these differences. */
10932 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
10933 highest_order_field_bit_offset = bitpos_int;
10935 if (! BYTES_BIG_ENDIAN)
10937 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
10938 highest_order_object_bit_offset += simple_type_size_in_bits (type);
10941 bit_offset
10942 = (! BYTES_BIG_ENDIAN
10943 ? highest_order_object_bit_offset - highest_order_field_bit_offset
10944 : highest_order_field_bit_offset - highest_order_object_bit_offset);
10946 add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
10949 /* For a FIELD_DECL node which represents a bit field, output an attribute
10950 which specifies the length in bits of the given field. */
10952 static inline void
10953 add_bit_size_attribute (dw_die_ref die, tree decl)
10955 /* Must be a field and a bit field. */
10956 gcc_assert (TREE_CODE (decl) == FIELD_DECL
10957 && DECL_BIT_FIELD_TYPE (decl));
10959 if (host_integerp (DECL_SIZE (decl), 1))
10960 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
10963 /* If the compiled language is ANSI C, then add a 'prototyped'
10964 attribute, if arg types are given for the parameters of a function. */
10966 static inline void
10967 add_prototyped_attribute (dw_die_ref die, tree func_type)
10969 if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
10970 && TYPE_ARG_TYPES (func_type) != NULL)
10971 add_AT_flag (die, DW_AT_prototyped, 1);
10974 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
10975 by looking in either the type declaration or object declaration
10976 equate table. */
10978 static inline void
10979 add_abstract_origin_attribute (dw_die_ref die, tree origin)
10981 dw_die_ref origin_die = NULL;
10983 if (TREE_CODE (origin) != FUNCTION_DECL)
10985 /* We may have gotten separated from the block for the inlined
10986 function, if we're in an exception handler or some such; make
10987 sure that the abstract function has been written out.
10989 Doing this for nested functions is wrong, however; functions are
10990 distinct units, and our context might not even be inline. */
10991 tree fn = origin;
10993 if (TYPE_P (fn))
10994 fn = TYPE_STUB_DECL (fn);
10996 fn = decl_function_context (fn);
10997 if (fn)
10998 dwarf2out_abstract_function (fn);
11001 if (DECL_P (origin))
11002 origin_die = lookup_decl_die (origin);
11003 else if (TYPE_P (origin))
11004 origin_die = lookup_type_die (origin);
11006 /* XXX: Functions that are never lowered don't always have correct block
11007 trees (in the case of java, they simply have no block tree, in some other
11008 languages). For these functions, there is nothing we can really do to
11009 output correct debug info for inlined functions in all cases. Rather
11010 than die, we'll just produce deficient debug info now, in that we will
11011 have variables without a proper abstract origin. In the future, when all
11012 functions are lowered, we should re-add a gcc_assert (origin_die)
11013 here. */
11015 if (origin_die)
11016 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
11019 /* We do not currently support the pure_virtual attribute. */
11021 static inline void
11022 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
11024 if (DECL_VINDEX (func_decl))
11026 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
11028 if (host_integerp (DECL_VINDEX (func_decl), 0))
11029 add_AT_loc (die, DW_AT_vtable_elem_location,
11030 new_loc_descr (DW_OP_constu,
11031 tree_low_cst (DECL_VINDEX (func_decl), 0),
11032 0));
11034 /* GNU extension: Record what type this method came from originally. */
11035 if (debug_info_level > DINFO_LEVEL_TERSE)
11036 add_AT_die_ref (die, DW_AT_containing_type,
11037 lookup_type_die (DECL_CONTEXT (func_decl)));
11041 /* Add source coordinate attributes for the given decl. */
11043 static void
11044 add_src_coords_attributes (dw_die_ref die, tree decl)
11046 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11048 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
11049 add_AT_unsigned (die, DW_AT_decl_line, s.line);
11052 /* Add a DW_AT_name attribute and source coordinate attribute for the
11053 given decl, but only if it actually has a name. */
11055 static void
11056 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
11058 tree decl_name;
11060 decl_name = DECL_NAME (decl);
11061 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
11063 add_name_attribute (die, dwarf2_name (decl, 0));
11064 if (! DECL_ARTIFICIAL (decl))
11065 add_src_coords_attributes (die, decl);
11067 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
11068 && TREE_PUBLIC (decl)
11069 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
11070 && !DECL_ABSTRACT (decl)
11071 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl)))
11072 add_AT_string (die, DW_AT_MIPS_linkage_name,
11073 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
11076 #ifdef VMS_DEBUGGING_INFO
11077 /* Get the function's name, as described by its RTL. This may be different
11078 from the DECL_NAME name used in the source file. */
11079 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
11081 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
11082 XEXP (DECL_RTL (decl), 0));
11083 VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
11085 #endif
11088 /* Push a new declaration scope. */
11090 static void
11091 push_decl_scope (tree scope)
11093 VEC_safe_push (tree, gc, decl_scope_table, scope);
11096 /* Pop a declaration scope. */
11098 static inline void
11099 pop_decl_scope (void)
11101 VEC_pop (tree, decl_scope_table);
11104 /* Return the DIE for the scope that immediately contains this type.
11105 Non-named types get global scope. Named types nested in other
11106 types get their containing scope if it's open, or global scope
11107 otherwise. All other types (i.e. function-local named types) get
11108 the current active scope. */
11110 static dw_die_ref
11111 scope_die_for (tree t, dw_die_ref context_die)
11113 dw_die_ref scope_die = NULL;
11114 tree containing_scope;
11115 int i;
11117 /* Non-types always go in the current scope. */
11118 gcc_assert (TYPE_P (t));
11120 containing_scope = TYPE_CONTEXT (t);
11122 /* Use the containing namespace if it was passed in (for a declaration). */
11123 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
11125 if (context_die == lookup_decl_die (containing_scope))
11126 /* OK */;
11127 else
11128 containing_scope = NULL_TREE;
11131 /* Ignore function type "scopes" from the C frontend. They mean that
11132 a tagged type is local to a parmlist of a function declarator, but
11133 that isn't useful to DWARF. */
11134 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
11135 containing_scope = NULL_TREE;
11137 if (containing_scope == NULL_TREE)
11138 scope_die = comp_unit_die;
11139 else if (TYPE_P (containing_scope))
11141 /* For types, we can just look up the appropriate DIE. But
11142 first we check to see if we're in the middle of emitting it
11143 so we know where the new DIE should go. */
11144 for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
11145 if (VEC_index (tree, decl_scope_table, i) == containing_scope)
11146 break;
11148 if (i < 0)
11150 gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
11151 || TREE_ASM_WRITTEN (containing_scope));
11153 /* If none of the current dies are suitable, we get file scope. */
11154 scope_die = comp_unit_die;
11156 else
11157 scope_die = lookup_type_die (containing_scope);
11159 else
11160 scope_die = context_die;
11162 return scope_die;
11165 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
11167 static inline int
11168 local_scope_p (dw_die_ref context_die)
11170 for (; context_die; context_die = context_die->die_parent)
11171 if (context_die->die_tag == DW_TAG_inlined_subroutine
11172 || context_die->die_tag == DW_TAG_subprogram)
11173 return 1;
11175 return 0;
11178 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
11179 whether or not to treat a DIE in this context as a declaration. */
11181 static inline int
11182 class_or_namespace_scope_p (dw_die_ref context_die)
11184 return (context_die
11185 && (context_die->die_tag == DW_TAG_structure_type
11186 || context_die->die_tag == DW_TAG_union_type
11187 || context_die->die_tag == DW_TAG_namespace));
11190 /* Many forms of DIEs require a "type description" attribute. This
11191 routine locates the proper "type descriptor" die for the type given
11192 by 'type', and adds a DW_AT_type attribute below the given die. */
11194 static void
11195 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
11196 int decl_volatile, dw_die_ref context_die)
11198 enum tree_code code = TREE_CODE (type);
11199 dw_die_ref type_die = NULL;
11201 /* ??? If this type is an unnamed subrange type of an integral or
11202 floating-point type, use the inner type. This is because we have no
11203 support for unnamed types in base_type_die. This can happen if this is
11204 an Ada subrange type. Correct solution is emit a subrange type die. */
11205 if ((code == INTEGER_TYPE || code == REAL_TYPE)
11206 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
11207 type = TREE_TYPE (type), code = TREE_CODE (type);
11209 if (code == ERROR_MARK
11210 /* Handle a special case. For functions whose return type is void, we
11211 generate *no* type attribute. (Note that no object may have type
11212 `void', so this only applies to function return types). */
11213 || code == VOID_TYPE)
11214 return;
11216 type_die = modified_type_die (type,
11217 decl_const || TYPE_READONLY (type),
11218 decl_volatile || TYPE_VOLATILE (type),
11219 context_die);
11221 if (type_die != NULL)
11222 add_AT_die_ref (object_die, DW_AT_type, type_die);
11225 /* Given an object die, add the calling convention attribute for the
11226 function call type. */
11227 static void
11228 add_calling_convention_attribute (dw_die_ref subr_die, tree type)
11230 enum dwarf_calling_convention value = DW_CC_normal;
11232 value = targetm.dwarf_calling_convention (type);
11234 /* Only add the attribute if the backend requests it, and
11235 is not DW_CC_normal. */
11236 if (value && (value != DW_CC_normal))
11237 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
11240 /* Given a tree pointer to a struct, class, union, or enum type node, return
11241 a pointer to the (string) tag name for the given type, or zero if the type
11242 was declared without a tag. */
11244 static const char *
11245 type_tag (tree type)
11247 const char *name = 0;
11249 if (TYPE_NAME (type) != 0)
11251 tree t = 0;
11253 /* Find the IDENTIFIER_NODE for the type name. */
11254 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
11255 t = TYPE_NAME (type);
11257 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
11258 a TYPE_DECL node, regardless of whether or not a `typedef' was
11259 involved. */
11260 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11261 && ! DECL_IGNORED_P (TYPE_NAME (type)))
11263 /* We want to be extra verbose. Don't call dwarf_name if
11264 DECL_NAME isn't set. The default hook for decl_printable_name
11265 doesn't like that, and in this context it's correct to return
11266 0, instead of "<anonymous>" or the like. */
11267 if (DECL_NAME (TYPE_NAME (type)))
11268 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
11271 /* Now get the name as a string, or invent one. */
11272 if (!name && t != 0)
11273 name = IDENTIFIER_POINTER (t);
11276 return (name == 0 || *name == '\0') ? 0 : name;
11279 /* Return the type associated with a data member, make a special check
11280 for bit field types. */
11282 static inline tree
11283 member_declared_type (tree member)
11285 return (DECL_BIT_FIELD_TYPE (member)
11286 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
11289 /* Get the decl's label, as described by its RTL. This may be different
11290 from the DECL_NAME name used in the source file. */
11292 #if 0
11293 static const char *
11294 decl_start_label (tree decl)
11296 rtx x;
11297 const char *fnname;
11299 x = DECL_RTL (decl);
11300 gcc_assert (MEM_P (x));
11302 x = XEXP (x, 0);
11303 gcc_assert (GET_CODE (x) == SYMBOL_REF);
11305 fnname = XSTR (x, 0);
11306 return fnname;
11308 #endif
11310 /* These routines generate the internal representation of the DIE's for
11311 the compilation unit. Debugging information is collected by walking
11312 the declaration trees passed in from dwarf2out_decl(). */
11314 static void
11315 gen_array_type_die (tree type, dw_die_ref context_die)
11317 dw_die_ref scope_die = scope_die_for (type, context_die);
11318 dw_die_ref array_die;
11319 tree element_type;
11321 /* ??? The SGI dwarf reader fails for array of array of enum types unless
11322 the inner array type comes before the outer array type. Thus we must
11323 call gen_type_die before we call new_die. See below also. */
11324 #ifdef MIPS_DEBUGGING_INFO
11325 gen_type_die (TREE_TYPE (type), context_die);
11326 #endif
11328 array_die = new_die (DW_TAG_array_type, scope_die, type);
11329 add_name_attribute (array_die, type_tag (type));
11330 equate_type_number_to_die (type, array_die);
11332 if (TREE_CODE (type) == VECTOR_TYPE)
11334 /* The frontend feeds us a representation for the vector as a struct
11335 containing an array. Pull out the array type. */
11336 type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
11337 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
11340 #if 0
11341 /* We default the array ordering. SDB will probably do
11342 the right things even if DW_AT_ordering is not present. It's not even
11343 an issue until we start to get into multidimensional arrays anyway. If
11344 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
11345 then we'll have to put the DW_AT_ordering attribute back in. (But if
11346 and when we find out that we need to put these in, we will only do so
11347 for multidimensional arrays. */
11348 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
11349 #endif
11351 #ifdef MIPS_DEBUGGING_INFO
11352 /* The SGI compilers handle arrays of unknown bound by setting
11353 AT_declaration and not emitting any subrange DIEs. */
11354 if (! TYPE_DOMAIN (type))
11355 add_AT_flag (array_die, DW_AT_declaration, 1);
11356 else
11357 #endif
11358 add_subscript_info (array_die, type);
11360 /* Add representation of the type of the elements of this array type. */
11361 element_type = TREE_TYPE (type);
11363 /* ??? The SGI dwarf reader fails for multidimensional arrays with a
11364 const enum type. E.g. const enum machine_mode insn_operand_mode[2][10].
11365 We work around this by disabling this feature. See also
11366 add_subscript_info. */
11367 #ifndef MIPS_DEBUGGING_INFO
11368 while (TREE_CODE (element_type) == ARRAY_TYPE)
11369 element_type = TREE_TYPE (element_type);
11371 gen_type_die (element_type, context_die);
11372 #endif
11374 add_type_attribute (array_die, element_type, 0, 0, context_die);
11376 if (get_AT (array_die, DW_AT_name))
11377 add_pubtype (type, array_die);
11380 #if 0
11381 static void
11382 gen_entry_point_die (tree decl, dw_die_ref context_die)
11384 tree origin = decl_ultimate_origin (decl);
11385 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
11387 if (origin != NULL)
11388 add_abstract_origin_attribute (decl_die, origin);
11389 else
11391 add_name_and_src_coords_attributes (decl_die, decl);
11392 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
11393 0, 0, context_die);
11396 if (DECL_ABSTRACT (decl))
11397 equate_decl_number_to_die (decl, decl_die);
11398 else
11399 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
11401 #endif
11403 /* Walk through the list of incomplete types again, trying once more to
11404 emit full debugging info for them. */
11406 static void
11407 retry_incomplete_types (void)
11409 int i;
11411 for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
11412 gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
11415 /* Generate a DIE to represent an inlined instance of an enumeration type. */
11417 static void
11418 gen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
11420 dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
11422 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11423 be incomplete and such types are not marked. */
11424 add_abstract_origin_attribute (type_die, type);
11427 /* Generate a DIE to represent an inlined instance of a structure type. */
11429 static void
11430 gen_inlined_structure_type_die (tree type, dw_die_ref context_die)
11432 dw_die_ref type_die = new_die (DW_TAG_structure_type, context_die, type);
11434 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11435 be incomplete and such types are not marked. */
11436 add_abstract_origin_attribute (type_die, type);
11439 /* Generate a DIE to represent an inlined instance of a union type. */
11441 static void
11442 gen_inlined_union_type_die (tree type, dw_die_ref context_die)
11444 dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
11446 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11447 be incomplete and such types are not marked. */
11448 add_abstract_origin_attribute (type_die, type);
11451 /* Generate a DIE to represent an enumeration type. Note that these DIEs
11452 include all of the information about the enumeration values also. Each
11453 enumerated type name/value is listed as a child of the enumerated type
11454 DIE. */
11456 static dw_die_ref
11457 gen_enumeration_type_die (tree type, dw_die_ref context_die)
11459 dw_die_ref type_die = lookup_type_die (type);
11461 if (type_die == NULL)
11463 type_die = new_die (DW_TAG_enumeration_type,
11464 scope_die_for (type, context_die), type);
11465 equate_type_number_to_die (type, type_die);
11466 add_name_attribute (type_die, type_tag (type));
11468 else if (! TYPE_SIZE (type))
11469 return type_die;
11470 else
11471 remove_AT (type_die, DW_AT_declaration);
11473 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
11474 given enum type is incomplete, do not generate the DW_AT_byte_size
11475 attribute or the DW_AT_element_list attribute. */
11476 if (TYPE_SIZE (type))
11478 tree link;
11480 TREE_ASM_WRITTEN (type) = 1;
11481 add_byte_size_attribute (type_die, type);
11482 if (TYPE_STUB_DECL (type) != NULL_TREE)
11483 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
11485 /* If the first reference to this type was as the return type of an
11486 inline function, then it may not have a parent. Fix this now. */
11487 if (type_die->die_parent == NULL)
11488 add_child_die (scope_die_for (type, context_die), type_die);
11490 for (link = TYPE_VALUES (type);
11491 link != NULL; link = TREE_CHAIN (link))
11493 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
11494 tree value = TREE_VALUE (link);
11496 add_name_attribute (enum_die,
11497 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
11499 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
11500 /* DWARF2 does not provide a way of indicating whether or
11501 not enumeration constants are signed or unsigned. GDB
11502 always assumes the values are signed, so we output all
11503 values as if they were signed. That means that
11504 enumeration constants with very large unsigned values
11505 will appear to have negative values in the debugger. */
11506 add_AT_int (enum_die, DW_AT_const_value,
11507 tree_low_cst (value, tree_int_cst_sgn (value) > 0));
11510 else
11511 add_AT_flag (type_die, DW_AT_declaration, 1);
11513 if (get_AT (type_die, DW_AT_name))
11514 add_pubtype (type, type_die);
11516 return type_die;
11519 /* Generate a DIE to represent either a real live formal parameter decl or to
11520 represent just the type of some formal parameter position in some function
11521 type.
11523 Note that this routine is a bit unusual because its argument may be a
11524 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
11525 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
11526 node. If it's the former then this function is being called to output a
11527 DIE to represent a formal parameter object (or some inlining thereof). If
11528 it's the latter, then this function is only being called to output a
11529 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
11530 argument type of some subprogram type. */
11532 static dw_die_ref
11533 gen_formal_parameter_die (tree node, dw_die_ref context_die)
11535 dw_die_ref parm_die
11536 = new_die (DW_TAG_formal_parameter, context_die, node);
11537 tree origin;
11539 switch (TREE_CODE_CLASS (TREE_CODE (node)))
11541 case tcc_declaration:
11542 origin = decl_ultimate_origin (node);
11543 if (origin != NULL)
11544 add_abstract_origin_attribute (parm_die, origin);
11545 else
11547 add_name_and_src_coords_attributes (parm_die, node);
11548 add_type_attribute (parm_die, TREE_TYPE (node),
11549 TREE_READONLY (node),
11550 TREE_THIS_VOLATILE (node),
11551 context_die);
11552 if (DECL_ARTIFICIAL (node))
11553 add_AT_flag (parm_die, DW_AT_artificial, 1);
11556 equate_decl_number_to_die (node, parm_die);
11557 if (! DECL_ABSTRACT (node))
11558 add_location_or_const_value_attribute (parm_die, node, DW_AT_location);
11560 break;
11562 case tcc_type:
11563 /* We were called with some kind of a ..._TYPE node. */
11564 add_type_attribute (parm_die, node, 0, 0, context_die);
11565 break;
11567 default:
11568 gcc_unreachable ();
11571 return parm_die;
11574 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
11575 at the end of an (ANSI prototyped) formal parameters list. */
11577 static void
11578 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
11580 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
11583 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
11584 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
11585 parameters as specified in some function type specification (except for
11586 those which appear as part of a function *definition*). */
11588 static void
11589 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
11591 tree link;
11592 tree formal_type = NULL;
11593 tree first_parm_type;
11594 tree arg;
11596 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
11598 arg = DECL_ARGUMENTS (function_or_method_type);
11599 function_or_method_type = TREE_TYPE (function_or_method_type);
11601 else
11602 arg = NULL_TREE;
11604 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
11606 /* Make our first pass over the list of formal parameter types and output a
11607 DW_TAG_formal_parameter DIE for each one. */
11608 for (link = first_parm_type; link; )
11610 dw_die_ref parm_die;
11612 formal_type = TREE_VALUE (link);
11613 if (formal_type == void_type_node)
11614 break;
11616 /* Output a (nameless) DIE to represent the formal parameter itself. */
11617 parm_die = gen_formal_parameter_die (formal_type, context_die);
11618 if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
11619 && link == first_parm_type)
11620 || (arg && DECL_ARTIFICIAL (arg)))
11621 add_AT_flag (parm_die, DW_AT_artificial, 1);
11623 link = TREE_CHAIN (link);
11624 if (arg)
11625 arg = TREE_CHAIN (arg);
11628 /* If this function type has an ellipsis, add a
11629 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
11630 if (formal_type != void_type_node)
11631 gen_unspecified_parameters_die (function_or_method_type, context_die);
11633 /* Make our second (and final) pass over the list of formal parameter types
11634 and output DIEs to represent those types (as necessary). */
11635 for (link = TYPE_ARG_TYPES (function_or_method_type);
11636 link && TREE_VALUE (link);
11637 link = TREE_CHAIN (link))
11638 gen_type_die (TREE_VALUE (link), context_die);
11641 /* We want to generate the DIE for TYPE so that we can generate the
11642 die for MEMBER, which has been defined; we will need to refer back
11643 to the member declaration nested within TYPE. If we're trying to
11644 generate minimal debug info for TYPE, processing TYPE won't do the
11645 trick; we need to attach the member declaration by hand. */
11647 static void
11648 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
11650 gen_type_die (type, context_die);
11652 /* If we're trying to avoid duplicate debug info, we may not have
11653 emitted the member decl for this function. Emit it now. */
11654 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
11655 && ! lookup_decl_die (member))
11657 dw_die_ref type_die;
11658 gcc_assert (!decl_ultimate_origin (member));
11660 push_decl_scope (type);
11661 type_die = lookup_type_die (type);
11662 if (TREE_CODE (member) == FUNCTION_DECL)
11663 gen_subprogram_die (member, type_die);
11664 else if (TREE_CODE (member) == FIELD_DECL)
11666 /* Ignore the nameless fields that are used to skip bits but handle
11667 C++ anonymous unions and structs. */
11668 if (DECL_NAME (member) != NULL_TREE
11669 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
11670 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
11672 gen_type_die (member_declared_type (member), type_die);
11673 gen_field_die (member, type_die);
11676 else
11677 gen_variable_die (member, type_die);
11679 pop_decl_scope ();
11683 /* Generate the DWARF2 info for the "abstract" instance of a function which we
11684 may later generate inlined and/or out-of-line instances of. */
11686 static void
11687 dwarf2out_abstract_function (tree decl)
11689 dw_die_ref old_die;
11690 tree save_fn;
11691 struct function *save_cfun;
11692 tree context;
11693 int was_abstract = DECL_ABSTRACT (decl);
11695 /* Make sure we have the actual abstract inline, not a clone. */
11696 decl = DECL_ORIGIN (decl);
11698 old_die = lookup_decl_die (decl);
11699 if (old_die && get_AT (old_die, DW_AT_inline))
11700 /* We've already generated the abstract instance. */
11701 return;
11703 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
11704 we don't get confused by DECL_ABSTRACT. */
11705 if (debug_info_level > DINFO_LEVEL_TERSE)
11707 context = decl_class_context (decl);
11708 if (context)
11709 gen_type_die_for_member
11710 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
11713 /* Pretend we've just finished compiling this function. */
11714 save_fn = current_function_decl;
11715 save_cfun = cfun;
11716 current_function_decl = decl;
11717 cfun = DECL_STRUCT_FUNCTION (decl);
11719 set_decl_abstract_flags (decl, 1);
11720 dwarf2out_decl (decl);
11721 if (! was_abstract)
11722 set_decl_abstract_flags (decl, 0);
11724 current_function_decl = save_fn;
11725 cfun = save_cfun;
11728 /* Helper function of premark_used_types() which gets called through
11729 htab_traverse_resize().
11731 Marks the DIE of a given type in *SLOT as perennial, so it never gets
11732 marked as unused by prune_unused_types. */
11733 static int
11734 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
11736 tree type;
11737 dw_die_ref die;
11739 type = *slot;
11740 die = lookup_type_die (type);
11741 if (die != NULL)
11742 die->die_perennial_p = 1;
11743 return 1;
11746 /* Mark all members of used_types_hash as perennial. */
11747 static void
11748 premark_used_types (void)
11750 if (cfun && cfun->used_types_hash)
11751 htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
11754 /* Generate a DIE to represent a declared function (either file-scope or
11755 block-local). */
11757 static void
11758 gen_subprogram_die (tree decl, dw_die_ref context_die)
11760 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
11761 tree origin = decl_ultimate_origin (decl);
11762 dw_die_ref subr_die;
11763 tree fn_arg_types;
11764 tree outer_scope;
11765 dw_die_ref old_die = lookup_decl_die (decl);
11766 int declaration = (current_function_decl != decl
11767 || class_or_namespace_scope_p (context_die));
11769 premark_used_types ();
11771 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
11772 started to generate the abstract instance of an inline, decided to output
11773 its containing class, and proceeded to emit the declaration of the inline
11774 from the member list for the class. If so, DECLARATION takes priority;
11775 we'll get back to the abstract instance when done with the class. */
11777 /* The class-scope declaration DIE must be the primary DIE. */
11778 if (origin && declaration && class_or_namespace_scope_p (context_die))
11780 origin = NULL;
11781 gcc_assert (!old_die);
11784 /* Now that the C++ front end lazily declares artificial member fns, we
11785 might need to retrofit the declaration into its class. */
11786 if (!declaration && !origin && !old_die
11787 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
11788 && !class_or_namespace_scope_p (context_die)
11789 && debug_info_level > DINFO_LEVEL_TERSE)
11790 old_die = force_decl_die (decl);
11792 if (origin != NULL)
11794 gcc_assert (!declaration || local_scope_p (context_die));
11796 /* Fixup die_parent for the abstract instance of a nested
11797 inline function. */
11798 if (old_die && old_die->die_parent == NULL)
11799 add_child_die (context_die, old_die);
11801 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11802 add_abstract_origin_attribute (subr_die, origin);
11804 else if (old_die)
11806 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11807 struct dwarf_file_data * file_index = lookup_filename (s.file);
11809 if (!get_AT_flag (old_die, DW_AT_declaration)
11810 /* We can have a normal definition following an inline one in the
11811 case of redefinition of GNU C extern inlines.
11812 It seems reasonable to use AT_specification in this case. */
11813 && !get_AT (old_die, DW_AT_inline))
11815 /* Detect and ignore this case, where we are trying to output
11816 something we have already output. */
11817 return;
11820 /* If the definition comes from the same place as the declaration,
11821 maybe use the old DIE. We always want the DIE for this function
11822 that has the *_pc attributes to be under comp_unit_die so the
11823 debugger can find it. We also need to do this for abstract
11824 instances of inlines, since the spec requires the out-of-line copy
11825 to have the same parent. For local class methods, this doesn't
11826 apply; we just use the old DIE. */
11827 if ((old_die->die_parent == comp_unit_die || context_die == NULL)
11828 && (DECL_ARTIFICIAL (decl)
11829 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
11830 && (get_AT_unsigned (old_die, DW_AT_decl_line)
11831 == (unsigned) s.line))))
11833 subr_die = old_die;
11835 /* Clear out the declaration attribute and the formal parameters.
11836 Do not remove all children, because it is possible that this
11837 declaration die was forced using force_decl_die(). In such
11838 cases die that forced declaration die (e.g. TAG_imported_module)
11839 is one of the children that we do not want to remove. */
11840 remove_AT (subr_die, DW_AT_declaration);
11841 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
11843 else
11845 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11846 add_AT_specification (subr_die, old_die);
11847 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
11848 add_AT_file (subr_die, DW_AT_decl_file, file_index);
11849 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
11850 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
11853 else
11855 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11857 if (TREE_PUBLIC (decl))
11858 add_AT_flag (subr_die, DW_AT_external, 1);
11860 add_name_and_src_coords_attributes (subr_die, decl);
11861 if (debug_info_level > DINFO_LEVEL_TERSE)
11863 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
11864 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
11865 0, 0, context_die);
11868 add_pure_or_virtual_attribute (subr_die, decl);
11869 if (DECL_ARTIFICIAL (decl))
11870 add_AT_flag (subr_die, DW_AT_artificial, 1);
11872 if (TREE_PROTECTED (decl))
11873 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
11874 else if (TREE_PRIVATE (decl))
11875 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
11878 if (declaration)
11880 if (!old_die || !get_AT (old_die, DW_AT_inline))
11882 add_AT_flag (subr_die, DW_AT_declaration, 1);
11884 /* The first time we see a member function, it is in the context of
11885 the class to which it belongs. We make sure of this by emitting
11886 the class first. The next time is the definition, which is
11887 handled above. The two may come from the same source text.
11889 Note that force_decl_die() forces function declaration die. It is
11890 later reused to represent definition. */
11891 equate_decl_number_to_die (decl, subr_die);
11894 else if (DECL_ABSTRACT (decl))
11896 if (DECL_DECLARED_INLINE_P (decl))
11898 if (cgraph_function_possibly_inlined_p (decl))
11899 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
11900 else
11901 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
11903 else
11905 if (cgraph_function_possibly_inlined_p (decl))
11906 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
11907 else
11908 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
11911 equate_decl_number_to_die (decl, subr_die);
11913 else if (!DECL_EXTERNAL (decl))
11915 HOST_WIDE_INT cfa_fb_offset;
11917 if (!old_die || !get_AT (old_die, DW_AT_inline))
11918 equate_decl_number_to_die (decl, subr_die);
11920 if (!flag_reorder_blocks_and_partition)
11922 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
11923 current_function_funcdef_no);
11924 add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
11925 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
11926 current_function_funcdef_no);
11927 add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
11929 add_pubname (decl, subr_die);
11930 add_arange (decl, subr_die);
11932 else
11933 { /* Do nothing for now; maybe need to duplicate die, one for
11934 hot section and ond for cold section, then use the hot/cold
11935 section begin/end labels to generate the aranges... */
11937 add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
11938 add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
11939 add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
11940 add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
11942 add_pubname (decl, subr_die);
11943 add_arange (decl, subr_die);
11944 add_arange (decl, subr_die);
11948 #ifdef MIPS_DEBUGGING_INFO
11949 /* Add a reference to the FDE for this routine. */
11950 add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
11951 #endif
11953 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
11955 /* We define the "frame base" as the function's CFA. This is more
11956 convenient for several reasons: (1) It's stable across the prologue
11957 and epilogue, which makes it better than just a frame pointer,
11958 (2) With dwarf3, there exists a one-byte encoding that allows us
11959 to reference the .debug_frame data by proxy, but failing that,
11960 (3) We can at least reuse the code inspection and interpretation
11961 code that determines the CFA position at various points in the
11962 function. */
11963 /* ??? Use some command-line or configury switch to enable the use
11964 of dwarf3 DW_OP_call_frame_cfa. At present there are no dwarf
11965 consumers that understand it; fall back to "pure" dwarf2 and
11966 convert the CFA data into a location list. */
11968 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
11969 if (list->dw_loc_next)
11970 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
11971 else
11972 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
11975 /* Compute a displacement from the "steady-state frame pointer" to
11976 the CFA. The former is what all stack slots and argument slots
11977 will reference in the rtl; the later is what we've told the
11978 debugger about. We'll need to adjust all frame_base references
11979 by this displacement. */
11980 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
11982 if (cfun->static_chain_decl)
11983 add_AT_location_description (subr_die, DW_AT_static_link,
11984 loc_descriptor_from_tree (cfun->static_chain_decl));
11987 /* Now output descriptions of the arguments for this function. This gets
11988 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
11989 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
11990 `...' at the end of the formal parameter list. In order to find out if
11991 there was a trailing ellipsis or not, we must instead look at the type
11992 associated with the FUNCTION_DECL. This will be a node of type
11993 FUNCTION_TYPE. If the chain of type nodes hanging off of this
11994 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
11995 an ellipsis at the end. */
11997 /* In the case where we are describing a mere function declaration, all we
11998 need to do here (and all we *can* do here) is to describe the *types* of
11999 its formal parameters. */
12000 if (debug_info_level <= DINFO_LEVEL_TERSE)
12002 else if (declaration)
12003 gen_formal_types_die (decl, subr_die);
12004 else
12006 /* Generate DIEs to represent all known formal parameters. */
12007 tree arg_decls = DECL_ARGUMENTS (decl);
12008 tree parm;
12010 /* When generating DIEs, generate the unspecified_parameters DIE
12011 instead if we come across the arg "__builtin_va_alist" */
12012 for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
12013 if (TREE_CODE (parm) == PARM_DECL)
12015 if (DECL_NAME (parm)
12016 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
12017 "__builtin_va_alist"))
12018 gen_unspecified_parameters_die (parm, subr_die);
12019 else
12020 gen_decl_die (parm, subr_die);
12023 /* Decide whether we need an unspecified_parameters DIE at the end.
12024 There are 2 more cases to do this for: 1) the ansi ... declaration -
12025 this is detectable when the end of the arg list is not a
12026 void_type_node 2) an unprototyped function declaration (not a
12027 definition). This just means that we have no info about the
12028 parameters at all. */
12029 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
12030 if (fn_arg_types != NULL)
12032 /* This is the prototyped case, check for.... */
12033 if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
12034 gen_unspecified_parameters_die (decl, subr_die);
12036 else if (DECL_INITIAL (decl) == NULL_TREE)
12037 gen_unspecified_parameters_die (decl, subr_die);
12040 /* Output Dwarf info for all of the stuff within the body of the function
12041 (if it has one - it may be just a declaration). */
12042 outer_scope = DECL_INITIAL (decl);
12044 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
12045 a function. This BLOCK actually represents the outermost binding contour
12046 for the function, i.e. the contour in which the function's formal
12047 parameters and labels get declared. Curiously, it appears that the front
12048 end doesn't actually put the PARM_DECL nodes for the current function onto
12049 the BLOCK_VARS list for this outer scope, but are strung off of the
12050 DECL_ARGUMENTS list for the function instead.
12052 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
12053 the LABEL_DECL nodes for the function however, and we output DWARF info
12054 for those in decls_for_scope. Just within the `outer_scope' there will be
12055 a BLOCK node representing the function's outermost pair of curly braces,
12056 and any blocks used for the base and member initializers of a C++
12057 constructor function. */
12058 if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
12060 /* Emit a DW_TAG_variable DIE for a named return value. */
12061 if (DECL_NAME (DECL_RESULT (decl)))
12062 gen_decl_die (DECL_RESULT (decl), subr_die);
12064 current_function_has_inlines = 0;
12065 decls_for_scope (outer_scope, subr_die, 0);
12067 #if 0 && defined (MIPS_DEBUGGING_INFO)
12068 if (current_function_has_inlines)
12070 add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
12071 if (! comp_unit_has_inlines)
12073 add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
12074 comp_unit_has_inlines = 1;
12077 #endif
12079 /* Add the calling convention attribute if requested. */
12080 add_calling_convention_attribute (subr_die, TREE_TYPE (decl));
12084 /* Generate a DIE to represent a declared data object. */
12086 static void
12087 gen_variable_die (tree decl, dw_die_ref context_die)
12089 tree origin = decl_ultimate_origin (decl);
12090 dw_die_ref var_die = new_die (DW_TAG_variable, context_die, decl);
12092 dw_die_ref old_die = lookup_decl_die (decl);
12093 int declaration = (DECL_EXTERNAL (decl)
12094 /* If DECL is COMDAT and has not actually been
12095 emitted, we cannot take its address; there
12096 might end up being no definition anywhere in
12097 the program. For example, consider the C++
12098 test case:
12100 template <class T>
12101 struct S { static const int i = 7; };
12103 template <class T>
12104 const int S<T>::i;
12106 int f() { return S<int>::i; }
12108 Here, S<int>::i is not DECL_EXTERNAL, but no
12109 definition is required, so the compiler will
12110 not emit a definition. */
12111 || (TREE_CODE (decl) == VAR_DECL
12112 && DECL_COMDAT (decl) && !TREE_ASM_WRITTEN (decl))
12113 || class_or_namespace_scope_p (context_die));
12115 if (origin != NULL)
12116 add_abstract_origin_attribute (var_die, origin);
12118 /* Loop unrolling can create multiple blocks that refer to the same
12119 static variable, so we must test for the DW_AT_declaration flag.
12121 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
12122 copy decls and set the DECL_ABSTRACT flag on them instead of
12123 sharing them.
12125 ??? Duplicated blocks have been rewritten to use .debug_ranges.
12127 ??? The declare_in_namespace support causes us to get two DIEs for one
12128 variable, both of which are declarations. We want to avoid considering
12129 one to be a specification, so we must test that this DIE is not a
12130 declaration. */
12131 else if (old_die && TREE_STATIC (decl) && ! declaration
12132 && get_AT_flag (old_die, DW_AT_declaration) == 1)
12134 /* This is a definition of a C++ class level static. */
12135 add_AT_specification (var_die, old_die);
12136 if (DECL_NAME (decl))
12138 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12139 struct dwarf_file_data * file_index = lookup_filename (s.file);
12141 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
12142 add_AT_file (var_die, DW_AT_decl_file, file_index);
12144 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
12145 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
12148 else
12150 add_name_and_src_coords_attributes (var_die, decl);
12151 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
12152 TREE_THIS_VOLATILE (decl), context_die);
12154 if (TREE_PUBLIC (decl))
12155 add_AT_flag (var_die, DW_AT_external, 1);
12157 if (DECL_ARTIFICIAL (decl))
12158 add_AT_flag (var_die, DW_AT_artificial, 1);
12160 if (TREE_PROTECTED (decl))
12161 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
12162 else if (TREE_PRIVATE (decl))
12163 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
12166 if (declaration)
12167 add_AT_flag (var_die, DW_AT_declaration, 1);
12169 if (DECL_ABSTRACT (decl) || declaration)
12170 equate_decl_number_to_die (decl, var_die);
12172 if (! declaration && ! DECL_ABSTRACT (decl))
12174 add_location_or_const_value_attribute (var_die, decl, DW_AT_location);
12175 add_pubname (decl, var_die);
12177 else
12178 tree_add_const_value_attribute (var_die, decl);
12181 /* Generate a DIE to represent a label identifier. */
12183 static void
12184 gen_label_die (tree decl, dw_die_ref context_die)
12186 tree origin = decl_ultimate_origin (decl);
12187 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
12188 rtx insn;
12189 char label[MAX_ARTIFICIAL_LABEL_BYTES];
12191 if (origin != NULL)
12192 add_abstract_origin_attribute (lbl_die, origin);
12193 else
12194 add_name_and_src_coords_attributes (lbl_die, decl);
12196 if (DECL_ABSTRACT (decl))
12197 equate_decl_number_to_die (decl, lbl_die);
12198 else
12200 insn = DECL_RTL_IF_SET (decl);
12202 /* Deleted labels are programmer specified labels which have been
12203 eliminated because of various optimizations. We still emit them
12204 here so that it is possible to put breakpoints on them. */
12205 if (insn
12206 && (LABEL_P (insn)
12207 || ((NOTE_P (insn)
12208 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED_LABEL))))
12210 /* When optimization is enabled (via -O) some parts of the compiler
12211 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
12212 represent source-level labels which were explicitly declared by
12213 the user. This really shouldn't be happening though, so catch
12214 it if it ever does happen. */
12215 gcc_assert (!INSN_DELETED_P (insn));
12217 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
12218 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
12223 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
12224 attributes to the DIE for a block STMT, to describe where the inlined
12225 function was called from. This is similar to add_src_coords_attributes. */
12227 static inline void
12228 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
12230 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
12232 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
12233 add_AT_unsigned (die, DW_AT_call_line, s.line);
12237 /* If STMT's abstract origin is a function declaration and STMT's
12238 first subblock's abstract origin is the function's outermost block,
12239 then we're looking at the main entry point. */
12240 static bool
12241 is_inlined_entry_point (tree stmt)
12243 tree decl, block;
12245 if (!stmt || TREE_CODE (stmt) != BLOCK)
12246 return false;
12248 decl = block_ultimate_origin (stmt);
12250 if (!decl || TREE_CODE (decl) != FUNCTION_DECL)
12251 return false;
12253 block = BLOCK_SUBBLOCKS (stmt);
12255 if (block)
12257 if (TREE_CODE (block) != BLOCK)
12258 return false;
12260 block = block_ultimate_origin (block);
12263 return block == DECL_INITIAL (decl);
12266 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
12267 Add low_pc and high_pc attributes to the DIE for a block STMT. */
12269 static inline void
12270 add_high_low_attributes (tree stmt, dw_die_ref die)
12272 char label[MAX_ARTIFICIAL_LABEL_BYTES];
12274 if (BLOCK_FRAGMENT_CHAIN (stmt))
12276 tree chain;
12278 if (is_inlined_entry_point (stmt))
12280 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
12281 BLOCK_NUMBER (stmt));
12282 add_AT_lbl_id (die, DW_AT_entry_pc, label);
12285 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
12287 chain = BLOCK_FRAGMENT_CHAIN (stmt);
12290 add_ranges (chain);
12291 chain = BLOCK_FRAGMENT_CHAIN (chain);
12293 while (chain);
12294 add_ranges (NULL);
12296 else
12298 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
12299 BLOCK_NUMBER (stmt));
12300 add_AT_lbl_id (die, DW_AT_low_pc, label);
12301 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
12302 BLOCK_NUMBER (stmt));
12303 add_AT_lbl_id (die, DW_AT_high_pc, label);
12307 /* Generate a DIE for a lexical block. */
12309 static void
12310 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
12312 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
12314 if (! BLOCK_ABSTRACT (stmt))
12315 add_high_low_attributes (stmt, stmt_die);
12317 decls_for_scope (stmt, stmt_die, depth);
12320 /* Generate a DIE for an inlined subprogram. */
12322 static void
12323 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
12325 tree decl = block_ultimate_origin (stmt);
12327 /* Emit info for the abstract instance first, if we haven't yet. We
12328 must emit this even if the block is abstract, otherwise when we
12329 emit the block below (or elsewhere), we may end up trying to emit
12330 a die whose origin die hasn't been emitted, and crashing. */
12331 dwarf2out_abstract_function (decl);
12333 if (! BLOCK_ABSTRACT (stmt))
12335 dw_die_ref subr_die
12336 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
12338 add_abstract_origin_attribute (subr_die, decl);
12339 add_high_low_attributes (stmt, subr_die);
12340 add_call_src_coords_attributes (stmt, subr_die);
12342 decls_for_scope (stmt, subr_die, depth);
12343 current_function_has_inlines = 1;
12345 else
12346 /* We may get here if we're the outer block of function A that was
12347 inlined into function B that was inlined into function C. When
12348 generating debugging info for C, dwarf2out_abstract_function(B)
12349 would mark all inlined blocks as abstract, including this one.
12350 So, we wouldn't (and shouldn't) expect labels to be generated
12351 for this one. Instead, just emit debugging info for
12352 declarations within the block. This is particularly important
12353 in the case of initializers of arguments passed from B to us:
12354 if they're statement expressions containing declarations, we
12355 wouldn't generate dies for their abstract variables, and then,
12356 when generating dies for the real variables, we'd die (pun
12357 intended :-) */
12358 gen_lexical_block_die (stmt, context_die, depth);
12361 /* Generate a DIE for a field in a record, or structure. */
12363 static void
12364 gen_field_die (tree decl, dw_die_ref context_die)
12366 dw_die_ref decl_die;
12368 if (TREE_TYPE (decl) == error_mark_node)
12369 return;
12371 decl_die = new_die (DW_TAG_member, context_die, decl);
12372 add_name_and_src_coords_attributes (decl_die, decl);
12373 add_type_attribute (decl_die, member_declared_type (decl),
12374 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
12375 context_die);
12377 if (DECL_BIT_FIELD_TYPE (decl))
12379 add_byte_size_attribute (decl_die, decl);
12380 add_bit_size_attribute (decl_die, decl);
12381 add_bit_offset_attribute (decl_die, decl);
12384 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
12385 add_data_member_location_attribute (decl_die, decl);
12387 if (DECL_ARTIFICIAL (decl))
12388 add_AT_flag (decl_die, DW_AT_artificial, 1);
12390 if (TREE_PROTECTED (decl))
12391 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
12392 else if (TREE_PRIVATE (decl))
12393 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
12395 /* Equate decl number to die, so that we can look up this decl later on. */
12396 equate_decl_number_to_die (decl, decl_die);
12399 #if 0
12400 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
12401 Use modified_type_die instead.
12402 We keep this code here just in case these types of DIEs may be needed to
12403 represent certain things in other languages (e.g. Pascal) someday. */
12405 static void
12406 gen_pointer_type_die (tree type, dw_die_ref context_die)
12408 dw_die_ref ptr_die
12409 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
12411 equate_type_number_to_die (type, ptr_die);
12412 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
12413 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
12416 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
12417 Use modified_type_die instead.
12418 We keep this code here just in case these types of DIEs may be needed to
12419 represent certain things in other languages (e.g. Pascal) someday. */
12421 static void
12422 gen_reference_type_die (tree type, dw_die_ref context_die)
12424 dw_die_ref ref_die
12425 = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
12427 equate_type_number_to_die (type, ref_die);
12428 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
12429 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
12431 #endif
12433 /* Generate a DIE for a pointer to a member type. */
12435 static void
12436 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
12438 dw_die_ref ptr_die
12439 = new_die (DW_TAG_ptr_to_member_type,
12440 scope_die_for (type, context_die), type);
12442 equate_type_number_to_die (type, ptr_die);
12443 add_AT_die_ref (ptr_die, DW_AT_containing_type,
12444 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
12445 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
12448 /* Generate the DIE for the compilation unit. */
12450 static dw_die_ref
12451 gen_compile_unit_die (const char *filename)
12453 dw_die_ref die;
12454 char producer[250];
12455 const char *language_string = lang_hooks.name;
12456 int language;
12458 die = new_die (DW_TAG_compile_unit, NULL, NULL);
12460 if (filename)
12462 add_name_attribute (die, filename);
12463 /* Don't add cwd for <built-in>. */
12464 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
12465 add_comp_dir_attribute (die);
12468 sprintf (producer, "%s %s", language_string, version_string);
12470 #ifdef MIPS_DEBUGGING_INFO
12471 /* The MIPS/SGI compilers place the 'cc' command line options in the producer
12472 string. The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
12473 not appear in the producer string, the debugger reaches the conclusion
12474 that the object file is stripped and has no debugging information.
12475 To get the MIPS/SGI debugger to believe that there is debugging
12476 information in the object file, we add a -g to the producer string. */
12477 if (debug_info_level > DINFO_LEVEL_TERSE)
12478 strcat (producer, " -g");
12479 #endif
12481 add_AT_string (die, DW_AT_producer, producer);
12483 if (strcmp (language_string, "GNU C++") == 0)
12484 language = DW_LANG_C_plus_plus;
12485 else if (strcmp (language_string, "GNU Ada") == 0)
12486 language = DW_LANG_Ada95;
12487 else if (strcmp (language_string, "GNU F77") == 0)
12488 language = DW_LANG_Fortran77;
12489 else if (strcmp (language_string, "GNU F95") == 0)
12490 language = DW_LANG_Fortran95;
12491 else if (strcmp (language_string, "GNU Pascal") == 0)
12492 language = DW_LANG_Pascal83;
12493 else if (strcmp (language_string, "GNU Java") == 0)
12494 language = DW_LANG_Java;
12495 else if (strcmp (language_string, "GNU Objective-C") == 0)
12496 language = DW_LANG_ObjC;
12497 else if (strcmp (language_string, "GNU Objective-C++") == 0)
12498 language = DW_LANG_ObjC_plus_plus;
12499 else
12500 language = DW_LANG_C89;
12502 add_AT_unsigned (die, DW_AT_language, language);
12503 return die;
12506 /* Generate the DIE for a base class. */
12508 static void
12509 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
12511 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
12513 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
12514 add_data_member_location_attribute (die, binfo);
12516 if (BINFO_VIRTUAL_P (binfo))
12517 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
12519 if (access == access_public_node)
12520 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
12521 else if (access == access_protected_node)
12522 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
12525 /* Generate a DIE for a class member. */
12527 static void
12528 gen_member_die (tree type, dw_die_ref context_die)
12530 tree member;
12531 tree binfo = TYPE_BINFO (type);
12532 dw_die_ref child;
12534 /* If this is not an incomplete type, output descriptions of each of its
12535 members. Note that as we output the DIEs necessary to represent the
12536 members of this record or union type, we will also be trying to output
12537 DIEs to represent the *types* of those members. However the `type'
12538 function (above) will specifically avoid generating type DIEs for member
12539 types *within* the list of member DIEs for this (containing) type except
12540 for those types (of members) which are explicitly marked as also being
12541 members of this (containing) type themselves. The g++ front- end can
12542 force any given type to be treated as a member of some other (containing)
12543 type by setting the TYPE_CONTEXT of the given (member) type to point to
12544 the TREE node representing the appropriate (containing) type. */
12546 /* First output info about the base classes. */
12547 if (binfo)
12549 VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
12550 int i;
12551 tree base;
12553 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
12554 gen_inheritance_die (base,
12555 (accesses ? VEC_index (tree, accesses, i)
12556 : access_public_node), context_die);
12559 /* Now output info about the data members and type members. */
12560 for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
12562 /* If we thought we were generating minimal debug info for TYPE
12563 and then changed our minds, some of the member declarations
12564 may have already been defined. Don't define them again, but
12565 do put them in the right order. */
12567 child = lookup_decl_die (member);
12568 if (child)
12569 splice_child_die (context_die, child);
12570 else
12571 gen_decl_die (member, context_die);
12574 /* Now output info about the function members (if any). */
12575 for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
12577 /* Don't include clones in the member list. */
12578 if (DECL_ABSTRACT_ORIGIN (member))
12579 continue;
12581 child = lookup_decl_die (member);
12582 if (child)
12583 splice_child_die (context_die, child);
12584 else
12585 gen_decl_die (member, context_die);
12589 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
12590 is set, we pretend that the type was never defined, so we only get the
12591 member DIEs needed by later specification DIEs. */
12593 static void
12594 gen_struct_or_union_type_die (tree type, dw_die_ref context_die)
12596 dw_die_ref type_die = lookup_type_die (type);
12597 dw_die_ref scope_die = 0;
12598 int nested = 0;
12599 int complete = (TYPE_SIZE (type)
12600 && (! TYPE_STUB_DECL (type)
12601 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
12602 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
12604 if (type_die && ! complete)
12605 return;
12607 if (TYPE_CONTEXT (type) != NULL_TREE
12608 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
12609 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
12610 nested = 1;
12612 scope_die = scope_die_for (type, context_die);
12614 if (! type_die || (nested && scope_die == comp_unit_die))
12615 /* First occurrence of type or toplevel definition of nested class. */
12617 dw_die_ref old_die = type_die;
12619 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
12620 ? DW_TAG_structure_type : DW_TAG_union_type,
12621 scope_die, type);
12622 equate_type_number_to_die (type, type_die);
12623 if (old_die)
12624 add_AT_specification (type_die, old_die);
12625 else
12626 add_name_attribute (type_die, type_tag (type));
12628 else
12629 remove_AT (type_die, DW_AT_declaration);
12631 /* If this type has been completed, then give it a byte_size attribute and
12632 then give a list of members. */
12633 if (complete && !ns_decl)
12635 /* Prevent infinite recursion in cases where the type of some member of
12636 this type is expressed in terms of this type itself. */
12637 TREE_ASM_WRITTEN (type) = 1;
12638 add_byte_size_attribute (type_die, type);
12639 if (TYPE_STUB_DECL (type) != NULL_TREE)
12640 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
12642 /* If the first reference to this type was as the return type of an
12643 inline function, then it may not have a parent. Fix this now. */
12644 if (type_die->die_parent == NULL)
12645 add_child_die (scope_die, type_die);
12647 push_decl_scope (type);
12648 gen_member_die (type, type_die);
12649 pop_decl_scope ();
12651 /* GNU extension: Record what type our vtable lives in. */
12652 if (TYPE_VFIELD (type))
12654 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
12656 gen_type_die (vtype, context_die);
12657 add_AT_die_ref (type_die, DW_AT_containing_type,
12658 lookup_type_die (vtype));
12661 else
12663 add_AT_flag (type_die, DW_AT_declaration, 1);
12665 /* We don't need to do this for function-local types. */
12666 if (TYPE_STUB_DECL (type)
12667 && ! decl_function_context (TYPE_STUB_DECL (type)))
12668 VEC_safe_push (tree, gc, incomplete_types, type);
12671 if (get_AT (type_die, DW_AT_name))
12672 add_pubtype (type, type_die);
12675 /* Generate a DIE for a subroutine _type_. */
12677 static void
12678 gen_subroutine_type_die (tree type, dw_die_ref context_die)
12680 tree return_type = TREE_TYPE (type);
12681 dw_die_ref subr_die
12682 = new_die (DW_TAG_subroutine_type,
12683 scope_die_for (type, context_die), type);
12685 equate_type_number_to_die (type, subr_die);
12686 add_prototyped_attribute (subr_die, type);
12687 add_type_attribute (subr_die, return_type, 0, 0, context_die);
12688 gen_formal_types_die (type, subr_die);
12690 if (get_AT (subr_die, DW_AT_name))
12691 add_pubtype (type, subr_die);
12694 /* Generate a DIE for a type definition. */
12696 static void
12697 gen_typedef_die (tree decl, dw_die_ref context_die)
12699 dw_die_ref type_die;
12700 tree origin;
12702 if (TREE_ASM_WRITTEN (decl))
12703 return;
12705 TREE_ASM_WRITTEN (decl) = 1;
12706 type_die = new_die (DW_TAG_typedef, context_die, decl);
12707 origin = decl_ultimate_origin (decl);
12708 if (origin != NULL)
12709 add_abstract_origin_attribute (type_die, origin);
12710 else
12712 tree type;
12714 add_name_and_src_coords_attributes (type_die, decl);
12715 if (DECL_ORIGINAL_TYPE (decl))
12717 type = DECL_ORIGINAL_TYPE (decl);
12719 gcc_assert (type != TREE_TYPE (decl));
12720 equate_type_number_to_die (TREE_TYPE (decl), type_die);
12722 else
12723 type = TREE_TYPE (decl);
12725 add_type_attribute (type_die, type, TREE_READONLY (decl),
12726 TREE_THIS_VOLATILE (decl), context_die);
12729 if (DECL_ABSTRACT (decl))
12730 equate_decl_number_to_die (decl, type_die);
12732 if (get_AT (type_die, DW_AT_name))
12733 add_pubtype (decl, type_die);
12736 /* Generate a type description DIE. */
12738 static void
12739 gen_type_die (tree type, dw_die_ref context_die)
12741 int need_pop;
12743 if (type == NULL_TREE || type == error_mark_node)
12744 return;
12746 if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12747 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
12749 if (TREE_ASM_WRITTEN (type))
12750 return;
12752 /* Prevent broken recursion; we can't hand off to the same type. */
12753 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
12755 TREE_ASM_WRITTEN (type) = 1;
12756 gen_decl_die (TYPE_NAME (type), context_die);
12757 return;
12760 /* We are going to output a DIE to represent the unqualified version
12761 of this type (i.e. without any const or volatile qualifiers) so
12762 get the main variant (i.e. the unqualified version) of this type
12763 now. (Vectors are special because the debugging info is in the
12764 cloned type itself). */
12765 if (TREE_CODE (type) != VECTOR_TYPE)
12766 type = type_main_variant (type);
12768 if (TREE_ASM_WRITTEN (type))
12769 return;
12771 switch (TREE_CODE (type))
12773 case ERROR_MARK:
12774 break;
12776 case POINTER_TYPE:
12777 case REFERENCE_TYPE:
12778 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
12779 ensures that the gen_type_die recursion will terminate even if the
12780 type is recursive. Recursive types are possible in Ada. */
12781 /* ??? We could perhaps do this for all types before the switch
12782 statement. */
12783 TREE_ASM_WRITTEN (type) = 1;
12785 /* For these types, all that is required is that we output a DIE (or a
12786 set of DIEs) to represent the "basis" type. */
12787 gen_type_die (TREE_TYPE (type), context_die);
12788 break;
12790 case OFFSET_TYPE:
12791 /* This code is used for C++ pointer-to-data-member types.
12792 Output a description of the relevant class type. */
12793 gen_type_die (TYPE_OFFSET_BASETYPE (type), context_die);
12795 /* Output a description of the type of the object pointed to. */
12796 gen_type_die (TREE_TYPE (type), context_die);
12798 /* Now output a DIE to represent this pointer-to-data-member type
12799 itself. */
12800 gen_ptr_to_mbr_type_die (type, context_die);
12801 break;
12803 case FUNCTION_TYPE:
12804 /* Force out return type (in case it wasn't forced out already). */
12805 gen_type_die (TREE_TYPE (type), context_die);
12806 gen_subroutine_type_die (type, context_die);
12807 break;
12809 case METHOD_TYPE:
12810 /* Force out return type (in case it wasn't forced out already). */
12811 gen_type_die (TREE_TYPE (type), context_die);
12812 gen_subroutine_type_die (type, context_die);
12813 break;
12815 case ARRAY_TYPE:
12816 gen_array_type_die (type, context_die);
12817 break;
12819 case VECTOR_TYPE:
12820 gen_array_type_die (type, context_die);
12821 break;
12823 case ENUMERAL_TYPE:
12824 case RECORD_TYPE:
12825 case UNION_TYPE:
12826 case QUAL_UNION_TYPE:
12827 /* If this is a nested type whose containing class hasn't been written
12828 out yet, writing it out will cover this one, too. This does not apply
12829 to instantiations of member class templates; they need to be added to
12830 the containing class as they are generated. FIXME: This hurts the
12831 idea of combining type decls from multiple TUs, since we can't predict
12832 what set of template instantiations we'll get. */
12833 if (TYPE_CONTEXT (type)
12834 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
12835 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
12837 gen_type_die (TYPE_CONTEXT (type), context_die);
12839 if (TREE_ASM_WRITTEN (type))
12840 return;
12842 /* If that failed, attach ourselves to the stub. */
12843 push_decl_scope (TYPE_CONTEXT (type));
12844 context_die = lookup_type_die (TYPE_CONTEXT (type));
12845 need_pop = 1;
12847 else
12849 declare_in_namespace (type, context_die);
12850 need_pop = 0;
12853 if (TREE_CODE (type) == ENUMERAL_TYPE)
12855 /* This might have been written out by the call to
12856 declare_in_namespace. */
12857 if (!TREE_ASM_WRITTEN (type))
12858 gen_enumeration_type_die (type, context_die);
12860 else
12861 gen_struct_or_union_type_die (type, context_die);
12863 if (need_pop)
12864 pop_decl_scope ();
12866 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
12867 it up if it is ever completed. gen_*_type_die will set it for us
12868 when appropriate. */
12869 return;
12871 case VOID_TYPE:
12872 case INTEGER_TYPE:
12873 case REAL_TYPE:
12874 case COMPLEX_TYPE:
12875 case BOOLEAN_TYPE:
12876 /* No DIEs needed for fundamental types. */
12877 break;
12879 case LANG_TYPE:
12880 /* No Dwarf representation currently defined. */
12881 break;
12883 default:
12884 gcc_unreachable ();
12887 TREE_ASM_WRITTEN (type) = 1;
12890 /* Generate a DIE for a tagged type instantiation. */
12892 static void
12893 gen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
12895 if (type == NULL_TREE || type == error_mark_node)
12896 return;
12898 /* We are going to output a DIE to represent the unqualified version of
12899 this type (i.e. without any const or volatile qualifiers) so make sure
12900 that we have the main variant (i.e. the unqualified version) of this
12901 type now. */
12902 gcc_assert (type == type_main_variant (type));
12904 /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
12905 an instance of an unresolved type. */
12907 switch (TREE_CODE (type))
12909 case ERROR_MARK:
12910 break;
12912 case ENUMERAL_TYPE:
12913 gen_inlined_enumeration_type_die (type, context_die);
12914 break;
12916 case RECORD_TYPE:
12917 gen_inlined_structure_type_die (type, context_die);
12918 break;
12920 case UNION_TYPE:
12921 case QUAL_UNION_TYPE:
12922 gen_inlined_union_type_die (type, context_die);
12923 break;
12925 default:
12926 gcc_unreachable ();
12930 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
12931 things which are local to the given block. */
12933 static void
12934 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
12936 int must_output_die = 0;
12937 tree origin;
12938 tree decl;
12939 enum tree_code origin_code;
12941 /* Ignore blocks that are NULL. */
12942 if (stmt == NULL_TREE)
12943 return;
12945 /* If the block is one fragment of a non-contiguous block, do not
12946 process the variables, since they will have been done by the
12947 origin block. Do process subblocks. */
12948 if (BLOCK_FRAGMENT_ORIGIN (stmt))
12950 tree sub;
12952 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
12953 gen_block_die (sub, context_die, depth + 1);
12955 return;
12958 /* Determine the "ultimate origin" of this block. This block may be an
12959 inlined instance of an inlined instance of inline function, so we have
12960 to trace all of the way back through the origin chain to find out what
12961 sort of node actually served as the original seed for the creation of
12962 the current block. */
12963 origin = block_ultimate_origin (stmt);
12964 origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
12966 /* Determine if we need to output any Dwarf DIEs at all to represent this
12967 block. */
12968 if (origin_code == FUNCTION_DECL)
12969 /* The outer scopes for inlinings *must* always be represented. We
12970 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
12971 must_output_die = 1;
12972 else
12974 /* In the case where the current block represents an inlining of the
12975 "body block" of an inline function, we must *NOT* output any DIE for
12976 this block because we have already output a DIE to represent the whole
12977 inlined function scope and the "body block" of any function doesn't
12978 really represent a different scope according to ANSI C rules. So we
12979 check here to make sure that this block does not represent a "body
12980 block inlining" before trying to set the MUST_OUTPUT_DIE flag. */
12981 if (! is_body_block (origin ? origin : stmt))
12983 /* Determine if this block directly contains any "significant"
12984 local declarations which we will need to output DIEs for. */
12985 if (debug_info_level > DINFO_LEVEL_TERSE)
12986 /* We are not in terse mode so *any* local declaration counts
12987 as being a "significant" one. */
12988 must_output_die = (BLOCK_VARS (stmt) != NULL
12989 && (TREE_USED (stmt)
12990 || TREE_ASM_WRITTEN (stmt)
12991 || BLOCK_ABSTRACT (stmt)));
12992 else
12993 /* We are in terse mode, so only local (nested) function
12994 definitions count as "significant" local declarations. */
12995 for (decl = BLOCK_VARS (stmt);
12996 decl != NULL; decl = TREE_CHAIN (decl))
12997 if (TREE_CODE (decl) == FUNCTION_DECL
12998 && DECL_INITIAL (decl))
13000 must_output_die = 1;
13001 break;
13006 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
13007 DIE for any block which contains no significant local declarations at
13008 all. Rather, in such cases we just call `decls_for_scope' so that any
13009 needed Dwarf info for any sub-blocks will get properly generated. Note
13010 that in terse mode, our definition of what constitutes a "significant"
13011 local declaration gets restricted to include only inlined function
13012 instances and local (nested) function definitions. */
13013 if (must_output_die)
13015 if (origin_code == FUNCTION_DECL)
13016 gen_inlined_subroutine_die (stmt, context_die, depth);
13017 else
13018 gen_lexical_block_die (stmt, context_die, depth);
13020 else
13021 decls_for_scope (stmt, context_die, depth);
13024 /* Generate all of the decls declared within a given scope and (recursively)
13025 all of its sub-blocks. */
13027 static void
13028 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
13030 tree decl;
13031 tree subblocks;
13033 /* Ignore NULL blocks. */
13034 if (stmt == NULL_TREE)
13035 return;
13037 if (TREE_USED (stmt))
13039 /* Output the DIEs to represent all of the data objects and typedefs
13040 declared directly within this block but not within any nested
13041 sub-blocks. Also, nested function and tag DIEs have been
13042 generated with a parent of NULL; fix that up now. */
13043 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
13045 dw_die_ref die;
13047 if (TREE_CODE (decl) == FUNCTION_DECL)
13048 die = lookup_decl_die (decl);
13049 else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
13050 die = lookup_type_die (TREE_TYPE (decl));
13051 else
13052 die = NULL;
13054 if (die != NULL && die->die_parent == NULL)
13055 add_child_die (context_die, die);
13056 /* Do not produce debug information for static variables since
13057 these might be optimized out. We are called for these later
13058 in varpool_analyze_pending_decls. */
13059 if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
13061 else
13062 gen_decl_die (decl, context_die);
13066 /* If we're at -g1, we're not interested in subblocks. */
13067 if (debug_info_level <= DINFO_LEVEL_TERSE)
13068 return;
13070 /* Output the DIEs to represent all sub-blocks (and the items declared
13071 therein) of this block. */
13072 for (subblocks = BLOCK_SUBBLOCKS (stmt);
13073 subblocks != NULL;
13074 subblocks = BLOCK_CHAIN (subblocks))
13075 gen_block_die (subblocks, context_die, depth + 1);
13078 /* Is this a typedef we can avoid emitting? */
13080 static inline int
13081 is_redundant_typedef (tree decl)
13083 if (TYPE_DECL_IS_STUB (decl))
13084 return 1;
13086 if (DECL_ARTIFICIAL (decl)
13087 && DECL_CONTEXT (decl)
13088 && is_tagged_type (DECL_CONTEXT (decl))
13089 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
13090 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
13091 /* Also ignore the artificial member typedef for the class name. */
13092 return 1;
13094 return 0;
13097 /* Returns the DIE for decl. A DIE will always be returned. */
13099 static dw_die_ref
13100 force_decl_die (tree decl)
13102 dw_die_ref decl_die;
13103 unsigned saved_external_flag;
13104 tree save_fn = NULL_TREE;
13105 decl_die = lookup_decl_die (decl);
13106 if (!decl_die)
13108 dw_die_ref context_die;
13109 tree decl_context = DECL_CONTEXT (decl);
13110 if (decl_context)
13112 /* Find die that represents this context. */
13113 if (TYPE_P (decl_context))
13114 context_die = force_type_die (decl_context);
13115 else
13116 context_die = force_decl_die (decl_context);
13118 else
13119 context_die = comp_unit_die;
13121 decl_die = lookup_decl_die (decl);
13122 if (decl_die)
13123 return decl_die;
13125 switch (TREE_CODE (decl))
13127 case FUNCTION_DECL:
13128 /* Clear current_function_decl, so that gen_subprogram_die thinks
13129 that this is a declaration. At this point, we just want to force
13130 declaration die. */
13131 save_fn = current_function_decl;
13132 current_function_decl = NULL_TREE;
13133 gen_subprogram_die (decl, context_die);
13134 current_function_decl = save_fn;
13135 break;
13137 case VAR_DECL:
13138 /* Set external flag to force declaration die. Restore it after
13139 gen_decl_die() call. */
13140 saved_external_flag = DECL_EXTERNAL (decl);
13141 DECL_EXTERNAL (decl) = 1;
13142 gen_decl_die (decl, context_die);
13143 DECL_EXTERNAL (decl) = saved_external_flag;
13144 break;
13146 case NAMESPACE_DECL:
13147 dwarf2out_decl (decl);
13148 break;
13150 default:
13151 gcc_unreachable ();
13154 /* We should be able to find the DIE now. */
13155 if (!decl_die)
13156 decl_die = lookup_decl_die (decl);
13157 gcc_assert (decl_die);
13160 return decl_die;
13163 /* Returns the DIE for TYPE. A DIE is always returned. */
13165 static dw_die_ref
13166 force_type_die (tree type)
13168 dw_die_ref type_die;
13170 type_die = lookup_type_die (type);
13171 if (!type_die)
13173 dw_die_ref context_die;
13174 if (TYPE_CONTEXT (type))
13176 if (TYPE_P (TYPE_CONTEXT (type)))
13177 context_die = force_type_die (TYPE_CONTEXT (type));
13178 else
13179 context_die = force_decl_die (TYPE_CONTEXT (type));
13181 else
13182 context_die = comp_unit_die;
13184 type_die = lookup_type_die (type);
13185 if (type_die)
13186 return type_die;
13187 gen_type_die (type, context_die);
13188 type_die = lookup_type_die (type);
13189 gcc_assert (type_die);
13191 return type_die;
13194 /* Force out any required namespaces to be able to output DECL,
13195 and return the new context_die for it, if it's changed. */
13197 static dw_die_ref
13198 setup_namespace_context (tree thing, dw_die_ref context_die)
13200 tree context = (DECL_P (thing)
13201 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
13202 if (context && TREE_CODE (context) == NAMESPACE_DECL)
13203 /* Force out the namespace. */
13204 context_die = force_decl_die (context);
13206 return context_die;
13209 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
13210 type) within its namespace, if appropriate.
13212 For compatibility with older debuggers, namespace DIEs only contain
13213 declarations; all definitions are emitted at CU scope. */
13215 static void
13216 declare_in_namespace (tree thing, dw_die_ref context_die)
13218 dw_die_ref ns_context;
13220 if (debug_info_level <= DINFO_LEVEL_TERSE)
13221 return;
13223 /* If this decl is from an inlined function, then don't try to emit it in its
13224 namespace, as we will get confused. It would have already been emitted
13225 when the abstract instance of the inline function was emitted anyways. */
13226 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
13227 return;
13229 ns_context = setup_namespace_context (thing, context_die);
13231 if (ns_context != context_die)
13233 if (DECL_P (thing))
13234 gen_decl_die (thing, ns_context);
13235 else
13236 gen_type_die (thing, ns_context);
13240 /* Generate a DIE for a namespace or namespace alias. */
13242 static void
13243 gen_namespace_die (tree decl)
13245 dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
13247 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
13248 they are an alias of. */
13249 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
13251 /* Output a real namespace. */
13252 dw_die_ref namespace_die
13253 = new_die (DW_TAG_namespace, context_die, decl);
13254 add_name_and_src_coords_attributes (namespace_die, decl);
13255 equate_decl_number_to_die (decl, namespace_die);
13257 else
13259 /* Output a namespace alias. */
13261 /* Force out the namespace we are an alias of, if necessary. */
13262 dw_die_ref origin_die
13263 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
13265 /* Now create the namespace alias DIE. */
13266 dw_die_ref namespace_die
13267 = new_die (DW_TAG_imported_declaration, context_die, decl);
13268 add_name_and_src_coords_attributes (namespace_die, decl);
13269 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
13270 equate_decl_number_to_die (decl, namespace_die);
13274 /* Generate Dwarf debug information for a decl described by DECL. */
13276 static void
13277 gen_decl_die (tree decl, dw_die_ref context_die)
13279 tree origin;
13281 if (DECL_P (decl) && DECL_IGNORED_P (decl))
13282 return;
13284 switch (TREE_CODE (decl))
13286 case ERROR_MARK:
13287 break;
13289 case CONST_DECL:
13290 /* The individual enumerators of an enum type get output when we output
13291 the Dwarf representation of the relevant enum type itself. */
13292 break;
13294 case FUNCTION_DECL:
13295 /* Don't output any DIEs to represent mere function declarations,
13296 unless they are class members or explicit block externs. */
13297 if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
13298 && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
13299 break;
13301 #if 0
13302 /* FIXME */
13303 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
13304 on local redeclarations of global functions. That seems broken. */
13305 if (current_function_decl != decl)
13306 /* This is only a declaration. */;
13307 #endif
13309 /* If we're emitting a clone, emit info for the abstract instance. */
13310 if (DECL_ORIGIN (decl) != decl)
13311 dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
13313 /* If we're emitting an out-of-line copy of an inline function,
13314 emit info for the abstract instance and set up to refer to it. */
13315 else if (cgraph_function_possibly_inlined_p (decl)
13316 && ! DECL_ABSTRACT (decl)
13317 && ! class_or_namespace_scope_p (context_die)
13318 /* dwarf2out_abstract_function won't emit a die if this is just
13319 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
13320 that case, because that works only if we have a die. */
13321 && DECL_INITIAL (decl) != NULL_TREE)
13323 dwarf2out_abstract_function (decl);
13324 set_decl_origin_self (decl);
13327 /* Otherwise we're emitting the primary DIE for this decl. */
13328 else if (debug_info_level > DINFO_LEVEL_TERSE)
13330 /* Before we describe the FUNCTION_DECL itself, make sure that we
13331 have described its return type. */
13332 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
13334 /* And its virtual context. */
13335 if (DECL_VINDEX (decl) != NULL_TREE)
13336 gen_type_die (DECL_CONTEXT (decl), context_die);
13338 /* And its containing type. */
13339 origin = decl_class_context (decl);
13340 if (origin != NULL_TREE)
13341 gen_type_die_for_member (origin, decl, context_die);
13343 /* And its containing namespace. */
13344 declare_in_namespace (decl, context_die);
13347 /* Now output a DIE to represent the function itself. */
13348 gen_subprogram_die (decl, context_die);
13349 break;
13351 case TYPE_DECL:
13352 /* If we are in terse mode, don't generate any DIEs to represent any
13353 actual typedefs. */
13354 if (debug_info_level <= DINFO_LEVEL_TERSE)
13355 break;
13357 /* In the special case of a TYPE_DECL node representing the declaration
13358 of some type tag, if the given TYPE_DECL is marked as having been
13359 instantiated from some other (original) TYPE_DECL node (e.g. one which
13360 was generated within the original definition of an inline function) we
13361 have to generate a special (abbreviated) DW_TAG_structure_type,
13362 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. */
13363 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
13365 gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
13366 break;
13369 if (is_redundant_typedef (decl))
13370 gen_type_die (TREE_TYPE (decl), context_die);
13371 else
13372 /* Output a DIE to represent the typedef itself. */
13373 gen_typedef_die (decl, context_die);
13374 break;
13376 case LABEL_DECL:
13377 if (debug_info_level >= DINFO_LEVEL_NORMAL)
13378 gen_label_die (decl, context_die);
13379 break;
13381 case VAR_DECL:
13382 case RESULT_DECL:
13383 /* If we are in terse mode, don't generate any DIEs to represent any
13384 variable declarations or definitions. */
13385 if (debug_info_level <= DINFO_LEVEL_TERSE)
13386 break;
13388 /* Output any DIEs that are needed to specify the type of this data
13389 object. */
13390 gen_type_die (TREE_TYPE (decl), context_die);
13392 /* And its containing type. */
13393 origin = decl_class_context (decl);
13394 if (origin != NULL_TREE)
13395 gen_type_die_for_member (origin, decl, context_die);
13397 /* And its containing namespace. */
13398 declare_in_namespace (decl, context_die);
13400 /* Now output the DIE to represent the data object itself. This gets
13401 complicated because of the possibility that the VAR_DECL really
13402 represents an inlined instance of a formal parameter for an inline
13403 function. */
13404 origin = decl_ultimate_origin (decl);
13405 if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
13406 gen_formal_parameter_die (decl, context_die);
13407 else
13408 gen_variable_die (decl, context_die);
13409 break;
13411 case FIELD_DECL:
13412 /* Ignore the nameless fields that are used to skip bits but handle C++
13413 anonymous unions and structs. */
13414 if (DECL_NAME (decl) != NULL_TREE
13415 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
13416 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
13418 gen_type_die (member_declared_type (decl), context_die);
13419 gen_field_die (decl, context_die);
13421 break;
13423 case PARM_DECL:
13424 gen_type_die (TREE_TYPE (decl), context_die);
13425 gen_formal_parameter_die (decl, context_die);
13426 break;
13428 case NAMESPACE_DECL:
13429 gen_namespace_die (decl);
13430 break;
13432 default:
13433 /* Probably some frontend-internal decl. Assume we don't care. */
13434 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
13435 break;
13439 /* Output debug information for global decl DECL. Called from toplev.c after
13440 compilation proper has finished. */
13442 static void
13443 dwarf2out_global_decl (tree decl)
13445 /* Output DWARF2 information for file-scope tentative data object
13446 declarations, file-scope (extern) function declarations (which had no
13447 corresponding body) and file-scope tagged type declarations and
13448 definitions which have not yet been forced out. */
13449 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
13450 dwarf2out_decl (decl);
13453 /* Output debug information for type decl DECL. Called from toplev.c
13454 and from language front ends (to record built-in types). */
13455 static void
13456 dwarf2out_type_decl (tree decl, int local)
13458 if (!local)
13459 dwarf2out_decl (decl);
13462 /* Output debug information for imported module or decl. */
13464 static void
13465 dwarf2out_imported_module_or_decl (tree decl, tree context)
13467 dw_die_ref imported_die, at_import_die;
13468 dw_die_ref scope_die;
13469 expanded_location xloc;
13471 if (debug_info_level <= DINFO_LEVEL_TERSE)
13472 return;
13474 gcc_assert (decl);
13476 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
13477 We need decl DIE for reference and scope die. First, get DIE for the decl
13478 itself. */
13480 /* Get the scope die for decl context. Use comp_unit_die for global module
13481 or decl. If die is not found for non globals, force new die. */
13482 if (!context)
13483 scope_die = comp_unit_die;
13484 else if (TYPE_P (context))
13485 scope_die = force_type_die (context);
13486 else
13487 scope_die = force_decl_die (context);
13489 /* For TYPE_DECL or CONST_DECL, lookup TREE_TYPE. */
13490 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
13491 at_import_die = force_type_die (TREE_TYPE (decl));
13492 else
13494 at_import_die = lookup_decl_die (decl);
13495 if (!at_import_die)
13497 /* If we're trying to avoid duplicate debug info, we may not have
13498 emitted the member decl for this field. Emit it now. */
13499 if (TREE_CODE (decl) == FIELD_DECL)
13501 tree type = DECL_CONTEXT (decl);
13502 dw_die_ref type_context_die;
13504 if (TYPE_CONTEXT (type))
13505 if (TYPE_P (TYPE_CONTEXT (type)))
13506 type_context_die = force_type_die (TYPE_CONTEXT (type));
13507 else
13508 type_context_die = force_decl_die (TYPE_CONTEXT (type));
13509 else
13510 type_context_die = comp_unit_die;
13511 gen_type_die_for_member (type, decl, type_context_die);
13513 at_import_die = force_decl_die (decl);
13517 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
13518 if (TREE_CODE (decl) == NAMESPACE_DECL)
13519 imported_die = new_die (DW_TAG_imported_module, scope_die, context);
13520 else
13521 imported_die = new_die (DW_TAG_imported_declaration, scope_die, context);
13523 xloc = expand_location (input_location);
13524 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
13525 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
13526 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
13529 /* Write the debugging output for DECL. */
13531 void
13532 dwarf2out_decl (tree decl)
13534 dw_die_ref context_die = comp_unit_die;
13536 switch (TREE_CODE (decl))
13538 case ERROR_MARK:
13539 return;
13541 case FUNCTION_DECL:
13542 /* What we would really like to do here is to filter out all mere
13543 file-scope declarations of file-scope functions which are never
13544 referenced later within this translation unit (and keep all of ones
13545 that *are* referenced later on) but we aren't clairvoyant, so we have
13546 no idea which functions will be referenced in the future (i.e. later
13547 on within the current translation unit). So here we just ignore all
13548 file-scope function declarations which are not also definitions. If
13549 and when the debugger needs to know something about these functions,
13550 it will have to hunt around and find the DWARF information associated
13551 with the definition of the function.
13553 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
13554 nodes represent definitions and which ones represent mere
13555 declarations. We have to check DECL_INITIAL instead. That's because
13556 the C front-end supports some weird semantics for "extern inline"
13557 function definitions. These can get inlined within the current
13558 translation unit (and thus, we need to generate Dwarf info for their
13559 abstract instances so that the Dwarf info for the concrete inlined
13560 instances can have something to refer to) but the compiler never
13561 generates any out-of-lines instances of such things (despite the fact
13562 that they *are* definitions).
13564 The important point is that the C front-end marks these "extern
13565 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
13566 them anyway. Note that the C++ front-end also plays some similar games
13567 for inline function definitions appearing within include files which
13568 also contain `#pragma interface' pragmas. */
13569 if (DECL_INITIAL (decl) == NULL_TREE)
13570 return;
13572 /* If we're a nested function, initially use a parent of NULL; if we're
13573 a plain function, this will be fixed up in decls_for_scope. If
13574 we're a method, it will be ignored, since we already have a DIE. */
13575 if (decl_function_context (decl)
13576 /* But if we're in terse mode, we don't care about scope. */
13577 && debug_info_level > DINFO_LEVEL_TERSE)
13578 context_die = NULL;
13579 break;
13581 case VAR_DECL:
13582 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
13583 declaration and if the declaration was never even referenced from
13584 within this entire compilation unit. We suppress these DIEs in
13585 order to save space in the .debug section (by eliminating entries
13586 which are probably useless). Note that we must not suppress
13587 block-local extern declarations (whether used or not) because that
13588 would screw-up the debugger's name lookup mechanism and cause it to
13589 miss things which really ought to be in scope at a given point. */
13590 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
13591 return;
13593 /* For local statics lookup proper context die. */
13594 if (TREE_STATIC (decl) && decl_function_context (decl))
13595 context_die = lookup_decl_die (DECL_CONTEXT (decl));
13597 /* If we are in terse mode, don't generate any DIEs to represent any
13598 variable declarations or definitions. */
13599 if (debug_info_level <= DINFO_LEVEL_TERSE)
13600 return;
13601 break;
13603 case NAMESPACE_DECL:
13604 if (debug_info_level <= DINFO_LEVEL_TERSE)
13605 return;
13606 if (lookup_decl_die (decl) != NULL)
13607 return;
13608 break;
13610 case TYPE_DECL:
13611 /* Don't emit stubs for types unless they are needed by other DIEs. */
13612 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
13613 return;
13615 /* Don't bother trying to generate any DIEs to represent any of the
13616 normal built-in types for the language we are compiling. */
13617 if (DECL_IS_BUILTIN (decl))
13619 /* OK, we need to generate one for `bool' so GDB knows what type
13620 comparisons have. */
13621 if (is_cxx ()
13622 && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
13623 && ! DECL_IGNORED_P (decl))
13624 modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
13626 return;
13629 /* If we are in terse mode, don't generate any DIEs for types. */
13630 if (debug_info_level <= DINFO_LEVEL_TERSE)
13631 return;
13633 /* If we're a function-scope tag, initially use a parent of NULL;
13634 this will be fixed up in decls_for_scope. */
13635 if (decl_function_context (decl))
13636 context_die = NULL;
13638 break;
13640 default:
13641 return;
13644 gen_decl_die (decl, context_die);
13647 /* Output a marker (i.e. a label) for the beginning of the generated code for
13648 a lexical block. */
13650 static void
13651 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
13652 unsigned int blocknum)
13654 switch_to_section (current_function_section ());
13655 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
13658 /* Output a marker (i.e. a label) for the end of the generated code for a
13659 lexical block. */
13661 static void
13662 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
13664 switch_to_section (current_function_section ());
13665 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
13668 /* Returns nonzero if it is appropriate not to emit any debugging
13669 information for BLOCK, because it doesn't contain any instructions.
13671 Don't allow this for blocks with nested functions or local classes
13672 as we would end up with orphans, and in the presence of scheduling
13673 we may end up calling them anyway. */
13675 static bool
13676 dwarf2out_ignore_block (tree block)
13678 tree decl;
13680 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
13681 if (TREE_CODE (decl) == FUNCTION_DECL
13682 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
13683 return 0;
13685 return 1;
13688 /* Hash table routines for file_hash. */
13690 static int
13691 file_table_eq (const void *p1_p, const void *p2_p)
13693 const struct dwarf_file_data * p1 = p1_p;
13694 const char * p2 = p2_p;
13695 return strcmp (p1->filename, p2) == 0;
13698 static hashval_t
13699 file_table_hash (const void *p_p)
13701 const struct dwarf_file_data * p = p_p;
13702 return htab_hash_string (p->filename);
13705 /* Lookup FILE_NAME (in the list of filenames that we know about here in
13706 dwarf2out.c) and return its "index". The index of each (known) filename is
13707 just a unique number which is associated with only that one filename. We
13708 need such numbers for the sake of generating labels (in the .debug_sfnames
13709 section) and references to those files numbers (in the .debug_srcinfo
13710 and.debug_macinfo sections). If the filename given as an argument is not
13711 found in our current list, add it to the list and assign it the next
13712 available unique index number. In order to speed up searches, we remember
13713 the index of the filename was looked up last. This handles the majority of
13714 all searches. */
13716 static struct dwarf_file_data *
13717 lookup_filename (const char *file_name)
13719 void ** slot;
13720 struct dwarf_file_data * created;
13722 /* Check to see if the file name that was searched on the previous
13723 call matches this file name. If so, return the index. */
13724 if (file_table_last_lookup
13725 && (file_name == file_table_last_lookup->filename
13726 || strcmp (file_table_last_lookup->filename, file_name) == 0))
13727 return file_table_last_lookup;
13729 /* Didn't match the previous lookup, search the table. */
13730 slot = htab_find_slot_with_hash (file_table, file_name,
13731 htab_hash_string (file_name), INSERT);
13732 if (*slot)
13733 return *slot;
13735 created = ggc_alloc (sizeof (struct dwarf_file_data));
13736 created->filename = file_name;
13737 created->emitted_number = 0;
13738 *slot = created;
13739 return created;
13742 /* If the assembler will construct the file table, then translate the compiler
13743 internal file table number into the assembler file table number, and emit
13744 a .file directive if we haven't already emitted one yet. The file table
13745 numbers are different because we prune debug info for unused variables and
13746 types, which may include filenames. */
13748 static int
13749 maybe_emit_file (struct dwarf_file_data * fd)
13751 if (! fd->emitted_number)
13753 if (last_emitted_file)
13754 fd->emitted_number = last_emitted_file->emitted_number + 1;
13755 else
13756 fd->emitted_number = 1;
13757 last_emitted_file = fd;
13759 if (DWARF2_ASM_LINE_DEBUG_INFO)
13761 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
13762 output_quoted_string (asm_out_file, fd->filename);
13763 fputc ('\n', asm_out_file);
13767 return fd->emitted_number;
13770 /* Called by the final INSN scan whenever we see a var location. We
13771 use it to drop labels in the right places, and throw the location in
13772 our lookup table. */
13774 static void
13775 dwarf2out_var_location (rtx loc_note)
13777 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
13778 struct var_loc_node *newloc;
13779 rtx prev_insn;
13780 static rtx last_insn;
13781 static const char *last_label;
13782 tree decl;
13784 if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
13785 return;
13786 prev_insn = PREV_INSN (loc_note);
13788 newloc = ggc_alloc_cleared (sizeof (struct var_loc_node));
13789 /* If the insn we processed last time is the previous insn
13790 and it is also a var location note, use the label we emitted
13791 last time. */
13792 if (last_insn != NULL_RTX
13793 && last_insn == prev_insn
13794 && NOTE_P (prev_insn)
13795 && NOTE_LINE_NUMBER (prev_insn) == NOTE_INSN_VAR_LOCATION)
13797 newloc->label = last_label;
13799 else
13801 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
13802 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
13803 loclabel_num++;
13804 newloc->label = ggc_strdup (loclabel);
13806 newloc->var_loc_note = loc_note;
13807 newloc->next = NULL;
13809 if (cfun && in_cold_section_p)
13810 newloc->section_label = cfun->cold_section_label;
13811 else
13812 newloc->section_label = text_section_label;
13814 last_insn = loc_note;
13815 last_label = newloc->label;
13816 decl = NOTE_VAR_LOCATION_DECL (loc_note);
13817 add_var_loc_to_decl (decl, newloc);
13820 /* We need to reset the locations at the beginning of each
13821 function. We can't do this in the end_function hook, because the
13822 declarations that use the locations won't have been output when
13823 that hook is called. Also compute have_multiple_function_sections here. */
13825 static void
13826 dwarf2out_begin_function (tree fun)
13828 htab_empty (decl_loc_table);
13830 if (function_section (fun) != text_section)
13831 have_multiple_function_sections = true;
13834 /* Output a label to mark the beginning of a source code line entry
13835 and record information relating to this source line, in
13836 'line_info_table' for later output of the .debug_line section. */
13838 static void
13839 dwarf2out_source_line (unsigned int line, const char *filename)
13841 if (debug_info_level >= DINFO_LEVEL_NORMAL
13842 && line != 0)
13844 int file_num = maybe_emit_file (lookup_filename (filename));
13846 switch_to_section (current_function_section ());
13848 /* If requested, emit something human-readable. */
13849 if (flag_debug_asm)
13850 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
13851 filename, line);
13853 if (DWARF2_ASM_LINE_DEBUG_INFO)
13855 /* Emit the .loc directive understood by GNU as. */
13856 fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
13858 /* Indicate that line number info exists. */
13859 line_info_table_in_use++;
13861 else if (function_section (current_function_decl) != text_section)
13863 dw_separate_line_info_ref line_info;
13864 targetm.asm_out.internal_label (asm_out_file,
13865 SEPARATE_LINE_CODE_LABEL,
13866 separate_line_info_table_in_use);
13868 /* Expand the line info table if necessary. */
13869 if (separate_line_info_table_in_use
13870 == separate_line_info_table_allocated)
13872 separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
13873 separate_line_info_table
13874 = ggc_realloc (separate_line_info_table,
13875 separate_line_info_table_allocated
13876 * sizeof (dw_separate_line_info_entry));
13877 memset (separate_line_info_table
13878 + separate_line_info_table_in_use,
13880 (LINE_INFO_TABLE_INCREMENT
13881 * sizeof (dw_separate_line_info_entry)));
13884 /* Add the new entry at the end of the line_info_table. */
13885 line_info
13886 = &separate_line_info_table[separate_line_info_table_in_use++];
13887 line_info->dw_file_num = file_num;
13888 line_info->dw_line_num = line;
13889 line_info->function = current_function_funcdef_no;
13891 else
13893 dw_line_info_ref line_info;
13895 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
13896 line_info_table_in_use);
13898 /* Expand the line info table if necessary. */
13899 if (line_info_table_in_use == line_info_table_allocated)
13901 line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
13902 line_info_table
13903 = ggc_realloc (line_info_table,
13904 (line_info_table_allocated
13905 * sizeof (dw_line_info_entry)));
13906 memset (line_info_table + line_info_table_in_use, 0,
13907 LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
13910 /* Add the new entry at the end of the line_info_table. */
13911 line_info = &line_info_table[line_info_table_in_use++];
13912 line_info->dw_file_num = file_num;
13913 line_info->dw_line_num = line;
13918 /* Record the beginning of a new source file. */
13920 static void
13921 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
13923 if (flag_eliminate_dwarf2_dups)
13925 /* Record the beginning of the file for break_out_includes. */
13926 dw_die_ref bincl_die;
13928 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
13929 add_AT_string (bincl_die, DW_AT_name, filename);
13932 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13934 int file_num = maybe_emit_file (lookup_filename (filename));
13936 switch_to_section (debug_macinfo_section);
13937 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
13938 dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
13939 lineno);
13941 dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
13945 /* Record the end of a source file. */
13947 static void
13948 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
13950 if (flag_eliminate_dwarf2_dups)
13951 /* Record the end of the file for break_out_includes. */
13952 new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
13954 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13956 switch_to_section (debug_macinfo_section);
13957 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
13961 /* Called from debug_define in toplev.c. The `buffer' parameter contains
13962 the tail part of the directive line, i.e. the part which is past the
13963 initial whitespace, #, whitespace, directive-name, whitespace part. */
13965 static void
13966 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
13967 const char *buffer ATTRIBUTE_UNUSED)
13969 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13971 switch_to_section (debug_macinfo_section);
13972 dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
13973 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
13974 dw2_asm_output_nstring (buffer, -1, "The macro");
13978 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
13979 the tail part of the directive line, i.e. the part which is past the
13980 initial whitespace, #, whitespace, directive-name, whitespace part. */
13982 static void
13983 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
13984 const char *buffer ATTRIBUTE_UNUSED)
13986 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13988 switch_to_section (debug_macinfo_section);
13989 dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
13990 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
13991 dw2_asm_output_nstring (buffer, -1, "The macro");
13995 /* Set up for Dwarf output at the start of compilation. */
13997 static void
13998 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
14000 /* Allocate the file_table. */
14001 file_table = htab_create_ggc (50, file_table_hash,
14002 file_table_eq, NULL);
14004 /* Allocate the decl_die_table. */
14005 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
14006 decl_die_table_eq, NULL);
14008 /* Allocate the decl_loc_table. */
14009 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
14010 decl_loc_table_eq, NULL);
14012 /* Allocate the initial hunk of the decl_scope_table. */
14013 decl_scope_table = VEC_alloc (tree, gc, 256);
14015 /* Allocate the initial hunk of the abbrev_die_table. */
14016 abbrev_die_table = ggc_alloc_cleared (ABBREV_DIE_TABLE_INCREMENT
14017 * sizeof (dw_die_ref));
14018 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
14019 /* Zero-th entry is allocated, but unused. */
14020 abbrev_die_table_in_use = 1;
14022 /* Allocate the initial hunk of the line_info_table. */
14023 line_info_table = ggc_alloc_cleared (LINE_INFO_TABLE_INCREMENT
14024 * sizeof (dw_line_info_entry));
14025 line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
14027 /* Zero-th entry is allocated, but unused. */
14028 line_info_table_in_use = 1;
14030 /* Allocate the pubtypes and pubnames vectors. */
14031 pubname_table = VEC_alloc (pubname_entry, gc, 32);
14032 pubtype_table = VEC_alloc (pubname_entry, gc, 32);
14034 /* Generate the initial DIE for the .debug section. Note that the (string)
14035 value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
14036 will (typically) be a relative pathname and that this pathname should be
14037 taken as being relative to the directory from which the compiler was
14038 invoked when the given (base) source file was compiled. We will fill
14039 in this value in dwarf2out_finish. */
14040 comp_unit_die = gen_compile_unit_die (NULL);
14042 incomplete_types = VEC_alloc (tree, gc, 64);
14044 used_rtx_array = VEC_alloc (rtx, gc, 32);
14046 debug_info_section = get_section (DEBUG_INFO_SECTION,
14047 SECTION_DEBUG, NULL);
14048 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
14049 SECTION_DEBUG, NULL);
14050 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
14051 SECTION_DEBUG, NULL);
14052 debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
14053 SECTION_DEBUG, NULL);
14054 debug_line_section = get_section (DEBUG_LINE_SECTION,
14055 SECTION_DEBUG, NULL);
14056 debug_loc_section = get_section (DEBUG_LOC_SECTION,
14057 SECTION_DEBUG, NULL);
14058 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
14059 SECTION_DEBUG, NULL);
14060 #ifdef DEBUG_PUBTYPES_SECTION
14061 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
14062 SECTION_DEBUG, NULL);
14063 #endif
14064 debug_str_section = get_section (DEBUG_STR_SECTION,
14065 DEBUG_STR_SECTION_FLAGS, NULL);
14066 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
14067 SECTION_DEBUG, NULL);
14068 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
14069 SECTION_DEBUG, NULL);
14071 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
14072 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
14073 DEBUG_ABBREV_SECTION_LABEL, 0);
14074 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
14075 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
14076 COLD_TEXT_SECTION_LABEL, 0);
14077 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
14079 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
14080 DEBUG_INFO_SECTION_LABEL, 0);
14081 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
14082 DEBUG_LINE_SECTION_LABEL, 0);
14083 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
14084 DEBUG_RANGES_SECTION_LABEL, 0);
14085 switch_to_section (debug_abbrev_section);
14086 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
14087 switch_to_section (debug_info_section);
14088 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
14089 switch_to_section (debug_line_section);
14090 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
14092 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14094 switch_to_section (debug_macinfo_section);
14095 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
14096 DEBUG_MACINFO_SECTION_LABEL, 0);
14097 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
14100 switch_to_section (text_section);
14101 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
14102 if (flag_reorder_blocks_and_partition)
14104 switch_to_section (unlikely_text_section ());
14105 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
14109 /* A helper function for dwarf2out_finish called through
14110 ht_forall. Emit one queued .debug_str string. */
14112 static int
14113 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
14115 struct indirect_string_node *node = (struct indirect_string_node *) *h;
14117 if (node->form == DW_FORM_strp)
14119 switch_to_section (debug_str_section);
14120 ASM_OUTPUT_LABEL (asm_out_file, node->label);
14121 assemble_string (node->str, strlen (node->str) + 1);
14124 return 1;
14127 #if ENABLE_ASSERT_CHECKING
14128 /* Verify that all marks are clear. */
14130 static void
14131 verify_marks_clear (dw_die_ref die)
14133 dw_die_ref c;
14135 gcc_assert (! die->die_mark);
14136 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
14138 #endif /* ENABLE_ASSERT_CHECKING */
14140 /* Clear the marks for a die and its children.
14141 Be cool if the mark isn't set. */
14143 static void
14144 prune_unmark_dies (dw_die_ref die)
14146 dw_die_ref c;
14148 if (die->die_mark)
14149 die->die_mark = 0;
14150 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
14153 /* Given DIE that we're marking as used, find any other dies
14154 it references as attributes and mark them as used. */
14156 static void
14157 prune_unused_types_walk_attribs (dw_die_ref die)
14159 dw_attr_ref a;
14160 unsigned ix;
14162 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
14164 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
14166 /* A reference to another DIE.
14167 Make sure that it will get emitted. */
14168 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
14170 /* Set the string's refcount to 0 so that prune_unused_types_mark
14171 accounts properly for it. */
14172 if (AT_class (a) == dw_val_class_str)
14173 a->dw_attr_val.v.val_str->refcount = 0;
14178 /* Mark DIE as being used. If DOKIDS is true, then walk down
14179 to DIE's children. */
14181 static void
14182 prune_unused_types_mark (dw_die_ref die, int dokids)
14184 dw_die_ref c;
14186 if (die->die_mark == 0)
14188 /* We haven't done this node yet. Mark it as used. */
14189 die->die_mark = 1;
14191 /* We also have to mark its parents as used.
14192 (But we don't want to mark our parents' kids due to this.) */
14193 if (die->die_parent)
14194 prune_unused_types_mark (die->die_parent, 0);
14196 /* Mark any referenced nodes. */
14197 prune_unused_types_walk_attribs (die);
14199 /* If this node is a specification,
14200 also mark the definition, if it exists. */
14201 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
14202 prune_unused_types_mark (die->die_definition, 1);
14205 if (dokids && die->die_mark != 2)
14207 /* We need to walk the children, but haven't done so yet.
14208 Remember that we've walked the kids. */
14209 die->die_mark = 2;
14211 /* If this is an array type, we need to make sure our
14212 kids get marked, even if they're types. */
14213 if (die->die_tag == DW_TAG_array_type)
14214 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
14215 else
14216 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
14221 /* Walk the tree DIE and mark types that we actually use. */
14223 static void
14224 prune_unused_types_walk (dw_die_ref die)
14226 dw_die_ref c;
14228 /* Don't do anything if this node is already marked. */
14229 if (die->die_mark)
14230 return;
14232 switch (die->die_tag)
14234 case DW_TAG_const_type:
14235 case DW_TAG_packed_type:
14236 case DW_TAG_pointer_type:
14237 case DW_TAG_reference_type:
14238 case DW_TAG_volatile_type:
14239 case DW_TAG_typedef:
14240 case DW_TAG_array_type:
14241 case DW_TAG_structure_type:
14242 case DW_TAG_union_type:
14243 case DW_TAG_class_type:
14244 case DW_TAG_friend:
14245 case DW_TAG_variant_part:
14246 case DW_TAG_enumeration_type:
14247 case DW_TAG_subroutine_type:
14248 case DW_TAG_string_type:
14249 case DW_TAG_set_type:
14250 case DW_TAG_subrange_type:
14251 case DW_TAG_ptr_to_member_type:
14252 case DW_TAG_file_type:
14253 if (die->die_perennial_p)
14254 break;
14256 /* It's a type node --- don't mark it. */
14257 return;
14259 default:
14260 /* Mark everything else. */
14261 break;
14264 die->die_mark = 1;
14266 /* Now, mark any dies referenced from here. */
14267 prune_unused_types_walk_attribs (die);
14269 /* Mark children. */
14270 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
14273 /* Increment the string counts on strings referred to from DIE's
14274 attributes. */
14276 static void
14277 prune_unused_types_update_strings (dw_die_ref die)
14279 dw_attr_ref a;
14280 unsigned ix;
14282 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
14283 if (AT_class (a) == dw_val_class_str)
14285 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
14286 s->refcount++;
14287 /* Avoid unnecessarily putting strings that are used less than
14288 twice in the hash table. */
14289 if (s->refcount
14290 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
14292 void ** slot;
14293 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
14294 htab_hash_string (s->str),
14295 INSERT);
14296 gcc_assert (*slot == NULL);
14297 *slot = s;
14302 /* Remove from the tree DIE any dies that aren't marked. */
14304 static void
14305 prune_unused_types_prune (dw_die_ref die)
14307 dw_die_ref c;
14309 gcc_assert (die->die_mark);
14310 prune_unused_types_update_strings (die);
14312 if (! die->die_child)
14313 return;
14315 c = die->die_child;
14316 do {
14317 dw_die_ref prev = c;
14318 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
14319 if (c == die->die_child)
14321 /* No marked children between 'prev' and the end of the list. */
14322 if (prev == c)
14323 /* No marked children at all. */
14324 die->die_child = NULL;
14325 else
14327 prev->die_sib = c->die_sib;
14328 die->die_child = prev;
14330 return;
14333 if (c != prev->die_sib)
14334 prev->die_sib = c;
14335 prune_unused_types_prune (c);
14336 } while (c != die->die_child);
14340 /* Remove dies representing declarations that we never use. */
14342 static void
14343 prune_unused_types (void)
14345 unsigned int i;
14346 limbo_die_node *node;
14347 pubname_ref pub;
14349 #if ENABLE_ASSERT_CHECKING
14350 /* All the marks should already be clear. */
14351 verify_marks_clear (comp_unit_die);
14352 for (node = limbo_die_list; node; node = node->next)
14353 verify_marks_clear (node->die);
14354 #endif /* ENABLE_ASSERT_CHECKING */
14356 /* Set the mark on nodes that are actually used. */
14357 prune_unused_types_walk (comp_unit_die);
14358 for (node = limbo_die_list; node; node = node->next)
14359 prune_unused_types_walk (node->die);
14361 /* Also set the mark on nodes referenced from the
14362 pubname_table or arange_table. */
14363 for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
14364 prune_unused_types_mark (pub->die, 1);
14365 for (i = 0; i < arange_table_in_use; i++)
14366 prune_unused_types_mark (arange_table[i], 1);
14368 /* Get rid of nodes that aren't marked; and update the string counts. */
14369 if (debug_str_hash)
14370 htab_empty (debug_str_hash);
14371 prune_unused_types_prune (comp_unit_die);
14372 for (node = limbo_die_list; node; node = node->next)
14373 prune_unused_types_prune (node->die);
14375 /* Leave the marks clear. */
14376 prune_unmark_dies (comp_unit_die);
14377 for (node = limbo_die_list; node; node = node->next)
14378 prune_unmark_dies (node->die);
14381 /* Set the parameter to true if there are any relative pathnames in
14382 the file table. */
14383 static int
14384 file_table_relative_p (void ** slot, void *param)
14386 bool *p = param;
14387 struct dwarf_file_data *d = *slot;
14388 if (d->emitted_number && !IS_ABSOLUTE_PATH (d->filename))
14390 *p = true;
14391 return 0;
14393 return 1;
14396 /* Output stuff that dwarf requires at the end of every file,
14397 and generate the DWARF-2 debugging info. */
14399 static void
14400 dwarf2out_finish (const char *filename)
14402 limbo_die_node *node, *next_node;
14403 dw_die_ref die = 0;
14405 /* Add the name for the main input file now. We delayed this from
14406 dwarf2out_init to avoid complications with PCH. */
14407 add_name_attribute (comp_unit_die, filename);
14408 if (!IS_ABSOLUTE_PATH (filename))
14409 add_comp_dir_attribute (comp_unit_die);
14410 else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
14412 bool p = false;
14413 htab_traverse (file_table, file_table_relative_p, &p);
14414 if (p)
14415 add_comp_dir_attribute (comp_unit_die);
14418 /* Traverse the limbo die list, and add parent/child links. The only
14419 dies without parents that should be here are concrete instances of
14420 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
14421 For concrete instances, we can get the parent die from the abstract
14422 instance. */
14423 for (node = limbo_die_list; node; node = next_node)
14425 next_node = node->next;
14426 die = node->die;
14428 if (die->die_parent == NULL)
14430 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
14432 if (origin)
14433 add_child_die (origin->die_parent, die);
14434 else if (die == comp_unit_die)
14436 else if (errorcount > 0 || sorrycount > 0)
14437 /* It's OK to be confused by errors in the input. */
14438 add_child_die (comp_unit_die, die);
14439 else
14441 /* In certain situations, the lexical block containing a
14442 nested function can be optimized away, which results
14443 in the nested function die being orphaned. Likewise
14444 with the return type of that nested function. Force
14445 this to be a child of the containing function.
14447 It may happen that even the containing function got fully
14448 inlined and optimized out. In that case we are lost and
14449 assign the empty child. This should not be big issue as
14450 the function is likely unreachable too. */
14451 tree context = NULL_TREE;
14453 gcc_assert (node->created_for);
14455 if (DECL_P (node->created_for))
14456 context = DECL_CONTEXT (node->created_for);
14457 else if (TYPE_P (node->created_for))
14458 context = TYPE_CONTEXT (node->created_for);
14460 gcc_assert (context
14461 && (TREE_CODE (context) == FUNCTION_DECL
14462 || TREE_CODE (context) == NAMESPACE_DECL));
14464 origin = lookup_decl_die (context);
14465 if (origin)
14466 add_child_die (origin, die);
14467 else
14468 add_child_die (comp_unit_die, die);
14473 limbo_die_list = NULL;
14475 /* Walk through the list of incomplete types again, trying once more to
14476 emit full debugging info for them. */
14477 retry_incomplete_types ();
14479 if (flag_eliminate_unused_debug_types)
14480 prune_unused_types ();
14482 /* Generate separate CUs for each of the include files we've seen.
14483 They will go into limbo_die_list. */
14484 if (flag_eliminate_dwarf2_dups)
14485 break_out_includes (comp_unit_die);
14487 /* Traverse the DIE's and add add sibling attributes to those DIE's
14488 that have children. */
14489 add_sibling_attributes (comp_unit_die);
14490 for (node = limbo_die_list; node; node = node->next)
14491 add_sibling_attributes (node->die);
14493 /* Output a terminator label for the .text section. */
14494 switch_to_section (text_section);
14495 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
14496 if (flag_reorder_blocks_and_partition)
14498 switch_to_section (unlikely_text_section ());
14499 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
14502 /* We can only use the low/high_pc attributes if all of the code was
14503 in .text. */
14504 if (!have_multiple_function_sections)
14506 add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
14507 add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
14510 /* If it wasn't, we need to give .debug_loc and .debug_ranges an appropriate
14511 "base address". Use zero so that these addresses become absolute. */
14512 else if (have_location_lists || ranges_table_in_use)
14513 add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
14515 /* Output location list section if necessary. */
14516 if (have_location_lists)
14518 /* Output the location lists info. */
14519 switch_to_section (debug_loc_section);
14520 ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
14521 DEBUG_LOC_SECTION_LABEL, 0);
14522 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
14523 output_location_lists (die);
14526 if (debug_info_level >= DINFO_LEVEL_NORMAL)
14527 add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
14528 debug_line_section_label);
14530 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14531 add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
14533 /* Output all of the compilation units. We put the main one last so that
14534 the offsets are available to output_pubnames. */
14535 for (node = limbo_die_list; node; node = node->next)
14536 output_comp_unit (node->die, 0);
14538 output_comp_unit (comp_unit_die, 0);
14540 /* Output the abbreviation table. */
14541 switch_to_section (debug_abbrev_section);
14542 output_abbrev_section ();
14544 /* Output public names table if necessary. */
14545 if (!VEC_empty (pubname_entry, pubname_table))
14547 switch_to_section (debug_pubnames_section);
14548 output_pubnames (pubname_table);
14551 #ifdef DEBUG_PUBTYPES_SECTION
14552 /* Output public types table if necessary. */
14553 if (!VEC_empty (pubname_entry, pubtype_table))
14555 switch_to_section (debug_pubtypes_section);
14556 output_pubnames (pubtype_table);
14558 #endif
14560 /* Output the address range information. We only put functions in the arange
14561 table, so don't write it out if we don't have any. */
14562 if (fde_table_in_use)
14564 switch_to_section (debug_aranges_section);
14565 output_aranges ();
14568 /* Output ranges section if necessary. */
14569 if (ranges_table_in_use)
14571 switch_to_section (debug_ranges_section);
14572 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
14573 output_ranges ();
14576 /* Output the source line correspondence table. We must do this
14577 even if there is no line information. Otherwise, on an empty
14578 translation unit, we will generate a present, but empty,
14579 .debug_info section. IRIX 6.5 `nm' will then complain when
14580 examining the file. This is done late so that any filenames
14581 used by the debug_info section are marked as 'used'. */
14582 if (! DWARF2_ASM_LINE_DEBUG_INFO)
14584 switch_to_section (debug_line_section);
14585 output_line_info ();
14588 /* Have to end the macro section. */
14589 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14591 switch_to_section (debug_macinfo_section);
14592 dw2_asm_output_data (1, 0, "End compilation unit");
14595 /* If we emitted any DW_FORM_strp form attribute, output the string
14596 table too. */
14597 if (debug_str_hash)
14598 htab_traverse (debug_str_hash, output_indirect_string, NULL);
14600 #else
14602 /* This should never be used, but its address is needed for comparisons. */
14603 const struct gcc_debug_hooks dwarf2_debug_hooks;
14605 #endif /* DWARF2_DEBUGGING_INFO */
14607 #include "gt-dwarf2out.h"