Daily bump.
[official-gcc.git] / gcc / dwarf2out.c
blob527de82dd5d5f7d530eb7c4d4b44e01fa5d27d37
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
4 Contributed by Gary Funck (gary@intrepid.com).
5 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
6 Extensively modified by Jason Merrill (jason@cygnus.com).
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
13 version.
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
24 /* TODO: Emit .debug_line header even when there are no functions, since
25 the file numbers are used by .debug_info. Alternately, leave
26 out locations for types and decls.
27 Avoid talking about ctors and op= for PODs.
28 Factor out common prologue sequences into multiple CIEs. */
30 /* The first part of this file deals with the DWARF 2 frame unwind
31 information, which is also used by the GCC efficient exception handling
32 mechanism. The second part, controlled only by an #ifdef
33 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
34 information. */
36 /* DWARF2 Abbreviation Glossary:
38 CFA = Canonical Frame Address
39 a fixed address on the stack which identifies a call frame.
40 We define it to be the value of SP just before the call insn.
41 The CFA register and offset, which may change during the course
42 of the function, are used to calculate its value at runtime.
44 CFI = Call Frame Instruction
45 an instruction for the DWARF2 abstract machine
47 CIE = Common Information Entry
48 information describing information common to one or more FDEs
50 DIE = Debugging Information Entry
52 FDE = Frame Description Entry
53 information describing the stack call frame, in particular,
54 how to restore registers
56 DW_CFA_... = DWARF2 CFA call frame instruction
57 DW_TAG_... = DWARF2 DIE tag */
59 #include "config.h"
60 #include "system.h"
61 #include "coretypes.h"
62 #include "tm.h"
63 #include "tree.h"
64 #include "version.h"
65 #include "flags.h"
66 #include "real.h"
67 #include "rtl.h"
68 #include "hard-reg-set.h"
69 #include "regs.h"
70 #include "insn-config.h"
71 #include "reload.h"
72 #include "function.h"
73 #include "output.h"
74 #include "expr.h"
75 #include "libfuncs.h"
76 #include "except.h"
77 #include "dwarf2.h"
78 #include "dwarf2out.h"
79 #include "dwarf2asm.h"
80 #include "toplev.h"
81 #include "varray.h"
82 #include "ggc.h"
83 #include "md5.h"
84 #include "tm_p.h"
85 #include "diagnostic.h"
86 #include "debug.h"
87 #include "target.h"
88 #include "langhooks.h"
89 #include "hashtab.h"
90 #include "cgraph.h"
91 #include "input.h"
93 #ifdef DWARF2_DEBUGGING_INFO
94 static void dwarf2out_source_line (unsigned int, const char *);
95 #endif
97 #ifndef DWARF2_FRAME_INFO
98 # ifdef DWARF2_DEBUGGING_INFO
99 # define DWARF2_FRAME_INFO \
100 (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
101 # else
102 # define DWARF2_FRAME_INFO 0
103 # endif
104 #endif
106 /* Map register numbers held in the call frame info that gcc has
107 collected using DWARF_FRAME_REGNUM to those that should be output in
108 .debug_frame and .eh_frame. */
109 #ifndef DWARF2_FRAME_REG_OUT
110 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
111 #endif
113 /* Decide whether we want to emit frame unwind information for the current
114 translation unit. */
117 dwarf2out_do_frame (void)
119 /* We want to emit correct CFA location expressions or lists, so we
120 have to return true if we're going to output debug info, even if
121 we're not going to output frame or unwind info. */
122 return (write_symbols == DWARF2_DEBUG
123 || write_symbols == VMS_AND_DWARF2_DEBUG
124 || DWARF2_FRAME_INFO
125 #ifdef DWARF2_UNWIND_INFO
126 || (DWARF2_UNWIND_INFO
127 && (flag_unwind_tables
128 || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)))
129 #endif
133 /* The size of the target's pointer type. */
134 #ifndef PTR_SIZE
135 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
136 #endif
138 /* Array of RTXes referenced by the debugging information, which therefore
139 must be kept around forever. */
140 static GTY(()) VEC(rtx,gc) *used_rtx_array;
142 /* A pointer to the base of a list of incomplete types which might be
143 completed at some later time. incomplete_types_list needs to be a
144 VEC(tree,gc) because we want to tell the garbage collector about
145 it. */
146 static GTY(()) VEC(tree,gc) *incomplete_types;
148 /* A pointer to the base of a table of references to declaration
149 scopes. This table is a display which tracks the nesting
150 of declaration scopes at the current scope and containing
151 scopes. This table is used to find the proper place to
152 define type declaration DIE's. */
153 static GTY(()) VEC(tree,gc) *decl_scope_table;
155 /* Pointers to various DWARF2 sections. */
156 static GTY(()) section *debug_info_section;
157 static GTY(()) section *debug_abbrev_section;
158 static GTY(()) section *debug_aranges_section;
159 static GTY(()) section *debug_macinfo_section;
160 static GTY(()) section *debug_line_section;
161 static GTY(()) section *debug_loc_section;
162 static GTY(()) section *debug_pubnames_section;
163 static GTY(()) section *debug_pubtypes_section;
164 static GTY(()) section *debug_str_section;
165 static GTY(()) section *debug_ranges_section;
166 static GTY(()) section *debug_frame_section;
168 /* How to start an assembler comment. */
169 #ifndef ASM_COMMENT_START
170 #define ASM_COMMENT_START ";#"
171 #endif
173 typedef struct dw_cfi_struct *dw_cfi_ref;
174 typedef struct dw_fde_struct *dw_fde_ref;
175 typedef union dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
177 /* Call frames are described using a sequence of Call Frame
178 Information instructions. The register number, offset
179 and address fields are provided as possible operands;
180 their use is selected by the opcode field. */
182 enum dw_cfi_oprnd_type {
183 dw_cfi_oprnd_unused,
184 dw_cfi_oprnd_reg_num,
185 dw_cfi_oprnd_offset,
186 dw_cfi_oprnd_addr,
187 dw_cfi_oprnd_loc
190 typedef union dw_cfi_oprnd_struct GTY(())
192 unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
193 HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
194 const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
195 struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
197 dw_cfi_oprnd;
199 typedef struct dw_cfi_struct GTY(())
201 dw_cfi_ref dw_cfi_next;
202 enum dwarf_call_frame_info dw_cfi_opc;
203 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
204 dw_cfi_oprnd1;
205 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
206 dw_cfi_oprnd2;
208 dw_cfi_node;
210 /* This is how we define the location of the CFA. We use to handle it
211 as REG + OFFSET all the time, but now it can be more complex.
212 It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
213 Instead of passing around REG and OFFSET, we pass a copy
214 of this structure. */
215 typedef struct cfa_loc GTY(())
217 HOST_WIDE_INT offset;
218 HOST_WIDE_INT base_offset;
219 unsigned int reg;
220 int indirect; /* 1 if CFA is accessed via a dereference. */
221 } dw_cfa_location;
223 /* All call frame descriptions (FDE's) in the GCC generated DWARF
224 refer to a single Common Information Entry (CIE), defined at
225 the beginning of the .debug_frame section. This use of a single
226 CIE obviates the need to keep track of multiple CIE's
227 in the DWARF generation routines below. */
229 typedef struct dw_fde_struct GTY(())
231 tree decl;
232 const char *dw_fde_begin;
233 const char *dw_fde_current_label;
234 const char *dw_fde_end;
235 const char *dw_fde_hot_section_label;
236 const char *dw_fde_hot_section_end_label;
237 const char *dw_fde_unlikely_section_label;
238 const char *dw_fde_unlikely_section_end_label;
239 bool dw_fde_switched_sections;
240 dw_cfi_ref dw_fde_cfi;
241 unsigned funcdef_number;
242 unsigned all_throwers_are_sibcalls : 1;
243 unsigned nothrow : 1;
244 unsigned uses_eh_lsda : 1;
246 dw_fde_node;
248 /* Maximum size (in bytes) of an artificially generated label. */
249 #define MAX_ARTIFICIAL_LABEL_BYTES 30
251 /* The size of addresses as they appear in the Dwarf 2 data.
252 Some architectures use word addresses to refer to code locations,
253 but Dwarf 2 info always uses byte addresses. On such machines,
254 Dwarf 2 addresses need to be larger than the architecture's
255 pointers. */
256 #ifndef DWARF2_ADDR_SIZE
257 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
258 #endif
260 /* The size in bytes of a DWARF field indicating an offset or length
261 relative to a debug info section, specified to be 4 bytes in the
262 DWARF-2 specification. The SGI/MIPS ABI defines it to be the same
263 as PTR_SIZE. */
265 #ifndef DWARF_OFFSET_SIZE
266 #define DWARF_OFFSET_SIZE 4
267 #endif
269 /* According to the (draft) DWARF 3 specification, the initial length
270 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
271 bytes are 0xffffffff, followed by the length stored in the next 8
272 bytes.
274 However, the SGI/MIPS ABI uses an initial length which is equal to
275 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
277 #ifndef DWARF_INITIAL_LENGTH_SIZE
278 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
279 #endif
281 #define DWARF_VERSION 2
283 /* Round SIZE up to the nearest BOUNDARY. */
284 #define DWARF_ROUND(SIZE,BOUNDARY) \
285 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
287 /* Offsets recorded in opcodes are a multiple of this alignment factor. */
288 #ifndef DWARF_CIE_DATA_ALIGNMENT
289 #ifdef STACK_GROWS_DOWNWARD
290 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
291 #else
292 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
293 #endif
294 #endif
296 /* CIE identifier. */
297 #if HOST_BITS_PER_WIDE_INT >= 64
298 #define DWARF_CIE_ID \
299 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
300 #else
301 #define DWARF_CIE_ID DW_CIE_ID
302 #endif
304 /* A pointer to the base of a table that contains frame description
305 information for each routine. */
306 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
308 /* Number of elements currently allocated for fde_table. */
309 static GTY(()) unsigned fde_table_allocated;
311 /* Number of elements in fde_table currently in use. */
312 static GTY(()) unsigned fde_table_in_use;
314 /* Size (in elements) of increments by which we may expand the
315 fde_table. */
316 #define FDE_TABLE_INCREMENT 256
318 /* A list of call frame insns for the CIE. */
319 static GTY(()) dw_cfi_ref cie_cfi_head;
321 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
322 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
323 attribute that accelerates the lookup of the FDE associated
324 with the subprogram. This variable holds the table index of the FDE
325 associated with the current function (body) definition. */
326 static unsigned current_funcdef_fde;
327 #endif
329 struct indirect_string_node GTY(())
331 const char *str;
332 unsigned int refcount;
333 unsigned int form;
334 char *label;
337 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
339 static GTY(()) int dw2_string_counter;
340 static GTY(()) unsigned long dwarf2out_cfi_label_num;
342 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
344 /* Forward declarations for functions defined in this file. */
346 static char *stripattributes (const char *);
347 static const char *dwarf_cfi_name (unsigned);
348 static dw_cfi_ref new_cfi (void);
349 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
350 static void add_fde_cfi (const char *, dw_cfi_ref);
351 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *);
352 static void lookup_cfa (dw_cfa_location *);
353 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
354 #ifdef DWARF2_UNWIND_INFO
355 static void initial_return_save (rtx);
356 #endif
357 static HOST_WIDE_INT stack_adjust_offset (const_rtx);
358 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
359 static void output_call_frame_info (int);
360 static void dwarf2out_stack_adjust (rtx, bool);
361 static void flush_queued_reg_saves (void);
362 static bool clobbers_queued_reg_save (const_rtx);
363 static void dwarf2out_frame_debug_expr (rtx, const char *);
365 /* Support for complex CFA locations. */
366 static void output_cfa_loc (dw_cfi_ref);
367 static void get_cfa_from_loc_descr (dw_cfa_location *,
368 struct dw_loc_descr_struct *);
369 static struct dw_loc_descr_struct *build_cfa_loc
370 (dw_cfa_location *, HOST_WIDE_INT);
371 static void def_cfa_1 (const char *, dw_cfa_location *);
373 /* How to start an assembler comment. */
374 #ifndef ASM_COMMENT_START
375 #define ASM_COMMENT_START ";#"
376 #endif
378 /* Data and reference forms for relocatable data. */
379 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
380 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
382 #ifndef DEBUG_FRAME_SECTION
383 #define DEBUG_FRAME_SECTION ".debug_frame"
384 #endif
386 #ifndef FUNC_BEGIN_LABEL
387 #define FUNC_BEGIN_LABEL "LFB"
388 #endif
390 #ifndef FUNC_END_LABEL
391 #define FUNC_END_LABEL "LFE"
392 #endif
394 #ifndef FRAME_BEGIN_LABEL
395 #define FRAME_BEGIN_LABEL "Lframe"
396 #endif
397 #define CIE_AFTER_SIZE_LABEL "LSCIE"
398 #define CIE_END_LABEL "LECIE"
399 #define FDE_LABEL "LSFDE"
400 #define FDE_AFTER_SIZE_LABEL "LASFDE"
401 #define FDE_END_LABEL "LEFDE"
402 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
403 #define LINE_NUMBER_END_LABEL "LELT"
404 #define LN_PROLOG_AS_LABEL "LASLTP"
405 #define LN_PROLOG_END_LABEL "LELTP"
406 #define DIE_LABEL_PREFIX "DW"
408 /* The DWARF 2 CFA column which tracks the return address. Normally this
409 is the column for PC, or the first column after all of the hard
410 registers. */
411 #ifndef DWARF_FRAME_RETURN_COLUMN
412 #ifdef PC_REGNUM
413 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGNUM (PC_REGNUM)
414 #else
415 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGISTERS
416 #endif
417 #endif
419 /* The mapping from gcc register number to DWARF 2 CFA column number. By
420 default, we just provide columns for all registers. */
421 #ifndef DWARF_FRAME_REGNUM
422 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
423 #endif
425 /* Hook used by __throw. */
428 expand_builtin_dwarf_sp_column (void)
430 unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
431 return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
434 /* Return a pointer to a copy of the section string name S with all
435 attributes stripped off, and an asterisk prepended (for assemble_name). */
437 static inline char *
438 stripattributes (const char *s)
440 char *stripped = XNEWVEC (char, strlen (s) + 2);
441 char *p = stripped;
443 *p++ = '*';
445 while (*s && *s != ',')
446 *p++ = *s++;
448 *p = '\0';
449 return stripped;
452 /* MEM is a memory reference for the register size table, each element of
453 which has mode MODE. Initialize column C as a return address column. */
455 static void
456 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
458 HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
459 HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
460 emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
463 /* Generate code to initialize the register size table. */
465 void
466 expand_builtin_init_dwarf_reg_sizes (tree address)
468 unsigned int i;
469 enum machine_mode mode = TYPE_MODE (char_type_node);
470 rtx addr = expand_normal (address);
471 rtx mem = gen_rtx_MEM (BLKmode, addr);
472 bool wrote_return_column = false;
474 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
476 int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
478 if (rnum < DWARF_FRAME_REGISTERS)
480 HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
481 enum machine_mode save_mode = reg_raw_mode[i];
482 HOST_WIDE_INT size;
484 if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
485 save_mode = choose_hard_reg_mode (i, 1, true);
486 if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
488 if (save_mode == VOIDmode)
489 continue;
490 wrote_return_column = true;
492 size = GET_MODE_SIZE (save_mode);
493 if (offset < 0)
494 continue;
496 emit_move_insn (adjust_address (mem, mode, offset),
497 gen_int_mode (size, mode));
501 if (!wrote_return_column)
502 init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
504 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
505 init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
506 #endif
508 targetm.init_dwarf_reg_sizes_extra (address);
511 /* Convert a DWARF call frame info. operation to its string name */
513 static const char *
514 dwarf_cfi_name (unsigned int cfi_opc)
516 switch (cfi_opc)
518 case DW_CFA_advance_loc:
519 return "DW_CFA_advance_loc";
520 case DW_CFA_offset:
521 return "DW_CFA_offset";
522 case DW_CFA_restore:
523 return "DW_CFA_restore";
524 case DW_CFA_nop:
525 return "DW_CFA_nop";
526 case DW_CFA_set_loc:
527 return "DW_CFA_set_loc";
528 case DW_CFA_advance_loc1:
529 return "DW_CFA_advance_loc1";
530 case DW_CFA_advance_loc2:
531 return "DW_CFA_advance_loc2";
532 case DW_CFA_advance_loc4:
533 return "DW_CFA_advance_loc4";
534 case DW_CFA_offset_extended:
535 return "DW_CFA_offset_extended";
536 case DW_CFA_restore_extended:
537 return "DW_CFA_restore_extended";
538 case DW_CFA_undefined:
539 return "DW_CFA_undefined";
540 case DW_CFA_same_value:
541 return "DW_CFA_same_value";
542 case DW_CFA_register:
543 return "DW_CFA_register";
544 case DW_CFA_remember_state:
545 return "DW_CFA_remember_state";
546 case DW_CFA_restore_state:
547 return "DW_CFA_restore_state";
548 case DW_CFA_def_cfa:
549 return "DW_CFA_def_cfa";
550 case DW_CFA_def_cfa_register:
551 return "DW_CFA_def_cfa_register";
552 case DW_CFA_def_cfa_offset:
553 return "DW_CFA_def_cfa_offset";
555 /* DWARF 3 */
556 case DW_CFA_def_cfa_expression:
557 return "DW_CFA_def_cfa_expression";
558 case DW_CFA_expression:
559 return "DW_CFA_expression";
560 case DW_CFA_offset_extended_sf:
561 return "DW_CFA_offset_extended_sf";
562 case DW_CFA_def_cfa_sf:
563 return "DW_CFA_def_cfa_sf";
564 case DW_CFA_def_cfa_offset_sf:
565 return "DW_CFA_def_cfa_offset_sf";
567 /* SGI/MIPS specific */
568 case DW_CFA_MIPS_advance_loc8:
569 return "DW_CFA_MIPS_advance_loc8";
571 /* GNU extensions */
572 case DW_CFA_GNU_window_save:
573 return "DW_CFA_GNU_window_save";
574 case DW_CFA_GNU_args_size:
575 return "DW_CFA_GNU_args_size";
576 case DW_CFA_GNU_negative_offset_extended:
577 return "DW_CFA_GNU_negative_offset_extended";
579 default:
580 return "DW_CFA_<unknown>";
584 /* Return a pointer to a newly allocated Call Frame Instruction. */
586 static inline dw_cfi_ref
587 new_cfi (void)
589 dw_cfi_ref cfi = ggc_alloc (sizeof (dw_cfi_node));
591 cfi->dw_cfi_next = NULL;
592 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
593 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
595 return cfi;
598 /* Add a Call Frame Instruction to list of instructions. */
600 static inline void
601 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
603 dw_cfi_ref *p;
605 /* Find the end of the chain. */
606 for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
609 *p = cfi;
612 /* Generate a new label for the CFI info to refer to. */
614 char *
615 dwarf2out_cfi_label (void)
617 static char label[20];
619 ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
620 ASM_OUTPUT_LABEL (asm_out_file, label);
621 return label;
624 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
625 or to the CIE if LABEL is NULL. */
627 static void
628 add_fde_cfi (const char *label, dw_cfi_ref cfi)
630 if (label)
632 dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
634 if (*label == 0)
635 label = dwarf2out_cfi_label ();
637 if (fde->dw_fde_current_label == NULL
638 || strcmp (label, fde->dw_fde_current_label) != 0)
640 dw_cfi_ref xcfi;
642 label = xstrdup (label);
644 /* Set the location counter to the new label. */
645 xcfi = new_cfi ();
646 /* If we have a current label, advance from there, otherwise
647 set the location directly using set_loc. */
648 xcfi->dw_cfi_opc = fde->dw_fde_current_label
649 ? DW_CFA_advance_loc4
650 : DW_CFA_set_loc;
651 xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
652 add_cfi (&fde->dw_fde_cfi, xcfi);
654 fde->dw_fde_current_label = label;
657 add_cfi (&fde->dw_fde_cfi, cfi);
660 else
661 add_cfi (&cie_cfi_head, cfi);
664 /* Subroutine of lookup_cfa. */
666 static void
667 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc)
669 switch (cfi->dw_cfi_opc)
671 case DW_CFA_def_cfa_offset:
672 loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
673 break;
674 case DW_CFA_def_cfa_offset_sf:
675 loc->offset
676 = cfi->dw_cfi_oprnd1.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
677 break;
678 case DW_CFA_def_cfa_register:
679 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
680 break;
681 case DW_CFA_def_cfa:
682 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
683 loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
684 break;
685 case DW_CFA_def_cfa_sf:
686 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
687 loc->offset
688 = cfi->dw_cfi_oprnd2.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
689 break;
690 case DW_CFA_def_cfa_expression:
691 get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
692 break;
693 default:
694 break;
698 /* Find the previous value for the CFA. */
700 static void
701 lookup_cfa (dw_cfa_location *loc)
703 dw_cfi_ref cfi;
705 loc->reg = INVALID_REGNUM;
706 loc->offset = 0;
707 loc->indirect = 0;
708 loc->base_offset = 0;
710 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
711 lookup_cfa_1 (cfi, loc);
713 if (fde_table_in_use)
715 dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
716 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
717 lookup_cfa_1 (cfi, loc);
721 /* The current rule for calculating the DWARF2 canonical frame address. */
722 static dw_cfa_location cfa;
724 /* The register used for saving registers to the stack, and its offset
725 from the CFA. */
726 static dw_cfa_location cfa_store;
728 /* The running total of the size of arguments pushed onto the stack. */
729 static HOST_WIDE_INT args_size;
731 /* The last args_size we actually output. */
732 static HOST_WIDE_INT old_args_size;
734 /* Entry point to update the canonical frame address (CFA).
735 LABEL is passed to add_fde_cfi. The value of CFA is now to be
736 calculated from REG+OFFSET. */
738 void
739 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
741 dw_cfa_location loc;
742 loc.indirect = 0;
743 loc.base_offset = 0;
744 loc.reg = reg;
745 loc.offset = offset;
746 def_cfa_1 (label, &loc);
749 /* Determine if two dw_cfa_location structures define the same data. */
751 static bool
752 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
754 return (loc1->reg == loc2->reg
755 && loc1->offset == loc2->offset
756 && loc1->indirect == loc2->indirect
757 && (loc1->indirect == 0
758 || loc1->base_offset == loc2->base_offset));
761 /* This routine does the actual work. The CFA is now calculated from
762 the dw_cfa_location structure. */
764 static void
765 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
767 dw_cfi_ref cfi;
768 dw_cfa_location old_cfa, loc;
770 cfa = *loc_p;
771 loc = *loc_p;
773 if (cfa_store.reg == loc.reg && loc.indirect == 0)
774 cfa_store.offset = loc.offset;
776 loc.reg = DWARF_FRAME_REGNUM (loc.reg);
777 lookup_cfa (&old_cfa);
779 /* If nothing changed, no need to issue any call frame instructions. */
780 if (cfa_equal_p (&loc, &old_cfa))
781 return;
783 cfi = new_cfi ();
785 if (loc.reg == old_cfa.reg && !loc.indirect)
787 /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
788 the CFA register did not change but the offset did. */
789 if (loc.offset < 0)
791 HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
792 gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
794 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
795 cfi->dw_cfi_oprnd1.dw_cfi_offset = f_offset;
797 else
799 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
800 cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
804 #ifndef MIPS_DEBUGGING_INFO /* SGI dbx thinks this means no offset. */
805 else if (loc.offset == old_cfa.offset
806 && old_cfa.reg != INVALID_REGNUM
807 && !loc.indirect)
809 /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
810 indicating the CFA register has changed to <register> but the
811 offset has not changed. */
812 cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
813 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
815 #endif
817 else if (loc.indirect == 0)
819 /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
820 indicating the CFA register has changed to <register> with
821 the specified offset. */
822 if (loc.offset < 0)
824 HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
825 gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
827 cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
828 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
829 cfi->dw_cfi_oprnd2.dw_cfi_offset = f_offset;
831 else
833 cfi->dw_cfi_opc = DW_CFA_def_cfa;
834 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
835 cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
838 else
840 /* Construct a DW_CFA_def_cfa_expression instruction to
841 calculate the CFA using a full location expression since no
842 register-offset pair is available. */
843 struct dw_loc_descr_struct *loc_list;
845 cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
846 loc_list = build_cfa_loc (&loc, 0);
847 cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
850 add_fde_cfi (label, cfi);
853 /* Add the CFI for saving a register. REG is the CFA column number.
854 LABEL is passed to add_fde_cfi.
855 If SREG is -1, the register is saved at OFFSET from the CFA;
856 otherwise it is saved in SREG. */
858 static void
859 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
861 dw_cfi_ref cfi = new_cfi ();
863 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
865 if (sreg == INVALID_REGNUM)
867 if (reg & ~0x3f)
868 /* The register number won't fit in 6 bits, so we have to use
869 the long form. */
870 cfi->dw_cfi_opc = DW_CFA_offset_extended;
871 else
872 cfi->dw_cfi_opc = DW_CFA_offset;
874 #ifdef ENABLE_CHECKING
876 /* If we get an offset that is not a multiple of
877 DWARF_CIE_DATA_ALIGNMENT, there is either a bug in the
878 definition of DWARF_CIE_DATA_ALIGNMENT, or a bug in the machine
879 description. */
880 HOST_WIDE_INT check_offset = offset / DWARF_CIE_DATA_ALIGNMENT;
882 gcc_assert (check_offset * DWARF_CIE_DATA_ALIGNMENT == offset);
884 #endif
885 offset /= DWARF_CIE_DATA_ALIGNMENT;
886 if (offset < 0)
887 cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
889 cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
891 else if (sreg == reg)
892 cfi->dw_cfi_opc = DW_CFA_same_value;
893 else
895 cfi->dw_cfi_opc = DW_CFA_register;
896 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
899 add_fde_cfi (label, cfi);
902 /* Add the CFI for saving a register window. LABEL is passed to reg_save.
903 This CFI tells the unwinder that it needs to restore the window registers
904 from the previous frame's window save area.
906 ??? Perhaps we should note in the CIE where windows are saved (instead of
907 assuming 0(cfa)) and what registers are in the window. */
909 void
910 dwarf2out_window_save (const char *label)
912 dw_cfi_ref cfi = new_cfi ();
914 cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
915 add_fde_cfi (label, cfi);
918 /* Add a CFI to update the running total of the size of arguments
919 pushed onto the stack. */
921 void
922 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
924 dw_cfi_ref cfi;
926 if (size == old_args_size)
927 return;
929 old_args_size = size;
931 cfi = new_cfi ();
932 cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
933 cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
934 add_fde_cfi (label, cfi);
937 /* Entry point for saving a register to the stack. REG is the GCC register
938 number. LABEL and OFFSET are passed to reg_save. */
940 void
941 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
943 reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
946 /* Entry point for saving the return address in the stack.
947 LABEL and OFFSET are passed to reg_save. */
949 void
950 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
952 reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
955 /* Entry point for saving the return address in a register.
956 LABEL and SREG are passed to reg_save. */
958 void
959 dwarf2out_return_reg (const char *label, unsigned int sreg)
961 reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
964 #ifdef DWARF2_UNWIND_INFO
965 /* Record the initial position of the return address. RTL is
966 INCOMING_RETURN_ADDR_RTX. */
968 static void
969 initial_return_save (rtx rtl)
971 unsigned int reg = INVALID_REGNUM;
972 HOST_WIDE_INT offset = 0;
974 switch (GET_CODE (rtl))
976 case REG:
977 /* RA is in a register. */
978 reg = DWARF_FRAME_REGNUM (REGNO (rtl));
979 break;
981 case MEM:
982 /* RA is on the stack. */
983 rtl = XEXP (rtl, 0);
984 switch (GET_CODE (rtl))
986 case REG:
987 gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
988 offset = 0;
989 break;
991 case PLUS:
992 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
993 offset = INTVAL (XEXP (rtl, 1));
994 break;
996 case MINUS:
997 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
998 offset = -INTVAL (XEXP (rtl, 1));
999 break;
1001 default:
1002 gcc_unreachable ();
1005 break;
1007 case PLUS:
1008 /* The return address is at some offset from any value we can
1009 actually load. For instance, on the SPARC it is in %i7+8. Just
1010 ignore the offset for now; it doesn't matter for unwinding frames. */
1011 gcc_assert (GET_CODE (XEXP (rtl, 1)) == CONST_INT);
1012 initial_return_save (XEXP (rtl, 0));
1013 return;
1015 default:
1016 gcc_unreachable ();
1019 if (reg != DWARF_FRAME_RETURN_COLUMN)
1020 reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1022 #endif
1024 /* Given a SET, calculate the amount of stack adjustment it
1025 contains. */
1027 static HOST_WIDE_INT
1028 stack_adjust_offset (const_rtx pattern)
1030 const_rtx src = SET_SRC (pattern);
1031 const_rtx dest = SET_DEST (pattern);
1032 HOST_WIDE_INT offset = 0;
1033 enum rtx_code code;
1035 if (dest == stack_pointer_rtx)
1037 /* (set (reg sp) (plus (reg sp) (const_int))) */
1038 code = GET_CODE (src);
1039 if (! (code == PLUS || code == MINUS)
1040 || XEXP (src, 0) != stack_pointer_rtx
1041 || GET_CODE (XEXP (src, 1)) != CONST_INT)
1042 return 0;
1044 offset = INTVAL (XEXP (src, 1));
1045 if (code == PLUS)
1046 offset = -offset;
1048 else if (MEM_P (dest))
1050 /* (set (mem (pre_dec (reg sp))) (foo)) */
1051 src = XEXP (dest, 0);
1052 code = GET_CODE (src);
1054 switch (code)
1056 case PRE_MODIFY:
1057 case POST_MODIFY:
1058 if (XEXP (src, 0) == stack_pointer_rtx)
1060 rtx val = XEXP (XEXP (src, 1), 1);
1061 /* We handle only adjustments by constant amount. */
1062 gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1063 && GET_CODE (val) == CONST_INT);
1064 offset = -INTVAL (val);
1065 break;
1067 return 0;
1069 case PRE_DEC:
1070 case POST_DEC:
1071 if (XEXP (src, 0) == stack_pointer_rtx)
1073 offset = GET_MODE_SIZE (GET_MODE (dest));
1074 break;
1076 return 0;
1078 case PRE_INC:
1079 case POST_INC:
1080 if (XEXP (src, 0) == stack_pointer_rtx)
1082 offset = -GET_MODE_SIZE (GET_MODE (dest));
1083 break;
1085 return 0;
1087 default:
1088 return 0;
1091 else
1092 return 0;
1094 return offset;
1097 /* Check INSN to see if it looks like a push or a stack adjustment, and
1098 make a note of it if it does. EH uses this information to find out how
1099 much extra space it needs to pop off the stack. */
1101 static void
1102 dwarf2out_stack_adjust (rtx insn, bool after_p)
1104 HOST_WIDE_INT offset;
1105 const char *label;
1106 int i;
1108 /* Don't handle epilogues at all. Certainly it would be wrong to do so
1109 with this function. Proper support would require all frame-related
1110 insns to be marked, and to be able to handle saving state around
1111 epilogues textually in the middle of the function. */
1112 if (prologue_epilogue_contains (insn) || sibcall_epilogue_contains (insn))
1113 return;
1115 /* If only calls can throw, and we have a frame pointer,
1116 save up adjustments until we see the CALL_INSN. */
1117 if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1119 if (CALL_P (insn) && !after_p)
1121 /* Extract the size of the args from the CALL rtx itself. */
1122 insn = PATTERN (insn);
1123 if (GET_CODE (insn) == PARALLEL)
1124 insn = XVECEXP (insn, 0, 0);
1125 if (GET_CODE (insn) == SET)
1126 insn = SET_SRC (insn);
1127 gcc_assert (GET_CODE (insn) == CALL);
1128 dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1130 return;
1133 if (CALL_P (insn) && !after_p)
1135 if (!flag_asynchronous_unwind_tables)
1136 dwarf2out_args_size ("", args_size);
1137 return;
1139 else if (BARRIER_P (insn))
1141 /* When we see a BARRIER, we know to reset args_size to 0. Usually
1142 the compiler will have already emitted a stack adjustment, but
1143 doesn't bother for calls to noreturn functions. */
1144 #ifdef STACK_GROWS_DOWNWARD
1145 offset = -args_size;
1146 #else
1147 offset = args_size;
1148 #endif
1150 else if (GET_CODE (PATTERN (insn)) == SET)
1151 offset = stack_adjust_offset (PATTERN (insn));
1152 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1153 || GET_CODE (PATTERN (insn)) == SEQUENCE)
1155 /* There may be stack adjustments inside compound insns. Search
1156 for them. */
1157 for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1158 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1159 offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i));
1161 else
1162 return;
1164 if (offset == 0)
1165 return;
1167 if (cfa.reg == STACK_POINTER_REGNUM)
1168 cfa.offset += offset;
1170 #ifndef STACK_GROWS_DOWNWARD
1171 offset = -offset;
1172 #endif
1174 args_size += offset;
1175 if (args_size < 0)
1176 args_size = 0;
1178 label = dwarf2out_cfi_label ();
1179 def_cfa_1 (label, &cfa);
1180 if (flag_asynchronous_unwind_tables)
1181 dwarf2out_args_size (label, args_size);
1184 #endif
1186 /* We delay emitting a register save until either (a) we reach the end
1187 of the prologue or (b) the register is clobbered. This clusters
1188 register saves so that there are fewer pc advances. */
1190 struct queued_reg_save GTY(())
1192 struct queued_reg_save *next;
1193 rtx reg;
1194 HOST_WIDE_INT cfa_offset;
1195 rtx saved_reg;
1198 static GTY(()) struct queued_reg_save *queued_reg_saves;
1200 /* The caller's ORIG_REG is saved in SAVED_IN_REG. */
1201 struct reg_saved_in_data GTY(()) {
1202 rtx orig_reg;
1203 rtx saved_in_reg;
1206 /* A list of registers saved in other registers.
1207 The list intentionally has a small maximum capacity of 4; if your
1208 port needs more than that, you might consider implementing a
1209 more efficient data structure. */
1210 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1211 static GTY(()) size_t num_regs_saved_in_regs;
1213 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1214 static const char *last_reg_save_label;
1216 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1217 SREG, or if SREG is NULL then it is saved at OFFSET to the CFA. */
1219 static void
1220 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1222 struct queued_reg_save *q;
1224 /* Duplicates waste space, but it's also necessary to remove them
1225 for correctness, since the queue gets output in reverse
1226 order. */
1227 for (q = queued_reg_saves; q != NULL; q = q->next)
1228 if (REGNO (q->reg) == REGNO (reg))
1229 break;
1231 if (q == NULL)
1233 q = ggc_alloc (sizeof (*q));
1234 q->next = queued_reg_saves;
1235 queued_reg_saves = q;
1238 q->reg = reg;
1239 q->cfa_offset = offset;
1240 q->saved_reg = sreg;
1242 last_reg_save_label = label;
1245 /* Output all the entries in QUEUED_REG_SAVES. */
1247 static void
1248 flush_queued_reg_saves (void)
1250 struct queued_reg_save *q;
1252 for (q = queued_reg_saves; q; q = q->next)
1254 size_t i;
1255 unsigned int reg, sreg;
1257 for (i = 0; i < num_regs_saved_in_regs; i++)
1258 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1259 break;
1260 if (q->saved_reg && i == num_regs_saved_in_regs)
1262 gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1263 num_regs_saved_in_regs++;
1265 if (i != num_regs_saved_in_regs)
1267 regs_saved_in_regs[i].orig_reg = q->reg;
1268 regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1271 reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1272 if (q->saved_reg)
1273 sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1274 else
1275 sreg = INVALID_REGNUM;
1276 reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1279 queued_reg_saves = NULL;
1280 last_reg_save_label = NULL;
1283 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1284 location for? Or, does it clobber a register which we've previously
1285 said that some other register is saved in, and for which we now
1286 have a new location for? */
1288 static bool
1289 clobbers_queued_reg_save (const_rtx insn)
1291 struct queued_reg_save *q;
1293 for (q = queued_reg_saves; q; q = q->next)
1295 size_t i;
1296 if (modified_in_p (q->reg, insn))
1297 return true;
1298 for (i = 0; i < num_regs_saved_in_regs; i++)
1299 if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1300 && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1301 return true;
1304 return false;
1307 /* Entry point for saving the first register into the second. */
1309 void
1310 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1312 size_t i;
1313 unsigned int regno, sregno;
1315 for (i = 0; i < num_regs_saved_in_regs; i++)
1316 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1317 break;
1318 if (i == num_regs_saved_in_regs)
1320 gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1321 num_regs_saved_in_regs++;
1323 regs_saved_in_regs[i].orig_reg = reg;
1324 regs_saved_in_regs[i].saved_in_reg = sreg;
1326 regno = DWARF_FRAME_REGNUM (REGNO (reg));
1327 sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1328 reg_save (label, regno, sregno, 0);
1331 /* What register, if any, is currently saved in REG? */
1333 static rtx
1334 reg_saved_in (rtx reg)
1336 unsigned int regn = REGNO (reg);
1337 size_t i;
1338 struct queued_reg_save *q;
1340 for (q = queued_reg_saves; q; q = q->next)
1341 if (q->saved_reg && regn == REGNO (q->saved_reg))
1342 return q->reg;
1344 for (i = 0; i < num_regs_saved_in_regs; i++)
1345 if (regs_saved_in_regs[i].saved_in_reg
1346 && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1347 return regs_saved_in_regs[i].orig_reg;
1349 return NULL_RTX;
1353 /* A temporary register holding an integral value used in adjusting SP
1354 or setting up the store_reg. The "offset" field holds the integer
1355 value, not an offset. */
1356 static dw_cfa_location cfa_temp;
1358 /* Record call frame debugging information for an expression EXPR,
1359 which either sets SP or FP (adjusting how we calculate the frame
1360 address) or saves a register to the stack or another register.
1361 LABEL indicates the address of EXPR.
1363 This function encodes a state machine mapping rtxes to actions on
1364 cfa, cfa_store, and cfa_temp.reg. We describe these rules so
1365 users need not read the source code.
1367 The High-Level Picture
1369 Changes in the register we use to calculate the CFA: Currently we
1370 assume that if you copy the CFA register into another register, we
1371 should take the other one as the new CFA register; this seems to
1372 work pretty well. If it's wrong for some target, it's simple
1373 enough not to set RTX_FRAME_RELATED_P on the insn in question.
1375 Changes in the register we use for saving registers to the stack:
1376 This is usually SP, but not always. Again, we deduce that if you
1377 copy SP into another register (and SP is not the CFA register),
1378 then the new register is the one we will be using for register
1379 saves. This also seems to work.
1381 Register saves: There's not much guesswork about this one; if
1382 RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1383 register save, and the register used to calculate the destination
1384 had better be the one we think we're using for this purpose.
1385 It's also assumed that a copy from a call-saved register to another
1386 register is saving that register if RTX_FRAME_RELATED_P is set on
1387 that instruction. If the copy is from a call-saved register to
1388 the *same* register, that means that the register is now the same
1389 value as in the caller.
1391 Except: If the register being saved is the CFA register, and the
1392 offset is nonzero, we are saving the CFA, so we assume we have to
1393 use DW_CFA_def_cfa_expression. If the offset is 0, we assume that
1394 the intent is to save the value of SP from the previous frame.
1396 In addition, if a register has previously been saved to a different
1397 register,
1399 Invariants / Summaries of Rules
1401 cfa current rule for calculating the CFA. It usually
1402 consists of a register and an offset.
1403 cfa_store register used by prologue code to save things to the stack
1404 cfa_store.offset is the offset from the value of
1405 cfa_store.reg to the actual CFA
1406 cfa_temp register holding an integral value. cfa_temp.offset
1407 stores the value, which will be used to adjust the
1408 stack pointer. cfa_temp is also used like cfa_store,
1409 to track stores to the stack via fp or a temp reg.
1411 Rules 1- 4: Setting a register's value to cfa.reg or an expression
1412 with cfa.reg as the first operand changes the cfa.reg and its
1413 cfa.offset. Rule 1 and 4 also set cfa_temp.reg and
1414 cfa_temp.offset.
1416 Rules 6- 9: Set a non-cfa.reg register value to a constant or an
1417 expression yielding a constant. This sets cfa_temp.reg
1418 and cfa_temp.offset.
1420 Rule 5: Create a new register cfa_store used to save items to the
1421 stack.
1423 Rules 10-14: Save a register to the stack. Define offset as the
1424 difference of the original location and cfa_store's
1425 location (or cfa_temp's location if cfa_temp is used).
1427 The Rules
1429 "{a,b}" indicates a choice of a xor b.
1430 "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1432 Rule 1:
1433 (set <reg1> <reg2>:cfa.reg)
1434 effects: cfa.reg = <reg1>
1435 cfa.offset unchanged
1436 cfa_temp.reg = <reg1>
1437 cfa_temp.offset = cfa.offset
1439 Rule 2:
1440 (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1441 {<const_int>,<reg>:cfa_temp.reg}))
1442 effects: cfa.reg = sp if fp used
1443 cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
1444 cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1445 if cfa_store.reg==sp
1447 Rule 3:
1448 (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
1449 effects: cfa.reg = fp
1450 cfa_offset += +/- <const_int>
1452 Rule 4:
1453 (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
1454 constraints: <reg1> != fp
1455 <reg1> != sp
1456 effects: cfa.reg = <reg1>
1457 cfa_temp.reg = <reg1>
1458 cfa_temp.offset = cfa.offset
1460 Rule 5:
1461 (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1462 constraints: <reg1> != fp
1463 <reg1> != sp
1464 effects: cfa_store.reg = <reg1>
1465 cfa_store.offset = cfa.offset - cfa_temp.offset
1467 Rule 6:
1468 (set <reg> <const_int>)
1469 effects: cfa_temp.reg = <reg>
1470 cfa_temp.offset = <const_int>
1472 Rule 7:
1473 (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1474 effects: cfa_temp.reg = <reg1>
1475 cfa_temp.offset |= <const_int>
1477 Rule 8:
1478 (set <reg> (high <exp>))
1479 effects: none
1481 Rule 9:
1482 (set <reg> (lo_sum <exp> <const_int>))
1483 effects: cfa_temp.reg = <reg>
1484 cfa_temp.offset = <const_int>
1486 Rule 10:
1487 (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1488 effects: cfa_store.offset -= <const_int>
1489 cfa.offset = cfa_store.offset if cfa.reg == sp
1490 cfa.reg = sp
1491 cfa.base_offset = -cfa_store.offset
1493 Rule 11:
1494 (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1495 effects: cfa_store.offset += -/+ mode_size(mem)
1496 cfa.offset = cfa_store.offset if cfa.reg == sp
1497 cfa.reg = sp
1498 cfa.base_offset = -cfa_store.offset
1500 Rule 12:
1501 (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1503 <reg2>)
1504 effects: cfa.reg = <reg1>
1505 cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
1507 Rule 13:
1508 (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1509 effects: cfa.reg = <reg1>
1510 cfa.base_offset = -{cfa_store,cfa_temp}.offset
1512 Rule 14:
1513 (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
1514 effects: cfa.reg = <reg1>
1515 cfa.base_offset = -cfa_temp.offset
1516 cfa_temp.offset -= mode_size(mem)
1518 Rule 15:
1519 (set <reg> {unspec, unspec_volatile})
1520 effects: target-dependent */
1522 static void
1523 dwarf2out_frame_debug_expr (rtx expr, const char *label)
1525 rtx src, dest;
1526 HOST_WIDE_INT offset;
1528 /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1529 the PARALLEL independently. The first element is always processed if
1530 it is a SET. This is for backward compatibility. Other elements
1531 are processed only if they are SETs and the RTX_FRAME_RELATED_P
1532 flag is set in them. */
1533 if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
1535 int par_index;
1536 int limit = XVECLEN (expr, 0);
1537 rtx elem;
1539 /* PARALLELs have strict read-modify-write semantics, so we
1540 ought to evaluate every rvalue before changing any lvalue.
1541 It's cumbersome to do that in general, but there's an
1542 easy approximation that is enough for all current users:
1543 handle register saves before register assignments. */
1544 if (GET_CODE (expr) == PARALLEL)
1545 for (par_index = 0; par_index < limit; par_index++)
1547 elem = XVECEXP (expr, 0, par_index);
1548 if (GET_CODE (elem) == SET
1549 && MEM_P (SET_DEST (elem))
1550 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1551 dwarf2out_frame_debug_expr (elem, label);
1554 for (par_index = 0; par_index < limit; par_index++)
1556 elem = XVECEXP (expr, 0, par_index);
1557 if (GET_CODE (elem) == SET
1558 && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
1559 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1560 dwarf2out_frame_debug_expr (elem, label);
1562 return;
1565 gcc_assert (GET_CODE (expr) == SET);
1567 src = SET_SRC (expr);
1568 dest = SET_DEST (expr);
1570 if (REG_P (src))
1572 rtx rsi = reg_saved_in (src);
1573 if (rsi)
1574 src = rsi;
1577 switch (GET_CODE (dest))
1579 case REG:
1580 switch (GET_CODE (src))
1582 /* Setting FP from SP. */
1583 case REG:
1584 if (cfa.reg == (unsigned) REGNO (src))
1586 /* Rule 1 */
1587 /* Update the CFA rule wrt SP or FP. Make sure src is
1588 relative to the current CFA register.
1590 We used to require that dest be either SP or FP, but the
1591 ARM copies SP to a temporary register, and from there to
1592 FP. So we just rely on the backends to only set
1593 RTX_FRAME_RELATED_P on appropriate insns. */
1594 cfa.reg = REGNO (dest);
1595 cfa_temp.reg = cfa.reg;
1596 cfa_temp.offset = cfa.offset;
1598 else
1600 /* Saving a register in a register. */
1601 gcc_assert (!fixed_regs [REGNO (dest)]
1602 /* For the SPARC and its register window. */
1603 || (DWARF_FRAME_REGNUM (REGNO (src))
1604 == DWARF_FRAME_RETURN_COLUMN));
1605 queue_reg_save (label, src, dest, 0);
1607 break;
1609 case PLUS:
1610 case MINUS:
1611 case LO_SUM:
1612 if (dest == stack_pointer_rtx)
1614 /* Rule 2 */
1615 /* Adjusting SP. */
1616 switch (GET_CODE (XEXP (src, 1)))
1618 case CONST_INT:
1619 offset = INTVAL (XEXP (src, 1));
1620 break;
1621 case REG:
1622 gcc_assert ((unsigned) REGNO (XEXP (src, 1))
1623 == cfa_temp.reg);
1624 offset = cfa_temp.offset;
1625 break;
1626 default:
1627 gcc_unreachable ();
1630 if (XEXP (src, 0) == hard_frame_pointer_rtx)
1632 /* Restoring SP from FP in the epilogue. */
1633 gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
1634 cfa.reg = STACK_POINTER_REGNUM;
1636 else if (GET_CODE (src) == LO_SUM)
1637 /* Assume we've set the source reg of the LO_SUM from sp. */
1639 else
1640 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
1642 if (GET_CODE (src) != MINUS)
1643 offset = -offset;
1644 if (cfa.reg == STACK_POINTER_REGNUM)
1645 cfa.offset += offset;
1646 if (cfa_store.reg == STACK_POINTER_REGNUM)
1647 cfa_store.offset += offset;
1649 else if (dest == hard_frame_pointer_rtx)
1651 /* Rule 3 */
1652 /* Either setting the FP from an offset of the SP,
1653 or adjusting the FP */
1654 gcc_assert (frame_pointer_needed);
1656 gcc_assert (REG_P (XEXP (src, 0))
1657 && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
1658 && GET_CODE (XEXP (src, 1)) == CONST_INT);
1659 offset = INTVAL (XEXP (src, 1));
1660 if (GET_CODE (src) != MINUS)
1661 offset = -offset;
1662 cfa.offset += offset;
1663 cfa.reg = HARD_FRAME_POINTER_REGNUM;
1665 else
1667 gcc_assert (GET_CODE (src) != MINUS);
1669 /* Rule 4 */
1670 if (REG_P (XEXP (src, 0))
1671 && REGNO (XEXP (src, 0)) == cfa.reg
1672 && GET_CODE (XEXP (src, 1)) == CONST_INT)
1674 /* Setting a temporary CFA register that will be copied
1675 into the FP later on. */
1676 offset = - INTVAL (XEXP (src, 1));
1677 cfa.offset += offset;
1678 cfa.reg = REGNO (dest);
1679 /* Or used to save regs to the stack. */
1680 cfa_temp.reg = cfa.reg;
1681 cfa_temp.offset = cfa.offset;
1684 /* Rule 5 */
1685 else if (REG_P (XEXP (src, 0))
1686 && REGNO (XEXP (src, 0)) == cfa_temp.reg
1687 && XEXP (src, 1) == stack_pointer_rtx)
1689 /* Setting a scratch register that we will use instead
1690 of SP for saving registers to the stack. */
1691 gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
1692 cfa_store.reg = REGNO (dest);
1693 cfa_store.offset = cfa.offset - cfa_temp.offset;
1696 /* Rule 9 */
1697 else if (GET_CODE (src) == LO_SUM
1698 && GET_CODE (XEXP (src, 1)) == CONST_INT)
1700 cfa_temp.reg = REGNO (dest);
1701 cfa_temp.offset = INTVAL (XEXP (src, 1));
1703 else
1704 gcc_unreachable ();
1706 break;
1708 /* Rule 6 */
1709 case CONST_INT:
1710 cfa_temp.reg = REGNO (dest);
1711 cfa_temp.offset = INTVAL (src);
1712 break;
1714 /* Rule 7 */
1715 case IOR:
1716 gcc_assert (REG_P (XEXP (src, 0))
1717 && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
1718 && GET_CODE (XEXP (src, 1)) == CONST_INT);
1720 if ((unsigned) REGNO (dest) != cfa_temp.reg)
1721 cfa_temp.reg = REGNO (dest);
1722 cfa_temp.offset |= INTVAL (XEXP (src, 1));
1723 break;
1725 /* Skip over HIGH, assuming it will be followed by a LO_SUM,
1726 which will fill in all of the bits. */
1727 /* Rule 8 */
1728 case HIGH:
1729 break;
1731 /* Rule 15 */
1732 case UNSPEC:
1733 case UNSPEC_VOLATILE:
1734 gcc_assert (targetm.dwarf_handle_frame_unspec);
1735 targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
1736 return;
1738 default:
1739 gcc_unreachable ();
1742 def_cfa_1 (label, &cfa);
1743 break;
1745 case MEM:
1746 gcc_assert (REG_P (src));
1748 /* Saving a register to the stack. Make sure dest is relative to the
1749 CFA register. */
1750 switch (GET_CODE (XEXP (dest, 0)))
1752 /* Rule 10 */
1753 /* With a push. */
1754 case PRE_MODIFY:
1755 /* We can't handle variable size modifications. */
1756 gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
1757 == CONST_INT);
1758 offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
1760 gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1761 && cfa_store.reg == STACK_POINTER_REGNUM);
1763 cfa_store.offset += offset;
1764 if (cfa.reg == STACK_POINTER_REGNUM)
1765 cfa.offset = cfa_store.offset;
1767 offset = -cfa_store.offset;
1768 break;
1770 /* Rule 11 */
1771 case PRE_INC:
1772 case PRE_DEC:
1773 offset = GET_MODE_SIZE (GET_MODE (dest));
1774 if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
1775 offset = -offset;
1777 gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1778 && cfa_store.reg == STACK_POINTER_REGNUM);
1780 cfa_store.offset += offset;
1781 if (cfa.reg == STACK_POINTER_REGNUM)
1782 cfa.offset = cfa_store.offset;
1784 offset = -cfa_store.offset;
1785 break;
1787 /* Rule 12 */
1788 /* With an offset. */
1789 case PLUS:
1790 case MINUS:
1791 case LO_SUM:
1793 int regno;
1795 gcc_assert (GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT
1796 && REG_P (XEXP (XEXP (dest, 0), 0)));
1797 offset = INTVAL (XEXP (XEXP (dest, 0), 1));
1798 if (GET_CODE (XEXP (dest, 0)) == MINUS)
1799 offset = -offset;
1801 regno = REGNO (XEXP (XEXP (dest, 0), 0));
1803 if (cfa_store.reg == (unsigned) regno)
1804 offset -= cfa_store.offset;
1805 else
1807 gcc_assert (cfa_temp.reg == (unsigned) regno);
1808 offset -= cfa_temp.offset;
1811 break;
1813 /* Rule 13 */
1814 /* Without an offset. */
1815 case REG:
1817 int regno = REGNO (XEXP (dest, 0));
1819 if (cfa_store.reg == (unsigned) regno)
1820 offset = -cfa_store.offset;
1821 else
1823 gcc_assert (cfa_temp.reg == (unsigned) regno);
1824 offset = -cfa_temp.offset;
1827 break;
1829 /* Rule 14 */
1830 case POST_INC:
1831 gcc_assert (cfa_temp.reg
1832 == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
1833 offset = -cfa_temp.offset;
1834 cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
1835 break;
1837 default:
1838 gcc_unreachable ();
1841 if (REGNO (src) != STACK_POINTER_REGNUM
1842 && REGNO (src) != HARD_FRAME_POINTER_REGNUM
1843 && (unsigned) REGNO (src) == cfa.reg)
1845 /* We're storing the current CFA reg into the stack. */
1847 if (cfa.offset == 0)
1849 /* If the source register is exactly the CFA, assume
1850 we're saving SP like any other register; this happens
1851 on the ARM. */
1852 def_cfa_1 (label, &cfa);
1853 queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
1854 break;
1856 else
1858 /* Otherwise, we'll need to look in the stack to
1859 calculate the CFA. */
1860 rtx x = XEXP (dest, 0);
1862 if (!REG_P (x))
1863 x = XEXP (x, 0);
1864 gcc_assert (REG_P (x));
1866 cfa.reg = REGNO (x);
1867 cfa.base_offset = offset;
1868 cfa.indirect = 1;
1869 def_cfa_1 (label, &cfa);
1870 break;
1874 def_cfa_1 (label, &cfa);
1875 queue_reg_save (label, src, NULL_RTX, offset);
1876 break;
1878 default:
1879 gcc_unreachable ();
1883 /* Record call frame debugging information for INSN, which either
1884 sets SP or FP (adjusting how we calculate the frame address) or saves a
1885 register to the stack. If INSN is NULL_RTX, initialize our state.
1887 If AFTER_P is false, we're being called before the insn is emitted,
1888 otherwise after. Call instructions get invoked twice. */
1890 void
1891 dwarf2out_frame_debug (rtx insn, bool after_p)
1893 const char *label;
1894 rtx src;
1896 if (insn == NULL_RTX)
1898 size_t i;
1900 /* Flush any queued register saves. */
1901 flush_queued_reg_saves ();
1903 /* Set up state for generating call frame debug info. */
1904 lookup_cfa (&cfa);
1905 gcc_assert (cfa.reg
1906 == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
1908 cfa.reg = STACK_POINTER_REGNUM;
1909 cfa_store = cfa;
1910 cfa_temp.reg = -1;
1911 cfa_temp.offset = 0;
1913 for (i = 0; i < num_regs_saved_in_regs; i++)
1915 regs_saved_in_regs[i].orig_reg = NULL_RTX;
1916 regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
1918 num_regs_saved_in_regs = 0;
1919 return;
1922 if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
1923 flush_queued_reg_saves ();
1925 if (! RTX_FRAME_RELATED_P (insn))
1927 if (!ACCUMULATE_OUTGOING_ARGS)
1928 dwarf2out_stack_adjust (insn, after_p);
1929 return;
1932 label = dwarf2out_cfi_label ();
1933 src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1934 if (src)
1935 insn = XEXP (src, 0);
1936 else
1937 insn = PATTERN (insn);
1939 dwarf2out_frame_debug_expr (insn, label);
1942 #endif
1944 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
1945 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
1946 (enum dwarf_call_frame_info cfi);
1948 static enum dw_cfi_oprnd_type
1949 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
1951 switch (cfi)
1953 case DW_CFA_nop:
1954 case DW_CFA_GNU_window_save:
1955 return dw_cfi_oprnd_unused;
1957 case DW_CFA_set_loc:
1958 case DW_CFA_advance_loc1:
1959 case DW_CFA_advance_loc2:
1960 case DW_CFA_advance_loc4:
1961 case DW_CFA_MIPS_advance_loc8:
1962 return dw_cfi_oprnd_addr;
1964 case DW_CFA_offset:
1965 case DW_CFA_offset_extended:
1966 case DW_CFA_def_cfa:
1967 case DW_CFA_offset_extended_sf:
1968 case DW_CFA_def_cfa_sf:
1969 case DW_CFA_restore_extended:
1970 case DW_CFA_undefined:
1971 case DW_CFA_same_value:
1972 case DW_CFA_def_cfa_register:
1973 case DW_CFA_register:
1974 return dw_cfi_oprnd_reg_num;
1976 case DW_CFA_def_cfa_offset:
1977 case DW_CFA_GNU_args_size:
1978 case DW_CFA_def_cfa_offset_sf:
1979 return dw_cfi_oprnd_offset;
1981 case DW_CFA_def_cfa_expression:
1982 case DW_CFA_expression:
1983 return dw_cfi_oprnd_loc;
1985 default:
1986 gcc_unreachable ();
1990 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
1991 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
1992 (enum dwarf_call_frame_info cfi);
1994 static enum dw_cfi_oprnd_type
1995 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
1997 switch (cfi)
1999 case DW_CFA_def_cfa:
2000 case DW_CFA_def_cfa_sf:
2001 case DW_CFA_offset:
2002 case DW_CFA_offset_extended_sf:
2003 case DW_CFA_offset_extended:
2004 return dw_cfi_oprnd_offset;
2006 case DW_CFA_register:
2007 return dw_cfi_oprnd_reg_num;
2009 default:
2010 return dw_cfi_oprnd_unused;
2014 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2016 /* Switch to eh_frame_section. If we don't have an eh_frame_section,
2017 switch to the data section instead, and write out a synthetic label
2018 for collect2. */
2020 static void
2021 switch_to_eh_frame_section (void)
2023 tree label;
2025 #ifdef EH_FRAME_SECTION_NAME
2026 if (eh_frame_section == 0)
2028 int flags;
2030 if (EH_TABLES_CAN_BE_READ_ONLY)
2032 int fde_encoding;
2033 int per_encoding;
2034 int lsda_encoding;
2036 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2037 /*global=*/0);
2038 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2039 /*global=*/1);
2040 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2041 /*global=*/0);
2042 flags = ((! flag_pic
2043 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2044 && (fde_encoding & 0x70) != DW_EH_PE_aligned
2045 && (per_encoding & 0x70) != DW_EH_PE_absptr
2046 && (per_encoding & 0x70) != DW_EH_PE_aligned
2047 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2048 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2049 ? 0 : SECTION_WRITE);
2051 else
2052 flags = SECTION_WRITE;
2053 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2055 #endif
2057 if (eh_frame_section)
2058 switch_to_section (eh_frame_section);
2059 else
2061 /* We have no special eh_frame section. Put the information in
2062 the data section and emit special labels to guide collect2. */
2063 switch_to_section (data_section);
2064 label = get_file_function_name ("F");
2065 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2066 targetm.asm_out.globalize_label (asm_out_file,
2067 IDENTIFIER_POINTER (label));
2068 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
2072 /* Output a Call Frame Information opcode and its operand(s). */
2074 static void
2075 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
2077 unsigned long r;
2078 if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
2079 dw2_asm_output_data (1, (cfi->dw_cfi_opc
2080 | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
2081 "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
2082 cfi->dw_cfi_oprnd1.dw_cfi_offset);
2083 else if (cfi->dw_cfi_opc == DW_CFA_offset)
2085 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2086 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2087 "DW_CFA_offset, column 0x%lx", r);
2088 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2090 else if (cfi->dw_cfi_opc == DW_CFA_restore)
2092 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2093 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2094 "DW_CFA_restore, column 0x%lx", r);
2096 else
2098 dw2_asm_output_data (1, cfi->dw_cfi_opc,
2099 "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
2101 switch (cfi->dw_cfi_opc)
2103 case DW_CFA_set_loc:
2104 if (for_eh)
2105 dw2_asm_output_encoded_addr_rtx (
2106 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
2107 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
2108 false, NULL);
2109 else
2110 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2111 cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
2112 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2113 break;
2115 case DW_CFA_advance_loc1:
2116 dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2117 fde->dw_fde_current_label, NULL);
2118 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2119 break;
2121 case DW_CFA_advance_loc2:
2122 dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2123 fde->dw_fde_current_label, NULL);
2124 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2125 break;
2127 case DW_CFA_advance_loc4:
2128 dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2129 fde->dw_fde_current_label, NULL);
2130 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2131 break;
2133 case DW_CFA_MIPS_advance_loc8:
2134 dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2135 fde->dw_fde_current_label, NULL);
2136 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2137 break;
2139 case DW_CFA_offset_extended:
2140 case DW_CFA_def_cfa:
2141 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2142 dw2_asm_output_data_uleb128 (r, NULL);
2143 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2144 break;
2146 case DW_CFA_offset_extended_sf:
2147 case DW_CFA_def_cfa_sf:
2148 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2149 dw2_asm_output_data_uleb128 (r, NULL);
2150 dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2151 break;
2153 case DW_CFA_restore_extended:
2154 case DW_CFA_undefined:
2155 case DW_CFA_same_value:
2156 case DW_CFA_def_cfa_register:
2157 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2158 dw2_asm_output_data_uleb128 (r, NULL);
2159 break;
2161 case DW_CFA_register:
2162 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2163 dw2_asm_output_data_uleb128 (r, NULL);
2164 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
2165 dw2_asm_output_data_uleb128 (r, NULL);
2166 break;
2168 case DW_CFA_def_cfa_offset:
2169 case DW_CFA_GNU_args_size:
2170 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2171 break;
2173 case DW_CFA_def_cfa_offset_sf:
2174 dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2175 break;
2177 case DW_CFA_GNU_window_save:
2178 break;
2180 case DW_CFA_def_cfa_expression:
2181 case DW_CFA_expression:
2182 output_cfa_loc (cfi);
2183 break;
2185 case DW_CFA_GNU_negative_offset_extended:
2186 /* Obsoleted by DW_CFA_offset_extended_sf. */
2187 gcc_unreachable ();
2189 default:
2190 break;
2195 /* Output the call frame information used to record information
2196 that relates to calculating the frame pointer, and records the
2197 location of saved registers. */
2199 static void
2200 output_call_frame_info (int for_eh)
2202 unsigned int i;
2203 dw_fde_ref fde;
2204 dw_cfi_ref cfi;
2205 char l1[20], l2[20], section_start_label[20];
2206 bool any_lsda_needed = false;
2207 char augmentation[6];
2208 int augmentation_size;
2209 int fde_encoding = DW_EH_PE_absptr;
2210 int per_encoding = DW_EH_PE_absptr;
2211 int lsda_encoding = DW_EH_PE_absptr;
2212 int return_reg;
2214 /* Don't emit a CIE if there won't be any FDEs. */
2215 if (fde_table_in_use == 0)
2216 return;
2218 /* If we make FDEs linkonce, we may have to emit an empty label for
2219 an FDE that wouldn't otherwise be emitted. We want to avoid
2220 having an FDE kept around when the function it refers to is
2221 discarded. Example where this matters: a primary function
2222 template in C++ requires EH information, but an explicit
2223 specialization doesn't. */
2224 if (TARGET_USES_WEAK_UNWIND_INFO
2225 && ! flag_asynchronous_unwind_tables
2226 && for_eh)
2227 for (i = 0; i < fde_table_in_use; i++)
2228 if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
2229 && !fde_table[i].uses_eh_lsda
2230 && ! DECL_WEAK (fde_table[i].decl))
2231 targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
2232 for_eh, /* empty */ 1);
2234 /* If we don't have any functions we'll want to unwind out of, don't
2235 emit any EH unwind information. Note that if exceptions aren't
2236 enabled, we won't have collected nothrow information, and if we
2237 asked for asynchronous tables, we always want this info. */
2238 if (for_eh)
2240 bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
2242 for (i = 0; i < fde_table_in_use; i++)
2243 if (fde_table[i].uses_eh_lsda)
2244 any_eh_needed = any_lsda_needed = true;
2245 else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2246 any_eh_needed = true;
2247 else if (! fde_table[i].nothrow
2248 && ! fde_table[i].all_throwers_are_sibcalls)
2249 any_eh_needed = true;
2251 if (! any_eh_needed)
2252 return;
2255 /* We're going to be generating comments, so turn on app. */
2256 if (flag_debug_asm)
2257 app_enable ();
2259 if (for_eh)
2260 switch_to_eh_frame_section ();
2261 else
2263 if (!debug_frame_section)
2264 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
2265 SECTION_DEBUG, NULL);
2266 switch_to_section (debug_frame_section);
2269 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
2270 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
2272 /* Output the CIE. */
2273 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
2274 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
2275 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2276 dw2_asm_output_data (4, 0xffffffff,
2277 "Initial length escape value indicating 64-bit DWARF extension");
2278 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2279 "Length of Common Information Entry");
2280 ASM_OUTPUT_LABEL (asm_out_file, l1);
2282 /* Now that the CIE pointer is PC-relative for EH,
2283 use 0 to identify the CIE. */
2284 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
2285 (for_eh ? 0 : DWARF_CIE_ID),
2286 "CIE Identifier Tag");
2288 dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
2290 augmentation[0] = 0;
2291 augmentation_size = 0;
2292 if (for_eh)
2294 char *p;
2296 /* Augmentation:
2297 z Indicates that a uleb128 is present to size the
2298 augmentation section.
2299 L Indicates the encoding (and thus presence) of
2300 an LSDA pointer in the FDE augmentation.
2301 R Indicates a non-default pointer encoding for
2302 FDE code pointers.
2303 P Indicates the presence of an encoding + language
2304 personality routine in the CIE augmentation. */
2306 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
2307 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
2308 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
2310 p = augmentation + 1;
2311 if (eh_personality_libfunc)
2313 *p++ = 'P';
2314 augmentation_size += 1 + size_of_encoded_value (per_encoding);
2315 assemble_external_libcall (eh_personality_libfunc);
2317 if (any_lsda_needed)
2319 *p++ = 'L';
2320 augmentation_size += 1;
2322 if (fde_encoding != DW_EH_PE_absptr)
2324 *p++ = 'R';
2325 augmentation_size += 1;
2327 if (p > augmentation + 1)
2329 augmentation[0] = 'z';
2330 *p = '\0';
2333 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
2334 if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
2336 int offset = ( 4 /* Length */
2337 + 4 /* CIE Id */
2338 + 1 /* CIE version */
2339 + strlen (augmentation) + 1 /* Augmentation */
2340 + size_of_uleb128 (1) /* Code alignment */
2341 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
2342 + 1 /* RA column */
2343 + 1 /* Augmentation size */
2344 + 1 /* Personality encoding */ );
2345 int pad = -offset & (PTR_SIZE - 1);
2347 augmentation_size += pad;
2349 /* Augmentations should be small, so there's scarce need to
2350 iterate for a solution. Die if we exceed one uleb128 byte. */
2351 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
2355 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
2356 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
2357 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
2358 "CIE Data Alignment Factor");
2360 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
2361 if (DW_CIE_VERSION == 1)
2362 dw2_asm_output_data (1, return_reg, "CIE RA Column");
2363 else
2364 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
2366 if (augmentation[0])
2368 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
2369 if (eh_personality_libfunc)
2371 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
2372 eh_data_format_name (per_encoding));
2373 dw2_asm_output_encoded_addr_rtx (per_encoding,
2374 eh_personality_libfunc,
2375 true, NULL);
2378 if (any_lsda_needed)
2379 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
2380 eh_data_format_name (lsda_encoding));
2382 if (fde_encoding != DW_EH_PE_absptr)
2383 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
2384 eh_data_format_name (fde_encoding));
2387 for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
2388 output_cfi (cfi, NULL, for_eh);
2390 /* Pad the CIE out to an address sized boundary. */
2391 ASM_OUTPUT_ALIGN (asm_out_file,
2392 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
2393 ASM_OUTPUT_LABEL (asm_out_file, l2);
2395 /* Loop through all of the FDE's. */
2396 for (i = 0; i < fde_table_in_use; i++)
2398 fde = &fde_table[i];
2400 /* Don't emit EH unwind info for leaf functions that don't need it. */
2401 if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
2402 && (fde->nothrow || fde->all_throwers_are_sibcalls)
2403 && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2404 && !fde->uses_eh_lsda)
2405 continue;
2407 targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
2408 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
2409 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
2410 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
2411 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2412 dw2_asm_output_data (4, 0xffffffff,
2413 "Initial length escape value indicating 64-bit DWARF extension");
2414 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2415 "FDE Length");
2416 ASM_OUTPUT_LABEL (asm_out_file, l1);
2418 if (for_eh)
2419 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
2420 else
2421 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
2422 debug_frame_section, "FDE CIE offset");
2424 if (for_eh)
2426 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
2427 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
2428 dw2_asm_output_encoded_addr_rtx (fde_encoding,
2429 sym_ref,
2430 false,
2431 "FDE initial location");
2432 if (fde->dw_fde_switched_sections)
2434 rtx sym_ref2 = gen_rtx_SYMBOL_REF (Pmode,
2435 fde->dw_fde_unlikely_section_label);
2436 rtx sym_ref3= gen_rtx_SYMBOL_REF (Pmode,
2437 fde->dw_fde_hot_section_label);
2438 SYMBOL_REF_FLAGS (sym_ref2) |= SYMBOL_FLAG_LOCAL;
2439 SYMBOL_REF_FLAGS (sym_ref3) |= SYMBOL_FLAG_LOCAL;
2440 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref3, false,
2441 "FDE initial location");
2442 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2443 fde->dw_fde_hot_section_end_label,
2444 fde->dw_fde_hot_section_label,
2445 "FDE address range");
2446 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref2, false,
2447 "FDE initial location");
2448 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2449 fde->dw_fde_unlikely_section_end_label,
2450 fde->dw_fde_unlikely_section_label,
2451 "FDE address range");
2453 else
2454 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2455 fde->dw_fde_end, fde->dw_fde_begin,
2456 "FDE address range");
2458 else
2460 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
2461 "FDE initial location");
2462 if (fde->dw_fde_switched_sections)
2464 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2465 fde->dw_fde_hot_section_label,
2466 "FDE initial location");
2467 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2468 fde->dw_fde_hot_section_end_label,
2469 fde->dw_fde_hot_section_label,
2470 "FDE address range");
2471 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2472 fde->dw_fde_unlikely_section_label,
2473 "FDE initial location");
2474 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2475 fde->dw_fde_unlikely_section_end_label,
2476 fde->dw_fde_unlikely_section_label,
2477 "FDE address range");
2479 else
2480 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2481 fde->dw_fde_end, fde->dw_fde_begin,
2482 "FDE address range");
2485 if (augmentation[0])
2487 if (any_lsda_needed)
2489 int size = size_of_encoded_value (lsda_encoding);
2491 if (lsda_encoding == DW_EH_PE_aligned)
2493 int offset = ( 4 /* Length */
2494 + 4 /* CIE offset */
2495 + 2 * size_of_encoded_value (fde_encoding)
2496 + 1 /* Augmentation size */ );
2497 int pad = -offset & (PTR_SIZE - 1);
2499 size += pad;
2500 gcc_assert (size_of_uleb128 (size) == 1);
2503 dw2_asm_output_data_uleb128 (size, "Augmentation size");
2505 if (fde->uses_eh_lsda)
2507 ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
2508 fde->funcdef_number);
2509 dw2_asm_output_encoded_addr_rtx (
2510 lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
2511 false, "Language Specific Data Area");
2513 else
2515 if (lsda_encoding == DW_EH_PE_aligned)
2516 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2517 dw2_asm_output_data
2518 (size_of_encoded_value (lsda_encoding), 0,
2519 "Language Specific Data Area (none)");
2522 else
2523 dw2_asm_output_data_uleb128 (0, "Augmentation size");
2526 /* Loop through the Call Frame Instructions associated with
2527 this FDE. */
2528 fde->dw_fde_current_label = fde->dw_fde_begin;
2529 for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
2530 output_cfi (cfi, fde, for_eh);
2532 /* Pad the FDE out to an address sized boundary. */
2533 ASM_OUTPUT_ALIGN (asm_out_file,
2534 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
2535 ASM_OUTPUT_LABEL (asm_out_file, l2);
2538 if (for_eh && targetm.terminate_dw2_eh_frame_info)
2539 dw2_asm_output_data (4, 0, "End of Table");
2540 #ifdef MIPS_DEBUGGING_INFO
2541 /* Work around Irix 6 assembler bug whereby labels at the end of a section
2542 get a value of 0. Putting .align 0 after the label fixes it. */
2543 ASM_OUTPUT_ALIGN (asm_out_file, 0);
2544 #endif
2546 /* Turn off app to make assembly quicker. */
2547 if (flag_debug_asm)
2548 app_disable ();
2551 /* Output a marker (i.e. a label) for the beginning of a function, before
2552 the prologue. */
2554 void
2555 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
2556 const char *file ATTRIBUTE_UNUSED)
2558 char label[MAX_ARTIFICIAL_LABEL_BYTES];
2559 char * dup_label;
2560 dw_fde_ref fde;
2562 current_function_func_begin_label = NULL;
2564 #ifdef TARGET_UNWIND_INFO
2565 /* ??? current_function_func_begin_label is also used by except.c
2566 for call-site information. We must emit this label if it might
2567 be used. */
2568 if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
2569 && ! dwarf2out_do_frame ())
2570 return;
2571 #else
2572 if (! dwarf2out_do_frame ())
2573 return;
2574 #endif
2576 switch_to_section (function_section (current_function_decl));
2577 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
2578 current_function_funcdef_no);
2579 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
2580 current_function_funcdef_no);
2581 dup_label = xstrdup (label);
2582 current_function_func_begin_label = dup_label;
2584 #ifdef TARGET_UNWIND_INFO
2585 /* We can elide the fde allocation if we're not emitting debug info. */
2586 if (! dwarf2out_do_frame ())
2587 return;
2588 #endif
2590 /* Expand the fde table if necessary. */
2591 if (fde_table_in_use == fde_table_allocated)
2593 fde_table_allocated += FDE_TABLE_INCREMENT;
2594 fde_table = ggc_realloc (fde_table,
2595 fde_table_allocated * sizeof (dw_fde_node));
2596 memset (fde_table + fde_table_in_use, 0,
2597 FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2600 /* Record the FDE associated with this function. */
2601 current_funcdef_fde = fde_table_in_use;
2603 /* Add the new FDE at the end of the fde_table. */
2604 fde = &fde_table[fde_table_in_use++];
2605 fde->decl = current_function_decl;
2606 fde->dw_fde_begin = dup_label;
2607 fde->dw_fde_current_label = dup_label;
2608 fde->dw_fde_hot_section_label = NULL;
2609 fde->dw_fde_hot_section_end_label = NULL;
2610 fde->dw_fde_unlikely_section_label = NULL;
2611 fde->dw_fde_unlikely_section_end_label = NULL;
2612 fde->dw_fde_switched_sections = false;
2613 fde->dw_fde_end = NULL;
2614 fde->dw_fde_cfi = NULL;
2615 fde->funcdef_number = current_function_funcdef_no;
2616 fde->nothrow = TREE_NOTHROW (current_function_decl);
2617 fde->uses_eh_lsda = cfun->uses_eh_lsda;
2618 fde->all_throwers_are_sibcalls = cfun->all_throwers_are_sibcalls;
2620 args_size = old_args_size = 0;
2622 /* We only want to output line number information for the genuine dwarf2
2623 prologue case, not the eh frame case. */
2624 #ifdef DWARF2_DEBUGGING_INFO
2625 if (file)
2626 dwarf2out_source_line (line, file);
2627 #endif
2630 /* Output a marker (i.e. a label) for the absolute end of the generated code
2631 for a function definition. This gets called *after* the epilogue code has
2632 been generated. */
2634 void
2635 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
2636 const char *file ATTRIBUTE_UNUSED)
2638 dw_fde_ref fde;
2639 char label[MAX_ARTIFICIAL_LABEL_BYTES];
2641 /* Output a label to mark the endpoint of the code generated for this
2642 function. */
2643 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
2644 current_function_funcdef_no);
2645 ASM_OUTPUT_LABEL (asm_out_file, label);
2646 fde = &fde_table[fde_table_in_use - 1];
2647 fde->dw_fde_end = xstrdup (label);
2650 void
2651 dwarf2out_frame_init (void)
2653 /* Allocate the initial hunk of the fde_table. */
2654 fde_table = ggc_alloc_cleared (FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2655 fde_table_allocated = FDE_TABLE_INCREMENT;
2656 fde_table_in_use = 0;
2658 /* Generate the CFA instructions common to all FDE's. Do it now for the
2659 sake of lookup_cfa. */
2661 /* On entry, the Canonical Frame Address is at SP. */
2662 dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
2664 #ifdef DWARF2_UNWIND_INFO
2665 if (DWARF2_UNWIND_INFO)
2666 initial_return_save (INCOMING_RETURN_ADDR_RTX);
2667 #endif
2670 void
2671 dwarf2out_frame_finish (void)
2673 /* Output call frame information. */
2674 if (DWARF2_FRAME_INFO)
2675 output_call_frame_info (0);
2677 #ifndef TARGET_UNWIND_INFO
2678 /* Output another copy for the unwinder. */
2679 if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
2680 output_call_frame_info (1);
2681 #endif
2683 #endif
2685 /* And now, the subset of the debugging information support code necessary
2686 for emitting location expressions. */
2688 /* Data about a single source file. */
2689 struct dwarf_file_data GTY(())
2691 const char * filename;
2692 int emitted_number;
2695 /* We need some way to distinguish DW_OP_addr with a direct symbol
2696 relocation from DW_OP_addr with a dtp-relative symbol relocation. */
2697 #define INTERNAL_DW_OP_tls_addr (0x100 + DW_OP_addr)
2700 typedef struct dw_val_struct *dw_val_ref;
2701 typedef struct die_struct *dw_die_ref;
2702 typedef const struct die_struct *const_dw_die_ref;
2703 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
2704 typedef struct dw_loc_list_struct *dw_loc_list_ref;
2706 /* Each DIE may have a series of attribute/value pairs. Values
2707 can take on several forms. The forms that are used in this
2708 implementation are listed below. */
2710 enum dw_val_class
2712 dw_val_class_addr,
2713 dw_val_class_offset,
2714 dw_val_class_loc,
2715 dw_val_class_loc_list,
2716 dw_val_class_range_list,
2717 dw_val_class_const,
2718 dw_val_class_unsigned_const,
2719 dw_val_class_long_long,
2720 dw_val_class_vec,
2721 dw_val_class_flag,
2722 dw_val_class_die_ref,
2723 dw_val_class_fde_ref,
2724 dw_val_class_lbl_id,
2725 dw_val_class_lineptr,
2726 dw_val_class_str,
2727 dw_val_class_macptr,
2728 dw_val_class_file
2731 /* Describe a double word constant value. */
2732 /* ??? Every instance of long_long in the code really means CONST_DOUBLE. */
2734 typedef struct dw_long_long_struct GTY(())
2736 unsigned long hi;
2737 unsigned long low;
2739 dw_long_long_const;
2741 /* Describe a floating point constant value, or a vector constant value. */
2743 typedef struct dw_vec_struct GTY(())
2745 unsigned char * GTY((length ("%h.length"))) array;
2746 unsigned length;
2747 unsigned elt_size;
2749 dw_vec_const;
2751 /* The dw_val_node describes an attribute's value, as it is
2752 represented internally. */
2754 typedef struct dw_val_struct GTY(())
2756 enum dw_val_class val_class;
2757 union dw_val_struct_union
2759 rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
2760 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
2761 dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
2762 dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
2763 HOST_WIDE_INT GTY ((default)) val_int;
2764 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
2765 dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
2766 dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
2767 struct dw_val_die_union
2769 dw_die_ref die;
2770 int external;
2771 } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
2772 unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
2773 struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
2774 char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
2775 unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
2776 struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
2778 GTY ((desc ("%1.val_class"))) v;
2780 dw_val_node;
2782 /* Locations in memory are described using a sequence of stack machine
2783 operations. */
2785 typedef struct dw_loc_descr_struct GTY(())
2787 dw_loc_descr_ref dw_loc_next;
2788 enum dwarf_location_atom dw_loc_opc;
2789 dw_val_node dw_loc_oprnd1;
2790 dw_val_node dw_loc_oprnd2;
2791 int dw_loc_addr;
2793 dw_loc_descr_node;
2795 /* Location lists are ranges + location descriptions for that range,
2796 so you can track variables that are in different places over
2797 their entire life. */
2798 typedef struct dw_loc_list_struct GTY(())
2800 dw_loc_list_ref dw_loc_next;
2801 const char *begin; /* Label for begin address of range */
2802 const char *end; /* Label for end address of range */
2803 char *ll_symbol; /* Label for beginning of location list.
2804 Only on head of list */
2805 const char *section; /* Section this loclist is relative to */
2806 dw_loc_descr_ref expr;
2807 } dw_loc_list_node;
2809 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2811 static const char *dwarf_stack_op_name (unsigned);
2812 static dw_loc_descr_ref new_loc_descr (enum dwarf_location_atom,
2813 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2814 static void add_loc_descr (dw_loc_descr_ref *, dw_loc_descr_ref);
2815 static unsigned long size_of_loc_descr (dw_loc_descr_ref);
2816 static unsigned long size_of_locs (dw_loc_descr_ref);
2817 static void output_loc_operands (dw_loc_descr_ref);
2818 static void output_loc_sequence (dw_loc_descr_ref);
2820 /* Convert a DWARF stack opcode into its string name. */
2822 static const char *
2823 dwarf_stack_op_name (unsigned int op)
2825 switch (op)
2827 case DW_OP_addr:
2828 case INTERNAL_DW_OP_tls_addr:
2829 return "DW_OP_addr";
2830 case DW_OP_deref:
2831 return "DW_OP_deref";
2832 case DW_OP_const1u:
2833 return "DW_OP_const1u";
2834 case DW_OP_const1s:
2835 return "DW_OP_const1s";
2836 case DW_OP_const2u:
2837 return "DW_OP_const2u";
2838 case DW_OP_const2s:
2839 return "DW_OP_const2s";
2840 case DW_OP_const4u:
2841 return "DW_OP_const4u";
2842 case DW_OP_const4s:
2843 return "DW_OP_const4s";
2844 case DW_OP_const8u:
2845 return "DW_OP_const8u";
2846 case DW_OP_const8s:
2847 return "DW_OP_const8s";
2848 case DW_OP_constu:
2849 return "DW_OP_constu";
2850 case DW_OP_consts:
2851 return "DW_OP_consts";
2852 case DW_OP_dup:
2853 return "DW_OP_dup";
2854 case DW_OP_drop:
2855 return "DW_OP_drop";
2856 case DW_OP_over:
2857 return "DW_OP_over";
2858 case DW_OP_pick:
2859 return "DW_OP_pick";
2860 case DW_OP_swap:
2861 return "DW_OP_swap";
2862 case DW_OP_rot:
2863 return "DW_OP_rot";
2864 case DW_OP_xderef:
2865 return "DW_OP_xderef";
2866 case DW_OP_abs:
2867 return "DW_OP_abs";
2868 case DW_OP_and:
2869 return "DW_OP_and";
2870 case DW_OP_div:
2871 return "DW_OP_div";
2872 case DW_OP_minus:
2873 return "DW_OP_minus";
2874 case DW_OP_mod:
2875 return "DW_OP_mod";
2876 case DW_OP_mul:
2877 return "DW_OP_mul";
2878 case DW_OP_neg:
2879 return "DW_OP_neg";
2880 case DW_OP_not:
2881 return "DW_OP_not";
2882 case DW_OP_or:
2883 return "DW_OP_or";
2884 case DW_OP_plus:
2885 return "DW_OP_plus";
2886 case DW_OP_plus_uconst:
2887 return "DW_OP_plus_uconst";
2888 case DW_OP_shl:
2889 return "DW_OP_shl";
2890 case DW_OP_shr:
2891 return "DW_OP_shr";
2892 case DW_OP_shra:
2893 return "DW_OP_shra";
2894 case DW_OP_xor:
2895 return "DW_OP_xor";
2896 case DW_OP_bra:
2897 return "DW_OP_bra";
2898 case DW_OP_eq:
2899 return "DW_OP_eq";
2900 case DW_OP_ge:
2901 return "DW_OP_ge";
2902 case DW_OP_gt:
2903 return "DW_OP_gt";
2904 case DW_OP_le:
2905 return "DW_OP_le";
2906 case DW_OP_lt:
2907 return "DW_OP_lt";
2908 case DW_OP_ne:
2909 return "DW_OP_ne";
2910 case DW_OP_skip:
2911 return "DW_OP_skip";
2912 case DW_OP_lit0:
2913 return "DW_OP_lit0";
2914 case DW_OP_lit1:
2915 return "DW_OP_lit1";
2916 case DW_OP_lit2:
2917 return "DW_OP_lit2";
2918 case DW_OP_lit3:
2919 return "DW_OP_lit3";
2920 case DW_OP_lit4:
2921 return "DW_OP_lit4";
2922 case DW_OP_lit5:
2923 return "DW_OP_lit5";
2924 case DW_OP_lit6:
2925 return "DW_OP_lit6";
2926 case DW_OP_lit7:
2927 return "DW_OP_lit7";
2928 case DW_OP_lit8:
2929 return "DW_OP_lit8";
2930 case DW_OP_lit9:
2931 return "DW_OP_lit9";
2932 case DW_OP_lit10:
2933 return "DW_OP_lit10";
2934 case DW_OP_lit11:
2935 return "DW_OP_lit11";
2936 case DW_OP_lit12:
2937 return "DW_OP_lit12";
2938 case DW_OP_lit13:
2939 return "DW_OP_lit13";
2940 case DW_OP_lit14:
2941 return "DW_OP_lit14";
2942 case DW_OP_lit15:
2943 return "DW_OP_lit15";
2944 case DW_OP_lit16:
2945 return "DW_OP_lit16";
2946 case DW_OP_lit17:
2947 return "DW_OP_lit17";
2948 case DW_OP_lit18:
2949 return "DW_OP_lit18";
2950 case DW_OP_lit19:
2951 return "DW_OP_lit19";
2952 case DW_OP_lit20:
2953 return "DW_OP_lit20";
2954 case DW_OP_lit21:
2955 return "DW_OP_lit21";
2956 case DW_OP_lit22:
2957 return "DW_OP_lit22";
2958 case DW_OP_lit23:
2959 return "DW_OP_lit23";
2960 case DW_OP_lit24:
2961 return "DW_OP_lit24";
2962 case DW_OP_lit25:
2963 return "DW_OP_lit25";
2964 case DW_OP_lit26:
2965 return "DW_OP_lit26";
2966 case DW_OP_lit27:
2967 return "DW_OP_lit27";
2968 case DW_OP_lit28:
2969 return "DW_OP_lit28";
2970 case DW_OP_lit29:
2971 return "DW_OP_lit29";
2972 case DW_OP_lit30:
2973 return "DW_OP_lit30";
2974 case DW_OP_lit31:
2975 return "DW_OP_lit31";
2976 case DW_OP_reg0:
2977 return "DW_OP_reg0";
2978 case DW_OP_reg1:
2979 return "DW_OP_reg1";
2980 case DW_OP_reg2:
2981 return "DW_OP_reg2";
2982 case DW_OP_reg3:
2983 return "DW_OP_reg3";
2984 case DW_OP_reg4:
2985 return "DW_OP_reg4";
2986 case DW_OP_reg5:
2987 return "DW_OP_reg5";
2988 case DW_OP_reg6:
2989 return "DW_OP_reg6";
2990 case DW_OP_reg7:
2991 return "DW_OP_reg7";
2992 case DW_OP_reg8:
2993 return "DW_OP_reg8";
2994 case DW_OP_reg9:
2995 return "DW_OP_reg9";
2996 case DW_OP_reg10:
2997 return "DW_OP_reg10";
2998 case DW_OP_reg11:
2999 return "DW_OP_reg11";
3000 case DW_OP_reg12:
3001 return "DW_OP_reg12";
3002 case DW_OP_reg13:
3003 return "DW_OP_reg13";
3004 case DW_OP_reg14:
3005 return "DW_OP_reg14";
3006 case DW_OP_reg15:
3007 return "DW_OP_reg15";
3008 case DW_OP_reg16:
3009 return "DW_OP_reg16";
3010 case DW_OP_reg17:
3011 return "DW_OP_reg17";
3012 case DW_OP_reg18:
3013 return "DW_OP_reg18";
3014 case DW_OP_reg19:
3015 return "DW_OP_reg19";
3016 case DW_OP_reg20:
3017 return "DW_OP_reg20";
3018 case DW_OP_reg21:
3019 return "DW_OP_reg21";
3020 case DW_OP_reg22:
3021 return "DW_OP_reg22";
3022 case DW_OP_reg23:
3023 return "DW_OP_reg23";
3024 case DW_OP_reg24:
3025 return "DW_OP_reg24";
3026 case DW_OP_reg25:
3027 return "DW_OP_reg25";
3028 case DW_OP_reg26:
3029 return "DW_OP_reg26";
3030 case DW_OP_reg27:
3031 return "DW_OP_reg27";
3032 case DW_OP_reg28:
3033 return "DW_OP_reg28";
3034 case DW_OP_reg29:
3035 return "DW_OP_reg29";
3036 case DW_OP_reg30:
3037 return "DW_OP_reg30";
3038 case DW_OP_reg31:
3039 return "DW_OP_reg31";
3040 case DW_OP_breg0:
3041 return "DW_OP_breg0";
3042 case DW_OP_breg1:
3043 return "DW_OP_breg1";
3044 case DW_OP_breg2:
3045 return "DW_OP_breg2";
3046 case DW_OP_breg3:
3047 return "DW_OP_breg3";
3048 case DW_OP_breg4:
3049 return "DW_OP_breg4";
3050 case DW_OP_breg5:
3051 return "DW_OP_breg5";
3052 case DW_OP_breg6:
3053 return "DW_OP_breg6";
3054 case DW_OP_breg7:
3055 return "DW_OP_breg7";
3056 case DW_OP_breg8:
3057 return "DW_OP_breg8";
3058 case DW_OP_breg9:
3059 return "DW_OP_breg9";
3060 case DW_OP_breg10:
3061 return "DW_OP_breg10";
3062 case DW_OP_breg11:
3063 return "DW_OP_breg11";
3064 case DW_OP_breg12:
3065 return "DW_OP_breg12";
3066 case DW_OP_breg13:
3067 return "DW_OP_breg13";
3068 case DW_OP_breg14:
3069 return "DW_OP_breg14";
3070 case DW_OP_breg15:
3071 return "DW_OP_breg15";
3072 case DW_OP_breg16:
3073 return "DW_OP_breg16";
3074 case DW_OP_breg17:
3075 return "DW_OP_breg17";
3076 case DW_OP_breg18:
3077 return "DW_OP_breg18";
3078 case DW_OP_breg19:
3079 return "DW_OP_breg19";
3080 case DW_OP_breg20:
3081 return "DW_OP_breg20";
3082 case DW_OP_breg21:
3083 return "DW_OP_breg21";
3084 case DW_OP_breg22:
3085 return "DW_OP_breg22";
3086 case DW_OP_breg23:
3087 return "DW_OP_breg23";
3088 case DW_OP_breg24:
3089 return "DW_OP_breg24";
3090 case DW_OP_breg25:
3091 return "DW_OP_breg25";
3092 case DW_OP_breg26:
3093 return "DW_OP_breg26";
3094 case DW_OP_breg27:
3095 return "DW_OP_breg27";
3096 case DW_OP_breg28:
3097 return "DW_OP_breg28";
3098 case DW_OP_breg29:
3099 return "DW_OP_breg29";
3100 case DW_OP_breg30:
3101 return "DW_OP_breg30";
3102 case DW_OP_breg31:
3103 return "DW_OP_breg31";
3104 case DW_OP_regx:
3105 return "DW_OP_regx";
3106 case DW_OP_fbreg:
3107 return "DW_OP_fbreg";
3108 case DW_OP_bregx:
3109 return "DW_OP_bregx";
3110 case DW_OP_piece:
3111 return "DW_OP_piece";
3112 case DW_OP_deref_size:
3113 return "DW_OP_deref_size";
3114 case DW_OP_xderef_size:
3115 return "DW_OP_xderef_size";
3116 case DW_OP_nop:
3117 return "DW_OP_nop";
3118 case DW_OP_push_object_address:
3119 return "DW_OP_push_object_address";
3120 case DW_OP_call2:
3121 return "DW_OP_call2";
3122 case DW_OP_call4:
3123 return "DW_OP_call4";
3124 case DW_OP_call_ref:
3125 return "DW_OP_call_ref";
3126 case DW_OP_GNU_push_tls_address:
3127 return "DW_OP_GNU_push_tls_address";
3128 case DW_OP_GNU_uninit:
3129 return "DW_OP_GNU_uninit";
3130 default:
3131 return "OP_<unknown>";
3135 /* Return a pointer to a newly allocated location description. Location
3136 descriptions are simple expression terms that can be strung
3137 together to form more complicated location (address) descriptions. */
3139 static inline dw_loc_descr_ref
3140 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
3141 unsigned HOST_WIDE_INT oprnd2)
3143 dw_loc_descr_ref descr = ggc_alloc_cleared (sizeof (dw_loc_descr_node));
3145 descr->dw_loc_opc = op;
3146 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
3147 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
3148 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
3149 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
3151 return descr;
3154 /* Add a location description term to a location description expression. */
3156 static inline void
3157 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
3159 dw_loc_descr_ref *d;
3161 /* Find the end of the chain. */
3162 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
3165 *d = descr;
3168 /* Return the size of a location descriptor. */
3170 static unsigned long
3171 size_of_loc_descr (dw_loc_descr_ref loc)
3173 unsigned long size = 1;
3175 switch (loc->dw_loc_opc)
3177 case DW_OP_addr:
3178 case INTERNAL_DW_OP_tls_addr:
3179 size += DWARF2_ADDR_SIZE;
3180 break;
3181 case DW_OP_const1u:
3182 case DW_OP_const1s:
3183 size += 1;
3184 break;
3185 case DW_OP_const2u:
3186 case DW_OP_const2s:
3187 size += 2;
3188 break;
3189 case DW_OP_const4u:
3190 case DW_OP_const4s:
3191 size += 4;
3192 break;
3193 case DW_OP_const8u:
3194 case DW_OP_const8s:
3195 size += 8;
3196 break;
3197 case DW_OP_constu:
3198 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3199 break;
3200 case DW_OP_consts:
3201 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3202 break;
3203 case DW_OP_pick:
3204 size += 1;
3205 break;
3206 case DW_OP_plus_uconst:
3207 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3208 break;
3209 case DW_OP_skip:
3210 case DW_OP_bra:
3211 size += 2;
3212 break;
3213 case DW_OP_breg0:
3214 case DW_OP_breg1:
3215 case DW_OP_breg2:
3216 case DW_OP_breg3:
3217 case DW_OP_breg4:
3218 case DW_OP_breg5:
3219 case DW_OP_breg6:
3220 case DW_OP_breg7:
3221 case DW_OP_breg8:
3222 case DW_OP_breg9:
3223 case DW_OP_breg10:
3224 case DW_OP_breg11:
3225 case DW_OP_breg12:
3226 case DW_OP_breg13:
3227 case DW_OP_breg14:
3228 case DW_OP_breg15:
3229 case DW_OP_breg16:
3230 case DW_OP_breg17:
3231 case DW_OP_breg18:
3232 case DW_OP_breg19:
3233 case DW_OP_breg20:
3234 case DW_OP_breg21:
3235 case DW_OP_breg22:
3236 case DW_OP_breg23:
3237 case DW_OP_breg24:
3238 case DW_OP_breg25:
3239 case DW_OP_breg26:
3240 case DW_OP_breg27:
3241 case DW_OP_breg28:
3242 case DW_OP_breg29:
3243 case DW_OP_breg30:
3244 case DW_OP_breg31:
3245 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3246 break;
3247 case DW_OP_regx:
3248 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3249 break;
3250 case DW_OP_fbreg:
3251 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3252 break;
3253 case DW_OP_bregx:
3254 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3255 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
3256 break;
3257 case DW_OP_piece:
3258 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3259 break;
3260 case DW_OP_deref_size:
3261 case DW_OP_xderef_size:
3262 size += 1;
3263 break;
3264 case DW_OP_call2:
3265 size += 2;
3266 break;
3267 case DW_OP_call4:
3268 size += 4;
3269 break;
3270 case DW_OP_call_ref:
3271 size += DWARF2_ADDR_SIZE;
3272 break;
3273 default:
3274 break;
3277 return size;
3280 /* Return the size of a series of location descriptors. */
3282 static unsigned long
3283 size_of_locs (dw_loc_descr_ref loc)
3285 dw_loc_descr_ref l;
3286 unsigned long size;
3288 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
3289 field, to avoid writing to a PCH file. */
3290 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3292 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
3293 break;
3294 size += size_of_loc_descr (l);
3296 if (! l)
3297 return size;
3299 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3301 l->dw_loc_addr = size;
3302 size += size_of_loc_descr (l);
3305 return size;
3308 /* Output location description stack opcode's operands (if any). */
3310 static void
3311 output_loc_operands (dw_loc_descr_ref loc)
3313 dw_val_ref val1 = &loc->dw_loc_oprnd1;
3314 dw_val_ref val2 = &loc->dw_loc_oprnd2;
3316 switch (loc->dw_loc_opc)
3318 #ifdef DWARF2_DEBUGGING_INFO
3319 case DW_OP_addr:
3320 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
3321 break;
3322 case DW_OP_const2u:
3323 case DW_OP_const2s:
3324 dw2_asm_output_data (2, val1->v.val_int, NULL);
3325 break;
3326 case DW_OP_const4u:
3327 case DW_OP_const4s:
3328 dw2_asm_output_data (4, val1->v.val_int, NULL);
3329 break;
3330 case DW_OP_const8u:
3331 case DW_OP_const8s:
3332 gcc_assert (HOST_BITS_PER_LONG >= 64);
3333 dw2_asm_output_data (8, val1->v.val_int, NULL);
3334 break;
3335 case DW_OP_skip:
3336 case DW_OP_bra:
3338 int offset;
3340 gcc_assert (val1->val_class == dw_val_class_loc);
3341 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
3343 dw2_asm_output_data (2, offset, NULL);
3345 break;
3346 #else
3347 case DW_OP_addr:
3348 case DW_OP_const2u:
3349 case DW_OP_const2s:
3350 case DW_OP_const4u:
3351 case DW_OP_const4s:
3352 case DW_OP_const8u:
3353 case DW_OP_const8s:
3354 case DW_OP_skip:
3355 case DW_OP_bra:
3356 /* We currently don't make any attempt to make sure these are
3357 aligned properly like we do for the main unwind info, so
3358 don't support emitting things larger than a byte if we're
3359 only doing unwinding. */
3360 gcc_unreachable ();
3361 #endif
3362 case DW_OP_const1u:
3363 case DW_OP_const1s:
3364 dw2_asm_output_data (1, val1->v.val_int, NULL);
3365 break;
3366 case DW_OP_constu:
3367 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3368 break;
3369 case DW_OP_consts:
3370 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3371 break;
3372 case DW_OP_pick:
3373 dw2_asm_output_data (1, val1->v.val_int, NULL);
3374 break;
3375 case DW_OP_plus_uconst:
3376 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3377 break;
3378 case DW_OP_breg0:
3379 case DW_OP_breg1:
3380 case DW_OP_breg2:
3381 case DW_OP_breg3:
3382 case DW_OP_breg4:
3383 case DW_OP_breg5:
3384 case DW_OP_breg6:
3385 case DW_OP_breg7:
3386 case DW_OP_breg8:
3387 case DW_OP_breg9:
3388 case DW_OP_breg10:
3389 case DW_OP_breg11:
3390 case DW_OP_breg12:
3391 case DW_OP_breg13:
3392 case DW_OP_breg14:
3393 case DW_OP_breg15:
3394 case DW_OP_breg16:
3395 case DW_OP_breg17:
3396 case DW_OP_breg18:
3397 case DW_OP_breg19:
3398 case DW_OP_breg20:
3399 case DW_OP_breg21:
3400 case DW_OP_breg22:
3401 case DW_OP_breg23:
3402 case DW_OP_breg24:
3403 case DW_OP_breg25:
3404 case DW_OP_breg26:
3405 case DW_OP_breg27:
3406 case DW_OP_breg28:
3407 case DW_OP_breg29:
3408 case DW_OP_breg30:
3409 case DW_OP_breg31:
3410 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3411 break;
3412 case DW_OP_regx:
3413 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3414 break;
3415 case DW_OP_fbreg:
3416 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3417 break;
3418 case DW_OP_bregx:
3419 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3420 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
3421 break;
3422 case DW_OP_piece:
3423 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3424 break;
3425 case DW_OP_deref_size:
3426 case DW_OP_xderef_size:
3427 dw2_asm_output_data (1, val1->v.val_int, NULL);
3428 break;
3430 case INTERNAL_DW_OP_tls_addr:
3431 if (targetm.asm_out.output_dwarf_dtprel)
3433 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
3434 DWARF2_ADDR_SIZE,
3435 val1->v.val_addr);
3436 fputc ('\n', asm_out_file);
3438 else
3439 gcc_unreachable ();
3440 break;
3442 default:
3443 /* Other codes have no operands. */
3444 break;
3448 /* Output a sequence of location operations. */
3450 static void
3451 output_loc_sequence (dw_loc_descr_ref loc)
3453 for (; loc != NULL; loc = loc->dw_loc_next)
3455 /* Output the opcode. */
3456 dw2_asm_output_data (1, loc->dw_loc_opc,
3457 "%s", dwarf_stack_op_name (loc->dw_loc_opc));
3459 /* Output the operand(s) (if any). */
3460 output_loc_operands (loc);
3464 /* This routine will generate the correct assembly data for a location
3465 description based on a cfi entry with a complex address. */
3467 static void
3468 output_cfa_loc (dw_cfi_ref cfi)
3470 dw_loc_descr_ref loc;
3471 unsigned long size;
3473 /* Output the size of the block. */
3474 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
3475 size = size_of_locs (loc);
3476 dw2_asm_output_data_uleb128 (size, NULL);
3478 /* Now output the operations themselves. */
3479 output_loc_sequence (loc);
3482 /* This function builds a dwarf location descriptor sequence from a
3483 dw_cfa_location, adding the given OFFSET to the result of the
3484 expression. */
3486 static struct dw_loc_descr_struct *
3487 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
3489 struct dw_loc_descr_struct *head, *tmp;
3491 offset += cfa->offset;
3493 if (cfa->indirect)
3495 if (cfa->base_offset)
3497 if (cfa->reg <= 31)
3498 head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->base_offset, 0);
3499 else
3500 head = new_loc_descr (DW_OP_bregx, cfa->reg, cfa->base_offset);
3502 else if (cfa->reg <= 31)
3503 head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3504 else
3505 head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3507 head->dw_loc_oprnd1.val_class = dw_val_class_const;
3508 tmp = new_loc_descr (DW_OP_deref, 0, 0);
3509 add_loc_descr (&head, tmp);
3510 if (offset != 0)
3512 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
3513 add_loc_descr (&head, tmp);
3516 else
3518 if (offset == 0)
3519 if (cfa->reg <= 31)
3520 head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3521 else
3522 head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3523 else if (cfa->reg <= 31)
3524 head = new_loc_descr (DW_OP_breg0 + cfa->reg, offset, 0);
3525 else
3526 head = new_loc_descr (DW_OP_bregx, cfa->reg, offset);
3529 return head;
3532 /* This function fills in aa dw_cfa_location structure from a dwarf location
3533 descriptor sequence. */
3535 static void
3536 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
3538 struct dw_loc_descr_struct *ptr;
3539 cfa->offset = 0;
3540 cfa->base_offset = 0;
3541 cfa->indirect = 0;
3542 cfa->reg = -1;
3544 for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
3546 enum dwarf_location_atom op = ptr->dw_loc_opc;
3548 switch (op)
3550 case DW_OP_reg0:
3551 case DW_OP_reg1:
3552 case DW_OP_reg2:
3553 case DW_OP_reg3:
3554 case DW_OP_reg4:
3555 case DW_OP_reg5:
3556 case DW_OP_reg6:
3557 case DW_OP_reg7:
3558 case DW_OP_reg8:
3559 case DW_OP_reg9:
3560 case DW_OP_reg10:
3561 case DW_OP_reg11:
3562 case DW_OP_reg12:
3563 case DW_OP_reg13:
3564 case DW_OP_reg14:
3565 case DW_OP_reg15:
3566 case DW_OP_reg16:
3567 case DW_OP_reg17:
3568 case DW_OP_reg18:
3569 case DW_OP_reg19:
3570 case DW_OP_reg20:
3571 case DW_OP_reg21:
3572 case DW_OP_reg22:
3573 case DW_OP_reg23:
3574 case DW_OP_reg24:
3575 case DW_OP_reg25:
3576 case DW_OP_reg26:
3577 case DW_OP_reg27:
3578 case DW_OP_reg28:
3579 case DW_OP_reg29:
3580 case DW_OP_reg30:
3581 case DW_OP_reg31:
3582 cfa->reg = op - DW_OP_reg0;
3583 break;
3584 case DW_OP_regx:
3585 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3586 break;
3587 case DW_OP_breg0:
3588 case DW_OP_breg1:
3589 case DW_OP_breg2:
3590 case DW_OP_breg3:
3591 case DW_OP_breg4:
3592 case DW_OP_breg5:
3593 case DW_OP_breg6:
3594 case DW_OP_breg7:
3595 case DW_OP_breg8:
3596 case DW_OP_breg9:
3597 case DW_OP_breg10:
3598 case DW_OP_breg11:
3599 case DW_OP_breg12:
3600 case DW_OP_breg13:
3601 case DW_OP_breg14:
3602 case DW_OP_breg15:
3603 case DW_OP_breg16:
3604 case DW_OP_breg17:
3605 case DW_OP_breg18:
3606 case DW_OP_breg19:
3607 case DW_OP_breg20:
3608 case DW_OP_breg21:
3609 case DW_OP_breg22:
3610 case DW_OP_breg23:
3611 case DW_OP_breg24:
3612 case DW_OP_breg25:
3613 case DW_OP_breg26:
3614 case DW_OP_breg27:
3615 case DW_OP_breg28:
3616 case DW_OP_breg29:
3617 case DW_OP_breg30:
3618 case DW_OP_breg31:
3619 cfa->reg = op - DW_OP_breg0;
3620 cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
3621 break;
3622 case DW_OP_bregx:
3623 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3624 cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
3625 break;
3626 case DW_OP_deref:
3627 cfa->indirect = 1;
3628 break;
3629 case DW_OP_plus_uconst:
3630 cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
3631 break;
3632 default:
3633 internal_error ("DW_LOC_OP %s not implemented",
3634 dwarf_stack_op_name (ptr->dw_loc_opc));
3638 #endif /* .debug_frame support */
3640 /* And now, the support for symbolic debugging information. */
3641 #ifdef DWARF2_DEBUGGING_INFO
3643 /* .debug_str support. */
3644 static int output_indirect_string (void **, void *);
3646 static void dwarf2out_init (const char *);
3647 static void dwarf2out_finish (const char *);
3648 static void dwarf2out_define (unsigned int, const char *);
3649 static void dwarf2out_undef (unsigned int, const char *);
3650 static void dwarf2out_start_source_file (unsigned, const char *);
3651 static void dwarf2out_end_source_file (unsigned);
3652 static void dwarf2out_begin_block (unsigned, unsigned);
3653 static void dwarf2out_end_block (unsigned, unsigned);
3654 static bool dwarf2out_ignore_block (const_tree);
3655 static void dwarf2out_global_decl (tree);
3656 static void dwarf2out_type_decl (tree, int);
3657 static void dwarf2out_imported_module_or_decl (tree, tree);
3658 static void dwarf2out_abstract_function (tree);
3659 static void dwarf2out_var_location (rtx);
3660 static void dwarf2out_begin_function (tree);
3661 static void dwarf2out_switch_text_section (void);
3663 /* The debug hooks structure. */
3665 const struct gcc_debug_hooks dwarf2_debug_hooks =
3667 dwarf2out_init,
3668 dwarf2out_finish,
3669 dwarf2out_define,
3670 dwarf2out_undef,
3671 dwarf2out_start_source_file,
3672 dwarf2out_end_source_file,
3673 dwarf2out_begin_block,
3674 dwarf2out_end_block,
3675 dwarf2out_ignore_block,
3676 dwarf2out_source_line,
3677 dwarf2out_begin_prologue,
3678 debug_nothing_int_charstar, /* end_prologue */
3679 dwarf2out_end_epilogue,
3680 dwarf2out_begin_function,
3681 debug_nothing_int, /* end_function */
3682 dwarf2out_decl, /* function_decl */
3683 dwarf2out_global_decl,
3684 dwarf2out_type_decl, /* type_decl */
3685 dwarf2out_imported_module_or_decl,
3686 debug_nothing_tree, /* deferred_inline_function */
3687 /* The DWARF 2 backend tries to reduce debugging bloat by not
3688 emitting the abstract description of inline functions until
3689 something tries to reference them. */
3690 dwarf2out_abstract_function, /* outlining_inline_function */
3691 debug_nothing_rtx, /* label */
3692 debug_nothing_int, /* handle_pch */
3693 dwarf2out_var_location,
3694 dwarf2out_switch_text_section,
3695 1 /* start_end_main_source_file */
3697 #endif
3699 /* NOTE: In the comments in this file, many references are made to
3700 "Debugging Information Entries". This term is abbreviated as `DIE'
3701 throughout the remainder of this file. */
3703 /* An internal representation of the DWARF output is built, and then
3704 walked to generate the DWARF debugging info. The walk of the internal
3705 representation is done after the entire program has been compiled.
3706 The types below are used to describe the internal representation. */
3708 /* Various DIE's use offsets relative to the beginning of the
3709 .debug_info section to refer to each other. */
3711 typedef long int dw_offset;
3713 /* Define typedefs here to avoid circular dependencies. */
3715 typedef struct dw_attr_struct *dw_attr_ref;
3716 typedef struct dw_line_info_struct *dw_line_info_ref;
3717 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
3718 typedef struct pubname_struct *pubname_ref;
3719 typedef struct dw_ranges_struct *dw_ranges_ref;
3720 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
3722 /* Each entry in the line_info_table maintains the file and
3723 line number associated with the label generated for that
3724 entry. The label gives the PC value associated with
3725 the line number entry. */
3727 typedef struct dw_line_info_struct GTY(())
3729 unsigned long dw_file_num;
3730 unsigned long dw_line_num;
3732 dw_line_info_entry;
3734 /* Line information for functions in separate sections; each one gets its
3735 own sequence. */
3736 typedef struct dw_separate_line_info_struct GTY(())
3738 unsigned long dw_file_num;
3739 unsigned long dw_line_num;
3740 unsigned long function;
3742 dw_separate_line_info_entry;
3744 /* Each DIE attribute has a field specifying the attribute kind,
3745 a link to the next attribute in the chain, and an attribute value.
3746 Attributes are typically linked below the DIE they modify. */
3748 typedef struct dw_attr_struct GTY(())
3750 enum dwarf_attribute dw_attr;
3751 dw_val_node dw_attr_val;
3753 dw_attr_node;
3755 DEF_VEC_O(dw_attr_node);
3756 DEF_VEC_ALLOC_O(dw_attr_node,gc);
3758 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
3759 The children of each node form a circular list linked by
3760 die_sib. die_child points to the node *before* the "first" child node. */
3762 typedef struct die_struct GTY(())
3764 enum dwarf_tag die_tag;
3765 char *die_symbol;
3766 VEC(dw_attr_node,gc) * die_attr;
3767 dw_die_ref die_parent;
3768 dw_die_ref die_child;
3769 dw_die_ref die_sib;
3770 dw_die_ref die_definition; /* ref from a specification to its definition */
3771 dw_offset die_offset;
3772 unsigned long die_abbrev;
3773 int die_mark;
3774 /* Die is used and must not be pruned as unused. */
3775 int die_perennial_p;
3776 unsigned int decl_id;
3778 die_node;
3780 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
3781 #define FOR_EACH_CHILD(die, c, expr) do { \
3782 c = die->die_child; \
3783 if (c) do { \
3784 c = c->die_sib; \
3785 expr; \
3786 } while (c != die->die_child); \
3787 } while (0)
3789 /* The pubname structure */
3791 typedef struct pubname_struct GTY(())
3793 dw_die_ref die;
3794 const char *name;
3796 pubname_entry;
3798 DEF_VEC_O(pubname_entry);
3799 DEF_VEC_ALLOC_O(pubname_entry, gc);
3801 struct dw_ranges_struct GTY(())
3803 /* If this is positive, it's a block number, otherwise it's a
3804 bitwise-negated index into dw_ranges_by_label. */
3805 int num;
3808 struct dw_ranges_by_label_struct GTY(())
3810 const char *begin;
3811 const char *end;
3814 /* The limbo die list structure. */
3815 typedef struct limbo_die_struct GTY(())
3817 dw_die_ref die;
3818 tree created_for;
3819 struct limbo_die_struct *next;
3821 limbo_die_node;
3823 /* How to start an assembler comment. */
3824 #ifndef ASM_COMMENT_START
3825 #define ASM_COMMENT_START ";#"
3826 #endif
3828 /* Define a macro which returns nonzero for a TYPE_DECL which was
3829 implicitly generated for a tagged type.
3831 Note that unlike the gcc front end (which generates a NULL named
3832 TYPE_DECL node for each complete tagged type, each array type, and
3833 each function type node created) the g++ front end generates a
3834 _named_ TYPE_DECL node for each tagged type node created.
3835 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3836 generate a DW_TAG_typedef DIE for them. */
3838 #define TYPE_DECL_IS_STUB(decl) \
3839 (DECL_NAME (decl) == NULL_TREE \
3840 || (DECL_ARTIFICIAL (decl) \
3841 && is_tagged_type (TREE_TYPE (decl)) \
3842 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3843 /* This is necessary for stub decls that \
3844 appear in nested inline functions. */ \
3845 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3846 && (decl_ultimate_origin (decl) \
3847 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3849 /* Information concerning the compilation unit's programming
3850 language, and compiler version. */
3852 /* Fixed size portion of the DWARF compilation unit header. */
3853 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3854 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
3856 /* Fixed size portion of public names info. */
3857 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3859 /* Fixed size portion of the address range info. */
3860 #define DWARF_ARANGES_HEADER_SIZE \
3861 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3862 DWARF2_ADDR_SIZE * 2) \
3863 - DWARF_INITIAL_LENGTH_SIZE)
3865 /* Size of padding portion in the address range info. It must be
3866 aligned to twice the pointer size. */
3867 #define DWARF_ARANGES_PAD_SIZE \
3868 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3869 DWARF2_ADDR_SIZE * 2) \
3870 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3872 /* Use assembler line directives if available. */
3873 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3874 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3875 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3876 #else
3877 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3878 #endif
3879 #endif
3881 /* Minimum line offset in a special line info. opcode.
3882 This value was chosen to give a reasonable range of values. */
3883 #define DWARF_LINE_BASE -10
3885 /* First special line opcode - leave room for the standard opcodes. */
3886 #define DWARF_LINE_OPCODE_BASE 10
3888 /* Range of line offsets in a special line info. opcode. */
3889 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3891 /* Flag that indicates the initial value of the is_stmt_start flag.
3892 In the present implementation, we do not mark any lines as
3893 the beginning of a source statement, because that information
3894 is not made available by the GCC front-end. */
3895 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3897 #ifdef DWARF2_DEBUGGING_INFO
3898 /* This location is used by calc_die_sizes() to keep track
3899 the offset of each DIE within the .debug_info section. */
3900 static unsigned long next_die_offset;
3901 #endif
3903 /* Record the root of the DIE's built for the current compilation unit. */
3904 static GTY(()) dw_die_ref comp_unit_die;
3906 /* A list of DIEs with a NULL parent waiting to be relocated. */
3907 static GTY(()) limbo_die_node *limbo_die_list;
3909 /* Filenames referenced by this compilation unit. */
3910 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
3912 /* A hash table of references to DIE's that describe declarations.
3913 The key is a DECL_UID() which is a unique number identifying each decl. */
3914 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
3916 /* Node of the variable location list. */
3917 struct var_loc_node GTY ((chain_next ("%h.next")))
3919 rtx GTY (()) var_loc_note;
3920 const char * GTY (()) label;
3921 const char * GTY (()) section_label;
3922 struct var_loc_node * GTY (()) next;
3925 /* Variable location list. */
3926 struct var_loc_list_def GTY (())
3928 struct var_loc_node * GTY (()) first;
3930 /* Do not mark the last element of the chained list because
3931 it is marked through the chain. */
3932 struct var_loc_node * GTY ((skip ("%h"))) last;
3934 /* DECL_UID of the variable decl. */
3935 unsigned int decl_id;
3937 typedef struct var_loc_list_def var_loc_list;
3940 /* Table of decl location linked lists. */
3941 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
3943 /* A pointer to the base of a list of references to DIE's that
3944 are uniquely identified by their tag, presence/absence of
3945 children DIE's, and list of attribute/value pairs. */
3946 static GTY((length ("abbrev_die_table_allocated")))
3947 dw_die_ref *abbrev_die_table;
3949 /* Number of elements currently allocated for abbrev_die_table. */
3950 static GTY(()) unsigned abbrev_die_table_allocated;
3952 /* Number of elements in type_die_table currently in use. */
3953 static GTY(()) unsigned abbrev_die_table_in_use;
3955 /* Size (in elements) of increments by which we may expand the
3956 abbrev_die_table. */
3957 #define ABBREV_DIE_TABLE_INCREMENT 256
3959 /* A pointer to the base of a table that contains line information
3960 for each source code line in .text in the compilation unit. */
3961 static GTY((length ("line_info_table_allocated")))
3962 dw_line_info_ref line_info_table;
3964 /* Number of elements currently allocated for line_info_table. */
3965 static GTY(()) unsigned line_info_table_allocated;
3967 /* Number of elements in line_info_table currently in use. */
3968 static GTY(()) unsigned line_info_table_in_use;
3970 /* True if the compilation unit places functions in more than one section. */
3971 static GTY(()) bool have_multiple_function_sections = false;
3973 /* A pointer to the base of a table that contains line information
3974 for each source code line outside of .text in the compilation unit. */
3975 static GTY ((length ("separate_line_info_table_allocated")))
3976 dw_separate_line_info_ref separate_line_info_table;
3978 /* Number of elements currently allocated for separate_line_info_table. */
3979 static GTY(()) unsigned separate_line_info_table_allocated;
3981 /* Number of elements in separate_line_info_table currently in use. */
3982 static GTY(()) unsigned separate_line_info_table_in_use;
3984 /* Size (in elements) of increments by which we may expand the
3985 line_info_table. */
3986 #define LINE_INFO_TABLE_INCREMENT 1024
3988 /* A pointer to the base of a table that contains a list of publicly
3989 accessible names. */
3990 static GTY (()) VEC (pubname_entry, gc) * pubname_table;
3992 /* A pointer to the base of a table that contains a list of publicly
3993 accessible types. */
3994 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
3996 /* Array of dies for which we should generate .debug_arange info. */
3997 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
3999 /* Number of elements currently allocated for arange_table. */
4000 static GTY(()) unsigned arange_table_allocated;
4002 /* Number of elements in arange_table currently in use. */
4003 static GTY(()) unsigned arange_table_in_use;
4005 /* Size (in elements) of increments by which we may expand the
4006 arange_table. */
4007 #define ARANGE_TABLE_INCREMENT 64
4009 /* Array of dies for which we should generate .debug_ranges info. */
4010 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
4012 /* Number of elements currently allocated for ranges_table. */
4013 static GTY(()) unsigned ranges_table_allocated;
4015 /* Number of elements in ranges_table currently in use. */
4016 static GTY(()) unsigned ranges_table_in_use;
4018 /* Array of pairs of labels referenced in ranges_table. */
4019 static GTY ((length ("ranges_by_label_allocated")))
4020 dw_ranges_by_label_ref ranges_by_label;
4022 /* Number of elements currently allocated for ranges_by_label. */
4023 static GTY(()) unsigned ranges_by_label_allocated;
4025 /* Number of elements in ranges_by_label currently in use. */
4026 static GTY(()) unsigned ranges_by_label_in_use;
4028 /* Size (in elements) of increments by which we may expand the
4029 ranges_table. */
4030 #define RANGES_TABLE_INCREMENT 64
4032 /* Whether we have location lists that need outputting */
4033 static GTY(()) bool have_location_lists;
4035 /* Unique label counter. */
4036 static GTY(()) unsigned int loclabel_num;
4038 #ifdef DWARF2_DEBUGGING_INFO
4039 /* Record whether the function being analyzed contains inlined functions. */
4040 static int current_function_has_inlines;
4041 #endif
4042 #if 0 && defined (MIPS_DEBUGGING_INFO)
4043 static int comp_unit_has_inlines;
4044 #endif
4046 /* The last file entry emitted by maybe_emit_file(). */
4047 static GTY(()) struct dwarf_file_data * last_emitted_file;
4049 /* Number of internal labels generated by gen_internal_sym(). */
4050 static GTY(()) int label_num;
4052 /* Cached result of previous call to lookup_filename. */
4053 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
4055 /* Whether the default text and cold text sections have been used at
4056 all. */
4058 static GTY(()) bool text_section_used = false;
4059 static GTY(()) bool cold_text_section_used = false;
4061 /* The default cold text section. */
4062 static GTY(()) section *cold_text_section;
4064 #ifdef DWARF2_DEBUGGING_INFO
4066 /* Offset from the "steady-state frame pointer" to the frame base,
4067 within the current function. */
4068 static HOST_WIDE_INT frame_pointer_fb_offset;
4070 /* Forward declarations for functions defined in this file. */
4072 static int is_pseudo_reg (const_rtx);
4073 static tree type_main_variant (tree);
4074 static int is_tagged_type (const_tree);
4075 static const char *dwarf_tag_name (unsigned);
4076 static const char *dwarf_attr_name (unsigned);
4077 static const char *dwarf_form_name (unsigned);
4078 static tree decl_ultimate_origin (const_tree);
4079 static tree block_ultimate_origin (const_tree);
4080 static tree decl_class_context (tree);
4081 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
4082 static inline enum dw_val_class AT_class (dw_attr_ref);
4083 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
4084 static inline unsigned AT_flag (dw_attr_ref);
4085 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
4086 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
4087 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
4088 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
4089 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
4090 unsigned long);
4091 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
4092 unsigned int, unsigned char *);
4093 static hashval_t debug_str_do_hash (const void *);
4094 static int debug_str_eq (const void *, const void *);
4095 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
4096 static inline const char *AT_string (dw_attr_ref);
4097 static int AT_string_form (dw_attr_ref);
4098 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
4099 static void add_AT_specification (dw_die_ref, dw_die_ref);
4100 static inline dw_die_ref AT_ref (dw_attr_ref);
4101 static inline int AT_ref_external (dw_attr_ref);
4102 static inline void set_AT_ref_external (dw_attr_ref, int);
4103 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
4104 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
4105 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
4106 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
4107 dw_loc_list_ref);
4108 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
4109 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
4110 static inline rtx AT_addr (dw_attr_ref);
4111 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
4112 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
4113 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
4114 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
4115 unsigned HOST_WIDE_INT);
4116 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
4117 unsigned long);
4118 static inline const char *AT_lbl (dw_attr_ref);
4119 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
4120 static const char *get_AT_low_pc (dw_die_ref);
4121 static const char *get_AT_hi_pc (dw_die_ref);
4122 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
4123 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
4124 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
4125 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
4126 static bool is_c_family (void);
4127 static bool is_cxx (void);
4128 static bool is_java (void);
4129 static bool is_fortran (void);
4130 static bool is_ada (void);
4131 static void remove_AT (dw_die_ref, enum dwarf_attribute);
4132 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
4133 static void add_child_die (dw_die_ref, dw_die_ref);
4134 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
4135 static dw_die_ref lookup_type_die (tree);
4136 static void equate_type_number_to_die (tree, dw_die_ref);
4137 static hashval_t decl_die_table_hash (const void *);
4138 static int decl_die_table_eq (const void *, const void *);
4139 static dw_die_ref lookup_decl_die (tree);
4140 static hashval_t decl_loc_table_hash (const void *);
4141 static int decl_loc_table_eq (const void *, const void *);
4142 static var_loc_list *lookup_decl_loc (const_tree);
4143 static void equate_decl_number_to_die (tree, dw_die_ref);
4144 static void add_var_loc_to_decl (tree, struct var_loc_node *);
4145 static void print_spaces (FILE *);
4146 static void print_die (dw_die_ref, FILE *);
4147 static void print_dwarf_line_table (FILE *);
4148 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
4149 static dw_die_ref pop_compile_unit (dw_die_ref);
4150 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
4151 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
4152 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
4153 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
4154 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
4155 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
4156 static int same_die_p (dw_die_ref, dw_die_ref, int *);
4157 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
4158 static void compute_section_prefix (dw_die_ref);
4159 static int is_type_die (dw_die_ref);
4160 static int is_comdat_die (dw_die_ref);
4161 static int is_symbol_die (dw_die_ref);
4162 static void assign_symbol_names (dw_die_ref);
4163 static void break_out_includes (dw_die_ref);
4164 static hashval_t htab_cu_hash (const void *);
4165 static int htab_cu_eq (const void *, const void *);
4166 static void htab_cu_del (void *);
4167 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
4168 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
4169 static void add_sibling_attributes (dw_die_ref);
4170 static void build_abbrev_table (dw_die_ref);
4171 static void output_location_lists (dw_die_ref);
4172 static int constant_size (long unsigned);
4173 static unsigned long size_of_die (dw_die_ref);
4174 static void calc_die_sizes (dw_die_ref);
4175 static void mark_dies (dw_die_ref);
4176 static void unmark_dies (dw_die_ref);
4177 static void unmark_all_dies (dw_die_ref);
4178 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
4179 static unsigned long size_of_aranges (void);
4180 static enum dwarf_form value_format (dw_attr_ref);
4181 static void output_value_format (dw_attr_ref);
4182 static void output_abbrev_section (void);
4183 static void output_die_symbol (dw_die_ref);
4184 static void output_die (dw_die_ref);
4185 static void output_compilation_unit_header (void);
4186 static void output_comp_unit (dw_die_ref, int);
4187 static const char *dwarf2_name (tree, int);
4188 static void add_pubname (tree, dw_die_ref);
4189 static void add_pubtype (tree, dw_die_ref);
4190 static void output_pubnames (VEC (pubname_entry,gc) *);
4191 static void add_arange (tree, dw_die_ref);
4192 static void output_aranges (void);
4193 static unsigned int add_ranges_num (int);
4194 static unsigned int add_ranges (const_tree);
4195 static unsigned int add_ranges_by_labels (const char *, const char *);
4196 static void output_ranges (void);
4197 static void output_line_info (void);
4198 static void output_file_names (void);
4199 static dw_die_ref base_type_die (tree);
4200 static int is_base_type (tree);
4201 static bool is_subrange_type (const_tree);
4202 static dw_die_ref subrange_type_die (tree, dw_die_ref);
4203 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
4204 static int type_is_enum (const_tree);
4205 static unsigned int dbx_reg_number (const_rtx);
4206 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
4207 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
4208 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
4209 enum var_init_status);
4210 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
4211 enum var_init_status);
4212 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4213 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
4214 enum var_init_status);
4215 static int is_based_loc (const_rtx);
4216 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
4217 enum var_init_status);
4218 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
4219 enum var_init_status);
4220 static dw_loc_descr_ref loc_descriptor (rtx, enum var_init_status);
4221 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
4222 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
4223 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
4224 static tree field_type (const_tree);
4225 static unsigned int simple_type_align_in_bits (const_tree);
4226 static unsigned int simple_decl_align_in_bits (const_tree);
4227 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
4228 static HOST_WIDE_INT field_byte_offset (const_tree);
4229 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
4230 dw_loc_descr_ref);
4231 static void add_data_member_location_attribute (dw_die_ref, tree);
4232 static void add_const_value_attribute (dw_die_ref, rtx);
4233 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
4234 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4235 static void insert_float (const_rtx, unsigned char *);
4236 static rtx rtl_for_decl_location (tree);
4237 static void add_location_or_const_value_attribute (dw_die_ref, tree,
4238 enum dwarf_attribute);
4239 static void tree_add_const_value_attribute (dw_die_ref, tree);
4240 static void add_name_attribute (dw_die_ref, const char *);
4241 static void add_comp_dir_attribute (dw_die_ref);
4242 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
4243 static void add_subscript_info (dw_die_ref, tree);
4244 static void add_byte_size_attribute (dw_die_ref, tree);
4245 static void add_bit_offset_attribute (dw_die_ref, tree);
4246 static void add_bit_size_attribute (dw_die_ref, tree);
4247 static void add_prototyped_attribute (dw_die_ref, tree);
4248 static void add_abstract_origin_attribute (dw_die_ref, tree);
4249 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
4250 static void add_src_coords_attributes (dw_die_ref, tree);
4251 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
4252 static void push_decl_scope (tree);
4253 static void pop_decl_scope (void);
4254 static dw_die_ref scope_die_for (tree, dw_die_ref);
4255 static inline int local_scope_p (dw_die_ref);
4256 static inline int class_or_namespace_scope_p (dw_die_ref);
4257 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
4258 static void add_calling_convention_attribute (dw_die_ref, tree);
4259 static const char *type_tag (const_tree);
4260 static tree member_declared_type (const_tree);
4261 #if 0
4262 static const char *decl_start_label (tree);
4263 #endif
4264 static void gen_array_type_die (tree, dw_die_ref);
4265 #if 0
4266 static void gen_entry_point_die (tree, dw_die_ref);
4267 #endif
4268 static void gen_inlined_enumeration_type_die (tree, dw_die_ref);
4269 static void gen_inlined_structure_type_die (tree, dw_die_ref);
4270 static void gen_inlined_union_type_die (tree, dw_die_ref);
4271 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
4272 static dw_die_ref gen_formal_parameter_die (tree, dw_die_ref);
4273 static void gen_unspecified_parameters_die (tree, dw_die_ref);
4274 static void gen_formal_types_die (tree, dw_die_ref);
4275 static void gen_subprogram_die (tree, dw_die_ref);
4276 static void gen_variable_die (tree, dw_die_ref);
4277 static void gen_label_die (tree, dw_die_ref);
4278 static void gen_lexical_block_die (tree, dw_die_ref, int);
4279 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
4280 static void gen_field_die (tree, dw_die_ref);
4281 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
4282 static dw_die_ref gen_compile_unit_die (const char *);
4283 static void gen_inheritance_die (tree, tree, dw_die_ref);
4284 static void gen_member_die (tree, dw_die_ref);
4285 static void gen_struct_or_union_type_die (tree, dw_die_ref,
4286 enum debug_info_usage);
4287 static void gen_subroutine_type_die (tree, dw_die_ref);
4288 static void gen_typedef_die (tree, dw_die_ref);
4289 static void gen_type_die (tree, dw_die_ref);
4290 static void gen_tagged_type_instantiation_die (tree, dw_die_ref);
4291 static void gen_block_die (tree, dw_die_ref, int);
4292 static void decls_for_scope (tree, dw_die_ref, int);
4293 static int is_redundant_typedef (const_tree);
4294 static void gen_namespace_die (tree);
4295 static void gen_decl_die (tree, dw_die_ref);
4296 static dw_die_ref force_decl_die (tree);
4297 static dw_die_ref force_type_die (tree);
4298 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
4299 static void declare_in_namespace (tree, dw_die_ref);
4300 static struct dwarf_file_data * lookup_filename (const char *);
4301 static void retry_incomplete_types (void);
4302 static void gen_type_die_for_member (tree, tree, dw_die_ref);
4303 static void splice_child_die (dw_die_ref, dw_die_ref);
4304 static int file_info_cmp (const void *, const void *);
4305 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
4306 const char *, const char *, unsigned);
4307 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
4308 const char *, const char *,
4309 const char *);
4310 static void output_loc_list (dw_loc_list_ref);
4311 static char *gen_internal_sym (const char *);
4313 static void prune_unmark_dies (dw_die_ref);
4314 static void prune_unused_types_mark (dw_die_ref, int);
4315 static void prune_unused_types_walk (dw_die_ref);
4316 static void prune_unused_types_walk_attribs (dw_die_ref);
4317 static void prune_unused_types_prune (dw_die_ref);
4318 static void prune_unused_types (void);
4319 static int maybe_emit_file (struct dwarf_file_data *fd);
4321 /* Section names used to hold DWARF debugging information. */
4322 #ifndef DEBUG_INFO_SECTION
4323 #define DEBUG_INFO_SECTION ".debug_info"
4324 #endif
4325 #ifndef DEBUG_ABBREV_SECTION
4326 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
4327 #endif
4328 #ifndef DEBUG_ARANGES_SECTION
4329 #define DEBUG_ARANGES_SECTION ".debug_aranges"
4330 #endif
4331 #ifndef DEBUG_MACINFO_SECTION
4332 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
4333 #endif
4334 #ifndef DEBUG_LINE_SECTION
4335 #define DEBUG_LINE_SECTION ".debug_line"
4336 #endif
4337 #ifndef DEBUG_LOC_SECTION
4338 #define DEBUG_LOC_SECTION ".debug_loc"
4339 #endif
4340 #ifndef DEBUG_PUBNAMES_SECTION
4341 #define DEBUG_PUBNAMES_SECTION ".debug_pubnames"
4342 #endif
4343 #ifndef DEBUG_STR_SECTION
4344 #define DEBUG_STR_SECTION ".debug_str"
4345 #endif
4346 #ifndef DEBUG_RANGES_SECTION
4347 #define DEBUG_RANGES_SECTION ".debug_ranges"
4348 #endif
4350 /* Standard ELF section names for compiled code and data. */
4351 #ifndef TEXT_SECTION_NAME
4352 #define TEXT_SECTION_NAME ".text"
4353 #endif
4355 /* Section flags for .debug_str section. */
4356 #define DEBUG_STR_SECTION_FLAGS \
4357 (HAVE_GAS_SHF_MERGE && flag_merge_constants \
4358 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
4359 : SECTION_DEBUG)
4361 /* Labels we insert at beginning sections we can reference instead of
4362 the section names themselves. */
4364 #ifndef TEXT_SECTION_LABEL
4365 #define TEXT_SECTION_LABEL "Ltext"
4366 #endif
4367 #ifndef COLD_TEXT_SECTION_LABEL
4368 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
4369 #endif
4370 #ifndef DEBUG_LINE_SECTION_LABEL
4371 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
4372 #endif
4373 #ifndef DEBUG_INFO_SECTION_LABEL
4374 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
4375 #endif
4376 #ifndef DEBUG_ABBREV_SECTION_LABEL
4377 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
4378 #endif
4379 #ifndef DEBUG_LOC_SECTION_LABEL
4380 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
4381 #endif
4382 #ifndef DEBUG_RANGES_SECTION_LABEL
4383 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
4384 #endif
4385 #ifndef DEBUG_MACINFO_SECTION_LABEL
4386 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
4387 #endif
4389 /* Definitions of defaults for formats and names of various special
4390 (artificial) labels which may be generated within this file (when the -g
4391 options is used and DWARF2_DEBUGGING_INFO is in effect.
4392 If necessary, these may be overridden from within the tm.h file, but
4393 typically, overriding these defaults is unnecessary. */
4395 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4396 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4397 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4398 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4399 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4400 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4401 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4402 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4403 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4404 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4406 #ifndef TEXT_END_LABEL
4407 #define TEXT_END_LABEL "Letext"
4408 #endif
4409 #ifndef COLD_END_LABEL
4410 #define COLD_END_LABEL "Letext_cold"
4411 #endif
4412 #ifndef BLOCK_BEGIN_LABEL
4413 #define BLOCK_BEGIN_LABEL "LBB"
4414 #endif
4415 #ifndef BLOCK_END_LABEL
4416 #define BLOCK_END_LABEL "LBE"
4417 #endif
4418 #ifndef LINE_CODE_LABEL
4419 #define LINE_CODE_LABEL "LM"
4420 #endif
4421 #ifndef SEPARATE_LINE_CODE_LABEL
4422 #define SEPARATE_LINE_CODE_LABEL "LSM"
4423 #endif
4426 /* We allow a language front-end to designate a function that is to be
4427 called to "demangle" any name before it is put into a DIE. */
4429 static const char *(*demangle_name_func) (const char *);
4431 void
4432 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4434 demangle_name_func = func;
4437 /* Test if rtl node points to a pseudo register. */
4439 static inline int
4440 is_pseudo_reg (const_rtx rtl)
4442 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4443 || (GET_CODE (rtl) == SUBREG
4444 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4447 /* Return a reference to a type, with its const and volatile qualifiers
4448 removed. */
4450 static inline tree
4451 type_main_variant (tree type)
4453 type = TYPE_MAIN_VARIANT (type);
4455 /* ??? There really should be only one main variant among any group of
4456 variants of a given type (and all of the MAIN_VARIANT values for all
4457 members of the group should point to that one type) but sometimes the C
4458 front-end messes this up for array types, so we work around that bug
4459 here. */
4460 if (TREE_CODE (type) == ARRAY_TYPE)
4461 while (type != TYPE_MAIN_VARIANT (type))
4462 type = TYPE_MAIN_VARIANT (type);
4464 return type;
4467 /* Return nonzero if the given type node represents a tagged type. */
4469 static inline int
4470 is_tagged_type (const_tree type)
4472 enum tree_code code = TREE_CODE (type);
4474 return (code == RECORD_TYPE || code == UNION_TYPE
4475 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4478 /* Convert a DIE tag into its string name. */
4480 static const char *
4481 dwarf_tag_name (unsigned int tag)
4483 switch (tag)
4485 case DW_TAG_padding:
4486 return "DW_TAG_padding";
4487 case DW_TAG_array_type:
4488 return "DW_TAG_array_type";
4489 case DW_TAG_class_type:
4490 return "DW_TAG_class_type";
4491 case DW_TAG_entry_point:
4492 return "DW_TAG_entry_point";
4493 case DW_TAG_enumeration_type:
4494 return "DW_TAG_enumeration_type";
4495 case DW_TAG_formal_parameter:
4496 return "DW_TAG_formal_parameter";
4497 case DW_TAG_imported_declaration:
4498 return "DW_TAG_imported_declaration";
4499 case DW_TAG_label:
4500 return "DW_TAG_label";
4501 case DW_TAG_lexical_block:
4502 return "DW_TAG_lexical_block";
4503 case DW_TAG_member:
4504 return "DW_TAG_member";
4505 case DW_TAG_pointer_type:
4506 return "DW_TAG_pointer_type";
4507 case DW_TAG_reference_type:
4508 return "DW_TAG_reference_type";
4509 case DW_TAG_compile_unit:
4510 return "DW_TAG_compile_unit";
4511 case DW_TAG_string_type:
4512 return "DW_TAG_string_type";
4513 case DW_TAG_structure_type:
4514 return "DW_TAG_structure_type";
4515 case DW_TAG_subroutine_type:
4516 return "DW_TAG_subroutine_type";
4517 case DW_TAG_typedef:
4518 return "DW_TAG_typedef";
4519 case DW_TAG_union_type:
4520 return "DW_TAG_union_type";
4521 case DW_TAG_unspecified_parameters:
4522 return "DW_TAG_unspecified_parameters";
4523 case DW_TAG_variant:
4524 return "DW_TAG_variant";
4525 case DW_TAG_common_block:
4526 return "DW_TAG_common_block";
4527 case DW_TAG_common_inclusion:
4528 return "DW_TAG_common_inclusion";
4529 case DW_TAG_inheritance:
4530 return "DW_TAG_inheritance";
4531 case DW_TAG_inlined_subroutine:
4532 return "DW_TAG_inlined_subroutine";
4533 case DW_TAG_module:
4534 return "DW_TAG_module";
4535 case DW_TAG_ptr_to_member_type:
4536 return "DW_TAG_ptr_to_member_type";
4537 case DW_TAG_set_type:
4538 return "DW_TAG_set_type";
4539 case DW_TAG_subrange_type:
4540 return "DW_TAG_subrange_type";
4541 case DW_TAG_with_stmt:
4542 return "DW_TAG_with_stmt";
4543 case DW_TAG_access_declaration:
4544 return "DW_TAG_access_declaration";
4545 case DW_TAG_base_type:
4546 return "DW_TAG_base_type";
4547 case DW_TAG_catch_block:
4548 return "DW_TAG_catch_block";
4549 case DW_TAG_const_type:
4550 return "DW_TAG_const_type";
4551 case DW_TAG_constant:
4552 return "DW_TAG_constant";
4553 case DW_TAG_enumerator:
4554 return "DW_TAG_enumerator";
4555 case DW_TAG_file_type:
4556 return "DW_TAG_file_type";
4557 case DW_TAG_friend:
4558 return "DW_TAG_friend";
4559 case DW_TAG_namelist:
4560 return "DW_TAG_namelist";
4561 case DW_TAG_namelist_item:
4562 return "DW_TAG_namelist_item";
4563 case DW_TAG_namespace:
4564 return "DW_TAG_namespace";
4565 case DW_TAG_packed_type:
4566 return "DW_TAG_packed_type";
4567 case DW_TAG_subprogram:
4568 return "DW_TAG_subprogram";
4569 case DW_TAG_template_type_param:
4570 return "DW_TAG_template_type_param";
4571 case DW_TAG_template_value_param:
4572 return "DW_TAG_template_value_param";
4573 case DW_TAG_thrown_type:
4574 return "DW_TAG_thrown_type";
4575 case DW_TAG_try_block:
4576 return "DW_TAG_try_block";
4577 case DW_TAG_variant_part:
4578 return "DW_TAG_variant_part";
4579 case DW_TAG_variable:
4580 return "DW_TAG_variable";
4581 case DW_TAG_volatile_type:
4582 return "DW_TAG_volatile_type";
4583 case DW_TAG_imported_module:
4584 return "DW_TAG_imported_module";
4585 case DW_TAG_MIPS_loop:
4586 return "DW_TAG_MIPS_loop";
4587 case DW_TAG_format_label:
4588 return "DW_TAG_format_label";
4589 case DW_TAG_function_template:
4590 return "DW_TAG_function_template";
4591 case DW_TAG_class_template:
4592 return "DW_TAG_class_template";
4593 case DW_TAG_GNU_BINCL:
4594 return "DW_TAG_GNU_BINCL";
4595 case DW_TAG_GNU_EINCL:
4596 return "DW_TAG_GNU_EINCL";
4597 default:
4598 return "DW_TAG_<unknown>";
4602 /* Convert a DWARF attribute code into its string name. */
4604 static const char *
4605 dwarf_attr_name (unsigned int attr)
4607 switch (attr)
4609 case DW_AT_sibling:
4610 return "DW_AT_sibling";
4611 case DW_AT_location:
4612 return "DW_AT_location";
4613 case DW_AT_name:
4614 return "DW_AT_name";
4615 case DW_AT_ordering:
4616 return "DW_AT_ordering";
4617 case DW_AT_subscr_data:
4618 return "DW_AT_subscr_data";
4619 case DW_AT_byte_size:
4620 return "DW_AT_byte_size";
4621 case DW_AT_bit_offset:
4622 return "DW_AT_bit_offset";
4623 case DW_AT_bit_size:
4624 return "DW_AT_bit_size";
4625 case DW_AT_element_list:
4626 return "DW_AT_element_list";
4627 case DW_AT_stmt_list:
4628 return "DW_AT_stmt_list";
4629 case DW_AT_low_pc:
4630 return "DW_AT_low_pc";
4631 case DW_AT_high_pc:
4632 return "DW_AT_high_pc";
4633 case DW_AT_language:
4634 return "DW_AT_language";
4635 case DW_AT_member:
4636 return "DW_AT_member";
4637 case DW_AT_discr:
4638 return "DW_AT_discr";
4639 case DW_AT_discr_value:
4640 return "DW_AT_discr_value";
4641 case DW_AT_visibility:
4642 return "DW_AT_visibility";
4643 case DW_AT_import:
4644 return "DW_AT_import";
4645 case DW_AT_string_length:
4646 return "DW_AT_string_length";
4647 case DW_AT_common_reference:
4648 return "DW_AT_common_reference";
4649 case DW_AT_comp_dir:
4650 return "DW_AT_comp_dir";
4651 case DW_AT_const_value:
4652 return "DW_AT_const_value";
4653 case DW_AT_containing_type:
4654 return "DW_AT_containing_type";
4655 case DW_AT_default_value:
4656 return "DW_AT_default_value";
4657 case DW_AT_inline:
4658 return "DW_AT_inline";
4659 case DW_AT_is_optional:
4660 return "DW_AT_is_optional";
4661 case DW_AT_lower_bound:
4662 return "DW_AT_lower_bound";
4663 case DW_AT_producer:
4664 return "DW_AT_producer";
4665 case DW_AT_prototyped:
4666 return "DW_AT_prototyped";
4667 case DW_AT_return_addr:
4668 return "DW_AT_return_addr";
4669 case DW_AT_start_scope:
4670 return "DW_AT_start_scope";
4671 case DW_AT_stride_size:
4672 return "DW_AT_stride_size";
4673 case DW_AT_upper_bound:
4674 return "DW_AT_upper_bound";
4675 case DW_AT_abstract_origin:
4676 return "DW_AT_abstract_origin";
4677 case DW_AT_accessibility:
4678 return "DW_AT_accessibility";
4679 case DW_AT_address_class:
4680 return "DW_AT_address_class";
4681 case DW_AT_artificial:
4682 return "DW_AT_artificial";
4683 case DW_AT_base_types:
4684 return "DW_AT_base_types";
4685 case DW_AT_calling_convention:
4686 return "DW_AT_calling_convention";
4687 case DW_AT_count:
4688 return "DW_AT_count";
4689 case DW_AT_data_member_location:
4690 return "DW_AT_data_member_location";
4691 case DW_AT_decl_column:
4692 return "DW_AT_decl_column";
4693 case DW_AT_decl_file:
4694 return "DW_AT_decl_file";
4695 case DW_AT_decl_line:
4696 return "DW_AT_decl_line";
4697 case DW_AT_declaration:
4698 return "DW_AT_declaration";
4699 case DW_AT_discr_list:
4700 return "DW_AT_discr_list";
4701 case DW_AT_encoding:
4702 return "DW_AT_encoding";
4703 case DW_AT_external:
4704 return "DW_AT_external";
4705 case DW_AT_frame_base:
4706 return "DW_AT_frame_base";
4707 case DW_AT_friend:
4708 return "DW_AT_friend";
4709 case DW_AT_identifier_case:
4710 return "DW_AT_identifier_case";
4711 case DW_AT_macro_info:
4712 return "DW_AT_macro_info";
4713 case DW_AT_namelist_items:
4714 return "DW_AT_namelist_items";
4715 case DW_AT_priority:
4716 return "DW_AT_priority";
4717 case DW_AT_segment:
4718 return "DW_AT_segment";
4719 case DW_AT_specification:
4720 return "DW_AT_specification";
4721 case DW_AT_static_link:
4722 return "DW_AT_static_link";
4723 case DW_AT_type:
4724 return "DW_AT_type";
4725 case DW_AT_use_location:
4726 return "DW_AT_use_location";
4727 case DW_AT_variable_parameter:
4728 return "DW_AT_variable_parameter";
4729 case DW_AT_virtuality:
4730 return "DW_AT_virtuality";
4731 case DW_AT_vtable_elem_location:
4732 return "DW_AT_vtable_elem_location";
4734 case DW_AT_allocated:
4735 return "DW_AT_allocated";
4736 case DW_AT_associated:
4737 return "DW_AT_associated";
4738 case DW_AT_data_location:
4739 return "DW_AT_data_location";
4740 case DW_AT_stride:
4741 return "DW_AT_stride";
4742 case DW_AT_entry_pc:
4743 return "DW_AT_entry_pc";
4744 case DW_AT_use_UTF8:
4745 return "DW_AT_use_UTF8";
4746 case DW_AT_extension:
4747 return "DW_AT_extension";
4748 case DW_AT_ranges:
4749 return "DW_AT_ranges";
4750 case DW_AT_trampoline:
4751 return "DW_AT_trampoline";
4752 case DW_AT_call_column:
4753 return "DW_AT_call_column";
4754 case DW_AT_call_file:
4755 return "DW_AT_call_file";
4756 case DW_AT_call_line:
4757 return "DW_AT_call_line";
4759 case DW_AT_MIPS_fde:
4760 return "DW_AT_MIPS_fde";
4761 case DW_AT_MIPS_loop_begin:
4762 return "DW_AT_MIPS_loop_begin";
4763 case DW_AT_MIPS_tail_loop_begin:
4764 return "DW_AT_MIPS_tail_loop_begin";
4765 case DW_AT_MIPS_epilog_begin:
4766 return "DW_AT_MIPS_epilog_begin";
4767 case DW_AT_MIPS_loop_unroll_factor:
4768 return "DW_AT_MIPS_loop_unroll_factor";
4769 case DW_AT_MIPS_software_pipeline_depth:
4770 return "DW_AT_MIPS_software_pipeline_depth";
4771 case DW_AT_MIPS_linkage_name:
4772 return "DW_AT_MIPS_linkage_name";
4773 case DW_AT_MIPS_stride:
4774 return "DW_AT_MIPS_stride";
4775 case DW_AT_MIPS_abstract_name:
4776 return "DW_AT_MIPS_abstract_name";
4777 case DW_AT_MIPS_clone_origin:
4778 return "DW_AT_MIPS_clone_origin";
4779 case DW_AT_MIPS_has_inlines:
4780 return "DW_AT_MIPS_has_inlines";
4782 case DW_AT_sf_names:
4783 return "DW_AT_sf_names";
4784 case DW_AT_src_info:
4785 return "DW_AT_src_info";
4786 case DW_AT_mac_info:
4787 return "DW_AT_mac_info";
4788 case DW_AT_src_coords:
4789 return "DW_AT_src_coords";
4790 case DW_AT_body_begin:
4791 return "DW_AT_body_begin";
4792 case DW_AT_body_end:
4793 return "DW_AT_body_end";
4794 case DW_AT_GNU_vector:
4795 return "DW_AT_GNU_vector";
4797 case DW_AT_VMS_rtnbeg_pd_address:
4798 return "DW_AT_VMS_rtnbeg_pd_address";
4800 default:
4801 return "DW_AT_<unknown>";
4805 /* Convert a DWARF value form code into its string name. */
4807 static const char *
4808 dwarf_form_name (unsigned int form)
4810 switch (form)
4812 case DW_FORM_addr:
4813 return "DW_FORM_addr";
4814 case DW_FORM_block2:
4815 return "DW_FORM_block2";
4816 case DW_FORM_block4:
4817 return "DW_FORM_block4";
4818 case DW_FORM_data2:
4819 return "DW_FORM_data2";
4820 case DW_FORM_data4:
4821 return "DW_FORM_data4";
4822 case DW_FORM_data8:
4823 return "DW_FORM_data8";
4824 case DW_FORM_string:
4825 return "DW_FORM_string";
4826 case DW_FORM_block:
4827 return "DW_FORM_block";
4828 case DW_FORM_block1:
4829 return "DW_FORM_block1";
4830 case DW_FORM_data1:
4831 return "DW_FORM_data1";
4832 case DW_FORM_flag:
4833 return "DW_FORM_flag";
4834 case DW_FORM_sdata:
4835 return "DW_FORM_sdata";
4836 case DW_FORM_strp:
4837 return "DW_FORM_strp";
4838 case DW_FORM_udata:
4839 return "DW_FORM_udata";
4840 case DW_FORM_ref_addr:
4841 return "DW_FORM_ref_addr";
4842 case DW_FORM_ref1:
4843 return "DW_FORM_ref1";
4844 case DW_FORM_ref2:
4845 return "DW_FORM_ref2";
4846 case DW_FORM_ref4:
4847 return "DW_FORM_ref4";
4848 case DW_FORM_ref8:
4849 return "DW_FORM_ref8";
4850 case DW_FORM_ref_udata:
4851 return "DW_FORM_ref_udata";
4852 case DW_FORM_indirect:
4853 return "DW_FORM_indirect";
4854 default:
4855 return "DW_FORM_<unknown>";
4859 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4860 instance of an inlined instance of a decl which is local to an inline
4861 function, so we have to trace all of the way back through the origin chain
4862 to find out what sort of node actually served as the original seed for the
4863 given block. */
4865 static tree
4866 decl_ultimate_origin (const_tree decl)
4868 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4869 return NULL_TREE;
4871 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4872 nodes in the function to point to themselves; ignore that if
4873 we're trying to output the abstract instance of this function. */
4874 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4875 return NULL_TREE;
4877 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4878 most distant ancestor, this should never happen. */
4879 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4881 return DECL_ABSTRACT_ORIGIN (decl);
4884 /* Determine the "ultimate origin" of a block. The block may be an inlined
4885 instance of an inlined instance of a block which is local to an inline
4886 function, so we have to trace all of the way back through the origin chain
4887 to find out what sort of node actually served as the original seed for the
4888 given block. */
4890 static tree
4891 block_ultimate_origin (const_tree block)
4893 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
4895 /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
4896 nodes in the function to point to themselves; ignore that if
4897 we're trying to output the abstract instance of this function. */
4898 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
4899 return NULL_TREE;
4901 if (immediate_origin == NULL_TREE)
4902 return NULL_TREE;
4903 else
4905 tree ret_val;
4906 tree lookahead = immediate_origin;
4910 ret_val = lookahead;
4911 lookahead = (TREE_CODE (ret_val) == BLOCK
4912 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
4914 while (lookahead != NULL && lookahead != ret_val);
4916 /* The block's abstract origin chain may not be the *ultimate* origin of
4917 the block. It could lead to a DECL that has an abstract origin set.
4918 If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
4919 will give us if it has one). Note that DECL's abstract origins are
4920 supposed to be the most distant ancestor (or so decl_ultimate_origin
4921 claims), so we don't need to loop following the DECL origins. */
4922 if (DECL_P (ret_val))
4923 return DECL_ORIGIN (ret_val);
4925 return ret_val;
4929 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4930 of a virtual function may refer to a base class, so we check the 'this'
4931 parameter. */
4933 static tree
4934 decl_class_context (tree decl)
4936 tree context = NULL_TREE;
4938 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4939 context = DECL_CONTEXT (decl);
4940 else
4941 context = TYPE_MAIN_VARIANT
4942 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4944 if (context && !TYPE_P (context))
4945 context = NULL_TREE;
4947 return context;
4950 /* Add an attribute/value pair to a DIE. */
4952 static inline void
4953 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
4955 /* Maybe this should be an assert? */
4956 if (die == NULL)
4957 return;
4959 if (die->die_attr == NULL)
4960 die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
4961 VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
4964 static inline enum dw_val_class
4965 AT_class (dw_attr_ref a)
4967 return a->dw_attr_val.val_class;
4970 /* Add a flag value attribute to a DIE. */
4972 static inline void
4973 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4975 dw_attr_node attr;
4977 attr.dw_attr = attr_kind;
4978 attr.dw_attr_val.val_class = dw_val_class_flag;
4979 attr.dw_attr_val.v.val_flag = flag;
4980 add_dwarf_attr (die, &attr);
4983 static inline unsigned
4984 AT_flag (dw_attr_ref a)
4986 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4987 return a->dw_attr_val.v.val_flag;
4990 /* Add a signed integer attribute value to a DIE. */
4992 static inline void
4993 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4995 dw_attr_node attr;
4997 attr.dw_attr = attr_kind;
4998 attr.dw_attr_val.val_class = dw_val_class_const;
4999 attr.dw_attr_val.v.val_int = int_val;
5000 add_dwarf_attr (die, &attr);
5003 static inline HOST_WIDE_INT
5004 AT_int (dw_attr_ref a)
5006 gcc_assert (a && AT_class (a) == dw_val_class_const);
5007 return a->dw_attr_val.v.val_int;
5010 /* Add an unsigned integer attribute value to a DIE. */
5012 static inline void
5013 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
5014 unsigned HOST_WIDE_INT unsigned_val)
5016 dw_attr_node attr;
5018 attr.dw_attr = attr_kind;
5019 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
5020 attr.dw_attr_val.v.val_unsigned = unsigned_val;
5021 add_dwarf_attr (die, &attr);
5024 static inline unsigned HOST_WIDE_INT
5025 AT_unsigned (dw_attr_ref a)
5027 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
5028 return a->dw_attr_val.v.val_unsigned;
5031 /* Add an unsigned double integer attribute value to a DIE. */
5033 static inline void
5034 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
5035 long unsigned int val_hi, long unsigned int val_low)
5037 dw_attr_node attr;
5039 attr.dw_attr = attr_kind;
5040 attr.dw_attr_val.val_class = dw_val_class_long_long;
5041 attr.dw_attr_val.v.val_long_long.hi = val_hi;
5042 attr.dw_attr_val.v.val_long_long.low = val_low;
5043 add_dwarf_attr (die, &attr);
5046 /* Add a floating point attribute value to a DIE and return it. */
5048 static inline void
5049 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
5050 unsigned int length, unsigned int elt_size, unsigned char *array)
5052 dw_attr_node attr;
5054 attr.dw_attr = attr_kind;
5055 attr.dw_attr_val.val_class = dw_val_class_vec;
5056 attr.dw_attr_val.v.val_vec.length = length;
5057 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
5058 attr.dw_attr_val.v.val_vec.array = array;
5059 add_dwarf_attr (die, &attr);
5062 /* Hash and equality functions for debug_str_hash. */
5064 static hashval_t
5065 debug_str_do_hash (const void *x)
5067 return htab_hash_string (((const struct indirect_string_node *)x)->str);
5070 static int
5071 debug_str_eq (const void *x1, const void *x2)
5073 return strcmp ((((const struct indirect_string_node *)x1)->str),
5074 (const char *)x2) == 0;
5077 /* Add a string attribute value to a DIE. */
5079 static inline void
5080 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
5082 dw_attr_node attr;
5083 struct indirect_string_node *node;
5084 void **slot;
5086 if (! debug_str_hash)
5087 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
5088 debug_str_eq, NULL);
5090 slot = htab_find_slot_with_hash (debug_str_hash, str,
5091 htab_hash_string (str), INSERT);
5092 if (*slot == NULL)
5094 node = (struct indirect_string_node *)
5095 ggc_alloc_cleared (sizeof (struct indirect_string_node));
5096 node->str = ggc_strdup (str);
5097 *slot = node;
5099 else
5100 node = (struct indirect_string_node *) *slot;
5102 node->refcount++;
5104 attr.dw_attr = attr_kind;
5105 attr.dw_attr_val.val_class = dw_val_class_str;
5106 attr.dw_attr_val.v.val_str = node;
5107 add_dwarf_attr (die, &attr);
5110 static inline const char *
5111 AT_string (dw_attr_ref a)
5113 gcc_assert (a && AT_class (a) == dw_val_class_str);
5114 return a->dw_attr_val.v.val_str->str;
5117 /* Find out whether a string should be output inline in DIE
5118 or out-of-line in .debug_str section. */
5120 static int
5121 AT_string_form (dw_attr_ref a)
5123 struct indirect_string_node *node;
5124 unsigned int len;
5125 char label[32];
5127 gcc_assert (a && AT_class (a) == dw_val_class_str);
5129 node = a->dw_attr_val.v.val_str;
5130 if (node->form)
5131 return node->form;
5133 len = strlen (node->str) + 1;
5135 /* If the string is shorter or equal to the size of the reference, it is
5136 always better to put it inline. */
5137 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
5138 return node->form = DW_FORM_string;
5140 /* If we cannot expect the linker to merge strings in .debug_str
5141 section, only put it into .debug_str if it is worth even in this
5142 single module. */
5143 if ((debug_str_section->common.flags & SECTION_MERGE) == 0
5144 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
5145 return node->form = DW_FORM_string;
5147 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
5148 ++dw2_string_counter;
5149 node->label = xstrdup (label);
5151 return node->form = DW_FORM_strp;
5154 /* Add a DIE reference attribute value to a DIE. */
5156 static inline void
5157 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
5159 dw_attr_node attr;
5161 attr.dw_attr = attr_kind;
5162 attr.dw_attr_val.val_class = dw_val_class_die_ref;
5163 attr.dw_attr_val.v.val_die_ref.die = targ_die;
5164 attr.dw_attr_val.v.val_die_ref.external = 0;
5165 add_dwarf_attr (die, &attr);
5168 /* Add an AT_specification attribute to a DIE, and also make the back
5169 pointer from the specification to the definition. */
5171 static inline void
5172 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
5174 add_AT_die_ref (die, DW_AT_specification, targ_die);
5175 gcc_assert (!targ_die->die_definition);
5176 targ_die->die_definition = die;
5179 static inline dw_die_ref
5180 AT_ref (dw_attr_ref a)
5182 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5183 return a->dw_attr_val.v.val_die_ref.die;
5186 static inline int
5187 AT_ref_external (dw_attr_ref a)
5189 if (a && AT_class (a) == dw_val_class_die_ref)
5190 return a->dw_attr_val.v.val_die_ref.external;
5192 return 0;
5195 static inline void
5196 set_AT_ref_external (dw_attr_ref a, int i)
5198 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5199 a->dw_attr_val.v.val_die_ref.external = i;
5202 /* Add an FDE reference attribute value to a DIE. */
5204 static inline void
5205 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
5207 dw_attr_node attr;
5209 attr.dw_attr = attr_kind;
5210 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
5211 attr.dw_attr_val.v.val_fde_index = targ_fde;
5212 add_dwarf_attr (die, &attr);
5215 /* Add a location description attribute value to a DIE. */
5217 static inline void
5218 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
5220 dw_attr_node attr;
5222 attr.dw_attr = attr_kind;
5223 attr.dw_attr_val.val_class = dw_val_class_loc;
5224 attr.dw_attr_val.v.val_loc = loc;
5225 add_dwarf_attr (die, &attr);
5228 static inline dw_loc_descr_ref
5229 AT_loc (dw_attr_ref a)
5231 gcc_assert (a && AT_class (a) == dw_val_class_loc);
5232 return a->dw_attr_val.v.val_loc;
5235 static inline void
5236 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
5238 dw_attr_node attr;
5240 attr.dw_attr = attr_kind;
5241 attr.dw_attr_val.val_class = dw_val_class_loc_list;
5242 attr.dw_attr_val.v.val_loc_list = loc_list;
5243 add_dwarf_attr (die, &attr);
5244 have_location_lists = true;
5247 static inline dw_loc_list_ref
5248 AT_loc_list (dw_attr_ref a)
5250 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
5251 return a->dw_attr_val.v.val_loc_list;
5254 /* Add an address constant attribute value to a DIE. */
5256 static inline void
5257 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
5259 dw_attr_node attr;
5261 attr.dw_attr = attr_kind;
5262 attr.dw_attr_val.val_class = dw_val_class_addr;
5263 attr.dw_attr_val.v.val_addr = addr;
5264 add_dwarf_attr (die, &attr);
5267 /* Get the RTX from to an address DIE attribute. */
5269 static inline rtx
5270 AT_addr (dw_attr_ref a)
5272 gcc_assert (a && AT_class (a) == dw_val_class_addr);
5273 return a->dw_attr_val.v.val_addr;
5276 /* Add a file attribute value to a DIE. */
5278 static inline void
5279 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
5280 struct dwarf_file_data *fd)
5282 dw_attr_node attr;
5284 attr.dw_attr = attr_kind;
5285 attr.dw_attr_val.val_class = dw_val_class_file;
5286 attr.dw_attr_val.v.val_file = fd;
5287 add_dwarf_attr (die, &attr);
5290 /* Get the dwarf_file_data from a file DIE attribute. */
5292 static inline struct dwarf_file_data *
5293 AT_file (dw_attr_ref a)
5295 gcc_assert (a && AT_class (a) == dw_val_class_file);
5296 return a->dw_attr_val.v.val_file;
5299 /* Add a label identifier attribute value to a DIE. */
5301 static inline void
5302 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
5304 dw_attr_node attr;
5306 attr.dw_attr = attr_kind;
5307 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
5308 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5309 add_dwarf_attr (die, &attr);
5312 /* Add a section offset attribute value to a DIE, an offset into the
5313 debug_line section. */
5315 static inline void
5316 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5317 const char *label)
5319 dw_attr_node attr;
5321 attr.dw_attr = attr_kind;
5322 attr.dw_attr_val.val_class = dw_val_class_lineptr;
5323 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5324 add_dwarf_attr (die, &attr);
5327 /* Add a section offset attribute value to a DIE, an offset into the
5328 debug_macinfo section. */
5330 static inline void
5331 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5332 const char *label)
5334 dw_attr_node attr;
5336 attr.dw_attr = attr_kind;
5337 attr.dw_attr_val.val_class = dw_val_class_macptr;
5338 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5339 add_dwarf_attr (die, &attr);
5342 /* Add an offset attribute value to a DIE. */
5344 static inline void
5345 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
5346 unsigned HOST_WIDE_INT offset)
5348 dw_attr_node attr;
5350 attr.dw_attr = attr_kind;
5351 attr.dw_attr_val.val_class = dw_val_class_offset;
5352 attr.dw_attr_val.v.val_offset = offset;
5353 add_dwarf_attr (die, &attr);
5356 /* Add an range_list attribute value to a DIE. */
5358 static void
5359 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5360 long unsigned int offset)
5362 dw_attr_node attr;
5364 attr.dw_attr = attr_kind;
5365 attr.dw_attr_val.val_class = dw_val_class_range_list;
5366 attr.dw_attr_val.v.val_offset = offset;
5367 add_dwarf_attr (die, &attr);
5370 static inline const char *
5371 AT_lbl (dw_attr_ref a)
5373 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5374 || AT_class (a) == dw_val_class_lineptr
5375 || AT_class (a) == dw_val_class_macptr));
5376 return a->dw_attr_val.v.val_lbl_id;
5379 /* Get the attribute of type attr_kind. */
5381 static dw_attr_ref
5382 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5384 dw_attr_ref a;
5385 unsigned ix;
5386 dw_die_ref spec = NULL;
5388 if (! die)
5389 return NULL;
5391 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5392 if (a->dw_attr == attr_kind)
5393 return a;
5394 else if (a->dw_attr == DW_AT_specification
5395 || a->dw_attr == DW_AT_abstract_origin)
5396 spec = AT_ref (a);
5398 if (spec)
5399 return get_AT (spec, attr_kind);
5401 return NULL;
5404 /* Return the "low pc" attribute value, typically associated with a subprogram
5405 DIE. Return null if the "low pc" attribute is either not present, or if it
5406 cannot be represented as an assembler label identifier. */
5408 static inline const char *
5409 get_AT_low_pc (dw_die_ref die)
5411 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
5413 return a ? AT_lbl (a) : NULL;
5416 /* Return the "high pc" attribute value, typically associated with a subprogram
5417 DIE. Return null if the "high pc" attribute is either not present, or if it
5418 cannot be represented as an assembler label identifier. */
5420 static inline const char *
5421 get_AT_hi_pc (dw_die_ref die)
5423 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
5425 return a ? AT_lbl (a) : NULL;
5428 /* Return the value of the string attribute designated by ATTR_KIND, or
5429 NULL if it is not present. */
5431 static inline const char *
5432 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5434 dw_attr_ref a = get_AT (die, attr_kind);
5436 return a ? AT_string (a) : NULL;
5439 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5440 if it is not present. */
5442 static inline int
5443 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5445 dw_attr_ref a = get_AT (die, attr_kind);
5447 return a ? AT_flag (a) : 0;
5450 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5451 if it is not present. */
5453 static inline unsigned
5454 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5456 dw_attr_ref a = get_AT (die, attr_kind);
5458 return a ? AT_unsigned (a) : 0;
5461 static inline dw_die_ref
5462 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5464 dw_attr_ref a = get_AT (die, attr_kind);
5466 return a ? AT_ref (a) : NULL;
5469 static inline struct dwarf_file_data *
5470 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5472 dw_attr_ref a = get_AT (die, attr_kind);
5474 return a ? AT_file (a) : NULL;
5477 /* Return TRUE if the language is C or C++. */
5479 static inline bool
5480 is_c_family (void)
5482 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5484 return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
5485 || lang == DW_LANG_C99
5486 || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
5489 /* Return TRUE if the language is C++. */
5491 static inline bool
5492 is_cxx (void)
5494 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5496 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
5499 /* Return TRUE if the language is Fortran. */
5501 static inline bool
5502 is_fortran (void)
5504 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5506 return (lang == DW_LANG_Fortran77
5507 || lang == DW_LANG_Fortran90
5508 || lang == DW_LANG_Fortran95);
5511 /* Return TRUE if the language is Java. */
5513 static inline bool
5514 is_java (void)
5516 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5518 return lang == DW_LANG_Java;
5521 /* Return TRUE if the language is Ada. */
5523 static inline bool
5524 is_ada (void)
5526 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5528 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5531 /* Remove the specified attribute if present. */
5533 static void
5534 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5536 dw_attr_ref a;
5537 unsigned ix;
5539 if (! die)
5540 return;
5542 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5543 if (a->dw_attr == attr_kind)
5545 if (AT_class (a) == dw_val_class_str)
5546 if (a->dw_attr_val.v.val_str->refcount)
5547 a->dw_attr_val.v.val_str->refcount--;
5549 /* VEC_ordered_remove should help reduce the number of abbrevs
5550 that are needed. */
5551 VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
5552 return;
5556 /* Remove CHILD from its parent. PREV must have the property that
5557 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5559 static void
5560 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5562 gcc_assert (child->die_parent == prev->die_parent);
5563 gcc_assert (prev->die_sib == child);
5564 if (prev == child)
5566 gcc_assert (child->die_parent->die_child == child);
5567 prev = NULL;
5569 else
5570 prev->die_sib = child->die_sib;
5571 if (child->die_parent->die_child == child)
5572 child->die_parent->die_child = prev;
5575 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
5576 matches TAG. */
5578 static void
5579 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5581 dw_die_ref c;
5583 c = die->die_child;
5584 if (c) do {
5585 dw_die_ref prev = c;
5586 c = c->die_sib;
5587 while (c->die_tag == tag)
5589 remove_child_with_prev (c, prev);
5590 /* Might have removed every child. */
5591 if (c == c->die_sib)
5592 return;
5593 c = c->die_sib;
5595 } while (c != die->die_child);
5598 /* Add a CHILD_DIE as the last child of DIE. */
5600 static void
5601 add_child_die (dw_die_ref die, dw_die_ref child_die)
5603 /* FIXME this should probably be an assert. */
5604 if (! die || ! child_die)
5605 return;
5606 gcc_assert (die != child_die);
5608 child_die->die_parent = die;
5609 if (die->die_child)
5611 child_die->die_sib = die->die_child->die_sib;
5612 die->die_child->die_sib = child_die;
5614 else
5615 child_die->die_sib = child_die;
5616 die->die_child = child_die;
5619 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5620 is the specification, to the end of PARENT's list of children.
5621 This is done by removing and re-adding it. */
5623 static void
5624 splice_child_die (dw_die_ref parent, dw_die_ref child)
5626 dw_die_ref p;
5628 /* We want the declaration DIE from inside the class, not the
5629 specification DIE at toplevel. */
5630 if (child->die_parent != parent)
5632 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5634 if (tmp)
5635 child = tmp;
5638 gcc_assert (child->die_parent == parent
5639 || (child->die_parent
5640 == get_AT_ref (parent, DW_AT_specification)));
5642 for (p = child->die_parent->die_child; ; p = p->die_sib)
5643 if (p->die_sib == child)
5645 remove_child_with_prev (child, p);
5646 break;
5649 add_child_die (parent, child);
5652 /* Return a pointer to a newly created DIE node. */
5654 static inline dw_die_ref
5655 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5657 dw_die_ref die = ggc_alloc_cleared (sizeof (die_node));
5659 die->die_tag = tag_value;
5661 if (parent_die != NULL)
5662 add_child_die (parent_die, die);
5663 else
5665 limbo_die_node *limbo_node;
5667 limbo_node = ggc_alloc_cleared (sizeof (limbo_die_node));
5668 limbo_node->die = die;
5669 limbo_node->created_for = t;
5670 limbo_node->next = limbo_die_list;
5671 limbo_die_list = limbo_node;
5674 return die;
5677 /* Return the DIE associated with the given type specifier. */
5679 static inline dw_die_ref
5680 lookup_type_die (tree type)
5682 return TYPE_SYMTAB_DIE (type);
5685 /* Equate a DIE to a given type specifier. */
5687 static inline void
5688 equate_type_number_to_die (tree type, dw_die_ref type_die)
5690 TYPE_SYMTAB_DIE (type) = type_die;
5693 /* Returns a hash value for X (which really is a die_struct). */
5695 static hashval_t
5696 decl_die_table_hash (const void *x)
5698 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
5701 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5703 static int
5704 decl_die_table_eq (const void *x, const void *y)
5706 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
5709 /* Return the DIE associated with a given declaration. */
5711 static inline dw_die_ref
5712 lookup_decl_die (tree decl)
5714 return htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
5717 /* Returns a hash value for X (which really is a var_loc_list). */
5719 static hashval_t
5720 decl_loc_table_hash (const void *x)
5722 return (hashval_t) ((const var_loc_list *) x)->decl_id;
5725 /* Return nonzero if decl_id of var_loc_list X is the same as
5726 UID of decl *Y. */
5728 static int
5729 decl_loc_table_eq (const void *x, const void *y)
5731 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
5734 /* Return the var_loc list associated with a given declaration. */
5736 static inline var_loc_list *
5737 lookup_decl_loc (const_tree decl)
5739 return htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5742 /* Equate a DIE to a particular declaration. */
5744 static void
5745 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5747 unsigned int decl_id = DECL_UID (decl);
5748 void **slot;
5750 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5751 *slot = decl_die;
5752 decl_die->decl_id = decl_id;
5755 /* Add a variable location node to the linked list for DECL. */
5757 static void
5758 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
5760 unsigned int decl_id = DECL_UID (decl);
5761 var_loc_list *temp;
5762 void **slot;
5764 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5765 if (*slot == NULL)
5767 temp = ggc_alloc_cleared (sizeof (var_loc_list));
5768 temp->decl_id = decl_id;
5769 *slot = temp;
5771 else
5772 temp = *slot;
5774 if (temp->last)
5776 /* If the current location is the same as the end of the list,
5777 and either both or neither of the locations is uninitialized,
5778 we have nothing to do. */
5779 if ((!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
5780 NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
5781 || ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
5782 != NOTE_VAR_LOCATION_STATUS (loc->var_loc_note))
5783 && ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
5784 == VAR_INIT_STATUS_UNINITIALIZED)
5785 || (NOTE_VAR_LOCATION_STATUS (loc->var_loc_note)
5786 == VAR_INIT_STATUS_UNINITIALIZED))))
5788 /* Add LOC to the end of list and update LAST. */
5789 temp->last->next = loc;
5790 temp->last = loc;
5793 /* Do not add empty location to the beginning of the list. */
5794 else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
5796 temp->first = loc;
5797 temp->last = loc;
5801 /* Keep track of the number of spaces used to indent the
5802 output of the debugging routines that print the structure of
5803 the DIE internal representation. */
5804 static int print_indent;
5806 /* Indent the line the number of spaces given by print_indent. */
5808 static inline void
5809 print_spaces (FILE *outfile)
5811 fprintf (outfile, "%*s", print_indent, "");
5814 /* Print the information associated with a given DIE, and its children.
5815 This routine is a debugging aid only. */
5817 static void
5818 print_die (dw_die_ref die, FILE *outfile)
5820 dw_attr_ref a;
5821 dw_die_ref c;
5822 unsigned ix;
5824 print_spaces (outfile);
5825 fprintf (outfile, "DIE %4ld: %s\n",
5826 die->die_offset, dwarf_tag_name (die->die_tag));
5827 print_spaces (outfile);
5828 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5829 fprintf (outfile, " offset: %ld\n", die->die_offset);
5831 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5833 print_spaces (outfile);
5834 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5836 switch (AT_class (a))
5838 case dw_val_class_addr:
5839 fprintf (outfile, "address");
5840 break;
5841 case dw_val_class_offset:
5842 fprintf (outfile, "offset");
5843 break;
5844 case dw_val_class_loc:
5845 fprintf (outfile, "location descriptor");
5846 break;
5847 case dw_val_class_loc_list:
5848 fprintf (outfile, "location list -> label:%s",
5849 AT_loc_list (a)->ll_symbol);
5850 break;
5851 case dw_val_class_range_list:
5852 fprintf (outfile, "range list");
5853 break;
5854 case dw_val_class_const:
5855 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5856 break;
5857 case dw_val_class_unsigned_const:
5858 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5859 break;
5860 case dw_val_class_long_long:
5861 fprintf (outfile, "constant (%lu,%lu)",
5862 a->dw_attr_val.v.val_long_long.hi,
5863 a->dw_attr_val.v.val_long_long.low);
5864 break;
5865 case dw_val_class_vec:
5866 fprintf (outfile, "floating-point or vector constant");
5867 break;
5868 case dw_val_class_flag:
5869 fprintf (outfile, "%u", AT_flag (a));
5870 break;
5871 case dw_val_class_die_ref:
5872 if (AT_ref (a) != NULL)
5874 if (AT_ref (a)->die_symbol)
5875 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
5876 else
5877 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5879 else
5880 fprintf (outfile, "die -> <null>");
5881 break;
5882 case dw_val_class_lbl_id:
5883 case dw_val_class_lineptr:
5884 case dw_val_class_macptr:
5885 fprintf (outfile, "label: %s", AT_lbl (a));
5886 break;
5887 case dw_val_class_str:
5888 if (AT_string (a) != NULL)
5889 fprintf (outfile, "\"%s\"", AT_string (a));
5890 else
5891 fprintf (outfile, "<null>");
5892 break;
5893 case dw_val_class_file:
5894 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5895 AT_file (a)->emitted_number);
5896 break;
5897 default:
5898 break;
5901 fprintf (outfile, "\n");
5904 if (die->die_child != NULL)
5906 print_indent += 4;
5907 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5908 print_indent -= 4;
5910 if (print_indent == 0)
5911 fprintf (outfile, "\n");
5914 /* Print the contents of the source code line number correspondence table.
5915 This routine is a debugging aid only. */
5917 static void
5918 print_dwarf_line_table (FILE *outfile)
5920 unsigned i;
5921 dw_line_info_ref line_info;
5923 fprintf (outfile, "\n\nDWARF source line information\n");
5924 for (i = 1; i < line_info_table_in_use; i++)
5926 line_info = &line_info_table[i];
5927 fprintf (outfile, "%5d: %4ld %6ld\n", i,
5928 line_info->dw_file_num,
5929 line_info->dw_line_num);
5932 fprintf (outfile, "\n\n");
5935 /* Print the information collected for a given DIE. */
5937 void
5938 debug_dwarf_die (dw_die_ref die)
5940 print_die (die, stderr);
5943 /* Print all DWARF information collected for the compilation unit.
5944 This routine is a debugging aid only. */
5946 void
5947 debug_dwarf (void)
5949 print_indent = 0;
5950 print_die (comp_unit_die, stderr);
5951 if (! DWARF2_ASM_LINE_DEBUG_INFO)
5952 print_dwarf_line_table (stderr);
5955 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5956 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5957 DIE that marks the start of the DIEs for this include file. */
5959 static dw_die_ref
5960 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5962 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5963 dw_die_ref new_unit = gen_compile_unit_die (filename);
5965 new_unit->die_sib = old_unit;
5966 return new_unit;
5969 /* Close an include-file CU and reopen the enclosing one. */
5971 static dw_die_ref
5972 pop_compile_unit (dw_die_ref old_unit)
5974 dw_die_ref new_unit = old_unit->die_sib;
5976 old_unit->die_sib = NULL;
5977 return new_unit;
5980 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5981 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5983 /* Calculate the checksum of a location expression. */
5985 static inline void
5986 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5988 CHECKSUM (loc->dw_loc_opc);
5989 CHECKSUM (loc->dw_loc_oprnd1);
5990 CHECKSUM (loc->dw_loc_oprnd2);
5993 /* Calculate the checksum of an attribute. */
5995 static void
5996 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5998 dw_loc_descr_ref loc;
5999 rtx r;
6001 CHECKSUM (at->dw_attr);
6003 /* We don't care that this was compiled with a different compiler
6004 snapshot; if the output is the same, that's what matters. */
6005 if (at->dw_attr == DW_AT_producer)
6006 return;
6008 switch (AT_class (at))
6010 case dw_val_class_const:
6011 CHECKSUM (at->dw_attr_val.v.val_int);
6012 break;
6013 case dw_val_class_unsigned_const:
6014 CHECKSUM (at->dw_attr_val.v.val_unsigned);
6015 break;
6016 case dw_val_class_long_long:
6017 CHECKSUM (at->dw_attr_val.v.val_long_long);
6018 break;
6019 case dw_val_class_vec:
6020 CHECKSUM (at->dw_attr_val.v.val_vec);
6021 break;
6022 case dw_val_class_flag:
6023 CHECKSUM (at->dw_attr_val.v.val_flag);
6024 break;
6025 case dw_val_class_str:
6026 CHECKSUM_STRING (AT_string (at));
6027 break;
6029 case dw_val_class_addr:
6030 r = AT_addr (at);
6031 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6032 CHECKSUM_STRING (XSTR (r, 0));
6033 break;
6035 case dw_val_class_offset:
6036 CHECKSUM (at->dw_attr_val.v.val_offset);
6037 break;
6039 case dw_val_class_loc:
6040 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6041 loc_checksum (loc, ctx);
6042 break;
6044 case dw_val_class_die_ref:
6045 die_checksum (AT_ref (at), ctx, mark);
6046 break;
6048 case dw_val_class_fde_ref:
6049 case dw_val_class_lbl_id:
6050 case dw_val_class_lineptr:
6051 case dw_val_class_macptr:
6052 break;
6054 case dw_val_class_file:
6055 CHECKSUM_STRING (AT_file (at)->filename);
6056 break;
6058 default:
6059 break;
6063 /* Calculate the checksum of a DIE. */
6065 static void
6066 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6068 dw_die_ref c;
6069 dw_attr_ref a;
6070 unsigned ix;
6072 /* To avoid infinite recursion. */
6073 if (die->die_mark)
6075 CHECKSUM (die->die_mark);
6076 return;
6078 die->die_mark = ++(*mark);
6080 CHECKSUM (die->die_tag);
6082 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6083 attr_checksum (a, ctx, mark);
6085 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6088 #undef CHECKSUM
6089 #undef CHECKSUM_STRING
6091 /* Do the location expressions look same? */
6092 static inline int
6093 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6095 return loc1->dw_loc_opc == loc2->dw_loc_opc
6096 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6097 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6100 /* Do the values look the same? */
6101 static int
6102 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6104 dw_loc_descr_ref loc1, loc2;
6105 rtx r1, r2;
6107 if (v1->val_class != v2->val_class)
6108 return 0;
6110 switch (v1->val_class)
6112 case dw_val_class_const:
6113 return v1->v.val_int == v2->v.val_int;
6114 case dw_val_class_unsigned_const:
6115 return v1->v.val_unsigned == v2->v.val_unsigned;
6116 case dw_val_class_long_long:
6117 return v1->v.val_long_long.hi == v2->v.val_long_long.hi
6118 && v1->v.val_long_long.low == v2->v.val_long_long.low;
6119 case dw_val_class_vec:
6120 if (v1->v.val_vec.length != v2->v.val_vec.length
6121 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6122 return 0;
6123 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6124 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6125 return 0;
6126 return 1;
6127 case dw_val_class_flag:
6128 return v1->v.val_flag == v2->v.val_flag;
6129 case dw_val_class_str:
6130 return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6132 case dw_val_class_addr:
6133 r1 = v1->v.val_addr;
6134 r2 = v2->v.val_addr;
6135 if (GET_CODE (r1) != GET_CODE (r2))
6136 return 0;
6137 gcc_assert (GET_CODE (r1) == SYMBOL_REF);
6138 return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
6140 case dw_val_class_offset:
6141 return v1->v.val_offset == v2->v.val_offset;
6143 case dw_val_class_loc:
6144 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6145 loc1 && loc2;
6146 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6147 if (!same_loc_p (loc1, loc2, mark))
6148 return 0;
6149 return !loc1 && !loc2;
6151 case dw_val_class_die_ref:
6152 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6154 case dw_val_class_fde_ref:
6155 case dw_val_class_lbl_id:
6156 case dw_val_class_lineptr:
6157 case dw_val_class_macptr:
6158 return 1;
6160 case dw_val_class_file:
6161 return v1->v.val_file == v2->v.val_file;
6163 default:
6164 return 1;
6168 /* Do the attributes look the same? */
6170 static int
6171 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6173 if (at1->dw_attr != at2->dw_attr)
6174 return 0;
6176 /* We don't care that this was compiled with a different compiler
6177 snapshot; if the output is the same, that's what matters. */
6178 if (at1->dw_attr == DW_AT_producer)
6179 return 1;
6181 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6184 /* Do the dies look the same? */
6186 static int
6187 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6189 dw_die_ref c1, c2;
6190 dw_attr_ref a1;
6191 unsigned ix;
6193 /* To avoid infinite recursion. */
6194 if (die1->die_mark)
6195 return die1->die_mark == die2->die_mark;
6196 die1->die_mark = die2->die_mark = ++(*mark);
6198 if (die1->die_tag != die2->die_tag)
6199 return 0;
6201 if (VEC_length (dw_attr_node, die1->die_attr)
6202 != VEC_length (dw_attr_node, die2->die_attr))
6203 return 0;
6205 for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
6206 if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
6207 return 0;
6209 c1 = die1->die_child;
6210 c2 = die2->die_child;
6211 if (! c1)
6213 if (c2)
6214 return 0;
6216 else
6217 for (;;)
6219 if (!same_die_p (c1, c2, mark))
6220 return 0;
6221 c1 = c1->die_sib;
6222 c2 = c2->die_sib;
6223 if (c1 == die1->die_child)
6225 if (c2 == die2->die_child)
6226 break;
6227 else
6228 return 0;
6232 return 1;
6235 /* Do the dies look the same? Wrapper around same_die_p. */
6237 static int
6238 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6240 int mark = 0;
6241 int ret = same_die_p (die1, die2, &mark);
6243 unmark_all_dies (die1);
6244 unmark_all_dies (die2);
6246 return ret;
6249 /* The prefix to attach to symbols on DIEs in the current comdat debug
6250 info section. */
6251 static char *comdat_symbol_id;
6253 /* The index of the current symbol within the current comdat CU. */
6254 static unsigned int comdat_symbol_number;
6256 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6257 children, and set comdat_symbol_id accordingly. */
6259 static void
6260 compute_section_prefix (dw_die_ref unit_die)
6262 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6263 const char *base = die_name ? lbasename (die_name) : "anonymous";
6264 char *name = alloca (strlen (base) + 64);
6265 char *p;
6266 int i, mark;
6267 unsigned char checksum[16];
6268 struct md5_ctx ctx;
6270 /* Compute the checksum of the DIE, then append part of it as hex digits to
6271 the name filename of the unit. */
6273 md5_init_ctx (&ctx);
6274 mark = 0;
6275 die_checksum (unit_die, &ctx, &mark);
6276 unmark_all_dies (unit_die);
6277 md5_finish_ctx (&ctx, checksum);
6279 sprintf (name, "%s.", base);
6280 clean_symbol_name (name);
6282 p = name + strlen (name);
6283 for (i = 0; i < 4; i++)
6285 sprintf (p, "%.2x", checksum[i]);
6286 p += 2;
6289 comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
6290 comdat_symbol_number = 0;
6293 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6295 static int
6296 is_type_die (dw_die_ref die)
6298 switch (die->die_tag)
6300 case DW_TAG_array_type:
6301 case DW_TAG_class_type:
6302 case DW_TAG_enumeration_type:
6303 case DW_TAG_pointer_type:
6304 case DW_TAG_reference_type:
6305 case DW_TAG_string_type:
6306 case DW_TAG_structure_type:
6307 case DW_TAG_subroutine_type:
6308 case DW_TAG_union_type:
6309 case DW_TAG_ptr_to_member_type:
6310 case DW_TAG_set_type:
6311 case DW_TAG_subrange_type:
6312 case DW_TAG_base_type:
6313 case DW_TAG_const_type:
6314 case DW_TAG_file_type:
6315 case DW_TAG_packed_type:
6316 case DW_TAG_volatile_type:
6317 case DW_TAG_typedef:
6318 return 1;
6319 default:
6320 return 0;
6324 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6325 Basically, we want to choose the bits that are likely to be shared between
6326 compilations (types) and leave out the bits that are specific to individual
6327 compilations (functions). */
6329 static int
6330 is_comdat_die (dw_die_ref c)
6332 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6333 we do for stabs. The advantage is a greater likelihood of sharing between
6334 objects that don't include headers in the same order (and therefore would
6335 put the base types in a different comdat). jason 8/28/00 */
6337 if (c->die_tag == DW_TAG_base_type)
6338 return 0;
6340 if (c->die_tag == DW_TAG_pointer_type
6341 || c->die_tag == DW_TAG_reference_type
6342 || c->die_tag == DW_TAG_const_type
6343 || c->die_tag == DW_TAG_volatile_type)
6345 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6347 return t ? is_comdat_die (t) : 0;
6350 return is_type_die (c);
6353 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6354 compilation unit. */
6356 static int
6357 is_symbol_die (dw_die_ref c)
6359 return (is_type_die (c)
6360 || (get_AT (c, DW_AT_declaration)
6361 && !get_AT (c, DW_AT_specification))
6362 || c->die_tag == DW_TAG_namespace);
6365 static char *
6366 gen_internal_sym (const char *prefix)
6368 char buf[256];
6370 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6371 return xstrdup (buf);
6374 /* Assign symbols to all worthy DIEs under DIE. */
6376 static void
6377 assign_symbol_names (dw_die_ref die)
6379 dw_die_ref c;
6381 if (is_symbol_die (die))
6383 if (comdat_symbol_id)
6385 char *p = alloca (strlen (comdat_symbol_id) + 64);
6387 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6388 comdat_symbol_id, comdat_symbol_number++);
6389 die->die_symbol = xstrdup (p);
6391 else
6392 die->die_symbol = gen_internal_sym ("LDIE");
6395 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6398 struct cu_hash_table_entry
6400 dw_die_ref cu;
6401 unsigned min_comdat_num, max_comdat_num;
6402 struct cu_hash_table_entry *next;
6405 /* Routines to manipulate hash table of CUs. */
6406 static hashval_t
6407 htab_cu_hash (const void *of)
6409 const struct cu_hash_table_entry *entry = of;
6411 return htab_hash_string (entry->cu->die_symbol);
6414 static int
6415 htab_cu_eq (const void *of1, const void *of2)
6417 const struct cu_hash_table_entry *entry1 = of1;
6418 const struct die_struct *entry2 = of2;
6420 return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
6423 static void
6424 htab_cu_del (void *what)
6426 struct cu_hash_table_entry *next, *entry = what;
6428 while (entry)
6430 next = entry->next;
6431 free (entry);
6432 entry = next;
6436 /* Check whether we have already seen this CU and set up SYM_NUM
6437 accordingly. */
6438 static int
6439 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6441 struct cu_hash_table_entry dummy;
6442 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6444 dummy.max_comdat_num = 0;
6446 slot = (struct cu_hash_table_entry **)
6447 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6448 INSERT);
6449 entry = *slot;
6451 for (; entry; last = entry, entry = entry->next)
6453 if (same_die_p_wrap (cu, entry->cu))
6454 break;
6457 if (entry)
6459 *sym_num = entry->min_comdat_num;
6460 return 1;
6463 entry = XCNEW (struct cu_hash_table_entry);
6464 entry->cu = cu;
6465 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6466 entry->next = *slot;
6467 *slot = entry;
6469 return 0;
6472 /* Record SYM_NUM to record of CU in HTABLE. */
6473 static void
6474 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6476 struct cu_hash_table_entry **slot, *entry;
6478 slot = (struct cu_hash_table_entry **)
6479 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6480 NO_INSERT);
6481 entry = *slot;
6483 entry->max_comdat_num = sym_num;
6486 /* Traverse the DIE (which is always comp_unit_die), and set up
6487 additional compilation units for each of the include files we see
6488 bracketed by BINCL/EINCL. */
6490 static void
6491 break_out_includes (dw_die_ref die)
6493 dw_die_ref c;
6494 dw_die_ref unit = NULL;
6495 limbo_die_node *node, **pnode;
6496 htab_t cu_hash_table;
6498 c = die->die_child;
6499 if (c) do {
6500 dw_die_ref prev = c;
6501 c = c->die_sib;
6502 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6503 || (unit && is_comdat_die (c)))
6505 dw_die_ref next = c->die_sib;
6507 /* This DIE is for a secondary CU; remove it from the main one. */
6508 remove_child_with_prev (c, prev);
6510 if (c->die_tag == DW_TAG_GNU_BINCL)
6511 unit = push_new_compile_unit (unit, c);
6512 else if (c->die_tag == DW_TAG_GNU_EINCL)
6513 unit = pop_compile_unit (unit);
6514 else
6515 add_child_die (unit, c);
6516 c = next;
6517 if (c == die->die_child)
6518 break;
6520 } while (c != die->die_child);
6522 #if 0
6523 /* We can only use this in debugging, since the frontend doesn't check
6524 to make sure that we leave every include file we enter. */
6525 gcc_assert (!unit);
6526 #endif
6528 assign_symbol_names (die);
6529 cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6530 for (node = limbo_die_list, pnode = &limbo_die_list;
6531 node;
6532 node = node->next)
6534 int is_dupl;
6536 compute_section_prefix (node->die);
6537 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6538 &comdat_symbol_number);
6539 assign_symbol_names (node->die);
6540 if (is_dupl)
6541 *pnode = node->next;
6542 else
6544 pnode = &node->next;
6545 record_comdat_symbol_number (node->die, cu_hash_table,
6546 comdat_symbol_number);
6549 htab_delete (cu_hash_table);
6552 /* Traverse the DIE and add a sibling attribute if it may have the
6553 effect of speeding up access to siblings. To save some space,
6554 avoid generating sibling attributes for DIE's without children. */
6556 static void
6557 add_sibling_attributes (dw_die_ref die)
6559 dw_die_ref c;
6561 if (! die->die_child)
6562 return;
6564 if (die->die_parent && die != die->die_parent->die_child)
6565 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
6567 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
6570 /* Output all location lists for the DIE and its children. */
6572 static void
6573 output_location_lists (dw_die_ref die)
6575 dw_die_ref c;
6576 dw_attr_ref a;
6577 unsigned ix;
6579 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6580 if (AT_class (a) == dw_val_class_loc_list)
6581 output_loc_list (AT_loc_list (a));
6583 FOR_EACH_CHILD (die, c, output_location_lists (c));
6586 /* The format of each DIE (and its attribute value pairs) is encoded in an
6587 abbreviation table. This routine builds the abbreviation table and assigns
6588 a unique abbreviation id for each abbreviation entry. The children of each
6589 die are visited recursively. */
6591 static void
6592 build_abbrev_table (dw_die_ref die)
6594 unsigned long abbrev_id;
6595 unsigned int n_alloc;
6596 dw_die_ref c;
6597 dw_attr_ref a;
6598 unsigned ix;
6600 /* Scan the DIE references, and mark as external any that refer to
6601 DIEs from other CUs (i.e. those which are not marked). */
6602 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6603 if (AT_class (a) == dw_val_class_die_ref
6604 && AT_ref (a)->die_mark == 0)
6606 gcc_assert (AT_ref (a)->die_symbol);
6608 set_AT_ref_external (a, 1);
6611 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6613 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6614 dw_attr_ref die_a, abbrev_a;
6615 unsigned ix;
6616 bool ok = true;
6618 if (abbrev->die_tag != die->die_tag)
6619 continue;
6620 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
6621 continue;
6623 if (VEC_length (dw_attr_node, abbrev->die_attr)
6624 != VEC_length (dw_attr_node, die->die_attr))
6625 continue;
6627 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
6629 abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
6630 if ((abbrev_a->dw_attr != die_a->dw_attr)
6631 || (value_format (abbrev_a) != value_format (die_a)))
6633 ok = false;
6634 break;
6637 if (ok)
6638 break;
6641 if (abbrev_id >= abbrev_die_table_in_use)
6643 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
6645 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
6646 abbrev_die_table = ggc_realloc (abbrev_die_table,
6647 sizeof (dw_die_ref) * n_alloc);
6649 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
6650 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
6651 abbrev_die_table_allocated = n_alloc;
6654 ++abbrev_die_table_in_use;
6655 abbrev_die_table[abbrev_id] = die;
6658 die->die_abbrev = abbrev_id;
6659 FOR_EACH_CHILD (die, c, build_abbrev_table (c));
6662 /* Return the power-of-two number of bytes necessary to represent VALUE. */
6664 static int
6665 constant_size (long unsigned int value)
6667 int log;
6669 if (value == 0)
6670 log = 0;
6671 else
6672 log = floor_log2 (value);
6674 log = log / 8;
6675 log = 1 << (floor_log2 (log) + 1);
6677 return log;
6680 /* Return the size of a DIE as it is represented in the
6681 .debug_info section. */
6683 static unsigned long
6684 size_of_die (dw_die_ref die)
6686 unsigned long size = 0;
6687 dw_attr_ref a;
6688 unsigned ix;
6690 size += size_of_uleb128 (die->die_abbrev);
6691 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6693 switch (AT_class (a))
6695 case dw_val_class_addr:
6696 size += DWARF2_ADDR_SIZE;
6697 break;
6698 case dw_val_class_offset:
6699 size += DWARF_OFFSET_SIZE;
6700 break;
6701 case dw_val_class_loc:
6703 unsigned long lsize = size_of_locs (AT_loc (a));
6705 /* Block length. */
6706 size += constant_size (lsize);
6707 size += lsize;
6709 break;
6710 case dw_val_class_loc_list:
6711 size += DWARF_OFFSET_SIZE;
6712 break;
6713 case dw_val_class_range_list:
6714 size += DWARF_OFFSET_SIZE;
6715 break;
6716 case dw_val_class_const:
6717 size += size_of_sleb128 (AT_int (a));
6718 break;
6719 case dw_val_class_unsigned_const:
6720 size += constant_size (AT_unsigned (a));
6721 break;
6722 case dw_val_class_long_long:
6723 size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
6724 break;
6725 case dw_val_class_vec:
6726 size += 1 + (a->dw_attr_val.v.val_vec.length
6727 * a->dw_attr_val.v.val_vec.elt_size); /* block */
6728 break;
6729 case dw_val_class_flag:
6730 size += 1;
6731 break;
6732 case dw_val_class_die_ref:
6733 if (AT_ref_external (a))
6734 size += DWARF2_ADDR_SIZE;
6735 else
6736 size += DWARF_OFFSET_SIZE;
6737 break;
6738 case dw_val_class_fde_ref:
6739 size += DWARF_OFFSET_SIZE;
6740 break;
6741 case dw_val_class_lbl_id:
6742 size += DWARF2_ADDR_SIZE;
6743 break;
6744 case dw_val_class_lineptr:
6745 case dw_val_class_macptr:
6746 size += DWARF_OFFSET_SIZE;
6747 break;
6748 case dw_val_class_str:
6749 if (AT_string_form (a) == DW_FORM_strp)
6750 size += DWARF_OFFSET_SIZE;
6751 else
6752 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
6753 break;
6754 case dw_val_class_file:
6755 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
6756 break;
6757 default:
6758 gcc_unreachable ();
6762 return size;
6765 /* Size the debugging information associated with a given DIE. Visits the
6766 DIE's children recursively. Updates the global variable next_die_offset, on
6767 each time through. Uses the current value of next_die_offset to update the
6768 die_offset field in each DIE. */
6770 static void
6771 calc_die_sizes (dw_die_ref die)
6773 dw_die_ref c;
6775 die->die_offset = next_die_offset;
6776 next_die_offset += size_of_die (die);
6778 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
6780 if (die->die_child != NULL)
6781 /* Count the null byte used to terminate sibling lists. */
6782 next_die_offset += 1;
6785 /* Set the marks for a die and its children. We do this so
6786 that we know whether or not a reference needs to use FORM_ref_addr; only
6787 DIEs in the same CU will be marked. We used to clear out the offset
6788 and use that as the flag, but ran into ordering problems. */
6790 static void
6791 mark_dies (dw_die_ref die)
6793 dw_die_ref c;
6795 gcc_assert (!die->die_mark);
6797 die->die_mark = 1;
6798 FOR_EACH_CHILD (die, c, mark_dies (c));
6801 /* Clear the marks for a die and its children. */
6803 static void
6804 unmark_dies (dw_die_ref die)
6806 dw_die_ref c;
6808 gcc_assert (die->die_mark);
6810 die->die_mark = 0;
6811 FOR_EACH_CHILD (die, c, unmark_dies (c));
6814 /* Clear the marks for a die, its children and referred dies. */
6816 static void
6817 unmark_all_dies (dw_die_ref die)
6819 dw_die_ref c;
6820 dw_attr_ref a;
6821 unsigned ix;
6823 if (!die->die_mark)
6824 return;
6825 die->die_mark = 0;
6827 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
6829 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6830 if (AT_class (a) == dw_val_class_die_ref)
6831 unmark_all_dies (AT_ref (a));
6834 /* Return the size of the .debug_pubnames or .debug_pubtypes table
6835 generated for the compilation unit. */
6837 static unsigned long
6838 size_of_pubnames (VEC (pubname_entry, gc) * names)
6840 unsigned long size;
6841 unsigned i;
6842 pubname_ref p;
6844 size = DWARF_PUBNAMES_HEADER_SIZE;
6845 for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
6846 if (names != pubtype_table
6847 || p->die->die_offset != 0
6848 || !flag_eliminate_unused_debug_types)
6849 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
6851 size += DWARF_OFFSET_SIZE;
6852 return size;
6855 /* Return the size of the information in the .debug_aranges section. */
6857 static unsigned long
6858 size_of_aranges (void)
6860 unsigned long size;
6862 size = DWARF_ARANGES_HEADER_SIZE;
6864 /* Count the address/length pair for this compilation unit. */
6865 if (text_section_used)
6866 size += 2 * DWARF2_ADDR_SIZE;
6867 if (cold_text_section_used)
6868 size += 2 * DWARF2_ADDR_SIZE;
6869 size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
6871 /* Count the two zero words used to terminated the address range table. */
6872 size += 2 * DWARF2_ADDR_SIZE;
6873 return size;
6876 /* Select the encoding of an attribute value. */
6878 static enum dwarf_form
6879 value_format (dw_attr_ref a)
6881 switch (a->dw_attr_val.val_class)
6883 case dw_val_class_addr:
6884 return DW_FORM_addr;
6885 case dw_val_class_range_list:
6886 case dw_val_class_offset:
6887 case dw_val_class_loc_list:
6888 switch (DWARF_OFFSET_SIZE)
6890 case 4:
6891 return DW_FORM_data4;
6892 case 8:
6893 return DW_FORM_data8;
6894 default:
6895 gcc_unreachable ();
6897 case dw_val_class_loc:
6898 switch (constant_size (size_of_locs (AT_loc (a))))
6900 case 1:
6901 return DW_FORM_block1;
6902 case 2:
6903 return DW_FORM_block2;
6904 default:
6905 gcc_unreachable ();
6907 case dw_val_class_const:
6908 return DW_FORM_sdata;
6909 case dw_val_class_unsigned_const:
6910 switch (constant_size (AT_unsigned (a)))
6912 case 1:
6913 return DW_FORM_data1;
6914 case 2:
6915 return DW_FORM_data2;
6916 case 4:
6917 return DW_FORM_data4;
6918 case 8:
6919 return DW_FORM_data8;
6920 default:
6921 gcc_unreachable ();
6923 case dw_val_class_long_long:
6924 return DW_FORM_block1;
6925 case dw_val_class_vec:
6926 return DW_FORM_block1;
6927 case dw_val_class_flag:
6928 return DW_FORM_flag;
6929 case dw_val_class_die_ref:
6930 if (AT_ref_external (a))
6931 return DW_FORM_ref_addr;
6932 else
6933 return DW_FORM_ref;
6934 case dw_val_class_fde_ref:
6935 return DW_FORM_data;
6936 case dw_val_class_lbl_id:
6937 return DW_FORM_addr;
6938 case dw_val_class_lineptr:
6939 case dw_val_class_macptr:
6940 return DW_FORM_data;
6941 case dw_val_class_str:
6942 return AT_string_form (a);
6943 case dw_val_class_file:
6944 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
6946 case 1:
6947 return DW_FORM_data1;
6948 case 2:
6949 return DW_FORM_data2;
6950 case 4:
6951 return DW_FORM_data4;
6952 default:
6953 gcc_unreachable ();
6956 default:
6957 gcc_unreachable ();
6961 /* Output the encoding of an attribute value. */
6963 static void
6964 output_value_format (dw_attr_ref a)
6966 enum dwarf_form form = value_format (a);
6968 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
6971 /* Output the .debug_abbrev section which defines the DIE abbreviation
6972 table. */
6974 static void
6975 output_abbrev_section (void)
6977 unsigned long abbrev_id;
6979 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6981 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6982 unsigned ix;
6983 dw_attr_ref a_attr;
6985 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
6986 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
6987 dwarf_tag_name (abbrev->die_tag));
6989 if (abbrev->die_child != NULL)
6990 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
6991 else
6992 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
6994 for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
6995 ix++)
6997 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
6998 dwarf_attr_name (a_attr->dw_attr));
6999 output_value_format (a_attr);
7002 dw2_asm_output_data (1, 0, NULL);
7003 dw2_asm_output_data (1, 0, NULL);
7006 /* Terminate the table. */
7007 dw2_asm_output_data (1, 0, NULL);
7010 /* Output a symbol we can use to refer to this DIE from another CU. */
7012 static inline void
7013 output_die_symbol (dw_die_ref die)
7015 char *sym = die->die_symbol;
7017 if (sym == 0)
7018 return;
7020 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
7021 /* We make these global, not weak; if the target doesn't support
7022 .linkonce, it doesn't support combining the sections, so debugging
7023 will break. */
7024 targetm.asm_out.globalize_label (asm_out_file, sym);
7026 ASM_OUTPUT_LABEL (asm_out_file, sym);
7029 /* Return a new location list, given the begin and end range, and the
7030 expression. gensym tells us whether to generate a new internal symbol for
7031 this location list node, which is done for the head of the list only. */
7033 static inline dw_loc_list_ref
7034 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
7035 const char *section, unsigned int gensym)
7037 dw_loc_list_ref retlist = ggc_alloc_cleared (sizeof (dw_loc_list_node));
7039 retlist->begin = begin;
7040 retlist->end = end;
7041 retlist->expr = expr;
7042 retlist->section = section;
7043 if (gensym)
7044 retlist->ll_symbol = gen_internal_sym ("LLST");
7046 return retlist;
7049 /* Add a location description expression to a location list. */
7051 static inline void
7052 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
7053 const char *begin, const char *end,
7054 const char *section)
7056 dw_loc_list_ref *d;
7058 /* Find the end of the chain. */
7059 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
7062 /* Add a new location list node to the list. */
7063 *d = new_loc_list (descr, begin, end, section, 0);
7066 /* Note that the current function section is being used for code. */
7068 static void
7069 dwarf2out_note_section_used (void)
7071 section *sec = current_function_section ();
7072 if (sec == text_section)
7073 text_section_used = true;
7074 else if (sec == cold_text_section)
7075 cold_text_section_used = true;
7078 static void
7079 dwarf2out_switch_text_section (void)
7081 dw_fde_ref fde;
7083 gcc_assert (cfun);
7085 fde = &fde_table[fde_table_in_use - 1];
7086 fde->dw_fde_switched_sections = true;
7087 fde->dw_fde_hot_section_label = cfun->hot_section_label;
7088 fde->dw_fde_hot_section_end_label = cfun->hot_section_end_label;
7089 fde->dw_fde_unlikely_section_label = cfun->cold_section_label;
7090 fde->dw_fde_unlikely_section_end_label = cfun->cold_section_end_label;
7091 have_multiple_function_sections = true;
7093 /* Reset the current label on switching text sections, so that we
7094 don't attempt to advance_loc4 between labels in different sections. */
7095 fde->dw_fde_current_label = NULL;
7097 dwarf2out_note_section_used ();
7100 /* Output the location list given to us. */
7102 static void
7103 output_loc_list (dw_loc_list_ref list_head)
7105 dw_loc_list_ref curr = list_head;
7107 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
7109 /* Walk the location list, and output each range + expression. */
7110 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
7112 unsigned long size;
7113 /* Don't output an entry that starts and ends at the same address. */
7114 if (strcmp (curr->begin, curr->end) == 0)
7115 continue;
7116 if (!have_multiple_function_sections)
7118 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
7119 "Location list begin address (%s)",
7120 list_head->ll_symbol);
7121 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
7122 "Location list end address (%s)",
7123 list_head->ll_symbol);
7125 else
7127 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
7128 "Location list begin address (%s)",
7129 list_head->ll_symbol);
7130 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
7131 "Location list end address (%s)",
7132 list_head->ll_symbol);
7134 size = size_of_locs (curr->expr);
7136 /* Output the block length for this list of location operations. */
7137 gcc_assert (size <= 0xffff);
7138 dw2_asm_output_data (2, size, "%s", "Location expression size");
7140 output_loc_sequence (curr->expr);
7143 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7144 "Location list terminator begin (%s)",
7145 list_head->ll_symbol);
7146 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7147 "Location list terminator end (%s)",
7148 list_head->ll_symbol);
7151 /* Output the DIE and its attributes. Called recursively to generate
7152 the definitions of each child DIE. */
7154 static void
7155 output_die (dw_die_ref die)
7157 dw_attr_ref a;
7158 dw_die_ref c;
7159 unsigned long size;
7160 unsigned ix;
7162 /* If someone in another CU might refer to us, set up a symbol for
7163 them to point to. */
7164 if (die->die_symbol)
7165 output_die_symbol (die);
7167 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
7168 (unsigned long)die->die_offset,
7169 dwarf_tag_name (die->die_tag));
7171 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7173 const char *name = dwarf_attr_name (a->dw_attr);
7175 switch (AT_class (a))
7177 case dw_val_class_addr:
7178 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
7179 break;
7181 case dw_val_class_offset:
7182 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
7183 "%s", name);
7184 break;
7186 case dw_val_class_range_list:
7188 char *p = strchr (ranges_section_label, '\0');
7190 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
7191 a->dw_attr_val.v.val_offset);
7192 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
7193 debug_ranges_section, "%s", name);
7194 *p = '\0';
7196 break;
7198 case dw_val_class_loc:
7199 size = size_of_locs (AT_loc (a));
7201 /* Output the block length for this list of location operations. */
7202 dw2_asm_output_data (constant_size (size), size, "%s", name);
7204 output_loc_sequence (AT_loc (a));
7205 break;
7207 case dw_val_class_const:
7208 /* ??? It would be slightly more efficient to use a scheme like is
7209 used for unsigned constants below, but gdb 4.x does not sign
7210 extend. Gdb 5.x does sign extend. */
7211 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
7212 break;
7214 case dw_val_class_unsigned_const:
7215 dw2_asm_output_data (constant_size (AT_unsigned (a)),
7216 AT_unsigned (a), "%s", name);
7217 break;
7219 case dw_val_class_long_long:
7221 unsigned HOST_WIDE_INT first, second;
7223 dw2_asm_output_data (1,
7224 2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7225 "%s", name);
7227 if (WORDS_BIG_ENDIAN)
7229 first = a->dw_attr_val.v.val_long_long.hi;
7230 second = a->dw_attr_val.v.val_long_long.low;
7232 else
7234 first = a->dw_attr_val.v.val_long_long.low;
7235 second = a->dw_attr_val.v.val_long_long.hi;
7238 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7239 first, "long long constant");
7240 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7241 second, NULL);
7243 break;
7245 case dw_val_class_vec:
7247 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
7248 unsigned int len = a->dw_attr_val.v.val_vec.length;
7249 unsigned int i;
7250 unsigned char *p;
7252 dw2_asm_output_data (1, len * elt_size, "%s", name);
7253 if (elt_size > sizeof (HOST_WIDE_INT))
7255 elt_size /= 2;
7256 len *= 2;
7258 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
7259 i < len;
7260 i++, p += elt_size)
7261 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
7262 "fp or vector constant word %u", i);
7263 break;
7266 case dw_val_class_flag:
7267 dw2_asm_output_data (1, AT_flag (a), "%s", name);
7268 break;
7270 case dw_val_class_loc_list:
7272 char *sym = AT_loc_list (a)->ll_symbol;
7274 gcc_assert (sym);
7275 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
7276 "%s", name);
7278 break;
7280 case dw_val_class_die_ref:
7281 if (AT_ref_external (a))
7283 char *sym = AT_ref (a)->die_symbol;
7285 gcc_assert (sym);
7286 dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, debug_info_section,
7287 "%s", name);
7289 else
7291 gcc_assert (AT_ref (a)->die_offset);
7292 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
7293 "%s", name);
7295 break;
7297 case dw_val_class_fde_ref:
7299 char l1[20];
7301 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
7302 a->dw_attr_val.v.val_fde_index * 2);
7303 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
7304 "%s", name);
7306 break;
7308 case dw_val_class_lbl_id:
7309 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
7310 break;
7312 case dw_val_class_lineptr:
7313 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7314 debug_line_section, "%s", name);
7315 break;
7317 case dw_val_class_macptr:
7318 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7319 debug_macinfo_section, "%s", name);
7320 break;
7322 case dw_val_class_str:
7323 if (AT_string_form (a) == DW_FORM_strp)
7324 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
7325 a->dw_attr_val.v.val_str->label,
7326 debug_str_section,
7327 "%s: \"%s\"", name, AT_string (a));
7328 else
7329 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
7330 break;
7332 case dw_val_class_file:
7334 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
7336 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
7337 a->dw_attr_val.v.val_file->filename);
7338 break;
7341 default:
7342 gcc_unreachable ();
7346 FOR_EACH_CHILD (die, c, output_die (c));
7348 /* Add null byte to terminate sibling list. */
7349 if (die->die_child != NULL)
7350 dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
7351 (unsigned long) die->die_offset);
7354 /* Output the compilation unit that appears at the beginning of the
7355 .debug_info section, and precedes the DIE descriptions. */
7357 static void
7358 output_compilation_unit_header (void)
7360 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7361 dw2_asm_output_data (4, 0xffffffff,
7362 "Initial length escape value indicating 64-bit DWARF extension");
7363 dw2_asm_output_data (DWARF_OFFSET_SIZE,
7364 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
7365 "Length of Compilation Unit Info");
7366 dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
7367 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
7368 debug_abbrev_section,
7369 "Offset Into Abbrev. Section");
7370 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
7373 /* Output the compilation unit DIE and its children. */
7375 static void
7376 output_comp_unit (dw_die_ref die, int output_if_empty)
7378 const char *secname;
7379 char *oldsym, *tmp;
7381 /* Unless we are outputting main CU, we may throw away empty ones. */
7382 if (!output_if_empty && die->die_child == NULL)
7383 return;
7385 /* Even if there are no children of this DIE, we must output the information
7386 about the compilation unit. Otherwise, on an empty translation unit, we
7387 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
7388 will then complain when examining the file. First mark all the DIEs in
7389 this CU so we know which get local refs. */
7390 mark_dies (die);
7392 build_abbrev_table (die);
7394 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
7395 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7396 calc_die_sizes (die);
7398 oldsym = die->die_symbol;
7399 if (oldsym)
7401 tmp = alloca (strlen (oldsym) + 24);
7403 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
7404 secname = tmp;
7405 die->die_symbol = NULL;
7406 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
7408 else
7409 switch_to_section (debug_info_section);
7411 /* Output debugging information. */
7412 output_compilation_unit_header ();
7413 output_die (die);
7415 /* Leave the marks on the main CU, so we can check them in
7416 output_pubnames. */
7417 if (oldsym)
7419 unmark_dies (die);
7420 die->die_symbol = oldsym;
7424 /* Return the DWARF2/3 pubname associated with a decl. */
7426 static const char *
7427 dwarf2_name (tree decl, int scope)
7429 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
7432 /* Add a new entry to .debug_pubnames if appropriate. */
7434 static void
7435 add_pubname (tree decl, dw_die_ref die)
7437 pubname_entry e;
7439 if (! TREE_PUBLIC (decl))
7440 return;
7442 e.die = die;
7443 e.name = xstrdup (dwarf2_name (decl, 1));
7444 VEC_safe_push (pubname_entry, gc, pubname_table, &e);
7447 /* Add a new entry to .debug_pubtypes if appropriate. */
7449 static void
7450 add_pubtype (tree decl, dw_die_ref die)
7452 pubname_entry e;
7454 e.name = NULL;
7455 if ((TREE_PUBLIC (decl)
7456 || die->die_parent == comp_unit_die)
7457 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
7459 e.die = die;
7460 if (TYPE_P (decl))
7462 if (TYPE_NAME (decl))
7464 if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
7465 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
7466 else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
7467 && DECL_NAME (TYPE_NAME (decl)))
7468 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
7469 else
7470 e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
7473 else
7474 e.name = xstrdup (dwarf2_name (decl, 1));
7476 /* If we don't have a name for the type, there's no point in adding
7477 it to the table. */
7478 if (e.name && e.name[0] != '\0')
7479 VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
7483 /* Output the public names table used to speed up access to externally
7484 visible names; or the public types table used to find type definitions. */
7486 static void
7487 output_pubnames (VEC (pubname_entry, gc) * names)
7489 unsigned i;
7490 unsigned long pubnames_length = size_of_pubnames (names);
7491 pubname_ref pub;
7493 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7494 dw2_asm_output_data (4, 0xffffffff,
7495 "Initial length escape value indicating 64-bit DWARF extension");
7496 if (names == pubname_table)
7497 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7498 "Length of Public Names Info");
7499 else
7500 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7501 "Length of Public Type Names Info");
7502 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7503 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7504 debug_info_section,
7505 "Offset of Compilation Unit Info");
7506 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
7507 "Compilation Unit Length");
7509 for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
7511 /* We shouldn't see pubnames for DIEs outside of the main CU. */
7512 if (names == pubname_table)
7513 gcc_assert (pub->die->die_mark);
7515 if (names != pubtype_table
7516 || pub->die->die_offset != 0
7517 || !flag_eliminate_unused_debug_types)
7519 dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
7520 "DIE offset");
7522 dw2_asm_output_nstring (pub->name, -1, "external name");
7526 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
7529 /* Add a new entry to .debug_aranges if appropriate. */
7531 static void
7532 add_arange (tree decl, dw_die_ref die)
7534 if (! DECL_SECTION_NAME (decl))
7535 return;
7537 if (arange_table_in_use == arange_table_allocated)
7539 arange_table_allocated += ARANGE_TABLE_INCREMENT;
7540 arange_table = ggc_realloc (arange_table,
7541 (arange_table_allocated
7542 * sizeof (dw_die_ref)));
7543 memset (arange_table + arange_table_in_use, 0,
7544 ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
7547 arange_table[arange_table_in_use++] = die;
7550 /* Output the information that goes into the .debug_aranges table.
7551 Namely, define the beginning and ending address range of the
7552 text section generated for this compilation unit. */
7554 static void
7555 output_aranges (void)
7557 unsigned i;
7558 unsigned long aranges_length = size_of_aranges ();
7560 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7561 dw2_asm_output_data (4, 0xffffffff,
7562 "Initial length escape value indicating 64-bit DWARF extension");
7563 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
7564 "Length of Address Ranges Info");
7565 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7566 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7567 debug_info_section,
7568 "Offset of Compilation Unit Info");
7569 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
7570 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
7572 /* We need to align to twice the pointer size here. */
7573 if (DWARF_ARANGES_PAD_SIZE)
7575 /* Pad using a 2 byte words so that padding is correct for any
7576 pointer size. */
7577 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
7578 2 * DWARF2_ADDR_SIZE);
7579 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
7580 dw2_asm_output_data (2, 0, NULL);
7583 /* It is necessary not to output these entries if the sections were
7584 not used; if the sections were not used, the length will be 0 and
7585 the address may end up as 0 if the section is discarded by ld
7586 --gc-sections, leaving an invalid (0, 0) entry that can be
7587 confused with the terminator. */
7588 if (text_section_used)
7590 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
7591 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
7592 text_section_label, "Length");
7594 if (cold_text_section_used)
7596 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
7597 "Address");
7598 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
7599 cold_text_section_label, "Length");
7602 for (i = 0; i < arange_table_in_use; i++)
7604 dw_die_ref die = arange_table[i];
7606 /* We shouldn't see aranges for DIEs outside of the main CU. */
7607 gcc_assert (die->die_mark);
7609 if (die->die_tag == DW_TAG_subprogram)
7611 dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
7612 "Address");
7613 dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
7614 get_AT_low_pc (die), "Length");
7616 else
7618 /* A static variable; extract the symbol from DW_AT_location.
7619 Note that this code isn't currently hit, as we only emit
7620 aranges for functions (jason 9/23/99). */
7621 dw_attr_ref a = get_AT (die, DW_AT_location);
7622 dw_loc_descr_ref loc;
7624 gcc_assert (a && AT_class (a) == dw_val_class_loc);
7626 loc = AT_loc (a);
7627 gcc_assert (loc->dw_loc_opc == DW_OP_addr);
7629 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
7630 loc->dw_loc_oprnd1.v.val_addr, "Address");
7631 dw2_asm_output_data (DWARF2_ADDR_SIZE,
7632 get_AT_unsigned (die, DW_AT_byte_size),
7633 "Length");
7637 /* Output the terminator words. */
7638 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7639 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7642 /* Add a new entry to .debug_ranges. Return the offset at which it
7643 was placed. */
7645 static unsigned int
7646 add_ranges_num (int num)
7648 unsigned int in_use = ranges_table_in_use;
7650 if (in_use == ranges_table_allocated)
7652 ranges_table_allocated += RANGES_TABLE_INCREMENT;
7653 ranges_table
7654 = ggc_realloc (ranges_table, (ranges_table_allocated
7655 * sizeof (struct dw_ranges_struct)));
7656 memset (ranges_table + ranges_table_in_use, 0,
7657 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
7660 ranges_table[in_use].num = num;
7661 ranges_table_in_use = in_use + 1;
7663 return in_use * 2 * DWARF2_ADDR_SIZE;
7666 /* Add a new entry to .debug_ranges corresponding to a block, or a
7667 range terminator if BLOCK is NULL. */
7669 static unsigned int
7670 add_ranges (const_tree block)
7672 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
7675 /* Add a new entry to .debug_ranges corresponding to a pair of
7676 labels. */
7678 static unsigned int
7679 add_ranges_by_labels (const char *begin, const char *end)
7681 unsigned int in_use = ranges_by_label_in_use;
7683 if (in_use == ranges_by_label_allocated)
7685 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
7686 ranges_by_label
7687 = ggc_realloc (ranges_by_label,
7688 (ranges_by_label_allocated
7689 * sizeof (struct dw_ranges_by_label_struct)));
7690 memset (ranges_by_label + ranges_by_label_in_use, 0,
7691 RANGES_TABLE_INCREMENT
7692 * sizeof (struct dw_ranges_by_label_struct));
7695 ranges_by_label[in_use].begin = begin;
7696 ranges_by_label[in_use].end = end;
7697 ranges_by_label_in_use = in_use + 1;
7699 return add_ranges_num (-(int)in_use - 1);
7702 static void
7703 output_ranges (void)
7705 unsigned i;
7706 static const char *const start_fmt = "Offset 0x%x";
7707 const char *fmt = start_fmt;
7709 for (i = 0; i < ranges_table_in_use; i++)
7711 int block_num = ranges_table[i].num;
7713 if (block_num > 0)
7715 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
7716 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
7718 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
7719 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
7721 /* If all code is in the text section, then the compilation
7722 unit base address defaults to DW_AT_low_pc, which is the
7723 base of the text section. */
7724 if (!have_multiple_function_sections)
7726 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
7727 text_section_label,
7728 fmt, i * 2 * DWARF2_ADDR_SIZE);
7729 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
7730 text_section_label, NULL);
7733 /* Otherwise, the compilation unit base address is zero,
7734 which allows us to use absolute addresses, and not worry
7735 about whether the target supports cross-section
7736 arithmetic. */
7737 else
7739 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
7740 fmt, i * 2 * DWARF2_ADDR_SIZE);
7741 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
7744 fmt = NULL;
7747 /* Negative block_num stands for an index into ranges_by_label. */
7748 else if (block_num < 0)
7750 int lab_idx = - block_num - 1;
7752 if (!have_multiple_function_sections)
7754 gcc_unreachable ();
7755 #if 0
7756 /* If we ever use add_ranges_by_labels () for a single
7757 function section, all we have to do is to take out
7758 the #if 0 above. */
7759 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
7760 ranges_by_label[lab_idx].begin,
7761 text_section_label,
7762 fmt, i * 2 * DWARF2_ADDR_SIZE);
7763 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
7764 ranges_by_label[lab_idx].end,
7765 text_section_label, NULL);
7766 #endif
7768 else
7770 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
7771 ranges_by_label[lab_idx].begin,
7772 fmt, i * 2 * DWARF2_ADDR_SIZE);
7773 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
7774 ranges_by_label[lab_idx].end,
7775 NULL);
7778 else
7780 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7781 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7782 fmt = start_fmt;
7787 /* Data structure containing information about input files. */
7788 struct file_info
7790 const char *path; /* Complete file name. */
7791 const char *fname; /* File name part. */
7792 int length; /* Length of entire string. */
7793 struct dwarf_file_data * file_idx; /* Index in input file table. */
7794 int dir_idx; /* Index in directory table. */
7797 /* Data structure containing information about directories with source
7798 files. */
7799 struct dir_info
7801 const char *path; /* Path including directory name. */
7802 int length; /* Path length. */
7803 int prefix; /* Index of directory entry which is a prefix. */
7804 int count; /* Number of files in this directory. */
7805 int dir_idx; /* Index of directory used as base. */
7808 /* Callback function for file_info comparison. We sort by looking at
7809 the directories in the path. */
7811 static int
7812 file_info_cmp (const void *p1, const void *p2)
7814 const struct file_info *s1 = p1;
7815 const struct file_info *s2 = p2;
7816 const unsigned char *cp1;
7817 const unsigned char *cp2;
7819 /* Take care of file names without directories. We need to make sure that
7820 we return consistent values to qsort since some will get confused if
7821 we return the same value when identical operands are passed in opposite
7822 orders. So if neither has a directory, return 0 and otherwise return
7823 1 or -1 depending on which one has the directory. */
7824 if ((s1->path == s1->fname || s2->path == s2->fname))
7825 return (s2->path == s2->fname) - (s1->path == s1->fname);
7827 cp1 = (const unsigned char *) s1->path;
7828 cp2 = (const unsigned char *) s2->path;
7830 while (1)
7832 ++cp1;
7833 ++cp2;
7834 /* Reached the end of the first path? If so, handle like above. */
7835 if ((cp1 == (const unsigned char *) s1->fname)
7836 || (cp2 == (const unsigned char *) s2->fname))
7837 return ((cp2 == (const unsigned char *) s2->fname)
7838 - (cp1 == (const unsigned char *) s1->fname));
7840 /* Character of current path component the same? */
7841 else if (*cp1 != *cp2)
7842 return *cp1 - *cp2;
7846 struct file_name_acquire_data
7848 struct file_info *files;
7849 int used_files;
7850 int max_files;
7853 /* Traversal function for the hash table. */
7855 static int
7856 file_name_acquire (void ** slot, void *data)
7858 struct file_name_acquire_data *fnad = data;
7859 struct dwarf_file_data *d = *slot;
7860 struct file_info *fi;
7861 const char *f;
7863 gcc_assert (fnad->max_files >= d->emitted_number);
7865 if (! d->emitted_number)
7866 return 1;
7868 gcc_assert (fnad->max_files != fnad->used_files);
7870 fi = fnad->files + fnad->used_files++;
7872 /* Skip all leading "./". */
7873 f = d->filename;
7874 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
7875 f += 2;
7877 /* Create a new array entry. */
7878 fi->path = f;
7879 fi->length = strlen (f);
7880 fi->file_idx = d;
7882 /* Search for the file name part. */
7883 f = strrchr (f, DIR_SEPARATOR);
7884 #if defined (DIR_SEPARATOR_2)
7886 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
7888 if (g != NULL)
7890 if (f == NULL || f < g)
7891 f = g;
7894 #endif
7896 fi->fname = f == NULL ? fi->path : f + 1;
7897 return 1;
7900 /* Output the directory table and the file name table. We try to minimize
7901 the total amount of memory needed. A heuristic is used to avoid large
7902 slowdowns with many input files. */
7904 static void
7905 output_file_names (void)
7907 struct file_name_acquire_data fnad;
7908 int numfiles;
7909 struct file_info *files;
7910 struct dir_info *dirs;
7911 int *saved;
7912 int *savehere;
7913 int *backmap;
7914 int ndirs;
7915 int idx_offset;
7916 int i;
7917 int idx;
7919 if (!last_emitted_file)
7921 dw2_asm_output_data (1, 0, "End directory table");
7922 dw2_asm_output_data (1, 0, "End file name table");
7923 return;
7926 numfiles = last_emitted_file->emitted_number;
7928 /* Allocate the various arrays we need. */
7929 files = alloca (numfiles * sizeof (struct file_info));
7930 dirs = alloca (numfiles * sizeof (struct dir_info));
7932 fnad.files = files;
7933 fnad.used_files = 0;
7934 fnad.max_files = numfiles;
7935 htab_traverse (file_table, file_name_acquire, &fnad);
7936 gcc_assert (fnad.used_files == fnad.max_files);
7938 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
7940 /* Find all the different directories used. */
7941 dirs[0].path = files[0].path;
7942 dirs[0].length = files[0].fname - files[0].path;
7943 dirs[0].prefix = -1;
7944 dirs[0].count = 1;
7945 dirs[0].dir_idx = 0;
7946 files[0].dir_idx = 0;
7947 ndirs = 1;
7949 for (i = 1; i < numfiles; i++)
7950 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
7951 && memcmp (dirs[ndirs - 1].path, files[i].path,
7952 dirs[ndirs - 1].length) == 0)
7954 /* Same directory as last entry. */
7955 files[i].dir_idx = ndirs - 1;
7956 ++dirs[ndirs - 1].count;
7958 else
7960 int j;
7962 /* This is a new directory. */
7963 dirs[ndirs].path = files[i].path;
7964 dirs[ndirs].length = files[i].fname - files[i].path;
7965 dirs[ndirs].count = 1;
7966 dirs[ndirs].dir_idx = ndirs;
7967 files[i].dir_idx = ndirs;
7969 /* Search for a prefix. */
7970 dirs[ndirs].prefix = -1;
7971 for (j = 0; j < ndirs; j++)
7972 if (dirs[j].length < dirs[ndirs].length
7973 && dirs[j].length > 1
7974 && (dirs[ndirs].prefix == -1
7975 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
7976 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
7977 dirs[ndirs].prefix = j;
7979 ++ndirs;
7982 /* Now to the actual work. We have to find a subset of the directories which
7983 allow expressing the file name using references to the directory table
7984 with the least amount of characters. We do not do an exhaustive search
7985 where we would have to check out every combination of every single
7986 possible prefix. Instead we use a heuristic which provides nearly optimal
7987 results in most cases and never is much off. */
7988 saved = alloca (ndirs * sizeof (int));
7989 savehere = alloca (ndirs * sizeof (int));
7991 memset (saved, '\0', ndirs * sizeof (saved[0]));
7992 for (i = 0; i < ndirs; i++)
7994 int j;
7995 int total;
7997 /* We can always save some space for the current directory. But this
7998 does not mean it will be enough to justify adding the directory. */
7999 savehere[i] = dirs[i].length;
8000 total = (savehere[i] - saved[i]) * dirs[i].count;
8002 for (j = i + 1; j < ndirs; j++)
8004 savehere[j] = 0;
8005 if (saved[j] < dirs[i].length)
8007 /* Determine whether the dirs[i] path is a prefix of the
8008 dirs[j] path. */
8009 int k;
8011 k = dirs[j].prefix;
8012 while (k != -1 && k != (int) i)
8013 k = dirs[k].prefix;
8015 if (k == (int) i)
8017 /* Yes it is. We can possibly save some memory by
8018 writing the filenames in dirs[j] relative to
8019 dirs[i]. */
8020 savehere[j] = dirs[i].length;
8021 total += (savehere[j] - saved[j]) * dirs[j].count;
8026 /* Check whether we can save enough to justify adding the dirs[i]
8027 directory. */
8028 if (total > dirs[i].length + 1)
8030 /* It's worthwhile adding. */
8031 for (j = i; j < ndirs; j++)
8032 if (savehere[j] > 0)
8034 /* Remember how much we saved for this directory so far. */
8035 saved[j] = savehere[j];
8037 /* Remember the prefix directory. */
8038 dirs[j].dir_idx = i;
8043 /* Emit the directory name table. */
8044 idx = 1;
8045 idx_offset = dirs[0].length > 0 ? 1 : 0;
8046 for (i = 1 - idx_offset; i < ndirs; i++)
8047 dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
8048 "Directory Entry: 0x%x", i + idx_offset);
8050 dw2_asm_output_data (1, 0, "End directory table");
8052 /* We have to emit them in the order of emitted_number since that's
8053 used in the debug info generation. To do this efficiently we
8054 generate a back-mapping of the indices first. */
8055 backmap = alloca (numfiles * sizeof (int));
8056 for (i = 0; i < numfiles; i++)
8057 backmap[files[i].file_idx->emitted_number - 1] = i;
8059 /* Now write all the file names. */
8060 for (i = 0; i < numfiles; i++)
8062 int file_idx = backmap[i];
8063 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
8065 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
8066 "File Entry: 0x%x", (unsigned) i + 1);
8068 /* Include directory index. */
8069 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
8071 /* Modification time. */
8072 dw2_asm_output_data_uleb128 (0, NULL);
8074 /* File length in bytes. */
8075 dw2_asm_output_data_uleb128 (0, NULL);
8078 dw2_asm_output_data (1, 0, "End file name table");
8082 /* Output the source line number correspondence information. This
8083 information goes into the .debug_line section. */
8085 static void
8086 output_line_info (void)
8088 char l1[20], l2[20], p1[20], p2[20];
8089 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8090 char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8091 unsigned opc;
8092 unsigned n_op_args;
8093 unsigned long lt_index;
8094 unsigned long current_line;
8095 long line_offset;
8096 long line_delta;
8097 unsigned long current_file;
8098 unsigned long function;
8100 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
8101 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
8102 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
8103 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
8105 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8106 dw2_asm_output_data (4, 0xffffffff,
8107 "Initial length escape value indicating 64-bit DWARF extension");
8108 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
8109 "Length of Source Line Info");
8110 ASM_OUTPUT_LABEL (asm_out_file, l1);
8112 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8113 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
8114 ASM_OUTPUT_LABEL (asm_out_file, p1);
8116 /* Define the architecture-dependent minimum instruction length (in
8117 bytes). In this implementation of DWARF, this field is used for
8118 information purposes only. Since GCC generates assembly language,
8119 we have no a priori knowledge of how many instruction bytes are
8120 generated for each source line, and therefore can use only the
8121 DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
8122 commands. Accordingly, we fix this as `1', which is "correct
8123 enough" for all architectures, and don't let the target override. */
8124 dw2_asm_output_data (1, 1,
8125 "Minimum Instruction Length");
8127 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
8128 "Default is_stmt_start flag");
8129 dw2_asm_output_data (1, DWARF_LINE_BASE,
8130 "Line Base Value (Special Opcodes)");
8131 dw2_asm_output_data (1, DWARF_LINE_RANGE,
8132 "Line Range Value (Special Opcodes)");
8133 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
8134 "Special Opcode Base");
8136 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
8138 switch (opc)
8140 case DW_LNS_advance_pc:
8141 case DW_LNS_advance_line:
8142 case DW_LNS_set_file:
8143 case DW_LNS_set_column:
8144 case DW_LNS_fixed_advance_pc:
8145 n_op_args = 1;
8146 break;
8147 default:
8148 n_op_args = 0;
8149 break;
8152 dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
8153 opc, n_op_args);
8156 /* Write out the information about the files we use. */
8157 output_file_names ();
8158 ASM_OUTPUT_LABEL (asm_out_file, p2);
8160 /* We used to set the address register to the first location in the text
8161 section here, but that didn't accomplish anything since we already
8162 have a line note for the opening brace of the first function. */
8164 /* Generate the line number to PC correspondence table, encoded as
8165 a series of state machine operations. */
8166 current_file = 1;
8167 current_line = 1;
8169 if (cfun && in_cold_section_p)
8170 strcpy (prev_line_label, cfun->cold_section_label);
8171 else
8172 strcpy (prev_line_label, text_section_label);
8173 for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
8175 dw_line_info_ref line_info = &line_info_table[lt_index];
8177 #if 0
8178 /* Disable this optimization for now; GDB wants to see two line notes
8179 at the beginning of a function so it can find the end of the
8180 prologue. */
8182 /* Don't emit anything for redundant notes. Just updating the
8183 address doesn't accomplish anything, because we already assume
8184 that anything after the last address is this line. */
8185 if (line_info->dw_line_num == current_line
8186 && line_info->dw_file_num == current_file)
8187 continue;
8188 #endif
8190 /* Emit debug info for the address of the current line.
8192 Unfortunately, we have little choice here currently, and must always
8193 use the most general form. GCC does not know the address delta
8194 itself, so we can't use DW_LNS_advance_pc. Many ports do have length
8195 attributes which will give an upper bound on the address range. We
8196 could perhaps use length attributes to determine when it is safe to
8197 use DW_LNS_fixed_advance_pc. */
8199 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
8200 if (0)
8202 /* This can handle deltas up to 0xffff. This takes 3 bytes. */
8203 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8204 "DW_LNS_fixed_advance_pc");
8205 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8207 else
8209 /* This can handle any delta. This takes
8210 4+DWARF2_ADDR_SIZE bytes. */
8211 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8212 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8213 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8214 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8217 strcpy (prev_line_label, line_label);
8219 /* Emit debug info for the source file of the current line, if
8220 different from the previous line. */
8221 if (line_info->dw_file_num != current_file)
8223 current_file = line_info->dw_file_num;
8224 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8225 dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
8228 /* Emit debug info for the current line number, choosing the encoding
8229 that uses the least amount of space. */
8230 if (line_info->dw_line_num != current_line)
8232 line_offset = line_info->dw_line_num - current_line;
8233 line_delta = line_offset - DWARF_LINE_BASE;
8234 current_line = line_info->dw_line_num;
8235 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
8236 /* This can handle deltas from -10 to 234, using the current
8237 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE. This
8238 takes 1 byte. */
8239 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
8240 "line %lu", current_line);
8241 else
8243 /* This can handle any delta. This takes at least 4 bytes,
8244 depending on the value being encoded. */
8245 dw2_asm_output_data (1, DW_LNS_advance_line,
8246 "advance to line %lu", current_line);
8247 dw2_asm_output_data_sleb128 (line_offset, NULL);
8248 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8251 else
8252 /* We still need to start a new row, so output a copy insn. */
8253 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8256 /* Emit debug info for the address of the end of the function. */
8257 if (0)
8259 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8260 "DW_LNS_fixed_advance_pc");
8261 dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
8263 else
8265 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8266 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8267 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8268 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
8271 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
8272 dw2_asm_output_data_uleb128 (1, NULL);
8273 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
8275 function = 0;
8276 current_file = 1;
8277 current_line = 1;
8278 for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
8280 dw_separate_line_info_ref line_info
8281 = &separate_line_info_table[lt_index];
8283 #if 0
8284 /* Don't emit anything for redundant notes. */
8285 if (line_info->dw_line_num == current_line
8286 && line_info->dw_file_num == current_file
8287 && line_info->function == function)
8288 goto cont;
8289 #endif
8291 /* Emit debug info for the address of the current line. If this is
8292 a new function, or the first line of a function, then we need
8293 to handle it differently. */
8294 ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
8295 lt_index);
8296 if (function != line_info->function)
8298 function = line_info->function;
8300 /* Set the address register to the first line in the function. */
8301 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8302 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8303 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8304 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8306 else
8308 /* ??? See the DW_LNS_advance_pc comment above. */
8309 if (0)
8311 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8312 "DW_LNS_fixed_advance_pc");
8313 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8315 else
8317 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8318 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8319 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8320 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8324 strcpy (prev_line_label, line_label);
8326 /* Emit debug info for the source file of the current line, if
8327 different from the previous line. */
8328 if (line_info->dw_file_num != current_file)
8330 current_file = line_info->dw_file_num;
8331 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8332 dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
8335 /* Emit debug info for the current line number, choosing the encoding
8336 that uses the least amount of space. */
8337 if (line_info->dw_line_num != current_line)
8339 line_offset = line_info->dw_line_num - current_line;
8340 line_delta = line_offset - DWARF_LINE_BASE;
8341 current_line = line_info->dw_line_num;
8342 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
8343 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
8344 "line %lu", current_line);
8345 else
8347 dw2_asm_output_data (1, DW_LNS_advance_line,
8348 "advance to line %lu", current_line);
8349 dw2_asm_output_data_sleb128 (line_offset, NULL);
8350 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8353 else
8354 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8356 #if 0
8357 cont:
8358 #endif
8360 lt_index++;
8362 /* If we're done with a function, end its sequence. */
8363 if (lt_index == separate_line_info_table_in_use
8364 || separate_line_info_table[lt_index].function != function)
8366 current_file = 1;
8367 current_line = 1;
8369 /* Emit debug info for the address of the end of the function. */
8370 ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
8371 if (0)
8373 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8374 "DW_LNS_fixed_advance_pc");
8375 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8377 else
8379 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8380 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8381 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8382 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8385 /* Output the marker for the end of this sequence. */
8386 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
8387 dw2_asm_output_data_uleb128 (1, NULL);
8388 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
8392 /* Output the marker for the end of the line number info. */
8393 ASM_OUTPUT_LABEL (asm_out_file, l2);
8396 /* Given a pointer to a tree node for some base type, return a pointer to
8397 a DIE that describes the given type.
8399 This routine must only be called for GCC type nodes that correspond to
8400 Dwarf base (fundamental) types. */
8402 static dw_die_ref
8403 base_type_die (tree type)
8405 dw_die_ref base_type_result;
8406 enum dwarf_type encoding;
8408 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
8409 return 0;
8411 switch (TREE_CODE (type))
8413 case INTEGER_TYPE:
8414 if (TYPE_STRING_FLAG (type))
8416 if (TYPE_UNSIGNED (type))
8417 encoding = DW_ATE_unsigned_char;
8418 else
8419 encoding = DW_ATE_signed_char;
8421 else if (TYPE_UNSIGNED (type))
8422 encoding = DW_ATE_unsigned;
8423 else
8424 encoding = DW_ATE_signed;
8425 break;
8427 case REAL_TYPE:
8428 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
8429 encoding = DW_ATE_decimal_float;
8430 else
8431 encoding = DW_ATE_float;
8432 break;
8434 case FIXED_POINT_TYPE:
8435 if (TYPE_UNSIGNED (type))
8436 encoding = DW_ATE_signed_fixed;
8437 else
8438 encoding = DW_ATE_unsigned_fixed;
8439 break;
8441 /* Dwarf2 doesn't know anything about complex ints, so use
8442 a user defined type for it. */
8443 case COMPLEX_TYPE:
8444 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
8445 encoding = DW_ATE_complex_float;
8446 else
8447 encoding = DW_ATE_lo_user;
8448 break;
8450 case BOOLEAN_TYPE:
8451 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
8452 encoding = DW_ATE_boolean;
8453 break;
8455 default:
8456 /* No other TREE_CODEs are Dwarf fundamental types. */
8457 gcc_unreachable ();
8460 base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
8462 /* This probably indicates a bug. */
8463 if (! TYPE_NAME (type))
8464 add_name_attribute (base_type_result, "__unknown__");
8466 add_AT_unsigned (base_type_result, DW_AT_byte_size,
8467 int_size_in_bytes (type));
8468 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
8470 return base_type_result;
8473 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
8474 given input type is a Dwarf "fundamental" type. Otherwise return null. */
8476 static inline int
8477 is_base_type (tree type)
8479 switch (TREE_CODE (type))
8481 case ERROR_MARK:
8482 case VOID_TYPE:
8483 case INTEGER_TYPE:
8484 case REAL_TYPE:
8485 case FIXED_POINT_TYPE:
8486 case COMPLEX_TYPE:
8487 case BOOLEAN_TYPE:
8488 return 1;
8490 case ARRAY_TYPE:
8491 case RECORD_TYPE:
8492 case UNION_TYPE:
8493 case QUAL_UNION_TYPE:
8494 case ENUMERAL_TYPE:
8495 case FUNCTION_TYPE:
8496 case METHOD_TYPE:
8497 case POINTER_TYPE:
8498 case REFERENCE_TYPE:
8499 case OFFSET_TYPE:
8500 case LANG_TYPE:
8501 case VECTOR_TYPE:
8502 return 0;
8504 default:
8505 gcc_unreachable ();
8508 return 0;
8511 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
8512 node, return the size in bits for the type if it is a constant, or else
8513 return the alignment for the type if the type's size is not constant, or
8514 else return BITS_PER_WORD if the type actually turns out to be an
8515 ERROR_MARK node. */
8517 static inline unsigned HOST_WIDE_INT
8518 simple_type_size_in_bits (const_tree type)
8520 if (TREE_CODE (type) == ERROR_MARK)
8521 return BITS_PER_WORD;
8522 else if (TYPE_SIZE (type) == NULL_TREE)
8523 return 0;
8524 else if (host_integerp (TYPE_SIZE (type), 1))
8525 return tree_low_cst (TYPE_SIZE (type), 1);
8526 else
8527 return TYPE_ALIGN (type);
8530 /* Return true if the debug information for the given type should be
8531 emitted as a subrange type. */
8533 static inline bool
8534 is_subrange_type (const_tree type)
8536 tree subtype = TREE_TYPE (type);
8538 /* Subrange types are identified by the fact that they are integer
8539 types, and that they have a subtype which is either an integer type
8540 or an enumeral type. */
8542 if (TREE_CODE (type) != INTEGER_TYPE
8543 || subtype == NULL_TREE)
8544 return false;
8546 if (TREE_CODE (subtype) != INTEGER_TYPE
8547 && TREE_CODE (subtype) != ENUMERAL_TYPE)
8548 return false;
8550 if (TREE_CODE (type) == TREE_CODE (subtype)
8551 && int_size_in_bytes (type) == int_size_in_bytes (subtype)
8552 && TYPE_MIN_VALUE (type) != NULL
8553 && TYPE_MIN_VALUE (subtype) != NULL
8554 && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
8555 && TYPE_MAX_VALUE (type) != NULL
8556 && TYPE_MAX_VALUE (subtype) != NULL
8557 && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
8559 /* The type and its subtype have the same representation. If in
8560 addition the two types also have the same name, then the given
8561 type is not a subrange type, but rather a plain base type. */
8562 /* FIXME: brobecker/2004-03-22:
8563 Sizetype INTEGER_CSTs nodes are canonicalized. It should
8564 therefore be sufficient to check the TYPE_SIZE node pointers
8565 rather than checking the actual size. Unfortunately, we have
8566 found some cases, such as in the Ada "integer" type, where
8567 this is not the case. Until this problem is solved, we need to
8568 keep checking the actual size. */
8569 tree type_name = TYPE_NAME (type);
8570 tree subtype_name = TYPE_NAME (subtype);
8572 if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
8573 type_name = DECL_NAME (type_name);
8575 if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
8576 subtype_name = DECL_NAME (subtype_name);
8578 if (type_name == subtype_name)
8579 return false;
8582 return true;
8585 /* Given a pointer to a tree node for a subrange type, return a pointer
8586 to a DIE that describes the given type. */
8588 static dw_die_ref
8589 subrange_type_die (tree type, dw_die_ref context_die)
8591 dw_die_ref subrange_die;
8592 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
8594 if (context_die == NULL)
8595 context_die = comp_unit_die;
8597 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
8599 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
8601 /* The size of the subrange type and its base type do not match,
8602 so we need to generate a size attribute for the subrange type. */
8603 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
8606 if (TYPE_MIN_VALUE (type) != NULL)
8607 add_bound_info (subrange_die, DW_AT_lower_bound,
8608 TYPE_MIN_VALUE (type));
8609 if (TYPE_MAX_VALUE (type) != NULL)
8610 add_bound_info (subrange_die, DW_AT_upper_bound,
8611 TYPE_MAX_VALUE (type));
8613 return subrange_die;
8616 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
8617 entry that chains various modifiers in front of the given type. */
8619 static dw_die_ref
8620 modified_type_die (tree type, int is_const_type, int is_volatile_type,
8621 dw_die_ref context_die)
8623 enum tree_code code = TREE_CODE (type);
8624 dw_die_ref mod_type_die;
8625 dw_die_ref sub_die = NULL;
8626 tree item_type = NULL;
8627 tree qualified_type;
8628 tree name;
8630 if (code == ERROR_MARK)
8631 return NULL;
8633 /* See if we already have the appropriately qualified variant of
8634 this type. */
8635 qualified_type
8636 = get_qualified_type (type,
8637 ((is_const_type ? TYPE_QUAL_CONST : 0)
8638 | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
8640 /* If we do, then we can just use its DIE, if it exists. */
8641 if (qualified_type)
8643 mod_type_die = lookup_type_die (qualified_type);
8644 if (mod_type_die)
8645 return mod_type_die;
8648 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
8650 /* Handle C typedef types. */
8651 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
8653 tree dtype = TREE_TYPE (name);
8655 if (qualified_type == dtype)
8657 /* For a named type, use the typedef. */
8658 gen_type_die (qualified_type, context_die);
8659 return lookup_type_die (qualified_type);
8661 else if (is_const_type < TYPE_READONLY (dtype)
8662 || is_volatile_type < TYPE_VOLATILE (dtype)
8663 || (is_const_type <= TYPE_READONLY (dtype)
8664 && is_volatile_type <= TYPE_VOLATILE (dtype)
8665 && DECL_ORIGINAL_TYPE (name) != type))
8666 /* cv-unqualified version of named type. Just use the unnamed
8667 type to which it refers. */
8668 return modified_type_die (DECL_ORIGINAL_TYPE (name),
8669 is_const_type, is_volatile_type,
8670 context_die);
8671 /* Else cv-qualified version of named type; fall through. */
8674 if (is_const_type)
8676 mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
8677 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
8679 else if (is_volatile_type)
8681 mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
8682 sub_die = modified_type_die (type, 0, 0, context_die);
8684 else if (code == POINTER_TYPE)
8686 mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
8687 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8688 simple_type_size_in_bits (type) / BITS_PER_UNIT);
8689 item_type = TREE_TYPE (type);
8691 else if (code == REFERENCE_TYPE)
8693 mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
8694 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8695 simple_type_size_in_bits (type) / BITS_PER_UNIT);
8696 item_type = TREE_TYPE (type);
8698 else if (is_subrange_type (type))
8700 mod_type_die = subrange_type_die (type, context_die);
8701 item_type = TREE_TYPE (type);
8703 else if (is_base_type (type))
8704 mod_type_die = base_type_die (type);
8705 else
8707 gen_type_die (type, context_die);
8709 /* We have to get the type_main_variant here (and pass that to the
8710 `lookup_type_die' routine) because the ..._TYPE node we have
8711 might simply be a *copy* of some original type node (where the
8712 copy was created to help us keep track of typedef names) and
8713 that copy might have a different TYPE_UID from the original
8714 ..._TYPE node. */
8715 if (TREE_CODE (type) != VECTOR_TYPE)
8716 return lookup_type_die (type_main_variant (type));
8717 else
8718 /* Vectors have the debugging information in the type,
8719 not the main variant. */
8720 return lookup_type_die (type);
8723 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
8724 don't output a DW_TAG_typedef, since there isn't one in the
8725 user's program; just attach a DW_AT_name to the type. */
8726 if (name
8727 && (TREE_CODE (name) != TYPE_DECL || TREE_TYPE (name) == qualified_type))
8729 if (TREE_CODE (name) == TYPE_DECL)
8730 /* Could just call add_name_and_src_coords_attributes here,
8731 but since this is a builtin type it doesn't have any
8732 useful source coordinates anyway. */
8733 name = DECL_NAME (name);
8734 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
8737 if (qualified_type)
8738 equate_type_number_to_die (qualified_type, mod_type_die);
8740 if (item_type)
8741 /* We must do this after the equate_type_number_to_die call, in case
8742 this is a recursive type. This ensures that the modified_type_die
8743 recursion will terminate even if the type is recursive. Recursive
8744 types are possible in Ada. */
8745 sub_die = modified_type_die (item_type,
8746 TYPE_READONLY (item_type),
8747 TYPE_VOLATILE (item_type),
8748 context_die);
8750 if (sub_die != NULL)
8751 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
8753 return mod_type_die;
8756 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
8757 an enumerated type. */
8759 static inline int
8760 type_is_enum (const_tree type)
8762 return TREE_CODE (type) == ENUMERAL_TYPE;
8765 /* Return the DBX register number described by a given RTL node. */
8767 static unsigned int
8768 dbx_reg_number (const_rtx rtl)
8770 unsigned regno = REGNO (rtl);
8772 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
8774 #ifdef LEAF_REG_REMAP
8775 if (current_function_uses_only_leaf_regs)
8777 int leaf_reg = LEAF_REG_REMAP (regno);
8778 if (leaf_reg != -1)
8779 regno = (unsigned) leaf_reg;
8781 #endif
8783 return DBX_REGISTER_NUMBER (regno);
8786 /* Optionally add a DW_OP_piece term to a location description expression.
8787 DW_OP_piece is only added if the location description expression already
8788 doesn't end with DW_OP_piece. */
8790 static void
8791 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
8793 dw_loc_descr_ref loc;
8795 if (*list_head != NULL)
8797 /* Find the end of the chain. */
8798 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
8801 if (loc->dw_loc_opc != DW_OP_piece)
8802 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
8806 /* Return a location descriptor that designates a machine register or
8807 zero if there is none. */
8809 static dw_loc_descr_ref
8810 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
8812 rtx regs;
8814 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
8815 return 0;
8817 regs = targetm.dwarf_register_span (rtl);
8819 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
8820 return multiple_reg_loc_descriptor (rtl, regs, initialized);
8821 else
8822 return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
8825 /* Return a location descriptor that designates a machine register for
8826 a given hard register number. */
8828 static dw_loc_descr_ref
8829 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
8831 dw_loc_descr_ref reg_loc_descr;
8832 if (regno <= 31)
8833 reg_loc_descr = new_loc_descr (DW_OP_reg0 + regno, 0, 0);
8834 else
8835 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
8837 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
8838 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
8840 return reg_loc_descr;
8843 /* Given an RTL of a register, return a location descriptor that
8844 designates a value that spans more than one register. */
8846 static dw_loc_descr_ref
8847 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
8848 enum var_init_status initialized)
8850 int nregs, size, i;
8851 unsigned reg;
8852 dw_loc_descr_ref loc_result = NULL;
8854 reg = REGNO (rtl);
8855 #ifdef LEAF_REG_REMAP
8856 if (current_function_uses_only_leaf_regs)
8858 int leaf_reg = LEAF_REG_REMAP (reg);
8859 if (leaf_reg != -1)
8860 reg = (unsigned) leaf_reg;
8862 #endif
8863 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
8864 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
8866 /* Simple, contiguous registers. */
8867 if (regs == NULL_RTX)
8869 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
8871 loc_result = NULL;
8872 while (nregs--)
8874 dw_loc_descr_ref t;
8876 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
8877 VAR_INIT_STATUS_INITIALIZED);
8878 add_loc_descr (&loc_result, t);
8879 add_loc_descr_op_piece (&loc_result, size);
8880 ++reg;
8882 return loc_result;
8885 /* Now onto stupid register sets in non contiguous locations. */
8887 gcc_assert (GET_CODE (regs) == PARALLEL);
8889 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8890 loc_result = NULL;
8892 for (i = 0; i < XVECLEN (regs, 0); ++i)
8894 dw_loc_descr_ref t;
8896 t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
8897 VAR_INIT_STATUS_INITIALIZED);
8898 add_loc_descr (&loc_result, t);
8899 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8900 add_loc_descr_op_piece (&loc_result, size);
8903 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
8904 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
8905 return loc_result;
8908 /* Return a location descriptor that designates a constant. */
8910 static dw_loc_descr_ref
8911 int_loc_descriptor (HOST_WIDE_INT i)
8913 enum dwarf_location_atom op;
8915 /* Pick the smallest representation of a constant, rather than just
8916 defaulting to the LEB encoding. */
8917 if (i >= 0)
8919 if (i <= 31)
8920 op = DW_OP_lit0 + i;
8921 else if (i <= 0xff)
8922 op = DW_OP_const1u;
8923 else if (i <= 0xffff)
8924 op = DW_OP_const2u;
8925 else if (HOST_BITS_PER_WIDE_INT == 32
8926 || i <= 0xffffffff)
8927 op = DW_OP_const4u;
8928 else
8929 op = DW_OP_constu;
8931 else
8933 if (i >= -0x80)
8934 op = DW_OP_const1s;
8935 else if (i >= -0x8000)
8936 op = DW_OP_const2s;
8937 else if (HOST_BITS_PER_WIDE_INT == 32
8938 || i >= -0x80000000)
8939 op = DW_OP_const4s;
8940 else
8941 op = DW_OP_consts;
8944 return new_loc_descr (op, i, 0);
8947 /* Return a location descriptor that designates a base+offset location. */
8949 static dw_loc_descr_ref
8950 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
8951 enum var_init_status initialized)
8953 unsigned int regno;
8954 dw_loc_descr_ref result;
8956 /* We only use "frame base" when we're sure we're talking about the
8957 post-prologue local stack frame. We do this by *not* running
8958 register elimination until this point, and recognizing the special
8959 argument pointer and soft frame pointer rtx's. */
8960 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
8962 rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
8964 if (elim != reg)
8966 if (GET_CODE (elim) == PLUS)
8968 offset += INTVAL (XEXP (elim, 1));
8969 elim = XEXP (elim, 0);
8971 gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
8972 : stack_pointer_rtx));
8973 offset += frame_pointer_fb_offset;
8975 return new_loc_descr (DW_OP_fbreg, offset, 0);
8979 regno = dbx_reg_number (reg);
8980 if (regno <= 31)
8981 result = new_loc_descr (DW_OP_breg0 + regno, offset, 0);
8982 else
8983 result = new_loc_descr (DW_OP_bregx, regno, offset);
8985 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
8986 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
8988 return result;
8991 /* Return true if this RTL expression describes a base+offset calculation. */
8993 static inline int
8994 is_based_loc (const_rtx rtl)
8996 return (GET_CODE (rtl) == PLUS
8997 && ((REG_P (XEXP (rtl, 0))
8998 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
8999 && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
9002 /* Return a descriptor that describes the concatenation of N locations
9003 used to form the address of a memory location. */
9005 static dw_loc_descr_ref
9006 concatn_mem_loc_descriptor (rtx concatn, enum machine_mode mode,
9007 enum var_init_status initialized)
9009 unsigned int i;
9010 dw_loc_descr_ref cc_loc_result = NULL;
9011 unsigned int n = XVECLEN (concatn, 0);
9013 for (i = 0; i < n; ++i)
9015 dw_loc_descr_ref ref;
9016 rtx x = XVECEXP (concatn, 0, i);
9018 ref = mem_loc_descriptor (x, mode, VAR_INIT_STATUS_INITIALIZED);
9019 if (ref == NULL)
9020 return NULL;
9022 add_loc_descr (&cc_loc_result, ref);
9023 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
9026 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9027 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9029 return cc_loc_result;
9032 /* The following routine converts the RTL for a variable or parameter
9033 (resident in memory) into an equivalent Dwarf representation of a
9034 mechanism for getting the address of that same variable onto the top of a
9035 hypothetical "address evaluation" stack.
9037 When creating memory location descriptors, we are effectively transforming
9038 the RTL for a memory-resident object into its Dwarf postfix expression
9039 equivalent. This routine recursively descends an RTL tree, turning
9040 it into Dwarf postfix code as it goes.
9042 MODE is the mode of the memory reference, needed to handle some
9043 autoincrement addressing modes.
9045 CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
9046 location list for RTL.
9048 Return 0 if we can't represent the location. */
9050 static dw_loc_descr_ref
9051 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
9052 enum var_init_status initialized)
9054 dw_loc_descr_ref mem_loc_result = NULL;
9055 enum dwarf_location_atom op;
9057 /* Note that for a dynamically sized array, the location we will generate a
9058 description of here will be the lowest numbered location which is
9059 actually within the array. That's *not* necessarily the same as the
9060 zeroth element of the array. */
9062 rtl = targetm.delegitimize_address (rtl);
9064 switch (GET_CODE (rtl))
9066 case POST_INC:
9067 case POST_DEC:
9068 case POST_MODIFY:
9069 /* POST_INC and POST_DEC can be handled just like a SUBREG. So we
9070 just fall into the SUBREG code. */
9072 /* ... fall through ... */
9074 case SUBREG:
9075 /* The case of a subreg may arise when we have a local (register)
9076 variable or a formal (register) parameter which doesn't quite fill
9077 up an entire register. For now, just assume that it is
9078 legitimate to make the Dwarf info refer to the whole register which
9079 contains the given subreg. */
9080 rtl = XEXP (rtl, 0);
9082 /* ... fall through ... */
9084 case REG:
9085 /* Whenever a register number forms a part of the description of the
9086 method for calculating the (dynamic) address of a memory resident
9087 object, DWARF rules require the register number be referred to as
9088 a "base register". This distinction is not based in any way upon
9089 what category of register the hardware believes the given register
9090 belongs to. This is strictly DWARF terminology we're dealing with
9091 here. Note that in cases where the location of a memory-resident
9092 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
9093 OP_CONST (0)) the actual DWARF location descriptor that we generate
9094 may just be OP_BASEREG (basereg). This may look deceptively like
9095 the object in question was allocated to a register (rather than in
9096 memory) so DWARF consumers need to be aware of the subtle
9097 distinction between OP_REG and OP_BASEREG. */
9098 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
9099 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
9100 break;
9102 case MEM:
9103 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
9104 VAR_INIT_STATUS_INITIALIZED);
9105 if (mem_loc_result != 0)
9106 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
9107 break;
9109 case LO_SUM:
9110 rtl = XEXP (rtl, 1);
9112 /* ... fall through ... */
9114 case LABEL_REF:
9115 /* Some ports can transform a symbol ref into a label ref, because
9116 the symbol ref is too far away and has to be dumped into a constant
9117 pool. */
9118 case CONST:
9119 case SYMBOL_REF:
9120 /* Alternatively, the symbol in the constant pool might be referenced
9121 by a different symbol. */
9122 if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
9124 bool marked;
9125 rtx tmp = get_pool_constant_mark (rtl, &marked);
9127 if (GET_CODE (tmp) == SYMBOL_REF)
9129 rtl = tmp;
9130 if (CONSTANT_POOL_ADDRESS_P (tmp))
9131 get_pool_constant_mark (tmp, &marked);
9132 else
9133 marked = true;
9136 /* If all references to this pool constant were optimized away,
9137 it was not output and thus we can't represent it.
9138 FIXME: might try to use DW_OP_const_value here, though
9139 DW_OP_piece complicates it. */
9140 if (!marked)
9141 return 0;
9144 mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
9145 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
9146 mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
9147 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
9148 break;
9150 case PRE_MODIFY:
9151 /* Extract the PLUS expression nested inside and fall into
9152 PLUS code below. */
9153 rtl = XEXP (rtl, 1);
9154 goto plus;
9156 case PRE_INC:
9157 case PRE_DEC:
9158 /* Turn these into a PLUS expression and fall into the PLUS code
9159 below. */
9160 rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
9161 GEN_INT (GET_CODE (rtl) == PRE_INC
9162 ? GET_MODE_UNIT_SIZE (mode)
9163 : -GET_MODE_UNIT_SIZE (mode)));
9165 /* ... fall through ... */
9167 case PLUS:
9168 plus:
9169 if (is_based_loc (rtl))
9170 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
9171 INTVAL (XEXP (rtl, 1)),
9172 VAR_INIT_STATUS_INITIALIZED);
9173 else
9175 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
9176 VAR_INIT_STATUS_INITIALIZED);
9177 if (mem_loc_result == 0)
9178 break;
9180 if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
9181 && INTVAL (XEXP (rtl, 1)) >= 0)
9182 add_loc_descr (&mem_loc_result,
9183 new_loc_descr (DW_OP_plus_uconst,
9184 INTVAL (XEXP (rtl, 1)), 0));
9185 else
9187 add_loc_descr (&mem_loc_result,
9188 mem_loc_descriptor (XEXP (rtl, 1), mode,
9189 VAR_INIT_STATUS_INITIALIZED));
9190 add_loc_descr (&mem_loc_result,
9191 new_loc_descr (DW_OP_plus, 0, 0));
9194 break;
9196 /* If a pseudo-reg is optimized away, it is possible for it to
9197 be replaced with a MEM containing a multiply or shift. */
9198 case MULT:
9199 op = DW_OP_mul;
9200 goto do_binop;
9202 case ASHIFT:
9203 op = DW_OP_shl;
9204 goto do_binop;
9206 case ASHIFTRT:
9207 op = DW_OP_shra;
9208 goto do_binop;
9210 case LSHIFTRT:
9211 op = DW_OP_shr;
9212 goto do_binop;
9214 do_binop:
9216 dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
9217 VAR_INIT_STATUS_INITIALIZED);
9218 dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
9219 VAR_INIT_STATUS_INITIALIZED);
9221 if (op0 == 0 || op1 == 0)
9222 break;
9224 mem_loc_result = op0;
9225 add_loc_descr (&mem_loc_result, op1);
9226 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
9227 break;
9230 case CONST_INT:
9231 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
9232 break;
9234 case CONCATN:
9235 mem_loc_result = concatn_mem_loc_descriptor (rtl, mode,
9236 VAR_INIT_STATUS_INITIALIZED);
9237 break;
9239 default:
9240 gcc_unreachable ();
9243 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9244 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9246 return mem_loc_result;
9249 /* Return a descriptor that describes the concatenation of two locations.
9250 This is typically a complex variable. */
9252 static dw_loc_descr_ref
9253 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
9255 dw_loc_descr_ref cc_loc_result = NULL;
9256 dw_loc_descr_ref x0_ref = loc_descriptor (x0, VAR_INIT_STATUS_INITIALIZED);
9257 dw_loc_descr_ref x1_ref = loc_descriptor (x1, VAR_INIT_STATUS_INITIALIZED);
9259 if (x0_ref == 0 || x1_ref == 0)
9260 return 0;
9262 cc_loc_result = x0_ref;
9263 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
9265 add_loc_descr (&cc_loc_result, x1_ref);
9266 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
9268 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9269 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9271 return cc_loc_result;
9274 /* Return a descriptor that describes the concatenation of N
9275 locations. */
9277 static dw_loc_descr_ref
9278 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
9280 unsigned int i;
9281 dw_loc_descr_ref cc_loc_result = NULL;
9282 unsigned int n = XVECLEN (concatn, 0);
9284 for (i = 0; i < n; ++i)
9286 dw_loc_descr_ref ref;
9287 rtx x = XVECEXP (concatn, 0, i);
9289 ref = loc_descriptor (x, VAR_INIT_STATUS_INITIALIZED);
9290 if (ref == NULL)
9291 return NULL;
9293 add_loc_descr (&cc_loc_result, ref);
9294 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
9297 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9298 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9300 return cc_loc_result;
9303 /* Output a proper Dwarf location descriptor for a variable or parameter
9304 which is either allocated in a register or in a memory location. For a
9305 register, we just generate an OP_REG and the register number. For a
9306 memory location we provide a Dwarf postfix expression describing how to
9307 generate the (dynamic) address of the object onto the address stack.
9309 If we don't know how to describe it, return 0. */
9311 static dw_loc_descr_ref
9312 loc_descriptor (rtx rtl, enum var_init_status initialized)
9314 dw_loc_descr_ref loc_result = NULL;
9316 switch (GET_CODE (rtl))
9318 case SUBREG:
9319 /* The case of a subreg may arise when we have a local (register)
9320 variable or a formal (register) parameter which doesn't quite fill
9321 up an entire register. For now, just assume that it is
9322 legitimate to make the Dwarf info refer to the whole register which
9323 contains the given subreg. */
9324 rtl = SUBREG_REG (rtl);
9326 /* ... fall through ... */
9328 case REG:
9329 loc_result = reg_loc_descriptor (rtl, initialized);
9330 break;
9332 case MEM:
9333 loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
9334 initialized);
9335 break;
9337 case CONCAT:
9338 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
9339 initialized);
9340 break;
9342 case CONCATN:
9343 loc_result = concatn_loc_descriptor (rtl, initialized);
9344 break;
9346 case VAR_LOCATION:
9347 /* Single part. */
9348 if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
9350 loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), initialized);
9351 break;
9354 rtl = XEXP (rtl, 1);
9355 /* FALLTHRU */
9357 case PARALLEL:
9359 rtvec par_elems = XVEC (rtl, 0);
9360 int num_elem = GET_NUM_ELEM (par_elems);
9361 enum machine_mode mode;
9362 int i;
9364 /* Create the first one, so we have something to add to. */
9365 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
9366 initialized);
9367 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
9368 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9369 for (i = 1; i < num_elem; i++)
9371 dw_loc_descr_ref temp;
9373 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
9374 initialized);
9375 add_loc_descr (&loc_result, temp);
9376 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
9377 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9380 break;
9382 default:
9383 gcc_unreachable ();
9386 return loc_result;
9389 /* Similar, but generate the descriptor from trees instead of rtl. This comes
9390 up particularly with variable length arrays. WANT_ADDRESS is 2 if this is
9391 a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
9392 top-level invocation, and we require the address of LOC; is 0 if we require
9393 the value of LOC. */
9395 static dw_loc_descr_ref
9396 loc_descriptor_from_tree_1 (tree loc, int want_address)
9398 dw_loc_descr_ref ret, ret1;
9399 int have_address = 0;
9400 enum dwarf_location_atom op;
9402 /* ??? Most of the time we do not take proper care for sign/zero
9403 extending the values properly. Hopefully this won't be a real
9404 problem... */
9406 switch (TREE_CODE (loc))
9408 case ERROR_MARK:
9409 return 0;
9411 case PLACEHOLDER_EXPR:
9412 /* This case involves extracting fields from an object to determine the
9413 position of other fields. We don't try to encode this here. The
9414 only user of this is Ada, which encodes the needed information using
9415 the names of types. */
9416 return 0;
9418 case CALL_EXPR:
9419 return 0;
9421 case PREINCREMENT_EXPR:
9422 case PREDECREMENT_EXPR:
9423 case POSTINCREMENT_EXPR:
9424 case POSTDECREMENT_EXPR:
9425 /* There are no opcodes for these operations. */
9426 return 0;
9428 case ADDR_EXPR:
9429 /* If we already want an address, there's nothing we can do. */
9430 if (want_address)
9431 return 0;
9433 /* Otherwise, process the argument and look for the address. */
9434 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
9436 case VAR_DECL:
9437 if (DECL_THREAD_LOCAL_P (loc))
9439 rtx rtl;
9441 /* If this is not defined, we have no way to emit the data. */
9442 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
9443 return 0;
9445 /* The way DW_OP_GNU_push_tls_address is specified, we can only
9446 look up addresses of objects in the current module. */
9447 if (DECL_EXTERNAL (loc))
9448 return 0;
9450 rtl = rtl_for_decl_location (loc);
9451 if (rtl == NULL_RTX)
9452 return 0;
9454 if (!MEM_P (rtl))
9455 return 0;
9456 rtl = XEXP (rtl, 0);
9457 if (! CONSTANT_P (rtl))
9458 return 0;
9460 ret = new_loc_descr (INTERNAL_DW_OP_tls_addr, 0, 0);
9461 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9462 ret->dw_loc_oprnd1.v.val_addr = rtl;
9464 ret1 = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
9465 add_loc_descr (&ret, ret1);
9467 have_address = 1;
9468 break;
9470 /* FALLTHRU */
9472 case PARM_DECL:
9473 if (DECL_HAS_VALUE_EXPR_P (loc))
9474 return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
9475 want_address);
9476 /* FALLTHRU */
9478 case RESULT_DECL:
9479 case FUNCTION_DECL:
9481 rtx rtl = rtl_for_decl_location (loc);
9483 if (rtl == NULL_RTX)
9484 return 0;
9485 else if (GET_CODE (rtl) == CONST_INT)
9487 HOST_WIDE_INT val = INTVAL (rtl);
9488 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
9489 val &= GET_MODE_MASK (DECL_MODE (loc));
9490 ret = int_loc_descriptor (val);
9492 else if (GET_CODE (rtl) == CONST_STRING)
9493 return 0;
9494 else if (CONSTANT_P (rtl))
9496 ret = new_loc_descr (DW_OP_addr, 0, 0);
9497 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9498 ret->dw_loc_oprnd1.v.val_addr = rtl;
9500 else
9502 enum machine_mode mode;
9504 /* Certain constructs can only be represented at top-level. */
9505 if (want_address == 2)
9506 return loc_descriptor (rtl, VAR_INIT_STATUS_INITIALIZED);
9508 mode = GET_MODE (rtl);
9509 if (MEM_P (rtl))
9511 rtl = XEXP (rtl, 0);
9512 have_address = 1;
9514 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
9517 break;
9519 case INDIRECT_REF:
9520 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9521 have_address = 1;
9522 break;
9524 case COMPOUND_EXPR:
9525 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
9527 case NOP_EXPR:
9528 case CONVERT_EXPR:
9529 case NON_LVALUE_EXPR:
9530 case VIEW_CONVERT_EXPR:
9531 case SAVE_EXPR:
9532 case GIMPLE_MODIFY_STMT:
9533 return loc_descriptor_from_tree_1 (GENERIC_TREE_OPERAND (loc, 0),
9534 want_address);
9536 case COMPONENT_REF:
9537 case BIT_FIELD_REF:
9538 case ARRAY_REF:
9539 case ARRAY_RANGE_REF:
9541 tree obj, offset;
9542 HOST_WIDE_INT bitsize, bitpos, bytepos;
9543 enum machine_mode mode;
9544 int volatilep;
9545 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
9547 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
9548 &unsignedp, &volatilep, false);
9550 if (obj == loc)
9551 return 0;
9553 ret = loc_descriptor_from_tree_1 (obj, 1);
9554 if (ret == 0
9555 || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
9556 return 0;
9558 if (offset != NULL_TREE)
9560 /* Variable offset. */
9561 add_loc_descr (&ret, loc_descriptor_from_tree_1 (offset, 0));
9562 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9565 bytepos = bitpos / BITS_PER_UNIT;
9566 if (bytepos > 0)
9567 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
9568 else if (bytepos < 0)
9570 add_loc_descr (&ret, int_loc_descriptor (bytepos));
9571 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9574 have_address = 1;
9575 break;
9578 case INTEGER_CST:
9579 if (host_integerp (loc, 0))
9580 ret = int_loc_descriptor (tree_low_cst (loc, 0));
9581 else
9582 return 0;
9583 break;
9585 case CONSTRUCTOR:
9587 /* Get an RTL for this, if something has been emitted. */
9588 rtx rtl = lookup_constant_def (loc);
9589 enum machine_mode mode;
9591 if (!rtl || !MEM_P (rtl))
9592 return 0;
9593 mode = GET_MODE (rtl);
9594 rtl = XEXP (rtl, 0);
9595 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
9596 have_address = 1;
9597 break;
9600 case TRUTH_AND_EXPR:
9601 case TRUTH_ANDIF_EXPR:
9602 case BIT_AND_EXPR:
9603 op = DW_OP_and;
9604 goto do_binop;
9606 case TRUTH_XOR_EXPR:
9607 case BIT_XOR_EXPR:
9608 op = DW_OP_xor;
9609 goto do_binop;
9611 case TRUTH_OR_EXPR:
9612 case TRUTH_ORIF_EXPR:
9613 case BIT_IOR_EXPR:
9614 op = DW_OP_or;
9615 goto do_binop;
9617 case FLOOR_DIV_EXPR:
9618 case CEIL_DIV_EXPR:
9619 case ROUND_DIV_EXPR:
9620 case TRUNC_DIV_EXPR:
9621 op = DW_OP_div;
9622 goto do_binop;
9624 case MINUS_EXPR:
9625 op = DW_OP_minus;
9626 goto do_binop;
9628 case FLOOR_MOD_EXPR:
9629 case CEIL_MOD_EXPR:
9630 case ROUND_MOD_EXPR:
9631 case TRUNC_MOD_EXPR:
9632 op = DW_OP_mod;
9633 goto do_binop;
9635 case MULT_EXPR:
9636 op = DW_OP_mul;
9637 goto do_binop;
9639 case LSHIFT_EXPR:
9640 op = DW_OP_shl;
9641 goto do_binop;
9643 case RSHIFT_EXPR:
9644 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
9645 goto do_binop;
9647 case POINTER_PLUS_EXPR:
9648 case PLUS_EXPR:
9649 if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
9650 && host_integerp (TREE_OPERAND (loc, 1), 0))
9652 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9653 if (ret == 0)
9654 return 0;
9656 add_loc_descr (&ret,
9657 new_loc_descr (DW_OP_plus_uconst,
9658 tree_low_cst (TREE_OPERAND (loc, 1),
9660 0));
9661 break;
9664 op = DW_OP_plus;
9665 goto do_binop;
9667 case LE_EXPR:
9668 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9669 return 0;
9671 op = DW_OP_le;
9672 goto do_binop;
9674 case GE_EXPR:
9675 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9676 return 0;
9678 op = DW_OP_ge;
9679 goto do_binop;
9681 case LT_EXPR:
9682 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9683 return 0;
9685 op = DW_OP_lt;
9686 goto do_binop;
9688 case GT_EXPR:
9689 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9690 return 0;
9692 op = DW_OP_gt;
9693 goto do_binop;
9695 case EQ_EXPR:
9696 op = DW_OP_eq;
9697 goto do_binop;
9699 case NE_EXPR:
9700 op = DW_OP_ne;
9701 goto do_binop;
9703 do_binop:
9704 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9705 ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9706 if (ret == 0 || ret1 == 0)
9707 return 0;
9709 add_loc_descr (&ret, ret1);
9710 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9711 break;
9713 case TRUTH_NOT_EXPR:
9714 case BIT_NOT_EXPR:
9715 op = DW_OP_not;
9716 goto do_unop;
9718 case ABS_EXPR:
9719 op = DW_OP_abs;
9720 goto do_unop;
9722 case NEGATE_EXPR:
9723 op = DW_OP_neg;
9724 goto do_unop;
9726 do_unop:
9727 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9728 if (ret == 0)
9729 return 0;
9731 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9732 break;
9734 case MIN_EXPR:
9735 case MAX_EXPR:
9737 const enum tree_code code =
9738 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
9740 loc = build3 (COND_EXPR, TREE_TYPE (loc),
9741 build2 (code, integer_type_node,
9742 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
9743 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
9746 /* ... fall through ... */
9748 case COND_EXPR:
9750 dw_loc_descr_ref lhs
9751 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9752 dw_loc_descr_ref rhs
9753 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
9754 dw_loc_descr_ref bra_node, jump_node, tmp;
9756 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9757 if (ret == 0 || lhs == 0 || rhs == 0)
9758 return 0;
9760 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
9761 add_loc_descr (&ret, bra_node);
9763 add_loc_descr (&ret, rhs);
9764 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
9765 add_loc_descr (&ret, jump_node);
9767 add_loc_descr (&ret, lhs);
9768 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9769 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
9771 /* ??? Need a node to point the skip at. Use a nop. */
9772 tmp = new_loc_descr (DW_OP_nop, 0, 0);
9773 add_loc_descr (&ret, tmp);
9774 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9775 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
9777 break;
9779 case FIX_TRUNC_EXPR:
9780 return 0;
9782 default:
9783 /* Leave front-end specific codes as simply unknown. This comes
9784 up, for instance, with the C STMT_EXPR. */
9785 if ((unsigned int) TREE_CODE (loc)
9786 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
9787 return 0;
9789 #ifdef ENABLE_CHECKING
9790 /* Otherwise this is a generic code; we should just lists all of
9791 these explicitly. We forgot one. */
9792 gcc_unreachable ();
9793 #else
9794 /* In a release build, we want to degrade gracefully: better to
9795 generate incomplete debugging information than to crash. */
9796 return NULL;
9797 #endif
9800 /* Show if we can't fill the request for an address. */
9801 if (want_address && !have_address)
9802 return 0;
9804 /* If we've got an address and don't want one, dereference. */
9805 if (!want_address && have_address && ret)
9807 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
9809 if (size > DWARF2_ADDR_SIZE || size == -1)
9810 return 0;
9811 else if (size == DWARF2_ADDR_SIZE)
9812 op = DW_OP_deref;
9813 else
9814 op = DW_OP_deref_size;
9816 add_loc_descr (&ret, new_loc_descr (op, size, 0));
9819 return ret;
9822 static inline dw_loc_descr_ref
9823 loc_descriptor_from_tree (tree loc)
9825 return loc_descriptor_from_tree_1 (loc, 2);
9828 /* Given a value, round it up to the lowest multiple of `boundary'
9829 which is not less than the value itself. */
9831 static inline HOST_WIDE_INT
9832 ceiling (HOST_WIDE_INT value, unsigned int boundary)
9834 return (((value + boundary - 1) / boundary) * boundary);
9837 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
9838 pointer to the declared type for the relevant field variable, or return
9839 `integer_type_node' if the given node turns out to be an
9840 ERROR_MARK node. */
9842 static inline tree
9843 field_type (const_tree decl)
9845 tree type;
9847 if (TREE_CODE (decl) == ERROR_MARK)
9848 return integer_type_node;
9850 type = DECL_BIT_FIELD_TYPE (decl);
9851 if (type == NULL_TREE)
9852 type = TREE_TYPE (decl);
9854 return type;
9857 /* Given a pointer to a tree node, return the alignment in bits for
9858 it, or else return BITS_PER_WORD if the node actually turns out to
9859 be an ERROR_MARK node. */
9861 static inline unsigned
9862 simple_type_align_in_bits (const_tree type)
9864 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
9867 static inline unsigned
9868 simple_decl_align_in_bits (const_tree decl)
9870 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
9873 /* Return the result of rounding T up to ALIGN. */
9875 static inline HOST_WIDE_INT
9876 round_up_to_align (HOST_WIDE_INT t, unsigned int align)
9878 /* We must be careful if T is negative because HOST_WIDE_INT can be
9879 either "above" or "below" unsigned int as per the C promotion
9880 rules, depending on the host, thus making the signedness of the
9881 direct multiplication and division unpredictable. */
9882 unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
9884 u += align - 1;
9885 u /= align;
9886 u *= align;
9888 return (HOST_WIDE_INT) u;
9891 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
9892 lowest addressed byte of the "containing object" for the given FIELD_DECL,
9893 or return 0 if we are unable to determine what that offset is, either
9894 because the argument turns out to be a pointer to an ERROR_MARK node, or
9895 because the offset is actually variable. (We can't handle the latter case
9896 just yet). */
9898 static HOST_WIDE_INT
9899 field_byte_offset (const_tree decl)
9901 HOST_WIDE_INT object_offset_in_bits;
9902 HOST_WIDE_INT bitpos_int;
9904 if (TREE_CODE (decl) == ERROR_MARK)
9905 return 0;
9907 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
9909 /* We cannot yet cope with fields whose positions are variable, so
9910 for now, when we see such things, we simply return 0. Someday, we may
9911 be able to handle such cases, but it will be damn difficult. */
9912 if (! host_integerp (bit_position (decl), 0))
9913 return 0;
9915 bitpos_int = int_bit_position (decl);
9917 #ifdef PCC_BITFIELD_TYPE_MATTERS
9918 if (PCC_BITFIELD_TYPE_MATTERS)
9920 tree type;
9921 tree field_size_tree;
9922 HOST_WIDE_INT deepest_bitpos;
9923 unsigned HOST_WIDE_INT field_size_in_bits;
9924 unsigned int type_align_in_bits;
9925 unsigned int decl_align_in_bits;
9926 unsigned HOST_WIDE_INT type_size_in_bits;
9928 type = field_type (decl);
9929 field_size_tree = DECL_SIZE (decl);
9931 /* The size could be unspecified if there was an error, or for
9932 a flexible array member. */
9933 if (! field_size_tree)
9934 field_size_tree = bitsize_zero_node;
9936 /* If we don't know the size of the field, pretend it's a full word. */
9937 if (host_integerp (field_size_tree, 1))
9938 field_size_in_bits = tree_low_cst (field_size_tree, 1);
9939 else
9940 field_size_in_bits = BITS_PER_WORD;
9942 type_size_in_bits = simple_type_size_in_bits (type);
9943 type_align_in_bits = simple_type_align_in_bits (type);
9944 decl_align_in_bits = simple_decl_align_in_bits (decl);
9946 /* The GCC front-end doesn't make any attempt to keep track of the
9947 starting bit offset (relative to the start of the containing
9948 structure type) of the hypothetical "containing object" for a
9949 bit-field. Thus, when computing the byte offset value for the
9950 start of the "containing object" of a bit-field, we must deduce
9951 this information on our own. This can be rather tricky to do in
9952 some cases. For example, handling the following structure type
9953 definition when compiling for an i386/i486 target (which only
9954 aligns long long's to 32-bit boundaries) can be very tricky:
9956 struct S { int field1; long long field2:31; };
9958 Fortunately, there is a simple rule-of-thumb which can be used
9959 in such cases. When compiling for an i386/i486, GCC will
9960 allocate 8 bytes for the structure shown above. It decides to
9961 do this based upon one simple rule for bit-field allocation.
9962 GCC allocates each "containing object" for each bit-field at
9963 the first (i.e. lowest addressed) legitimate alignment boundary
9964 (based upon the required minimum alignment for the declared
9965 type of the field) which it can possibly use, subject to the
9966 condition that there is still enough available space remaining
9967 in the containing object (when allocated at the selected point)
9968 to fully accommodate all of the bits of the bit-field itself.
9970 This simple rule makes it obvious why GCC allocates 8 bytes for
9971 each object of the structure type shown above. When looking
9972 for a place to allocate the "containing object" for `field2',
9973 the compiler simply tries to allocate a 64-bit "containing
9974 object" at each successive 32-bit boundary (starting at zero)
9975 until it finds a place to allocate that 64- bit field such that
9976 at least 31 contiguous (and previously unallocated) bits remain
9977 within that selected 64 bit field. (As it turns out, for the
9978 example above, the compiler finds it is OK to allocate the
9979 "containing object" 64-bit field at bit-offset zero within the
9980 structure type.)
9982 Here we attempt to work backwards from the limited set of facts
9983 we're given, and we try to deduce from those facts, where GCC
9984 must have believed that the containing object started (within
9985 the structure type). The value we deduce is then used (by the
9986 callers of this routine) to generate DW_AT_location and
9987 DW_AT_bit_offset attributes for fields (both bit-fields and, in
9988 the case of DW_AT_location, regular fields as well). */
9990 /* Figure out the bit-distance from the start of the structure to
9991 the "deepest" bit of the bit-field. */
9992 deepest_bitpos = bitpos_int + field_size_in_bits;
9994 /* This is the tricky part. Use some fancy footwork to deduce
9995 where the lowest addressed bit of the containing object must
9996 be. */
9997 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9999 /* Round up to type_align by default. This works best for
10000 bitfields. */
10001 object_offset_in_bits
10002 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
10004 if (object_offset_in_bits > bitpos_int)
10006 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
10008 /* Round up to decl_align instead. */
10009 object_offset_in_bits
10010 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
10013 else
10014 #endif
10015 object_offset_in_bits = bitpos_int;
10017 return object_offset_in_bits / BITS_PER_UNIT;
10020 /* The following routines define various Dwarf attributes and any data
10021 associated with them. */
10023 /* Add a location description attribute value to a DIE.
10025 This emits location attributes suitable for whole variables and
10026 whole parameters. Note that the location attributes for struct fields are
10027 generated by the routine `data_member_location_attribute' below. */
10029 static inline void
10030 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
10031 dw_loc_descr_ref descr)
10033 if (descr != 0)
10034 add_AT_loc (die, attr_kind, descr);
10037 /* Attach the specialized form of location attribute used for data members of
10038 struct and union types. In the special case of a FIELD_DECL node which
10039 represents a bit-field, the "offset" part of this special location
10040 descriptor must indicate the distance in bytes from the lowest-addressed
10041 byte of the containing struct or union type to the lowest-addressed byte of
10042 the "containing object" for the bit-field. (See the `field_byte_offset'
10043 function above).
10045 For any given bit-field, the "containing object" is a hypothetical object
10046 (of some integral or enum type) within which the given bit-field lives. The
10047 type of this hypothetical "containing object" is always the same as the
10048 declared type of the individual bit-field itself (for GCC anyway... the
10049 DWARF spec doesn't actually mandate this). Note that it is the size (in
10050 bytes) of the hypothetical "containing object" which will be given in the
10051 DW_AT_byte_size attribute for this bit-field. (See the
10052 `byte_size_attribute' function below.) It is also used when calculating the
10053 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
10054 function below.) */
10056 static void
10057 add_data_member_location_attribute (dw_die_ref die, tree decl)
10059 HOST_WIDE_INT offset;
10060 dw_loc_descr_ref loc_descr = 0;
10062 if (TREE_CODE (decl) == TREE_BINFO)
10064 /* We're working on the TAG_inheritance for a base class. */
10065 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
10067 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
10068 aren't at a fixed offset from all (sub)objects of the same
10069 type. We need to extract the appropriate offset from our
10070 vtable. The following dwarf expression means
10072 BaseAddr = ObAddr + *((*ObAddr) - Offset)
10074 This is specific to the V3 ABI, of course. */
10076 dw_loc_descr_ref tmp;
10078 /* Make a copy of the object address. */
10079 tmp = new_loc_descr (DW_OP_dup, 0, 0);
10080 add_loc_descr (&loc_descr, tmp);
10082 /* Extract the vtable address. */
10083 tmp = new_loc_descr (DW_OP_deref, 0, 0);
10084 add_loc_descr (&loc_descr, tmp);
10086 /* Calculate the address of the offset. */
10087 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
10088 gcc_assert (offset < 0);
10090 tmp = int_loc_descriptor (-offset);
10091 add_loc_descr (&loc_descr, tmp);
10092 tmp = new_loc_descr (DW_OP_minus, 0, 0);
10093 add_loc_descr (&loc_descr, tmp);
10095 /* Extract the offset. */
10096 tmp = new_loc_descr (DW_OP_deref, 0, 0);
10097 add_loc_descr (&loc_descr, tmp);
10099 /* Add it to the object address. */
10100 tmp = new_loc_descr (DW_OP_plus, 0, 0);
10101 add_loc_descr (&loc_descr, tmp);
10103 else
10104 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
10106 else
10107 offset = field_byte_offset (decl);
10109 if (! loc_descr)
10111 enum dwarf_location_atom op;
10113 /* The DWARF2 standard says that we should assume that the structure
10114 address is already on the stack, so we can specify a structure field
10115 address by using DW_OP_plus_uconst. */
10117 #ifdef MIPS_DEBUGGING_INFO
10118 /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
10119 operator correctly. It works only if we leave the offset on the
10120 stack. */
10121 op = DW_OP_constu;
10122 #else
10123 op = DW_OP_plus_uconst;
10124 #endif
10126 loc_descr = new_loc_descr (op, offset, 0);
10129 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
10132 /* Writes integer values to dw_vec_const array. */
10134 static void
10135 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
10137 while (size != 0)
10139 *dest++ = val & 0xff;
10140 val >>= 8;
10141 --size;
10145 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
10147 static HOST_WIDE_INT
10148 extract_int (const unsigned char *src, unsigned int size)
10150 HOST_WIDE_INT val = 0;
10152 src += size;
10153 while (size != 0)
10155 val <<= 8;
10156 val |= *--src & 0xff;
10157 --size;
10159 return val;
10162 /* Writes floating point values to dw_vec_const array. */
10164 static void
10165 insert_float (const_rtx rtl, unsigned char *array)
10167 REAL_VALUE_TYPE rv;
10168 long val[4];
10169 int i;
10171 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
10172 real_to_target (val, &rv, GET_MODE (rtl));
10174 /* real_to_target puts 32-bit pieces in each long. Pack them. */
10175 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
10177 insert_int (val[i], 4, array);
10178 array += 4;
10182 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
10183 does not have a "location" either in memory or in a register. These
10184 things can arise in GNU C when a constant is passed as an actual parameter
10185 to an inlined function. They can also arise in C++ where declared
10186 constants do not necessarily get memory "homes". */
10188 static void
10189 add_const_value_attribute (dw_die_ref die, rtx rtl)
10191 switch (GET_CODE (rtl))
10193 case CONST_INT:
10195 HOST_WIDE_INT val = INTVAL (rtl);
10197 if (val < 0)
10198 add_AT_int (die, DW_AT_const_value, val);
10199 else
10200 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
10202 break;
10204 case CONST_DOUBLE:
10205 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
10206 floating-point constant. A CONST_DOUBLE is used whenever the
10207 constant requires more than one word in order to be adequately
10208 represented. We output CONST_DOUBLEs as blocks. */
10210 enum machine_mode mode = GET_MODE (rtl);
10212 if (SCALAR_FLOAT_MODE_P (mode))
10214 unsigned int length = GET_MODE_SIZE (mode);
10215 unsigned char *array = ggc_alloc (length);
10217 insert_float (rtl, array);
10218 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
10220 else
10222 /* ??? We really should be using HOST_WIDE_INT throughout. */
10223 gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
10225 add_AT_long_long (die, DW_AT_const_value,
10226 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
10229 break;
10231 case CONST_VECTOR:
10233 enum machine_mode mode = GET_MODE (rtl);
10234 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
10235 unsigned int length = CONST_VECTOR_NUNITS (rtl);
10236 unsigned char *array = ggc_alloc (length * elt_size);
10237 unsigned int i;
10238 unsigned char *p;
10240 switch (GET_MODE_CLASS (mode))
10242 case MODE_VECTOR_INT:
10243 for (i = 0, p = array; i < length; i++, p += elt_size)
10245 rtx elt = CONST_VECTOR_ELT (rtl, i);
10246 HOST_WIDE_INT lo, hi;
10248 switch (GET_CODE (elt))
10250 case CONST_INT:
10251 lo = INTVAL (elt);
10252 hi = -(lo < 0);
10253 break;
10255 case CONST_DOUBLE:
10256 lo = CONST_DOUBLE_LOW (elt);
10257 hi = CONST_DOUBLE_HIGH (elt);
10258 break;
10260 default:
10261 gcc_unreachable ();
10264 if (elt_size <= sizeof (HOST_WIDE_INT))
10265 insert_int (lo, elt_size, p);
10266 else
10268 unsigned char *p0 = p;
10269 unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
10271 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
10272 if (WORDS_BIG_ENDIAN)
10274 p0 = p1;
10275 p1 = p;
10277 insert_int (lo, sizeof (HOST_WIDE_INT), p0);
10278 insert_int (hi, sizeof (HOST_WIDE_INT), p1);
10281 break;
10283 case MODE_VECTOR_FLOAT:
10284 for (i = 0, p = array; i < length; i++, p += elt_size)
10286 rtx elt = CONST_VECTOR_ELT (rtl, i);
10287 insert_float (elt, p);
10289 break;
10291 default:
10292 gcc_unreachable ();
10295 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
10297 break;
10299 case CONST_STRING:
10300 add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
10301 break;
10303 case SYMBOL_REF:
10304 case LABEL_REF:
10305 case CONST:
10306 add_AT_addr (die, DW_AT_const_value, rtl);
10307 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
10308 break;
10310 case PLUS:
10311 /* In cases where an inlined instance of an inline function is passed
10312 the address of an `auto' variable (which is local to the caller) we
10313 can get a situation where the DECL_RTL of the artificial local
10314 variable (for the inlining) which acts as a stand-in for the
10315 corresponding formal parameter (of the inline function) will look
10316 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
10317 exactly a compile-time constant expression, but it isn't the address
10318 of the (artificial) local variable either. Rather, it represents the
10319 *value* which the artificial local variable always has during its
10320 lifetime. We currently have no way to represent such quasi-constant
10321 values in Dwarf, so for now we just punt and generate nothing. */
10322 break;
10324 default:
10325 /* No other kinds of rtx should be possible here. */
10326 gcc_unreachable ();
10331 /* Determine whether the evaluation of EXPR references any variables
10332 or functions which aren't otherwise used (and therefore may not be
10333 output). */
10334 static tree
10335 reference_to_unused (tree * tp, int * walk_subtrees,
10336 void * data ATTRIBUTE_UNUSED)
10338 if (! EXPR_P (*tp) && ! GIMPLE_STMT_P (*tp) && ! CONSTANT_CLASS_P (*tp))
10339 *walk_subtrees = 0;
10341 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
10342 && ! TREE_ASM_WRITTEN (*tp))
10343 return *tp;
10344 else if (!flag_unit_at_a_time)
10345 return NULL_TREE;
10346 else if (!cgraph_global_info_ready
10347 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
10348 gcc_unreachable ();
10349 else if (DECL_P (*tp) && TREE_CODE (*tp) == VAR_DECL)
10351 struct varpool_node *node = varpool_node (*tp);
10352 if (!node->needed)
10353 return *tp;
10355 else if (DECL_P (*tp) && TREE_CODE (*tp) == FUNCTION_DECL
10356 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
10358 struct cgraph_node *node = cgraph_node (*tp);
10359 if (!node->output)
10360 return *tp;
10363 return NULL_TREE;
10366 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
10367 for use in a later add_const_value_attribute call. */
10369 static rtx
10370 rtl_for_decl_init (tree init, tree type)
10372 rtx rtl = NULL_RTX;
10374 /* If a variable is initialized with a string constant without embedded
10375 zeros, build CONST_STRING. */
10376 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
10378 tree enttype = TREE_TYPE (type);
10379 tree domain = TYPE_DOMAIN (type);
10380 enum machine_mode mode = TYPE_MODE (enttype);
10382 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
10383 && domain
10384 && integer_zerop (TYPE_MIN_VALUE (domain))
10385 && compare_tree_int (TYPE_MAX_VALUE (domain),
10386 TREE_STRING_LENGTH (init) - 1) == 0
10387 && ((size_t) TREE_STRING_LENGTH (init)
10388 == strlen (TREE_STRING_POINTER (init)) + 1))
10389 rtl = gen_rtx_CONST_STRING (VOIDmode,
10390 ggc_strdup (TREE_STRING_POINTER (init)));
10392 /* Other aggregates, and complex values, could be represented using
10393 CONCAT: FIXME! */
10394 else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
10396 /* Vectors only work if their mode is supported by the target.
10397 FIXME: generic vectors ought to work too. */
10398 else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
10400 /* If the initializer is something that we know will expand into an
10401 immediate RTL constant, expand it now. We must be careful not to
10402 reference variables which won't be output. */
10403 else if (initializer_constant_valid_p (init, type)
10404 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
10406 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
10407 possible. */
10408 if (TREE_CODE (type) == VECTOR_TYPE)
10409 switch (TREE_CODE (init))
10411 case VECTOR_CST:
10412 break;
10413 case CONSTRUCTOR:
10414 if (TREE_CONSTANT (init))
10416 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
10417 bool constant_p = true;
10418 tree value;
10419 unsigned HOST_WIDE_INT ix;
10421 /* Even when ctor is constant, it might contain non-*_CST
10422 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
10423 belong into VECTOR_CST nodes. */
10424 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
10425 if (!CONSTANT_CLASS_P (value))
10427 constant_p = false;
10428 break;
10431 if (constant_p)
10433 init = build_vector_from_ctor (type, elts);
10434 break;
10437 /* FALLTHRU */
10439 default:
10440 return NULL;
10443 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
10445 /* If expand_expr returns a MEM, it wasn't immediate. */
10446 gcc_assert (!rtl || !MEM_P (rtl));
10449 return rtl;
10452 /* Generate RTL for the variable DECL to represent its location. */
10454 static rtx
10455 rtl_for_decl_location (tree decl)
10457 rtx rtl;
10459 /* Here we have to decide where we are going to say the parameter "lives"
10460 (as far as the debugger is concerned). We only have a couple of
10461 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
10463 DECL_RTL normally indicates where the parameter lives during most of the
10464 activation of the function. If optimization is enabled however, this
10465 could be either NULL or else a pseudo-reg. Both of those cases indicate
10466 that the parameter doesn't really live anywhere (as far as the code
10467 generation parts of GCC are concerned) during most of the function's
10468 activation. That will happen (for example) if the parameter is never
10469 referenced within the function.
10471 We could just generate a location descriptor here for all non-NULL
10472 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
10473 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
10474 where DECL_RTL is NULL or is a pseudo-reg.
10476 Note however that we can only get away with using DECL_INCOMING_RTL as
10477 a backup substitute for DECL_RTL in certain limited cases. In cases
10478 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
10479 we can be sure that the parameter was passed using the same type as it is
10480 declared to have within the function, and that its DECL_INCOMING_RTL
10481 points us to a place where a value of that type is passed.
10483 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
10484 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
10485 because in these cases DECL_INCOMING_RTL points us to a value of some
10486 type which is *different* from the type of the parameter itself. Thus,
10487 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
10488 such cases, the debugger would end up (for example) trying to fetch a
10489 `float' from a place which actually contains the first part of a
10490 `double'. That would lead to really incorrect and confusing
10491 output at debug-time.
10493 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
10494 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
10495 are a couple of exceptions however. On little-endian machines we can
10496 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
10497 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
10498 an integral type that is smaller than TREE_TYPE (decl). These cases arise
10499 when (on a little-endian machine) a non-prototyped function has a
10500 parameter declared to be of type `short' or `char'. In such cases,
10501 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
10502 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
10503 passed `int' value. If the debugger then uses that address to fetch
10504 a `short' or a `char' (on a little-endian machine) the result will be
10505 the correct data, so we allow for such exceptional cases below.
10507 Note that our goal here is to describe the place where the given formal
10508 parameter lives during most of the function's activation (i.e. between the
10509 end of the prologue and the start of the epilogue). We'll do that as best
10510 as we can. Note however that if the given formal parameter is modified
10511 sometime during the execution of the function, then a stack backtrace (at
10512 debug-time) will show the function as having been called with the *new*
10513 value rather than the value which was originally passed in. This happens
10514 rarely enough that it is not a major problem, but it *is* a problem, and
10515 I'd like to fix it.
10517 A future version of dwarf2out.c may generate two additional attributes for
10518 any given DW_TAG_formal_parameter DIE which will describe the "passed
10519 type" and the "passed location" for the given formal parameter in addition
10520 to the attributes we now generate to indicate the "declared type" and the
10521 "active location" for each parameter. This additional set of attributes
10522 could be used by debuggers for stack backtraces. Separately, note that
10523 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
10524 This happens (for example) for inlined-instances of inline function formal
10525 parameters which are never referenced. This really shouldn't be
10526 happening. All PARM_DECL nodes should get valid non-NULL
10527 DECL_INCOMING_RTL values. FIXME. */
10529 /* Use DECL_RTL as the "location" unless we find something better. */
10530 rtl = DECL_RTL_IF_SET (decl);
10532 /* When generating abstract instances, ignore everything except
10533 constants, symbols living in memory, and symbols living in
10534 fixed registers. */
10535 if (! reload_completed)
10537 if (rtl
10538 && (CONSTANT_P (rtl)
10539 || (MEM_P (rtl)
10540 && CONSTANT_P (XEXP (rtl, 0)))
10541 || (REG_P (rtl)
10542 && TREE_CODE (decl) == VAR_DECL
10543 && TREE_STATIC (decl))))
10545 rtl = targetm.delegitimize_address (rtl);
10546 return rtl;
10548 rtl = NULL_RTX;
10550 else if (TREE_CODE (decl) == PARM_DECL)
10552 if (rtl == NULL_RTX || is_pseudo_reg (rtl))
10554 tree declared_type = TREE_TYPE (decl);
10555 tree passed_type = DECL_ARG_TYPE (decl);
10556 enum machine_mode dmode = TYPE_MODE (declared_type);
10557 enum machine_mode pmode = TYPE_MODE (passed_type);
10559 /* This decl represents a formal parameter which was optimized out.
10560 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
10561 all cases where (rtl == NULL_RTX) just below. */
10562 if (dmode == pmode)
10563 rtl = DECL_INCOMING_RTL (decl);
10564 else if (SCALAR_INT_MODE_P (dmode)
10565 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
10566 && DECL_INCOMING_RTL (decl))
10568 rtx inc = DECL_INCOMING_RTL (decl);
10569 if (REG_P (inc))
10570 rtl = inc;
10571 else if (MEM_P (inc))
10573 if (BYTES_BIG_ENDIAN)
10574 rtl = adjust_address_nv (inc, dmode,
10575 GET_MODE_SIZE (pmode)
10576 - GET_MODE_SIZE (dmode));
10577 else
10578 rtl = inc;
10583 /* If the parm was passed in registers, but lives on the stack, then
10584 make a big endian correction if the mode of the type of the
10585 parameter is not the same as the mode of the rtl. */
10586 /* ??? This is the same series of checks that are made in dbxout.c before
10587 we reach the big endian correction code there. It isn't clear if all
10588 of these checks are necessary here, but keeping them all is the safe
10589 thing to do. */
10590 else if (MEM_P (rtl)
10591 && XEXP (rtl, 0) != const0_rtx
10592 && ! CONSTANT_P (XEXP (rtl, 0))
10593 /* Not passed in memory. */
10594 && !MEM_P (DECL_INCOMING_RTL (decl))
10595 /* Not passed by invisible reference. */
10596 && (!REG_P (XEXP (rtl, 0))
10597 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
10598 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
10599 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
10600 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
10601 #endif
10603 /* Big endian correction check. */
10604 && BYTES_BIG_ENDIAN
10605 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
10606 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
10607 < UNITS_PER_WORD))
10609 int offset = (UNITS_PER_WORD
10610 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
10612 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10613 plus_constant (XEXP (rtl, 0), offset));
10616 else if (TREE_CODE (decl) == VAR_DECL
10617 && rtl
10618 && MEM_P (rtl)
10619 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
10620 && BYTES_BIG_ENDIAN)
10622 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
10623 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
10625 /* If a variable is declared "register" yet is smaller than
10626 a register, then if we store the variable to memory, it
10627 looks like we're storing a register-sized value, when in
10628 fact we are not. We need to adjust the offset of the
10629 storage location to reflect the actual value's bytes,
10630 else gdb will not be able to display it. */
10631 if (rsize > dsize)
10632 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10633 plus_constant (XEXP (rtl, 0), rsize-dsize));
10636 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
10637 and will have been substituted directly into all expressions that use it.
10638 C does not have such a concept, but C++ and other languages do. */
10639 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
10640 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
10642 if (rtl)
10643 rtl = targetm.delegitimize_address (rtl);
10645 /* If we don't look past the constant pool, we risk emitting a
10646 reference to a constant pool entry that isn't referenced from
10647 code, and thus is not emitted. */
10648 if (rtl)
10649 rtl = avoid_constant_pool_reference (rtl);
10651 return rtl;
10654 /* We need to figure out what section we should use as the base for the
10655 address ranges where a given location is valid.
10656 1. If this particular DECL has a section associated with it, use that.
10657 2. If this function has a section associated with it, use that.
10658 3. Otherwise, use the text section.
10659 XXX: If you split a variable across multiple sections, we won't notice. */
10661 static const char *
10662 secname_for_decl (const_tree decl)
10664 const char *secname;
10666 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
10668 tree sectree = DECL_SECTION_NAME (decl);
10669 secname = TREE_STRING_POINTER (sectree);
10671 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
10673 tree sectree = DECL_SECTION_NAME (current_function_decl);
10674 secname = TREE_STRING_POINTER (sectree);
10676 else if (cfun && in_cold_section_p)
10677 secname = cfun->cold_section_label;
10678 else
10679 secname = text_section_label;
10681 return secname;
10684 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
10685 data attribute for a variable or a parameter. We generate the
10686 DW_AT_const_value attribute only in those cases where the given variable
10687 or parameter does not have a true "location" either in memory or in a
10688 register. This can happen (for example) when a constant is passed as an
10689 actual argument in a call to an inline function. (It's possible that
10690 these things can crop up in other ways also.) Note that one type of
10691 constant value which can be passed into an inlined function is a constant
10692 pointer. This can happen for example if an actual argument in an inlined
10693 function call evaluates to a compile-time constant address. */
10695 static void
10696 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
10697 enum dwarf_attribute attr)
10699 rtx rtl;
10700 dw_loc_descr_ref descr;
10701 var_loc_list *loc_list;
10702 struct var_loc_node *node;
10703 if (TREE_CODE (decl) == ERROR_MARK)
10704 return;
10706 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
10707 || TREE_CODE (decl) == RESULT_DECL);
10709 /* See if we possibly have multiple locations for this variable. */
10710 loc_list = lookup_decl_loc (decl);
10712 /* If it truly has multiple locations, the first and last node will
10713 differ. */
10714 if (loc_list && loc_list->first != loc_list->last)
10716 const char *endname, *secname;
10717 dw_loc_list_ref list;
10718 rtx varloc;
10719 enum var_init_status initialized;
10721 /* Now that we know what section we are using for a base,
10722 actually construct the list of locations.
10723 The first location information is what is passed to the
10724 function that creates the location list, and the remaining
10725 locations just get added on to that list.
10726 Note that we only know the start address for a location
10727 (IE location changes), so to build the range, we use
10728 the range [current location start, next location start].
10729 This means we have to special case the last node, and generate
10730 a range of [last location start, end of function label]. */
10732 node = loc_list->first;
10733 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10734 secname = secname_for_decl (decl);
10736 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note))
10737 initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10738 else
10739 initialized = VAR_INIT_STATUS_INITIALIZED;
10741 list = new_loc_list (loc_descriptor (varloc, initialized),
10742 node->label, node->next->label, secname, 1);
10743 node = node->next;
10745 for (; node->next; node = node->next)
10746 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10748 /* The variable has a location between NODE->LABEL and
10749 NODE->NEXT->LABEL. */
10750 enum var_init_status initialized =
10751 NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10752 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10753 add_loc_descr_to_loc_list (&list,
10754 loc_descriptor (varloc, initialized),
10755 node->label, node->next->label, secname);
10758 /* If the variable has a location at the last label
10759 it keeps its location until the end of function. */
10760 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10762 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
10763 enum var_init_status initialized =
10764 NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10766 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10767 if (!current_function_decl)
10768 endname = text_end_label;
10769 else
10771 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
10772 current_function_funcdef_no);
10773 endname = ggc_strdup (label_id);
10775 add_loc_descr_to_loc_list (&list,
10776 loc_descriptor (varloc, initialized),
10777 node->label, endname, secname);
10780 /* Finally, add the location list to the DIE, and we are done. */
10781 add_AT_loc_list (die, attr, list);
10782 return;
10785 /* Try to get some constant RTL for this decl, and use that as the value of
10786 the location. */
10788 rtl = rtl_for_decl_location (decl);
10789 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
10791 add_const_value_attribute (die, rtl);
10792 return;
10795 /* If we have tried to generate the location otherwise, and it
10796 didn't work out (we wouldn't be here if we did), and we have a one entry
10797 location list, try generating a location from that. */
10798 if (loc_list && loc_list->first)
10800 enum var_init_status status;
10801 node = loc_list->first;
10802 status = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10803 descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note), status);
10804 if (descr)
10806 add_AT_location_description (die, attr, descr);
10807 return;
10811 /* We couldn't get any rtl, so try directly generating the location
10812 description from the tree. */
10813 descr = loc_descriptor_from_tree (decl);
10814 if (descr)
10816 add_AT_location_description (die, attr, descr);
10817 return;
10819 /* None of that worked, so it must not really have a location;
10820 try adding a constant value attribute from the DECL_INITIAL. */
10821 tree_add_const_value_attribute (die, decl);
10824 /* If we don't have a copy of this variable in memory for some reason (such
10825 as a C++ member constant that doesn't have an out-of-line definition),
10826 we should tell the debugger about the constant value. */
10828 static void
10829 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
10831 tree init = DECL_INITIAL (decl);
10832 tree type = TREE_TYPE (decl);
10833 rtx rtl;
10835 if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
10836 /* OK */;
10837 else
10838 return;
10840 rtl = rtl_for_decl_init (init, type);
10841 if (rtl)
10842 add_const_value_attribute (var_die, rtl);
10845 /* Convert the CFI instructions for the current function into a
10846 location list. This is used for DW_AT_frame_base when we targeting
10847 a dwarf2 consumer that does not support the dwarf3
10848 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
10849 expressions. */
10851 static dw_loc_list_ref
10852 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
10854 dw_fde_ref fde;
10855 dw_loc_list_ref list, *list_tail;
10856 dw_cfi_ref cfi;
10857 dw_cfa_location last_cfa, next_cfa;
10858 const char *start_label, *last_label, *section;
10860 fde = &fde_table[fde_table_in_use - 1];
10862 section = secname_for_decl (current_function_decl);
10863 list_tail = &list;
10864 list = NULL;
10866 next_cfa.reg = INVALID_REGNUM;
10867 next_cfa.offset = 0;
10868 next_cfa.indirect = 0;
10869 next_cfa.base_offset = 0;
10871 start_label = fde->dw_fde_begin;
10873 /* ??? Bald assumption that the CIE opcode list does not contain
10874 advance opcodes. */
10875 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
10876 lookup_cfa_1 (cfi, &next_cfa);
10878 last_cfa = next_cfa;
10879 last_label = start_label;
10881 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
10882 switch (cfi->dw_cfi_opc)
10884 case DW_CFA_set_loc:
10885 case DW_CFA_advance_loc1:
10886 case DW_CFA_advance_loc2:
10887 case DW_CFA_advance_loc4:
10888 if (!cfa_equal_p (&last_cfa, &next_cfa))
10890 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
10891 start_label, last_label, section,
10892 list == NULL);
10894 list_tail = &(*list_tail)->dw_loc_next;
10895 last_cfa = next_cfa;
10896 start_label = last_label;
10898 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
10899 break;
10901 case DW_CFA_advance_loc:
10902 /* The encoding is complex enough that we should never emit this. */
10903 case DW_CFA_remember_state:
10904 case DW_CFA_restore_state:
10905 /* We don't handle these two in this function. It would be possible
10906 if it were to be required. */
10907 gcc_unreachable ();
10909 default:
10910 lookup_cfa_1 (cfi, &next_cfa);
10911 break;
10914 if (!cfa_equal_p (&last_cfa, &next_cfa))
10916 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
10917 start_label, last_label, section,
10918 list == NULL);
10919 list_tail = &(*list_tail)->dw_loc_next;
10920 start_label = last_label;
10922 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
10923 start_label, fde->dw_fde_end, section,
10924 list == NULL);
10926 return list;
10929 /* Compute a displacement from the "steady-state frame pointer" to the
10930 frame base (often the same as the CFA), and store it in
10931 frame_pointer_fb_offset. OFFSET is added to the displacement
10932 before the latter is negated. */
10934 static void
10935 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
10937 rtx reg, elim;
10939 #ifdef FRAME_POINTER_CFA_OFFSET
10940 reg = frame_pointer_rtx;
10941 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
10942 #else
10943 reg = arg_pointer_rtx;
10944 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
10945 #endif
10947 elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10948 if (GET_CODE (elim) == PLUS)
10950 offset += INTVAL (XEXP (elim, 1));
10951 elim = XEXP (elim, 0);
10953 gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
10954 : stack_pointer_rtx));
10956 frame_pointer_fb_offset = -offset;
10959 /* Generate a DW_AT_name attribute given some string value to be included as
10960 the value of the attribute. */
10962 static void
10963 add_name_attribute (dw_die_ref die, const char *name_string)
10965 if (name_string != NULL && *name_string != 0)
10967 if (demangle_name_func)
10968 name_string = (*demangle_name_func) (name_string);
10970 add_AT_string (die, DW_AT_name, name_string);
10974 /* Generate a DW_AT_comp_dir attribute for DIE. */
10976 static void
10977 add_comp_dir_attribute (dw_die_ref die)
10979 const char *wd = get_src_pwd ();
10980 if (wd != NULL)
10981 add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
10984 /* Given a tree node describing an array bound (either lower or upper) output
10985 a representation for that bound. */
10987 static void
10988 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
10990 switch (TREE_CODE (bound))
10992 case ERROR_MARK:
10993 return;
10995 /* All fixed-bounds are represented by INTEGER_CST nodes. */
10996 case INTEGER_CST:
10997 if (! host_integerp (bound, 0)
10998 || (bound_attr == DW_AT_lower_bound
10999 && (((is_c_family () || is_java ()) && integer_zerop (bound))
11000 || (is_fortran () && integer_onep (bound)))))
11001 /* Use the default. */
11003 else
11004 add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
11005 break;
11007 case CONVERT_EXPR:
11008 case NOP_EXPR:
11009 case NON_LVALUE_EXPR:
11010 case VIEW_CONVERT_EXPR:
11011 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
11012 break;
11014 case SAVE_EXPR:
11015 break;
11017 case VAR_DECL:
11018 case PARM_DECL:
11019 case RESULT_DECL:
11021 dw_die_ref decl_die = lookup_decl_die (bound);
11023 /* ??? Can this happen, or should the variable have been bound
11024 first? Probably it can, since I imagine that we try to create
11025 the types of parameters in the order in which they exist in
11026 the list, and won't have created a forward reference to a
11027 later parameter. */
11028 if (decl_die != NULL)
11029 add_AT_die_ref (subrange_die, bound_attr, decl_die);
11030 break;
11033 default:
11035 /* Otherwise try to create a stack operation procedure to
11036 evaluate the value of the array bound. */
11038 dw_die_ref ctx, decl_die;
11039 dw_loc_descr_ref loc;
11041 loc = loc_descriptor_from_tree (bound);
11042 if (loc == NULL)
11043 break;
11045 if (current_function_decl == 0)
11046 ctx = comp_unit_die;
11047 else
11048 ctx = lookup_decl_die (current_function_decl);
11050 decl_die = new_die (DW_TAG_variable, ctx, bound);
11051 add_AT_flag (decl_die, DW_AT_artificial, 1);
11052 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
11053 add_AT_loc (decl_die, DW_AT_location, loc);
11055 add_AT_die_ref (subrange_die, bound_attr, decl_die);
11056 break;
11061 /* Note that the block of subscript information for an array type also
11062 includes information about the element type of type given array type. */
11064 static void
11065 add_subscript_info (dw_die_ref type_die, tree type)
11067 #ifndef MIPS_DEBUGGING_INFO
11068 unsigned dimension_number;
11069 #endif
11070 tree lower, upper;
11071 dw_die_ref subrange_die;
11073 /* The GNU compilers represent multidimensional array types as sequences of
11074 one dimensional array types whose element types are themselves array
11075 types. Here we squish that down, so that each multidimensional array
11076 type gets only one array_type DIE in the Dwarf debugging info. The draft
11077 Dwarf specification say that we are allowed to do this kind of
11078 compression in C (because there is no difference between an array or
11079 arrays and a multidimensional array in C) but for other source languages
11080 (e.g. Ada) we probably shouldn't do this. */
11082 /* ??? The SGI dwarf reader fails for multidimensional arrays with a
11083 const enum type. E.g. const enum machine_mode insn_operand_mode[2][10].
11084 We work around this by disabling this feature. See also
11085 gen_array_type_die. */
11086 #ifndef MIPS_DEBUGGING_INFO
11087 for (dimension_number = 0;
11088 TREE_CODE (type) == ARRAY_TYPE;
11089 type = TREE_TYPE (type), dimension_number++)
11090 #endif
11092 tree domain = TYPE_DOMAIN (type);
11094 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
11095 and (in GNU C only) variable bounds. Handle all three forms
11096 here. */
11097 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
11098 if (domain)
11100 /* We have an array type with specified bounds. */
11101 lower = TYPE_MIN_VALUE (domain);
11102 upper = TYPE_MAX_VALUE (domain);
11104 /* Define the index type. */
11105 if (TREE_TYPE (domain))
11107 /* ??? This is probably an Ada unnamed subrange type. Ignore the
11108 TREE_TYPE field. We can't emit debug info for this
11109 because it is an unnamed integral type. */
11110 if (TREE_CODE (domain) == INTEGER_TYPE
11111 && TYPE_NAME (domain) == NULL_TREE
11112 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
11113 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
11115 else
11116 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
11117 type_die);
11120 /* ??? If upper is NULL, the array has unspecified length,
11121 but it does have a lower bound. This happens with Fortran
11122 dimension arr(N:*)
11123 Since the debugger is definitely going to need to know N
11124 to produce useful results, go ahead and output the lower
11125 bound solo, and hope the debugger can cope. */
11127 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
11128 if (upper)
11129 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
11132 /* Otherwise we have an array type with an unspecified length. The
11133 DWARF-2 spec does not say how to handle this; let's just leave out the
11134 bounds. */
11138 static void
11139 add_byte_size_attribute (dw_die_ref die, tree tree_node)
11141 unsigned size;
11143 switch (TREE_CODE (tree_node))
11145 case ERROR_MARK:
11146 size = 0;
11147 break;
11148 case ENUMERAL_TYPE:
11149 case RECORD_TYPE:
11150 case UNION_TYPE:
11151 case QUAL_UNION_TYPE:
11152 size = int_size_in_bytes (tree_node);
11153 break;
11154 case FIELD_DECL:
11155 /* For a data member of a struct or union, the DW_AT_byte_size is
11156 generally given as the number of bytes normally allocated for an
11157 object of the *declared* type of the member itself. This is true
11158 even for bit-fields. */
11159 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
11160 break;
11161 default:
11162 gcc_unreachable ();
11165 /* Note that `size' might be -1 when we get to this point. If it is, that
11166 indicates that the byte size of the entity in question is variable. We
11167 have no good way of expressing this fact in Dwarf at the present time,
11168 so just let the -1 pass on through. */
11169 add_AT_unsigned (die, DW_AT_byte_size, size);
11172 /* For a FIELD_DECL node which represents a bit-field, output an attribute
11173 which specifies the distance in bits from the highest order bit of the
11174 "containing object" for the bit-field to the highest order bit of the
11175 bit-field itself.
11177 For any given bit-field, the "containing object" is a hypothetical object
11178 (of some integral or enum type) within which the given bit-field lives. The
11179 type of this hypothetical "containing object" is always the same as the
11180 declared type of the individual bit-field itself. The determination of the
11181 exact location of the "containing object" for a bit-field is rather
11182 complicated. It's handled by the `field_byte_offset' function (above).
11184 Note that it is the size (in bytes) of the hypothetical "containing object"
11185 which will be given in the DW_AT_byte_size attribute for this bit-field.
11186 (See `byte_size_attribute' above). */
11188 static inline void
11189 add_bit_offset_attribute (dw_die_ref die, tree decl)
11191 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
11192 tree type = DECL_BIT_FIELD_TYPE (decl);
11193 HOST_WIDE_INT bitpos_int;
11194 HOST_WIDE_INT highest_order_object_bit_offset;
11195 HOST_WIDE_INT highest_order_field_bit_offset;
11196 HOST_WIDE_INT unsigned bit_offset;
11198 /* Must be a field and a bit field. */
11199 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
11201 /* We can't yet handle bit-fields whose offsets are variable, so if we
11202 encounter such things, just return without generating any attribute
11203 whatsoever. Likewise for variable or too large size. */
11204 if (! host_integerp (bit_position (decl), 0)
11205 || ! host_integerp (DECL_SIZE (decl), 1))
11206 return;
11208 bitpos_int = int_bit_position (decl);
11210 /* Note that the bit offset is always the distance (in bits) from the
11211 highest-order bit of the "containing object" to the highest-order bit of
11212 the bit-field itself. Since the "high-order end" of any object or field
11213 is different on big-endian and little-endian machines, the computation
11214 below must take account of these differences. */
11215 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
11216 highest_order_field_bit_offset = bitpos_int;
11218 if (! BYTES_BIG_ENDIAN)
11220 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
11221 highest_order_object_bit_offset += simple_type_size_in_bits (type);
11224 bit_offset
11225 = (! BYTES_BIG_ENDIAN
11226 ? highest_order_object_bit_offset - highest_order_field_bit_offset
11227 : highest_order_field_bit_offset - highest_order_object_bit_offset);
11229 add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
11232 /* For a FIELD_DECL node which represents a bit field, output an attribute
11233 which specifies the length in bits of the given field. */
11235 static inline void
11236 add_bit_size_attribute (dw_die_ref die, tree decl)
11238 /* Must be a field and a bit field. */
11239 gcc_assert (TREE_CODE (decl) == FIELD_DECL
11240 && DECL_BIT_FIELD_TYPE (decl));
11242 if (host_integerp (DECL_SIZE (decl), 1))
11243 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
11246 /* If the compiled language is ANSI C, then add a 'prototyped'
11247 attribute, if arg types are given for the parameters of a function. */
11249 static inline void
11250 add_prototyped_attribute (dw_die_ref die, tree func_type)
11252 if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
11253 && TYPE_ARG_TYPES (func_type) != NULL)
11254 add_AT_flag (die, DW_AT_prototyped, 1);
11257 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
11258 by looking in either the type declaration or object declaration
11259 equate table. */
11261 static inline void
11262 add_abstract_origin_attribute (dw_die_ref die, tree origin)
11264 dw_die_ref origin_die = NULL;
11266 if (TREE_CODE (origin) != FUNCTION_DECL)
11268 /* We may have gotten separated from the block for the inlined
11269 function, if we're in an exception handler or some such; make
11270 sure that the abstract function has been written out.
11272 Doing this for nested functions is wrong, however; functions are
11273 distinct units, and our context might not even be inline. */
11274 tree fn = origin;
11276 if (TYPE_P (fn))
11277 fn = TYPE_STUB_DECL (fn);
11279 fn = decl_function_context (fn);
11280 if (fn)
11281 dwarf2out_abstract_function (fn);
11284 if (DECL_P (origin))
11285 origin_die = lookup_decl_die (origin);
11286 else if (TYPE_P (origin))
11287 origin_die = lookup_type_die (origin);
11289 /* XXX: Functions that are never lowered don't always have correct block
11290 trees (in the case of java, they simply have no block tree, in some other
11291 languages). For these functions, there is nothing we can really do to
11292 output correct debug info for inlined functions in all cases. Rather
11293 than die, we'll just produce deficient debug info now, in that we will
11294 have variables without a proper abstract origin. In the future, when all
11295 functions are lowered, we should re-add a gcc_assert (origin_die)
11296 here. */
11298 if (origin_die)
11299 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
11302 /* We do not currently support the pure_virtual attribute. */
11304 static inline void
11305 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
11307 if (DECL_VINDEX (func_decl))
11309 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
11311 if (host_integerp (DECL_VINDEX (func_decl), 0))
11312 add_AT_loc (die, DW_AT_vtable_elem_location,
11313 new_loc_descr (DW_OP_constu,
11314 tree_low_cst (DECL_VINDEX (func_decl), 0),
11315 0));
11317 /* GNU extension: Record what type this method came from originally. */
11318 if (debug_info_level > DINFO_LEVEL_TERSE)
11319 add_AT_die_ref (die, DW_AT_containing_type,
11320 lookup_type_die (DECL_CONTEXT (func_decl)));
11324 /* Add source coordinate attributes for the given decl. */
11326 static void
11327 add_src_coords_attributes (dw_die_ref die, tree decl)
11329 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11331 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
11332 add_AT_unsigned (die, DW_AT_decl_line, s.line);
11335 /* Add a DW_AT_name attribute and source coordinate attribute for the
11336 given decl, but only if it actually has a name. */
11338 static void
11339 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
11341 tree decl_name;
11343 decl_name = DECL_NAME (decl);
11344 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
11346 add_name_attribute (die, dwarf2_name (decl, 0));
11347 if (! DECL_ARTIFICIAL (decl))
11348 add_src_coords_attributes (die, decl);
11350 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
11351 && TREE_PUBLIC (decl)
11352 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
11353 && !DECL_ABSTRACT (decl)
11354 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl)))
11355 add_AT_string (die, DW_AT_MIPS_linkage_name,
11356 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
11359 #ifdef VMS_DEBUGGING_INFO
11360 /* Get the function's name, as described by its RTL. This may be different
11361 from the DECL_NAME name used in the source file. */
11362 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
11364 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
11365 XEXP (DECL_RTL (decl), 0));
11366 VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
11368 #endif
11371 /* Push a new declaration scope. */
11373 static void
11374 push_decl_scope (tree scope)
11376 VEC_safe_push (tree, gc, decl_scope_table, scope);
11379 /* Pop a declaration scope. */
11381 static inline void
11382 pop_decl_scope (void)
11384 VEC_pop (tree, decl_scope_table);
11387 /* Return the DIE for the scope that immediately contains this type.
11388 Non-named types get global scope. Named types nested in other
11389 types get their containing scope if it's open, or global scope
11390 otherwise. All other types (i.e. function-local named types) get
11391 the current active scope. */
11393 static dw_die_ref
11394 scope_die_for (tree t, dw_die_ref context_die)
11396 dw_die_ref scope_die = NULL;
11397 tree containing_scope;
11398 int i;
11400 /* Non-types always go in the current scope. */
11401 gcc_assert (TYPE_P (t));
11403 containing_scope = TYPE_CONTEXT (t);
11405 /* Use the containing namespace if it was passed in (for a declaration). */
11406 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
11408 if (context_die == lookup_decl_die (containing_scope))
11409 /* OK */;
11410 else
11411 containing_scope = NULL_TREE;
11414 /* Ignore function type "scopes" from the C frontend. They mean that
11415 a tagged type is local to a parmlist of a function declarator, but
11416 that isn't useful to DWARF. */
11417 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
11418 containing_scope = NULL_TREE;
11420 if (containing_scope == NULL_TREE)
11421 scope_die = comp_unit_die;
11422 else if (TYPE_P (containing_scope))
11424 /* For types, we can just look up the appropriate DIE. But
11425 first we check to see if we're in the middle of emitting it
11426 so we know where the new DIE should go. */
11427 for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
11428 if (VEC_index (tree, decl_scope_table, i) == containing_scope)
11429 break;
11431 if (i < 0)
11433 gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
11434 || TREE_ASM_WRITTEN (containing_scope));
11436 /* If none of the current dies are suitable, we get file scope. */
11437 scope_die = comp_unit_die;
11439 else
11440 scope_die = lookup_type_die (containing_scope);
11442 else
11443 scope_die = context_die;
11445 return scope_die;
11448 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
11450 static inline int
11451 local_scope_p (dw_die_ref context_die)
11453 for (; context_die; context_die = context_die->die_parent)
11454 if (context_die->die_tag == DW_TAG_inlined_subroutine
11455 || context_die->die_tag == DW_TAG_subprogram)
11456 return 1;
11458 return 0;
11461 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
11462 whether or not to treat a DIE in this context as a declaration. */
11464 static inline int
11465 class_or_namespace_scope_p (dw_die_ref context_die)
11467 return (context_die
11468 && (context_die->die_tag == DW_TAG_structure_type
11469 || context_die->die_tag == DW_TAG_union_type
11470 || context_die->die_tag == DW_TAG_namespace));
11473 /* Many forms of DIEs require a "type description" attribute. This
11474 routine locates the proper "type descriptor" die for the type given
11475 by 'type', and adds a DW_AT_type attribute below the given die. */
11477 static void
11478 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
11479 int decl_volatile, dw_die_ref context_die)
11481 enum tree_code code = TREE_CODE (type);
11482 dw_die_ref type_die = NULL;
11484 /* ??? If this type is an unnamed subrange type of an integral, floating-point
11485 or fixed-point type, use the inner type. This is because we have no
11486 support for unnamed types in base_type_die. This can happen if this is
11487 an Ada subrange type. Correct solution is emit a subrange type die. */
11488 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
11489 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
11490 type = TREE_TYPE (type), code = TREE_CODE (type);
11492 if (code == ERROR_MARK
11493 /* Handle a special case. For functions whose return type is void, we
11494 generate *no* type attribute. (Note that no object may have type
11495 `void', so this only applies to function return types). */
11496 || code == VOID_TYPE)
11497 return;
11499 type_die = modified_type_die (type,
11500 decl_const || TYPE_READONLY (type),
11501 decl_volatile || TYPE_VOLATILE (type),
11502 context_die);
11504 if (type_die != NULL)
11505 add_AT_die_ref (object_die, DW_AT_type, type_die);
11508 /* Given an object die, add the calling convention attribute for the
11509 function call type. */
11510 static void
11511 add_calling_convention_attribute (dw_die_ref subr_die, tree type)
11513 enum dwarf_calling_convention value = DW_CC_normal;
11515 value = targetm.dwarf_calling_convention (type);
11517 /* Only add the attribute if the backend requests it, and
11518 is not DW_CC_normal. */
11519 if (value && (value != DW_CC_normal))
11520 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
11523 /* Given a tree pointer to a struct, class, union, or enum type node, return
11524 a pointer to the (string) tag name for the given type, or zero if the type
11525 was declared without a tag. */
11527 static const char *
11528 type_tag (const_tree type)
11530 const char *name = 0;
11532 if (TYPE_NAME (type) != 0)
11534 tree t = 0;
11536 /* Find the IDENTIFIER_NODE for the type name. */
11537 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
11538 t = TYPE_NAME (type);
11540 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
11541 a TYPE_DECL node, regardless of whether or not a `typedef' was
11542 involved. */
11543 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11544 && ! DECL_IGNORED_P (TYPE_NAME (type)))
11546 /* We want to be extra verbose. Don't call dwarf_name if
11547 DECL_NAME isn't set. The default hook for decl_printable_name
11548 doesn't like that, and in this context it's correct to return
11549 0, instead of "<anonymous>" or the like. */
11550 if (DECL_NAME (TYPE_NAME (type)))
11551 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
11554 /* Now get the name as a string, or invent one. */
11555 if (!name && t != 0)
11556 name = IDENTIFIER_POINTER (t);
11559 return (name == 0 || *name == '\0') ? 0 : name;
11562 /* Return the type associated with a data member, make a special check
11563 for bit field types. */
11565 static inline tree
11566 member_declared_type (const_tree member)
11568 return (DECL_BIT_FIELD_TYPE (member)
11569 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
11572 /* Get the decl's label, as described by its RTL. This may be different
11573 from the DECL_NAME name used in the source file. */
11575 #if 0
11576 static const char *
11577 decl_start_label (tree decl)
11579 rtx x;
11580 const char *fnname;
11582 x = DECL_RTL (decl);
11583 gcc_assert (MEM_P (x));
11585 x = XEXP (x, 0);
11586 gcc_assert (GET_CODE (x) == SYMBOL_REF);
11588 fnname = XSTR (x, 0);
11589 return fnname;
11591 #endif
11593 /* These routines generate the internal representation of the DIE's for
11594 the compilation unit. Debugging information is collected by walking
11595 the declaration trees passed in from dwarf2out_decl(). */
11597 static void
11598 gen_array_type_die (tree type, dw_die_ref context_die)
11600 dw_die_ref scope_die = scope_die_for (type, context_die);
11601 dw_die_ref array_die;
11602 tree element_type;
11604 /* ??? The SGI dwarf reader fails for array of array of enum types unless
11605 the inner array type comes before the outer array type. Thus we must
11606 call gen_type_die before we call new_die. See below also. */
11607 #ifdef MIPS_DEBUGGING_INFO
11608 gen_type_die (TREE_TYPE (type), context_die);
11609 #endif
11611 array_die = new_die (DW_TAG_array_type, scope_die, type);
11612 add_name_attribute (array_die, type_tag (type));
11613 equate_type_number_to_die (type, array_die);
11615 if (TREE_CODE (type) == VECTOR_TYPE)
11617 /* The frontend feeds us a representation for the vector as a struct
11618 containing an array. Pull out the array type. */
11619 type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
11620 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
11623 #if 0
11624 /* We default the array ordering. SDB will probably do
11625 the right things even if DW_AT_ordering is not present. It's not even
11626 an issue until we start to get into multidimensional arrays anyway. If
11627 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
11628 then we'll have to put the DW_AT_ordering attribute back in. (But if
11629 and when we find out that we need to put these in, we will only do so
11630 for multidimensional arrays. */
11631 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
11632 #endif
11634 #ifdef MIPS_DEBUGGING_INFO
11635 /* The SGI compilers handle arrays of unknown bound by setting
11636 AT_declaration and not emitting any subrange DIEs. */
11637 if (! TYPE_DOMAIN (type))
11638 add_AT_flag (array_die, DW_AT_declaration, 1);
11639 else
11640 #endif
11641 add_subscript_info (array_die, type);
11643 /* Add representation of the type of the elements of this array type. */
11644 element_type = TREE_TYPE (type);
11646 /* ??? The SGI dwarf reader fails for multidimensional arrays with a
11647 const enum type. E.g. const enum machine_mode insn_operand_mode[2][10].
11648 We work around this by disabling this feature. See also
11649 add_subscript_info. */
11650 #ifndef MIPS_DEBUGGING_INFO
11651 while (TREE_CODE (element_type) == ARRAY_TYPE)
11652 element_type = TREE_TYPE (element_type);
11654 gen_type_die (element_type, context_die);
11655 #endif
11657 add_type_attribute (array_die, element_type, 0, 0, context_die);
11659 if (get_AT (array_die, DW_AT_name))
11660 add_pubtype (type, array_die);
11663 #if 0
11664 static void
11665 gen_entry_point_die (tree decl, dw_die_ref context_die)
11667 tree origin = decl_ultimate_origin (decl);
11668 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
11670 if (origin != NULL)
11671 add_abstract_origin_attribute (decl_die, origin);
11672 else
11674 add_name_and_src_coords_attributes (decl_die, decl);
11675 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
11676 0, 0, context_die);
11679 if (DECL_ABSTRACT (decl))
11680 equate_decl_number_to_die (decl, decl_die);
11681 else
11682 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
11684 #endif
11686 /* Walk through the list of incomplete types again, trying once more to
11687 emit full debugging info for them. */
11689 static void
11690 retry_incomplete_types (void)
11692 int i;
11694 for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
11695 gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
11698 /* Generate a DIE to represent an inlined instance of an enumeration type. */
11700 static void
11701 gen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
11703 dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
11705 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11706 be incomplete and such types are not marked. */
11707 add_abstract_origin_attribute (type_die, type);
11710 /* Generate a DIE to represent an inlined instance of a structure type. */
11712 static void
11713 gen_inlined_structure_type_die (tree type, dw_die_ref context_die)
11715 dw_die_ref type_die = new_die (DW_TAG_structure_type, context_die, type);
11717 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11718 be incomplete and such types are not marked. */
11719 add_abstract_origin_attribute (type_die, type);
11722 /* Generate a DIE to represent an inlined instance of a union type. */
11724 static void
11725 gen_inlined_union_type_die (tree type, dw_die_ref context_die)
11727 dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
11729 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11730 be incomplete and such types are not marked. */
11731 add_abstract_origin_attribute (type_die, type);
11734 /* Generate a DIE to represent an enumeration type. Note that these DIEs
11735 include all of the information about the enumeration values also. Each
11736 enumerated type name/value is listed as a child of the enumerated type
11737 DIE. */
11739 static dw_die_ref
11740 gen_enumeration_type_die (tree type, dw_die_ref context_die)
11742 dw_die_ref type_die = lookup_type_die (type);
11744 if (type_die == NULL)
11746 type_die = new_die (DW_TAG_enumeration_type,
11747 scope_die_for (type, context_die), type);
11748 equate_type_number_to_die (type, type_die);
11749 add_name_attribute (type_die, type_tag (type));
11751 else if (! TYPE_SIZE (type))
11752 return type_die;
11753 else
11754 remove_AT (type_die, DW_AT_declaration);
11756 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
11757 given enum type is incomplete, do not generate the DW_AT_byte_size
11758 attribute or the DW_AT_element_list attribute. */
11759 if (TYPE_SIZE (type))
11761 tree link;
11763 TREE_ASM_WRITTEN (type) = 1;
11764 add_byte_size_attribute (type_die, type);
11765 if (TYPE_STUB_DECL (type) != NULL_TREE)
11766 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
11768 /* If the first reference to this type was as the return type of an
11769 inline function, then it may not have a parent. Fix this now. */
11770 if (type_die->die_parent == NULL)
11771 add_child_die (scope_die_for (type, context_die), type_die);
11773 for (link = TYPE_VALUES (type);
11774 link != NULL; link = TREE_CHAIN (link))
11776 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
11777 tree value = TREE_VALUE (link);
11779 add_name_attribute (enum_die,
11780 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
11782 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
11783 /* DWARF2 does not provide a way of indicating whether or
11784 not enumeration constants are signed or unsigned. GDB
11785 always assumes the values are signed, so we output all
11786 values as if they were signed. That means that
11787 enumeration constants with very large unsigned values
11788 will appear to have negative values in the debugger. */
11789 add_AT_int (enum_die, DW_AT_const_value,
11790 tree_low_cst (value, tree_int_cst_sgn (value) > 0));
11793 else
11794 add_AT_flag (type_die, DW_AT_declaration, 1);
11796 if (get_AT (type_die, DW_AT_name))
11797 add_pubtype (type, type_die);
11799 return type_die;
11802 /* Generate a DIE to represent either a real live formal parameter decl or to
11803 represent just the type of some formal parameter position in some function
11804 type.
11806 Note that this routine is a bit unusual because its argument may be a
11807 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
11808 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
11809 node. If it's the former then this function is being called to output a
11810 DIE to represent a formal parameter object (or some inlining thereof). If
11811 it's the latter, then this function is only being called to output a
11812 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
11813 argument type of some subprogram type. */
11815 static dw_die_ref
11816 gen_formal_parameter_die (tree node, dw_die_ref context_die)
11818 dw_die_ref parm_die
11819 = new_die (DW_TAG_formal_parameter, context_die, node);
11820 tree origin;
11822 switch (TREE_CODE_CLASS (TREE_CODE (node)))
11824 case tcc_declaration:
11825 origin = decl_ultimate_origin (node);
11826 if (origin != NULL)
11827 add_abstract_origin_attribute (parm_die, origin);
11828 else
11830 add_name_and_src_coords_attributes (parm_die, node);
11831 add_type_attribute (parm_die, TREE_TYPE (node),
11832 TREE_READONLY (node),
11833 TREE_THIS_VOLATILE (node),
11834 context_die);
11835 if (DECL_ARTIFICIAL (node))
11836 add_AT_flag (parm_die, DW_AT_artificial, 1);
11839 equate_decl_number_to_die (node, parm_die);
11840 if (! DECL_ABSTRACT (node))
11841 add_location_or_const_value_attribute (parm_die, node, DW_AT_location);
11843 break;
11845 case tcc_type:
11846 /* We were called with some kind of a ..._TYPE node. */
11847 add_type_attribute (parm_die, node, 0, 0, context_die);
11848 break;
11850 default:
11851 gcc_unreachable ();
11854 return parm_die;
11857 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
11858 at the end of an (ANSI prototyped) formal parameters list. */
11860 static void
11861 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
11863 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
11866 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
11867 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
11868 parameters as specified in some function type specification (except for
11869 those which appear as part of a function *definition*). */
11871 static void
11872 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
11874 tree link;
11875 tree formal_type = NULL;
11876 tree first_parm_type;
11877 tree arg;
11879 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
11881 arg = DECL_ARGUMENTS (function_or_method_type);
11882 function_or_method_type = TREE_TYPE (function_or_method_type);
11884 else
11885 arg = NULL_TREE;
11887 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
11889 /* Make our first pass over the list of formal parameter types and output a
11890 DW_TAG_formal_parameter DIE for each one. */
11891 for (link = first_parm_type; link; )
11893 dw_die_ref parm_die;
11895 formal_type = TREE_VALUE (link);
11896 if (formal_type == void_type_node)
11897 break;
11899 /* Output a (nameless) DIE to represent the formal parameter itself. */
11900 parm_die = gen_formal_parameter_die (formal_type, context_die);
11901 if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
11902 && link == first_parm_type)
11903 || (arg && DECL_ARTIFICIAL (arg)))
11904 add_AT_flag (parm_die, DW_AT_artificial, 1);
11906 link = TREE_CHAIN (link);
11907 if (arg)
11908 arg = TREE_CHAIN (arg);
11911 /* If this function type has an ellipsis, add a
11912 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
11913 if (formal_type != void_type_node)
11914 gen_unspecified_parameters_die (function_or_method_type, context_die);
11916 /* Make our second (and final) pass over the list of formal parameter types
11917 and output DIEs to represent those types (as necessary). */
11918 for (link = TYPE_ARG_TYPES (function_or_method_type);
11919 link && TREE_VALUE (link);
11920 link = TREE_CHAIN (link))
11921 gen_type_die (TREE_VALUE (link), context_die);
11924 /* We want to generate the DIE for TYPE so that we can generate the
11925 die for MEMBER, which has been defined; we will need to refer back
11926 to the member declaration nested within TYPE. If we're trying to
11927 generate minimal debug info for TYPE, processing TYPE won't do the
11928 trick; we need to attach the member declaration by hand. */
11930 static void
11931 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
11933 gen_type_die (type, context_die);
11935 /* If we're trying to avoid duplicate debug info, we may not have
11936 emitted the member decl for this function. Emit it now. */
11937 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
11938 && ! lookup_decl_die (member))
11940 dw_die_ref type_die;
11941 gcc_assert (!decl_ultimate_origin (member));
11943 push_decl_scope (type);
11944 type_die = lookup_type_die (type);
11945 if (TREE_CODE (member) == FUNCTION_DECL)
11946 gen_subprogram_die (member, type_die);
11947 else if (TREE_CODE (member) == FIELD_DECL)
11949 /* Ignore the nameless fields that are used to skip bits but handle
11950 C++ anonymous unions and structs. */
11951 if (DECL_NAME (member) != NULL_TREE
11952 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
11953 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
11955 gen_type_die (member_declared_type (member), type_die);
11956 gen_field_die (member, type_die);
11959 else
11960 gen_variable_die (member, type_die);
11962 pop_decl_scope ();
11966 /* Generate the DWARF2 info for the "abstract" instance of a function which we
11967 may later generate inlined and/or out-of-line instances of. */
11969 static void
11970 dwarf2out_abstract_function (tree decl)
11972 dw_die_ref old_die;
11973 tree save_fn;
11974 tree context;
11975 int was_abstract = DECL_ABSTRACT (decl);
11977 /* Make sure we have the actual abstract inline, not a clone. */
11978 decl = DECL_ORIGIN (decl);
11980 old_die = lookup_decl_die (decl);
11981 if (old_die && get_AT (old_die, DW_AT_inline))
11982 /* We've already generated the abstract instance. */
11983 return;
11985 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
11986 we don't get confused by DECL_ABSTRACT. */
11987 if (debug_info_level > DINFO_LEVEL_TERSE)
11989 context = decl_class_context (decl);
11990 if (context)
11991 gen_type_die_for_member
11992 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
11995 /* Pretend we've just finished compiling this function. */
11996 save_fn = current_function_decl;
11997 current_function_decl = decl;
11998 push_cfun (DECL_STRUCT_FUNCTION (decl));
12000 set_decl_abstract_flags (decl, 1);
12001 dwarf2out_decl (decl);
12002 if (! was_abstract)
12003 set_decl_abstract_flags (decl, 0);
12005 current_function_decl = save_fn;
12006 pop_cfun ();
12009 /* Helper function of premark_used_types() which gets called through
12010 htab_traverse_resize().
12012 Marks the DIE of a given type in *SLOT as perennial, so it never gets
12013 marked as unused by prune_unused_types. */
12014 static int
12015 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
12017 tree type;
12018 dw_die_ref die;
12020 type = *slot;
12021 die = lookup_type_die (type);
12022 if (die != NULL)
12023 die->die_perennial_p = 1;
12024 return 1;
12027 /* Mark all members of used_types_hash as perennial. */
12028 static void
12029 premark_used_types (void)
12031 if (cfun && cfun->used_types_hash)
12032 htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
12035 /* Generate a DIE to represent a declared function (either file-scope or
12036 block-local). */
12038 static void
12039 gen_subprogram_die (tree decl, dw_die_ref context_die)
12041 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
12042 tree origin = decl_ultimate_origin (decl);
12043 dw_die_ref subr_die;
12044 tree fn_arg_types;
12045 tree outer_scope;
12046 dw_die_ref old_die = lookup_decl_die (decl);
12047 int declaration = (current_function_decl != decl
12048 || class_or_namespace_scope_p (context_die));
12050 premark_used_types ();
12052 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
12053 started to generate the abstract instance of an inline, decided to output
12054 its containing class, and proceeded to emit the declaration of the inline
12055 from the member list for the class. If so, DECLARATION takes priority;
12056 we'll get back to the abstract instance when done with the class. */
12058 /* The class-scope declaration DIE must be the primary DIE. */
12059 if (origin && declaration && class_or_namespace_scope_p (context_die))
12061 origin = NULL;
12062 gcc_assert (!old_die);
12065 /* Now that the C++ front end lazily declares artificial member fns, we
12066 might need to retrofit the declaration into its class. */
12067 if (!declaration && !origin && !old_die
12068 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
12069 && !class_or_namespace_scope_p (context_die)
12070 && debug_info_level > DINFO_LEVEL_TERSE)
12071 old_die = force_decl_die (decl);
12073 if (origin != NULL)
12075 gcc_assert (!declaration || local_scope_p (context_die));
12077 /* Fixup die_parent for the abstract instance of a nested
12078 inline function. */
12079 if (old_die && old_die->die_parent == NULL)
12080 add_child_die (context_die, old_die);
12082 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
12083 add_abstract_origin_attribute (subr_die, origin);
12085 else if (old_die)
12087 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12088 struct dwarf_file_data * file_index = lookup_filename (s.file);
12090 if (!get_AT_flag (old_die, DW_AT_declaration)
12091 /* We can have a normal definition following an inline one in the
12092 case of redefinition of GNU C extern inlines.
12093 It seems reasonable to use AT_specification in this case. */
12094 && !get_AT (old_die, DW_AT_inline))
12096 /* Detect and ignore this case, where we are trying to output
12097 something we have already output. */
12098 return;
12101 /* If the definition comes from the same place as the declaration,
12102 maybe use the old DIE. We always want the DIE for this function
12103 that has the *_pc attributes to be under comp_unit_die so the
12104 debugger can find it. We also need to do this for abstract
12105 instances of inlines, since the spec requires the out-of-line copy
12106 to have the same parent. For local class methods, this doesn't
12107 apply; we just use the old DIE. */
12108 if ((old_die->die_parent == comp_unit_die || context_die == NULL)
12109 && (DECL_ARTIFICIAL (decl)
12110 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
12111 && (get_AT_unsigned (old_die, DW_AT_decl_line)
12112 == (unsigned) s.line))))
12114 subr_die = old_die;
12116 /* Clear out the declaration attribute and the formal parameters.
12117 Do not remove all children, because it is possible that this
12118 declaration die was forced using force_decl_die(). In such
12119 cases die that forced declaration die (e.g. TAG_imported_module)
12120 is one of the children that we do not want to remove. */
12121 remove_AT (subr_die, DW_AT_declaration);
12122 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
12124 else
12126 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
12127 add_AT_specification (subr_die, old_die);
12128 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
12129 add_AT_file (subr_die, DW_AT_decl_file, file_index);
12130 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
12131 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
12134 else
12136 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
12138 if (TREE_PUBLIC (decl))
12139 add_AT_flag (subr_die, DW_AT_external, 1);
12141 add_name_and_src_coords_attributes (subr_die, decl);
12142 if (debug_info_level > DINFO_LEVEL_TERSE)
12144 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
12145 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
12146 0, 0, context_die);
12149 add_pure_or_virtual_attribute (subr_die, decl);
12150 if (DECL_ARTIFICIAL (decl))
12151 add_AT_flag (subr_die, DW_AT_artificial, 1);
12153 if (TREE_PROTECTED (decl))
12154 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
12155 else if (TREE_PRIVATE (decl))
12156 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
12159 if (declaration)
12161 if (!old_die || !get_AT (old_die, DW_AT_inline))
12163 add_AT_flag (subr_die, DW_AT_declaration, 1);
12165 /* The first time we see a member function, it is in the context of
12166 the class to which it belongs. We make sure of this by emitting
12167 the class first. The next time is the definition, which is
12168 handled above. The two may come from the same source text.
12170 Note that force_decl_die() forces function declaration die. It is
12171 later reused to represent definition. */
12172 equate_decl_number_to_die (decl, subr_die);
12175 else if (DECL_ABSTRACT (decl))
12177 if (DECL_DECLARED_INLINE_P (decl))
12179 if (cgraph_function_possibly_inlined_p (decl))
12180 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
12181 else
12182 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
12184 else
12186 if (cgraph_function_possibly_inlined_p (decl))
12187 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
12188 else
12189 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
12192 equate_decl_number_to_die (decl, subr_die);
12194 else if (!DECL_EXTERNAL (decl))
12196 HOST_WIDE_INT cfa_fb_offset;
12198 if (!old_die || !get_AT (old_die, DW_AT_inline))
12199 equate_decl_number_to_die (decl, subr_die);
12201 if (!flag_reorder_blocks_and_partition)
12203 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
12204 current_function_funcdef_no);
12205 add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
12206 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
12207 current_function_funcdef_no);
12208 add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
12210 add_pubname (decl, subr_die);
12211 add_arange (decl, subr_die);
12213 else
12214 { /* Do nothing for now; maybe need to duplicate die, one for
12215 hot section and ond for cold section, then use the hot/cold
12216 section begin/end labels to generate the aranges... */
12218 add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
12219 add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
12220 add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
12221 add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
12223 add_pubname (decl, subr_die);
12224 add_arange (decl, subr_die);
12225 add_arange (decl, subr_die);
12229 #ifdef MIPS_DEBUGGING_INFO
12230 /* Add a reference to the FDE for this routine. */
12231 add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
12232 #endif
12234 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
12236 /* We define the "frame base" as the function's CFA. This is more
12237 convenient for several reasons: (1) It's stable across the prologue
12238 and epilogue, which makes it better than just a frame pointer,
12239 (2) With dwarf3, there exists a one-byte encoding that allows us
12240 to reference the .debug_frame data by proxy, but failing that,
12241 (3) We can at least reuse the code inspection and interpretation
12242 code that determines the CFA position at various points in the
12243 function. */
12244 /* ??? Use some command-line or configury switch to enable the use
12245 of dwarf3 DW_OP_call_frame_cfa. At present there are no dwarf
12246 consumers that understand it; fall back to "pure" dwarf2 and
12247 convert the CFA data into a location list. */
12249 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
12250 if (list->dw_loc_next)
12251 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
12252 else
12253 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
12256 /* Compute a displacement from the "steady-state frame pointer" to
12257 the CFA. The former is what all stack slots and argument slots
12258 will reference in the rtl; the later is what we've told the
12259 debugger about. We'll need to adjust all frame_base references
12260 by this displacement. */
12261 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
12263 if (cfun->static_chain_decl)
12264 add_AT_location_description (subr_die, DW_AT_static_link,
12265 loc_descriptor_from_tree (cfun->static_chain_decl));
12268 /* Now output descriptions of the arguments for this function. This gets
12269 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
12270 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
12271 `...' at the end of the formal parameter list. In order to find out if
12272 there was a trailing ellipsis or not, we must instead look at the type
12273 associated with the FUNCTION_DECL. This will be a node of type
12274 FUNCTION_TYPE. If the chain of type nodes hanging off of this
12275 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
12276 an ellipsis at the end. */
12278 /* In the case where we are describing a mere function declaration, all we
12279 need to do here (and all we *can* do here) is to describe the *types* of
12280 its formal parameters. */
12281 if (debug_info_level <= DINFO_LEVEL_TERSE)
12283 else if (declaration)
12284 gen_formal_types_die (decl, subr_die);
12285 else
12287 /* Generate DIEs to represent all known formal parameters. */
12288 tree arg_decls = DECL_ARGUMENTS (decl);
12289 tree parm;
12291 /* When generating DIEs, generate the unspecified_parameters DIE
12292 instead if we come across the arg "__builtin_va_alist" */
12293 for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
12294 if (TREE_CODE (parm) == PARM_DECL)
12296 if (DECL_NAME (parm)
12297 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
12298 "__builtin_va_alist"))
12299 gen_unspecified_parameters_die (parm, subr_die);
12300 else
12301 gen_decl_die (parm, subr_die);
12304 /* Decide whether we need an unspecified_parameters DIE at the end.
12305 There are 2 more cases to do this for: 1) the ansi ... declaration -
12306 this is detectable when the end of the arg list is not a
12307 void_type_node 2) an unprototyped function declaration (not a
12308 definition). This just means that we have no info about the
12309 parameters at all. */
12310 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
12311 if (fn_arg_types != NULL)
12313 /* This is the prototyped case, check for.... */
12314 if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
12315 gen_unspecified_parameters_die (decl, subr_die);
12317 else if (DECL_INITIAL (decl) == NULL_TREE)
12318 gen_unspecified_parameters_die (decl, subr_die);
12321 /* Output Dwarf info for all of the stuff within the body of the function
12322 (if it has one - it may be just a declaration). */
12323 outer_scope = DECL_INITIAL (decl);
12325 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
12326 a function. This BLOCK actually represents the outermost binding contour
12327 for the function, i.e. the contour in which the function's formal
12328 parameters and labels get declared. Curiously, it appears that the front
12329 end doesn't actually put the PARM_DECL nodes for the current function onto
12330 the BLOCK_VARS list for this outer scope, but are strung off of the
12331 DECL_ARGUMENTS list for the function instead.
12333 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
12334 the LABEL_DECL nodes for the function however, and we output DWARF info
12335 for those in decls_for_scope. Just within the `outer_scope' there will be
12336 a BLOCK node representing the function's outermost pair of curly braces,
12337 and any blocks used for the base and member initializers of a C++
12338 constructor function. */
12339 if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
12341 /* Emit a DW_TAG_variable DIE for a named return value. */
12342 if (DECL_NAME (DECL_RESULT (decl)))
12343 gen_decl_die (DECL_RESULT (decl), subr_die);
12345 current_function_has_inlines = 0;
12346 decls_for_scope (outer_scope, subr_die, 0);
12348 #if 0 && defined (MIPS_DEBUGGING_INFO)
12349 if (current_function_has_inlines)
12351 add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
12352 if (! comp_unit_has_inlines)
12354 add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
12355 comp_unit_has_inlines = 1;
12358 #endif
12360 /* Add the calling convention attribute if requested. */
12361 add_calling_convention_attribute (subr_die, TREE_TYPE (decl));
12365 /* Generate a DIE to represent a declared data object. */
12367 static void
12368 gen_variable_die (tree decl, dw_die_ref context_die)
12370 tree origin = decl_ultimate_origin (decl);
12371 dw_die_ref var_die = new_die (DW_TAG_variable, context_die, decl);
12373 dw_die_ref old_die = lookup_decl_die (decl);
12374 int declaration = (DECL_EXTERNAL (decl)
12375 /* If DECL is COMDAT and has not actually been
12376 emitted, we cannot take its address; there
12377 might end up being no definition anywhere in
12378 the program. For example, consider the C++
12379 test case:
12381 template <class T>
12382 struct S { static const int i = 7; };
12384 template <class T>
12385 const int S<T>::i;
12387 int f() { return S<int>::i; }
12389 Here, S<int>::i is not DECL_EXTERNAL, but no
12390 definition is required, so the compiler will
12391 not emit a definition. */
12392 || (TREE_CODE (decl) == VAR_DECL
12393 && DECL_COMDAT (decl) && !TREE_ASM_WRITTEN (decl))
12394 || class_or_namespace_scope_p (context_die));
12396 if (origin != NULL)
12397 add_abstract_origin_attribute (var_die, origin);
12399 /* Loop unrolling can create multiple blocks that refer to the same
12400 static variable, so we must test for the DW_AT_declaration flag.
12402 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
12403 copy decls and set the DECL_ABSTRACT flag on them instead of
12404 sharing them.
12406 ??? Duplicated blocks have been rewritten to use .debug_ranges.
12408 ??? The declare_in_namespace support causes us to get two DIEs for one
12409 variable, both of which are declarations. We want to avoid considering
12410 one to be a specification, so we must test that this DIE is not a
12411 declaration. */
12412 else if (old_die && TREE_STATIC (decl) && ! declaration
12413 && get_AT_flag (old_die, DW_AT_declaration) == 1)
12415 /* This is a definition of a C++ class level static. */
12416 add_AT_specification (var_die, old_die);
12417 if (DECL_NAME (decl))
12419 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12420 struct dwarf_file_data * file_index = lookup_filename (s.file);
12422 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
12423 add_AT_file (var_die, DW_AT_decl_file, file_index);
12425 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
12426 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
12429 else
12431 add_name_and_src_coords_attributes (var_die, decl);
12432 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
12433 TREE_THIS_VOLATILE (decl), context_die);
12435 if (TREE_PUBLIC (decl))
12436 add_AT_flag (var_die, DW_AT_external, 1);
12438 if (DECL_ARTIFICIAL (decl))
12439 add_AT_flag (var_die, DW_AT_artificial, 1);
12441 if (TREE_PROTECTED (decl))
12442 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
12443 else if (TREE_PRIVATE (decl))
12444 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
12447 if (declaration)
12448 add_AT_flag (var_die, DW_AT_declaration, 1);
12450 if (DECL_ABSTRACT (decl) || declaration)
12451 equate_decl_number_to_die (decl, var_die);
12453 if (! declaration && ! DECL_ABSTRACT (decl))
12455 add_location_or_const_value_attribute (var_die, decl, DW_AT_location);
12456 add_pubname (decl, var_die);
12458 else
12459 tree_add_const_value_attribute (var_die, decl);
12462 /* Generate a DIE to represent a label identifier. */
12464 static void
12465 gen_label_die (tree decl, dw_die_ref context_die)
12467 tree origin = decl_ultimate_origin (decl);
12468 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
12469 rtx insn;
12470 char label[MAX_ARTIFICIAL_LABEL_BYTES];
12472 if (origin != NULL)
12473 add_abstract_origin_attribute (lbl_die, origin);
12474 else
12475 add_name_and_src_coords_attributes (lbl_die, decl);
12477 if (DECL_ABSTRACT (decl))
12478 equate_decl_number_to_die (decl, lbl_die);
12479 else
12481 insn = DECL_RTL_IF_SET (decl);
12483 /* Deleted labels are programmer specified labels which have been
12484 eliminated because of various optimizations. We still emit them
12485 here so that it is possible to put breakpoints on them. */
12486 if (insn
12487 && (LABEL_P (insn)
12488 || ((NOTE_P (insn)
12489 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
12491 /* When optimization is enabled (via -O) some parts of the compiler
12492 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
12493 represent source-level labels which were explicitly declared by
12494 the user. This really shouldn't be happening though, so catch
12495 it if it ever does happen. */
12496 gcc_assert (!INSN_DELETED_P (insn));
12498 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
12499 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
12504 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
12505 attributes to the DIE for a block STMT, to describe where the inlined
12506 function was called from. This is similar to add_src_coords_attributes. */
12508 static inline void
12509 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
12511 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
12513 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
12514 add_AT_unsigned (die, DW_AT_call_line, s.line);
12518 /* If STMT's abstract origin is a function declaration and STMT's
12519 first subblock's abstract origin is the function's outermost block,
12520 then we're looking at the main entry point. */
12521 static bool
12522 is_inlined_entry_point (const_tree stmt)
12524 tree decl, block;
12526 if (!stmt || TREE_CODE (stmt) != BLOCK)
12527 return false;
12529 decl = block_ultimate_origin (stmt);
12531 if (!decl || TREE_CODE (decl) != FUNCTION_DECL)
12532 return false;
12534 block = BLOCK_SUBBLOCKS (stmt);
12536 if (block)
12538 if (TREE_CODE (block) != BLOCK)
12539 return false;
12541 block = block_ultimate_origin (block);
12544 return block == DECL_INITIAL (decl);
12547 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
12548 Add low_pc and high_pc attributes to the DIE for a block STMT. */
12550 static inline void
12551 add_high_low_attributes (tree stmt, dw_die_ref die)
12553 char label[MAX_ARTIFICIAL_LABEL_BYTES];
12555 if (BLOCK_FRAGMENT_CHAIN (stmt))
12557 tree chain;
12559 if (is_inlined_entry_point (stmt))
12561 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
12562 BLOCK_NUMBER (stmt));
12563 add_AT_lbl_id (die, DW_AT_entry_pc, label);
12566 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
12568 chain = BLOCK_FRAGMENT_CHAIN (stmt);
12571 add_ranges (chain);
12572 chain = BLOCK_FRAGMENT_CHAIN (chain);
12574 while (chain);
12575 add_ranges (NULL);
12577 else
12579 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
12580 BLOCK_NUMBER (stmt));
12581 add_AT_lbl_id (die, DW_AT_low_pc, label);
12582 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
12583 BLOCK_NUMBER (stmt));
12584 add_AT_lbl_id (die, DW_AT_high_pc, label);
12588 /* Generate a DIE for a lexical block. */
12590 static void
12591 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
12593 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
12595 if (! BLOCK_ABSTRACT (stmt))
12596 add_high_low_attributes (stmt, stmt_die);
12598 decls_for_scope (stmt, stmt_die, depth);
12601 /* Generate a DIE for an inlined subprogram. */
12603 static void
12604 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
12606 tree decl = block_ultimate_origin (stmt);
12608 /* Emit info for the abstract instance first, if we haven't yet. We
12609 must emit this even if the block is abstract, otherwise when we
12610 emit the block below (or elsewhere), we may end up trying to emit
12611 a die whose origin die hasn't been emitted, and crashing. */
12612 dwarf2out_abstract_function (decl);
12614 if (! BLOCK_ABSTRACT (stmt))
12616 dw_die_ref subr_die
12617 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
12619 add_abstract_origin_attribute (subr_die, decl);
12620 add_high_low_attributes (stmt, subr_die);
12621 add_call_src_coords_attributes (stmt, subr_die);
12623 decls_for_scope (stmt, subr_die, depth);
12624 current_function_has_inlines = 1;
12626 else
12627 /* We may get here if we're the outer block of function A that was
12628 inlined into function B that was inlined into function C. When
12629 generating debugging info for C, dwarf2out_abstract_function(B)
12630 would mark all inlined blocks as abstract, including this one.
12631 So, we wouldn't (and shouldn't) expect labels to be generated
12632 for this one. Instead, just emit debugging info for
12633 declarations within the block. This is particularly important
12634 in the case of initializers of arguments passed from B to us:
12635 if they're statement expressions containing declarations, we
12636 wouldn't generate dies for their abstract variables, and then,
12637 when generating dies for the real variables, we'd die (pun
12638 intended :-) */
12639 gen_lexical_block_die (stmt, context_die, depth);
12642 /* Generate a DIE for a field in a record, or structure. */
12644 static void
12645 gen_field_die (tree decl, dw_die_ref context_die)
12647 dw_die_ref decl_die;
12649 if (TREE_TYPE (decl) == error_mark_node)
12650 return;
12652 decl_die = new_die (DW_TAG_member, context_die, decl);
12653 add_name_and_src_coords_attributes (decl_die, decl);
12654 add_type_attribute (decl_die, member_declared_type (decl),
12655 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
12656 context_die);
12658 if (DECL_BIT_FIELD_TYPE (decl))
12660 add_byte_size_attribute (decl_die, decl);
12661 add_bit_size_attribute (decl_die, decl);
12662 add_bit_offset_attribute (decl_die, decl);
12665 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
12666 add_data_member_location_attribute (decl_die, decl);
12668 if (DECL_ARTIFICIAL (decl))
12669 add_AT_flag (decl_die, DW_AT_artificial, 1);
12671 if (TREE_PROTECTED (decl))
12672 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
12673 else if (TREE_PRIVATE (decl))
12674 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
12676 /* Equate decl number to die, so that we can look up this decl later on. */
12677 equate_decl_number_to_die (decl, decl_die);
12680 #if 0
12681 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
12682 Use modified_type_die instead.
12683 We keep this code here just in case these types of DIEs may be needed to
12684 represent certain things in other languages (e.g. Pascal) someday. */
12686 static void
12687 gen_pointer_type_die (tree type, dw_die_ref context_die)
12689 dw_die_ref ptr_die
12690 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
12692 equate_type_number_to_die (type, ptr_die);
12693 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
12694 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
12697 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
12698 Use modified_type_die instead.
12699 We keep this code here just in case these types of DIEs may be needed to
12700 represent certain things in other languages (e.g. Pascal) someday. */
12702 static void
12703 gen_reference_type_die (tree type, dw_die_ref context_die)
12705 dw_die_ref ref_die
12706 = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
12708 equate_type_number_to_die (type, ref_die);
12709 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
12710 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
12712 #endif
12714 /* Generate a DIE for a pointer to a member type. */
12716 static void
12717 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
12719 dw_die_ref ptr_die
12720 = new_die (DW_TAG_ptr_to_member_type,
12721 scope_die_for (type, context_die), type);
12723 equate_type_number_to_die (type, ptr_die);
12724 add_AT_die_ref (ptr_die, DW_AT_containing_type,
12725 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
12726 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
12729 /* Generate the DIE for the compilation unit. */
12731 static dw_die_ref
12732 gen_compile_unit_die (const char *filename)
12734 dw_die_ref die;
12735 char producer[250];
12736 const char *language_string = lang_hooks.name;
12737 int language;
12739 die = new_die (DW_TAG_compile_unit, NULL, NULL);
12741 if (filename)
12743 add_name_attribute (die, filename);
12744 /* Don't add cwd for <built-in>. */
12745 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
12746 add_comp_dir_attribute (die);
12749 sprintf (producer, "%s %s", language_string, version_string);
12751 #ifdef MIPS_DEBUGGING_INFO
12752 /* The MIPS/SGI compilers place the 'cc' command line options in the producer
12753 string. The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
12754 not appear in the producer string, the debugger reaches the conclusion
12755 that the object file is stripped and has no debugging information.
12756 To get the MIPS/SGI debugger to believe that there is debugging
12757 information in the object file, we add a -g to the producer string. */
12758 if (debug_info_level > DINFO_LEVEL_TERSE)
12759 strcat (producer, " -g");
12760 #endif
12762 add_AT_string (die, DW_AT_producer, producer);
12764 if (strcmp (language_string, "GNU C++") == 0)
12765 language = DW_LANG_C_plus_plus;
12766 else if (strcmp (language_string, "GNU Ada") == 0)
12767 language = DW_LANG_Ada95;
12768 else if (strcmp (language_string, "GNU F77") == 0)
12769 language = DW_LANG_Fortran77;
12770 else if (strcmp (language_string, "GNU F95") == 0)
12771 language = DW_LANG_Fortran95;
12772 else if (strcmp (language_string, "GNU Pascal") == 0)
12773 language = DW_LANG_Pascal83;
12774 else if (strcmp (language_string, "GNU Java") == 0)
12775 language = DW_LANG_Java;
12776 else if (strcmp (language_string, "GNU Objective-C") == 0)
12777 language = DW_LANG_ObjC;
12778 else if (strcmp (language_string, "GNU Objective-C++") == 0)
12779 language = DW_LANG_ObjC_plus_plus;
12780 else
12781 language = DW_LANG_C89;
12783 add_AT_unsigned (die, DW_AT_language, language);
12784 return die;
12787 /* Generate the DIE for a base class. */
12789 static void
12790 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
12792 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
12794 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
12795 add_data_member_location_attribute (die, binfo);
12797 if (BINFO_VIRTUAL_P (binfo))
12798 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
12800 if (access == access_public_node)
12801 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
12802 else if (access == access_protected_node)
12803 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
12806 /* Generate a DIE for a class member. */
12808 static void
12809 gen_member_die (tree type, dw_die_ref context_die)
12811 tree member;
12812 tree binfo = TYPE_BINFO (type);
12813 dw_die_ref child;
12815 /* If this is not an incomplete type, output descriptions of each of its
12816 members. Note that as we output the DIEs necessary to represent the
12817 members of this record or union type, we will also be trying to output
12818 DIEs to represent the *types* of those members. However the `type'
12819 function (above) will specifically avoid generating type DIEs for member
12820 types *within* the list of member DIEs for this (containing) type except
12821 for those types (of members) which are explicitly marked as also being
12822 members of this (containing) type themselves. The g++ front- end can
12823 force any given type to be treated as a member of some other (containing)
12824 type by setting the TYPE_CONTEXT of the given (member) type to point to
12825 the TREE node representing the appropriate (containing) type. */
12827 /* First output info about the base classes. */
12828 if (binfo)
12830 VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
12831 int i;
12832 tree base;
12834 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
12835 gen_inheritance_die (base,
12836 (accesses ? VEC_index (tree, accesses, i)
12837 : access_public_node), context_die);
12840 /* Now output info about the data members and type members. */
12841 for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
12843 /* If we thought we were generating minimal debug info for TYPE
12844 and then changed our minds, some of the member declarations
12845 may have already been defined. Don't define them again, but
12846 do put them in the right order. */
12848 child = lookup_decl_die (member);
12849 if (child)
12850 splice_child_die (context_die, child);
12851 else
12852 gen_decl_die (member, context_die);
12855 /* Now output info about the function members (if any). */
12856 for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
12858 /* Don't include clones in the member list. */
12859 if (DECL_ABSTRACT_ORIGIN (member))
12860 continue;
12862 child = lookup_decl_die (member);
12863 if (child)
12864 splice_child_die (context_die, child);
12865 else
12866 gen_decl_die (member, context_die);
12870 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
12871 is set, we pretend that the type was never defined, so we only get the
12872 member DIEs needed by later specification DIEs. */
12874 static void
12875 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
12876 enum debug_info_usage usage)
12878 dw_die_ref type_die = lookup_type_die (type);
12879 dw_die_ref scope_die = 0;
12880 int nested = 0;
12881 int complete = (TYPE_SIZE (type)
12882 && (! TYPE_STUB_DECL (type)
12883 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
12884 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
12885 complete = complete && should_emit_struct_debug (type, usage);
12887 if (type_die && ! complete)
12888 return;
12890 if (TYPE_CONTEXT (type) != NULL_TREE
12891 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
12892 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
12893 nested = 1;
12895 scope_die = scope_die_for (type, context_die);
12897 if (! type_die || (nested && scope_die == comp_unit_die))
12898 /* First occurrence of type or toplevel definition of nested class. */
12900 dw_die_ref old_die = type_die;
12902 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
12903 ? DW_TAG_structure_type : DW_TAG_union_type,
12904 scope_die, type);
12905 equate_type_number_to_die (type, type_die);
12906 if (old_die)
12907 add_AT_specification (type_die, old_die);
12908 else
12909 add_name_attribute (type_die, type_tag (type));
12911 else
12912 remove_AT (type_die, DW_AT_declaration);
12914 /* If this type has been completed, then give it a byte_size attribute and
12915 then give a list of members. */
12916 if (complete && !ns_decl)
12918 /* Prevent infinite recursion in cases where the type of some member of
12919 this type is expressed in terms of this type itself. */
12920 TREE_ASM_WRITTEN (type) = 1;
12921 add_byte_size_attribute (type_die, type);
12922 if (TYPE_STUB_DECL (type) != NULL_TREE)
12923 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
12925 /* If the first reference to this type was as the return type of an
12926 inline function, then it may not have a parent. Fix this now. */
12927 if (type_die->die_parent == NULL)
12928 add_child_die (scope_die, type_die);
12930 push_decl_scope (type);
12931 gen_member_die (type, type_die);
12932 pop_decl_scope ();
12934 /* GNU extension: Record what type our vtable lives in. */
12935 if (TYPE_VFIELD (type))
12937 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
12939 gen_type_die (vtype, context_die);
12940 add_AT_die_ref (type_die, DW_AT_containing_type,
12941 lookup_type_die (vtype));
12944 else
12946 add_AT_flag (type_die, DW_AT_declaration, 1);
12948 /* We don't need to do this for function-local types. */
12949 if (TYPE_STUB_DECL (type)
12950 && ! decl_function_context (TYPE_STUB_DECL (type)))
12951 VEC_safe_push (tree, gc, incomplete_types, type);
12954 if (get_AT (type_die, DW_AT_name))
12955 add_pubtype (type, type_die);
12958 /* Generate a DIE for a subroutine _type_. */
12960 static void
12961 gen_subroutine_type_die (tree type, dw_die_ref context_die)
12963 tree return_type = TREE_TYPE (type);
12964 dw_die_ref subr_die
12965 = new_die (DW_TAG_subroutine_type,
12966 scope_die_for (type, context_die), type);
12968 equate_type_number_to_die (type, subr_die);
12969 add_prototyped_attribute (subr_die, type);
12970 add_type_attribute (subr_die, return_type, 0, 0, context_die);
12971 gen_formal_types_die (type, subr_die);
12973 if (get_AT (subr_die, DW_AT_name))
12974 add_pubtype (type, subr_die);
12977 /* Generate a DIE for a type definition. */
12979 static void
12980 gen_typedef_die (tree decl, dw_die_ref context_die)
12982 dw_die_ref type_die;
12983 tree origin;
12985 if (TREE_ASM_WRITTEN (decl))
12986 return;
12988 TREE_ASM_WRITTEN (decl) = 1;
12989 type_die = new_die (DW_TAG_typedef, context_die, decl);
12990 origin = decl_ultimate_origin (decl);
12991 if (origin != NULL)
12992 add_abstract_origin_attribute (type_die, origin);
12993 else
12995 tree type;
12997 add_name_and_src_coords_attributes (type_die, decl);
12998 if (DECL_ORIGINAL_TYPE (decl))
13000 type = DECL_ORIGINAL_TYPE (decl);
13002 gcc_assert (type != TREE_TYPE (decl));
13003 equate_type_number_to_die (TREE_TYPE (decl), type_die);
13005 else
13006 type = TREE_TYPE (decl);
13008 add_type_attribute (type_die, type, TREE_READONLY (decl),
13009 TREE_THIS_VOLATILE (decl), context_die);
13012 if (DECL_ABSTRACT (decl))
13013 equate_decl_number_to_die (decl, type_die);
13015 if (get_AT (type_die, DW_AT_name))
13016 add_pubtype (decl, type_die);
13019 /* Generate a type description DIE. */
13021 static void
13022 gen_type_die_with_usage (tree type, dw_die_ref context_die,
13023 enum debug_info_usage usage)
13025 int need_pop;
13027 if (type == NULL_TREE || type == error_mark_node)
13028 return;
13030 if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
13031 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
13033 if (TREE_ASM_WRITTEN (type))
13034 return;
13036 /* Prevent broken recursion; we can't hand off to the same type. */
13037 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
13039 TREE_ASM_WRITTEN (type) = 1;
13040 gen_decl_die (TYPE_NAME (type), context_die);
13041 return;
13044 /* We are going to output a DIE to represent the unqualified version
13045 of this type (i.e. without any const or volatile qualifiers) so
13046 get the main variant (i.e. the unqualified version) of this type
13047 now. (Vectors are special because the debugging info is in the
13048 cloned type itself). */
13049 if (TREE_CODE (type) != VECTOR_TYPE)
13050 type = type_main_variant (type);
13052 if (TREE_ASM_WRITTEN (type))
13053 return;
13055 switch (TREE_CODE (type))
13057 case ERROR_MARK:
13058 break;
13060 case POINTER_TYPE:
13061 case REFERENCE_TYPE:
13062 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
13063 ensures that the gen_type_die recursion will terminate even if the
13064 type is recursive. Recursive types are possible in Ada. */
13065 /* ??? We could perhaps do this for all types before the switch
13066 statement. */
13067 TREE_ASM_WRITTEN (type) = 1;
13069 /* For these types, all that is required is that we output a DIE (or a
13070 set of DIEs) to represent the "basis" type. */
13071 gen_type_die_with_usage (TREE_TYPE (type), context_die,
13072 DINFO_USAGE_IND_USE);
13073 break;
13075 case OFFSET_TYPE:
13076 /* This code is used for C++ pointer-to-data-member types.
13077 Output a description of the relevant class type. */
13078 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
13079 DINFO_USAGE_IND_USE);
13081 /* Output a description of the type of the object pointed to. */
13082 gen_type_die_with_usage (TREE_TYPE (type), context_die,
13083 DINFO_USAGE_IND_USE);
13085 /* Now output a DIE to represent this pointer-to-data-member type
13086 itself. */
13087 gen_ptr_to_mbr_type_die (type, context_die);
13088 break;
13090 case FUNCTION_TYPE:
13091 /* Force out return type (in case it wasn't forced out already). */
13092 gen_type_die_with_usage (TREE_TYPE (type), context_die,
13093 DINFO_USAGE_DIR_USE);
13094 gen_subroutine_type_die (type, context_die);
13095 break;
13097 case METHOD_TYPE:
13098 /* Force out return type (in case it wasn't forced out already). */
13099 gen_type_die_with_usage (TREE_TYPE (type), context_die,
13100 DINFO_USAGE_DIR_USE);
13101 gen_subroutine_type_die (type, context_die);
13102 break;
13104 case ARRAY_TYPE:
13105 gen_array_type_die (type, context_die);
13106 break;
13108 case VECTOR_TYPE:
13109 gen_array_type_die (type, context_die);
13110 break;
13112 case ENUMERAL_TYPE:
13113 case RECORD_TYPE:
13114 case UNION_TYPE:
13115 case QUAL_UNION_TYPE:
13116 /* If this is a nested type whose containing class hasn't been written
13117 out yet, writing it out will cover this one, too. This does not apply
13118 to instantiations of member class templates; they need to be added to
13119 the containing class as they are generated. FIXME: This hurts the
13120 idea of combining type decls from multiple TUs, since we can't predict
13121 what set of template instantiations we'll get. */
13122 if (TYPE_CONTEXT (type)
13123 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
13124 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
13126 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
13128 if (TREE_ASM_WRITTEN (type))
13129 return;
13131 /* If that failed, attach ourselves to the stub. */
13132 push_decl_scope (TYPE_CONTEXT (type));
13133 context_die = lookup_type_die (TYPE_CONTEXT (type));
13134 need_pop = 1;
13136 else
13138 declare_in_namespace (type, context_die);
13139 need_pop = 0;
13142 if (TREE_CODE (type) == ENUMERAL_TYPE)
13144 /* This might have been written out by the call to
13145 declare_in_namespace. */
13146 if (!TREE_ASM_WRITTEN (type))
13147 gen_enumeration_type_die (type, context_die);
13149 else
13150 gen_struct_or_union_type_die (type, context_die, usage);
13152 if (need_pop)
13153 pop_decl_scope ();
13155 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
13156 it up if it is ever completed. gen_*_type_die will set it for us
13157 when appropriate. */
13158 return;
13160 case VOID_TYPE:
13161 case INTEGER_TYPE:
13162 case REAL_TYPE:
13163 case FIXED_POINT_TYPE:
13164 case COMPLEX_TYPE:
13165 case BOOLEAN_TYPE:
13166 /* No DIEs needed for fundamental types. */
13167 break;
13169 case LANG_TYPE:
13170 /* No Dwarf representation currently defined. */
13171 break;
13173 default:
13174 gcc_unreachable ();
13177 TREE_ASM_WRITTEN (type) = 1;
13180 static void
13181 gen_type_die (tree type, dw_die_ref context_die)
13183 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
13186 /* Generate a DIE for a tagged type instantiation. */
13188 static void
13189 gen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
13191 if (type == NULL_TREE || type == error_mark_node)
13192 return;
13194 /* We are going to output a DIE to represent the unqualified version of
13195 this type (i.e. without any const or volatile qualifiers) so make sure
13196 that we have the main variant (i.e. the unqualified version) of this
13197 type now. */
13198 gcc_assert (type == type_main_variant (type));
13200 /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
13201 an instance of an unresolved type. */
13203 switch (TREE_CODE (type))
13205 case ERROR_MARK:
13206 break;
13208 case ENUMERAL_TYPE:
13209 gen_inlined_enumeration_type_die (type, context_die);
13210 break;
13212 case RECORD_TYPE:
13213 gen_inlined_structure_type_die (type, context_die);
13214 break;
13216 case UNION_TYPE:
13217 case QUAL_UNION_TYPE:
13218 gen_inlined_union_type_die (type, context_die);
13219 break;
13221 default:
13222 gcc_unreachable ();
13226 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
13227 things which are local to the given block. */
13229 static void
13230 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
13232 int must_output_die = 0;
13233 tree origin;
13234 tree decl;
13235 enum tree_code origin_code;
13237 /* Ignore blocks that are NULL. */
13238 if (stmt == NULL_TREE)
13239 return;
13241 /* If the block is one fragment of a non-contiguous block, do not
13242 process the variables, since they will have been done by the
13243 origin block. Do process subblocks. */
13244 if (BLOCK_FRAGMENT_ORIGIN (stmt))
13246 tree sub;
13248 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
13249 gen_block_die (sub, context_die, depth + 1);
13251 return;
13254 /* Determine the "ultimate origin" of this block. This block may be an
13255 inlined instance of an inlined instance of inline function, so we have
13256 to trace all of the way back through the origin chain to find out what
13257 sort of node actually served as the original seed for the creation of
13258 the current block. */
13259 origin = block_ultimate_origin (stmt);
13260 origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
13262 /* Determine if we need to output any Dwarf DIEs at all to represent this
13263 block. */
13264 if (origin_code == FUNCTION_DECL)
13265 /* The outer scopes for inlinings *must* always be represented. We
13266 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
13267 must_output_die = 1;
13268 else
13270 /* In the case where the current block represents an inlining of the
13271 "body block" of an inline function, we must *NOT* output any DIE for
13272 this block because we have already output a DIE to represent the whole
13273 inlined function scope and the "body block" of any function doesn't
13274 really represent a different scope according to ANSI C rules. So we
13275 check here to make sure that this block does not represent a "body
13276 block inlining" before trying to set the MUST_OUTPUT_DIE flag. */
13277 if (! is_body_block (origin ? origin : stmt))
13279 /* Determine if this block directly contains any "significant"
13280 local declarations which we will need to output DIEs for. */
13281 if (debug_info_level > DINFO_LEVEL_TERSE)
13282 /* We are not in terse mode so *any* local declaration counts
13283 as being a "significant" one. */
13284 must_output_die = (BLOCK_VARS (stmt) != NULL
13285 && (TREE_USED (stmt)
13286 || TREE_ASM_WRITTEN (stmt)
13287 || BLOCK_ABSTRACT (stmt)));
13288 else
13289 /* We are in terse mode, so only local (nested) function
13290 definitions count as "significant" local declarations. */
13291 for (decl = BLOCK_VARS (stmt);
13292 decl != NULL; decl = TREE_CHAIN (decl))
13293 if (TREE_CODE (decl) == FUNCTION_DECL
13294 && DECL_INITIAL (decl))
13296 must_output_die = 1;
13297 break;
13302 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
13303 DIE for any block which contains no significant local declarations at
13304 all. Rather, in such cases we just call `decls_for_scope' so that any
13305 needed Dwarf info for any sub-blocks will get properly generated. Note
13306 that in terse mode, our definition of what constitutes a "significant"
13307 local declaration gets restricted to include only inlined function
13308 instances and local (nested) function definitions. */
13309 if (must_output_die)
13311 if (origin_code == FUNCTION_DECL)
13312 gen_inlined_subroutine_die (stmt, context_die, depth);
13313 else
13314 gen_lexical_block_die (stmt, context_die, depth);
13316 else
13317 decls_for_scope (stmt, context_die, depth);
13320 /* Generate all of the decls declared within a given scope and (recursively)
13321 all of its sub-blocks. */
13323 static void
13324 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
13326 tree decl;
13327 tree subblocks;
13329 /* Ignore NULL blocks. */
13330 if (stmt == NULL_TREE)
13331 return;
13333 if (TREE_USED (stmt))
13335 /* Output the DIEs to represent all of the data objects and typedefs
13336 declared directly within this block but not within any nested
13337 sub-blocks. Also, nested function and tag DIEs have been
13338 generated with a parent of NULL; fix that up now. */
13339 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
13341 dw_die_ref die;
13343 if (TREE_CODE (decl) == FUNCTION_DECL)
13344 die = lookup_decl_die (decl);
13345 else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
13346 die = lookup_type_die (TREE_TYPE (decl));
13347 else
13348 die = NULL;
13350 if (die != NULL && die->die_parent == NULL)
13351 add_child_die (context_die, die);
13352 /* Do not produce debug information for static variables since
13353 these might be optimized out. We are called for these later
13354 in varpool_analyze_pending_decls. */
13355 if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
13357 else
13358 gen_decl_die (decl, context_die);
13362 /* If we're at -g1, we're not interested in subblocks. */
13363 if (debug_info_level <= DINFO_LEVEL_TERSE)
13364 return;
13366 /* Output the DIEs to represent all sub-blocks (and the items declared
13367 therein) of this block. */
13368 for (subblocks = BLOCK_SUBBLOCKS (stmt);
13369 subblocks != NULL;
13370 subblocks = BLOCK_CHAIN (subblocks))
13371 gen_block_die (subblocks, context_die, depth + 1);
13374 /* Is this a typedef we can avoid emitting? */
13376 static inline int
13377 is_redundant_typedef (const_tree decl)
13379 if (TYPE_DECL_IS_STUB (decl))
13380 return 1;
13382 if (DECL_ARTIFICIAL (decl)
13383 && DECL_CONTEXT (decl)
13384 && is_tagged_type (DECL_CONTEXT (decl))
13385 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
13386 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
13387 /* Also ignore the artificial member typedef for the class name. */
13388 return 1;
13390 return 0;
13393 /* Returns the DIE for decl. A DIE will always be returned. */
13395 static dw_die_ref
13396 force_decl_die (tree decl)
13398 dw_die_ref decl_die;
13399 unsigned saved_external_flag;
13400 tree save_fn = NULL_TREE;
13401 decl_die = lookup_decl_die (decl);
13402 if (!decl_die)
13404 dw_die_ref context_die;
13405 tree decl_context = DECL_CONTEXT (decl);
13406 if (decl_context)
13408 /* Find die that represents this context. */
13409 if (TYPE_P (decl_context))
13410 context_die = force_type_die (decl_context);
13411 else
13412 context_die = force_decl_die (decl_context);
13414 else
13415 context_die = comp_unit_die;
13417 decl_die = lookup_decl_die (decl);
13418 if (decl_die)
13419 return decl_die;
13421 switch (TREE_CODE (decl))
13423 case FUNCTION_DECL:
13424 /* Clear current_function_decl, so that gen_subprogram_die thinks
13425 that this is a declaration. At this point, we just want to force
13426 declaration die. */
13427 save_fn = current_function_decl;
13428 current_function_decl = NULL_TREE;
13429 gen_subprogram_die (decl, context_die);
13430 current_function_decl = save_fn;
13431 break;
13433 case VAR_DECL:
13434 /* Set external flag to force declaration die. Restore it after
13435 gen_decl_die() call. */
13436 saved_external_flag = DECL_EXTERNAL (decl);
13437 DECL_EXTERNAL (decl) = 1;
13438 gen_decl_die (decl, context_die);
13439 DECL_EXTERNAL (decl) = saved_external_flag;
13440 break;
13442 case NAMESPACE_DECL:
13443 dwarf2out_decl (decl);
13444 break;
13446 default:
13447 gcc_unreachable ();
13450 /* We should be able to find the DIE now. */
13451 if (!decl_die)
13452 decl_die = lookup_decl_die (decl);
13453 gcc_assert (decl_die);
13456 return decl_die;
13459 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
13460 always returned. */
13462 static dw_die_ref
13463 force_type_die (tree type)
13465 dw_die_ref type_die;
13467 type_die = lookup_type_die (type);
13468 if (!type_die)
13470 dw_die_ref context_die;
13471 if (TYPE_CONTEXT (type))
13473 if (TYPE_P (TYPE_CONTEXT (type)))
13474 context_die = force_type_die (TYPE_CONTEXT (type));
13475 else
13476 context_die = force_decl_die (TYPE_CONTEXT (type));
13478 else
13479 context_die = comp_unit_die;
13481 type_die = lookup_type_die (type);
13482 if (type_die)
13483 return type_die;
13484 gen_type_die (type, context_die);
13485 type_die = lookup_type_die (type);
13486 gcc_assert (type_die);
13488 return type_die;
13491 /* Force out any required namespaces to be able to output DECL,
13492 and return the new context_die for it, if it's changed. */
13494 static dw_die_ref
13495 setup_namespace_context (tree thing, dw_die_ref context_die)
13497 tree context = (DECL_P (thing)
13498 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
13499 if (context && TREE_CODE (context) == NAMESPACE_DECL)
13500 /* Force out the namespace. */
13501 context_die = force_decl_die (context);
13503 return context_die;
13506 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
13507 type) within its namespace, if appropriate.
13509 For compatibility with older debuggers, namespace DIEs only contain
13510 declarations; all definitions are emitted at CU scope. */
13512 static void
13513 declare_in_namespace (tree thing, dw_die_ref context_die)
13515 dw_die_ref ns_context;
13517 if (debug_info_level <= DINFO_LEVEL_TERSE)
13518 return;
13520 /* If this decl is from an inlined function, then don't try to emit it in its
13521 namespace, as we will get confused. It would have already been emitted
13522 when the abstract instance of the inline function was emitted anyways. */
13523 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
13524 return;
13526 ns_context = setup_namespace_context (thing, context_die);
13528 if (ns_context != context_die)
13530 if (DECL_P (thing))
13531 gen_decl_die (thing, ns_context);
13532 else
13533 gen_type_die (thing, ns_context);
13537 /* Generate a DIE for a namespace or namespace alias. */
13539 static void
13540 gen_namespace_die (tree decl)
13542 dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
13544 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
13545 they are an alias of. */
13546 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
13548 /* Output a real namespace. */
13549 dw_die_ref namespace_die
13550 = new_die (DW_TAG_namespace, context_die, decl);
13551 add_name_and_src_coords_attributes (namespace_die, decl);
13552 equate_decl_number_to_die (decl, namespace_die);
13554 else
13556 /* Output a namespace alias. */
13558 /* Force out the namespace we are an alias of, if necessary. */
13559 dw_die_ref origin_die
13560 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
13562 /* Now create the namespace alias DIE. */
13563 dw_die_ref namespace_die
13564 = new_die (DW_TAG_imported_declaration, context_die, decl);
13565 add_name_and_src_coords_attributes (namespace_die, decl);
13566 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
13567 equate_decl_number_to_die (decl, namespace_die);
13571 /* Generate Dwarf debug information for a decl described by DECL. */
13573 static void
13574 gen_decl_die (tree decl, dw_die_ref context_die)
13576 tree origin;
13578 if (DECL_P (decl) && DECL_IGNORED_P (decl))
13579 return;
13581 switch (TREE_CODE (decl))
13583 case ERROR_MARK:
13584 break;
13586 case CONST_DECL:
13587 /* The individual enumerators of an enum type get output when we output
13588 the Dwarf representation of the relevant enum type itself. */
13589 break;
13591 case FUNCTION_DECL:
13592 /* Don't output any DIEs to represent mere function declarations,
13593 unless they are class members or explicit block externs. */
13594 if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
13595 && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
13596 break;
13598 #if 0
13599 /* FIXME */
13600 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
13601 on local redeclarations of global functions. That seems broken. */
13602 if (current_function_decl != decl)
13603 /* This is only a declaration. */;
13604 #endif
13606 /* If we're emitting a clone, emit info for the abstract instance. */
13607 if (DECL_ORIGIN (decl) != decl)
13608 dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
13610 /* If we're emitting an out-of-line copy of an inline function,
13611 emit info for the abstract instance and set up to refer to it. */
13612 else if (cgraph_function_possibly_inlined_p (decl)
13613 && ! DECL_ABSTRACT (decl)
13614 && ! class_or_namespace_scope_p (context_die)
13615 /* dwarf2out_abstract_function won't emit a die if this is just
13616 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
13617 that case, because that works only if we have a die. */
13618 && DECL_INITIAL (decl) != NULL_TREE)
13620 dwarf2out_abstract_function (decl);
13621 set_decl_origin_self (decl);
13624 /* Otherwise we're emitting the primary DIE for this decl. */
13625 else if (debug_info_level > DINFO_LEVEL_TERSE)
13627 /* Before we describe the FUNCTION_DECL itself, make sure that we
13628 have described its return type. */
13629 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
13631 /* And its virtual context. */
13632 if (DECL_VINDEX (decl) != NULL_TREE)
13633 gen_type_die (DECL_CONTEXT (decl), context_die);
13635 /* And its containing type. */
13636 origin = decl_class_context (decl);
13637 if (origin != NULL_TREE)
13638 gen_type_die_for_member (origin, decl, context_die);
13640 /* And its containing namespace. */
13641 declare_in_namespace (decl, context_die);
13644 /* Now output a DIE to represent the function itself. */
13645 gen_subprogram_die (decl, context_die);
13646 break;
13648 case TYPE_DECL:
13649 /* If we are in terse mode, don't generate any DIEs to represent any
13650 actual typedefs. */
13651 if (debug_info_level <= DINFO_LEVEL_TERSE)
13652 break;
13654 /* In the special case of a TYPE_DECL node representing the declaration
13655 of some type tag, if the given TYPE_DECL is marked as having been
13656 instantiated from some other (original) TYPE_DECL node (e.g. one which
13657 was generated within the original definition of an inline function) we
13658 have to generate a special (abbreviated) DW_TAG_structure_type,
13659 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. */
13660 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE
13661 && is_tagged_type (TREE_TYPE (decl)))
13663 gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
13664 break;
13667 if (is_redundant_typedef (decl))
13668 gen_type_die (TREE_TYPE (decl), context_die);
13669 else
13670 /* Output a DIE to represent the typedef itself. */
13671 gen_typedef_die (decl, context_die);
13672 break;
13674 case LABEL_DECL:
13675 if (debug_info_level >= DINFO_LEVEL_NORMAL)
13676 gen_label_die (decl, context_die);
13677 break;
13679 case VAR_DECL:
13680 case RESULT_DECL:
13681 /* If we are in terse mode, don't generate any DIEs to represent any
13682 variable declarations or definitions. */
13683 if (debug_info_level <= DINFO_LEVEL_TERSE)
13684 break;
13686 /* Output any DIEs that are needed to specify the type of this data
13687 object. */
13688 gen_type_die (TREE_TYPE (decl), context_die);
13690 /* And its containing type. */
13691 origin = decl_class_context (decl);
13692 if (origin != NULL_TREE)
13693 gen_type_die_for_member (origin, decl, context_die);
13695 /* And its containing namespace. */
13696 declare_in_namespace (decl, context_die);
13698 /* Now output the DIE to represent the data object itself. This gets
13699 complicated because of the possibility that the VAR_DECL really
13700 represents an inlined instance of a formal parameter for an inline
13701 function. */
13702 origin = decl_ultimate_origin (decl);
13703 if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
13704 gen_formal_parameter_die (decl, context_die);
13705 else
13706 gen_variable_die (decl, context_die);
13707 break;
13709 case FIELD_DECL:
13710 /* Ignore the nameless fields that are used to skip bits but handle C++
13711 anonymous unions and structs. */
13712 if (DECL_NAME (decl) != NULL_TREE
13713 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
13714 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
13716 gen_type_die (member_declared_type (decl), context_die);
13717 gen_field_die (decl, context_die);
13719 break;
13721 case PARM_DECL:
13722 gen_type_die (TREE_TYPE (decl), context_die);
13723 gen_formal_parameter_die (decl, context_die);
13724 break;
13726 case NAMESPACE_DECL:
13727 gen_namespace_die (decl);
13728 break;
13730 default:
13731 /* Probably some frontend-internal decl. Assume we don't care. */
13732 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
13733 break;
13737 /* Output debug information for global decl DECL. Called from toplev.c after
13738 compilation proper has finished. */
13740 static void
13741 dwarf2out_global_decl (tree decl)
13743 /* Output DWARF2 information for file-scope tentative data object
13744 declarations, file-scope (extern) function declarations (which had no
13745 corresponding body) and file-scope tagged type declarations and
13746 definitions which have not yet been forced out. */
13747 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
13748 dwarf2out_decl (decl);
13751 /* Output debug information for type decl DECL. Called from toplev.c
13752 and from language front ends (to record built-in types). */
13753 static void
13754 dwarf2out_type_decl (tree decl, int local)
13756 if (!local)
13757 dwarf2out_decl (decl);
13760 /* Output debug information for imported module or decl. */
13762 static void
13763 dwarf2out_imported_module_or_decl (tree decl, tree context)
13765 dw_die_ref imported_die, at_import_die;
13766 dw_die_ref scope_die;
13767 expanded_location xloc;
13769 if (debug_info_level <= DINFO_LEVEL_TERSE)
13770 return;
13772 gcc_assert (decl);
13774 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
13775 We need decl DIE for reference and scope die. First, get DIE for the decl
13776 itself. */
13778 /* Get the scope die for decl context. Use comp_unit_die for global module
13779 or decl. If die is not found for non globals, force new die. */
13780 if (!context)
13781 scope_die = comp_unit_die;
13782 else if (TYPE_P (context))
13784 if (!should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
13785 return;
13786 scope_die = force_type_die (context);
13788 else
13789 scope_die = force_decl_die (context);
13791 /* For TYPE_DECL or CONST_DECL, lookup TREE_TYPE. */
13792 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
13794 if (is_base_type (TREE_TYPE (decl)))
13795 at_import_die = base_type_die (TREE_TYPE (decl));
13796 else
13797 at_import_die = force_type_die (TREE_TYPE (decl));
13799 else
13801 at_import_die = lookup_decl_die (decl);
13802 if (!at_import_die)
13804 /* If we're trying to avoid duplicate debug info, we may not have
13805 emitted the member decl for this field. Emit it now. */
13806 if (TREE_CODE (decl) == FIELD_DECL)
13808 tree type = DECL_CONTEXT (decl);
13809 dw_die_ref type_context_die;
13811 if (TYPE_CONTEXT (type))
13812 if (TYPE_P (TYPE_CONTEXT (type)))
13814 if (!should_emit_struct_debug (TYPE_CONTEXT (type),
13815 DINFO_USAGE_DIR_USE))
13816 return;
13817 type_context_die = force_type_die (TYPE_CONTEXT (type));
13819 else
13820 type_context_die = force_decl_die (TYPE_CONTEXT (type));
13821 else
13822 type_context_die = comp_unit_die;
13823 gen_type_die_for_member (type, decl, type_context_die);
13825 at_import_die = force_decl_die (decl);
13829 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
13830 if (TREE_CODE (decl) == NAMESPACE_DECL)
13831 imported_die = new_die (DW_TAG_imported_module, scope_die, context);
13832 else
13833 imported_die = new_die (DW_TAG_imported_declaration, scope_die, context);
13835 xloc = expand_location (input_location);
13836 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
13837 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
13838 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
13841 /* Write the debugging output for DECL. */
13843 void
13844 dwarf2out_decl (tree decl)
13846 dw_die_ref context_die = comp_unit_die;
13848 switch (TREE_CODE (decl))
13850 case ERROR_MARK:
13851 return;
13853 case FUNCTION_DECL:
13854 /* What we would really like to do here is to filter out all mere
13855 file-scope declarations of file-scope functions which are never
13856 referenced later within this translation unit (and keep all of ones
13857 that *are* referenced later on) but we aren't clairvoyant, so we have
13858 no idea which functions will be referenced in the future (i.e. later
13859 on within the current translation unit). So here we just ignore all
13860 file-scope function declarations which are not also definitions. If
13861 and when the debugger needs to know something about these functions,
13862 it will have to hunt around and find the DWARF information associated
13863 with the definition of the function.
13865 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
13866 nodes represent definitions and which ones represent mere
13867 declarations. We have to check DECL_INITIAL instead. That's because
13868 the C front-end supports some weird semantics for "extern inline"
13869 function definitions. These can get inlined within the current
13870 translation unit (and thus, we need to generate Dwarf info for their
13871 abstract instances so that the Dwarf info for the concrete inlined
13872 instances can have something to refer to) but the compiler never
13873 generates any out-of-lines instances of such things (despite the fact
13874 that they *are* definitions).
13876 The important point is that the C front-end marks these "extern
13877 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
13878 them anyway. Note that the C++ front-end also plays some similar games
13879 for inline function definitions appearing within include files which
13880 also contain `#pragma interface' pragmas. */
13881 if (DECL_INITIAL (decl) == NULL_TREE)
13882 return;
13884 /* If we're a nested function, initially use a parent of NULL; if we're
13885 a plain function, this will be fixed up in decls_for_scope. If
13886 we're a method, it will be ignored, since we already have a DIE. */
13887 if (decl_function_context (decl)
13888 /* But if we're in terse mode, we don't care about scope. */
13889 && debug_info_level > DINFO_LEVEL_TERSE)
13890 context_die = NULL;
13891 break;
13893 case VAR_DECL:
13894 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
13895 declaration and if the declaration was never even referenced from
13896 within this entire compilation unit. We suppress these DIEs in
13897 order to save space in the .debug section (by eliminating entries
13898 which are probably useless). Note that we must not suppress
13899 block-local extern declarations (whether used or not) because that
13900 would screw-up the debugger's name lookup mechanism and cause it to
13901 miss things which really ought to be in scope at a given point. */
13902 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
13903 return;
13905 /* For local statics lookup proper context die. */
13906 if (TREE_STATIC (decl) && decl_function_context (decl))
13907 context_die = lookup_decl_die (DECL_CONTEXT (decl));
13909 /* If we are in terse mode, don't generate any DIEs to represent any
13910 variable declarations or definitions. */
13911 if (debug_info_level <= DINFO_LEVEL_TERSE)
13912 return;
13913 break;
13915 case NAMESPACE_DECL:
13916 if (debug_info_level <= DINFO_LEVEL_TERSE)
13917 return;
13918 if (lookup_decl_die (decl) != NULL)
13919 return;
13920 break;
13922 case TYPE_DECL:
13923 /* Don't emit stubs for types unless they are needed by other DIEs. */
13924 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
13925 return;
13927 /* Don't bother trying to generate any DIEs to represent any of the
13928 normal built-in types for the language we are compiling. */
13929 if (DECL_IS_BUILTIN (decl))
13931 /* OK, we need to generate one for `bool' so GDB knows what type
13932 comparisons have. */
13933 if (is_cxx ()
13934 && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
13935 && ! DECL_IGNORED_P (decl))
13936 modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
13938 return;
13941 /* If we are in terse mode, don't generate any DIEs for types. */
13942 if (debug_info_level <= DINFO_LEVEL_TERSE)
13943 return;
13945 /* If we're a function-scope tag, initially use a parent of NULL;
13946 this will be fixed up in decls_for_scope. */
13947 if (decl_function_context (decl))
13948 context_die = NULL;
13950 break;
13952 default:
13953 return;
13956 gen_decl_die (decl, context_die);
13959 /* Output a marker (i.e. a label) for the beginning of the generated code for
13960 a lexical block. */
13962 static void
13963 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
13964 unsigned int blocknum)
13966 switch_to_section (current_function_section ());
13967 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
13970 /* Output a marker (i.e. a label) for the end of the generated code for a
13971 lexical block. */
13973 static void
13974 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
13976 switch_to_section (current_function_section ());
13977 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
13980 /* Returns nonzero if it is appropriate not to emit any debugging
13981 information for BLOCK, because it doesn't contain any instructions.
13983 Don't allow this for blocks with nested functions or local classes
13984 as we would end up with orphans, and in the presence of scheduling
13985 we may end up calling them anyway. */
13987 static bool
13988 dwarf2out_ignore_block (const_tree block)
13990 tree decl;
13992 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
13993 if (TREE_CODE (decl) == FUNCTION_DECL
13994 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
13995 return 0;
13997 return 1;
14000 /* Hash table routines for file_hash. */
14002 static int
14003 file_table_eq (const void *p1_p, const void *p2_p)
14005 const struct dwarf_file_data * p1 = p1_p;
14006 const char * p2 = p2_p;
14007 return strcmp (p1->filename, p2) == 0;
14010 static hashval_t
14011 file_table_hash (const void *p_p)
14013 const struct dwarf_file_data * p = p_p;
14014 return htab_hash_string (p->filename);
14017 /* Lookup FILE_NAME (in the list of filenames that we know about here in
14018 dwarf2out.c) and return its "index". The index of each (known) filename is
14019 just a unique number which is associated with only that one filename. We
14020 need such numbers for the sake of generating labels (in the .debug_sfnames
14021 section) and references to those files numbers (in the .debug_srcinfo
14022 and.debug_macinfo sections). If the filename given as an argument is not
14023 found in our current list, add it to the list and assign it the next
14024 available unique index number. In order to speed up searches, we remember
14025 the index of the filename was looked up last. This handles the majority of
14026 all searches. */
14028 static struct dwarf_file_data *
14029 lookup_filename (const char *file_name)
14031 void ** slot;
14032 struct dwarf_file_data * created;
14034 /* Check to see if the file name that was searched on the previous
14035 call matches this file name. If so, return the index. */
14036 if (file_table_last_lookup
14037 && (file_name == file_table_last_lookup->filename
14038 || strcmp (file_table_last_lookup->filename, file_name) == 0))
14039 return file_table_last_lookup;
14041 /* Didn't match the previous lookup, search the table. */
14042 slot = htab_find_slot_with_hash (file_table, file_name,
14043 htab_hash_string (file_name), INSERT);
14044 if (*slot)
14045 return *slot;
14047 created = ggc_alloc (sizeof (struct dwarf_file_data));
14048 created->filename = file_name;
14049 created->emitted_number = 0;
14050 *slot = created;
14051 return created;
14054 /* If the assembler will construct the file table, then translate the compiler
14055 internal file table number into the assembler file table number, and emit
14056 a .file directive if we haven't already emitted one yet. The file table
14057 numbers are different because we prune debug info for unused variables and
14058 types, which may include filenames. */
14060 static int
14061 maybe_emit_file (struct dwarf_file_data * fd)
14063 if (! fd->emitted_number)
14065 if (last_emitted_file)
14066 fd->emitted_number = last_emitted_file->emitted_number + 1;
14067 else
14068 fd->emitted_number = 1;
14069 last_emitted_file = fd;
14071 if (DWARF2_ASM_LINE_DEBUG_INFO)
14073 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
14074 output_quoted_string (asm_out_file,
14075 remap_debug_filename (fd->filename));
14076 fputc ('\n', asm_out_file);
14080 return fd->emitted_number;
14083 /* Called by the final INSN scan whenever we see a var location. We
14084 use it to drop labels in the right places, and throw the location in
14085 our lookup table. */
14087 static void
14088 dwarf2out_var_location (rtx loc_note)
14090 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
14091 struct var_loc_node *newloc;
14092 rtx prev_insn;
14093 static rtx last_insn;
14094 static const char *last_label;
14095 tree decl;
14097 if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
14098 return;
14099 prev_insn = PREV_INSN (loc_note);
14101 newloc = ggc_alloc_cleared (sizeof (struct var_loc_node));
14102 /* If the insn we processed last time is the previous insn
14103 and it is also a var location note, use the label we emitted
14104 last time. */
14105 if (last_insn != NULL_RTX
14106 && last_insn == prev_insn
14107 && NOTE_P (prev_insn)
14108 && NOTE_KIND (prev_insn) == NOTE_INSN_VAR_LOCATION)
14110 newloc->label = last_label;
14112 else
14114 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
14115 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
14116 loclabel_num++;
14117 newloc->label = ggc_strdup (loclabel);
14119 newloc->var_loc_note = loc_note;
14120 newloc->next = NULL;
14122 if (cfun && in_cold_section_p)
14123 newloc->section_label = cfun->cold_section_label;
14124 else
14125 newloc->section_label = text_section_label;
14127 last_insn = loc_note;
14128 last_label = newloc->label;
14129 decl = NOTE_VAR_LOCATION_DECL (loc_note);
14130 add_var_loc_to_decl (decl, newloc);
14133 /* We need to reset the locations at the beginning of each
14134 function. We can't do this in the end_function hook, because the
14135 declarations that use the locations won't have been output when
14136 that hook is called. Also compute have_multiple_function_sections here. */
14138 static void
14139 dwarf2out_begin_function (tree fun)
14141 htab_empty (decl_loc_table);
14143 if (function_section (fun) != text_section)
14144 have_multiple_function_sections = true;
14146 dwarf2out_note_section_used ();
14149 /* Output a label to mark the beginning of a source code line entry
14150 and record information relating to this source line, in
14151 'line_info_table' for later output of the .debug_line section. */
14153 static void
14154 dwarf2out_source_line (unsigned int line, const char *filename)
14156 if (debug_info_level >= DINFO_LEVEL_NORMAL
14157 && line != 0)
14159 int file_num = maybe_emit_file (lookup_filename (filename));
14161 switch_to_section (current_function_section ());
14163 /* If requested, emit something human-readable. */
14164 if (flag_debug_asm)
14165 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
14166 filename, line);
14168 if (DWARF2_ASM_LINE_DEBUG_INFO)
14170 /* Emit the .loc directive understood by GNU as. */
14171 fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
14173 /* Indicate that line number info exists. */
14174 line_info_table_in_use++;
14176 else if (function_section (current_function_decl) != text_section)
14178 dw_separate_line_info_ref line_info;
14179 targetm.asm_out.internal_label (asm_out_file,
14180 SEPARATE_LINE_CODE_LABEL,
14181 separate_line_info_table_in_use);
14183 /* Expand the line info table if necessary. */
14184 if (separate_line_info_table_in_use
14185 == separate_line_info_table_allocated)
14187 separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
14188 separate_line_info_table
14189 = ggc_realloc (separate_line_info_table,
14190 separate_line_info_table_allocated
14191 * sizeof (dw_separate_line_info_entry));
14192 memset (separate_line_info_table
14193 + separate_line_info_table_in_use,
14195 (LINE_INFO_TABLE_INCREMENT
14196 * sizeof (dw_separate_line_info_entry)));
14199 /* Add the new entry at the end of the line_info_table. */
14200 line_info
14201 = &separate_line_info_table[separate_line_info_table_in_use++];
14202 line_info->dw_file_num = file_num;
14203 line_info->dw_line_num = line;
14204 line_info->function = current_function_funcdef_no;
14206 else
14208 dw_line_info_ref line_info;
14210 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
14211 line_info_table_in_use);
14213 /* Expand the line info table if necessary. */
14214 if (line_info_table_in_use == line_info_table_allocated)
14216 line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
14217 line_info_table
14218 = ggc_realloc (line_info_table,
14219 (line_info_table_allocated
14220 * sizeof (dw_line_info_entry)));
14221 memset (line_info_table + line_info_table_in_use, 0,
14222 LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
14225 /* Add the new entry at the end of the line_info_table. */
14226 line_info = &line_info_table[line_info_table_in_use++];
14227 line_info->dw_file_num = file_num;
14228 line_info->dw_line_num = line;
14233 /* Record the beginning of a new source file. */
14235 static void
14236 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
14238 if (flag_eliminate_dwarf2_dups)
14240 /* Record the beginning of the file for break_out_includes. */
14241 dw_die_ref bincl_die;
14243 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
14244 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
14247 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14249 int file_num = maybe_emit_file (lookup_filename (filename));
14251 switch_to_section (debug_macinfo_section);
14252 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
14253 dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
14254 lineno);
14256 dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
14260 /* Record the end of a source file. */
14262 static void
14263 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
14265 if (flag_eliminate_dwarf2_dups)
14266 /* Record the end of the file for break_out_includes. */
14267 new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
14269 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14271 switch_to_section (debug_macinfo_section);
14272 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
14276 /* Called from debug_define in toplev.c. The `buffer' parameter contains
14277 the tail part of the directive line, i.e. the part which is past the
14278 initial whitespace, #, whitespace, directive-name, whitespace part. */
14280 static void
14281 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
14282 const char *buffer ATTRIBUTE_UNUSED)
14284 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14286 switch_to_section (debug_macinfo_section);
14287 dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
14288 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
14289 dw2_asm_output_nstring (buffer, -1, "The macro");
14293 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
14294 the tail part of the directive line, i.e. the part which is past the
14295 initial whitespace, #, whitespace, directive-name, whitespace part. */
14297 static void
14298 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
14299 const char *buffer ATTRIBUTE_UNUSED)
14301 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14303 switch_to_section (debug_macinfo_section);
14304 dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
14305 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
14306 dw2_asm_output_nstring (buffer, -1, "The macro");
14310 /* Set up for Dwarf output at the start of compilation. */
14312 static void
14313 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
14315 /* Allocate the file_table. */
14316 file_table = htab_create_ggc (50, file_table_hash,
14317 file_table_eq, NULL);
14319 /* Allocate the decl_die_table. */
14320 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
14321 decl_die_table_eq, NULL);
14323 /* Allocate the decl_loc_table. */
14324 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
14325 decl_loc_table_eq, NULL);
14327 /* Allocate the initial hunk of the decl_scope_table. */
14328 decl_scope_table = VEC_alloc (tree, gc, 256);
14330 /* Allocate the initial hunk of the abbrev_die_table. */
14331 abbrev_die_table = ggc_alloc_cleared (ABBREV_DIE_TABLE_INCREMENT
14332 * sizeof (dw_die_ref));
14333 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
14334 /* Zero-th entry is allocated, but unused. */
14335 abbrev_die_table_in_use = 1;
14337 /* Allocate the initial hunk of the line_info_table. */
14338 line_info_table = ggc_alloc_cleared (LINE_INFO_TABLE_INCREMENT
14339 * sizeof (dw_line_info_entry));
14340 line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
14342 /* Zero-th entry is allocated, but unused. */
14343 line_info_table_in_use = 1;
14345 /* Allocate the pubtypes and pubnames vectors. */
14346 pubname_table = VEC_alloc (pubname_entry, gc, 32);
14347 pubtype_table = VEC_alloc (pubname_entry, gc, 32);
14349 /* Generate the initial DIE for the .debug section. Note that the (string)
14350 value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
14351 will (typically) be a relative pathname and that this pathname should be
14352 taken as being relative to the directory from which the compiler was
14353 invoked when the given (base) source file was compiled. We will fill
14354 in this value in dwarf2out_finish. */
14355 comp_unit_die = gen_compile_unit_die (NULL);
14357 incomplete_types = VEC_alloc (tree, gc, 64);
14359 used_rtx_array = VEC_alloc (rtx, gc, 32);
14361 debug_info_section = get_section (DEBUG_INFO_SECTION,
14362 SECTION_DEBUG, NULL);
14363 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
14364 SECTION_DEBUG, NULL);
14365 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
14366 SECTION_DEBUG, NULL);
14367 debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
14368 SECTION_DEBUG, NULL);
14369 debug_line_section = get_section (DEBUG_LINE_SECTION,
14370 SECTION_DEBUG, NULL);
14371 debug_loc_section = get_section (DEBUG_LOC_SECTION,
14372 SECTION_DEBUG, NULL);
14373 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
14374 SECTION_DEBUG, NULL);
14375 #ifdef DEBUG_PUBTYPES_SECTION
14376 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
14377 SECTION_DEBUG, NULL);
14378 #endif
14379 debug_str_section = get_section (DEBUG_STR_SECTION,
14380 DEBUG_STR_SECTION_FLAGS, NULL);
14381 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
14382 SECTION_DEBUG, NULL);
14383 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
14384 SECTION_DEBUG, NULL);
14386 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
14387 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
14388 DEBUG_ABBREV_SECTION_LABEL, 0);
14389 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
14390 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
14391 COLD_TEXT_SECTION_LABEL, 0);
14392 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
14394 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
14395 DEBUG_INFO_SECTION_LABEL, 0);
14396 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
14397 DEBUG_LINE_SECTION_LABEL, 0);
14398 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
14399 DEBUG_RANGES_SECTION_LABEL, 0);
14400 switch_to_section (debug_abbrev_section);
14401 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
14402 switch_to_section (debug_info_section);
14403 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
14404 switch_to_section (debug_line_section);
14405 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
14407 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14409 switch_to_section (debug_macinfo_section);
14410 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
14411 DEBUG_MACINFO_SECTION_LABEL, 0);
14412 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
14415 switch_to_section (text_section);
14416 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
14417 if (flag_reorder_blocks_and_partition)
14419 cold_text_section = unlikely_text_section ();
14420 switch_to_section (cold_text_section);
14421 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
14425 /* A helper function for dwarf2out_finish called through
14426 ht_forall. Emit one queued .debug_str string. */
14428 static int
14429 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
14431 struct indirect_string_node *node = (struct indirect_string_node *) *h;
14433 if (node->form == DW_FORM_strp)
14435 switch_to_section (debug_str_section);
14436 ASM_OUTPUT_LABEL (asm_out_file, node->label);
14437 assemble_string (node->str, strlen (node->str) + 1);
14440 return 1;
14443 #if ENABLE_ASSERT_CHECKING
14444 /* Verify that all marks are clear. */
14446 static void
14447 verify_marks_clear (dw_die_ref die)
14449 dw_die_ref c;
14451 gcc_assert (! die->die_mark);
14452 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
14454 #endif /* ENABLE_ASSERT_CHECKING */
14456 /* Clear the marks for a die and its children.
14457 Be cool if the mark isn't set. */
14459 static void
14460 prune_unmark_dies (dw_die_ref die)
14462 dw_die_ref c;
14464 if (die->die_mark)
14465 die->die_mark = 0;
14466 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
14469 /* Given DIE that we're marking as used, find any other dies
14470 it references as attributes and mark them as used. */
14472 static void
14473 prune_unused_types_walk_attribs (dw_die_ref die)
14475 dw_attr_ref a;
14476 unsigned ix;
14478 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
14480 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
14482 /* A reference to another DIE.
14483 Make sure that it will get emitted. */
14484 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
14486 /* Set the string's refcount to 0 so that prune_unused_types_mark
14487 accounts properly for it. */
14488 if (AT_class (a) == dw_val_class_str)
14489 a->dw_attr_val.v.val_str->refcount = 0;
14494 /* Mark DIE as being used. If DOKIDS is true, then walk down
14495 to DIE's children. */
14497 static void
14498 prune_unused_types_mark (dw_die_ref die, int dokids)
14500 dw_die_ref c;
14502 if (die->die_mark == 0)
14504 /* We haven't done this node yet. Mark it as used. */
14505 die->die_mark = 1;
14507 /* We also have to mark its parents as used.
14508 (But we don't want to mark our parents' kids due to this.) */
14509 if (die->die_parent)
14510 prune_unused_types_mark (die->die_parent, 0);
14512 /* Mark any referenced nodes. */
14513 prune_unused_types_walk_attribs (die);
14515 /* If this node is a specification,
14516 also mark the definition, if it exists. */
14517 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
14518 prune_unused_types_mark (die->die_definition, 1);
14521 if (dokids && die->die_mark != 2)
14523 /* We need to walk the children, but haven't done so yet.
14524 Remember that we've walked the kids. */
14525 die->die_mark = 2;
14527 /* If this is an array type, we need to make sure our
14528 kids get marked, even if they're types. */
14529 if (die->die_tag == DW_TAG_array_type)
14530 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
14531 else
14532 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
14537 /* Walk the tree DIE and mark types that we actually use. */
14539 static void
14540 prune_unused_types_walk (dw_die_ref die)
14542 dw_die_ref c;
14544 /* Don't do anything if this node is already marked. */
14545 if (die->die_mark)
14546 return;
14548 switch (die->die_tag)
14550 case DW_TAG_const_type:
14551 case DW_TAG_packed_type:
14552 case DW_TAG_pointer_type:
14553 case DW_TAG_reference_type:
14554 case DW_TAG_volatile_type:
14555 case DW_TAG_typedef:
14556 case DW_TAG_array_type:
14557 case DW_TAG_structure_type:
14558 case DW_TAG_union_type:
14559 case DW_TAG_class_type:
14560 case DW_TAG_friend:
14561 case DW_TAG_variant_part:
14562 case DW_TAG_enumeration_type:
14563 case DW_TAG_subroutine_type:
14564 case DW_TAG_string_type:
14565 case DW_TAG_set_type:
14566 case DW_TAG_subrange_type:
14567 case DW_TAG_ptr_to_member_type:
14568 case DW_TAG_file_type:
14569 if (die->die_perennial_p)
14570 break;
14572 /* It's a type node --- don't mark it. */
14573 return;
14575 default:
14576 /* Mark everything else. */
14577 break;
14580 die->die_mark = 1;
14582 /* Now, mark any dies referenced from here. */
14583 prune_unused_types_walk_attribs (die);
14585 /* Mark children. */
14586 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
14589 /* Increment the string counts on strings referred to from DIE's
14590 attributes. */
14592 static void
14593 prune_unused_types_update_strings (dw_die_ref die)
14595 dw_attr_ref a;
14596 unsigned ix;
14598 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
14599 if (AT_class (a) == dw_val_class_str)
14601 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
14602 s->refcount++;
14603 /* Avoid unnecessarily putting strings that are used less than
14604 twice in the hash table. */
14605 if (s->refcount
14606 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
14608 void ** slot;
14609 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
14610 htab_hash_string (s->str),
14611 INSERT);
14612 gcc_assert (*slot == NULL);
14613 *slot = s;
14618 /* Remove from the tree DIE any dies that aren't marked. */
14620 static void
14621 prune_unused_types_prune (dw_die_ref die)
14623 dw_die_ref c;
14625 gcc_assert (die->die_mark);
14626 prune_unused_types_update_strings (die);
14628 if (! die->die_child)
14629 return;
14631 c = die->die_child;
14632 do {
14633 dw_die_ref prev = c;
14634 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
14635 if (c == die->die_child)
14637 /* No marked children between 'prev' and the end of the list. */
14638 if (prev == c)
14639 /* No marked children at all. */
14640 die->die_child = NULL;
14641 else
14643 prev->die_sib = c->die_sib;
14644 die->die_child = prev;
14646 return;
14649 if (c != prev->die_sib)
14650 prev->die_sib = c;
14651 prune_unused_types_prune (c);
14652 } while (c != die->die_child);
14656 /* Remove dies representing declarations that we never use. */
14658 static void
14659 prune_unused_types (void)
14661 unsigned int i;
14662 limbo_die_node *node;
14663 pubname_ref pub;
14665 #if ENABLE_ASSERT_CHECKING
14666 /* All the marks should already be clear. */
14667 verify_marks_clear (comp_unit_die);
14668 for (node = limbo_die_list; node; node = node->next)
14669 verify_marks_clear (node->die);
14670 #endif /* ENABLE_ASSERT_CHECKING */
14672 /* Set the mark on nodes that are actually used. */
14673 prune_unused_types_walk (comp_unit_die);
14674 for (node = limbo_die_list; node; node = node->next)
14675 prune_unused_types_walk (node->die);
14677 /* Also set the mark on nodes referenced from the
14678 pubname_table or arange_table. */
14679 for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
14680 prune_unused_types_mark (pub->die, 1);
14681 for (i = 0; i < arange_table_in_use; i++)
14682 prune_unused_types_mark (arange_table[i], 1);
14684 /* Get rid of nodes that aren't marked; and update the string counts. */
14685 if (debug_str_hash)
14686 htab_empty (debug_str_hash);
14687 prune_unused_types_prune (comp_unit_die);
14688 for (node = limbo_die_list; node; node = node->next)
14689 prune_unused_types_prune (node->die);
14691 /* Leave the marks clear. */
14692 prune_unmark_dies (comp_unit_die);
14693 for (node = limbo_die_list; node; node = node->next)
14694 prune_unmark_dies (node->die);
14697 /* Set the parameter to true if there are any relative pathnames in
14698 the file table. */
14699 static int
14700 file_table_relative_p (void ** slot, void *param)
14702 bool *p = param;
14703 struct dwarf_file_data *d = *slot;
14704 if (!IS_ABSOLUTE_PATH (d->filename))
14706 *p = true;
14707 return 0;
14709 return 1;
14712 /* Output stuff that dwarf requires at the end of every file,
14713 and generate the DWARF-2 debugging info. */
14715 static void
14716 dwarf2out_finish (const char *filename)
14718 limbo_die_node *node, *next_node;
14719 dw_die_ref die = 0;
14721 /* Add the name for the main input file now. We delayed this from
14722 dwarf2out_init to avoid complications with PCH. */
14723 add_name_attribute (comp_unit_die, remap_debug_filename (filename));
14724 if (!IS_ABSOLUTE_PATH (filename))
14725 add_comp_dir_attribute (comp_unit_die);
14726 else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
14728 bool p = false;
14729 htab_traverse (file_table, file_table_relative_p, &p);
14730 if (p)
14731 add_comp_dir_attribute (comp_unit_die);
14734 /* Traverse the limbo die list, and add parent/child links. The only
14735 dies without parents that should be here are concrete instances of
14736 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
14737 For concrete instances, we can get the parent die from the abstract
14738 instance. */
14739 for (node = limbo_die_list; node; node = next_node)
14741 next_node = node->next;
14742 die = node->die;
14744 if (die->die_parent == NULL)
14746 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
14748 if (origin)
14749 add_child_die (origin->die_parent, die);
14750 else if (die == comp_unit_die)
14752 else if (errorcount > 0 || sorrycount > 0)
14753 /* It's OK to be confused by errors in the input. */
14754 add_child_die (comp_unit_die, die);
14755 else
14757 /* In certain situations, the lexical block containing a
14758 nested function can be optimized away, which results
14759 in the nested function die being orphaned. Likewise
14760 with the return type of that nested function. Force
14761 this to be a child of the containing function.
14763 It may happen that even the containing function got fully
14764 inlined and optimized out. In that case we are lost and
14765 assign the empty child. This should not be big issue as
14766 the function is likely unreachable too. */
14767 tree context = NULL_TREE;
14769 gcc_assert (node->created_for);
14771 if (DECL_P (node->created_for))
14772 context = DECL_CONTEXT (node->created_for);
14773 else if (TYPE_P (node->created_for))
14774 context = TYPE_CONTEXT (node->created_for);
14776 gcc_assert (context
14777 && (TREE_CODE (context) == FUNCTION_DECL
14778 || TREE_CODE (context) == NAMESPACE_DECL));
14780 origin = lookup_decl_die (context);
14781 if (origin)
14782 add_child_die (origin, die);
14783 else
14784 add_child_die (comp_unit_die, die);
14789 limbo_die_list = NULL;
14791 /* Walk through the list of incomplete types again, trying once more to
14792 emit full debugging info for them. */
14793 retry_incomplete_types ();
14795 if (flag_eliminate_unused_debug_types)
14796 prune_unused_types ();
14798 /* Generate separate CUs for each of the include files we've seen.
14799 They will go into limbo_die_list. */
14800 if (flag_eliminate_dwarf2_dups)
14801 break_out_includes (comp_unit_die);
14803 /* Traverse the DIE's and add add sibling attributes to those DIE's
14804 that have children. */
14805 add_sibling_attributes (comp_unit_die);
14806 for (node = limbo_die_list; node; node = node->next)
14807 add_sibling_attributes (node->die);
14809 /* Output a terminator label for the .text section. */
14810 switch_to_section (text_section);
14811 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
14812 if (flag_reorder_blocks_and_partition)
14814 switch_to_section (unlikely_text_section ());
14815 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
14818 /* We can only use the low/high_pc attributes if all of the code was
14819 in .text. */
14820 if (!have_multiple_function_sections)
14822 add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
14823 add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
14826 else
14828 unsigned fde_idx = 0;
14830 /* We need to give .debug_loc and .debug_ranges an appropriate
14831 "base address". Use zero so that these addresses become
14832 absolute. Historically, we've emitted the unexpected
14833 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
14834 Emit both to give time for other tools to adapt. */
14835 add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
14836 add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
14838 add_AT_range_list (comp_unit_die, DW_AT_ranges,
14839 add_ranges_by_labels (text_section_label,
14840 text_end_label));
14841 if (flag_reorder_blocks_and_partition)
14842 add_ranges_by_labels (cold_text_section_label,
14843 cold_end_label);
14845 for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
14847 dw_fde_ref fde = &fde_table[fde_idx];
14849 if (fde->dw_fde_switched_sections)
14851 add_ranges_by_labels (fde->dw_fde_hot_section_label,
14852 fde->dw_fde_hot_section_end_label);
14853 add_ranges_by_labels (fde->dw_fde_unlikely_section_label,
14854 fde->dw_fde_unlikely_section_end_label);
14856 else
14857 add_ranges_by_labels (fde->dw_fde_begin,
14858 fde->dw_fde_end);
14861 add_ranges (NULL);
14864 /* Output location list section if necessary. */
14865 if (have_location_lists)
14867 /* Output the location lists info. */
14868 switch_to_section (debug_loc_section);
14869 ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
14870 DEBUG_LOC_SECTION_LABEL, 0);
14871 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
14872 output_location_lists (die);
14875 if (debug_info_level >= DINFO_LEVEL_NORMAL)
14876 add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
14877 debug_line_section_label);
14879 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14880 add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
14882 /* Output all of the compilation units. We put the main one last so that
14883 the offsets are available to output_pubnames. */
14884 for (node = limbo_die_list; node; node = node->next)
14885 output_comp_unit (node->die, 0);
14887 output_comp_unit (comp_unit_die, 0);
14889 /* Output the abbreviation table. */
14890 switch_to_section (debug_abbrev_section);
14891 output_abbrev_section ();
14893 /* Output public names table if necessary. */
14894 if (!VEC_empty (pubname_entry, pubname_table))
14896 switch_to_section (debug_pubnames_section);
14897 output_pubnames (pubname_table);
14900 #ifdef DEBUG_PUBTYPES_SECTION
14901 /* Output public types table if necessary. */
14902 if (!VEC_empty (pubname_entry, pubtype_table))
14904 switch_to_section (debug_pubtypes_section);
14905 output_pubnames (pubtype_table);
14907 #endif
14909 /* Output the address range information. We only put functions in the arange
14910 table, so don't write it out if we don't have any. */
14911 if (fde_table_in_use)
14913 switch_to_section (debug_aranges_section);
14914 output_aranges ();
14917 /* Output ranges section if necessary. */
14918 if (ranges_table_in_use)
14920 switch_to_section (debug_ranges_section);
14921 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
14922 output_ranges ();
14925 /* Output the source line correspondence table. We must do this
14926 even if there is no line information. Otherwise, on an empty
14927 translation unit, we will generate a present, but empty,
14928 .debug_info section. IRIX 6.5 `nm' will then complain when
14929 examining the file. This is done late so that any filenames
14930 used by the debug_info section are marked as 'used'. */
14931 if (! DWARF2_ASM_LINE_DEBUG_INFO)
14933 switch_to_section (debug_line_section);
14934 output_line_info ();
14937 /* Have to end the macro section. */
14938 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14940 switch_to_section (debug_macinfo_section);
14941 dw2_asm_output_data (1, 0, "End compilation unit");
14944 /* If we emitted any DW_FORM_strp form attribute, output the string
14945 table too. */
14946 if (debug_str_hash)
14947 htab_traverse (debug_str_hash, output_indirect_string, NULL);
14949 #else
14951 /* This should never be used, but its address is needed for comparisons. */
14952 const struct gcc_debug_hooks dwarf2_debug_hooks;
14954 #endif /* DWARF2_DEBUGGING_INFO */
14956 #include "gt-dwarf2out.h"