Daily bump.
[official-gcc.git] / gcc / dwarf2out.c
blob2d8736de0fca320f7c4546c26e99e69b83c676b8
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, 2008 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 /* True if the compilation unit places functions in more than one section. */
343 static GTY(()) bool have_multiple_function_sections = false;
345 /* Whether the default text and cold text sections have been used at all. */
347 static GTY(()) bool text_section_used = false;
348 static GTY(()) bool cold_text_section_used = false;
350 /* The default cold text section. */
351 static GTY(()) section *cold_text_section;
353 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
355 /* Forward declarations for functions defined in this file. */
357 static char *stripattributes (const char *);
358 static const char *dwarf_cfi_name (unsigned);
359 static dw_cfi_ref new_cfi (void);
360 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
361 static void add_fde_cfi (const char *, dw_cfi_ref);
362 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *);
363 static void lookup_cfa (dw_cfa_location *);
364 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
365 #ifdef DWARF2_UNWIND_INFO
366 static void initial_return_save (rtx);
367 #endif
368 static HOST_WIDE_INT stack_adjust_offset (const_rtx);
369 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
370 static void output_call_frame_info (int);
371 static void dwarf2out_note_section_used (void);
372 static void dwarf2out_stack_adjust (rtx, bool);
373 static void flush_queued_reg_saves (void);
374 static bool clobbers_queued_reg_save (const_rtx);
375 static void dwarf2out_frame_debug_expr (rtx, const char *);
377 /* Support for complex CFA locations. */
378 static void output_cfa_loc (dw_cfi_ref);
379 static void get_cfa_from_loc_descr (dw_cfa_location *,
380 struct dw_loc_descr_struct *);
381 static struct dw_loc_descr_struct *build_cfa_loc
382 (dw_cfa_location *, HOST_WIDE_INT);
383 static void def_cfa_1 (const char *, dw_cfa_location *);
385 /* How to start an assembler comment. */
386 #ifndef ASM_COMMENT_START
387 #define ASM_COMMENT_START ";#"
388 #endif
390 /* Data and reference forms for relocatable data. */
391 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
392 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
394 #ifndef DEBUG_FRAME_SECTION
395 #define DEBUG_FRAME_SECTION ".debug_frame"
396 #endif
398 #ifndef FUNC_BEGIN_LABEL
399 #define FUNC_BEGIN_LABEL "LFB"
400 #endif
402 #ifndef FUNC_END_LABEL
403 #define FUNC_END_LABEL "LFE"
404 #endif
406 #ifndef FRAME_BEGIN_LABEL
407 #define FRAME_BEGIN_LABEL "Lframe"
408 #endif
409 #define CIE_AFTER_SIZE_LABEL "LSCIE"
410 #define CIE_END_LABEL "LECIE"
411 #define FDE_LABEL "LSFDE"
412 #define FDE_AFTER_SIZE_LABEL "LASFDE"
413 #define FDE_END_LABEL "LEFDE"
414 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
415 #define LINE_NUMBER_END_LABEL "LELT"
416 #define LN_PROLOG_AS_LABEL "LASLTP"
417 #define LN_PROLOG_END_LABEL "LELTP"
418 #define DIE_LABEL_PREFIX "DW"
420 /* The DWARF 2 CFA column which tracks the return address. Normally this
421 is the column for PC, or the first column after all of the hard
422 registers. */
423 #ifndef DWARF_FRAME_RETURN_COLUMN
424 #ifdef PC_REGNUM
425 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGNUM (PC_REGNUM)
426 #else
427 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGISTERS
428 #endif
429 #endif
431 /* The mapping from gcc register number to DWARF 2 CFA column number. By
432 default, we just provide columns for all registers. */
433 #ifndef DWARF_FRAME_REGNUM
434 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
435 #endif
437 /* Hook used by __throw. */
440 expand_builtin_dwarf_sp_column (void)
442 unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
443 return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
446 /* Return a pointer to a copy of the section string name S with all
447 attributes stripped off, and an asterisk prepended (for assemble_name). */
449 static inline char *
450 stripattributes (const char *s)
452 char *stripped = XNEWVEC (char, strlen (s) + 2);
453 char *p = stripped;
455 *p++ = '*';
457 while (*s && *s != ',')
458 *p++ = *s++;
460 *p = '\0';
461 return stripped;
464 /* MEM is a memory reference for the register size table, each element of
465 which has mode MODE. Initialize column C as a return address column. */
467 static void
468 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
470 HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
471 HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
472 emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
475 /* Generate code to initialize the register size table. */
477 void
478 expand_builtin_init_dwarf_reg_sizes (tree address)
480 unsigned int i;
481 enum machine_mode mode = TYPE_MODE (char_type_node);
482 rtx addr = expand_normal (address);
483 rtx mem = gen_rtx_MEM (BLKmode, addr);
484 bool wrote_return_column = false;
486 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
488 int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
490 if (rnum < DWARF_FRAME_REGISTERS)
492 HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
493 enum machine_mode save_mode = reg_raw_mode[i];
494 HOST_WIDE_INT size;
496 if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
497 save_mode = choose_hard_reg_mode (i, 1, true);
498 if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
500 if (save_mode == VOIDmode)
501 continue;
502 wrote_return_column = true;
504 size = GET_MODE_SIZE (save_mode);
505 if (offset < 0)
506 continue;
508 emit_move_insn (adjust_address (mem, mode, offset),
509 gen_int_mode (size, mode));
513 if (!wrote_return_column)
514 init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
516 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
517 init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
518 #endif
520 targetm.init_dwarf_reg_sizes_extra (address);
523 /* Convert a DWARF call frame info. operation to its string name */
525 static const char *
526 dwarf_cfi_name (unsigned int cfi_opc)
528 switch (cfi_opc)
530 case DW_CFA_advance_loc:
531 return "DW_CFA_advance_loc";
532 case DW_CFA_offset:
533 return "DW_CFA_offset";
534 case DW_CFA_restore:
535 return "DW_CFA_restore";
536 case DW_CFA_nop:
537 return "DW_CFA_nop";
538 case DW_CFA_set_loc:
539 return "DW_CFA_set_loc";
540 case DW_CFA_advance_loc1:
541 return "DW_CFA_advance_loc1";
542 case DW_CFA_advance_loc2:
543 return "DW_CFA_advance_loc2";
544 case DW_CFA_advance_loc4:
545 return "DW_CFA_advance_loc4";
546 case DW_CFA_offset_extended:
547 return "DW_CFA_offset_extended";
548 case DW_CFA_restore_extended:
549 return "DW_CFA_restore_extended";
550 case DW_CFA_undefined:
551 return "DW_CFA_undefined";
552 case DW_CFA_same_value:
553 return "DW_CFA_same_value";
554 case DW_CFA_register:
555 return "DW_CFA_register";
556 case DW_CFA_remember_state:
557 return "DW_CFA_remember_state";
558 case DW_CFA_restore_state:
559 return "DW_CFA_restore_state";
560 case DW_CFA_def_cfa:
561 return "DW_CFA_def_cfa";
562 case DW_CFA_def_cfa_register:
563 return "DW_CFA_def_cfa_register";
564 case DW_CFA_def_cfa_offset:
565 return "DW_CFA_def_cfa_offset";
567 /* DWARF 3 */
568 case DW_CFA_def_cfa_expression:
569 return "DW_CFA_def_cfa_expression";
570 case DW_CFA_expression:
571 return "DW_CFA_expression";
572 case DW_CFA_offset_extended_sf:
573 return "DW_CFA_offset_extended_sf";
574 case DW_CFA_def_cfa_sf:
575 return "DW_CFA_def_cfa_sf";
576 case DW_CFA_def_cfa_offset_sf:
577 return "DW_CFA_def_cfa_offset_sf";
579 /* SGI/MIPS specific */
580 case DW_CFA_MIPS_advance_loc8:
581 return "DW_CFA_MIPS_advance_loc8";
583 /* GNU extensions */
584 case DW_CFA_GNU_window_save:
585 return "DW_CFA_GNU_window_save";
586 case DW_CFA_GNU_args_size:
587 return "DW_CFA_GNU_args_size";
588 case DW_CFA_GNU_negative_offset_extended:
589 return "DW_CFA_GNU_negative_offset_extended";
591 default:
592 return "DW_CFA_<unknown>";
596 /* Return a pointer to a newly allocated Call Frame Instruction. */
598 static inline dw_cfi_ref
599 new_cfi (void)
601 dw_cfi_ref cfi = ggc_alloc (sizeof (dw_cfi_node));
603 cfi->dw_cfi_next = NULL;
604 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
605 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
607 return cfi;
610 /* Add a Call Frame Instruction to list of instructions. */
612 static inline void
613 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
615 dw_cfi_ref *p;
617 /* Find the end of the chain. */
618 for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
621 *p = cfi;
624 /* Generate a new label for the CFI info to refer to. */
626 char *
627 dwarf2out_cfi_label (void)
629 static char label[20];
631 ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
632 ASM_OUTPUT_LABEL (asm_out_file, label);
633 return label;
636 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
637 or to the CIE if LABEL is NULL. */
639 static void
640 add_fde_cfi (const char *label, dw_cfi_ref cfi)
642 if (label)
644 dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
646 if (*label == 0)
647 label = dwarf2out_cfi_label ();
649 if (fde->dw_fde_current_label == NULL
650 || strcmp (label, fde->dw_fde_current_label) != 0)
652 dw_cfi_ref xcfi;
654 label = xstrdup (label);
656 /* Set the location counter to the new label. */
657 xcfi = new_cfi ();
658 /* If we have a current label, advance from there, otherwise
659 set the location directly using set_loc. */
660 xcfi->dw_cfi_opc = fde->dw_fde_current_label
661 ? DW_CFA_advance_loc4
662 : DW_CFA_set_loc;
663 xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
664 add_cfi (&fde->dw_fde_cfi, xcfi);
666 fde->dw_fde_current_label = label;
669 add_cfi (&fde->dw_fde_cfi, cfi);
672 else
673 add_cfi (&cie_cfi_head, cfi);
676 /* Subroutine of lookup_cfa. */
678 static void
679 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc)
681 switch (cfi->dw_cfi_opc)
683 case DW_CFA_def_cfa_offset:
684 loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
685 break;
686 case DW_CFA_def_cfa_offset_sf:
687 loc->offset
688 = cfi->dw_cfi_oprnd1.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
689 break;
690 case DW_CFA_def_cfa_register:
691 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
692 break;
693 case DW_CFA_def_cfa:
694 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
695 loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
696 break;
697 case DW_CFA_def_cfa_sf:
698 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
699 loc->offset
700 = cfi->dw_cfi_oprnd2.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
701 break;
702 case DW_CFA_def_cfa_expression:
703 get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
704 break;
705 default:
706 break;
710 /* Find the previous value for the CFA. */
712 static void
713 lookup_cfa (dw_cfa_location *loc)
715 dw_cfi_ref cfi;
717 loc->reg = INVALID_REGNUM;
718 loc->offset = 0;
719 loc->indirect = 0;
720 loc->base_offset = 0;
722 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
723 lookup_cfa_1 (cfi, loc);
725 if (fde_table_in_use)
727 dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
728 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
729 lookup_cfa_1 (cfi, loc);
733 /* The current rule for calculating the DWARF2 canonical frame address. */
734 static dw_cfa_location cfa;
736 /* The register used for saving registers to the stack, and its offset
737 from the CFA. */
738 static dw_cfa_location cfa_store;
740 /* The running total of the size of arguments pushed onto the stack. */
741 static HOST_WIDE_INT args_size;
743 /* The last args_size we actually output. */
744 static HOST_WIDE_INT old_args_size;
746 /* Entry point to update the canonical frame address (CFA).
747 LABEL is passed to add_fde_cfi. The value of CFA is now to be
748 calculated from REG+OFFSET. */
750 void
751 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
753 dw_cfa_location loc;
754 loc.indirect = 0;
755 loc.base_offset = 0;
756 loc.reg = reg;
757 loc.offset = offset;
758 def_cfa_1 (label, &loc);
761 /* Determine if two dw_cfa_location structures define the same data. */
763 static bool
764 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
766 return (loc1->reg == loc2->reg
767 && loc1->offset == loc2->offset
768 && loc1->indirect == loc2->indirect
769 && (loc1->indirect == 0
770 || loc1->base_offset == loc2->base_offset));
773 /* This routine does the actual work. The CFA is now calculated from
774 the dw_cfa_location structure. */
776 static void
777 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
779 dw_cfi_ref cfi;
780 dw_cfa_location old_cfa, loc;
782 cfa = *loc_p;
783 loc = *loc_p;
785 if (cfa_store.reg == loc.reg && loc.indirect == 0)
786 cfa_store.offset = loc.offset;
788 loc.reg = DWARF_FRAME_REGNUM (loc.reg);
789 lookup_cfa (&old_cfa);
791 /* If nothing changed, no need to issue any call frame instructions. */
792 if (cfa_equal_p (&loc, &old_cfa))
793 return;
795 cfi = new_cfi ();
797 if (loc.reg == old_cfa.reg && !loc.indirect)
799 /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
800 the CFA register did not change but the offset did. */
801 if (loc.offset < 0)
803 HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
804 gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
806 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
807 cfi->dw_cfi_oprnd1.dw_cfi_offset = f_offset;
809 else
811 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
812 cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
816 #ifndef MIPS_DEBUGGING_INFO /* SGI dbx thinks this means no offset. */
817 else if (loc.offset == old_cfa.offset
818 && old_cfa.reg != INVALID_REGNUM
819 && !loc.indirect)
821 /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
822 indicating the CFA register has changed to <register> but the
823 offset has not changed. */
824 cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
825 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
827 #endif
829 else if (loc.indirect == 0)
831 /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
832 indicating the CFA register has changed to <register> with
833 the specified offset. */
834 if (loc.offset < 0)
836 HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
837 gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
839 cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
840 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
841 cfi->dw_cfi_oprnd2.dw_cfi_offset = f_offset;
843 else
845 cfi->dw_cfi_opc = DW_CFA_def_cfa;
846 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
847 cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
850 else
852 /* Construct a DW_CFA_def_cfa_expression instruction to
853 calculate the CFA using a full location expression since no
854 register-offset pair is available. */
855 struct dw_loc_descr_struct *loc_list;
857 cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
858 loc_list = build_cfa_loc (&loc, 0);
859 cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
862 add_fde_cfi (label, cfi);
865 /* Add the CFI for saving a register. REG is the CFA column number.
866 LABEL is passed to add_fde_cfi.
867 If SREG is -1, the register is saved at OFFSET from the CFA;
868 otherwise it is saved in SREG. */
870 static void
871 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
873 dw_cfi_ref cfi = new_cfi ();
875 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
877 if (sreg == INVALID_REGNUM)
879 if (reg & ~0x3f)
880 /* The register number won't fit in 6 bits, so we have to use
881 the long form. */
882 cfi->dw_cfi_opc = DW_CFA_offset_extended;
883 else
884 cfi->dw_cfi_opc = DW_CFA_offset;
886 #ifdef ENABLE_CHECKING
888 /* If we get an offset that is not a multiple of
889 DWARF_CIE_DATA_ALIGNMENT, there is either a bug in the
890 definition of DWARF_CIE_DATA_ALIGNMENT, or a bug in the machine
891 description. */
892 HOST_WIDE_INT check_offset = offset / DWARF_CIE_DATA_ALIGNMENT;
894 gcc_assert (check_offset * DWARF_CIE_DATA_ALIGNMENT == offset);
896 #endif
897 offset /= DWARF_CIE_DATA_ALIGNMENT;
898 if (offset < 0)
899 cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
901 cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
903 else if (sreg == reg)
904 cfi->dw_cfi_opc = DW_CFA_same_value;
905 else
907 cfi->dw_cfi_opc = DW_CFA_register;
908 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
911 add_fde_cfi (label, cfi);
914 /* Add the CFI for saving a register window. LABEL is passed to reg_save.
915 This CFI tells the unwinder that it needs to restore the window registers
916 from the previous frame's window save area.
918 ??? Perhaps we should note in the CIE where windows are saved (instead of
919 assuming 0(cfa)) and what registers are in the window. */
921 void
922 dwarf2out_window_save (const char *label)
924 dw_cfi_ref cfi = new_cfi ();
926 cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
927 add_fde_cfi (label, cfi);
930 /* Add a CFI to update the running total of the size of arguments
931 pushed onto the stack. */
933 void
934 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
936 dw_cfi_ref cfi;
938 if (size == old_args_size)
939 return;
941 old_args_size = size;
943 cfi = new_cfi ();
944 cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
945 cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
946 add_fde_cfi (label, cfi);
949 /* Entry point for saving a register to the stack. REG is the GCC register
950 number. LABEL and OFFSET are passed to reg_save. */
952 void
953 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
955 reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
958 /* Entry point for saving the return address in the stack.
959 LABEL and OFFSET are passed to reg_save. */
961 void
962 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
964 reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
967 /* Entry point for saving the return address in a register.
968 LABEL and SREG are passed to reg_save. */
970 void
971 dwarf2out_return_reg (const char *label, unsigned int sreg)
973 reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
976 #ifdef DWARF2_UNWIND_INFO
977 /* Record the initial position of the return address. RTL is
978 INCOMING_RETURN_ADDR_RTX. */
980 static void
981 initial_return_save (rtx rtl)
983 unsigned int reg = INVALID_REGNUM;
984 HOST_WIDE_INT offset = 0;
986 switch (GET_CODE (rtl))
988 case REG:
989 /* RA is in a register. */
990 reg = DWARF_FRAME_REGNUM (REGNO (rtl));
991 break;
993 case MEM:
994 /* RA is on the stack. */
995 rtl = XEXP (rtl, 0);
996 switch (GET_CODE (rtl))
998 case REG:
999 gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1000 offset = 0;
1001 break;
1003 case PLUS:
1004 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1005 offset = INTVAL (XEXP (rtl, 1));
1006 break;
1008 case MINUS:
1009 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1010 offset = -INTVAL (XEXP (rtl, 1));
1011 break;
1013 default:
1014 gcc_unreachable ();
1017 break;
1019 case PLUS:
1020 /* The return address is at some offset from any value we can
1021 actually load. For instance, on the SPARC it is in %i7+8. Just
1022 ignore the offset for now; it doesn't matter for unwinding frames. */
1023 gcc_assert (GET_CODE (XEXP (rtl, 1)) == CONST_INT);
1024 initial_return_save (XEXP (rtl, 0));
1025 return;
1027 default:
1028 gcc_unreachable ();
1031 if (reg != DWARF_FRAME_RETURN_COLUMN)
1032 reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1034 #endif
1036 /* Given a SET, calculate the amount of stack adjustment it
1037 contains. */
1039 static HOST_WIDE_INT
1040 stack_adjust_offset (const_rtx pattern)
1042 const_rtx src = SET_SRC (pattern);
1043 const_rtx dest = SET_DEST (pattern);
1044 HOST_WIDE_INT offset = 0;
1045 enum rtx_code code;
1047 if (dest == stack_pointer_rtx)
1049 /* (set (reg sp) (plus (reg sp) (const_int))) */
1050 code = GET_CODE (src);
1051 if (! (code == PLUS || code == MINUS)
1052 || XEXP (src, 0) != stack_pointer_rtx
1053 || GET_CODE (XEXP (src, 1)) != CONST_INT)
1054 return 0;
1056 offset = INTVAL (XEXP (src, 1));
1057 if (code == PLUS)
1058 offset = -offset;
1060 else if (MEM_P (dest))
1062 /* (set (mem (pre_dec (reg sp))) (foo)) */
1063 src = XEXP (dest, 0);
1064 code = GET_CODE (src);
1066 switch (code)
1068 case PRE_MODIFY:
1069 case POST_MODIFY:
1070 if (XEXP (src, 0) == stack_pointer_rtx)
1072 rtx val = XEXP (XEXP (src, 1), 1);
1073 /* We handle only adjustments by constant amount. */
1074 gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1075 && GET_CODE (val) == CONST_INT);
1076 offset = -INTVAL (val);
1077 break;
1079 return 0;
1081 case PRE_DEC:
1082 case POST_DEC:
1083 if (XEXP (src, 0) == stack_pointer_rtx)
1085 offset = GET_MODE_SIZE (GET_MODE (dest));
1086 break;
1088 return 0;
1090 case PRE_INC:
1091 case POST_INC:
1092 if (XEXP (src, 0) == stack_pointer_rtx)
1094 offset = -GET_MODE_SIZE (GET_MODE (dest));
1095 break;
1097 return 0;
1099 default:
1100 return 0;
1103 else
1104 return 0;
1106 return offset;
1109 /* Check INSN to see if it looks like a push or a stack adjustment, and
1110 make a note of it if it does. EH uses this information to find out how
1111 much extra space it needs to pop off the stack. */
1113 static void
1114 dwarf2out_stack_adjust (rtx insn, bool after_p)
1116 HOST_WIDE_INT offset;
1117 const char *label;
1118 int i;
1120 /* Don't handle epilogues at all. Certainly it would be wrong to do so
1121 with this function. Proper support would require all frame-related
1122 insns to be marked, and to be able to handle saving state around
1123 epilogues textually in the middle of the function. */
1124 if (prologue_epilogue_contains (insn) || sibcall_epilogue_contains (insn))
1125 return;
1127 /* If only calls can throw, and we have a frame pointer,
1128 save up adjustments until we see the CALL_INSN. */
1129 if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1131 if (CALL_P (insn) && !after_p)
1133 /* Extract the size of the args from the CALL rtx itself. */
1134 insn = PATTERN (insn);
1135 if (GET_CODE (insn) == PARALLEL)
1136 insn = XVECEXP (insn, 0, 0);
1137 if (GET_CODE (insn) == SET)
1138 insn = SET_SRC (insn);
1139 gcc_assert (GET_CODE (insn) == CALL);
1140 dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1142 return;
1145 if (CALL_P (insn) && !after_p)
1147 if (!flag_asynchronous_unwind_tables)
1148 dwarf2out_args_size ("", args_size);
1149 return;
1151 else if (BARRIER_P (insn))
1153 /* When we see a BARRIER, we know to reset args_size to 0. Usually
1154 the compiler will have already emitted a stack adjustment, but
1155 doesn't bother for calls to noreturn functions. */
1156 #ifdef STACK_GROWS_DOWNWARD
1157 offset = -args_size;
1158 #else
1159 offset = args_size;
1160 #endif
1162 else if (GET_CODE (PATTERN (insn)) == SET)
1163 offset = stack_adjust_offset (PATTERN (insn));
1164 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1165 || GET_CODE (PATTERN (insn)) == SEQUENCE)
1167 /* There may be stack adjustments inside compound insns. Search
1168 for them. */
1169 for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1170 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1171 offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i));
1173 else
1174 return;
1176 if (offset == 0)
1177 return;
1179 if (cfa.reg == STACK_POINTER_REGNUM)
1180 cfa.offset += offset;
1182 #ifndef STACK_GROWS_DOWNWARD
1183 offset = -offset;
1184 #endif
1186 args_size += offset;
1187 if (args_size < 0)
1188 args_size = 0;
1190 label = dwarf2out_cfi_label ();
1191 def_cfa_1 (label, &cfa);
1192 if (flag_asynchronous_unwind_tables)
1193 dwarf2out_args_size (label, args_size);
1196 #endif
1198 /* We delay emitting a register save until either (a) we reach the end
1199 of the prologue or (b) the register is clobbered. This clusters
1200 register saves so that there are fewer pc advances. */
1202 struct queued_reg_save GTY(())
1204 struct queued_reg_save *next;
1205 rtx reg;
1206 HOST_WIDE_INT cfa_offset;
1207 rtx saved_reg;
1210 static GTY(()) struct queued_reg_save *queued_reg_saves;
1212 /* The caller's ORIG_REG is saved in SAVED_IN_REG. */
1213 struct reg_saved_in_data GTY(()) {
1214 rtx orig_reg;
1215 rtx saved_in_reg;
1218 /* A list of registers saved in other registers.
1219 The list intentionally has a small maximum capacity of 4; if your
1220 port needs more than that, you might consider implementing a
1221 more efficient data structure. */
1222 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1223 static GTY(()) size_t num_regs_saved_in_regs;
1225 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1226 static const char *last_reg_save_label;
1228 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1229 SREG, or if SREG is NULL then it is saved at OFFSET to the CFA. */
1231 static void
1232 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1234 struct queued_reg_save *q;
1236 /* Duplicates waste space, but it's also necessary to remove them
1237 for correctness, since the queue gets output in reverse
1238 order. */
1239 for (q = queued_reg_saves; q != NULL; q = q->next)
1240 if (REGNO (q->reg) == REGNO (reg))
1241 break;
1243 if (q == NULL)
1245 q = ggc_alloc (sizeof (*q));
1246 q->next = queued_reg_saves;
1247 queued_reg_saves = q;
1250 q->reg = reg;
1251 q->cfa_offset = offset;
1252 q->saved_reg = sreg;
1254 last_reg_save_label = label;
1257 /* Output all the entries in QUEUED_REG_SAVES. */
1259 static void
1260 flush_queued_reg_saves (void)
1262 struct queued_reg_save *q;
1264 for (q = queued_reg_saves; q; q = q->next)
1266 size_t i;
1267 unsigned int reg, sreg;
1269 for (i = 0; i < num_regs_saved_in_regs; i++)
1270 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1271 break;
1272 if (q->saved_reg && i == num_regs_saved_in_regs)
1274 gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1275 num_regs_saved_in_regs++;
1277 if (i != num_regs_saved_in_regs)
1279 regs_saved_in_regs[i].orig_reg = q->reg;
1280 regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1283 reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1284 if (q->saved_reg)
1285 sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1286 else
1287 sreg = INVALID_REGNUM;
1288 reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1291 queued_reg_saves = NULL;
1292 last_reg_save_label = NULL;
1295 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1296 location for? Or, does it clobber a register which we've previously
1297 said that some other register is saved in, and for which we now
1298 have a new location for? */
1300 static bool
1301 clobbers_queued_reg_save (const_rtx insn)
1303 struct queued_reg_save *q;
1305 for (q = queued_reg_saves; q; q = q->next)
1307 size_t i;
1308 if (modified_in_p (q->reg, insn))
1309 return true;
1310 for (i = 0; i < num_regs_saved_in_regs; i++)
1311 if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1312 && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1313 return true;
1316 return false;
1319 /* Entry point for saving the first register into the second. */
1321 void
1322 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1324 size_t i;
1325 unsigned int regno, sregno;
1327 for (i = 0; i < num_regs_saved_in_regs; i++)
1328 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1329 break;
1330 if (i == num_regs_saved_in_regs)
1332 gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1333 num_regs_saved_in_regs++;
1335 regs_saved_in_regs[i].orig_reg = reg;
1336 regs_saved_in_regs[i].saved_in_reg = sreg;
1338 regno = DWARF_FRAME_REGNUM (REGNO (reg));
1339 sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1340 reg_save (label, regno, sregno, 0);
1343 /* What register, if any, is currently saved in REG? */
1345 static rtx
1346 reg_saved_in (rtx reg)
1348 unsigned int regn = REGNO (reg);
1349 size_t i;
1350 struct queued_reg_save *q;
1352 for (q = queued_reg_saves; q; q = q->next)
1353 if (q->saved_reg && regn == REGNO (q->saved_reg))
1354 return q->reg;
1356 for (i = 0; i < num_regs_saved_in_regs; i++)
1357 if (regs_saved_in_regs[i].saved_in_reg
1358 && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1359 return regs_saved_in_regs[i].orig_reg;
1361 return NULL_RTX;
1365 /* A temporary register holding an integral value used in adjusting SP
1366 or setting up the store_reg. The "offset" field holds the integer
1367 value, not an offset. */
1368 static dw_cfa_location cfa_temp;
1370 /* Record call frame debugging information for an expression EXPR,
1371 which either sets SP or FP (adjusting how we calculate the frame
1372 address) or saves a register to the stack or another register.
1373 LABEL indicates the address of EXPR.
1375 This function encodes a state machine mapping rtxes to actions on
1376 cfa, cfa_store, and cfa_temp.reg. We describe these rules so
1377 users need not read the source code.
1379 The High-Level Picture
1381 Changes in the register we use to calculate the CFA: Currently we
1382 assume that if you copy the CFA register into another register, we
1383 should take the other one as the new CFA register; this seems to
1384 work pretty well. If it's wrong for some target, it's simple
1385 enough not to set RTX_FRAME_RELATED_P on the insn in question.
1387 Changes in the register we use for saving registers to the stack:
1388 This is usually SP, but not always. Again, we deduce that if you
1389 copy SP into another register (and SP is not the CFA register),
1390 then the new register is the one we will be using for register
1391 saves. This also seems to work.
1393 Register saves: There's not much guesswork about this one; if
1394 RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1395 register save, and the register used to calculate the destination
1396 had better be the one we think we're using for this purpose.
1397 It's also assumed that a copy from a call-saved register to another
1398 register is saving that register if RTX_FRAME_RELATED_P is set on
1399 that instruction. If the copy is from a call-saved register to
1400 the *same* register, that means that the register is now the same
1401 value as in the caller.
1403 Except: If the register being saved is the CFA register, and the
1404 offset is nonzero, we are saving the CFA, so we assume we have to
1405 use DW_CFA_def_cfa_expression. If the offset is 0, we assume that
1406 the intent is to save the value of SP from the previous frame.
1408 In addition, if a register has previously been saved to a different
1409 register,
1411 Invariants / Summaries of Rules
1413 cfa current rule for calculating the CFA. It usually
1414 consists of a register and an offset.
1415 cfa_store register used by prologue code to save things to the stack
1416 cfa_store.offset is the offset from the value of
1417 cfa_store.reg to the actual CFA
1418 cfa_temp register holding an integral value. cfa_temp.offset
1419 stores the value, which will be used to adjust the
1420 stack pointer. cfa_temp is also used like cfa_store,
1421 to track stores to the stack via fp or a temp reg.
1423 Rules 1- 4: Setting a register's value to cfa.reg or an expression
1424 with cfa.reg as the first operand changes the cfa.reg and its
1425 cfa.offset. Rule 1 and 4 also set cfa_temp.reg and
1426 cfa_temp.offset.
1428 Rules 6- 9: Set a non-cfa.reg register value to a constant or an
1429 expression yielding a constant. This sets cfa_temp.reg
1430 and cfa_temp.offset.
1432 Rule 5: Create a new register cfa_store used to save items to the
1433 stack.
1435 Rules 10-14: Save a register to the stack. Define offset as the
1436 difference of the original location and cfa_store's
1437 location (or cfa_temp's location if cfa_temp is used).
1439 The Rules
1441 "{a,b}" indicates a choice of a xor b.
1442 "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1444 Rule 1:
1445 (set <reg1> <reg2>:cfa.reg)
1446 effects: cfa.reg = <reg1>
1447 cfa.offset unchanged
1448 cfa_temp.reg = <reg1>
1449 cfa_temp.offset = cfa.offset
1451 Rule 2:
1452 (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1453 {<const_int>,<reg>:cfa_temp.reg}))
1454 effects: cfa.reg = sp if fp used
1455 cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
1456 cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1457 if cfa_store.reg==sp
1459 Rule 3:
1460 (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
1461 effects: cfa.reg = fp
1462 cfa_offset += +/- <const_int>
1464 Rule 4:
1465 (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
1466 constraints: <reg1> != fp
1467 <reg1> != sp
1468 effects: cfa.reg = <reg1>
1469 cfa_temp.reg = <reg1>
1470 cfa_temp.offset = cfa.offset
1472 Rule 5:
1473 (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1474 constraints: <reg1> != fp
1475 <reg1> != sp
1476 effects: cfa_store.reg = <reg1>
1477 cfa_store.offset = cfa.offset - cfa_temp.offset
1479 Rule 6:
1480 (set <reg> <const_int>)
1481 effects: cfa_temp.reg = <reg>
1482 cfa_temp.offset = <const_int>
1484 Rule 7:
1485 (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1486 effects: cfa_temp.reg = <reg1>
1487 cfa_temp.offset |= <const_int>
1489 Rule 8:
1490 (set <reg> (high <exp>))
1491 effects: none
1493 Rule 9:
1494 (set <reg> (lo_sum <exp> <const_int>))
1495 effects: cfa_temp.reg = <reg>
1496 cfa_temp.offset = <const_int>
1498 Rule 10:
1499 (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1500 effects: cfa_store.offset -= <const_int>
1501 cfa.offset = cfa_store.offset if cfa.reg == sp
1502 cfa.reg = sp
1503 cfa.base_offset = -cfa_store.offset
1505 Rule 11:
1506 (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1507 effects: cfa_store.offset += -/+ mode_size(mem)
1508 cfa.offset = cfa_store.offset if cfa.reg == sp
1509 cfa.reg = sp
1510 cfa.base_offset = -cfa_store.offset
1512 Rule 12:
1513 (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1515 <reg2>)
1516 effects: cfa.reg = <reg1>
1517 cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
1519 Rule 13:
1520 (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1521 effects: cfa.reg = <reg1>
1522 cfa.base_offset = -{cfa_store,cfa_temp}.offset
1524 Rule 14:
1525 (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
1526 effects: cfa.reg = <reg1>
1527 cfa.base_offset = -cfa_temp.offset
1528 cfa_temp.offset -= mode_size(mem)
1530 Rule 15:
1531 (set <reg> {unspec, unspec_volatile})
1532 effects: target-dependent */
1534 static void
1535 dwarf2out_frame_debug_expr (rtx expr, const char *label)
1537 rtx src, dest, span;
1538 HOST_WIDE_INT offset;
1540 /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1541 the PARALLEL independently. The first element is always processed if
1542 it is a SET. This is for backward compatibility. Other elements
1543 are processed only if they are SETs and the RTX_FRAME_RELATED_P
1544 flag is set in them. */
1545 if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
1547 int par_index;
1548 int limit = XVECLEN (expr, 0);
1549 rtx elem;
1551 /* PARALLELs have strict read-modify-write semantics, so we
1552 ought to evaluate every rvalue before changing any lvalue.
1553 It's cumbersome to do that in general, but there's an
1554 easy approximation that is enough for all current users:
1555 handle register saves before register assignments. */
1556 if (GET_CODE (expr) == PARALLEL)
1557 for (par_index = 0; par_index < limit; par_index++)
1559 elem = XVECEXP (expr, 0, par_index);
1560 if (GET_CODE (elem) == SET
1561 && MEM_P (SET_DEST (elem))
1562 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1563 dwarf2out_frame_debug_expr (elem, label);
1566 for (par_index = 0; par_index < limit; par_index++)
1568 elem = XVECEXP (expr, 0, par_index);
1569 if (GET_CODE (elem) == SET
1570 && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
1571 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1572 dwarf2out_frame_debug_expr (elem, label);
1574 return;
1577 gcc_assert (GET_CODE (expr) == SET);
1579 src = SET_SRC (expr);
1580 dest = SET_DEST (expr);
1582 if (REG_P (src))
1584 rtx rsi = reg_saved_in (src);
1585 if (rsi)
1586 src = rsi;
1589 switch (GET_CODE (dest))
1591 case REG:
1592 switch (GET_CODE (src))
1594 /* Setting FP from SP. */
1595 case REG:
1596 if (cfa.reg == (unsigned) REGNO (src))
1598 /* Rule 1 */
1599 /* Update the CFA rule wrt SP or FP. Make sure src is
1600 relative to the current CFA register.
1602 We used to require that dest be either SP or FP, but the
1603 ARM copies SP to a temporary register, and from there to
1604 FP. So we just rely on the backends to only set
1605 RTX_FRAME_RELATED_P on appropriate insns. */
1606 cfa.reg = REGNO (dest);
1607 cfa_temp.reg = cfa.reg;
1608 cfa_temp.offset = cfa.offset;
1610 else
1612 /* Saving a register in a register. */
1613 gcc_assert (!fixed_regs [REGNO (dest)]
1614 /* For the SPARC and its register window. */
1615 || (DWARF_FRAME_REGNUM (REGNO (src))
1616 == DWARF_FRAME_RETURN_COLUMN));
1617 queue_reg_save (label, src, dest, 0);
1619 break;
1621 case PLUS:
1622 case MINUS:
1623 case LO_SUM:
1624 if (dest == stack_pointer_rtx)
1626 /* Rule 2 */
1627 /* Adjusting SP. */
1628 switch (GET_CODE (XEXP (src, 1)))
1630 case CONST_INT:
1631 offset = INTVAL (XEXP (src, 1));
1632 break;
1633 case REG:
1634 gcc_assert ((unsigned) REGNO (XEXP (src, 1))
1635 == cfa_temp.reg);
1636 offset = cfa_temp.offset;
1637 break;
1638 default:
1639 gcc_unreachable ();
1642 if (XEXP (src, 0) == hard_frame_pointer_rtx)
1644 /* Restoring SP from FP in the epilogue. */
1645 gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
1646 cfa.reg = STACK_POINTER_REGNUM;
1648 else if (GET_CODE (src) == LO_SUM)
1649 /* Assume we've set the source reg of the LO_SUM from sp. */
1651 else
1652 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
1654 if (GET_CODE (src) != MINUS)
1655 offset = -offset;
1656 if (cfa.reg == STACK_POINTER_REGNUM)
1657 cfa.offset += offset;
1658 if (cfa_store.reg == STACK_POINTER_REGNUM)
1659 cfa_store.offset += offset;
1661 else if (dest == hard_frame_pointer_rtx)
1663 /* Rule 3 */
1664 /* Either setting the FP from an offset of the SP,
1665 or adjusting the FP */
1666 gcc_assert (frame_pointer_needed);
1668 gcc_assert (REG_P (XEXP (src, 0))
1669 && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
1670 && GET_CODE (XEXP (src, 1)) == CONST_INT);
1671 offset = INTVAL (XEXP (src, 1));
1672 if (GET_CODE (src) != MINUS)
1673 offset = -offset;
1674 cfa.offset += offset;
1675 cfa.reg = HARD_FRAME_POINTER_REGNUM;
1677 else
1679 gcc_assert (GET_CODE (src) != MINUS);
1681 /* Rule 4 */
1682 if (REG_P (XEXP (src, 0))
1683 && REGNO (XEXP (src, 0)) == cfa.reg
1684 && GET_CODE (XEXP (src, 1)) == CONST_INT)
1686 /* Setting a temporary CFA register that will be copied
1687 into the FP later on. */
1688 offset = - INTVAL (XEXP (src, 1));
1689 cfa.offset += offset;
1690 cfa.reg = REGNO (dest);
1691 /* Or used to save regs to the stack. */
1692 cfa_temp.reg = cfa.reg;
1693 cfa_temp.offset = cfa.offset;
1696 /* Rule 5 */
1697 else if (REG_P (XEXP (src, 0))
1698 && REGNO (XEXP (src, 0)) == cfa_temp.reg
1699 && XEXP (src, 1) == stack_pointer_rtx)
1701 /* Setting a scratch register that we will use instead
1702 of SP for saving registers to the stack. */
1703 gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
1704 cfa_store.reg = REGNO (dest);
1705 cfa_store.offset = cfa.offset - cfa_temp.offset;
1708 /* Rule 9 */
1709 else if (GET_CODE (src) == LO_SUM
1710 && GET_CODE (XEXP (src, 1)) == CONST_INT)
1712 cfa_temp.reg = REGNO (dest);
1713 cfa_temp.offset = INTVAL (XEXP (src, 1));
1715 else
1716 gcc_unreachable ();
1718 break;
1720 /* Rule 6 */
1721 case CONST_INT:
1722 cfa_temp.reg = REGNO (dest);
1723 cfa_temp.offset = INTVAL (src);
1724 break;
1726 /* Rule 7 */
1727 case IOR:
1728 gcc_assert (REG_P (XEXP (src, 0))
1729 && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
1730 && GET_CODE (XEXP (src, 1)) == CONST_INT);
1732 if ((unsigned) REGNO (dest) != cfa_temp.reg)
1733 cfa_temp.reg = REGNO (dest);
1734 cfa_temp.offset |= INTVAL (XEXP (src, 1));
1735 break;
1737 /* Skip over HIGH, assuming it will be followed by a LO_SUM,
1738 which will fill in all of the bits. */
1739 /* Rule 8 */
1740 case HIGH:
1741 break;
1743 /* Rule 15 */
1744 case UNSPEC:
1745 case UNSPEC_VOLATILE:
1746 gcc_assert (targetm.dwarf_handle_frame_unspec);
1747 targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
1748 return;
1750 default:
1751 gcc_unreachable ();
1754 def_cfa_1 (label, &cfa);
1755 break;
1757 case MEM:
1758 gcc_assert (REG_P (src));
1760 /* Saving a register to the stack. Make sure dest is relative to the
1761 CFA register. */
1762 switch (GET_CODE (XEXP (dest, 0)))
1764 /* Rule 10 */
1765 /* With a push. */
1766 case PRE_MODIFY:
1767 /* We can't handle variable size modifications. */
1768 gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
1769 == CONST_INT);
1770 offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
1772 gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1773 && cfa_store.reg == STACK_POINTER_REGNUM);
1775 cfa_store.offset += offset;
1776 if (cfa.reg == STACK_POINTER_REGNUM)
1777 cfa.offset = cfa_store.offset;
1779 offset = -cfa_store.offset;
1780 break;
1782 /* Rule 11 */
1783 case PRE_INC:
1784 case PRE_DEC:
1785 offset = GET_MODE_SIZE (GET_MODE (dest));
1786 if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
1787 offset = -offset;
1789 gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1790 && cfa_store.reg == STACK_POINTER_REGNUM);
1792 cfa_store.offset += offset;
1793 if (cfa.reg == STACK_POINTER_REGNUM)
1794 cfa.offset = cfa_store.offset;
1796 offset = -cfa_store.offset;
1797 break;
1799 /* Rule 12 */
1800 /* With an offset. */
1801 case PLUS:
1802 case MINUS:
1803 case LO_SUM:
1805 int regno;
1807 gcc_assert (GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT
1808 && REG_P (XEXP (XEXP (dest, 0), 0)));
1809 offset = INTVAL (XEXP (XEXP (dest, 0), 1));
1810 if (GET_CODE (XEXP (dest, 0)) == MINUS)
1811 offset = -offset;
1813 regno = REGNO (XEXP (XEXP (dest, 0), 0));
1815 if (cfa_store.reg == (unsigned) regno)
1816 offset -= cfa_store.offset;
1817 else
1819 gcc_assert (cfa_temp.reg == (unsigned) regno);
1820 offset -= cfa_temp.offset;
1823 break;
1825 /* Rule 13 */
1826 /* Without an offset. */
1827 case REG:
1829 int regno = REGNO (XEXP (dest, 0));
1831 if (cfa_store.reg == (unsigned) regno)
1832 offset = -cfa_store.offset;
1833 else
1835 gcc_assert (cfa_temp.reg == (unsigned) regno);
1836 offset = -cfa_temp.offset;
1839 break;
1841 /* Rule 14 */
1842 case POST_INC:
1843 gcc_assert (cfa_temp.reg
1844 == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
1845 offset = -cfa_temp.offset;
1846 cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
1847 break;
1849 default:
1850 gcc_unreachable ();
1853 if (REGNO (src) != STACK_POINTER_REGNUM
1854 && REGNO (src) != HARD_FRAME_POINTER_REGNUM
1855 && (unsigned) REGNO (src) == cfa.reg)
1857 /* We're storing the current CFA reg into the stack. */
1859 if (cfa.offset == 0)
1861 /* If the source register is exactly the CFA, assume
1862 we're saving SP like any other register; this happens
1863 on the ARM. */
1864 def_cfa_1 (label, &cfa);
1865 queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
1866 break;
1868 else
1870 /* Otherwise, we'll need to look in the stack to
1871 calculate the CFA. */
1872 rtx x = XEXP (dest, 0);
1874 if (!REG_P (x))
1875 x = XEXP (x, 0);
1876 gcc_assert (REG_P (x));
1878 cfa.reg = REGNO (x);
1879 cfa.base_offset = offset;
1880 cfa.indirect = 1;
1881 def_cfa_1 (label, &cfa);
1882 break;
1886 def_cfa_1 (label, &cfa);
1888 span = targetm.dwarf_register_span (src);
1890 if (!span)
1891 queue_reg_save (label, src, NULL_RTX, offset);
1892 else
1894 /* We have a PARALLEL describing where the contents of SRC
1895 live. Queue register saves for each piece of the
1896 PARALLEL. */
1897 int par_index;
1898 int limit;
1899 HOST_WIDE_INT span_offset = offset;
1901 gcc_assert (GET_CODE (span) == PARALLEL);
1903 limit = XVECLEN (span, 0);
1904 for (par_index = 0; par_index < limit; par_index++)
1906 rtx elem = XVECEXP (span, 0, par_index);
1908 queue_reg_save (label, elem, NULL_RTX, span_offset);
1909 span_offset += GET_MODE_SIZE (GET_MODE (elem));
1913 break;
1915 default:
1916 gcc_unreachable ();
1920 /* Record call frame debugging information for INSN, which either
1921 sets SP or FP (adjusting how we calculate the frame address) or saves a
1922 register to the stack. If INSN is NULL_RTX, initialize our state.
1924 If AFTER_P is false, we're being called before the insn is emitted,
1925 otherwise after. Call instructions get invoked twice. */
1927 void
1928 dwarf2out_frame_debug (rtx insn, bool after_p)
1930 const char *label;
1931 rtx src;
1933 if (insn == NULL_RTX)
1935 size_t i;
1937 /* Flush any queued register saves. */
1938 flush_queued_reg_saves ();
1940 /* Set up state for generating call frame debug info. */
1941 lookup_cfa (&cfa);
1942 gcc_assert (cfa.reg
1943 == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
1945 cfa.reg = STACK_POINTER_REGNUM;
1946 cfa_store = cfa;
1947 cfa_temp.reg = -1;
1948 cfa_temp.offset = 0;
1950 for (i = 0; i < num_regs_saved_in_regs; i++)
1952 regs_saved_in_regs[i].orig_reg = NULL_RTX;
1953 regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
1955 num_regs_saved_in_regs = 0;
1956 return;
1959 if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
1960 flush_queued_reg_saves ();
1962 if (! RTX_FRAME_RELATED_P (insn))
1964 if (!ACCUMULATE_OUTGOING_ARGS)
1965 dwarf2out_stack_adjust (insn, after_p);
1966 return;
1969 label = dwarf2out_cfi_label ();
1970 src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1971 if (src)
1972 insn = XEXP (src, 0);
1973 else
1974 insn = PATTERN (insn);
1976 dwarf2out_frame_debug_expr (insn, label);
1979 #endif
1981 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
1982 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
1983 (enum dwarf_call_frame_info cfi);
1985 static enum dw_cfi_oprnd_type
1986 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
1988 switch (cfi)
1990 case DW_CFA_nop:
1991 case DW_CFA_GNU_window_save:
1992 return dw_cfi_oprnd_unused;
1994 case DW_CFA_set_loc:
1995 case DW_CFA_advance_loc1:
1996 case DW_CFA_advance_loc2:
1997 case DW_CFA_advance_loc4:
1998 case DW_CFA_MIPS_advance_loc8:
1999 return dw_cfi_oprnd_addr;
2001 case DW_CFA_offset:
2002 case DW_CFA_offset_extended:
2003 case DW_CFA_def_cfa:
2004 case DW_CFA_offset_extended_sf:
2005 case DW_CFA_def_cfa_sf:
2006 case DW_CFA_restore_extended:
2007 case DW_CFA_undefined:
2008 case DW_CFA_same_value:
2009 case DW_CFA_def_cfa_register:
2010 case DW_CFA_register:
2011 return dw_cfi_oprnd_reg_num;
2013 case DW_CFA_def_cfa_offset:
2014 case DW_CFA_GNU_args_size:
2015 case DW_CFA_def_cfa_offset_sf:
2016 return dw_cfi_oprnd_offset;
2018 case DW_CFA_def_cfa_expression:
2019 case DW_CFA_expression:
2020 return dw_cfi_oprnd_loc;
2022 default:
2023 gcc_unreachable ();
2027 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
2028 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
2029 (enum dwarf_call_frame_info cfi);
2031 static enum dw_cfi_oprnd_type
2032 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
2034 switch (cfi)
2036 case DW_CFA_def_cfa:
2037 case DW_CFA_def_cfa_sf:
2038 case DW_CFA_offset:
2039 case DW_CFA_offset_extended_sf:
2040 case DW_CFA_offset_extended:
2041 return dw_cfi_oprnd_offset;
2043 case DW_CFA_register:
2044 return dw_cfi_oprnd_reg_num;
2046 default:
2047 return dw_cfi_oprnd_unused;
2051 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2053 /* Switch to eh_frame_section. If we don't have an eh_frame_section,
2054 switch to the data section instead, and write out a synthetic label
2055 for collect2. */
2057 static void
2058 switch_to_eh_frame_section (void)
2060 tree label;
2062 #ifdef EH_FRAME_SECTION_NAME
2063 if (eh_frame_section == 0)
2065 int flags;
2067 if (EH_TABLES_CAN_BE_READ_ONLY)
2069 int fde_encoding;
2070 int per_encoding;
2071 int lsda_encoding;
2073 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2074 /*global=*/0);
2075 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2076 /*global=*/1);
2077 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2078 /*global=*/0);
2079 flags = ((! flag_pic
2080 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2081 && (fde_encoding & 0x70) != DW_EH_PE_aligned
2082 && (per_encoding & 0x70) != DW_EH_PE_absptr
2083 && (per_encoding & 0x70) != DW_EH_PE_aligned
2084 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2085 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2086 ? 0 : SECTION_WRITE);
2088 else
2089 flags = SECTION_WRITE;
2090 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2092 #endif
2094 if (eh_frame_section)
2095 switch_to_section (eh_frame_section);
2096 else
2098 /* We have no special eh_frame section. Put the information in
2099 the data section and emit special labels to guide collect2. */
2100 switch_to_section (data_section);
2101 label = get_file_function_name ("F");
2102 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2103 targetm.asm_out.globalize_label (asm_out_file,
2104 IDENTIFIER_POINTER (label));
2105 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
2109 /* Output a Call Frame Information opcode and its operand(s). */
2111 static void
2112 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
2114 unsigned long r;
2115 if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
2116 dw2_asm_output_data (1, (cfi->dw_cfi_opc
2117 | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
2118 "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
2119 cfi->dw_cfi_oprnd1.dw_cfi_offset);
2120 else if (cfi->dw_cfi_opc == DW_CFA_offset)
2122 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2123 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2124 "DW_CFA_offset, column 0x%lx", r);
2125 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2127 else if (cfi->dw_cfi_opc == DW_CFA_restore)
2129 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2130 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2131 "DW_CFA_restore, column 0x%lx", r);
2133 else
2135 dw2_asm_output_data (1, cfi->dw_cfi_opc,
2136 "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
2138 switch (cfi->dw_cfi_opc)
2140 case DW_CFA_set_loc:
2141 if (for_eh)
2142 dw2_asm_output_encoded_addr_rtx (
2143 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
2144 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
2145 false, NULL);
2146 else
2147 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2148 cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
2149 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2150 break;
2152 case DW_CFA_advance_loc1:
2153 dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2154 fde->dw_fde_current_label, NULL);
2155 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2156 break;
2158 case DW_CFA_advance_loc2:
2159 dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2160 fde->dw_fde_current_label, NULL);
2161 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2162 break;
2164 case DW_CFA_advance_loc4:
2165 dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2166 fde->dw_fde_current_label, NULL);
2167 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2168 break;
2170 case DW_CFA_MIPS_advance_loc8:
2171 dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2172 fde->dw_fde_current_label, NULL);
2173 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2174 break;
2176 case DW_CFA_offset_extended:
2177 case DW_CFA_def_cfa:
2178 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2179 dw2_asm_output_data_uleb128 (r, NULL);
2180 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2181 break;
2183 case DW_CFA_offset_extended_sf:
2184 case DW_CFA_def_cfa_sf:
2185 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2186 dw2_asm_output_data_uleb128 (r, NULL);
2187 dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2188 break;
2190 case DW_CFA_restore_extended:
2191 case DW_CFA_undefined:
2192 case DW_CFA_same_value:
2193 case DW_CFA_def_cfa_register:
2194 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2195 dw2_asm_output_data_uleb128 (r, NULL);
2196 break;
2198 case DW_CFA_register:
2199 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2200 dw2_asm_output_data_uleb128 (r, NULL);
2201 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
2202 dw2_asm_output_data_uleb128 (r, NULL);
2203 break;
2205 case DW_CFA_def_cfa_offset:
2206 case DW_CFA_GNU_args_size:
2207 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2208 break;
2210 case DW_CFA_def_cfa_offset_sf:
2211 dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2212 break;
2214 case DW_CFA_GNU_window_save:
2215 break;
2217 case DW_CFA_def_cfa_expression:
2218 case DW_CFA_expression:
2219 output_cfa_loc (cfi);
2220 break;
2222 case DW_CFA_GNU_negative_offset_extended:
2223 /* Obsoleted by DW_CFA_offset_extended_sf. */
2224 gcc_unreachable ();
2226 default:
2227 break;
2232 /* Output the call frame information used to record information
2233 that relates to calculating the frame pointer, and records the
2234 location of saved registers. */
2236 static void
2237 output_call_frame_info (int for_eh)
2239 unsigned int i;
2240 dw_fde_ref fde;
2241 dw_cfi_ref cfi;
2242 char l1[20], l2[20], section_start_label[20];
2243 bool any_lsda_needed = false;
2244 char augmentation[6];
2245 int augmentation_size;
2246 int fde_encoding = DW_EH_PE_absptr;
2247 int per_encoding = DW_EH_PE_absptr;
2248 int lsda_encoding = DW_EH_PE_absptr;
2249 int return_reg;
2251 /* Don't emit a CIE if there won't be any FDEs. */
2252 if (fde_table_in_use == 0)
2253 return;
2255 /* If we make FDEs linkonce, we may have to emit an empty label for
2256 an FDE that wouldn't otherwise be emitted. We want to avoid
2257 having an FDE kept around when the function it refers to is
2258 discarded. Example where this matters: a primary function
2259 template in C++ requires EH information, but an explicit
2260 specialization doesn't. */
2261 if (TARGET_USES_WEAK_UNWIND_INFO
2262 && ! flag_asynchronous_unwind_tables
2263 && flag_exceptions
2264 && for_eh)
2265 for (i = 0; i < fde_table_in_use; i++)
2266 if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
2267 && !fde_table[i].uses_eh_lsda
2268 && ! DECL_WEAK (fde_table[i].decl))
2269 targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
2270 for_eh, /* empty */ 1);
2272 /* If we don't have any functions we'll want to unwind out of, don't
2273 emit any EH unwind information. Note that if exceptions aren't
2274 enabled, we won't have collected nothrow information, and if we
2275 asked for asynchronous tables, we always want this info. */
2276 if (for_eh)
2278 bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
2280 for (i = 0; i < fde_table_in_use; i++)
2281 if (fde_table[i].uses_eh_lsda)
2282 any_eh_needed = any_lsda_needed = true;
2283 else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2284 any_eh_needed = true;
2285 else if (! fde_table[i].nothrow
2286 && ! fde_table[i].all_throwers_are_sibcalls)
2287 any_eh_needed = true;
2289 if (! any_eh_needed)
2290 return;
2293 /* We're going to be generating comments, so turn on app. */
2294 if (flag_debug_asm)
2295 app_enable ();
2297 if (for_eh)
2298 switch_to_eh_frame_section ();
2299 else
2301 if (!debug_frame_section)
2302 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
2303 SECTION_DEBUG, NULL);
2304 switch_to_section (debug_frame_section);
2307 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
2308 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
2310 /* Output the CIE. */
2311 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
2312 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
2313 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2314 dw2_asm_output_data (4, 0xffffffff,
2315 "Initial length escape value indicating 64-bit DWARF extension");
2316 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2317 "Length of Common Information Entry");
2318 ASM_OUTPUT_LABEL (asm_out_file, l1);
2320 /* Now that the CIE pointer is PC-relative for EH,
2321 use 0 to identify the CIE. */
2322 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
2323 (for_eh ? 0 : DWARF_CIE_ID),
2324 "CIE Identifier Tag");
2326 dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
2328 augmentation[0] = 0;
2329 augmentation_size = 0;
2330 if (for_eh)
2332 char *p;
2334 /* Augmentation:
2335 z Indicates that a uleb128 is present to size the
2336 augmentation section.
2337 L Indicates the encoding (and thus presence) of
2338 an LSDA pointer in the FDE augmentation.
2339 R Indicates a non-default pointer encoding for
2340 FDE code pointers.
2341 P Indicates the presence of an encoding + language
2342 personality routine in the CIE augmentation. */
2344 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
2345 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
2346 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
2348 p = augmentation + 1;
2349 if (eh_personality_libfunc)
2351 *p++ = 'P';
2352 augmentation_size += 1 + size_of_encoded_value (per_encoding);
2353 assemble_external_libcall (eh_personality_libfunc);
2355 if (any_lsda_needed)
2357 *p++ = 'L';
2358 augmentation_size += 1;
2360 if (fde_encoding != DW_EH_PE_absptr)
2362 *p++ = 'R';
2363 augmentation_size += 1;
2365 if (p > augmentation + 1)
2367 augmentation[0] = 'z';
2368 *p = '\0';
2371 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
2372 if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
2374 int offset = ( 4 /* Length */
2375 + 4 /* CIE Id */
2376 + 1 /* CIE version */
2377 + strlen (augmentation) + 1 /* Augmentation */
2378 + size_of_uleb128 (1) /* Code alignment */
2379 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
2380 + 1 /* RA column */
2381 + 1 /* Augmentation size */
2382 + 1 /* Personality encoding */ );
2383 int pad = -offset & (PTR_SIZE - 1);
2385 augmentation_size += pad;
2387 /* Augmentations should be small, so there's scarce need to
2388 iterate for a solution. Die if we exceed one uleb128 byte. */
2389 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
2393 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
2394 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
2395 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
2396 "CIE Data Alignment Factor");
2398 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
2399 if (DW_CIE_VERSION == 1)
2400 dw2_asm_output_data (1, return_reg, "CIE RA Column");
2401 else
2402 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
2404 if (augmentation[0])
2406 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
2407 if (eh_personality_libfunc)
2409 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
2410 eh_data_format_name (per_encoding));
2411 dw2_asm_output_encoded_addr_rtx (per_encoding,
2412 eh_personality_libfunc,
2413 true, NULL);
2416 if (any_lsda_needed)
2417 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
2418 eh_data_format_name (lsda_encoding));
2420 if (fde_encoding != DW_EH_PE_absptr)
2421 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
2422 eh_data_format_name (fde_encoding));
2425 for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
2426 output_cfi (cfi, NULL, for_eh);
2428 /* Pad the CIE out to an address sized boundary. */
2429 ASM_OUTPUT_ALIGN (asm_out_file,
2430 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
2431 ASM_OUTPUT_LABEL (asm_out_file, l2);
2433 /* Loop through all of the FDE's. */
2434 for (i = 0; i < fde_table_in_use; i++)
2436 fde = &fde_table[i];
2438 /* Don't emit EH unwind info for leaf functions that don't need it. */
2439 if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
2440 && (fde->nothrow || fde->all_throwers_are_sibcalls)
2441 && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2442 && !fde->uses_eh_lsda)
2443 continue;
2445 targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
2446 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
2447 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
2448 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
2449 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2450 dw2_asm_output_data (4, 0xffffffff,
2451 "Initial length escape value indicating 64-bit DWARF extension");
2452 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2453 "FDE Length");
2454 ASM_OUTPUT_LABEL (asm_out_file, l1);
2456 if (for_eh)
2457 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
2458 else
2459 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
2460 debug_frame_section, "FDE CIE offset");
2462 if (for_eh)
2464 if (fde->dw_fde_switched_sections)
2466 rtx sym_ref2 = gen_rtx_SYMBOL_REF (Pmode,
2467 fde->dw_fde_unlikely_section_label);
2468 rtx sym_ref3= gen_rtx_SYMBOL_REF (Pmode,
2469 fde->dw_fde_hot_section_label);
2470 SYMBOL_REF_FLAGS (sym_ref2) |= SYMBOL_FLAG_LOCAL;
2471 SYMBOL_REF_FLAGS (sym_ref3) |= SYMBOL_FLAG_LOCAL;
2472 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref3, false,
2473 "FDE initial location");
2474 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2475 fde->dw_fde_hot_section_end_label,
2476 fde->dw_fde_hot_section_label,
2477 "FDE address range");
2478 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref2, false,
2479 "FDE initial location");
2480 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2481 fde->dw_fde_unlikely_section_end_label,
2482 fde->dw_fde_unlikely_section_label,
2483 "FDE address range");
2485 else
2487 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
2488 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
2489 dw2_asm_output_encoded_addr_rtx (fde_encoding,
2490 sym_ref,
2491 false,
2492 "FDE initial location");
2493 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2494 fde->dw_fde_end, fde->dw_fde_begin,
2495 "FDE address range");
2498 else
2500 if (fde->dw_fde_switched_sections)
2502 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2503 fde->dw_fde_hot_section_label,
2504 "FDE initial location");
2505 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2506 fde->dw_fde_hot_section_end_label,
2507 fde->dw_fde_hot_section_label,
2508 "FDE address range");
2509 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2510 fde->dw_fde_unlikely_section_label,
2511 "FDE initial location");
2512 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2513 fde->dw_fde_unlikely_section_end_label,
2514 fde->dw_fde_unlikely_section_label,
2515 "FDE address range");
2517 else
2519 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
2520 "FDE initial location");
2521 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2522 fde->dw_fde_end, fde->dw_fde_begin,
2523 "FDE address range");
2527 if (augmentation[0])
2529 if (any_lsda_needed)
2531 int size = size_of_encoded_value (lsda_encoding);
2533 if (lsda_encoding == DW_EH_PE_aligned)
2535 int offset = ( 4 /* Length */
2536 + 4 /* CIE offset */
2537 + 2 * size_of_encoded_value (fde_encoding)
2538 + 1 /* Augmentation size */ );
2539 int pad = -offset & (PTR_SIZE - 1);
2541 size += pad;
2542 gcc_assert (size_of_uleb128 (size) == 1);
2545 dw2_asm_output_data_uleb128 (size, "Augmentation size");
2547 if (fde->uses_eh_lsda)
2549 ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
2550 fde->funcdef_number);
2551 dw2_asm_output_encoded_addr_rtx (
2552 lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
2553 false, "Language Specific Data Area");
2555 else
2557 if (lsda_encoding == DW_EH_PE_aligned)
2558 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2559 dw2_asm_output_data
2560 (size_of_encoded_value (lsda_encoding), 0,
2561 "Language Specific Data Area (none)");
2564 else
2565 dw2_asm_output_data_uleb128 (0, "Augmentation size");
2568 /* Loop through the Call Frame Instructions associated with
2569 this FDE. */
2570 fde->dw_fde_current_label = fde->dw_fde_begin;
2571 for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
2572 output_cfi (cfi, fde, for_eh);
2574 /* Pad the FDE out to an address sized boundary. */
2575 ASM_OUTPUT_ALIGN (asm_out_file,
2576 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
2577 ASM_OUTPUT_LABEL (asm_out_file, l2);
2580 if (for_eh && targetm.terminate_dw2_eh_frame_info)
2581 dw2_asm_output_data (4, 0, "End of Table");
2582 #ifdef MIPS_DEBUGGING_INFO
2583 /* Work around Irix 6 assembler bug whereby labels at the end of a section
2584 get a value of 0. Putting .align 0 after the label fixes it. */
2585 ASM_OUTPUT_ALIGN (asm_out_file, 0);
2586 #endif
2588 /* Turn off app to make assembly quicker. */
2589 if (flag_debug_asm)
2590 app_disable ();
2593 /* Output a marker (i.e. a label) for the beginning of a function, before
2594 the prologue. */
2596 void
2597 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
2598 const char *file ATTRIBUTE_UNUSED)
2600 char label[MAX_ARTIFICIAL_LABEL_BYTES];
2601 char * dup_label;
2602 dw_fde_ref fde;
2604 current_function_func_begin_label = NULL;
2606 #ifdef TARGET_UNWIND_INFO
2607 /* ??? current_function_func_begin_label is also used by except.c
2608 for call-site information. We must emit this label if it might
2609 be used. */
2610 if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
2611 && ! dwarf2out_do_frame ())
2612 return;
2613 #else
2614 if (! dwarf2out_do_frame ())
2615 return;
2616 #endif
2618 switch_to_section (function_section (current_function_decl));
2619 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
2620 current_function_funcdef_no);
2621 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
2622 current_function_funcdef_no);
2623 dup_label = xstrdup (label);
2624 current_function_func_begin_label = dup_label;
2626 #ifdef TARGET_UNWIND_INFO
2627 /* We can elide the fde allocation if we're not emitting debug info. */
2628 if (! dwarf2out_do_frame ())
2629 return;
2630 #endif
2632 /* Expand the fde table if necessary. */
2633 if (fde_table_in_use == fde_table_allocated)
2635 fde_table_allocated += FDE_TABLE_INCREMENT;
2636 fde_table = ggc_realloc (fde_table,
2637 fde_table_allocated * sizeof (dw_fde_node));
2638 memset (fde_table + fde_table_in_use, 0,
2639 FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2642 /* Record the FDE associated with this function. */
2643 current_funcdef_fde = fde_table_in_use;
2645 /* Add the new FDE at the end of the fde_table. */
2646 fde = &fde_table[fde_table_in_use++];
2647 fde->decl = current_function_decl;
2648 fde->dw_fde_begin = dup_label;
2649 fde->dw_fde_current_label = dup_label;
2650 fde->dw_fde_hot_section_label = NULL;
2651 fde->dw_fde_hot_section_end_label = NULL;
2652 fde->dw_fde_unlikely_section_label = NULL;
2653 fde->dw_fde_unlikely_section_end_label = NULL;
2654 fde->dw_fde_switched_sections = false;
2655 fde->dw_fde_end = NULL;
2656 fde->dw_fde_cfi = NULL;
2657 fde->funcdef_number = current_function_funcdef_no;
2658 fde->nothrow = TREE_NOTHROW (current_function_decl);
2659 fde->uses_eh_lsda = cfun->uses_eh_lsda;
2660 fde->all_throwers_are_sibcalls = cfun->all_throwers_are_sibcalls;
2662 args_size = old_args_size = 0;
2664 /* We only want to output line number information for the genuine dwarf2
2665 prologue case, not the eh frame case. */
2666 #ifdef DWARF2_DEBUGGING_INFO
2667 if (file)
2668 dwarf2out_source_line (line, file);
2669 #endif
2672 /* Output a marker (i.e. a label) for the absolute end of the generated code
2673 for a function definition. This gets called *after* the epilogue code has
2674 been generated. */
2676 void
2677 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
2678 const char *file ATTRIBUTE_UNUSED)
2680 dw_fde_ref fde;
2681 char label[MAX_ARTIFICIAL_LABEL_BYTES];
2683 /* Output a label to mark the endpoint of the code generated for this
2684 function. */
2685 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
2686 current_function_funcdef_no);
2687 ASM_OUTPUT_LABEL (asm_out_file, label);
2688 fde = &fde_table[fde_table_in_use - 1];
2689 fde->dw_fde_end = xstrdup (label);
2692 void
2693 dwarf2out_frame_init (void)
2695 /* Allocate the initial hunk of the fde_table. */
2696 fde_table = ggc_alloc_cleared (FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2697 fde_table_allocated = FDE_TABLE_INCREMENT;
2698 fde_table_in_use = 0;
2700 /* Generate the CFA instructions common to all FDE's. Do it now for the
2701 sake of lookup_cfa. */
2703 /* On entry, the Canonical Frame Address is at SP. */
2704 dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
2706 #ifdef DWARF2_UNWIND_INFO
2707 if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
2708 initial_return_save (INCOMING_RETURN_ADDR_RTX);
2709 #endif
2712 void
2713 dwarf2out_frame_finish (void)
2715 /* Output call frame information. */
2716 if (DWARF2_FRAME_INFO)
2717 output_call_frame_info (0);
2719 #ifndef TARGET_UNWIND_INFO
2720 /* Output another copy for the unwinder. */
2721 if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
2722 output_call_frame_info (1);
2723 #endif
2726 /* Note that the current function section is being used for code. */
2728 static void
2729 dwarf2out_note_section_used (void)
2731 section *sec = current_function_section ();
2732 if (sec == text_section)
2733 text_section_used = true;
2734 else if (sec == cold_text_section)
2735 cold_text_section_used = true;
2738 void
2739 dwarf2out_switch_text_section (void)
2741 dw_fde_ref fde;
2743 gcc_assert (cfun);
2745 fde = &fde_table[fde_table_in_use - 1];
2746 fde->dw_fde_switched_sections = true;
2747 fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
2748 fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
2749 fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
2750 fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
2751 have_multiple_function_sections = true;
2753 /* Reset the current label on switching text sections, so that we
2754 don't attempt to advance_loc4 between labels in different sections. */
2755 fde->dw_fde_current_label = NULL;
2757 /* There is no need to mark used sections when not debugging. */
2758 if (cold_text_section != NULL)
2759 dwarf2out_note_section_used ();
2761 #endif
2763 /* And now, the subset of the debugging information support code necessary
2764 for emitting location expressions. */
2766 /* Data about a single source file. */
2767 struct dwarf_file_data GTY(())
2769 const char * filename;
2770 int emitted_number;
2773 /* We need some way to distinguish DW_OP_addr with a direct symbol
2774 relocation from DW_OP_addr with a dtp-relative symbol relocation. */
2775 #define INTERNAL_DW_OP_tls_addr (0x100 + DW_OP_addr)
2778 typedef struct dw_val_struct *dw_val_ref;
2779 typedef struct die_struct *dw_die_ref;
2780 typedef const struct die_struct *const_dw_die_ref;
2781 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
2782 typedef struct dw_loc_list_struct *dw_loc_list_ref;
2784 /* Each DIE may have a series of attribute/value pairs. Values
2785 can take on several forms. The forms that are used in this
2786 implementation are listed below. */
2788 enum dw_val_class
2790 dw_val_class_addr,
2791 dw_val_class_offset,
2792 dw_val_class_loc,
2793 dw_val_class_loc_list,
2794 dw_val_class_range_list,
2795 dw_val_class_const,
2796 dw_val_class_unsigned_const,
2797 dw_val_class_long_long,
2798 dw_val_class_vec,
2799 dw_val_class_flag,
2800 dw_val_class_die_ref,
2801 dw_val_class_fde_ref,
2802 dw_val_class_lbl_id,
2803 dw_val_class_lineptr,
2804 dw_val_class_str,
2805 dw_val_class_macptr,
2806 dw_val_class_file
2809 /* Describe a double word constant value. */
2810 /* ??? Every instance of long_long in the code really means CONST_DOUBLE. */
2812 typedef struct dw_long_long_struct GTY(())
2814 unsigned long hi;
2815 unsigned long low;
2817 dw_long_long_const;
2819 /* Describe a floating point constant value, or a vector constant value. */
2821 typedef struct dw_vec_struct GTY(())
2823 unsigned char * GTY((length ("%h.length"))) array;
2824 unsigned length;
2825 unsigned elt_size;
2827 dw_vec_const;
2829 /* The dw_val_node describes an attribute's value, as it is
2830 represented internally. */
2832 typedef struct dw_val_struct GTY(())
2834 enum dw_val_class val_class;
2835 union dw_val_struct_union
2837 rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
2838 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
2839 dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
2840 dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
2841 HOST_WIDE_INT GTY ((default)) val_int;
2842 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
2843 dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
2844 dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
2845 struct dw_val_die_union
2847 dw_die_ref die;
2848 int external;
2849 } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
2850 unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
2851 struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
2852 char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
2853 unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
2854 struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
2856 GTY ((desc ("%1.val_class"))) v;
2858 dw_val_node;
2860 /* Locations in memory are described using a sequence of stack machine
2861 operations. */
2863 typedef struct dw_loc_descr_struct GTY(())
2865 dw_loc_descr_ref dw_loc_next;
2866 enum dwarf_location_atom dw_loc_opc;
2867 dw_val_node dw_loc_oprnd1;
2868 dw_val_node dw_loc_oprnd2;
2869 int dw_loc_addr;
2871 dw_loc_descr_node;
2873 /* Location lists are ranges + location descriptions for that range,
2874 so you can track variables that are in different places over
2875 their entire life. */
2876 typedef struct dw_loc_list_struct GTY(())
2878 dw_loc_list_ref dw_loc_next;
2879 const char *begin; /* Label for begin address of range */
2880 const char *end; /* Label for end address of range */
2881 char *ll_symbol; /* Label for beginning of location list.
2882 Only on head of list */
2883 const char *section; /* Section this loclist is relative to */
2884 dw_loc_descr_ref expr;
2885 } dw_loc_list_node;
2887 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2889 static const char *dwarf_stack_op_name (unsigned);
2890 static dw_loc_descr_ref new_loc_descr (enum dwarf_location_atom,
2891 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2892 static void add_loc_descr (dw_loc_descr_ref *, dw_loc_descr_ref);
2893 static unsigned long size_of_loc_descr (dw_loc_descr_ref);
2894 static unsigned long size_of_locs (dw_loc_descr_ref);
2895 static void output_loc_operands (dw_loc_descr_ref);
2896 static void output_loc_sequence (dw_loc_descr_ref);
2898 /* Convert a DWARF stack opcode into its string name. */
2900 static const char *
2901 dwarf_stack_op_name (unsigned int op)
2903 switch (op)
2905 case DW_OP_addr:
2906 case INTERNAL_DW_OP_tls_addr:
2907 return "DW_OP_addr";
2908 case DW_OP_deref:
2909 return "DW_OP_deref";
2910 case DW_OP_const1u:
2911 return "DW_OP_const1u";
2912 case DW_OP_const1s:
2913 return "DW_OP_const1s";
2914 case DW_OP_const2u:
2915 return "DW_OP_const2u";
2916 case DW_OP_const2s:
2917 return "DW_OP_const2s";
2918 case DW_OP_const4u:
2919 return "DW_OP_const4u";
2920 case DW_OP_const4s:
2921 return "DW_OP_const4s";
2922 case DW_OP_const8u:
2923 return "DW_OP_const8u";
2924 case DW_OP_const8s:
2925 return "DW_OP_const8s";
2926 case DW_OP_constu:
2927 return "DW_OP_constu";
2928 case DW_OP_consts:
2929 return "DW_OP_consts";
2930 case DW_OP_dup:
2931 return "DW_OP_dup";
2932 case DW_OP_drop:
2933 return "DW_OP_drop";
2934 case DW_OP_over:
2935 return "DW_OP_over";
2936 case DW_OP_pick:
2937 return "DW_OP_pick";
2938 case DW_OP_swap:
2939 return "DW_OP_swap";
2940 case DW_OP_rot:
2941 return "DW_OP_rot";
2942 case DW_OP_xderef:
2943 return "DW_OP_xderef";
2944 case DW_OP_abs:
2945 return "DW_OP_abs";
2946 case DW_OP_and:
2947 return "DW_OP_and";
2948 case DW_OP_div:
2949 return "DW_OP_div";
2950 case DW_OP_minus:
2951 return "DW_OP_minus";
2952 case DW_OP_mod:
2953 return "DW_OP_mod";
2954 case DW_OP_mul:
2955 return "DW_OP_mul";
2956 case DW_OP_neg:
2957 return "DW_OP_neg";
2958 case DW_OP_not:
2959 return "DW_OP_not";
2960 case DW_OP_or:
2961 return "DW_OP_or";
2962 case DW_OP_plus:
2963 return "DW_OP_plus";
2964 case DW_OP_plus_uconst:
2965 return "DW_OP_plus_uconst";
2966 case DW_OP_shl:
2967 return "DW_OP_shl";
2968 case DW_OP_shr:
2969 return "DW_OP_shr";
2970 case DW_OP_shra:
2971 return "DW_OP_shra";
2972 case DW_OP_xor:
2973 return "DW_OP_xor";
2974 case DW_OP_bra:
2975 return "DW_OP_bra";
2976 case DW_OP_eq:
2977 return "DW_OP_eq";
2978 case DW_OP_ge:
2979 return "DW_OP_ge";
2980 case DW_OP_gt:
2981 return "DW_OP_gt";
2982 case DW_OP_le:
2983 return "DW_OP_le";
2984 case DW_OP_lt:
2985 return "DW_OP_lt";
2986 case DW_OP_ne:
2987 return "DW_OP_ne";
2988 case DW_OP_skip:
2989 return "DW_OP_skip";
2990 case DW_OP_lit0:
2991 return "DW_OP_lit0";
2992 case DW_OP_lit1:
2993 return "DW_OP_lit1";
2994 case DW_OP_lit2:
2995 return "DW_OP_lit2";
2996 case DW_OP_lit3:
2997 return "DW_OP_lit3";
2998 case DW_OP_lit4:
2999 return "DW_OP_lit4";
3000 case DW_OP_lit5:
3001 return "DW_OP_lit5";
3002 case DW_OP_lit6:
3003 return "DW_OP_lit6";
3004 case DW_OP_lit7:
3005 return "DW_OP_lit7";
3006 case DW_OP_lit8:
3007 return "DW_OP_lit8";
3008 case DW_OP_lit9:
3009 return "DW_OP_lit9";
3010 case DW_OP_lit10:
3011 return "DW_OP_lit10";
3012 case DW_OP_lit11:
3013 return "DW_OP_lit11";
3014 case DW_OP_lit12:
3015 return "DW_OP_lit12";
3016 case DW_OP_lit13:
3017 return "DW_OP_lit13";
3018 case DW_OP_lit14:
3019 return "DW_OP_lit14";
3020 case DW_OP_lit15:
3021 return "DW_OP_lit15";
3022 case DW_OP_lit16:
3023 return "DW_OP_lit16";
3024 case DW_OP_lit17:
3025 return "DW_OP_lit17";
3026 case DW_OP_lit18:
3027 return "DW_OP_lit18";
3028 case DW_OP_lit19:
3029 return "DW_OP_lit19";
3030 case DW_OP_lit20:
3031 return "DW_OP_lit20";
3032 case DW_OP_lit21:
3033 return "DW_OP_lit21";
3034 case DW_OP_lit22:
3035 return "DW_OP_lit22";
3036 case DW_OP_lit23:
3037 return "DW_OP_lit23";
3038 case DW_OP_lit24:
3039 return "DW_OP_lit24";
3040 case DW_OP_lit25:
3041 return "DW_OP_lit25";
3042 case DW_OP_lit26:
3043 return "DW_OP_lit26";
3044 case DW_OP_lit27:
3045 return "DW_OP_lit27";
3046 case DW_OP_lit28:
3047 return "DW_OP_lit28";
3048 case DW_OP_lit29:
3049 return "DW_OP_lit29";
3050 case DW_OP_lit30:
3051 return "DW_OP_lit30";
3052 case DW_OP_lit31:
3053 return "DW_OP_lit31";
3054 case DW_OP_reg0:
3055 return "DW_OP_reg0";
3056 case DW_OP_reg1:
3057 return "DW_OP_reg1";
3058 case DW_OP_reg2:
3059 return "DW_OP_reg2";
3060 case DW_OP_reg3:
3061 return "DW_OP_reg3";
3062 case DW_OP_reg4:
3063 return "DW_OP_reg4";
3064 case DW_OP_reg5:
3065 return "DW_OP_reg5";
3066 case DW_OP_reg6:
3067 return "DW_OP_reg6";
3068 case DW_OP_reg7:
3069 return "DW_OP_reg7";
3070 case DW_OP_reg8:
3071 return "DW_OP_reg8";
3072 case DW_OP_reg9:
3073 return "DW_OP_reg9";
3074 case DW_OP_reg10:
3075 return "DW_OP_reg10";
3076 case DW_OP_reg11:
3077 return "DW_OP_reg11";
3078 case DW_OP_reg12:
3079 return "DW_OP_reg12";
3080 case DW_OP_reg13:
3081 return "DW_OP_reg13";
3082 case DW_OP_reg14:
3083 return "DW_OP_reg14";
3084 case DW_OP_reg15:
3085 return "DW_OP_reg15";
3086 case DW_OP_reg16:
3087 return "DW_OP_reg16";
3088 case DW_OP_reg17:
3089 return "DW_OP_reg17";
3090 case DW_OP_reg18:
3091 return "DW_OP_reg18";
3092 case DW_OP_reg19:
3093 return "DW_OP_reg19";
3094 case DW_OP_reg20:
3095 return "DW_OP_reg20";
3096 case DW_OP_reg21:
3097 return "DW_OP_reg21";
3098 case DW_OP_reg22:
3099 return "DW_OP_reg22";
3100 case DW_OP_reg23:
3101 return "DW_OP_reg23";
3102 case DW_OP_reg24:
3103 return "DW_OP_reg24";
3104 case DW_OP_reg25:
3105 return "DW_OP_reg25";
3106 case DW_OP_reg26:
3107 return "DW_OP_reg26";
3108 case DW_OP_reg27:
3109 return "DW_OP_reg27";
3110 case DW_OP_reg28:
3111 return "DW_OP_reg28";
3112 case DW_OP_reg29:
3113 return "DW_OP_reg29";
3114 case DW_OP_reg30:
3115 return "DW_OP_reg30";
3116 case DW_OP_reg31:
3117 return "DW_OP_reg31";
3118 case DW_OP_breg0:
3119 return "DW_OP_breg0";
3120 case DW_OP_breg1:
3121 return "DW_OP_breg1";
3122 case DW_OP_breg2:
3123 return "DW_OP_breg2";
3124 case DW_OP_breg3:
3125 return "DW_OP_breg3";
3126 case DW_OP_breg4:
3127 return "DW_OP_breg4";
3128 case DW_OP_breg5:
3129 return "DW_OP_breg5";
3130 case DW_OP_breg6:
3131 return "DW_OP_breg6";
3132 case DW_OP_breg7:
3133 return "DW_OP_breg7";
3134 case DW_OP_breg8:
3135 return "DW_OP_breg8";
3136 case DW_OP_breg9:
3137 return "DW_OP_breg9";
3138 case DW_OP_breg10:
3139 return "DW_OP_breg10";
3140 case DW_OP_breg11:
3141 return "DW_OP_breg11";
3142 case DW_OP_breg12:
3143 return "DW_OP_breg12";
3144 case DW_OP_breg13:
3145 return "DW_OP_breg13";
3146 case DW_OP_breg14:
3147 return "DW_OP_breg14";
3148 case DW_OP_breg15:
3149 return "DW_OP_breg15";
3150 case DW_OP_breg16:
3151 return "DW_OP_breg16";
3152 case DW_OP_breg17:
3153 return "DW_OP_breg17";
3154 case DW_OP_breg18:
3155 return "DW_OP_breg18";
3156 case DW_OP_breg19:
3157 return "DW_OP_breg19";
3158 case DW_OP_breg20:
3159 return "DW_OP_breg20";
3160 case DW_OP_breg21:
3161 return "DW_OP_breg21";
3162 case DW_OP_breg22:
3163 return "DW_OP_breg22";
3164 case DW_OP_breg23:
3165 return "DW_OP_breg23";
3166 case DW_OP_breg24:
3167 return "DW_OP_breg24";
3168 case DW_OP_breg25:
3169 return "DW_OP_breg25";
3170 case DW_OP_breg26:
3171 return "DW_OP_breg26";
3172 case DW_OP_breg27:
3173 return "DW_OP_breg27";
3174 case DW_OP_breg28:
3175 return "DW_OP_breg28";
3176 case DW_OP_breg29:
3177 return "DW_OP_breg29";
3178 case DW_OP_breg30:
3179 return "DW_OP_breg30";
3180 case DW_OP_breg31:
3181 return "DW_OP_breg31";
3182 case DW_OP_regx:
3183 return "DW_OP_regx";
3184 case DW_OP_fbreg:
3185 return "DW_OP_fbreg";
3186 case DW_OP_bregx:
3187 return "DW_OP_bregx";
3188 case DW_OP_piece:
3189 return "DW_OP_piece";
3190 case DW_OP_deref_size:
3191 return "DW_OP_deref_size";
3192 case DW_OP_xderef_size:
3193 return "DW_OP_xderef_size";
3194 case DW_OP_nop:
3195 return "DW_OP_nop";
3196 case DW_OP_push_object_address:
3197 return "DW_OP_push_object_address";
3198 case DW_OP_call2:
3199 return "DW_OP_call2";
3200 case DW_OP_call4:
3201 return "DW_OP_call4";
3202 case DW_OP_call_ref:
3203 return "DW_OP_call_ref";
3204 case DW_OP_GNU_push_tls_address:
3205 return "DW_OP_GNU_push_tls_address";
3206 case DW_OP_GNU_uninit:
3207 return "DW_OP_GNU_uninit";
3208 default:
3209 return "OP_<unknown>";
3213 /* Return a pointer to a newly allocated location description. Location
3214 descriptions are simple expression terms that can be strung
3215 together to form more complicated location (address) descriptions. */
3217 static inline dw_loc_descr_ref
3218 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
3219 unsigned HOST_WIDE_INT oprnd2)
3221 dw_loc_descr_ref descr = ggc_alloc_cleared (sizeof (dw_loc_descr_node));
3223 descr->dw_loc_opc = op;
3224 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
3225 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
3226 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
3227 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
3229 return descr;
3232 /* Add a location description term to a location description expression. */
3234 static inline void
3235 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
3237 dw_loc_descr_ref *d;
3239 /* Find the end of the chain. */
3240 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
3243 *d = descr;
3246 /* Return the size of a location descriptor. */
3248 static unsigned long
3249 size_of_loc_descr (dw_loc_descr_ref loc)
3251 unsigned long size = 1;
3253 switch (loc->dw_loc_opc)
3255 case DW_OP_addr:
3256 case INTERNAL_DW_OP_tls_addr:
3257 size += DWARF2_ADDR_SIZE;
3258 break;
3259 case DW_OP_const1u:
3260 case DW_OP_const1s:
3261 size += 1;
3262 break;
3263 case DW_OP_const2u:
3264 case DW_OP_const2s:
3265 size += 2;
3266 break;
3267 case DW_OP_const4u:
3268 case DW_OP_const4s:
3269 size += 4;
3270 break;
3271 case DW_OP_const8u:
3272 case DW_OP_const8s:
3273 size += 8;
3274 break;
3275 case DW_OP_constu:
3276 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3277 break;
3278 case DW_OP_consts:
3279 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3280 break;
3281 case DW_OP_pick:
3282 size += 1;
3283 break;
3284 case DW_OP_plus_uconst:
3285 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3286 break;
3287 case DW_OP_skip:
3288 case DW_OP_bra:
3289 size += 2;
3290 break;
3291 case DW_OP_breg0:
3292 case DW_OP_breg1:
3293 case DW_OP_breg2:
3294 case DW_OP_breg3:
3295 case DW_OP_breg4:
3296 case DW_OP_breg5:
3297 case DW_OP_breg6:
3298 case DW_OP_breg7:
3299 case DW_OP_breg8:
3300 case DW_OP_breg9:
3301 case DW_OP_breg10:
3302 case DW_OP_breg11:
3303 case DW_OP_breg12:
3304 case DW_OP_breg13:
3305 case DW_OP_breg14:
3306 case DW_OP_breg15:
3307 case DW_OP_breg16:
3308 case DW_OP_breg17:
3309 case DW_OP_breg18:
3310 case DW_OP_breg19:
3311 case DW_OP_breg20:
3312 case DW_OP_breg21:
3313 case DW_OP_breg22:
3314 case DW_OP_breg23:
3315 case DW_OP_breg24:
3316 case DW_OP_breg25:
3317 case DW_OP_breg26:
3318 case DW_OP_breg27:
3319 case DW_OP_breg28:
3320 case DW_OP_breg29:
3321 case DW_OP_breg30:
3322 case DW_OP_breg31:
3323 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3324 break;
3325 case DW_OP_regx:
3326 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3327 break;
3328 case DW_OP_fbreg:
3329 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3330 break;
3331 case DW_OP_bregx:
3332 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3333 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
3334 break;
3335 case DW_OP_piece:
3336 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3337 break;
3338 case DW_OP_deref_size:
3339 case DW_OP_xderef_size:
3340 size += 1;
3341 break;
3342 case DW_OP_call2:
3343 size += 2;
3344 break;
3345 case DW_OP_call4:
3346 size += 4;
3347 break;
3348 case DW_OP_call_ref:
3349 size += DWARF2_ADDR_SIZE;
3350 break;
3351 default:
3352 break;
3355 return size;
3358 /* Return the size of a series of location descriptors. */
3360 static unsigned long
3361 size_of_locs (dw_loc_descr_ref loc)
3363 dw_loc_descr_ref l;
3364 unsigned long size;
3366 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
3367 field, to avoid writing to a PCH file. */
3368 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3370 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
3371 break;
3372 size += size_of_loc_descr (l);
3374 if (! l)
3375 return size;
3377 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3379 l->dw_loc_addr = size;
3380 size += size_of_loc_descr (l);
3383 return size;
3386 /* Output location description stack opcode's operands (if any). */
3388 static void
3389 output_loc_operands (dw_loc_descr_ref loc)
3391 dw_val_ref val1 = &loc->dw_loc_oprnd1;
3392 dw_val_ref val2 = &loc->dw_loc_oprnd2;
3394 switch (loc->dw_loc_opc)
3396 #ifdef DWARF2_DEBUGGING_INFO
3397 case DW_OP_addr:
3398 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
3399 break;
3400 case DW_OP_const2u:
3401 case DW_OP_const2s:
3402 dw2_asm_output_data (2, val1->v.val_int, NULL);
3403 break;
3404 case DW_OP_const4u:
3405 case DW_OP_const4s:
3406 dw2_asm_output_data (4, val1->v.val_int, NULL);
3407 break;
3408 case DW_OP_const8u:
3409 case DW_OP_const8s:
3410 gcc_assert (HOST_BITS_PER_LONG >= 64);
3411 dw2_asm_output_data (8, val1->v.val_int, NULL);
3412 break;
3413 case DW_OP_skip:
3414 case DW_OP_bra:
3416 int offset;
3418 gcc_assert (val1->val_class == dw_val_class_loc);
3419 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
3421 dw2_asm_output_data (2, offset, NULL);
3423 break;
3424 #else
3425 case DW_OP_addr:
3426 case DW_OP_const2u:
3427 case DW_OP_const2s:
3428 case DW_OP_const4u:
3429 case DW_OP_const4s:
3430 case DW_OP_const8u:
3431 case DW_OP_const8s:
3432 case DW_OP_skip:
3433 case DW_OP_bra:
3434 /* We currently don't make any attempt to make sure these are
3435 aligned properly like we do for the main unwind info, so
3436 don't support emitting things larger than a byte if we're
3437 only doing unwinding. */
3438 gcc_unreachable ();
3439 #endif
3440 case DW_OP_const1u:
3441 case DW_OP_const1s:
3442 dw2_asm_output_data (1, val1->v.val_int, NULL);
3443 break;
3444 case DW_OP_constu:
3445 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3446 break;
3447 case DW_OP_consts:
3448 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3449 break;
3450 case DW_OP_pick:
3451 dw2_asm_output_data (1, val1->v.val_int, NULL);
3452 break;
3453 case DW_OP_plus_uconst:
3454 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3455 break;
3456 case DW_OP_breg0:
3457 case DW_OP_breg1:
3458 case DW_OP_breg2:
3459 case DW_OP_breg3:
3460 case DW_OP_breg4:
3461 case DW_OP_breg5:
3462 case DW_OP_breg6:
3463 case DW_OP_breg7:
3464 case DW_OP_breg8:
3465 case DW_OP_breg9:
3466 case DW_OP_breg10:
3467 case DW_OP_breg11:
3468 case DW_OP_breg12:
3469 case DW_OP_breg13:
3470 case DW_OP_breg14:
3471 case DW_OP_breg15:
3472 case DW_OP_breg16:
3473 case DW_OP_breg17:
3474 case DW_OP_breg18:
3475 case DW_OP_breg19:
3476 case DW_OP_breg20:
3477 case DW_OP_breg21:
3478 case DW_OP_breg22:
3479 case DW_OP_breg23:
3480 case DW_OP_breg24:
3481 case DW_OP_breg25:
3482 case DW_OP_breg26:
3483 case DW_OP_breg27:
3484 case DW_OP_breg28:
3485 case DW_OP_breg29:
3486 case DW_OP_breg30:
3487 case DW_OP_breg31:
3488 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3489 break;
3490 case DW_OP_regx:
3491 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3492 break;
3493 case DW_OP_fbreg:
3494 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3495 break;
3496 case DW_OP_bregx:
3497 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3498 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
3499 break;
3500 case DW_OP_piece:
3501 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3502 break;
3503 case DW_OP_deref_size:
3504 case DW_OP_xderef_size:
3505 dw2_asm_output_data (1, val1->v.val_int, NULL);
3506 break;
3508 case INTERNAL_DW_OP_tls_addr:
3509 if (targetm.asm_out.output_dwarf_dtprel)
3511 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
3512 DWARF2_ADDR_SIZE,
3513 val1->v.val_addr);
3514 fputc ('\n', asm_out_file);
3516 else
3517 gcc_unreachable ();
3518 break;
3520 default:
3521 /* Other codes have no operands. */
3522 break;
3526 /* Output a sequence of location operations. */
3528 static void
3529 output_loc_sequence (dw_loc_descr_ref loc)
3531 for (; loc != NULL; loc = loc->dw_loc_next)
3533 /* Output the opcode. */
3534 dw2_asm_output_data (1, loc->dw_loc_opc,
3535 "%s", dwarf_stack_op_name (loc->dw_loc_opc));
3537 /* Output the operand(s) (if any). */
3538 output_loc_operands (loc);
3542 /* This routine will generate the correct assembly data for a location
3543 description based on a cfi entry with a complex address. */
3545 static void
3546 output_cfa_loc (dw_cfi_ref cfi)
3548 dw_loc_descr_ref loc;
3549 unsigned long size;
3551 /* Output the size of the block. */
3552 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
3553 size = size_of_locs (loc);
3554 dw2_asm_output_data_uleb128 (size, NULL);
3556 /* Now output the operations themselves. */
3557 output_loc_sequence (loc);
3560 /* This function builds a dwarf location descriptor sequence from a
3561 dw_cfa_location, adding the given OFFSET to the result of the
3562 expression. */
3564 static struct dw_loc_descr_struct *
3565 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
3567 struct dw_loc_descr_struct *head, *tmp;
3569 offset += cfa->offset;
3571 if (cfa->indirect)
3573 if (cfa->base_offset)
3575 if (cfa->reg <= 31)
3576 head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->base_offset, 0);
3577 else
3578 head = new_loc_descr (DW_OP_bregx, cfa->reg, cfa->base_offset);
3580 else if (cfa->reg <= 31)
3581 head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3582 else
3583 head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3585 head->dw_loc_oprnd1.val_class = dw_val_class_const;
3586 tmp = new_loc_descr (DW_OP_deref, 0, 0);
3587 add_loc_descr (&head, tmp);
3588 if (offset != 0)
3590 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
3591 add_loc_descr (&head, tmp);
3594 else
3596 if (offset == 0)
3597 if (cfa->reg <= 31)
3598 head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3599 else
3600 head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3601 else if (cfa->reg <= 31)
3602 head = new_loc_descr (DW_OP_breg0 + cfa->reg, offset, 0);
3603 else
3604 head = new_loc_descr (DW_OP_bregx, cfa->reg, offset);
3607 return head;
3610 /* This function fills in aa dw_cfa_location structure from a dwarf location
3611 descriptor sequence. */
3613 static void
3614 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
3616 struct dw_loc_descr_struct *ptr;
3617 cfa->offset = 0;
3618 cfa->base_offset = 0;
3619 cfa->indirect = 0;
3620 cfa->reg = -1;
3622 for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
3624 enum dwarf_location_atom op = ptr->dw_loc_opc;
3626 switch (op)
3628 case DW_OP_reg0:
3629 case DW_OP_reg1:
3630 case DW_OP_reg2:
3631 case DW_OP_reg3:
3632 case DW_OP_reg4:
3633 case DW_OP_reg5:
3634 case DW_OP_reg6:
3635 case DW_OP_reg7:
3636 case DW_OP_reg8:
3637 case DW_OP_reg9:
3638 case DW_OP_reg10:
3639 case DW_OP_reg11:
3640 case DW_OP_reg12:
3641 case DW_OP_reg13:
3642 case DW_OP_reg14:
3643 case DW_OP_reg15:
3644 case DW_OP_reg16:
3645 case DW_OP_reg17:
3646 case DW_OP_reg18:
3647 case DW_OP_reg19:
3648 case DW_OP_reg20:
3649 case DW_OP_reg21:
3650 case DW_OP_reg22:
3651 case DW_OP_reg23:
3652 case DW_OP_reg24:
3653 case DW_OP_reg25:
3654 case DW_OP_reg26:
3655 case DW_OP_reg27:
3656 case DW_OP_reg28:
3657 case DW_OP_reg29:
3658 case DW_OP_reg30:
3659 case DW_OP_reg31:
3660 cfa->reg = op - DW_OP_reg0;
3661 break;
3662 case DW_OP_regx:
3663 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3664 break;
3665 case DW_OP_breg0:
3666 case DW_OP_breg1:
3667 case DW_OP_breg2:
3668 case DW_OP_breg3:
3669 case DW_OP_breg4:
3670 case DW_OP_breg5:
3671 case DW_OP_breg6:
3672 case DW_OP_breg7:
3673 case DW_OP_breg8:
3674 case DW_OP_breg9:
3675 case DW_OP_breg10:
3676 case DW_OP_breg11:
3677 case DW_OP_breg12:
3678 case DW_OP_breg13:
3679 case DW_OP_breg14:
3680 case DW_OP_breg15:
3681 case DW_OP_breg16:
3682 case DW_OP_breg17:
3683 case DW_OP_breg18:
3684 case DW_OP_breg19:
3685 case DW_OP_breg20:
3686 case DW_OP_breg21:
3687 case DW_OP_breg22:
3688 case DW_OP_breg23:
3689 case DW_OP_breg24:
3690 case DW_OP_breg25:
3691 case DW_OP_breg26:
3692 case DW_OP_breg27:
3693 case DW_OP_breg28:
3694 case DW_OP_breg29:
3695 case DW_OP_breg30:
3696 case DW_OP_breg31:
3697 cfa->reg = op - DW_OP_breg0;
3698 cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
3699 break;
3700 case DW_OP_bregx:
3701 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3702 cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
3703 break;
3704 case DW_OP_deref:
3705 cfa->indirect = 1;
3706 break;
3707 case DW_OP_plus_uconst:
3708 cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
3709 break;
3710 default:
3711 internal_error ("DW_LOC_OP %s not implemented",
3712 dwarf_stack_op_name (ptr->dw_loc_opc));
3716 #endif /* .debug_frame support */
3718 /* And now, the support for symbolic debugging information. */
3719 #ifdef DWARF2_DEBUGGING_INFO
3721 /* .debug_str support. */
3722 static int output_indirect_string (void **, void *);
3724 static void dwarf2out_init (const char *);
3725 static void dwarf2out_finish (const char *);
3726 static void dwarf2out_define (unsigned int, const char *);
3727 static void dwarf2out_undef (unsigned int, const char *);
3728 static void dwarf2out_start_source_file (unsigned, const char *);
3729 static void dwarf2out_end_source_file (unsigned);
3730 static void dwarf2out_begin_block (unsigned, unsigned);
3731 static void dwarf2out_end_block (unsigned, unsigned);
3732 static bool dwarf2out_ignore_block (const_tree);
3733 static void dwarf2out_global_decl (tree);
3734 static void dwarf2out_type_decl (tree, int);
3735 static void dwarf2out_imported_module_or_decl (tree, tree);
3736 static void dwarf2out_abstract_function (tree);
3737 static void dwarf2out_var_location (rtx);
3738 static void dwarf2out_begin_function (tree);
3740 /* The debug hooks structure. */
3742 const struct gcc_debug_hooks dwarf2_debug_hooks =
3744 dwarf2out_init,
3745 dwarf2out_finish,
3746 dwarf2out_define,
3747 dwarf2out_undef,
3748 dwarf2out_start_source_file,
3749 dwarf2out_end_source_file,
3750 dwarf2out_begin_block,
3751 dwarf2out_end_block,
3752 dwarf2out_ignore_block,
3753 dwarf2out_source_line,
3754 dwarf2out_begin_prologue,
3755 debug_nothing_int_charstar, /* end_prologue */
3756 dwarf2out_end_epilogue,
3757 dwarf2out_begin_function,
3758 debug_nothing_int, /* end_function */
3759 dwarf2out_decl, /* function_decl */
3760 dwarf2out_global_decl,
3761 dwarf2out_type_decl, /* type_decl */
3762 dwarf2out_imported_module_or_decl,
3763 debug_nothing_tree, /* deferred_inline_function */
3764 /* The DWARF 2 backend tries to reduce debugging bloat by not
3765 emitting the abstract description of inline functions until
3766 something tries to reference them. */
3767 dwarf2out_abstract_function, /* outlining_inline_function */
3768 debug_nothing_rtx, /* label */
3769 debug_nothing_int, /* handle_pch */
3770 dwarf2out_var_location,
3771 dwarf2out_switch_text_section,
3772 1 /* start_end_main_source_file */
3774 #endif
3776 /* NOTE: In the comments in this file, many references are made to
3777 "Debugging Information Entries". This term is abbreviated as `DIE'
3778 throughout the remainder of this file. */
3780 /* An internal representation of the DWARF output is built, and then
3781 walked to generate the DWARF debugging info. The walk of the internal
3782 representation is done after the entire program has been compiled.
3783 The types below are used to describe the internal representation. */
3785 /* Various DIE's use offsets relative to the beginning of the
3786 .debug_info section to refer to each other. */
3788 typedef long int dw_offset;
3790 /* Define typedefs here to avoid circular dependencies. */
3792 typedef struct dw_attr_struct *dw_attr_ref;
3793 typedef struct dw_line_info_struct *dw_line_info_ref;
3794 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
3795 typedef struct pubname_struct *pubname_ref;
3796 typedef struct dw_ranges_struct *dw_ranges_ref;
3797 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
3799 /* Each entry in the line_info_table maintains the file and
3800 line number associated with the label generated for that
3801 entry. The label gives the PC value associated with
3802 the line number entry. */
3804 typedef struct dw_line_info_struct GTY(())
3806 unsigned long dw_file_num;
3807 unsigned long dw_line_num;
3809 dw_line_info_entry;
3811 /* Line information for functions in separate sections; each one gets its
3812 own sequence. */
3813 typedef struct dw_separate_line_info_struct GTY(())
3815 unsigned long dw_file_num;
3816 unsigned long dw_line_num;
3817 unsigned long function;
3819 dw_separate_line_info_entry;
3821 /* Each DIE attribute has a field specifying the attribute kind,
3822 a link to the next attribute in the chain, and an attribute value.
3823 Attributes are typically linked below the DIE they modify. */
3825 typedef struct dw_attr_struct GTY(())
3827 enum dwarf_attribute dw_attr;
3828 dw_val_node dw_attr_val;
3830 dw_attr_node;
3832 DEF_VEC_O(dw_attr_node);
3833 DEF_VEC_ALLOC_O(dw_attr_node,gc);
3835 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
3836 The children of each node form a circular list linked by
3837 die_sib. die_child points to the node *before* the "first" child node. */
3839 typedef struct die_struct GTY(())
3841 enum dwarf_tag die_tag;
3842 char *die_symbol;
3843 VEC(dw_attr_node,gc) * die_attr;
3844 dw_die_ref die_parent;
3845 dw_die_ref die_child;
3846 dw_die_ref die_sib;
3847 dw_die_ref die_definition; /* ref from a specification to its definition */
3848 dw_offset die_offset;
3849 unsigned long die_abbrev;
3850 int die_mark;
3851 /* Die is used and must not be pruned as unused. */
3852 int die_perennial_p;
3853 unsigned int decl_id;
3855 die_node;
3857 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
3858 #define FOR_EACH_CHILD(die, c, expr) do { \
3859 c = die->die_child; \
3860 if (c) do { \
3861 c = c->die_sib; \
3862 expr; \
3863 } while (c != die->die_child); \
3864 } while (0)
3866 /* The pubname structure */
3868 typedef struct pubname_struct GTY(())
3870 dw_die_ref die;
3871 const char *name;
3873 pubname_entry;
3875 DEF_VEC_O(pubname_entry);
3876 DEF_VEC_ALLOC_O(pubname_entry, gc);
3878 struct dw_ranges_struct GTY(())
3880 /* If this is positive, it's a block number, otherwise it's a
3881 bitwise-negated index into dw_ranges_by_label. */
3882 int num;
3885 struct dw_ranges_by_label_struct GTY(())
3887 const char *begin;
3888 const char *end;
3891 /* The limbo die list structure. */
3892 typedef struct limbo_die_struct GTY(())
3894 dw_die_ref die;
3895 tree created_for;
3896 struct limbo_die_struct *next;
3898 limbo_die_node;
3900 /* How to start an assembler comment. */
3901 #ifndef ASM_COMMENT_START
3902 #define ASM_COMMENT_START ";#"
3903 #endif
3905 /* Define a macro which returns nonzero for a TYPE_DECL which was
3906 implicitly generated for a tagged type.
3908 Note that unlike the gcc front end (which generates a NULL named
3909 TYPE_DECL node for each complete tagged type, each array type, and
3910 each function type node created) the g++ front end generates a
3911 _named_ TYPE_DECL node for each tagged type node created.
3912 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3913 generate a DW_TAG_typedef DIE for them. */
3915 #define TYPE_DECL_IS_STUB(decl) \
3916 (DECL_NAME (decl) == NULL_TREE \
3917 || (DECL_ARTIFICIAL (decl) \
3918 && is_tagged_type (TREE_TYPE (decl)) \
3919 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3920 /* This is necessary for stub decls that \
3921 appear in nested inline functions. */ \
3922 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3923 && (decl_ultimate_origin (decl) \
3924 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3926 /* Information concerning the compilation unit's programming
3927 language, and compiler version. */
3929 /* Fixed size portion of the DWARF compilation unit header. */
3930 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3931 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
3933 /* Fixed size portion of public names info. */
3934 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3936 /* Fixed size portion of the address range info. */
3937 #define DWARF_ARANGES_HEADER_SIZE \
3938 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3939 DWARF2_ADDR_SIZE * 2) \
3940 - DWARF_INITIAL_LENGTH_SIZE)
3942 /* Size of padding portion in the address range info. It must be
3943 aligned to twice the pointer size. */
3944 #define DWARF_ARANGES_PAD_SIZE \
3945 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3946 DWARF2_ADDR_SIZE * 2) \
3947 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3949 /* Use assembler line directives if available. */
3950 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3951 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3952 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3953 #else
3954 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3955 #endif
3956 #endif
3958 /* Minimum line offset in a special line info. opcode.
3959 This value was chosen to give a reasonable range of values. */
3960 #define DWARF_LINE_BASE -10
3962 /* First special line opcode - leave room for the standard opcodes. */
3963 #define DWARF_LINE_OPCODE_BASE 10
3965 /* Range of line offsets in a special line info. opcode. */
3966 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3968 /* Flag that indicates the initial value of the is_stmt_start flag.
3969 In the present implementation, we do not mark any lines as
3970 the beginning of a source statement, because that information
3971 is not made available by the GCC front-end. */
3972 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3974 #ifdef DWARF2_DEBUGGING_INFO
3975 /* This location is used by calc_die_sizes() to keep track
3976 the offset of each DIE within the .debug_info section. */
3977 static unsigned long next_die_offset;
3978 #endif
3980 /* Record the root of the DIE's built for the current compilation unit. */
3981 static GTY(()) dw_die_ref comp_unit_die;
3983 /* A list of DIEs with a NULL parent waiting to be relocated. */
3984 static GTY(()) limbo_die_node *limbo_die_list;
3986 /* Filenames referenced by this compilation unit. */
3987 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
3989 /* A hash table of references to DIE's that describe declarations.
3990 The key is a DECL_UID() which is a unique number identifying each decl. */
3991 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
3993 /* Node of the variable location list. */
3994 struct var_loc_node GTY ((chain_next ("%h.next")))
3996 rtx GTY (()) var_loc_note;
3997 const char * GTY (()) label;
3998 const char * GTY (()) section_label;
3999 struct var_loc_node * GTY (()) next;
4002 /* Variable location list. */
4003 struct var_loc_list_def GTY (())
4005 struct var_loc_node * GTY (()) first;
4007 /* Do not mark the last element of the chained list because
4008 it is marked through the chain. */
4009 struct var_loc_node * GTY ((skip ("%h"))) last;
4011 /* DECL_UID of the variable decl. */
4012 unsigned int decl_id;
4014 typedef struct var_loc_list_def var_loc_list;
4017 /* Table of decl location linked lists. */
4018 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
4020 /* A pointer to the base of a list of references to DIE's that
4021 are uniquely identified by their tag, presence/absence of
4022 children DIE's, and list of attribute/value pairs. */
4023 static GTY((length ("abbrev_die_table_allocated")))
4024 dw_die_ref *abbrev_die_table;
4026 /* Number of elements currently allocated for abbrev_die_table. */
4027 static GTY(()) unsigned abbrev_die_table_allocated;
4029 /* Number of elements in type_die_table currently in use. */
4030 static GTY(()) unsigned abbrev_die_table_in_use;
4032 /* Size (in elements) of increments by which we may expand the
4033 abbrev_die_table. */
4034 #define ABBREV_DIE_TABLE_INCREMENT 256
4036 /* A pointer to the base of a table that contains line information
4037 for each source code line in .text in the compilation unit. */
4038 static GTY((length ("line_info_table_allocated")))
4039 dw_line_info_ref line_info_table;
4041 /* Number of elements currently allocated for line_info_table. */
4042 static GTY(()) unsigned line_info_table_allocated;
4044 /* Number of elements in line_info_table currently in use. */
4045 static GTY(()) unsigned line_info_table_in_use;
4047 /* A pointer to the base of a table that contains line information
4048 for each source code line outside of .text in the compilation unit. */
4049 static GTY ((length ("separate_line_info_table_allocated")))
4050 dw_separate_line_info_ref separate_line_info_table;
4052 /* Number of elements currently allocated for separate_line_info_table. */
4053 static GTY(()) unsigned separate_line_info_table_allocated;
4055 /* Number of elements in separate_line_info_table currently in use. */
4056 static GTY(()) unsigned separate_line_info_table_in_use;
4058 /* Size (in elements) of increments by which we may expand the
4059 line_info_table. */
4060 #define LINE_INFO_TABLE_INCREMENT 1024
4062 /* A pointer to the base of a table that contains a list of publicly
4063 accessible names. */
4064 static GTY (()) VEC (pubname_entry, gc) * pubname_table;
4066 /* A pointer to the base of a table that contains a list of publicly
4067 accessible types. */
4068 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
4070 /* Array of dies for which we should generate .debug_arange info. */
4071 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
4073 /* Number of elements currently allocated for arange_table. */
4074 static GTY(()) unsigned arange_table_allocated;
4076 /* Number of elements in arange_table currently in use. */
4077 static GTY(()) unsigned arange_table_in_use;
4079 /* Size (in elements) of increments by which we may expand the
4080 arange_table. */
4081 #define ARANGE_TABLE_INCREMENT 64
4083 /* Array of dies for which we should generate .debug_ranges info. */
4084 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
4086 /* Number of elements currently allocated for ranges_table. */
4087 static GTY(()) unsigned ranges_table_allocated;
4089 /* Number of elements in ranges_table currently in use. */
4090 static GTY(()) unsigned ranges_table_in_use;
4092 /* Array of pairs of labels referenced in ranges_table. */
4093 static GTY ((length ("ranges_by_label_allocated")))
4094 dw_ranges_by_label_ref ranges_by_label;
4096 /* Number of elements currently allocated for ranges_by_label. */
4097 static GTY(()) unsigned ranges_by_label_allocated;
4099 /* Number of elements in ranges_by_label currently in use. */
4100 static GTY(()) unsigned ranges_by_label_in_use;
4102 /* Size (in elements) of increments by which we may expand the
4103 ranges_table. */
4104 #define RANGES_TABLE_INCREMENT 64
4106 /* Whether we have location lists that need outputting */
4107 static GTY(()) bool have_location_lists;
4109 /* Unique label counter. */
4110 static GTY(()) unsigned int loclabel_num;
4112 #ifdef DWARF2_DEBUGGING_INFO
4113 /* Record whether the function being analyzed contains inlined functions. */
4114 static int current_function_has_inlines;
4115 #endif
4116 #if 0 && defined (MIPS_DEBUGGING_INFO)
4117 static int comp_unit_has_inlines;
4118 #endif
4120 /* The last file entry emitted by maybe_emit_file(). */
4121 static GTY(()) struct dwarf_file_data * last_emitted_file;
4123 /* Number of internal labels generated by gen_internal_sym(). */
4124 static GTY(()) int label_num;
4126 /* Cached result of previous call to lookup_filename. */
4127 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
4129 #ifdef DWARF2_DEBUGGING_INFO
4131 /* Offset from the "steady-state frame pointer" to the frame base,
4132 within the current function. */
4133 static HOST_WIDE_INT frame_pointer_fb_offset;
4135 /* Forward declarations for functions defined in this file. */
4137 static int is_pseudo_reg (const_rtx);
4138 static tree type_main_variant (tree);
4139 static int is_tagged_type (const_tree);
4140 static const char *dwarf_tag_name (unsigned);
4141 static const char *dwarf_attr_name (unsigned);
4142 static const char *dwarf_form_name (unsigned);
4143 static tree decl_ultimate_origin (const_tree);
4144 static tree block_ultimate_origin (const_tree);
4145 static tree decl_class_context (tree);
4146 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
4147 static inline enum dw_val_class AT_class (dw_attr_ref);
4148 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
4149 static inline unsigned AT_flag (dw_attr_ref);
4150 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
4151 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
4152 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
4153 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
4154 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
4155 unsigned long);
4156 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
4157 unsigned int, unsigned char *);
4158 static hashval_t debug_str_do_hash (const void *);
4159 static int debug_str_eq (const void *, const void *);
4160 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
4161 static inline const char *AT_string (dw_attr_ref);
4162 static int AT_string_form (dw_attr_ref);
4163 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
4164 static void add_AT_specification (dw_die_ref, dw_die_ref);
4165 static inline dw_die_ref AT_ref (dw_attr_ref);
4166 static inline int AT_ref_external (dw_attr_ref);
4167 static inline void set_AT_ref_external (dw_attr_ref, int);
4168 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
4169 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
4170 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
4171 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
4172 dw_loc_list_ref);
4173 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
4174 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
4175 static inline rtx AT_addr (dw_attr_ref);
4176 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
4177 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
4178 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
4179 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
4180 unsigned HOST_WIDE_INT);
4181 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
4182 unsigned long);
4183 static inline const char *AT_lbl (dw_attr_ref);
4184 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
4185 static const char *get_AT_low_pc (dw_die_ref);
4186 static const char *get_AT_hi_pc (dw_die_ref);
4187 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
4188 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
4189 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
4190 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
4191 static bool is_c_family (void);
4192 static bool is_cxx (void);
4193 static bool is_java (void);
4194 static bool is_fortran (void);
4195 static bool is_ada (void);
4196 static void remove_AT (dw_die_ref, enum dwarf_attribute);
4197 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
4198 static void add_child_die (dw_die_ref, dw_die_ref);
4199 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
4200 static dw_die_ref lookup_type_die (tree);
4201 static void equate_type_number_to_die (tree, dw_die_ref);
4202 static hashval_t decl_die_table_hash (const void *);
4203 static int decl_die_table_eq (const void *, const void *);
4204 static dw_die_ref lookup_decl_die (tree);
4205 static hashval_t decl_loc_table_hash (const void *);
4206 static int decl_loc_table_eq (const void *, const void *);
4207 static var_loc_list *lookup_decl_loc (const_tree);
4208 static void equate_decl_number_to_die (tree, dw_die_ref);
4209 static void add_var_loc_to_decl (tree, struct var_loc_node *);
4210 static void print_spaces (FILE *);
4211 static void print_die (dw_die_ref, FILE *);
4212 static void print_dwarf_line_table (FILE *);
4213 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
4214 static dw_die_ref pop_compile_unit (dw_die_ref);
4215 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
4216 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
4217 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
4218 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
4219 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
4220 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
4221 static int same_die_p (dw_die_ref, dw_die_ref, int *);
4222 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
4223 static void compute_section_prefix (dw_die_ref);
4224 static int is_type_die (dw_die_ref);
4225 static int is_comdat_die (dw_die_ref);
4226 static int is_symbol_die (dw_die_ref);
4227 static void assign_symbol_names (dw_die_ref);
4228 static void break_out_includes (dw_die_ref);
4229 static hashval_t htab_cu_hash (const void *);
4230 static int htab_cu_eq (const void *, const void *);
4231 static void htab_cu_del (void *);
4232 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
4233 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
4234 static void add_sibling_attributes (dw_die_ref);
4235 static void build_abbrev_table (dw_die_ref);
4236 static void output_location_lists (dw_die_ref);
4237 static int constant_size (long unsigned);
4238 static unsigned long size_of_die (dw_die_ref);
4239 static void calc_die_sizes (dw_die_ref);
4240 static void mark_dies (dw_die_ref);
4241 static void unmark_dies (dw_die_ref);
4242 static void unmark_all_dies (dw_die_ref);
4243 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
4244 static unsigned long size_of_aranges (void);
4245 static enum dwarf_form value_format (dw_attr_ref);
4246 static void output_value_format (dw_attr_ref);
4247 static void output_abbrev_section (void);
4248 static void output_die_symbol (dw_die_ref);
4249 static void output_die (dw_die_ref);
4250 static void output_compilation_unit_header (void);
4251 static void output_comp_unit (dw_die_ref, int);
4252 static const char *dwarf2_name (tree, int);
4253 static void add_pubname (tree, dw_die_ref);
4254 static void add_pubname_string (const char *, dw_die_ref);
4255 static void add_pubtype (tree, dw_die_ref);
4256 static void output_pubnames (VEC (pubname_entry,gc) *);
4257 static void add_arange (tree, dw_die_ref);
4258 static void output_aranges (void);
4259 static unsigned int add_ranges_num (int);
4260 static unsigned int add_ranges (const_tree);
4261 static unsigned int add_ranges_by_labels (const char *, const char *);
4262 static void output_ranges (void);
4263 static void output_line_info (void);
4264 static void output_file_names (void);
4265 static dw_die_ref base_type_die (tree);
4266 static int is_base_type (tree);
4267 static bool is_subrange_type (const_tree);
4268 static dw_die_ref subrange_type_die (tree, dw_die_ref);
4269 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
4270 static int type_is_enum (const_tree);
4271 static unsigned int dbx_reg_number (const_rtx);
4272 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
4273 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
4274 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
4275 enum var_init_status);
4276 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
4277 enum var_init_status);
4278 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4279 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
4280 enum var_init_status);
4281 static int is_based_loc (const_rtx);
4282 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
4283 enum var_init_status);
4284 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
4285 enum var_init_status);
4286 static dw_loc_descr_ref loc_descriptor (rtx, enum var_init_status);
4287 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
4288 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
4289 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
4290 static tree field_type (const_tree);
4291 static unsigned int simple_type_align_in_bits (const_tree);
4292 static unsigned int simple_decl_align_in_bits (const_tree);
4293 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
4294 static HOST_WIDE_INT field_byte_offset (const_tree);
4295 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
4296 dw_loc_descr_ref);
4297 static void add_data_member_location_attribute (dw_die_ref, tree);
4298 static void add_const_value_attribute (dw_die_ref, rtx);
4299 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
4300 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4301 static void insert_float (const_rtx, unsigned char *);
4302 static rtx rtl_for_decl_location (tree);
4303 static void add_location_or_const_value_attribute (dw_die_ref, tree,
4304 enum dwarf_attribute);
4305 static void tree_add_const_value_attribute (dw_die_ref, tree);
4306 static void add_name_attribute (dw_die_ref, const char *);
4307 static void add_comp_dir_attribute (dw_die_ref);
4308 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
4309 static void add_subscript_info (dw_die_ref, tree);
4310 static void add_byte_size_attribute (dw_die_ref, tree);
4311 static void add_bit_offset_attribute (dw_die_ref, tree);
4312 static void add_bit_size_attribute (dw_die_ref, tree);
4313 static void add_prototyped_attribute (dw_die_ref, tree);
4314 static void add_abstract_origin_attribute (dw_die_ref, tree);
4315 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
4316 static void add_src_coords_attributes (dw_die_ref, tree);
4317 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
4318 static void push_decl_scope (tree);
4319 static void pop_decl_scope (void);
4320 static dw_die_ref scope_die_for (tree, dw_die_ref);
4321 static inline int local_scope_p (dw_die_ref);
4322 static inline int class_or_namespace_scope_p (dw_die_ref);
4323 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
4324 static void add_calling_convention_attribute (dw_die_ref, tree);
4325 static const char *type_tag (const_tree);
4326 static tree member_declared_type (const_tree);
4327 #if 0
4328 static const char *decl_start_label (tree);
4329 #endif
4330 static void gen_array_type_die (tree, dw_die_ref);
4331 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
4332 #if 0
4333 static void gen_entry_point_die (tree, dw_die_ref);
4334 #endif
4335 static void gen_inlined_enumeration_type_die (tree, dw_die_ref);
4336 static void gen_inlined_structure_type_die (tree, dw_die_ref);
4337 static void gen_inlined_union_type_die (tree, dw_die_ref);
4338 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
4339 static dw_die_ref gen_formal_parameter_die (tree, dw_die_ref);
4340 static void gen_unspecified_parameters_die (tree, dw_die_ref);
4341 static void gen_formal_types_die (tree, dw_die_ref);
4342 static void gen_subprogram_die (tree, dw_die_ref);
4343 static void gen_variable_die (tree, dw_die_ref);
4344 static void gen_label_die (tree, dw_die_ref);
4345 static void gen_lexical_block_die (tree, dw_die_ref, int);
4346 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
4347 static void gen_field_die (tree, dw_die_ref);
4348 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
4349 static dw_die_ref gen_compile_unit_die (const char *);
4350 static void gen_inheritance_die (tree, tree, dw_die_ref);
4351 static void gen_member_die (tree, dw_die_ref);
4352 static void gen_struct_or_union_type_die (tree, dw_die_ref,
4353 enum debug_info_usage);
4354 static void gen_subroutine_type_die (tree, dw_die_ref);
4355 static void gen_typedef_die (tree, dw_die_ref);
4356 static void gen_type_die (tree, dw_die_ref);
4357 static void gen_tagged_type_instantiation_die (tree, dw_die_ref);
4358 static void gen_block_die (tree, dw_die_ref, int);
4359 static void decls_for_scope (tree, dw_die_ref, int);
4360 static int is_redundant_typedef (const_tree);
4361 static void gen_namespace_die (tree);
4362 static void gen_decl_die (tree, dw_die_ref);
4363 static dw_die_ref force_decl_die (tree);
4364 static dw_die_ref force_type_die (tree);
4365 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
4366 static void declare_in_namespace (tree, dw_die_ref);
4367 static struct dwarf_file_data * lookup_filename (const char *);
4368 static void retry_incomplete_types (void);
4369 static void gen_type_die_for_member (tree, tree, dw_die_ref);
4370 static void splice_child_die (dw_die_ref, dw_die_ref);
4371 static int file_info_cmp (const void *, const void *);
4372 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
4373 const char *, const char *, unsigned);
4374 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
4375 const char *, const char *,
4376 const char *);
4377 static void output_loc_list (dw_loc_list_ref);
4378 static char *gen_internal_sym (const char *);
4380 static void prune_unmark_dies (dw_die_ref);
4381 static void prune_unused_types_mark (dw_die_ref, int);
4382 static void prune_unused_types_walk (dw_die_ref);
4383 static void prune_unused_types_walk_attribs (dw_die_ref);
4384 static void prune_unused_types_prune (dw_die_ref);
4385 static void prune_unused_types (void);
4386 static int maybe_emit_file (struct dwarf_file_data *fd);
4388 /* Section names used to hold DWARF debugging information. */
4389 #ifndef DEBUG_INFO_SECTION
4390 #define DEBUG_INFO_SECTION ".debug_info"
4391 #endif
4392 #ifndef DEBUG_ABBREV_SECTION
4393 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
4394 #endif
4395 #ifndef DEBUG_ARANGES_SECTION
4396 #define DEBUG_ARANGES_SECTION ".debug_aranges"
4397 #endif
4398 #ifndef DEBUG_MACINFO_SECTION
4399 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
4400 #endif
4401 #ifndef DEBUG_LINE_SECTION
4402 #define DEBUG_LINE_SECTION ".debug_line"
4403 #endif
4404 #ifndef DEBUG_LOC_SECTION
4405 #define DEBUG_LOC_SECTION ".debug_loc"
4406 #endif
4407 #ifndef DEBUG_PUBNAMES_SECTION
4408 #define DEBUG_PUBNAMES_SECTION ".debug_pubnames"
4409 #endif
4410 #ifndef DEBUG_STR_SECTION
4411 #define DEBUG_STR_SECTION ".debug_str"
4412 #endif
4413 #ifndef DEBUG_RANGES_SECTION
4414 #define DEBUG_RANGES_SECTION ".debug_ranges"
4415 #endif
4417 /* Standard ELF section names for compiled code and data. */
4418 #ifndef TEXT_SECTION_NAME
4419 #define TEXT_SECTION_NAME ".text"
4420 #endif
4422 /* Section flags for .debug_str section. */
4423 #define DEBUG_STR_SECTION_FLAGS \
4424 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
4425 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
4426 : SECTION_DEBUG)
4428 /* Labels we insert at beginning sections we can reference instead of
4429 the section names themselves. */
4431 #ifndef TEXT_SECTION_LABEL
4432 #define TEXT_SECTION_LABEL "Ltext"
4433 #endif
4434 #ifndef COLD_TEXT_SECTION_LABEL
4435 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
4436 #endif
4437 #ifndef DEBUG_LINE_SECTION_LABEL
4438 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
4439 #endif
4440 #ifndef DEBUG_INFO_SECTION_LABEL
4441 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
4442 #endif
4443 #ifndef DEBUG_ABBREV_SECTION_LABEL
4444 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
4445 #endif
4446 #ifndef DEBUG_LOC_SECTION_LABEL
4447 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
4448 #endif
4449 #ifndef DEBUG_RANGES_SECTION_LABEL
4450 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
4451 #endif
4452 #ifndef DEBUG_MACINFO_SECTION_LABEL
4453 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
4454 #endif
4456 /* Definitions of defaults for formats and names of various special
4457 (artificial) labels which may be generated within this file (when the -g
4458 options is used and DWARF2_DEBUGGING_INFO is in effect.
4459 If necessary, these may be overridden from within the tm.h file, but
4460 typically, overriding these defaults is unnecessary. */
4462 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4463 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4464 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4465 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4466 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4467 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4468 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4469 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4470 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4471 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4473 #ifndef TEXT_END_LABEL
4474 #define TEXT_END_LABEL "Letext"
4475 #endif
4476 #ifndef COLD_END_LABEL
4477 #define COLD_END_LABEL "Letext_cold"
4478 #endif
4479 #ifndef BLOCK_BEGIN_LABEL
4480 #define BLOCK_BEGIN_LABEL "LBB"
4481 #endif
4482 #ifndef BLOCK_END_LABEL
4483 #define BLOCK_END_LABEL "LBE"
4484 #endif
4485 #ifndef LINE_CODE_LABEL
4486 #define LINE_CODE_LABEL "LM"
4487 #endif
4488 #ifndef SEPARATE_LINE_CODE_LABEL
4489 #define SEPARATE_LINE_CODE_LABEL "LSM"
4490 #endif
4493 /* We allow a language front-end to designate a function that is to be
4494 called to "demangle" any name before it is put into a DIE. */
4496 static const char *(*demangle_name_func) (const char *);
4498 void
4499 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4501 demangle_name_func = func;
4504 /* Test if rtl node points to a pseudo register. */
4506 static inline int
4507 is_pseudo_reg (const_rtx rtl)
4509 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4510 || (GET_CODE (rtl) == SUBREG
4511 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4514 /* Return a reference to a type, with its const and volatile qualifiers
4515 removed. */
4517 static inline tree
4518 type_main_variant (tree type)
4520 type = TYPE_MAIN_VARIANT (type);
4522 /* ??? There really should be only one main variant among any group of
4523 variants of a given type (and all of the MAIN_VARIANT values for all
4524 members of the group should point to that one type) but sometimes the C
4525 front-end messes this up for array types, so we work around that bug
4526 here. */
4527 if (TREE_CODE (type) == ARRAY_TYPE)
4528 while (type != TYPE_MAIN_VARIANT (type))
4529 type = TYPE_MAIN_VARIANT (type);
4531 return type;
4534 /* Return nonzero if the given type node represents a tagged type. */
4536 static inline int
4537 is_tagged_type (const_tree type)
4539 enum tree_code code = TREE_CODE (type);
4541 return (code == RECORD_TYPE || code == UNION_TYPE
4542 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4545 /* Convert a DIE tag into its string name. */
4547 static const char *
4548 dwarf_tag_name (unsigned int tag)
4550 switch (tag)
4552 case DW_TAG_padding:
4553 return "DW_TAG_padding";
4554 case DW_TAG_array_type:
4555 return "DW_TAG_array_type";
4556 case DW_TAG_class_type:
4557 return "DW_TAG_class_type";
4558 case DW_TAG_entry_point:
4559 return "DW_TAG_entry_point";
4560 case DW_TAG_enumeration_type:
4561 return "DW_TAG_enumeration_type";
4562 case DW_TAG_formal_parameter:
4563 return "DW_TAG_formal_parameter";
4564 case DW_TAG_imported_declaration:
4565 return "DW_TAG_imported_declaration";
4566 case DW_TAG_label:
4567 return "DW_TAG_label";
4568 case DW_TAG_lexical_block:
4569 return "DW_TAG_lexical_block";
4570 case DW_TAG_member:
4571 return "DW_TAG_member";
4572 case DW_TAG_pointer_type:
4573 return "DW_TAG_pointer_type";
4574 case DW_TAG_reference_type:
4575 return "DW_TAG_reference_type";
4576 case DW_TAG_compile_unit:
4577 return "DW_TAG_compile_unit";
4578 case DW_TAG_string_type:
4579 return "DW_TAG_string_type";
4580 case DW_TAG_structure_type:
4581 return "DW_TAG_structure_type";
4582 case DW_TAG_subroutine_type:
4583 return "DW_TAG_subroutine_type";
4584 case DW_TAG_typedef:
4585 return "DW_TAG_typedef";
4586 case DW_TAG_union_type:
4587 return "DW_TAG_union_type";
4588 case DW_TAG_unspecified_parameters:
4589 return "DW_TAG_unspecified_parameters";
4590 case DW_TAG_variant:
4591 return "DW_TAG_variant";
4592 case DW_TAG_common_block:
4593 return "DW_TAG_common_block";
4594 case DW_TAG_common_inclusion:
4595 return "DW_TAG_common_inclusion";
4596 case DW_TAG_inheritance:
4597 return "DW_TAG_inheritance";
4598 case DW_TAG_inlined_subroutine:
4599 return "DW_TAG_inlined_subroutine";
4600 case DW_TAG_module:
4601 return "DW_TAG_module";
4602 case DW_TAG_ptr_to_member_type:
4603 return "DW_TAG_ptr_to_member_type";
4604 case DW_TAG_set_type:
4605 return "DW_TAG_set_type";
4606 case DW_TAG_subrange_type:
4607 return "DW_TAG_subrange_type";
4608 case DW_TAG_with_stmt:
4609 return "DW_TAG_with_stmt";
4610 case DW_TAG_access_declaration:
4611 return "DW_TAG_access_declaration";
4612 case DW_TAG_base_type:
4613 return "DW_TAG_base_type";
4614 case DW_TAG_catch_block:
4615 return "DW_TAG_catch_block";
4616 case DW_TAG_const_type:
4617 return "DW_TAG_const_type";
4618 case DW_TAG_constant:
4619 return "DW_TAG_constant";
4620 case DW_TAG_enumerator:
4621 return "DW_TAG_enumerator";
4622 case DW_TAG_file_type:
4623 return "DW_TAG_file_type";
4624 case DW_TAG_friend:
4625 return "DW_TAG_friend";
4626 case DW_TAG_namelist:
4627 return "DW_TAG_namelist";
4628 case DW_TAG_namelist_item:
4629 return "DW_TAG_namelist_item";
4630 case DW_TAG_packed_type:
4631 return "DW_TAG_packed_type";
4632 case DW_TAG_subprogram:
4633 return "DW_TAG_subprogram";
4634 case DW_TAG_template_type_param:
4635 return "DW_TAG_template_type_param";
4636 case DW_TAG_template_value_param:
4637 return "DW_TAG_template_value_param";
4638 case DW_TAG_thrown_type:
4639 return "DW_TAG_thrown_type";
4640 case DW_TAG_try_block:
4641 return "DW_TAG_try_block";
4642 case DW_TAG_variant_part:
4643 return "DW_TAG_variant_part";
4644 case DW_TAG_variable:
4645 return "DW_TAG_variable";
4646 case DW_TAG_volatile_type:
4647 return "DW_TAG_volatile_type";
4648 case DW_TAG_dwarf_procedure:
4649 return "DW_TAG_dwarf_procedure";
4650 case DW_TAG_restrict_type:
4651 return "DW_TAG_restrict_type";
4652 case DW_TAG_interface_type:
4653 return "DW_TAG_interface_type";
4654 case DW_TAG_namespace:
4655 return "DW_TAG_namespace";
4656 case DW_TAG_imported_module:
4657 return "DW_TAG_imported_module";
4658 case DW_TAG_unspecified_type:
4659 return "DW_TAG_unspecified_type";
4660 case DW_TAG_partial_unit:
4661 return "DW_TAG_partial_unit";
4662 case DW_TAG_imported_unit:
4663 return "DW_TAG_imported_unit";
4664 case DW_TAG_condition:
4665 return "DW_TAG_condition";
4666 case DW_TAG_shared_type:
4667 return "DW_TAG_shared_type";
4668 case DW_TAG_MIPS_loop:
4669 return "DW_TAG_MIPS_loop";
4670 case DW_TAG_format_label:
4671 return "DW_TAG_format_label";
4672 case DW_TAG_function_template:
4673 return "DW_TAG_function_template";
4674 case DW_TAG_class_template:
4675 return "DW_TAG_class_template";
4676 case DW_TAG_GNU_BINCL:
4677 return "DW_TAG_GNU_BINCL";
4678 case DW_TAG_GNU_EINCL:
4679 return "DW_TAG_GNU_EINCL";
4680 default:
4681 return "DW_TAG_<unknown>";
4685 /* Convert a DWARF attribute code into its string name. */
4687 static const char *
4688 dwarf_attr_name (unsigned int attr)
4690 switch (attr)
4692 case DW_AT_sibling:
4693 return "DW_AT_sibling";
4694 case DW_AT_location:
4695 return "DW_AT_location";
4696 case DW_AT_name:
4697 return "DW_AT_name";
4698 case DW_AT_ordering:
4699 return "DW_AT_ordering";
4700 case DW_AT_subscr_data:
4701 return "DW_AT_subscr_data";
4702 case DW_AT_byte_size:
4703 return "DW_AT_byte_size";
4704 case DW_AT_bit_offset:
4705 return "DW_AT_bit_offset";
4706 case DW_AT_bit_size:
4707 return "DW_AT_bit_size";
4708 case DW_AT_element_list:
4709 return "DW_AT_element_list";
4710 case DW_AT_stmt_list:
4711 return "DW_AT_stmt_list";
4712 case DW_AT_low_pc:
4713 return "DW_AT_low_pc";
4714 case DW_AT_high_pc:
4715 return "DW_AT_high_pc";
4716 case DW_AT_language:
4717 return "DW_AT_language";
4718 case DW_AT_member:
4719 return "DW_AT_member";
4720 case DW_AT_discr:
4721 return "DW_AT_discr";
4722 case DW_AT_discr_value:
4723 return "DW_AT_discr_value";
4724 case DW_AT_visibility:
4725 return "DW_AT_visibility";
4726 case DW_AT_import:
4727 return "DW_AT_import";
4728 case DW_AT_string_length:
4729 return "DW_AT_string_length";
4730 case DW_AT_common_reference:
4731 return "DW_AT_common_reference";
4732 case DW_AT_comp_dir:
4733 return "DW_AT_comp_dir";
4734 case DW_AT_const_value:
4735 return "DW_AT_const_value";
4736 case DW_AT_containing_type:
4737 return "DW_AT_containing_type";
4738 case DW_AT_default_value:
4739 return "DW_AT_default_value";
4740 case DW_AT_inline:
4741 return "DW_AT_inline";
4742 case DW_AT_is_optional:
4743 return "DW_AT_is_optional";
4744 case DW_AT_lower_bound:
4745 return "DW_AT_lower_bound";
4746 case DW_AT_producer:
4747 return "DW_AT_producer";
4748 case DW_AT_prototyped:
4749 return "DW_AT_prototyped";
4750 case DW_AT_return_addr:
4751 return "DW_AT_return_addr";
4752 case DW_AT_start_scope:
4753 return "DW_AT_start_scope";
4754 case DW_AT_bit_stride:
4755 return "DW_AT_bit_stride";
4756 case DW_AT_upper_bound:
4757 return "DW_AT_upper_bound";
4758 case DW_AT_abstract_origin:
4759 return "DW_AT_abstract_origin";
4760 case DW_AT_accessibility:
4761 return "DW_AT_accessibility";
4762 case DW_AT_address_class:
4763 return "DW_AT_address_class";
4764 case DW_AT_artificial:
4765 return "DW_AT_artificial";
4766 case DW_AT_base_types:
4767 return "DW_AT_base_types";
4768 case DW_AT_calling_convention:
4769 return "DW_AT_calling_convention";
4770 case DW_AT_count:
4771 return "DW_AT_count";
4772 case DW_AT_data_member_location:
4773 return "DW_AT_data_member_location";
4774 case DW_AT_decl_column:
4775 return "DW_AT_decl_column";
4776 case DW_AT_decl_file:
4777 return "DW_AT_decl_file";
4778 case DW_AT_decl_line:
4779 return "DW_AT_decl_line";
4780 case DW_AT_declaration:
4781 return "DW_AT_declaration";
4782 case DW_AT_discr_list:
4783 return "DW_AT_discr_list";
4784 case DW_AT_encoding:
4785 return "DW_AT_encoding";
4786 case DW_AT_external:
4787 return "DW_AT_external";
4788 case DW_AT_frame_base:
4789 return "DW_AT_frame_base";
4790 case DW_AT_friend:
4791 return "DW_AT_friend";
4792 case DW_AT_identifier_case:
4793 return "DW_AT_identifier_case";
4794 case DW_AT_macro_info:
4795 return "DW_AT_macro_info";
4796 case DW_AT_namelist_items:
4797 return "DW_AT_namelist_items";
4798 case DW_AT_priority:
4799 return "DW_AT_priority";
4800 case DW_AT_segment:
4801 return "DW_AT_segment";
4802 case DW_AT_specification:
4803 return "DW_AT_specification";
4804 case DW_AT_static_link:
4805 return "DW_AT_static_link";
4806 case DW_AT_type:
4807 return "DW_AT_type";
4808 case DW_AT_use_location:
4809 return "DW_AT_use_location";
4810 case DW_AT_variable_parameter:
4811 return "DW_AT_variable_parameter";
4812 case DW_AT_virtuality:
4813 return "DW_AT_virtuality";
4814 case DW_AT_vtable_elem_location:
4815 return "DW_AT_vtable_elem_location";
4817 case DW_AT_allocated:
4818 return "DW_AT_allocated";
4819 case DW_AT_associated:
4820 return "DW_AT_associated";
4821 case DW_AT_data_location:
4822 return "DW_AT_data_location";
4823 case DW_AT_byte_stride:
4824 return "DW_AT_byte_stride";
4825 case DW_AT_entry_pc:
4826 return "DW_AT_entry_pc";
4827 case DW_AT_use_UTF8:
4828 return "DW_AT_use_UTF8";
4829 case DW_AT_extension:
4830 return "DW_AT_extension";
4831 case DW_AT_ranges:
4832 return "DW_AT_ranges";
4833 case DW_AT_trampoline:
4834 return "DW_AT_trampoline";
4835 case DW_AT_call_column:
4836 return "DW_AT_call_column";
4837 case DW_AT_call_file:
4838 return "DW_AT_call_file";
4839 case DW_AT_call_line:
4840 return "DW_AT_call_line";
4842 case DW_AT_MIPS_fde:
4843 return "DW_AT_MIPS_fde";
4844 case DW_AT_MIPS_loop_begin:
4845 return "DW_AT_MIPS_loop_begin";
4846 case DW_AT_MIPS_tail_loop_begin:
4847 return "DW_AT_MIPS_tail_loop_begin";
4848 case DW_AT_MIPS_epilog_begin:
4849 return "DW_AT_MIPS_epilog_begin";
4850 case DW_AT_MIPS_loop_unroll_factor:
4851 return "DW_AT_MIPS_loop_unroll_factor";
4852 case DW_AT_MIPS_software_pipeline_depth:
4853 return "DW_AT_MIPS_software_pipeline_depth";
4854 case DW_AT_MIPS_linkage_name:
4855 return "DW_AT_MIPS_linkage_name";
4856 case DW_AT_MIPS_stride:
4857 return "DW_AT_MIPS_stride";
4858 case DW_AT_MIPS_abstract_name:
4859 return "DW_AT_MIPS_abstract_name";
4860 case DW_AT_MIPS_clone_origin:
4861 return "DW_AT_MIPS_clone_origin";
4862 case DW_AT_MIPS_has_inlines:
4863 return "DW_AT_MIPS_has_inlines";
4865 case DW_AT_sf_names:
4866 return "DW_AT_sf_names";
4867 case DW_AT_src_info:
4868 return "DW_AT_src_info";
4869 case DW_AT_mac_info:
4870 return "DW_AT_mac_info";
4871 case DW_AT_src_coords:
4872 return "DW_AT_src_coords";
4873 case DW_AT_body_begin:
4874 return "DW_AT_body_begin";
4875 case DW_AT_body_end:
4876 return "DW_AT_body_end";
4877 case DW_AT_GNU_vector:
4878 return "DW_AT_GNU_vector";
4880 case DW_AT_VMS_rtnbeg_pd_address:
4881 return "DW_AT_VMS_rtnbeg_pd_address";
4883 default:
4884 return "DW_AT_<unknown>";
4888 /* Convert a DWARF value form code into its string name. */
4890 static const char *
4891 dwarf_form_name (unsigned int form)
4893 switch (form)
4895 case DW_FORM_addr:
4896 return "DW_FORM_addr";
4897 case DW_FORM_block2:
4898 return "DW_FORM_block2";
4899 case DW_FORM_block4:
4900 return "DW_FORM_block4";
4901 case DW_FORM_data2:
4902 return "DW_FORM_data2";
4903 case DW_FORM_data4:
4904 return "DW_FORM_data4";
4905 case DW_FORM_data8:
4906 return "DW_FORM_data8";
4907 case DW_FORM_string:
4908 return "DW_FORM_string";
4909 case DW_FORM_block:
4910 return "DW_FORM_block";
4911 case DW_FORM_block1:
4912 return "DW_FORM_block1";
4913 case DW_FORM_data1:
4914 return "DW_FORM_data1";
4915 case DW_FORM_flag:
4916 return "DW_FORM_flag";
4917 case DW_FORM_sdata:
4918 return "DW_FORM_sdata";
4919 case DW_FORM_strp:
4920 return "DW_FORM_strp";
4921 case DW_FORM_udata:
4922 return "DW_FORM_udata";
4923 case DW_FORM_ref_addr:
4924 return "DW_FORM_ref_addr";
4925 case DW_FORM_ref1:
4926 return "DW_FORM_ref1";
4927 case DW_FORM_ref2:
4928 return "DW_FORM_ref2";
4929 case DW_FORM_ref4:
4930 return "DW_FORM_ref4";
4931 case DW_FORM_ref8:
4932 return "DW_FORM_ref8";
4933 case DW_FORM_ref_udata:
4934 return "DW_FORM_ref_udata";
4935 case DW_FORM_indirect:
4936 return "DW_FORM_indirect";
4937 default:
4938 return "DW_FORM_<unknown>";
4942 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4943 instance of an inlined instance of a decl which is local to an inline
4944 function, so we have to trace all of the way back through the origin chain
4945 to find out what sort of node actually served as the original seed for the
4946 given block. */
4948 static tree
4949 decl_ultimate_origin (const_tree decl)
4951 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4952 return NULL_TREE;
4954 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4955 nodes in the function to point to themselves; ignore that if
4956 we're trying to output the abstract instance of this function. */
4957 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4958 return NULL_TREE;
4960 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4961 most distant ancestor, this should never happen. */
4962 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4964 return DECL_ABSTRACT_ORIGIN (decl);
4967 /* Determine the "ultimate origin" of a block. The block may be an inlined
4968 instance of an inlined instance of a block which is local to an inline
4969 function, so we have to trace all of the way back through the origin chain
4970 to find out what sort of node actually served as the original seed for the
4971 given block. */
4973 static tree
4974 block_ultimate_origin (const_tree block)
4976 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
4978 /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
4979 nodes in the function to point to themselves; ignore that if
4980 we're trying to output the abstract instance of this function. */
4981 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
4982 return NULL_TREE;
4984 if (immediate_origin == NULL_TREE)
4985 return NULL_TREE;
4986 else
4988 tree ret_val;
4989 tree lookahead = immediate_origin;
4993 ret_val = lookahead;
4994 lookahead = (TREE_CODE (ret_val) == BLOCK
4995 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
4997 while (lookahead != NULL && lookahead != ret_val);
4999 /* The block's abstract origin chain may not be the *ultimate* origin of
5000 the block. It could lead to a DECL that has an abstract origin set.
5001 If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
5002 will give us if it has one). Note that DECL's abstract origins are
5003 supposed to be the most distant ancestor (or so decl_ultimate_origin
5004 claims), so we don't need to loop following the DECL origins. */
5005 if (DECL_P (ret_val))
5006 return DECL_ORIGIN (ret_val);
5008 return ret_val;
5012 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
5013 of a virtual function may refer to a base class, so we check the 'this'
5014 parameter. */
5016 static tree
5017 decl_class_context (tree decl)
5019 tree context = NULL_TREE;
5021 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
5022 context = DECL_CONTEXT (decl);
5023 else
5024 context = TYPE_MAIN_VARIANT
5025 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
5027 if (context && !TYPE_P (context))
5028 context = NULL_TREE;
5030 return context;
5033 /* Add an attribute/value pair to a DIE. */
5035 static inline void
5036 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
5038 /* Maybe this should be an assert? */
5039 if (die == NULL)
5040 return;
5042 if (die->die_attr == NULL)
5043 die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
5044 VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
5047 static inline enum dw_val_class
5048 AT_class (dw_attr_ref a)
5050 return a->dw_attr_val.val_class;
5053 /* Add a flag value attribute to a DIE. */
5055 static inline void
5056 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
5058 dw_attr_node attr;
5060 attr.dw_attr = attr_kind;
5061 attr.dw_attr_val.val_class = dw_val_class_flag;
5062 attr.dw_attr_val.v.val_flag = flag;
5063 add_dwarf_attr (die, &attr);
5066 static inline unsigned
5067 AT_flag (dw_attr_ref a)
5069 gcc_assert (a && AT_class (a) == dw_val_class_flag);
5070 return a->dw_attr_val.v.val_flag;
5073 /* Add a signed integer attribute value to a DIE. */
5075 static inline void
5076 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
5078 dw_attr_node attr;
5080 attr.dw_attr = attr_kind;
5081 attr.dw_attr_val.val_class = dw_val_class_const;
5082 attr.dw_attr_val.v.val_int = int_val;
5083 add_dwarf_attr (die, &attr);
5086 static inline HOST_WIDE_INT
5087 AT_int (dw_attr_ref a)
5089 gcc_assert (a && AT_class (a) == dw_val_class_const);
5090 return a->dw_attr_val.v.val_int;
5093 /* Add an unsigned integer attribute value to a DIE. */
5095 static inline void
5096 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
5097 unsigned HOST_WIDE_INT unsigned_val)
5099 dw_attr_node attr;
5101 attr.dw_attr = attr_kind;
5102 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
5103 attr.dw_attr_val.v.val_unsigned = unsigned_val;
5104 add_dwarf_attr (die, &attr);
5107 static inline unsigned HOST_WIDE_INT
5108 AT_unsigned (dw_attr_ref a)
5110 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
5111 return a->dw_attr_val.v.val_unsigned;
5114 /* Add an unsigned double integer attribute value to a DIE. */
5116 static inline void
5117 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
5118 long unsigned int val_hi, long unsigned int val_low)
5120 dw_attr_node attr;
5122 attr.dw_attr = attr_kind;
5123 attr.dw_attr_val.val_class = dw_val_class_long_long;
5124 attr.dw_attr_val.v.val_long_long.hi = val_hi;
5125 attr.dw_attr_val.v.val_long_long.low = val_low;
5126 add_dwarf_attr (die, &attr);
5129 /* Add a floating point attribute value to a DIE and return it. */
5131 static inline void
5132 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
5133 unsigned int length, unsigned int elt_size, unsigned char *array)
5135 dw_attr_node attr;
5137 attr.dw_attr = attr_kind;
5138 attr.dw_attr_val.val_class = dw_val_class_vec;
5139 attr.dw_attr_val.v.val_vec.length = length;
5140 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
5141 attr.dw_attr_val.v.val_vec.array = array;
5142 add_dwarf_attr (die, &attr);
5145 /* Hash and equality functions for debug_str_hash. */
5147 static hashval_t
5148 debug_str_do_hash (const void *x)
5150 return htab_hash_string (((const struct indirect_string_node *)x)->str);
5153 static int
5154 debug_str_eq (const void *x1, const void *x2)
5156 return strcmp ((((const struct indirect_string_node *)x1)->str),
5157 (const char *)x2) == 0;
5160 /* Add a string attribute value to a DIE. */
5162 static inline void
5163 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
5165 dw_attr_node attr;
5166 struct indirect_string_node *node;
5167 void **slot;
5169 if (! debug_str_hash)
5170 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
5171 debug_str_eq, NULL);
5173 slot = htab_find_slot_with_hash (debug_str_hash, str,
5174 htab_hash_string (str), INSERT);
5175 if (*slot == NULL)
5177 node = (struct indirect_string_node *)
5178 ggc_alloc_cleared (sizeof (struct indirect_string_node));
5179 node->str = ggc_strdup (str);
5180 *slot = node;
5182 else
5183 node = (struct indirect_string_node *) *slot;
5185 node->refcount++;
5187 attr.dw_attr = attr_kind;
5188 attr.dw_attr_val.val_class = dw_val_class_str;
5189 attr.dw_attr_val.v.val_str = node;
5190 add_dwarf_attr (die, &attr);
5193 static inline const char *
5194 AT_string (dw_attr_ref a)
5196 gcc_assert (a && AT_class (a) == dw_val_class_str);
5197 return a->dw_attr_val.v.val_str->str;
5200 /* Find out whether a string should be output inline in DIE
5201 or out-of-line in .debug_str section. */
5203 static int
5204 AT_string_form (dw_attr_ref a)
5206 struct indirect_string_node *node;
5207 unsigned int len;
5208 char label[32];
5210 gcc_assert (a && AT_class (a) == dw_val_class_str);
5212 node = a->dw_attr_val.v.val_str;
5213 if (node->form)
5214 return node->form;
5216 len = strlen (node->str) + 1;
5218 /* If the string is shorter or equal to the size of the reference, it is
5219 always better to put it inline. */
5220 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
5221 return node->form = DW_FORM_string;
5223 /* If we cannot expect the linker to merge strings in .debug_str
5224 section, only put it into .debug_str if it is worth even in this
5225 single module. */
5226 if ((debug_str_section->common.flags & SECTION_MERGE) == 0
5227 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
5228 return node->form = DW_FORM_string;
5230 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
5231 ++dw2_string_counter;
5232 node->label = xstrdup (label);
5234 return node->form = DW_FORM_strp;
5237 /* Add a DIE reference attribute value to a DIE. */
5239 static inline void
5240 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
5242 dw_attr_node attr;
5244 attr.dw_attr = attr_kind;
5245 attr.dw_attr_val.val_class = dw_val_class_die_ref;
5246 attr.dw_attr_val.v.val_die_ref.die = targ_die;
5247 attr.dw_attr_val.v.val_die_ref.external = 0;
5248 add_dwarf_attr (die, &attr);
5251 /* Add an AT_specification attribute to a DIE, and also make the back
5252 pointer from the specification to the definition. */
5254 static inline void
5255 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
5257 add_AT_die_ref (die, DW_AT_specification, targ_die);
5258 gcc_assert (!targ_die->die_definition);
5259 targ_die->die_definition = die;
5262 static inline dw_die_ref
5263 AT_ref (dw_attr_ref a)
5265 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5266 return a->dw_attr_val.v.val_die_ref.die;
5269 static inline int
5270 AT_ref_external (dw_attr_ref a)
5272 if (a && AT_class (a) == dw_val_class_die_ref)
5273 return a->dw_attr_val.v.val_die_ref.external;
5275 return 0;
5278 static inline void
5279 set_AT_ref_external (dw_attr_ref a, int i)
5281 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5282 a->dw_attr_val.v.val_die_ref.external = i;
5285 /* Add an FDE reference attribute value to a DIE. */
5287 static inline void
5288 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
5290 dw_attr_node attr;
5292 attr.dw_attr = attr_kind;
5293 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
5294 attr.dw_attr_val.v.val_fde_index = targ_fde;
5295 add_dwarf_attr (die, &attr);
5298 /* Add a location description attribute value to a DIE. */
5300 static inline void
5301 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
5303 dw_attr_node attr;
5305 attr.dw_attr = attr_kind;
5306 attr.dw_attr_val.val_class = dw_val_class_loc;
5307 attr.dw_attr_val.v.val_loc = loc;
5308 add_dwarf_attr (die, &attr);
5311 static inline dw_loc_descr_ref
5312 AT_loc (dw_attr_ref a)
5314 gcc_assert (a && AT_class (a) == dw_val_class_loc);
5315 return a->dw_attr_val.v.val_loc;
5318 static inline void
5319 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
5321 dw_attr_node attr;
5323 attr.dw_attr = attr_kind;
5324 attr.dw_attr_val.val_class = dw_val_class_loc_list;
5325 attr.dw_attr_val.v.val_loc_list = loc_list;
5326 add_dwarf_attr (die, &attr);
5327 have_location_lists = true;
5330 static inline dw_loc_list_ref
5331 AT_loc_list (dw_attr_ref a)
5333 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
5334 return a->dw_attr_val.v.val_loc_list;
5337 /* Add an address constant attribute value to a DIE. */
5339 static inline void
5340 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
5342 dw_attr_node attr;
5344 attr.dw_attr = attr_kind;
5345 attr.dw_attr_val.val_class = dw_val_class_addr;
5346 attr.dw_attr_val.v.val_addr = addr;
5347 add_dwarf_attr (die, &attr);
5350 /* Get the RTX from to an address DIE attribute. */
5352 static inline rtx
5353 AT_addr (dw_attr_ref a)
5355 gcc_assert (a && AT_class (a) == dw_val_class_addr);
5356 return a->dw_attr_val.v.val_addr;
5359 /* Add a file attribute value to a DIE. */
5361 static inline void
5362 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
5363 struct dwarf_file_data *fd)
5365 dw_attr_node attr;
5367 attr.dw_attr = attr_kind;
5368 attr.dw_attr_val.val_class = dw_val_class_file;
5369 attr.dw_attr_val.v.val_file = fd;
5370 add_dwarf_attr (die, &attr);
5373 /* Get the dwarf_file_data from a file DIE attribute. */
5375 static inline struct dwarf_file_data *
5376 AT_file (dw_attr_ref a)
5378 gcc_assert (a && AT_class (a) == dw_val_class_file);
5379 return a->dw_attr_val.v.val_file;
5382 /* Add a label identifier attribute value to a DIE. */
5384 static inline void
5385 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
5387 dw_attr_node attr;
5389 attr.dw_attr = attr_kind;
5390 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
5391 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5392 add_dwarf_attr (die, &attr);
5395 /* Add a section offset attribute value to a DIE, an offset into the
5396 debug_line section. */
5398 static inline void
5399 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5400 const char *label)
5402 dw_attr_node attr;
5404 attr.dw_attr = attr_kind;
5405 attr.dw_attr_val.val_class = dw_val_class_lineptr;
5406 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5407 add_dwarf_attr (die, &attr);
5410 /* Add a section offset attribute value to a DIE, an offset into the
5411 debug_macinfo section. */
5413 static inline void
5414 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5415 const char *label)
5417 dw_attr_node attr;
5419 attr.dw_attr = attr_kind;
5420 attr.dw_attr_val.val_class = dw_val_class_macptr;
5421 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5422 add_dwarf_attr (die, &attr);
5425 /* Add an offset attribute value to a DIE. */
5427 static inline void
5428 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
5429 unsigned HOST_WIDE_INT offset)
5431 dw_attr_node attr;
5433 attr.dw_attr = attr_kind;
5434 attr.dw_attr_val.val_class = dw_val_class_offset;
5435 attr.dw_attr_val.v.val_offset = offset;
5436 add_dwarf_attr (die, &attr);
5439 /* Add an range_list attribute value to a DIE. */
5441 static void
5442 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5443 long unsigned int offset)
5445 dw_attr_node attr;
5447 attr.dw_attr = attr_kind;
5448 attr.dw_attr_val.val_class = dw_val_class_range_list;
5449 attr.dw_attr_val.v.val_offset = offset;
5450 add_dwarf_attr (die, &attr);
5453 static inline const char *
5454 AT_lbl (dw_attr_ref a)
5456 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5457 || AT_class (a) == dw_val_class_lineptr
5458 || AT_class (a) == dw_val_class_macptr));
5459 return a->dw_attr_val.v.val_lbl_id;
5462 /* Get the attribute of type attr_kind. */
5464 static dw_attr_ref
5465 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5467 dw_attr_ref a;
5468 unsigned ix;
5469 dw_die_ref spec = NULL;
5471 if (! die)
5472 return NULL;
5474 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5475 if (a->dw_attr == attr_kind)
5476 return a;
5477 else if (a->dw_attr == DW_AT_specification
5478 || a->dw_attr == DW_AT_abstract_origin)
5479 spec = AT_ref (a);
5481 if (spec)
5482 return get_AT (spec, attr_kind);
5484 return NULL;
5487 /* Return the "low pc" attribute value, typically associated with a subprogram
5488 DIE. Return null if the "low pc" attribute is either not present, or if it
5489 cannot be represented as an assembler label identifier. */
5491 static inline const char *
5492 get_AT_low_pc (dw_die_ref die)
5494 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
5496 return a ? AT_lbl (a) : NULL;
5499 /* Return the "high pc" attribute value, typically associated with a subprogram
5500 DIE. Return null if the "high pc" attribute is either not present, or if it
5501 cannot be represented as an assembler label identifier. */
5503 static inline const char *
5504 get_AT_hi_pc (dw_die_ref die)
5506 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
5508 return a ? AT_lbl (a) : NULL;
5511 /* Return the value of the string attribute designated by ATTR_KIND, or
5512 NULL if it is not present. */
5514 static inline const char *
5515 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5517 dw_attr_ref a = get_AT (die, attr_kind);
5519 return a ? AT_string (a) : NULL;
5522 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5523 if it is not present. */
5525 static inline int
5526 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5528 dw_attr_ref a = get_AT (die, attr_kind);
5530 return a ? AT_flag (a) : 0;
5533 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5534 if it is not present. */
5536 static inline unsigned
5537 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5539 dw_attr_ref a = get_AT (die, attr_kind);
5541 return a ? AT_unsigned (a) : 0;
5544 static inline dw_die_ref
5545 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5547 dw_attr_ref a = get_AT (die, attr_kind);
5549 return a ? AT_ref (a) : NULL;
5552 static inline struct dwarf_file_data *
5553 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5555 dw_attr_ref a = get_AT (die, attr_kind);
5557 return a ? AT_file (a) : NULL;
5560 /* Return TRUE if the language is C or C++. */
5562 static inline bool
5563 is_c_family (void)
5565 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5567 return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
5568 || lang == DW_LANG_C99
5569 || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
5572 /* Return TRUE if the language is C++. */
5574 static inline bool
5575 is_cxx (void)
5577 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5579 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
5582 /* Return TRUE if the language is Fortran. */
5584 static inline bool
5585 is_fortran (void)
5587 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5589 return (lang == DW_LANG_Fortran77
5590 || lang == DW_LANG_Fortran90
5591 || lang == DW_LANG_Fortran95);
5594 /* Return TRUE if the language is Java. */
5596 static inline bool
5597 is_java (void)
5599 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5601 return lang == DW_LANG_Java;
5604 /* Return TRUE if the language is Ada. */
5606 static inline bool
5607 is_ada (void)
5609 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5611 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5614 /* Remove the specified attribute if present. */
5616 static void
5617 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5619 dw_attr_ref a;
5620 unsigned ix;
5622 if (! die)
5623 return;
5625 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5626 if (a->dw_attr == attr_kind)
5628 if (AT_class (a) == dw_val_class_str)
5629 if (a->dw_attr_val.v.val_str->refcount)
5630 a->dw_attr_val.v.val_str->refcount--;
5632 /* VEC_ordered_remove should help reduce the number of abbrevs
5633 that are needed. */
5634 VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
5635 return;
5639 /* Remove CHILD from its parent. PREV must have the property that
5640 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5642 static void
5643 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5645 gcc_assert (child->die_parent == prev->die_parent);
5646 gcc_assert (prev->die_sib == child);
5647 if (prev == child)
5649 gcc_assert (child->die_parent->die_child == child);
5650 prev = NULL;
5652 else
5653 prev->die_sib = child->die_sib;
5654 if (child->die_parent->die_child == child)
5655 child->die_parent->die_child = prev;
5658 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
5659 matches TAG. */
5661 static void
5662 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5664 dw_die_ref c;
5666 c = die->die_child;
5667 if (c) do {
5668 dw_die_ref prev = c;
5669 c = c->die_sib;
5670 while (c->die_tag == tag)
5672 remove_child_with_prev (c, prev);
5673 /* Might have removed every child. */
5674 if (c == c->die_sib)
5675 return;
5676 c = c->die_sib;
5678 } while (c != die->die_child);
5681 /* Add a CHILD_DIE as the last child of DIE. */
5683 static void
5684 add_child_die (dw_die_ref die, dw_die_ref child_die)
5686 /* FIXME this should probably be an assert. */
5687 if (! die || ! child_die)
5688 return;
5689 gcc_assert (die != child_die);
5691 child_die->die_parent = die;
5692 if (die->die_child)
5694 child_die->die_sib = die->die_child->die_sib;
5695 die->die_child->die_sib = child_die;
5697 else
5698 child_die->die_sib = child_die;
5699 die->die_child = child_die;
5702 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5703 is the specification, to the end of PARENT's list of children.
5704 This is done by removing and re-adding it. */
5706 static void
5707 splice_child_die (dw_die_ref parent, dw_die_ref child)
5709 dw_die_ref p;
5711 /* We want the declaration DIE from inside the class, not the
5712 specification DIE at toplevel. */
5713 if (child->die_parent != parent)
5715 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5717 if (tmp)
5718 child = tmp;
5721 gcc_assert (child->die_parent == parent
5722 || (child->die_parent
5723 == get_AT_ref (parent, DW_AT_specification)));
5725 for (p = child->die_parent->die_child; ; p = p->die_sib)
5726 if (p->die_sib == child)
5728 remove_child_with_prev (child, p);
5729 break;
5732 add_child_die (parent, child);
5735 /* Return a pointer to a newly created DIE node. */
5737 static inline dw_die_ref
5738 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5740 dw_die_ref die = ggc_alloc_cleared (sizeof (die_node));
5742 die->die_tag = tag_value;
5744 if (parent_die != NULL)
5745 add_child_die (parent_die, die);
5746 else
5748 limbo_die_node *limbo_node;
5750 limbo_node = ggc_alloc_cleared (sizeof (limbo_die_node));
5751 limbo_node->die = die;
5752 limbo_node->created_for = t;
5753 limbo_node->next = limbo_die_list;
5754 limbo_die_list = limbo_node;
5757 return die;
5760 /* Return the DIE associated with the given type specifier. */
5762 static inline dw_die_ref
5763 lookup_type_die (tree type)
5765 return TYPE_SYMTAB_DIE (type);
5768 /* Equate a DIE to a given type specifier. */
5770 static inline void
5771 equate_type_number_to_die (tree type, dw_die_ref type_die)
5773 TYPE_SYMTAB_DIE (type) = type_die;
5776 /* Returns a hash value for X (which really is a die_struct). */
5778 static hashval_t
5779 decl_die_table_hash (const void *x)
5781 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
5784 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5786 static int
5787 decl_die_table_eq (const void *x, const void *y)
5789 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
5792 /* Return the DIE associated with a given declaration. */
5794 static inline dw_die_ref
5795 lookup_decl_die (tree decl)
5797 return htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
5800 /* Returns a hash value for X (which really is a var_loc_list). */
5802 static hashval_t
5803 decl_loc_table_hash (const void *x)
5805 return (hashval_t) ((const var_loc_list *) x)->decl_id;
5808 /* Return nonzero if decl_id of var_loc_list X is the same as
5809 UID of decl *Y. */
5811 static int
5812 decl_loc_table_eq (const void *x, const void *y)
5814 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
5817 /* Return the var_loc list associated with a given declaration. */
5819 static inline var_loc_list *
5820 lookup_decl_loc (const_tree decl)
5822 return htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5825 /* Equate a DIE to a particular declaration. */
5827 static void
5828 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5830 unsigned int decl_id = DECL_UID (decl);
5831 void **slot;
5833 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5834 *slot = decl_die;
5835 decl_die->decl_id = decl_id;
5838 /* Add a variable location node to the linked list for DECL. */
5840 static void
5841 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
5843 unsigned int decl_id = DECL_UID (decl);
5844 var_loc_list *temp;
5845 void **slot;
5847 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5848 if (*slot == NULL)
5850 temp = ggc_alloc_cleared (sizeof (var_loc_list));
5851 temp->decl_id = decl_id;
5852 *slot = temp;
5854 else
5855 temp = *slot;
5857 if (temp->last)
5859 /* If the current location is the same as the end of the list,
5860 and either both or neither of the locations is uninitialized,
5861 we have nothing to do. */
5862 if ((!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
5863 NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
5864 || ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
5865 != NOTE_VAR_LOCATION_STATUS (loc->var_loc_note))
5866 && ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
5867 == VAR_INIT_STATUS_UNINITIALIZED)
5868 || (NOTE_VAR_LOCATION_STATUS (loc->var_loc_note)
5869 == VAR_INIT_STATUS_UNINITIALIZED))))
5871 /* Add LOC to the end of list and update LAST. */
5872 temp->last->next = loc;
5873 temp->last = loc;
5876 /* Do not add empty location to the beginning of the list. */
5877 else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
5879 temp->first = loc;
5880 temp->last = loc;
5884 /* Keep track of the number of spaces used to indent the
5885 output of the debugging routines that print the structure of
5886 the DIE internal representation. */
5887 static int print_indent;
5889 /* Indent the line the number of spaces given by print_indent. */
5891 static inline void
5892 print_spaces (FILE *outfile)
5894 fprintf (outfile, "%*s", print_indent, "");
5897 /* Print the information associated with a given DIE, and its children.
5898 This routine is a debugging aid only. */
5900 static void
5901 print_die (dw_die_ref die, FILE *outfile)
5903 dw_attr_ref a;
5904 dw_die_ref c;
5905 unsigned ix;
5907 print_spaces (outfile);
5908 fprintf (outfile, "DIE %4ld: %s\n",
5909 die->die_offset, dwarf_tag_name (die->die_tag));
5910 print_spaces (outfile);
5911 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5912 fprintf (outfile, " offset: %ld\n", die->die_offset);
5914 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5916 print_spaces (outfile);
5917 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5919 switch (AT_class (a))
5921 case dw_val_class_addr:
5922 fprintf (outfile, "address");
5923 break;
5924 case dw_val_class_offset:
5925 fprintf (outfile, "offset");
5926 break;
5927 case dw_val_class_loc:
5928 fprintf (outfile, "location descriptor");
5929 break;
5930 case dw_val_class_loc_list:
5931 fprintf (outfile, "location list -> label:%s",
5932 AT_loc_list (a)->ll_symbol);
5933 break;
5934 case dw_val_class_range_list:
5935 fprintf (outfile, "range list");
5936 break;
5937 case dw_val_class_const:
5938 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5939 break;
5940 case dw_val_class_unsigned_const:
5941 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5942 break;
5943 case dw_val_class_long_long:
5944 fprintf (outfile, "constant (%lu,%lu)",
5945 a->dw_attr_val.v.val_long_long.hi,
5946 a->dw_attr_val.v.val_long_long.low);
5947 break;
5948 case dw_val_class_vec:
5949 fprintf (outfile, "floating-point or vector constant");
5950 break;
5951 case dw_val_class_flag:
5952 fprintf (outfile, "%u", AT_flag (a));
5953 break;
5954 case dw_val_class_die_ref:
5955 if (AT_ref (a) != NULL)
5957 if (AT_ref (a)->die_symbol)
5958 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
5959 else
5960 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5962 else
5963 fprintf (outfile, "die -> <null>");
5964 break;
5965 case dw_val_class_lbl_id:
5966 case dw_val_class_lineptr:
5967 case dw_val_class_macptr:
5968 fprintf (outfile, "label: %s", AT_lbl (a));
5969 break;
5970 case dw_val_class_str:
5971 if (AT_string (a) != NULL)
5972 fprintf (outfile, "\"%s\"", AT_string (a));
5973 else
5974 fprintf (outfile, "<null>");
5975 break;
5976 case dw_val_class_file:
5977 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5978 AT_file (a)->emitted_number);
5979 break;
5980 default:
5981 break;
5984 fprintf (outfile, "\n");
5987 if (die->die_child != NULL)
5989 print_indent += 4;
5990 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5991 print_indent -= 4;
5993 if (print_indent == 0)
5994 fprintf (outfile, "\n");
5997 /* Print the contents of the source code line number correspondence table.
5998 This routine is a debugging aid only. */
6000 static void
6001 print_dwarf_line_table (FILE *outfile)
6003 unsigned i;
6004 dw_line_info_ref line_info;
6006 fprintf (outfile, "\n\nDWARF source line information\n");
6007 for (i = 1; i < line_info_table_in_use; i++)
6009 line_info = &line_info_table[i];
6010 fprintf (outfile, "%5d: %4ld %6ld\n", i,
6011 line_info->dw_file_num,
6012 line_info->dw_line_num);
6015 fprintf (outfile, "\n\n");
6018 /* Print the information collected for a given DIE. */
6020 void
6021 debug_dwarf_die (dw_die_ref die)
6023 print_die (die, stderr);
6026 /* Print all DWARF information collected for the compilation unit.
6027 This routine is a debugging aid only. */
6029 void
6030 debug_dwarf (void)
6032 print_indent = 0;
6033 print_die (comp_unit_die, stderr);
6034 if (! DWARF2_ASM_LINE_DEBUG_INFO)
6035 print_dwarf_line_table (stderr);
6038 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
6039 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
6040 DIE that marks the start of the DIEs for this include file. */
6042 static dw_die_ref
6043 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
6045 const char *filename = get_AT_string (bincl_die, DW_AT_name);
6046 dw_die_ref new_unit = gen_compile_unit_die (filename);
6048 new_unit->die_sib = old_unit;
6049 return new_unit;
6052 /* Close an include-file CU and reopen the enclosing one. */
6054 static dw_die_ref
6055 pop_compile_unit (dw_die_ref old_unit)
6057 dw_die_ref new_unit = old_unit->die_sib;
6059 old_unit->die_sib = NULL;
6060 return new_unit;
6063 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6064 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6066 /* Calculate the checksum of a location expression. */
6068 static inline void
6069 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6071 CHECKSUM (loc->dw_loc_opc);
6072 CHECKSUM (loc->dw_loc_oprnd1);
6073 CHECKSUM (loc->dw_loc_oprnd2);
6076 /* Calculate the checksum of an attribute. */
6078 static void
6079 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
6081 dw_loc_descr_ref loc;
6082 rtx r;
6084 CHECKSUM (at->dw_attr);
6086 /* We don't care that this was compiled with a different compiler
6087 snapshot; if the output is the same, that's what matters. */
6088 if (at->dw_attr == DW_AT_producer)
6089 return;
6091 switch (AT_class (at))
6093 case dw_val_class_const:
6094 CHECKSUM (at->dw_attr_val.v.val_int);
6095 break;
6096 case dw_val_class_unsigned_const:
6097 CHECKSUM (at->dw_attr_val.v.val_unsigned);
6098 break;
6099 case dw_val_class_long_long:
6100 CHECKSUM (at->dw_attr_val.v.val_long_long);
6101 break;
6102 case dw_val_class_vec:
6103 CHECKSUM (at->dw_attr_val.v.val_vec);
6104 break;
6105 case dw_val_class_flag:
6106 CHECKSUM (at->dw_attr_val.v.val_flag);
6107 break;
6108 case dw_val_class_str:
6109 CHECKSUM_STRING (AT_string (at));
6110 break;
6112 case dw_val_class_addr:
6113 r = AT_addr (at);
6114 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6115 CHECKSUM_STRING (XSTR (r, 0));
6116 break;
6118 case dw_val_class_offset:
6119 CHECKSUM (at->dw_attr_val.v.val_offset);
6120 break;
6122 case dw_val_class_loc:
6123 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6124 loc_checksum (loc, ctx);
6125 break;
6127 case dw_val_class_die_ref:
6128 die_checksum (AT_ref (at), ctx, mark);
6129 break;
6131 case dw_val_class_fde_ref:
6132 case dw_val_class_lbl_id:
6133 case dw_val_class_lineptr:
6134 case dw_val_class_macptr:
6135 break;
6137 case dw_val_class_file:
6138 CHECKSUM_STRING (AT_file (at)->filename);
6139 break;
6141 default:
6142 break;
6146 /* Calculate the checksum of a DIE. */
6148 static void
6149 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6151 dw_die_ref c;
6152 dw_attr_ref a;
6153 unsigned ix;
6155 /* To avoid infinite recursion. */
6156 if (die->die_mark)
6158 CHECKSUM (die->die_mark);
6159 return;
6161 die->die_mark = ++(*mark);
6163 CHECKSUM (die->die_tag);
6165 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6166 attr_checksum (a, ctx, mark);
6168 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6171 #undef CHECKSUM
6172 #undef CHECKSUM_STRING
6174 /* Do the location expressions look same? */
6175 static inline int
6176 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6178 return loc1->dw_loc_opc == loc2->dw_loc_opc
6179 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6180 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6183 /* Do the values look the same? */
6184 static int
6185 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6187 dw_loc_descr_ref loc1, loc2;
6188 rtx r1, r2;
6190 if (v1->val_class != v2->val_class)
6191 return 0;
6193 switch (v1->val_class)
6195 case dw_val_class_const:
6196 return v1->v.val_int == v2->v.val_int;
6197 case dw_val_class_unsigned_const:
6198 return v1->v.val_unsigned == v2->v.val_unsigned;
6199 case dw_val_class_long_long:
6200 return v1->v.val_long_long.hi == v2->v.val_long_long.hi
6201 && v1->v.val_long_long.low == v2->v.val_long_long.low;
6202 case dw_val_class_vec:
6203 if (v1->v.val_vec.length != v2->v.val_vec.length
6204 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6205 return 0;
6206 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6207 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6208 return 0;
6209 return 1;
6210 case dw_val_class_flag:
6211 return v1->v.val_flag == v2->v.val_flag;
6212 case dw_val_class_str:
6213 return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6215 case dw_val_class_addr:
6216 r1 = v1->v.val_addr;
6217 r2 = v2->v.val_addr;
6218 if (GET_CODE (r1) != GET_CODE (r2))
6219 return 0;
6220 gcc_assert (GET_CODE (r1) == SYMBOL_REF);
6221 return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
6223 case dw_val_class_offset:
6224 return v1->v.val_offset == v2->v.val_offset;
6226 case dw_val_class_loc:
6227 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6228 loc1 && loc2;
6229 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6230 if (!same_loc_p (loc1, loc2, mark))
6231 return 0;
6232 return !loc1 && !loc2;
6234 case dw_val_class_die_ref:
6235 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6237 case dw_val_class_fde_ref:
6238 case dw_val_class_lbl_id:
6239 case dw_val_class_lineptr:
6240 case dw_val_class_macptr:
6241 return 1;
6243 case dw_val_class_file:
6244 return v1->v.val_file == v2->v.val_file;
6246 default:
6247 return 1;
6251 /* Do the attributes look the same? */
6253 static int
6254 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6256 if (at1->dw_attr != at2->dw_attr)
6257 return 0;
6259 /* We don't care that this was compiled with a different compiler
6260 snapshot; if the output is the same, that's what matters. */
6261 if (at1->dw_attr == DW_AT_producer)
6262 return 1;
6264 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6267 /* Do the dies look the same? */
6269 static int
6270 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6272 dw_die_ref c1, c2;
6273 dw_attr_ref a1;
6274 unsigned ix;
6276 /* To avoid infinite recursion. */
6277 if (die1->die_mark)
6278 return die1->die_mark == die2->die_mark;
6279 die1->die_mark = die2->die_mark = ++(*mark);
6281 if (die1->die_tag != die2->die_tag)
6282 return 0;
6284 if (VEC_length (dw_attr_node, die1->die_attr)
6285 != VEC_length (dw_attr_node, die2->die_attr))
6286 return 0;
6288 for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
6289 if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
6290 return 0;
6292 c1 = die1->die_child;
6293 c2 = die2->die_child;
6294 if (! c1)
6296 if (c2)
6297 return 0;
6299 else
6300 for (;;)
6302 if (!same_die_p (c1, c2, mark))
6303 return 0;
6304 c1 = c1->die_sib;
6305 c2 = c2->die_sib;
6306 if (c1 == die1->die_child)
6308 if (c2 == die2->die_child)
6309 break;
6310 else
6311 return 0;
6315 return 1;
6318 /* Do the dies look the same? Wrapper around same_die_p. */
6320 static int
6321 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6323 int mark = 0;
6324 int ret = same_die_p (die1, die2, &mark);
6326 unmark_all_dies (die1);
6327 unmark_all_dies (die2);
6329 return ret;
6332 /* The prefix to attach to symbols on DIEs in the current comdat debug
6333 info section. */
6334 static char *comdat_symbol_id;
6336 /* The index of the current symbol within the current comdat CU. */
6337 static unsigned int comdat_symbol_number;
6339 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6340 children, and set comdat_symbol_id accordingly. */
6342 static void
6343 compute_section_prefix (dw_die_ref unit_die)
6345 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6346 const char *base = die_name ? lbasename (die_name) : "anonymous";
6347 char *name = alloca (strlen (base) + 64);
6348 char *p;
6349 int i, mark;
6350 unsigned char checksum[16];
6351 struct md5_ctx ctx;
6353 /* Compute the checksum of the DIE, then append part of it as hex digits to
6354 the name filename of the unit. */
6356 md5_init_ctx (&ctx);
6357 mark = 0;
6358 die_checksum (unit_die, &ctx, &mark);
6359 unmark_all_dies (unit_die);
6360 md5_finish_ctx (&ctx, checksum);
6362 sprintf (name, "%s.", base);
6363 clean_symbol_name (name);
6365 p = name + strlen (name);
6366 for (i = 0; i < 4; i++)
6368 sprintf (p, "%.2x", checksum[i]);
6369 p += 2;
6372 comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
6373 comdat_symbol_number = 0;
6376 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6378 static int
6379 is_type_die (dw_die_ref die)
6381 switch (die->die_tag)
6383 case DW_TAG_array_type:
6384 case DW_TAG_class_type:
6385 case DW_TAG_interface_type:
6386 case DW_TAG_enumeration_type:
6387 case DW_TAG_pointer_type:
6388 case DW_TAG_reference_type:
6389 case DW_TAG_string_type:
6390 case DW_TAG_structure_type:
6391 case DW_TAG_subroutine_type:
6392 case DW_TAG_union_type:
6393 case DW_TAG_ptr_to_member_type:
6394 case DW_TAG_set_type:
6395 case DW_TAG_subrange_type:
6396 case DW_TAG_base_type:
6397 case DW_TAG_const_type:
6398 case DW_TAG_file_type:
6399 case DW_TAG_packed_type:
6400 case DW_TAG_volatile_type:
6401 case DW_TAG_typedef:
6402 return 1;
6403 default:
6404 return 0;
6408 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6409 Basically, we want to choose the bits that are likely to be shared between
6410 compilations (types) and leave out the bits that are specific to individual
6411 compilations (functions). */
6413 static int
6414 is_comdat_die (dw_die_ref c)
6416 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6417 we do for stabs. The advantage is a greater likelihood of sharing between
6418 objects that don't include headers in the same order (and therefore would
6419 put the base types in a different comdat). jason 8/28/00 */
6421 if (c->die_tag == DW_TAG_base_type)
6422 return 0;
6424 if (c->die_tag == DW_TAG_pointer_type
6425 || c->die_tag == DW_TAG_reference_type
6426 || c->die_tag == DW_TAG_const_type
6427 || c->die_tag == DW_TAG_volatile_type)
6429 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6431 return t ? is_comdat_die (t) : 0;
6434 return is_type_die (c);
6437 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6438 compilation unit. */
6440 static int
6441 is_symbol_die (dw_die_ref c)
6443 return (is_type_die (c)
6444 || (get_AT (c, DW_AT_declaration)
6445 && !get_AT (c, DW_AT_specification))
6446 || c->die_tag == DW_TAG_namespace);
6449 static char *
6450 gen_internal_sym (const char *prefix)
6452 char buf[256];
6454 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6455 return xstrdup (buf);
6458 /* Assign symbols to all worthy DIEs under DIE. */
6460 static void
6461 assign_symbol_names (dw_die_ref die)
6463 dw_die_ref c;
6465 if (is_symbol_die (die))
6467 if (comdat_symbol_id)
6469 char *p = alloca (strlen (comdat_symbol_id) + 64);
6471 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6472 comdat_symbol_id, comdat_symbol_number++);
6473 die->die_symbol = xstrdup (p);
6475 else
6476 die->die_symbol = gen_internal_sym ("LDIE");
6479 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6482 struct cu_hash_table_entry
6484 dw_die_ref cu;
6485 unsigned min_comdat_num, max_comdat_num;
6486 struct cu_hash_table_entry *next;
6489 /* Routines to manipulate hash table of CUs. */
6490 static hashval_t
6491 htab_cu_hash (const void *of)
6493 const struct cu_hash_table_entry *entry = of;
6495 return htab_hash_string (entry->cu->die_symbol);
6498 static int
6499 htab_cu_eq (const void *of1, const void *of2)
6501 const struct cu_hash_table_entry *entry1 = of1;
6502 const struct die_struct *entry2 = of2;
6504 return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
6507 static void
6508 htab_cu_del (void *what)
6510 struct cu_hash_table_entry *next, *entry = what;
6512 while (entry)
6514 next = entry->next;
6515 free (entry);
6516 entry = next;
6520 /* Check whether we have already seen this CU and set up SYM_NUM
6521 accordingly. */
6522 static int
6523 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6525 struct cu_hash_table_entry dummy;
6526 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6528 dummy.max_comdat_num = 0;
6530 slot = (struct cu_hash_table_entry **)
6531 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6532 INSERT);
6533 entry = *slot;
6535 for (; entry; last = entry, entry = entry->next)
6537 if (same_die_p_wrap (cu, entry->cu))
6538 break;
6541 if (entry)
6543 *sym_num = entry->min_comdat_num;
6544 return 1;
6547 entry = XCNEW (struct cu_hash_table_entry);
6548 entry->cu = cu;
6549 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6550 entry->next = *slot;
6551 *slot = entry;
6553 return 0;
6556 /* Record SYM_NUM to record of CU in HTABLE. */
6557 static void
6558 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6560 struct cu_hash_table_entry **slot, *entry;
6562 slot = (struct cu_hash_table_entry **)
6563 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6564 NO_INSERT);
6565 entry = *slot;
6567 entry->max_comdat_num = sym_num;
6570 /* Traverse the DIE (which is always comp_unit_die), and set up
6571 additional compilation units for each of the include files we see
6572 bracketed by BINCL/EINCL. */
6574 static void
6575 break_out_includes (dw_die_ref die)
6577 dw_die_ref c;
6578 dw_die_ref unit = NULL;
6579 limbo_die_node *node, **pnode;
6580 htab_t cu_hash_table;
6582 c = die->die_child;
6583 if (c) do {
6584 dw_die_ref prev = c;
6585 c = c->die_sib;
6586 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6587 || (unit && is_comdat_die (c)))
6589 dw_die_ref next = c->die_sib;
6591 /* This DIE is for a secondary CU; remove it from the main one. */
6592 remove_child_with_prev (c, prev);
6594 if (c->die_tag == DW_TAG_GNU_BINCL)
6595 unit = push_new_compile_unit (unit, c);
6596 else if (c->die_tag == DW_TAG_GNU_EINCL)
6597 unit = pop_compile_unit (unit);
6598 else
6599 add_child_die (unit, c);
6600 c = next;
6601 if (c == die->die_child)
6602 break;
6604 } while (c != die->die_child);
6606 #if 0
6607 /* We can only use this in debugging, since the frontend doesn't check
6608 to make sure that we leave every include file we enter. */
6609 gcc_assert (!unit);
6610 #endif
6612 assign_symbol_names (die);
6613 cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6614 for (node = limbo_die_list, pnode = &limbo_die_list;
6615 node;
6616 node = node->next)
6618 int is_dupl;
6620 compute_section_prefix (node->die);
6621 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6622 &comdat_symbol_number);
6623 assign_symbol_names (node->die);
6624 if (is_dupl)
6625 *pnode = node->next;
6626 else
6628 pnode = &node->next;
6629 record_comdat_symbol_number (node->die, cu_hash_table,
6630 comdat_symbol_number);
6633 htab_delete (cu_hash_table);
6636 /* Traverse the DIE and add a sibling attribute if it may have the
6637 effect of speeding up access to siblings. To save some space,
6638 avoid generating sibling attributes for DIE's without children. */
6640 static void
6641 add_sibling_attributes (dw_die_ref die)
6643 dw_die_ref c;
6645 if (! die->die_child)
6646 return;
6648 if (die->die_parent && die != die->die_parent->die_child)
6649 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
6651 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
6654 /* Output all location lists for the DIE and its children. */
6656 static void
6657 output_location_lists (dw_die_ref die)
6659 dw_die_ref c;
6660 dw_attr_ref a;
6661 unsigned ix;
6663 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6664 if (AT_class (a) == dw_val_class_loc_list)
6665 output_loc_list (AT_loc_list (a));
6667 FOR_EACH_CHILD (die, c, output_location_lists (c));
6670 /* The format of each DIE (and its attribute value pairs) is encoded in an
6671 abbreviation table. This routine builds the abbreviation table and assigns
6672 a unique abbreviation id for each abbreviation entry. The children of each
6673 die are visited recursively. */
6675 static void
6676 build_abbrev_table (dw_die_ref die)
6678 unsigned long abbrev_id;
6679 unsigned int n_alloc;
6680 dw_die_ref c;
6681 dw_attr_ref a;
6682 unsigned ix;
6684 /* Scan the DIE references, and mark as external any that refer to
6685 DIEs from other CUs (i.e. those which are not marked). */
6686 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6687 if (AT_class (a) == dw_val_class_die_ref
6688 && AT_ref (a)->die_mark == 0)
6690 gcc_assert (AT_ref (a)->die_symbol);
6692 set_AT_ref_external (a, 1);
6695 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6697 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6698 dw_attr_ref die_a, abbrev_a;
6699 unsigned ix;
6700 bool ok = true;
6702 if (abbrev->die_tag != die->die_tag)
6703 continue;
6704 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
6705 continue;
6707 if (VEC_length (dw_attr_node, abbrev->die_attr)
6708 != VEC_length (dw_attr_node, die->die_attr))
6709 continue;
6711 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
6713 abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
6714 if ((abbrev_a->dw_attr != die_a->dw_attr)
6715 || (value_format (abbrev_a) != value_format (die_a)))
6717 ok = false;
6718 break;
6721 if (ok)
6722 break;
6725 if (abbrev_id >= abbrev_die_table_in_use)
6727 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
6729 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
6730 abbrev_die_table = ggc_realloc (abbrev_die_table,
6731 sizeof (dw_die_ref) * n_alloc);
6733 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
6734 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
6735 abbrev_die_table_allocated = n_alloc;
6738 ++abbrev_die_table_in_use;
6739 abbrev_die_table[abbrev_id] = die;
6742 die->die_abbrev = abbrev_id;
6743 FOR_EACH_CHILD (die, c, build_abbrev_table (c));
6746 /* Return the power-of-two number of bytes necessary to represent VALUE. */
6748 static int
6749 constant_size (long unsigned int value)
6751 int log;
6753 if (value == 0)
6754 log = 0;
6755 else
6756 log = floor_log2 (value);
6758 log = log / 8;
6759 log = 1 << (floor_log2 (log) + 1);
6761 return log;
6764 /* Return the size of a DIE as it is represented in the
6765 .debug_info section. */
6767 static unsigned long
6768 size_of_die (dw_die_ref die)
6770 unsigned long size = 0;
6771 dw_attr_ref a;
6772 unsigned ix;
6774 size += size_of_uleb128 (die->die_abbrev);
6775 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6777 switch (AT_class (a))
6779 case dw_val_class_addr:
6780 size += DWARF2_ADDR_SIZE;
6781 break;
6782 case dw_val_class_offset:
6783 size += DWARF_OFFSET_SIZE;
6784 break;
6785 case dw_val_class_loc:
6787 unsigned long lsize = size_of_locs (AT_loc (a));
6789 /* Block length. */
6790 size += constant_size (lsize);
6791 size += lsize;
6793 break;
6794 case dw_val_class_loc_list:
6795 size += DWARF_OFFSET_SIZE;
6796 break;
6797 case dw_val_class_range_list:
6798 size += DWARF_OFFSET_SIZE;
6799 break;
6800 case dw_val_class_const:
6801 size += size_of_sleb128 (AT_int (a));
6802 break;
6803 case dw_val_class_unsigned_const:
6804 size += constant_size (AT_unsigned (a));
6805 break;
6806 case dw_val_class_long_long:
6807 size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
6808 break;
6809 case dw_val_class_vec:
6810 size += 1 + (a->dw_attr_val.v.val_vec.length
6811 * a->dw_attr_val.v.val_vec.elt_size); /* block */
6812 break;
6813 case dw_val_class_flag:
6814 size += 1;
6815 break;
6816 case dw_val_class_die_ref:
6817 if (AT_ref_external (a))
6818 size += DWARF2_ADDR_SIZE;
6819 else
6820 size += DWARF_OFFSET_SIZE;
6821 break;
6822 case dw_val_class_fde_ref:
6823 size += DWARF_OFFSET_SIZE;
6824 break;
6825 case dw_val_class_lbl_id:
6826 size += DWARF2_ADDR_SIZE;
6827 break;
6828 case dw_val_class_lineptr:
6829 case dw_val_class_macptr:
6830 size += DWARF_OFFSET_SIZE;
6831 break;
6832 case dw_val_class_str:
6833 if (AT_string_form (a) == DW_FORM_strp)
6834 size += DWARF_OFFSET_SIZE;
6835 else
6836 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
6837 break;
6838 case dw_val_class_file:
6839 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
6840 break;
6841 default:
6842 gcc_unreachable ();
6846 return size;
6849 /* Size the debugging information associated with a given DIE. Visits the
6850 DIE's children recursively. Updates the global variable next_die_offset, on
6851 each time through. Uses the current value of next_die_offset to update the
6852 die_offset field in each DIE. */
6854 static void
6855 calc_die_sizes (dw_die_ref die)
6857 dw_die_ref c;
6859 die->die_offset = next_die_offset;
6860 next_die_offset += size_of_die (die);
6862 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
6864 if (die->die_child != NULL)
6865 /* Count the null byte used to terminate sibling lists. */
6866 next_die_offset += 1;
6869 /* Set the marks for a die and its children. We do this so
6870 that we know whether or not a reference needs to use FORM_ref_addr; only
6871 DIEs in the same CU will be marked. We used to clear out the offset
6872 and use that as the flag, but ran into ordering problems. */
6874 static void
6875 mark_dies (dw_die_ref die)
6877 dw_die_ref c;
6879 gcc_assert (!die->die_mark);
6881 die->die_mark = 1;
6882 FOR_EACH_CHILD (die, c, mark_dies (c));
6885 /* Clear the marks for a die and its children. */
6887 static void
6888 unmark_dies (dw_die_ref die)
6890 dw_die_ref c;
6892 gcc_assert (die->die_mark);
6894 die->die_mark = 0;
6895 FOR_EACH_CHILD (die, c, unmark_dies (c));
6898 /* Clear the marks for a die, its children and referred dies. */
6900 static void
6901 unmark_all_dies (dw_die_ref die)
6903 dw_die_ref c;
6904 dw_attr_ref a;
6905 unsigned ix;
6907 if (!die->die_mark)
6908 return;
6909 die->die_mark = 0;
6911 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
6913 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6914 if (AT_class (a) == dw_val_class_die_ref)
6915 unmark_all_dies (AT_ref (a));
6918 /* Return the size of the .debug_pubnames or .debug_pubtypes table
6919 generated for the compilation unit. */
6921 static unsigned long
6922 size_of_pubnames (VEC (pubname_entry, gc) * names)
6924 unsigned long size;
6925 unsigned i;
6926 pubname_ref p;
6928 size = DWARF_PUBNAMES_HEADER_SIZE;
6929 for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
6930 if (names != pubtype_table
6931 || p->die->die_offset != 0
6932 || !flag_eliminate_unused_debug_types)
6933 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
6935 size += DWARF_OFFSET_SIZE;
6936 return size;
6939 /* Return the size of the information in the .debug_aranges section. */
6941 static unsigned long
6942 size_of_aranges (void)
6944 unsigned long size;
6946 size = DWARF_ARANGES_HEADER_SIZE;
6948 /* Count the address/length pair for this compilation unit. */
6949 if (text_section_used)
6950 size += 2 * DWARF2_ADDR_SIZE;
6951 if (cold_text_section_used)
6952 size += 2 * DWARF2_ADDR_SIZE;
6953 size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
6955 /* Count the two zero words used to terminated the address range table. */
6956 size += 2 * DWARF2_ADDR_SIZE;
6957 return size;
6960 /* Select the encoding of an attribute value. */
6962 static enum dwarf_form
6963 value_format (dw_attr_ref a)
6965 switch (a->dw_attr_val.val_class)
6967 case dw_val_class_addr:
6968 return DW_FORM_addr;
6969 case dw_val_class_range_list:
6970 case dw_val_class_offset:
6971 case dw_val_class_loc_list:
6972 switch (DWARF_OFFSET_SIZE)
6974 case 4:
6975 return DW_FORM_data4;
6976 case 8:
6977 return DW_FORM_data8;
6978 default:
6979 gcc_unreachable ();
6981 case dw_val_class_loc:
6982 switch (constant_size (size_of_locs (AT_loc (a))))
6984 case 1:
6985 return DW_FORM_block1;
6986 case 2:
6987 return DW_FORM_block2;
6988 default:
6989 gcc_unreachable ();
6991 case dw_val_class_const:
6992 return DW_FORM_sdata;
6993 case dw_val_class_unsigned_const:
6994 switch (constant_size (AT_unsigned (a)))
6996 case 1:
6997 return DW_FORM_data1;
6998 case 2:
6999 return DW_FORM_data2;
7000 case 4:
7001 return DW_FORM_data4;
7002 case 8:
7003 return DW_FORM_data8;
7004 default:
7005 gcc_unreachable ();
7007 case dw_val_class_long_long:
7008 return DW_FORM_block1;
7009 case dw_val_class_vec:
7010 return DW_FORM_block1;
7011 case dw_val_class_flag:
7012 return DW_FORM_flag;
7013 case dw_val_class_die_ref:
7014 if (AT_ref_external (a))
7015 return DW_FORM_ref_addr;
7016 else
7017 return DW_FORM_ref;
7018 case dw_val_class_fde_ref:
7019 return DW_FORM_data;
7020 case dw_val_class_lbl_id:
7021 return DW_FORM_addr;
7022 case dw_val_class_lineptr:
7023 case dw_val_class_macptr:
7024 return DW_FORM_data;
7025 case dw_val_class_str:
7026 return AT_string_form (a);
7027 case dw_val_class_file:
7028 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
7030 case 1:
7031 return DW_FORM_data1;
7032 case 2:
7033 return DW_FORM_data2;
7034 case 4:
7035 return DW_FORM_data4;
7036 default:
7037 gcc_unreachable ();
7040 default:
7041 gcc_unreachable ();
7045 /* Output the encoding of an attribute value. */
7047 static void
7048 output_value_format (dw_attr_ref a)
7050 enum dwarf_form form = value_format (a);
7052 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
7055 /* Output the .debug_abbrev section which defines the DIE abbreviation
7056 table. */
7058 static void
7059 output_abbrev_section (void)
7061 unsigned long abbrev_id;
7063 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7065 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7066 unsigned ix;
7067 dw_attr_ref a_attr;
7069 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
7070 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
7071 dwarf_tag_name (abbrev->die_tag));
7073 if (abbrev->die_child != NULL)
7074 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
7075 else
7076 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
7078 for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
7079 ix++)
7081 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
7082 dwarf_attr_name (a_attr->dw_attr));
7083 output_value_format (a_attr);
7086 dw2_asm_output_data (1, 0, NULL);
7087 dw2_asm_output_data (1, 0, NULL);
7090 /* Terminate the table. */
7091 dw2_asm_output_data (1, 0, NULL);
7094 /* Output a symbol we can use to refer to this DIE from another CU. */
7096 static inline void
7097 output_die_symbol (dw_die_ref die)
7099 char *sym = die->die_symbol;
7101 if (sym == 0)
7102 return;
7104 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
7105 /* We make these global, not weak; if the target doesn't support
7106 .linkonce, it doesn't support combining the sections, so debugging
7107 will break. */
7108 targetm.asm_out.globalize_label (asm_out_file, sym);
7110 ASM_OUTPUT_LABEL (asm_out_file, sym);
7113 /* Return a new location list, given the begin and end range, and the
7114 expression. gensym tells us whether to generate a new internal symbol for
7115 this location list node, which is done for the head of the list only. */
7117 static inline dw_loc_list_ref
7118 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
7119 const char *section, unsigned int gensym)
7121 dw_loc_list_ref retlist = ggc_alloc_cleared (sizeof (dw_loc_list_node));
7123 retlist->begin = begin;
7124 retlist->end = end;
7125 retlist->expr = expr;
7126 retlist->section = section;
7127 if (gensym)
7128 retlist->ll_symbol = gen_internal_sym ("LLST");
7130 return retlist;
7133 /* Add a location description expression to a location list. */
7135 static inline void
7136 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
7137 const char *begin, const char *end,
7138 const char *section)
7140 dw_loc_list_ref *d;
7142 /* Find the end of the chain. */
7143 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
7146 /* Add a new location list node to the list. */
7147 *d = new_loc_list (descr, begin, end, section, 0);
7150 /* Output the location list given to us. */
7152 static void
7153 output_loc_list (dw_loc_list_ref list_head)
7155 dw_loc_list_ref curr = list_head;
7157 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
7159 /* Walk the location list, and output each range + expression. */
7160 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
7162 unsigned long size;
7163 /* Don't output an entry that starts and ends at the same address. */
7164 if (strcmp (curr->begin, curr->end) == 0)
7165 continue;
7166 if (!have_multiple_function_sections)
7168 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
7169 "Location list begin address (%s)",
7170 list_head->ll_symbol);
7171 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
7172 "Location list end address (%s)",
7173 list_head->ll_symbol);
7175 else
7177 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
7178 "Location list begin address (%s)",
7179 list_head->ll_symbol);
7180 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
7181 "Location list end address (%s)",
7182 list_head->ll_symbol);
7184 size = size_of_locs (curr->expr);
7186 /* Output the block length for this list of location operations. */
7187 gcc_assert (size <= 0xffff);
7188 dw2_asm_output_data (2, size, "%s", "Location expression size");
7190 output_loc_sequence (curr->expr);
7193 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7194 "Location list terminator begin (%s)",
7195 list_head->ll_symbol);
7196 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7197 "Location list terminator end (%s)",
7198 list_head->ll_symbol);
7201 /* Output the DIE and its attributes. Called recursively to generate
7202 the definitions of each child DIE. */
7204 static void
7205 output_die (dw_die_ref die)
7207 dw_attr_ref a;
7208 dw_die_ref c;
7209 unsigned long size;
7210 unsigned ix;
7212 /* If someone in another CU might refer to us, set up a symbol for
7213 them to point to. */
7214 if (die->die_symbol)
7215 output_die_symbol (die);
7217 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
7218 (unsigned long)die->die_offset,
7219 dwarf_tag_name (die->die_tag));
7221 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7223 const char *name = dwarf_attr_name (a->dw_attr);
7225 switch (AT_class (a))
7227 case dw_val_class_addr:
7228 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
7229 break;
7231 case dw_val_class_offset:
7232 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
7233 "%s", name);
7234 break;
7236 case dw_val_class_range_list:
7238 char *p = strchr (ranges_section_label, '\0');
7240 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
7241 a->dw_attr_val.v.val_offset);
7242 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
7243 debug_ranges_section, "%s", name);
7244 *p = '\0';
7246 break;
7248 case dw_val_class_loc:
7249 size = size_of_locs (AT_loc (a));
7251 /* Output the block length for this list of location operations. */
7252 dw2_asm_output_data (constant_size (size), size, "%s", name);
7254 output_loc_sequence (AT_loc (a));
7255 break;
7257 case dw_val_class_const:
7258 /* ??? It would be slightly more efficient to use a scheme like is
7259 used for unsigned constants below, but gdb 4.x does not sign
7260 extend. Gdb 5.x does sign extend. */
7261 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
7262 break;
7264 case dw_val_class_unsigned_const:
7265 dw2_asm_output_data (constant_size (AT_unsigned (a)),
7266 AT_unsigned (a), "%s", name);
7267 break;
7269 case dw_val_class_long_long:
7271 unsigned HOST_WIDE_INT first, second;
7273 dw2_asm_output_data (1,
7274 2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7275 "%s", name);
7277 if (WORDS_BIG_ENDIAN)
7279 first = a->dw_attr_val.v.val_long_long.hi;
7280 second = a->dw_attr_val.v.val_long_long.low;
7282 else
7284 first = a->dw_attr_val.v.val_long_long.low;
7285 second = a->dw_attr_val.v.val_long_long.hi;
7288 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7289 first, "long long constant");
7290 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7291 second, NULL);
7293 break;
7295 case dw_val_class_vec:
7297 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
7298 unsigned int len = a->dw_attr_val.v.val_vec.length;
7299 unsigned int i;
7300 unsigned char *p;
7302 dw2_asm_output_data (1, len * elt_size, "%s", name);
7303 if (elt_size > sizeof (HOST_WIDE_INT))
7305 elt_size /= 2;
7306 len *= 2;
7308 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
7309 i < len;
7310 i++, p += elt_size)
7311 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
7312 "fp or vector constant word %u", i);
7313 break;
7316 case dw_val_class_flag:
7317 dw2_asm_output_data (1, AT_flag (a), "%s", name);
7318 break;
7320 case dw_val_class_loc_list:
7322 char *sym = AT_loc_list (a)->ll_symbol;
7324 gcc_assert (sym);
7325 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
7326 "%s", name);
7328 break;
7330 case dw_val_class_die_ref:
7331 if (AT_ref_external (a))
7333 char *sym = AT_ref (a)->die_symbol;
7335 gcc_assert (sym);
7336 dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, debug_info_section,
7337 "%s", name);
7339 else
7341 gcc_assert (AT_ref (a)->die_offset);
7342 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
7343 "%s", name);
7345 break;
7347 case dw_val_class_fde_ref:
7349 char l1[20];
7351 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
7352 a->dw_attr_val.v.val_fde_index * 2);
7353 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
7354 "%s", name);
7356 break;
7358 case dw_val_class_lbl_id:
7359 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
7360 break;
7362 case dw_val_class_lineptr:
7363 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7364 debug_line_section, "%s", name);
7365 break;
7367 case dw_val_class_macptr:
7368 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7369 debug_macinfo_section, "%s", name);
7370 break;
7372 case dw_val_class_str:
7373 if (AT_string_form (a) == DW_FORM_strp)
7374 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
7375 a->dw_attr_val.v.val_str->label,
7376 debug_str_section,
7377 "%s: \"%s\"", name, AT_string (a));
7378 else
7379 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
7380 break;
7382 case dw_val_class_file:
7384 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
7386 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
7387 a->dw_attr_val.v.val_file->filename);
7388 break;
7391 default:
7392 gcc_unreachable ();
7396 FOR_EACH_CHILD (die, c, output_die (c));
7398 /* Add null byte to terminate sibling list. */
7399 if (die->die_child != NULL)
7400 dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
7401 (unsigned long) die->die_offset);
7404 /* Output the compilation unit that appears at the beginning of the
7405 .debug_info section, and precedes the DIE descriptions. */
7407 static void
7408 output_compilation_unit_header (void)
7410 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7411 dw2_asm_output_data (4, 0xffffffff,
7412 "Initial length escape value indicating 64-bit DWARF extension");
7413 dw2_asm_output_data (DWARF_OFFSET_SIZE,
7414 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
7415 "Length of Compilation Unit Info");
7416 dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
7417 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
7418 debug_abbrev_section,
7419 "Offset Into Abbrev. Section");
7420 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
7423 /* Output the compilation unit DIE and its children. */
7425 static void
7426 output_comp_unit (dw_die_ref die, int output_if_empty)
7428 const char *secname;
7429 char *oldsym, *tmp;
7431 /* Unless we are outputting main CU, we may throw away empty ones. */
7432 if (!output_if_empty && die->die_child == NULL)
7433 return;
7435 /* Even if there are no children of this DIE, we must output the information
7436 about the compilation unit. Otherwise, on an empty translation unit, we
7437 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
7438 will then complain when examining the file. First mark all the DIEs in
7439 this CU so we know which get local refs. */
7440 mark_dies (die);
7442 build_abbrev_table (die);
7444 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
7445 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7446 calc_die_sizes (die);
7448 oldsym = die->die_symbol;
7449 if (oldsym)
7451 tmp = alloca (strlen (oldsym) + 24);
7453 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
7454 secname = tmp;
7455 die->die_symbol = NULL;
7456 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
7458 else
7459 switch_to_section (debug_info_section);
7461 /* Output debugging information. */
7462 output_compilation_unit_header ();
7463 output_die (die);
7465 /* Leave the marks on the main CU, so we can check them in
7466 output_pubnames. */
7467 if (oldsym)
7469 unmark_dies (die);
7470 die->die_symbol = oldsym;
7474 /* Return the DWARF2/3 pubname associated with a decl. */
7476 static const char *
7477 dwarf2_name (tree decl, int scope)
7479 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
7482 /* Add a new entry to .debug_pubnames if appropriate. */
7484 static void
7485 add_pubname_string (const char *str, dw_die_ref die)
7487 pubname_entry e;
7489 e.die = die;
7490 e.name = xstrdup (str);
7491 VEC_safe_push (pubname_entry, gc, pubname_table, &e);
7494 static void
7495 add_pubname (tree decl, dw_die_ref die)
7498 if (TREE_PUBLIC (decl))
7499 add_pubname_string (dwarf2_name (decl, 1), die);
7502 /* Add a new entry to .debug_pubtypes if appropriate. */
7504 static void
7505 add_pubtype (tree decl, dw_die_ref die)
7507 pubname_entry e;
7509 e.name = NULL;
7510 if ((TREE_PUBLIC (decl)
7511 || die->die_parent == comp_unit_die)
7512 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
7514 e.die = die;
7515 if (TYPE_P (decl))
7517 if (TYPE_NAME (decl))
7519 if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
7520 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
7521 else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
7522 && DECL_NAME (TYPE_NAME (decl)))
7523 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
7524 else
7525 e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
7528 else
7529 e.name = xstrdup (dwarf2_name (decl, 1));
7531 /* If we don't have a name for the type, there's no point in adding
7532 it to the table. */
7533 if (e.name && e.name[0] != '\0')
7534 VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
7538 /* Output the public names table used to speed up access to externally
7539 visible names; or the public types table used to find type definitions. */
7541 static void
7542 output_pubnames (VEC (pubname_entry, gc) * names)
7544 unsigned i;
7545 unsigned long pubnames_length = size_of_pubnames (names);
7546 pubname_ref pub;
7548 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7549 dw2_asm_output_data (4, 0xffffffff,
7550 "Initial length escape value indicating 64-bit DWARF extension");
7551 if (names == pubname_table)
7552 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7553 "Length of Public Names Info");
7554 else
7555 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7556 "Length of Public Type Names Info");
7557 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7558 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7559 debug_info_section,
7560 "Offset of Compilation Unit Info");
7561 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
7562 "Compilation Unit Length");
7564 for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
7566 /* We shouldn't see pubnames for DIEs outside of the main CU. */
7567 if (names == pubname_table)
7568 gcc_assert (pub->die->die_mark);
7570 if (names != pubtype_table
7571 || pub->die->die_offset != 0
7572 || !flag_eliminate_unused_debug_types)
7574 dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
7575 "DIE offset");
7577 dw2_asm_output_nstring (pub->name, -1, "external name");
7581 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
7584 /* Add a new entry to .debug_aranges if appropriate. */
7586 static void
7587 add_arange (tree decl, dw_die_ref die)
7589 if (! DECL_SECTION_NAME (decl))
7590 return;
7592 if (arange_table_in_use == arange_table_allocated)
7594 arange_table_allocated += ARANGE_TABLE_INCREMENT;
7595 arange_table = ggc_realloc (arange_table,
7596 (arange_table_allocated
7597 * sizeof (dw_die_ref)));
7598 memset (arange_table + arange_table_in_use, 0,
7599 ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
7602 arange_table[arange_table_in_use++] = die;
7605 /* Output the information that goes into the .debug_aranges table.
7606 Namely, define the beginning and ending address range of the
7607 text section generated for this compilation unit. */
7609 static void
7610 output_aranges (void)
7612 unsigned i;
7613 unsigned long aranges_length = size_of_aranges ();
7615 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7616 dw2_asm_output_data (4, 0xffffffff,
7617 "Initial length escape value indicating 64-bit DWARF extension");
7618 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
7619 "Length of Address Ranges Info");
7620 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7621 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7622 debug_info_section,
7623 "Offset of Compilation Unit Info");
7624 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
7625 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
7627 /* We need to align to twice the pointer size here. */
7628 if (DWARF_ARANGES_PAD_SIZE)
7630 /* Pad using a 2 byte words so that padding is correct for any
7631 pointer size. */
7632 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
7633 2 * DWARF2_ADDR_SIZE);
7634 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
7635 dw2_asm_output_data (2, 0, NULL);
7638 /* It is necessary not to output these entries if the sections were
7639 not used; if the sections were not used, the length will be 0 and
7640 the address may end up as 0 if the section is discarded by ld
7641 --gc-sections, leaving an invalid (0, 0) entry that can be
7642 confused with the terminator. */
7643 if (text_section_used)
7645 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
7646 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
7647 text_section_label, "Length");
7649 if (cold_text_section_used)
7651 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
7652 "Address");
7653 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
7654 cold_text_section_label, "Length");
7657 for (i = 0; i < arange_table_in_use; i++)
7659 dw_die_ref die = arange_table[i];
7661 /* We shouldn't see aranges for DIEs outside of the main CU. */
7662 gcc_assert (die->die_mark);
7664 if (die->die_tag == DW_TAG_subprogram)
7666 dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
7667 "Address");
7668 dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
7669 get_AT_low_pc (die), "Length");
7671 else
7673 /* A static variable; extract the symbol from DW_AT_location.
7674 Note that this code isn't currently hit, as we only emit
7675 aranges for functions (jason 9/23/99). */
7676 dw_attr_ref a = get_AT (die, DW_AT_location);
7677 dw_loc_descr_ref loc;
7679 gcc_assert (a && AT_class (a) == dw_val_class_loc);
7681 loc = AT_loc (a);
7682 gcc_assert (loc->dw_loc_opc == DW_OP_addr);
7684 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
7685 loc->dw_loc_oprnd1.v.val_addr, "Address");
7686 dw2_asm_output_data (DWARF2_ADDR_SIZE,
7687 get_AT_unsigned (die, DW_AT_byte_size),
7688 "Length");
7692 /* Output the terminator words. */
7693 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7694 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7697 /* Add a new entry to .debug_ranges. Return the offset at which it
7698 was placed. */
7700 static unsigned int
7701 add_ranges_num (int num)
7703 unsigned int in_use = ranges_table_in_use;
7705 if (in_use == ranges_table_allocated)
7707 ranges_table_allocated += RANGES_TABLE_INCREMENT;
7708 ranges_table
7709 = ggc_realloc (ranges_table, (ranges_table_allocated
7710 * sizeof (struct dw_ranges_struct)));
7711 memset (ranges_table + ranges_table_in_use, 0,
7712 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
7715 ranges_table[in_use].num = num;
7716 ranges_table_in_use = in_use + 1;
7718 return in_use * 2 * DWARF2_ADDR_SIZE;
7721 /* Add a new entry to .debug_ranges corresponding to a block, or a
7722 range terminator if BLOCK is NULL. */
7724 static unsigned int
7725 add_ranges (const_tree block)
7727 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
7730 /* Add a new entry to .debug_ranges corresponding to a pair of
7731 labels. */
7733 static unsigned int
7734 add_ranges_by_labels (const char *begin, const char *end)
7736 unsigned int in_use = ranges_by_label_in_use;
7738 if (in_use == ranges_by_label_allocated)
7740 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
7741 ranges_by_label
7742 = ggc_realloc (ranges_by_label,
7743 (ranges_by_label_allocated
7744 * sizeof (struct dw_ranges_by_label_struct)));
7745 memset (ranges_by_label + ranges_by_label_in_use, 0,
7746 RANGES_TABLE_INCREMENT
7747 * sizeof (struct dw_ranges_by_label_struct));
7750 ranges_by_label[in_use].begin = begin;
7751 ranges_by_label[in_use].end = end;
7752 ranges_by_label_in_use = in_use + 1;
7754 return add_ranges_num (-(int)in_use - 1);
7757 static void
7758 output_ranges (void)
7760 unsigned i;
7761 static const char *const start_fmt = "Offset 0x%x";
7762 const char *fmt = start_fmt;
7764 for (i = 0; i < ranges_table_in_use; i++)
7766 int block_num = ranges_table[i].num;
7768 if (block_num > 0)
7770 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
7771 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
7773 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
7774 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
7776 /* If all code is in the text section, then the compilation
7777 unit base address defaults to DW_AT_low_pc, which is the
7778 base of the text section. */
7779 if (!have_multiple_function_sections)
7781 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
7782 text_section_label,
7783 fmt, i * 2 * DWARF2_ADDR_SIZE);
7784 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
7785 text_section_label, NULL);
7788 /* Otherwise, the compilation unit base address is zero,
7789 which allows us to use absolute addresses, and not worry
7790 about whether the target supports cross-section
7791 arithmetic. */
7792 else
7794 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
7795 fmt, i * 2 * DWARF2_ADDR_SIZE);
7796 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
7799 fmt = NULL;
7802 /* Negative block_num stands for an index into ranges_by_label. */
7803 else if (block_num < 0)
7805 int lab_idx = - block_num - 1;
7807 if (!have_multiple_function_sections)
7809 gcc_unreachable ();
7810 #if 0
7811 /* If we ever use add_ranges_by_labels () for a single
7812 function section, all we have to do is to take out
7813 the #if 0 above. */
7814 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
7815 ranges_by_label[lab_idx].begin,
7816 text_section_label,
7817 fmt, i * 2 * DWARF2_ADDR_SIZE);
7818 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
7819 ranges_by_label[lab_idx].end,
7820 text_section_label, NULL);
7821 #endif
7823 else
7825 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
7826 ranges_by_label[lab_idx].begin,
7827 fmt, i * 2 * DWARF2_ADDR_SIZE);
7828 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
7829 ranges_by_label[lab_idx].end,
7830 NULL);
7833 else
7835 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7836 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7837 fmt = start_fmt;
7842 /* Data structure containing information about input files. */
7843 struct file_info
7845 const char *path; /* Complete file name. */
7846 const char *fname; /* File name part. */
7847 int length; /* Length of entire string. */
7848 struct dwarf_file_data * file_idx; /* Index in input file table. */
7849 int dir_idx; /* Index in directory table. */
7852 /* Data structure containing information about directories with source
7853 files. */
7854 struct dir_info
7856 const char *path; /* Path including directory name. */
7857 int length; /* Path length. */
7858 int prefix; /* Index of directory entry which is a prefix. */
7859 int count; /* Number of files in this directory. */
7860 int dir_idx; /* Index of directory used as base. */
7863 /* Callback function for file_info comparison. We sort by looking at
7864 the directories in the path. */
7866 static int
7867 file_info_cmp (const void *p1, const void *p2)
7869 const struct file_info *s1 = p1;
7870 const struct file_info *s2 = p2;
7871 const unsigned char *cp1;
7872 const unsigned char *cp2;
7874 /* Take care of file names without directories. We need to make sure that
7875 we return consistent values to qsort since some will get confused if
7876 we return the same value when identical operands are passed in opposite
7877 orders. So if neither has a directory, return 0 and otherwise return
7878 1 or -1 depending on which one has the directory. */
7879 if ((s1->path == s1->fname || s2->path == s2->fname))
7880 return (s2->path == s2->fname) - (s1->path == s1->fname);
7882 cp1 = (const unsigned char *) s1->path;
7883 cp2 = (const unsigned char *) s2->path;
7885 while (1)
7887 ++cp1;
7888 ++cp2;
7889 /* Reached the end of the first path? If so, handle like above. */
7890 if ((cp1 == (const unsigned char *) s1->fname)
7891 || (cp2 == (const unsigned char *) s2->fname))
7892 return ((cp2 == (const unsigned char *) s2->fname)
7893 - (cp1 == (const unsigned char *) s1->fname));
7895 /* Character of current path component the same? */
7896 else if (*cp1 != *cp2)
7897 return *cp1 - *cp2;
7901 struct file_name_acquire_data
7903 struct file_info *files;
7904 int used_files;
7905 int max_files;
7908 /* Traversal function for the hash table. */
7910 static int
7911 file_name_acquire (void ** slot, void *data)
7913 struct file_name_acquire_data *fnad = data;
7914 struct dwarf_file_data *d = *slot;
7915 struct file_info *fi;
7916 const char *f;
7918 gcc_assert (fnad->max_files >= d->emitted_number);
7920 if (! d->emitted_number)
7921 return 1;
7923 gcc_assert (fnad->max_files != fnad->used_files);
7925 fi = fnad->files + fnad->used_files++;
7927 /* Skip all leading "./". */
7928 f = d->filename;
7929 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
7930 f += 2;
7932 /* Create a new array entry. */
7933 fi->path = f;
7934 fi->length = strlen (f);
7935 fi->file_idx = d;
7937 /* Search for the file name part. */
7938 f = strrchr (f, DIR_SEPARATOR);
7939 #if defined (DIR_SEPARATOR_2)
7941 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
7943 if (g != NULL)
7945 if (f == NULL || f < g)
7946 f = g;
7949 #endif
7951 fi->fname = f == NULL ? fi->path : f + 1;
7952 return 1;
7955 /* Output the directory table and the file name table. We try to minimize
7956 the total amount of memory needed. A heuristic is used to avoid large
7957 slowdowns with many input files. */
7959 static void
7960 output_file_names (void)
7962 struct file_name_acquire_data fnad;
7963 int numfiles;
7964 struct file_info *files;
7965 struct dir_info *dirs;
7966 int *saved;
7967 int *savehere;
7968 int *backmap;
7969 int ndirs;
7970 int idx_offset;
7971 int i;
7972 int idx;
7974 if (!last_emitted_file)
7976 dw2_asm_output_data (1, 0, "End directory table");
7977 dw2_asm_output_data (1, 0, "End file name table");
7978 return;
7981 numfiles = last_emitted_file->emitted_number;
7983 /* Allocate the various arrays we need. */
7984 files = alloca (numfiles * sizeof (struct file_info));
7985 dirs = alloca (numfiles * sizeof (struct dir_info));
7987 fnad.files = files;
7988 fnad.used_files = 0;
7989 fnad.max_files = numfiles;
7990 htab_traverse (file_table, file_name_acquire, &fnad);
7991 gcc_assert (fnad.used_files == fnad.max_files);
7993 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
7995 /* Find all the different directories used. */
7996 dirs[0].path = files[0].path;
7997 dirs[0].length = files[0].fname - files[0].path;
7998 dirs[0].prefix = -1;
7999 dirs[0].count = 1;
8000 dirs[0].dir_idx = 0;
8001 files[0].dir_idx = 0;
8002 ndirs = 1;
8004 for (i = 1; i < numfiles; i++)
8005 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
8006 && memcmp (dirs[ndirs - 1].path, files[i].path,
8007 dirs[ndirs - 1].length) == 0)
8009 /* Same directory as last entry. */
8010 files[i].dir_idx = ndirs - 1;
8011 ++dirs[ndirs - 1].count;
8013 else
8015 int j;
8017 /* This is a new directory. */
8018 dirs[ndirs].path = files[i].path;
8019 dirs[ndirs].length = files[i].fname - files[i].path;
8020 dirs[ndirs].count = 1;
8021 dirs[ndirs].dir_idx = ndirs;
8022 files[i].dir_idx = ndirs;
8024 /* Search for a prefix. */
8025 dirs[ndirs].prefix = -1;
8026 for (j = 0; j < ndirs; j++)
8027 if (dirs[j].length < dirs[ndirs].length
8028 && dirs[j].length > 1
8029 && (dirs[ndirs].prefix == -1
8030 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
8031 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
8032 dirs[ndirs].prefix = j;
8034 ++ndirs;
8037 /* Now to the actual work. We have to find a subset of the directories which
8038 allow expressing the file name using references to the directory table
8039 with the least amount of characters. We do not do an exhaustive search
8040 where we would have to check out every combination of every single
8041 possible prefix. Instead we use a heuristic which provides nearly optimal
8042 results in most cases and never is much off. */
8043 saved = alloca (ndirs * sizeof (int));
8044 savehere = alloca (ndirs * sizeof (int));
8046 memset (saved, '\0', ndirs * sizeof (saved[0]));
8047 for (i = 0; i < ndirs; i++)
8049 int j;
8050 int total;
8052 /* We can always save some space for the current directory. But this
8053 does not mean it will be enough to justify adding the directory. */
8054 savehere[i] = dirs[i].length;
8055 total = (savehere[i] - saved[i]) * dirs[i].count;
8057 for (j = i + 1; j < ndirs; j++)
8059 savehere[j] = 0;
8060 if (saved[j] < dirs[i].length)
8062 /* Determine whether the dirs[i] path is a prefix of the
8063 dirs[j] path. */
8064 int k;
8066 k = dirs[j].prefix;
8067 while (k != -1 && k != (int) i)
8068 k = dirs[k].prefix;
8070 if (k == (int) i)
8072 /* Yes it is. We can possibly save some memory by
8073 writing the filenames in dirs[j] relative to
8074 dirs[i]. */
8075 savehere[j] = dirs[i].length;
8076 total += (savehere[j] - saved[j]) * dirs[j].count;
8081 /* Check whether we can save enough to justify adding the dirs[i]
8082 directory. */
8083 if (total > dirs[i].length + 1)
8085 /* It's worthwhile adding. */
8086 for (j = i; j < ndirs; j++)
8087 if (savehere[j] > 0)
8089 /* Remember how much we saved for this directory so far. */
8090 saved[j] = savehere[j];
8092 /* Remember the prefix directory. */
8093 dirs[j].dir_idx = i;
8098 /* Emit the directory name table. */
8099 idx = 1;
8100 idx_offset = dirs[0].length > 0 ? 1 : 0;
8101 for (i = 1 - idx_offset; i < ndirs; i++)
8102 dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
8103 "Directory Entry: 0x%x", i + idx_offset);
8105 dw2_asm_output_data (1, 0, "End directory table");
8107 /* We have to emit them in the order of emitted_number since that's
8108 used in the debug info generation. To do this efficiently we
8109 generate a back-mapping of the indices first. */
8110 backmap = alloca (numfiles * sizeof (int));
8111 for (i = 0; i < numfiles; i++)
8112 backmap[files[i].file_idx->emitted_number - 1] = i;
8114 /* Now write all the file names. */
8115 for (i = 0; i < numfiles; i++)
8117 int file_idx = backmap[i];
8118 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
8120 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
8121 "File Entry: 0x%x", (unsigned) i + 1);
8123 /* Include directory index. */
8124 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
8126 /* Modification time. */
8127 dw2_asm_output_data_uleb128 (0, NULL);
8129 /* File length in bytes. */
8130 dw2_asm_output_data_uleb128 (0, NULL);
8133 dw2_asm_output_data (1, 0, "End file name table");
8137 /* Output the source line number correspondence information. This
8138 information goes into the .debug_line section. */
8140 static void
8141 output_line_info (void)
8143 char l1[20], l2[20], p1[20], p2[20];
8144 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8145 char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8146 unsigned opc;
8147 unsigned n_op_args;
8148 unsigned long lt_index;
8149 unsigned long current_line;
8150 long line_offset;
8151 long line_delta;
8152 unsigned long current_file;
8153 unsigned long function;
8155 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
8156 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
8157 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
8158 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
8160 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8161 dw2_asm_output_data (4, 0xffffffff,
8162 "Initial length escape value indicating 64-bit DWARF extension");
8163 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
8164 "Length of Source Line Info");
8165 ASM_OUTPUT_LABEL (asm_out_file, l1);
8167 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8168 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
8169 ASM_OUTPUT_LABEL (asm_out_file, p1);
8171 /* Define the architecture-dependent minimum instruction length (in
8172 bytes). In this implementation of DWARF, this field is used for
8173 information purposes only. Since GCC generates assembly language,
8174 we have no a priori knowledge of how many instruction bytes are
8175 generated for each source line, and therefore can use only the
8176 DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
8177 commands. Accordingly, we fix this as `1', which is "correct
8178 enough" for all architectures, and don't let the target override. */
8179 dw2_asm_output_data (1, 1,
8180 "Minimum Instruction Length");
8182 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
8183 "Default is_stmt_start flag");
8184 dw2_asm_output_data (1, DWARF_LINE_BASE,
8185 "Line Base Value (Special Opcodes)");
8186 dw2_asm_output_data (1, DWARF_LINE_RANGE,
8187 "Line Range Value (Special Opcodes)");
8188 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
8189 "Special Opcode Base");
8191 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
8193 switch (opc)
8195 case DW_LNS_advance_pc:
8196 case DW_LNS_advance_line:
8197 case DW_LNS_set_file:
8198 case DW_LNS_set_column:
8199 case DW_LNS_fixed_advance_pc:
8200 n_op_args = 1;
8201 break;
8202 default:
8203 n_op_args = 0;
8204 break;
8207 dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
8208 opc, n_op_args);
8211 /* Write out the information about the files we use. */
8212 output_file_names ();
8213 ASM_OUTPUT_LABEL (asm_out_file, p2);
8215 /* We used to set the address register to the first location in the text
8216 section here, but that didn't accomplish anything since we already
8217 have a line note for the opening brace of the first function. */
8219 /* Generate the line number to PC correspondence table, encoded as
8220 a series of state machine operations. */
8221 current_file = 1;
8222 current_line = 1;
8224 if (cfun && in_cold_section_p)
8225 strcpy (prev_line_label, crtl->subsections.cold_section_label);
8226 else
8227 strcpy (prev_line_label, text_section_label);
8228 for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
8230 dw_line_info_ref line_info = &line_info_table[lt_index];
8232 #if 0
8233 /* Disable this optimization for now; GDB wants to see two line notes
8234 at the beginning of a function so it can find the end of the
8235 prologue. */
8237 /* Don't emit anything for redundant notes. Just updating the
8238 address doesn't accomplish anything, because we already assume
8239 that anything after the last address is this line. */
8240 if (line_info->dw_line_num == current_line
8241 && line_info->dw_file_num == current_file)
8242 continue;
8243 #endif
8245 /* Emit debug info for the address of the current line.
8247 Unfortunately, we have little choice here currently, and must always
8248 use the most general form. GCC does not know the address delta
8249 itself, so we can't use DW_LNS_advance_pc. Many ports do have length
8250 attributes which will give an upper bound on the address range. We
8251 could perhaps use length attributes to determine when it is safe to
8252 use DW_LNS_fixed_advance_pc. */
8254 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
8255 if (0)
8257 /* This can handle deltas up to 0xffff. This takes 3 bytes. */
8258 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8259 "DW_LNS_fixed_advance_pc");
8260 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8262 else
8264 /* This can handle any delta. This takes
8265 4+DWARF2_ADDR_SIZE bytes. */
8266 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8267 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8268 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8269 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8272 strcpy (prev_line_label, line_label);
8274 /* Emit debug info for the source file of the current line, if
8275 different from the previous line. */
8276 if (line_info->dw_file_num != current_file)
8278 current_file = line_info->dw_file_num;
8279 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8280 dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
8283 /* Emit debug info for the current line number, choosing the encoding
8284 that uses the least amount of space. */
8285 if (line_info->dw_line_num != current_line)
8287 line_offset = line_info->dw_line_num - current_line;
8288 line_delta = line_offset - DWARF_LINE_BASE;
8289 current_line = line_info->dw_line_num;
8290 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
8291 /* This can handle deltas from -10 to 234, using the current
8292 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE. This
8293 takes 1 byte. */
8294 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
8295 "line %lu", current_line);
8296 else
8298 /* This can handle any delta. This takes at least 4 bytes,
8299 depending on the value being encoded. */
8300 dw2_asm_output_data (1, DW_LNS_advance_line,
8301 "advance to line %lu", current_line);
8302 dw2_asm_output_data_sleb128 (line_offset, NULL);
8303 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8306 else
8307 /* We still need to start a new row, so output a copy insn. */
8308 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8311 /* Emit debug info for the address of the end of the function. */
8312 if (0)
8314 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8315 "DW_LNS_fixed_advance_pc");
8316 dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
8318 else
8320 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8321 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8322 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8323 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
8326 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
8327 dw2_asm_output_data_uleb128 (1, NULL);
8328 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
8330 function = 0;
8331 current_file = 1;
8332 current_line = 1;
8333 for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
8335 dw_separate_line_info_ref line_info
8336 = &separate_line_info_table[lt_index];
8338 #if 0
8339 /* Don't emit anything for redundant notes. */
8340 if (line_info->dw_line_num == current_line
8341 && line_info->dw_file_num == current_file
8342 && line_info->function == function)
8343 goto cont;
8344 #endif
8346 /* Emit debug info for the address of the current line. If this is
8347 a new function, or the first line of a function, then we need
8348 to handle it differently. */
8349 ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
8350 lt_index);
8351 if (function != line_info->function)
8353 function = line_info->function;
8355 /* Set the address register to the first line in the function. */
8356 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8357 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8358 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8359 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8361 else
8363 /* ??? See the DW_LNS_advance_pc comment above. */
8364 if (0)
8366 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8367 "DW_LNS_fixed_advance_pc");
8368 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8370 else
8372 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8373 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8374 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8375 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8379 strcpy (prev_line_label, line_label);
8381 /* Emit debug info for the source file of the current line, if
8382 different from the previous line. */
8383 if (line_info->dw_file_num != current_file)
8385 current_file = line_info->dw_file_num;
8386 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8387 dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
8390 /* Emit debug info for the current line number, choosing the encoding
8391 that uses the least amount of space. */
8392 if (line_info->dw_line_num != current_line)
8394 line_offset = line_info->dw_line_num - current_line;
8395 line_delta = line_offset - DWARF_LINE_BASE;
8396 current_line = line_info->dw_line_num;
8397 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
8398 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
8399 "line %lu", current_line);
8400 else
8402 dw2_asm_output_data (1, DW_LNS_advance_line,
8403 "advance to line %lu", current_line);
8404 dw2_asm_output_data_sleb128 (line_offset, NULL);
8405 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8408 else
8409 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8411 #if 0
8412 cont:
8413 #endif
8415 lt_index++;
8417 /* If we're done with a function, end its sequence. */
8418 if (lt_index == separate_line_info_table_in_use
8419 || separate_line_info_table[lt_index].function != function)
8421 current_file = 1;
8422 current_line = 1;
8424 /* Emit debug info for the address of the end of the function. */
8425 ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
8426 if (0)
8428 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8429 "DW_LNS_fixed_advance_pc");
8430 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8432 else
8434 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8435 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8436 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8437 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8440 /* Output the marker for the end of this sequence. */
8441 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
8442 dw2_asm_output_data_uleb128 (1, NULL);
8443 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
8447 /* Output the marker for the end of the line number info. */
8448 ASM_OUTPUT_LABEL (asm_out_file, l2);
8451 /* Given a pointer to a tree node for some base type, return a pointer to
8452 a DIE that describes the given type.
8454 This routine must only be called for GCC type nodes that correspond to
8455 Dwarf base (fundamental) types. */
8457 static dw_die_ref
8458 base_type_die (tree type)
8460 dw_die_ref base_type_result;
8461 enum dwarf_type encoding;
8463 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
8464 return 0;
8466 switch (TREE_CODE (type))
8468 case INTEGER_TYPE:
8469 if (TYPE_STRING_FLAG (type))
8471 if (TYPE_UNSIGNED (type))
8472 encoding = DW_ATE_unsigned_char;
8473 else
8474 encoding = DW_ATE_signed_char;
8476 else if (TYPE_UNSIGNED (type))
8477 encoding = DW_ATE_unsigned;
8478 else
8479 encoding = DW_ATE_signed;
8480 break;
8482 case REAL_TYPE:
8483 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
8484 encoding = DW_ATE_decimal_float;
8485 else
8486 encoding = DW_ATE_float;
8487 break;
8489 case FIXED_POINT_TYPE:
8490 if (TYPE_UNSIGNED (type))
8491 encoding = DW_ATE_unsigned_fixed;
8492 else
8493 encoding = DW_ATE_signed_fixed;
8494 break;
8496 /* Dwarf2 doesn't know anything about complex ints, so use
8497 a user defined type for it. */
8498 case COMPLEX_TYPE:
8499 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
8500 encoding = DW_ATE_complex_float;
8501 else
8502 encoding = DW_ATE_lo_user;
8503 break;
8505 case BOOLEAN_TYPE:
8506 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
8507 encoding = DW_ATE_boolean;
8508 break;
8510 default:
8511 /* No other TREE_CODEs are Dwarf fundamental types. */
8512 gcc_unreachable ();
8515 base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
8517 /* This probably indicates a bug. */
8518 if (! TYPE_NAME (type))
8519 add_name_attribute (base_type_result, "__unknown__");
8521 add_AT_unsigned (base_type_result, DW_AT_byte_size,
8522 int_size_in_bytes (type));
8523 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
8525 return base_type_result;
8528 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
8529 given input type is a Dwarf "fundamental" type. Otherwise return null. */
8531 static inline int
8532 is_base_type (tree type)
8534 switch (TREE_CODE (type))
8536 case ERROR_MARK:
8537 case VOID_TYPE:
8538 case INTEGER_TYPE:
8539 case REAL_TYPE:
8540 case FIXED_POINT_TYPE:
8541 case COMPLEX_TYPE:
8542 case BOOLEAN_TYPE:
8543 return 1;
8545 case ARRAY_TYPE:
8546 case RECORD_TYPE:
8547 case UNION_TYPE:
8548 case QUAL_UNION_TYPE:
8549 case ENUMERAL_TYPE:
8550 case FUNCTION_TYPE:
8551 case METHOD_TYPE:
8552 case POINTER_TYPE:
8553 case REFERENCE_TYPE:
8554 case OFFSET_TYPE:
8555 case LANG_TYPE:
8556 case VECTOR_TYPE:
8557 return 0;
8559 default:
8560 gcc_unreachable ();
8563 return 0;
8566 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
8567 node, return the size in bits for the type if it is a constant, or else
8568 return the alignment for the type if the type's size is not constant, or
8569 else return BITS_PER_WORD if the type actually turns out to be an
8570 ERROR_MARK node. */
8572 static inline unsigned HOST_WIDE_INT
8573 simple_type_size_in_bits (const_tree type)
8575 if (TREE_CODE (type) == ERROR_MARK)
8576 return BITS_PER_WORD;
8577 else if (TYPE_SIZE (type) == NULL_TREE)
8578 return 0;
8579 else if (host_integerp (TYPE_SIZE (type), 1))
8580 return tree_low_cst (TYPE_SIZE (type), 1);
8581 else
8582 return TYPE_ALIGN (type);
8585 /* Return true if the debug information for the given type should be
8586 emitted as a subrange type. */
8588 static inline bool
8589 is_subrange_type (const_tree type)
8591 tree subtype = TREE_TYPE (type);
8593 /* Subrange types are identified by the fact that they are integer
8594 types, and that they have a subtype which is either an integer type
8595 or an enumeral type. */
8597 if (TREE_CODE (type) != INTEGER_TYPE
8598 || subtype == NULL_TREE)
8599 return false;
8601 if (TREE_CODE (subtype) != INTEGER_TYPE
8602 && TREE_CODE (subtype) != ENUMERAL_TYPE)
8603 return false;
8605 if (TREE_CODE (type) == TREE_CODE (subtype)
8606 && int_size_in_bytes (type) == int_size_in_bytes (subtype)
8607 && TYPE_MIN_VALUE (type) != NULL
8608 && TYPE_MIN_VALUE (subtype) != NULL
8609 && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
8610 && TYPE_MAX_VALUE (type) != NULL
8611 && TYPE_MAX_VALUE (subtype) != NULL
8612 && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
8614 /* The type and its subtype have the same representation. If in
8615 addition the two types also have the same name, then the given
8616 type is not a subrange type, but rather a plain base type. */
8617 /* FIXME: brobecker/2004-03-22:
8618 Sizetype INTEGER_CSTs nodes are canonicalized. It should
8619 therefore be sufficient to check the TYPE_SIZE node pointers
8620 rather than checking the actual size. Unfortunately, we have
8621 found some cases, such as in the Ada "integer" type, where
8622 this is not the case. Until this problem is solved, we need to
8623 keep checking the actual size. */
8624 tree type_name = TYPE_NAME (type);
8625 tree subtype_name = TYPE_NAME (subtype);
8627 if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
8628 type_name = DECL_NAME (type_name);
8630 if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
8631 subtype_name = DECL_NAME (subtype_name);
8633 if (type_name == subtype_name)
8634 return false;
8637 return true;
8640 /* Given a pointer to a tree node for a subrange type, return a pointer
8641 to a DIE that describes the given type. */
8643 static dw_die_ref
8644 subrange_type_die (tree type, dw_die_ref context_die)
8646 dw_die_ref subrange_die;
8647 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
8649 if (context_die == NULL)
8650 context_die = comp_unit_die;
8652 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
8654 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
8656 /* The size of the subrange type and its base type do not match,
8657 so we need to generate a size attribute for the subrange type. */
8658 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
8661 if (TYPE_MIN_VALUE (type) != NULL)
8662 add_bound_info (subrange_die, DW_AT_lower_bound,
8663 TYPE_MIN_VALUE (type));
8664 if (TYPE_MAX_VALUE (type) != NULL)
8665 add_bound_info (subrange_die, DW_AT_upper_bound,
8666 TYPE_MAX_VALUE (type));
8668 return subrange_die;
8671 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
8672 entry that chains various modifiers in front of the given type. */
8674 static dw_die_ref
8675 modified_type_die (tree type, int is_const_type, int is_volatile_type,
8676 dw_die_ref context_die)
8678 enum tree_code code = TREE_CODE (type);
8679 dw_die_ref mod_type_die;
8680 dw_die_ref sub_die = NULL;
8681 tree item_type = NULL;
8682 tree qualified_type;
8683 tree name;
8685 if (code == ERROR_MARK)
8686 return NULL;
8688 /* See if we already have the appropriately qualified variant of
8689 this type. */
8690 qualified_type
8691 = get_qualified_type (type,
8692 ((is_const_type ? TYPE_QUAL_CONST : 0)
8693 | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
8695 /* If we do, then we can just use its DIE, if it exists. */
8696 if (qualified_type)
8698 mod_type_die = lookup_type_die (qualified_type);
8699 if (mod_type_die)
8700 return mod_type_die;
8703 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
8705 /* Handle C typedef types. */
8706 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
8708 tree dtype = TREE_TYPE (name);
8710 if (qualified_type == dtype)
8712 /* For a named type, use the typedef. */
8713 gen_type_die (qualified_type, context_die);
8714 return lookup_type_die (qualified_type);
8716 else if (is_const_type < TYPE_READONLY (dtype)
8717 || is_volatile_type < TYPE_VOLATILE (dtype)
8718 || (is_const_type <= TYPE_READONLY (dtype)
8719 && is_volatile_type <= TYPE_VOLATILE (dtype)
8720 && DECL_ORIGINAL_TYPE (name) != type))
8721 /* cv-unqualified version of named type. Just use the unnamed
8722 type to which it refers. */
8723 return modified_type_die (DECL_ORIGINAL_TYPE (name),
8724 is_const_type, is_volatile_type,
8725 context_die);
8726 /* Else cv-qualified version of named type; fall through. */
8729 if (is_const_type)
8731 mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
8732 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
8734 else if (is_volatile_type)
8736 mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
8737 sub_die = modified_type_die (type, 0, 0, context_die);
8739 else if (code == POINTER_TYPE)
8741 mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
8742 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8743 simple_type_size_in_bits (type) / BITS_PER_UNIT);
8744 item_type = TREE_TYPE (type);
8746 else if (code == REFERENCE_TYPE)
8748 mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
8749 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8750 simple_type_size_in_bits (type) / BITS_PER_UNIT);
8751 item_type = TREE_TYPE (type);
8753 else if (is_subrange_type (type))
8755 mod_type_die = subrange_type_die (type, context_die);
8756 item_type = TREE_TYPE (type);
8758 else if (is_base_type (type))
8759 mod_type_die = base_type_die (type);
8760 else
8762 gen_type_die (type, context_die);
8764 /* We have to get the type_main_variant here (and pass that to the
8765 `lookup_type_die' routine) because the ..._TYPE node we have
8766 might simply be a *copy* of some original type node (where the
8767 copy was created to help us keep track of typedef names) and
8768 that copy might have a different TYPE_UID from the original
8769 ..._TYPE node. */
8770 if (TREE_CODE (type) != VECTOR_TYPE)
8771 return lookup_type_die (type_main_variant (type));
8772 else
8773 /* Vectors have the debugging information in the type,
8774 not the main variant. */
8775 return lookup_type_die (type);
8778 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
8779 don't output a DW_TAG_typedef, since there isn't one in the
8780 user's program; just attach a DW_AT_name to the type. */
8781 if (name
8782 && (TREE_CODE (name) != TYPE_DECL
8783 || (TREE_TYPE (name) == qualified_type && DECL_NAME (name))))
8785 if (TREE_CODE (name) == TYPE_DECL)
8786 /* Could just call add_name_and_src_coords_attributes here,
8787 but since this is a builtin type it doesn't have any
8788 useful source coordinates anyway. */
8789 name = DECL_NAME (name);
8790 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
8793 if (qualified_type)
8794 equate_type_number_to_die (qualified_type, mod_type_die);
8796 if (item_type)
8797 /* We must do this after the equate_type_number_to_die call, in case
8798 this is a recursive type. This ensures that the modified_type_die
8799 recursion will terminate even if the type is recursive. Recursive
8800 types are possible in Ada. */
8801 sub_die = modified_type_die (item_type,
8802 TYPE_READONLY (item_type),
8803 TYPE_VOLATILE (item_type),
8804 context_die);
8806 if (sub_die != NULL)
8807 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
8809 return mod_type_die;
8812 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
8813 an enumerated type. */
8815 static inline int
8816 type_is_enum (const_tree type)
8818 return TREE_CODE (type) == ENUMERAL_TYPE;
8821 /* Return the DBX register number described by a given RTL node. */
8823 static unsigned int
8824 dbx_reg_number (const_rtx rtl)
8826 unsigned regno = REGNO (rtl);
8828 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
8830 #ifdef LEAF_REG_REMAP
8831 if (current_function_uses_only_leaf_regs)
8833 int leaf_reg = LEAF_REG_REMAP (regno);
8834 if (leaf_reg != -1)
8835 regno = (unsigned) leaf_reg;
8837 #endif
8839 return DBX_REGISTER_NUMBER (regno);
8842 /* Optionally add a DW_OP_piece term to a location description expression.
8843 DW_OP_piece is only added if the location description expression already
8844 doesn't end with DW_OP_piece. */
8846 static void
8847 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
8849 dw_loc_descr_ref loc;
8851 if (*list_head != NULL)
8853 /* Find the end of the chain. */
8854 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
8857 if (loc->dw_loc_opc != DW_OP_piece)
8858 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
8862 /* Return a location descriptor that designates a machine register or
8863 zero if there is none. */
8865 static dw_loc_descr_ref
8866 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
8868 rtx regs;
8870 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
8871 return 0;
8873 regs = targetm.dwarf_register_span (rtl);
8875 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
8876 return multiple_reg_loc_descriptor (rtl, regs, initialized);
8877 else
8878 return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
8881 /* Return a location descriptor that designates a machine register for
8882 a given hard register number. */
8884 static dw_loc_descr_ref
8885 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
8887 dw_loc_descr_ref reg_loc_descr;
8888 if (regno <= 31)
8889 reg_loc_descr = new_loc_descr (DW_OP_reg0 + regno, 0, 0);
8890 else
8891 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
8893 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
8894 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
8896 return reg_loc_descr;
8899 /* Given an RTL of a register, return a location descriptor that
8900 designates a value that spans more than one register. */
8902 static dw_loc_descr_ref
8903 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
8904 enum var_init_status initialized)
8906 int nregs, size, i;
8907 unsigned reg;
8908 dw_loc_descr_ref loc_result = NULL;
8910 reg = REGNO (rtl);
8911 #ifdef LEAF_REG_REMAP
8912 if (current_function_uses_only_leaf_regs)
8914 int leaf_reg = LEAF_REG_REMAP (reg);
8915 if (leaf_reg != -1)
8916 reg = (unsigned) leaf_reg;
8918 #endif
8919 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
8920 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
8922 /* Simple, contiguous registers. */
8923 if (regs == NULL_RTX)
8925 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
8927 loc_result = NULL;
8928 while (nregs--)
8930 dw_loc_descr_ref t;
8932 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
8933 VAR_INIT_STATUS_INITIALIZED);
8934 add_loc_descr (&loc_result, t);
8935 add_loc_descr_op_piece (&loc_result, size);
8936 ++reg;
8938 return loc_result;
8941 /* Now onto stupid register sets in non contiguous locations. */
8943 gcc_assert (GET_CODE (regs) == PARALLEL);
8945 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8946 loc_result = NULL;
8948 for (i = 0; i < XVECLEN (regs, 0); ++i)
8950 dw_loc_descr_ref t;
8952 t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
8953 VAR_INIT_STATUS_INITIALIZED);
8954 add_loc_descr (&loc_result, t);
8955 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8956 add_loc_descr_op_piece (&loc_result, size);
8959 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
8960 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
8961 return loc_result;
8964 /* Return a location descriptor that designates a constant. */
8966 static dw_loc_descr_ref
8967 int_loc_descriptor (HOST_WIDE_INT i)
8969 enum dwarf_location_atom op;
8971 /* Pick the smallest representation of a constant, rather than just
8972 defaulting to the LEB encoding. */
8973 if (i >= 0)
8975 if (i <= 31)
8976 op = DW_OP_lit0 + i;
8977 else if (i <= 0xff)
8978 op = DW_OP_const1u;
8979 else if (i <= 0xffff)
8980 op = DW_OP_const2u;
8981 else if (HOST_BITS_PER_WIDE_INT == 32
8982 || i <= 0xffffffff)
8983 op = DW_OP_const4u;
8984 else
8985 op = DW_OP_constu;
8987 else
8989 if (i >= -0x80)
8990 op = DW_OP_const1s;
8991 else if (i >= -0x8000)
8992 op = DW_OP_const2s;
8993 else if (HOST_BITS_PER_WIDE_INT == 32
8994 || i >= -0x80000000)
8995 op = DW_OP_const4s;
8996 else
8997 op = DW_OP_consts;
9000 return new_loc_descr (op, i, 0);
9003 /* Return a location descriptor that designates a base+offset location. */
9005 static dw_loc_descr_ref
9006 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
9007 enum var_init_status initialized)
9009 unsigned int regno;
9010 dw_loc_descr_ref result;
9012 /* We only use "frame base" when we're sure we're talking about the
9013 post-prologue local stack frame. We do this by *not* running
9014 register elimination until this point, and recognizing the special
9015 argument pointer and soft frame pointer rtx's. */
9016 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
9018 rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
9020 if (elim != reg)
9022 if (GET_CODE (elim) == PLUS)
9024 offset += INTVAL (XEXP (elim, 1));
9025 elim = XEXP (elim, 0);
9027 gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
9028 : stack_pointer_rtx));
9029 offset += frame_pointer_fb_offset;
9031 return new_loc_descr (DW_OP_fbreg, offset, 0);
9035 regno = dbx_reg_number (reg);
9036 if (regno <= 31)
9037 result = new_loc_descr (DW_OP_breg0 + regno, offset, 0);
9038 else
9039 result = new_loc_descr (DW_OP_bregx, regno, offset);
9041 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9042 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9044 return result;
9047 /* Return true if this RTL expression describes a base+offset calculation. */
9049 static inline int
9050 is_based_loc (const_rtx rtl)
9052 return (GET_CODE (rtl) == PLUS
9053 && ((REG_P (XEXP (rtl, 0))
9054 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
9055 && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
9058 /* Return a descriptor that describes the concatenation of N locations
9059 used to form the address of a memory location. */
9061 static dw_loc_descr_ref
9062 concatn_mem_loc_descriptor (rtx concatn, enum machine_mode mode,
9063 enum var_init_status initialized)
9065 unsigned int i;
9066 dw_loc_descr_ref cc_loc_result = NULL;
9067 unsigned int n = XVECLEN (concatn, 0);
9069 for (i = 0; i < n; ++i)
9071 dw_loc_descr_ref ref;
9072 rtx x = XVECEXP (concatn, 0, i);
9074 ref = mem_loc_descriptor (x, mode, VAR_INIT_STATUS_INITIALIZED);
9075 if (ref == NULL)
9076 return NULL;
9078 add_loc_descr (&cc_loc_result, ref);
9079 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
9082 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9083 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9085 return cc_loc_result;
9088 /* The following routine converts the RTL for a variable or parameter
9089 (resident in memory) into an equivalent Dwarf representation of a
9090 mechanism for getting the address of that same variable onto the top of a
9091 hypothetical "address evaluation" stack.
9093 When creating memory location descriptors, we are effectively transforming
9094 the RTL for a memory-resident object into its Dwarf postfix expression
9095 equivalent. This routine recursively descends an RTL tree, turning
9096 it into Dwarf postfix code as it goes.
9098 MODE is the mode of the memory reference, needed to handle some
9099 autoincrement addressing modes.
9101 CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
9102 location list for RTL.
9104 Return 0 if we can't represent the location. */
9106 static dw_loc_descr_ref
9107 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
9108 enum var_init_status initialized)
9110 dw_loc_descr_ref mem_loc_result = NULL;
9111 enum dwarf_location_atom op;
9113 /* Note that for a dynamically sized array, the location we will generate a
9114 description of here will be the lowest numbered location which is
9115 actually within the array. That's *not* necessarily the same as the
9116 zeroth element of the array. */
9118 rtl = targetm.delegitimize_address (rtl);
9120 switch (GET_CODE (rtl))
9122 case POST_INC:
9123 case POST_DEC:
9124 case POST_MODIFY:
9125 /* POST_INC and POST_DEC can be handled just like a SUBREG. So we
9126 just fall into the SUBREG code. */
9128 /* ... fall through ... */
9130 case SUBREG:
9131 /* The case of a subreg may arise when we have a local (register)
9132 variable or a formal (register) parameter which doesn't quite fill
9133 up an entire register. For now, just assume that it is
9134 legitimate to make the Dwarf info refer to the whole register which
9135 contains the given subreg. */
9136 rtl = XEXP (rtl, 0);
9138 /* ... fall through ... */
9140 case REG:
9141 /* Whenever a register number forms a part of the description of the
9142 method for calculating the (dynamic) address of a memory resident
9143 object, DWARF rules require the register number be referred to as
9144 a "base register". This distinction is not based in any way upon
9145 what category of register the hardware believes the given register
9146 belongs to. This is strictly DWARF terminology we're dealing with
9147 here. Note that in cases where the location of a memory-resident
9148 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
9149 OP_CONST (0)) the actual DWARF location descriptor that we generate
9150 may just be OP_BASEREG (basereg). This may look deceptively like
9151 the object in question was allocated to a register (rather than in
9152 memory) so DWARF consumers need to be aware of the subtle
9153 distinction between OP_REG and OP_BASEREG. */
9154 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
9155 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
9156 break;
9158 case MEM:
9159 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
9160 VAR_INIT_STATUS_INITIALIZED);
9161 if (mem_loc_result != 0)
9162 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
9163 break;
9165 case LO_SUM:
9166 rtl = XEXP (rtl, 1);
9168 /* ... fall through ... */
9170 case LABEL_REF:
9171 /* Some ports can transform a symbol ref into a label ref, because
9172 the symbol ref is too far away and has to be dumped into a constant
9173 pool. */
9174 case CONST:
9175 case SYMBOL_REF:
9176 /* Alternatively, the symbol in the constant pool might be referenced
9177 by a different symbol. */
9178 if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
9180 bool marked;
9181 rtx tmp = get_pool_constant_mark (rtl, &marked);
9183 if (GET_CODE (tmp) == SYMBOL_REF)
9185 rtl = tmp;
9186 if (CONSTANT_POOL_ADDRESS_P (tmp))
9187 get_pool_constant_mark (tmp, &marked);
9188 else
9189 marked = true;
9192 /* If all references to this pool constant were optimized away,
9193 it was not output and thus we can't represent it.
9194 FIXME: might try to use DW_OP_const_value here, though
9195 DW_OP_piece complicates it. */
9196 if (!marked)
9197 return 0;
9200 mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
9201 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
9202 mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
9203 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
9204 break;
9206 case PRE_MODIFY:
9207 /* Extract the PLUS expression nested inside and fall into
9208 PLUS code below. */
9209 rtl = XEXP (rtl, 1);
9210 goto plus;
9212 case PRE_INC:
9213 case PRE_DEC:
9214 /* Turn these into a PLUS expression and fall into the PLUS code
9215 below. */
9216 rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
9217 GEN_INT (GET_CODE (rtl) == PRE_INC
9218 ? GET_MODE_UNIT_SIZE (mode)
9219 : -GET_MODE_UNIT_SIZE (mode)));
9221 /* ... fall through ... */
9223 case PLUS:
9224 plus:
9225 if (is_based_loc (rtl))
9226 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
9227 INTVAL (XEXP (rtl, 1)),
9228 VAR_INIT_STATUS_INITIALIZED);
9229 else
9231 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
9232 VAR_INIT_STATUS_INITIALIZED);
9233 if (mem_loc_result == 0)
9234 break;
9236 if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
9237 && INTVAL (XEXP (rtl, 1)) >= 0)
9238 add_loc_descr (&mem_loc_result,
9239 new_loc_descr (DW_OP_plus_uconst,
9240 INTVAL (XEXP (rtl, 1)), 0));
9241 else
9243 add_loc_descr (&mem_loc_result,
9244 mem_loc_descriptor (XEXP (rtl, 1), mode,
9245 VAR_INIT_STATUS_INITIALIZED));
9246 add_loc_descr (&mem_loc_result,
9247 new_loc_descr (DW_OP_plus, 0, 0));
9250 break;
9252 /* If a pseudo-reg is optimized away, it is possible for it to
9253 be replaced with a MEM containing a multiply or shift. */
9254 case MULT:
9255 op = DW_OP_mul;
9256 goto do_binop;
9258 case ASHIFT:
9259 op = DW_OP_shl;
9260 goto do_binop;
9262 case ASHIFTRT:
9263 op = DW_OP_shra;
9264 goto do_binop;
9266 case LSHIFTRT:
9267 op = DW_OP_shr;
9268 goto do_binop;
9270 do_binop:
9272 dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
9273 VAR_INIT_STATUS_INITIALIZED);
9274 dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
9275 VAR_INIT_STATUS_INITIALIZED);
9277 if (op0 == 0 || op1 == 0)
9278 break;
9280 mem_loc_result = op0;
9281 add_loc_descr (&mem_loc_result, op1);
9282 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
9283 break;
9286 case CONST_INT:
9287 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
9288 break;
9290 case CONCATN:
9291 mem_loc_result = concatn_mem_loc_descriptor (rtl, mode,
9292 VAR_INIT_STATUS_INITIALIZED);
9293 break;
9295 default:
9296 gcc_unreachable ();
9299 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9300 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9302 return mem_loc_result;
9305 /* Return a descriptor that describes the concatenation of two locations.
9306 This is typically a complex variable. */
9308 static dw_loc_descr_ref
9309 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
9311 dw_loc_descr_ref cc_loc_result = NULL;
9312 dw_loc_descr_ref x0_ref = loc_descriptor (x0, VAR_INIT_STATUS_INITIALIZED);
9313 dw_loc_descr_ref x1_ref = loc_descriptor (x1, VAR_INIT_STATUS_INITIALIZED);
9315 if (x0_ref == 0 || x1_ref == 0)
9316 return 0;
9318 cc_loc_result = x0_ref;
9319 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
9321 add_loc_descr (&cc_loc_result, x1_ref);
9322 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
9324 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9325 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9327 return cc_loc_result;
9330 /* Return a descriptor that describes the concatenation of N
9331 locations. */
9333 static dw_loc_descr_ref
9334 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
9336 unsigned int i;
9337 dw_loc_descr_ref cc_loc_result = NULL;
9338 unsigned int n = XVECLEN (concatn, 0);
9340 for (i = 0; i < n; ++i)
9342 dw_loc_descr_ref ref;
9343 rtx x = XVECEXP (concatn, 0, i);
9345 ref = loc_descriptor (x, VAR_INIT_STATUS_INITIALIZED);
9346 if (ref == NULL)
9347 return NULL;
9349 add_loc_descr (&cc_loc_result, ref);
9350 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
9353 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9354 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9356 return cc_loc_result;
9359 /* Output a proper Dwarf location descriptor for a variable or parameter
9360 which is either allocated in a register or in a memory location. For a
9361 register, we just generate an OP_REG and the register number. For a
9362 memory location we provide a Dwarf postfix expression describing how to
9363 generate the (dynamic) address of the object onto the address stack.
9365 If we don't know how to describe it, return 0. */
9367 static dw_loc_descr_ref
9368 loc_descriptor (rtx rtl, enum var_init_status initialized)
9370 dw_loc_descr_ref loc_result = NULL;
9372 switch (GET_CODE (rtl))
9374 case SUBREG:
9375 /* The case of a subreg may arise when we have a local (register)
9376 variable or a formal (register) parameter which doesn't quite fill
9377 up an entire register. For now, just assume that it is
9378 legitimate to make the Dwarf info refer to the whole register which
9379 contains the given subreg. */
9380 rtl = SUBREG_REG (rtl);
9382 /* ... fall through ... */
9384 case REG:
9385 loc_result = reg_loc_descriptor (rtl, initialized);
9386 break;
9388 case MEM:
9389 loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
9390 initialized);
9391 break;
9393 case CONCAT:
9394 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
9395 initialized);
9396 break;
9398 case CONCATN:
9399 loc_result = concatn_loc_descriptor (rtl, initialized);
9400 break;
9402 case VAR_LOCATION:
9403 /* Single part. */
9404 if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
9406 loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), initialized);
9407 break;
9410 rtl = XEXP (rtl, 1);
9411 /* FALLTHRU */
9413 case PARALLEL:
9415 rtvec par_elems = XVEC (rtl, 0);
9416 int num_elem = GET_NUM_ELEM (par_elems);
9417 enum machine_mode mode;
9418 int i;
9420 /* Create the first one, so we have something to add to. */
9421 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
9422 initialized);
9423 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
9424 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9425 for (i = 1; i < num_elem; i++)
9427 dw_loc_descr_ref temp;
9429 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
9430 initialized);
9431 add_loc_descr (&loc_result, temp);
9432 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
9433 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9436 break;
9438 default:
9439 gcc_unreachable ();
9442 return loc_result;
9445 /* Similar, but generate the descriptor from trees instead of rtl. This comes
9446 up particularly with variable length arrays. WANT_ADDRESS is 2 if this is
9447 a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
9448 top-level invocation, and we require the address of LOC; is 0 if we require
9449 the value of LOC. */
9451 static dw_loc_descr_ref
9452 loc_descriptor_from_tree_1 (tree loc, int want_address)
9454 dw_loc_descr_ref ret, ret1;
9455 int have_address = 0;
9456 enum dwarf_location_atom op;
9458 /* ??? Most of the time we do not take proper care for sign/zero
9459 extending the values properly. Hopefully this won't be a real
9460 problem... */
9462 switch (TREE_CODE (loc))
9464 case ERROR_MARK:
9465 return 0;
9467 case PLACEHOLDER_EXPR:
9468 /* This case involves extracting fields from an object to determine the
9469 position of other fields. We don't try to encode this here. The
9470 only user of this is Ada, which encodes the needed information using
9471 the names of types. */
9472 return 0;
9474 case CALL_EXPR:
9475 return 0;
9477 case PREINCREMENT_EXPR:
9478 case PREDECREMENT_EXPR:
9479 case POSTINCREMENT_EXPR:
9480 case POSTDECREMENT_EXPR:
9481 /* There are no opcodes for these operations. */
9482 return 0;
9484 case ADDR_EXPR:
9485 /* If we already want an address, there's nothing we can do. */
9486 if (want_address)
9487 return 0;
9489 /* Otherwise, process the argument and look for the address. */
9490 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
9492 case VAR_DECL:
9493 if (DECL_THREAD_LOCAL_P (loc))
9495 rtx rtl;
9497 /* If this is not defined, we have no way to emit the data. */
9498 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
9499 return 0;
9501 /* The way DW_OP_GNU_push_tls_address is specified, we can only
9502 look up addresses of objects in the current module. */
9503 if (DECL_EXTERNAL (loc))
9504 return 0;
9506 rtl = rtl_for_decl_location (loc);
9507 if (rtl == NULL_RTX)
9508 return 0;
9510 if (!MEM_P (rtl))
9511 return 0;
9512 rtl = XEXP (rtl, 0);
9513 if (! CONSTANT_P (rtl))
9514 return 0;
9516 ret = new_loc_descr (INTERNAL_DW_OP_tls_addr, 0, 0);
9517 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9518 ret->dw_loc_oprnd1.v.val_addr = rtl;
9520 ret1 = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
9521 add_loc_descr (&ret, ret1);
9523 have_address = 1;
9524 break;
9526 /* FALLTHRU */
9528 case PARM_DECL:
9529 if (DECL_HAS_VALUE_EXPR_P (loc))
9530 return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
9531 want_address);
9532 /* FALLTHRU */
9534 case RESULT_DECL:
9535 case FUNCTION_DECL:
9537 rtx rtl = rtl_for_decl_location (loc);
9539 if (rtl == NULL_RTX)
9540 return 0;
9541 else if (GET_CODE (rtl) == CONST_INT)
9543 HOST_WIDE_INT val = INTVAL (rtl);
9544 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
9545 val &= GET_MODE_MASK (DECL_MODE (loc));
9546 ret = int_loc_descriptor (val);
9548 else if (GET_CODE (rtl) == CONST_STRING)
9549 return 0;
9550 else if (CONSTANT_P (rtl))
9552 ret = new_loc_descr (DW_OP_addr, 0, 0);
9553 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9554 ret->dw_loc_oprnd1.v.val_addr = rtl;
9556 else
9558 enum machine_mode mode;
9560 /* Certain constructs can only be represented at top-level. */
9561 if (want_address == 2)
9562 return loc_descriptor (rtl, VAR_INIT_STATUS_INITIALIZED);
9564 mode = GET_MODE (rtl);
9565 if (MEM_P (rtl))
9567 rtl = XEXP (rtl, 0);
9568 have_address = 1;
9570 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
9573 break;
9575 case INDIRECT_REF:
9576 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9577 have_address = 1;
9578 break;
9580 case COMPOUND_EXPR:
9581 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
9583 case NOP_EXPR:
9584 case CONVERT_EXPR:
9585 case NON_LVALUE_EXPR:
9586 case VIEW_CONVERT_EXPR:
9587 case SAVE_EXPR:
9588 case GIMPLE_MODIFY_STMT:
9589 return loc_descriptor_from_tree_1 (GENERIC_TREE_OPERAND (loc, 0),
9590 want_address);
9592 case COMPONENT_REF:
9593 case BIT_FIELD_REF:
9594 case ARRAY_REF:
9595 case ARRAY_RANGE_REF:
9597 tree obj, offset;
9598 HOST_WIDE_INT bitsize, bitpos, bytepos;
9599 enum machine_mode mode;
9600 int volatilep;
9601 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
9603 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
9604 &unsignedp, &volatilep, false);
9606 if (obj == loc)
9607 return 0;
9609 ret = loc_descriptor_from_tree_1 (obj, 1);
9610 if (ret == 0
9611 || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
9612 return 0;
9614 if (offset != NULL_TREE)
9616 /* Variable offset. */
9617 add_loc_descr (&ret, loc_descriptor_from_tree_1 (offset, 0));
9618 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9621 bytepos = bitpos / BITS_PER_UNIT;
9622 if (bytepos > 0)
9623 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
9624 else if (bytepos < 0)
9626 add_loc_descr (&ret, int_loc_descriptor (bytepos));
9627 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9630 have_address = 1;
9631 break;
9634 case INTEGER_CST:
9635 if (host_integerp (loc, 0))
9636 ret = int_loc_descriptor (tree_low_cst (loc, 0));
9637 else
9638 return 0;
9639 break;
9641 case CONSTRUCTOR:
9643 /* Get an RTL for this, if something has been emitted. */
9644 rtx rtl = lookup_constant_def (loc);
9645 enum machine_mode mode;
9647 if (!rtl || !MEM_P (rtl))
9648 return 0;
9649 mode = GET_MODE (rtl);
9650 rtl = XEXP (rtl, 0);
9651 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
9652 have_address = 1;
9653 break;
9656 case TRUTH_AND_EXPR:
9657 case TRUTH_ANDIF_EXPR:
9658 case BIT_AND_EXPR:
9659 op = DW_OP_and;
9660 goto do_binop;
9662 case TRUTH_XOR_EXPR:
9663 case BIT_XOR_EXPR:
9664 op = DW_OP_xor;
9665 goto do_binop;
9667 case TRUTH_OR_EXPR:
9668 case TRUTH_ORIF_EXPR:
9669 case BIT_IOR_EXPR:
9670 op = DW_OP_or;
9671 goto do_binop;
9673 case FLOOR_DIV_EXPR:
9674 case CEIL_DIV_EXPR:
9675 case ROUND_DIV_EXPR:
9676 case TRUNC_DIV_EXPR:
9677 op = DW_OP_div;
9678 goto do_binop;
9680 case MINUS_EXPR:
9681 op = DW_OP_minus;
9682 goto do_binop;
9684 case FLOOR_MOD_EXPR:
9685 case CEIL_MOD_EXPR:
9686 case ROUND_MOD_EXPR:
9687 case TRUNC_MOD_EXPR:
9688 op = DW_OP_mod;
9689 goto do_binop;
9691 case MULT_EXPR:
9692 op = DW_OP_mul;
9693 goto do_binop;
9695 case LSHIFT_EXPR:
9696 op = DW_OP_shl;
9697 goto do_binop;
9699 case RSHIFT_EXPR:
9700 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
9701 goto do_binop;
9703 case POINTER_PLUS_EXPR:
9704 case PLUS_EXPR:
9705 if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
9706 && host_integerp (TREE_OPERAND (loc, 1), 0))
9708 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9709 if (ret == 0)
9710 return 0;
9712 add_loc_descr (&ret,
9713 new_loc_descr (DW_OP_plus_uconst,
9714 tree_low_cst (TREE_OPERAND (loc, 1),
9716 0));
9717 break;
9720 op = DW_OP_plus;
9721 goto do_binop;
9723 case LE_EXPR:
9724 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9725 return 0;
9727 op = DW_OP_le;
9728 goto do_binop;
9730 case GE_EXPR:
9731 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9732 return 0;
9734 op = DW_OP_ge;
9735 goto do_binop;
9737 case LT_EXPR:
9738 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9739 return 0;
9741 op = DW_OP_lt;
9742 goto do_binop;
9744 case GT_EXPR:
9745 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9746 return 0;
9748 op = DW_OP_gt;
9749 goto do_binop;
9751 case EQ_EXPR:
9752 op = DW_OP_eq;
9753 goto do_binop;
9755 case NE_EXPR:
9756 op = DW_OP_ne;
9757 goto do_binop;
9759 do_binop:
9760 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9761 ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9762 if (ret == 0 || ret1 == 0)
9763 return 0;
9765 add_loc_descr (&ret, ret1);
9766 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9767 break;
9769 case TRUTH_NOT_EXPR:
9770 case BIT_NOT_EXPR:
9771 op = DW_OP_not;
9772 goto do_unop;
9774 case ABS_EXPR:
9775 op = DW_OP_abs;
9776 goto do_unop;
9778 case NEGATE_EXPR:
9779 op = DW_OP_neg;
9780 goto do_unop;
9782 do_unop:
9783 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9784 if (ret == 0)
9785 return 0;
9787 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9788 break;
9790 case MIN_EXPR:
9791 case MAX_EXPR:
9793 const enum tree_code code =
9794 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
9796 loc = build3 (COND_EXPR, TREE_TYPE (loc),
9797 build2 (code, integer_type_node,
9798 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
9799 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
9802 /* ... fall through ... */
9804 case COND_EXPR:
9806 dw_loc_descr_ref lhs
9807 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9808 dw_loc_descr_ref rhs
9809 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
9810 dw_loc_descr_ref bra_node, jump_node, tmp;
9812 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9813 if (ret == 0 || lhs == 0 || rhs == 0)
9814 return 0;
9816 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
9817 add_loc_descr (&ret, bra_node);
9819 add_loc_descr (&ret, rhs);
9820 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
9821 add_loc_descr (&ret, jump_node);
9823 add_loc_descr (&ret, lhs);
9824 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9825 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
9827 /* ??? Need a node to point the skip at. Use a nop. */
9828 tmp = new_loc_descr (DW_OP_nop, 0, 0);
9829 add_loc_descr (&ret, tmp);
9830 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9831 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
9833 break;
9835 case FIX_TRUNC_EXPR:
9836 return 0;
9838 default:
9839 /* Leave front-end specific codes as simply unknown. This comes
9840 up, for instance, with the C STMT_EXPR. */
9841 if ((unsigned int) TREE_CODE (loc)
9842 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
9843 return 0;
9845 #ifdef ENABLE_CHECKING
9846 /* Otherwise this is a generic code; we should just lists all of
9847 these explicitly. We forgot one. */
9848 gcc_unreachable ();
9849 #else
9850 /* In a release build, we want to degrade gracefully: better to
9851 generate incomplete debugging information than to crash. */
9852 return NULL;
9853 #endif
9856 /* Show if we can't fill the request for an address. */
9857 if (want_address && !have_address)
9858 return 0;
9860 /* If we've got an address and don't want one, dereference. */
9861 if (!want_address && have_address && ret)
9863 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
9865 if (size > DWARF2_ADDR_SIZE || size == -1)
9866 return 0;
9867 else if (size == DWARF2_ADDR_SIZE)
9868 op = DW_OP_deref;
9869 else
9870 op = DW_OP_deref_size;
9872 add_loc_descr (&ret, new_loc_descr (op, size, 0));
9875 return ret;
9878 static inline dw_loc_descr_ref
9879 loc_descriptor_from_tree (tree loc)
9881 return loc_descriptor_from_tree_1 (loc, 2);
9884 /* Given a value, round it up to the lowest multiple of `boundary'
9885 which is not less than the value itself. */
9887 static inline HOST_WIDE_INT
9888 ceiling (HOST_WIDE_INT value, unsigned int boundary)
9890 return (((value + boundary - 1) / boundary) * boundary);
9893 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
9894 pointer to the declared type for the relevant field variable, or return
9895 `integer_type_node' if the given node turns out to be an
9896 ERROR_MARK node. */
9898 static inline tree
9899 field_type (const_tree decl)
9901 tree type;
9903 if (TREE_CODE (decl) == ERROR_MARK)
9904 return integer_type_node;
9906 type = DECL_BIT_FIELD_TYPE (decl);
9907 if (type == NULL_TREE)
9908 type = TREE_TYPE (decl);
9910 return type;
9913 /* Given a pointer to a tree node, return the alignment in bits for
9914 it, or else return BITS_PER_WORD if the node actually turns out to
9915 be an ERROR_MARK node. */
9917 static inline unsigned
9918 simple_type_align_in_bits (const_tree type)
9920 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
9923 static inline unsigned
9924 simple_decl_align_in_bits (const_tree decl)
9926 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
9929 /* Return the result of rounding T up to ALIGN. */
9931 static inline HOST_WIDE_INT
9932 round_up_to_align (HOST_WIDE_INT t, unsigned int align)
9934 /* We must be careful if T is negative because HOST_WIDE_INT can be
9935 either "above" or "below" unsigned int as per the C promotion
9936 rules, depending on the host, thus making the signedness of the
9937 direct multiplication and division unpredictable. */
9938 unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
9940 u += align - 1;
9941 u /= align;
9942 u *= align;
9944 return (HOST_WIDE_INT) u;
9947 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
9948 lowest addressed byte of the "containing object" for the given FIELD_DECL,
9949 or return 0 if we are unable to determine what that offset is, either
9950 because the argument turns out to be a pointer to an ERROR_MARK node, or
9951 because the offset is actually variable. (We can't handle the latter case
9952 just yet). */
9954 static HOST_WIDE_INT
9955 field_byte_offset (const_tree decl)
9957 HOST_WIDE_INT object_offset_in_bits;
9958 HOST_WIDE_INT bitpos_int;
9960 if (TREE_CODE (decl) == ERROR_MARK)
9961 return 0;
9963 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
9965 /* We cannot yet cope with fields whose positions are variable, so
9966 for now, when we see such things, we simply return 0. Someday, we may
9967 be able to handle such cases, but it will be damn difficult. */
9968 if (! host_integerp (bit_position (decl), 0))
9969 return 0;
9971 bitpos_int = int_bit_position (decl);
9973 #ifdef PCC_BITFIELD_TYPE_MATTERS
9974 if (PCC_BITFIELD_TYPE_MATTERS)
9976 tree type;
9977 tree field_size_tree;
9978 HOST_WIDE_INT deepest_bitpos;
9979 unsigned HOST_WIDE_INT field_size_in_bits;
9980 unsigned int type_align_in_bits;
9981 unsigned int decl_align_in_bits;
9982 unsigned HOST_WIDE_INT type_size_in_bits;
9984 type = field_type (decl);
9985 field_size_tree = DECL_SIZE (decl);
9987 /* The size could be unspecified if there was an error, or for
9988 a flexible array member. */
9989 if (! field_size_tree)
9990 field_size_tree = bitsize_zero_node;
9992 /* If we don't know the size of the field, pretend it's a full word. */
9993 if (host_integerp (field_size_tree, 1))
9994 field_size_in_bits = tree_low_cst (field_size_tree, 1);
9995 else
9996 field_size_in_bits = BITS_PER_WORD;
9998 type_size_in_bits = simple_type_size_in_bits (type);
9999 type_align_in_bits = simple_type_align_in_bits (type);
10000 decl_align_in_bits = simple_decl_align_in_bits (decl);
10002 /* The GCC front-end doesn't make any attempt to keep track of the
10003 starting bit offset (relative to the start of the containing
10004 structure type) of the hypothetical "containing object" for a
10005 bit-field. Thus, when computing the byte offset value for the
10006 start of the "containing object" of a bit-field, we must deduce
10007 this information on our own. This can be rather tricky to do in
10008 some cases. For example, handling the following structure type
10009 definition when compiling for an i386/i486 target (which only
10010 aligns long long's to 32-bit boundaries) can be very tricky:
10012 struct S { int field1; long long field2:31; };
10014 Fortunately, there is a simple rule-of-thumb which can be used
10015 in such cases. When compiling for an i386/i486, GCC will
10016 allocate 8 bytes for the structure shown above. It decides to
10017 do this based upon one simple rule for bit-field allocation.
10018 GCC allocates each "containing object" for each bit-field at
10019 the first (i.e. lowest addressed) legitimate alignment boundary
10020 (based upon the required minimum alignment for the declared
10021 type of the field) which it can possibly use, subject to the
10022 condition that there is still enough available space remaining
10023 in the containing object (when allocated at the selected point)
10024 to fully accommodate all of the bits of the bit-field itself.
10026 This simple rule makes it obvious why GCC allocates 8 bytes for
10027 each object of the structure type shown above. When looking
10028 for a place to allocate the "containing object" for `field2',
10029 the compiler simply tries to allocate a 64-bit "containing
10030 object" at each successive 32-bit boundary (starting at zero)
10031 until it finds a place to allocate that 64- bit field such that
10032 at least 31 contiguous (and previously unallocated) bits remain
10033 within that selected 64 bit field. (As it turns out, for the
10034 example above, the compiler finds it is OK to allocate the
10035 "containing object" 64-bit field at bit-offset zero within the
10036 structure type.)
10038 Here we attempt to work backwards from the limited set of facts
10039 we're given, and we try to deduce from those facts, where GCC
10040 must have believed that the containing object started (within
10041 the structure type). The value we deduce is then used (by the
10042 callers of this routine) to generate DW_AT_location and
10043 DW_AT_bit_offset attributes for fields (both bit-fields and, in
10044 the case of DW_AT_location, regular fields as well). */
10046 /* Figure out the bit-distance from the start of the structure to
10047 the "deepest" bit of the bit-field. */
10048 deepest_bitpos = bitpos_int + field_size_in_bits;
10050 /* This is the tricky part. Use some fancy footwork to deduce
10051 where the lowest addressed bit of the containing object must
10052 be. */
10053 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
10055 /* Round up to type_align by default. This works best for
10056 bitfields. */
10057 object_offset_in_bits
10058 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
10060 if (object_offset_in_bits > bitpos_int)
10062 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
10064 /* Round up to decl_align instead. */
10065 object_offset_in_bits
10066 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
10069 else
10070 #endif
10071 object_offset_in_bits = bitpos_int;
10073 return object_offset_in_bits / BITS_PER_UNIT;
10076 /* The following routines define various Dwarf attributes and any data
10077 associated with them. */
10079 /* Add a location description attribute value to a DIE.
10081 This emits location attributes suitable for whole variables and
10082 whole parameters. Note that the location attributes for struct fields are
10083 generated by the routine `data_member_location_attribute' below. */
10085 static inline void
10086 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
10087 dw_loc_descr_ref descr)
10089 if (descr != 0)
10090 add_AT_loc (die, attr_kind, descr);
10093 /* Attach the specialized form of location attribute used for data members of
10094 struct and union types. In the special case of a FIELD_DECL node which
10095 represents a bit-field, the "offset" part of this special location
10096 descriptor must indicate the distance in bytes from the lowest-addressed
10097 byte of the containing struct or union type to the lowest-addressed byte of
10098 the "containing object" for the bit-field. (See the `field_byte_offset'
10099 function above).
10101 For any given bit-field, the "containing object" is a hypothetical object
10102 (of some integral or enum type) within which the given bit-field lives. The
10103 type of this hypothetical "containing object" is always the same as the
10104 declared type of the individual bit-field itself (for GCC anyway... the
10105 DWARF spec doesn't actually mandate this). Note that it is the size (in
10106 bytes) of the hypothetical "containing object" which will be given in the
10107 DW_AT_byte_size attribute for this bit-field. (See the
10108 `byte_size_attribute' function below.) It is also used when calculating the
10109 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
10110 function below.) */
10112 static void
10113 add_data_member_location_attribute (dw_die_ref die, tree decl)
10115 HOST_WIDE_INT offset;
10116 dw_loc_descr_ref loc_descr = 0;
10118 if (TREE_CODE (decl) == TREE_BINFO)
10120 /* We're working on the TAG_inheritance for a base class. */
10121 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
10123 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
10124 aren't at a fixed offset from all (sub)objects of the same
10125 type. We need to extract the appropriate offset from our
10126 vtable. The following dwarf expression means
10128 BaseAddr = ObAddr + *((*ObAddr) - Offset)
10130 This is specific to the V3 ABI, of course. */
10132 dw_loc_descr_ref tmp;
10134 /* Make a copy of the object address. */
10135 tmp = new_loc_descr (DW_OP_dup, 0, 0);
10136 add_loc_descr (&loc_descr, tmp);
10138 /* Extract the vtable address. */
10139 tmp = new_loc_descr (DW_OP_deref, 0, 0);
10140 add_loc_descr (&loc_descr, tmp);
10142 /* Calculate the address of the offset. */
10143 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
10144 gcc_assert (offset < 0);
10146 tmp = int_loc_descriptor (-offset);
10147 add_loc_descr (&loc_descr, tmp);
10148 tmp = new_loc_descr (DW_OP_minus, 0, 0);
10149 add_loc_descr (&loc_descr, tmp);
10151 /* Extract the offset. */
10152 tmp = new_loc_descr (DW_OP_deref, 0, 0);
10153 add_loc_descr (&loc_descr, tmp);
10155 /* Add it to the object address. */
10156 tmp = new_loc_descr (DW_OP_plus, 0, 0);
10157 add_loc_descr (&loc_descr, tmp);
10159 else
10160 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
10162 else
10163 offset = field_byte_offset (decl);
10165 if (! loc_descr)
10167 enum dwarf_location_atom op;
10169 /* The DWARF2 standard says that we should assume that the structure
10170 address is already on the stack, so we can specify a structure field
10171 address by using DW_OP_plus_uconst. */
10173 #ifdef MIPS_DEBUGGING_INFO
10174 /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
10175 operator correctly. It works only if we leave the offset on the
10176 stack. */
10177 op = DW_OP_constu;
10178 #else
10179 op = DW_OP_plus_uconst;
10180 #endif
10182 loc_descr = new_loc_descr (op, offset, 0);
10185 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
10188 /* Writes integer values to dw_vec_const array. */
10190 static void
10191 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
10193 while (size != 0)
10195 *dest++ = val & 0xff;
10196 val >>= 8;
10197 --size;
10201 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
10203 static HOST_WIDE_INT
10204 extract_int (const unsigned char *src, unsigned int size)
10206 HOST_WIDE_INT val = 0;
10208 src += size;
10209 while (size != 0)
10211 val <<= 8;
10212 val |= *--src & 0xff;
10213 --size;
10215 return val;
10218 /* Writes floating point values to dw_vec_const array. */
10220 static void
10221 insert_float (const_rtx rtl, unsigned char *array)
10223 REAL_VALUE_TYPE rv;
10224 long val[4];
10225 int i;
10227 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
10228 real_to_target (val, &rv, GET_MODE (rtl));
10230 /* real_to_target puts 32-bit pieces in each long. Pack them. */
10231 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
10233 insert_int (val[i], 4, array);
10234 array += 4;
10238 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
10239 does not have a "location" either in memory or in a register. These
10240 things can arise in GNU C when a constant is passed as an actual parameter
10241 to an inlined function. They can also arise in C++ where declared
10242 constants do not necessarily get memory "homes". */
10244 static void
10245 add_const_value_attribute (dw_die_ref die, rtx rtl)
10247 switch (GET_CODE (rtl))
10249 case CONST_INT:
10251 HOST_WIDE_INT val = INTVAL (rtl);
10253 if (val < 0)
10254 add_AT_int (die, DW_AT_const_value, val);
10255 else
10256 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
10258 break;
10260 case CONST_DOUBLE:
10261 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
10262 floating-point constant. A CONST_DOUBLE is used whenever the
10263 constant requires more than one word in order to be adequately
10264 represented. We output CONST_DOUBLEs as blocks. */
10266 enum machine_mode mode = GET_MODE (rtl);
10268 if (SCALAR_FLOAT_MODE_P (mode))
10270 unsigned int length = GET_MODE_SIZE (mode);
10271 unsigned char *array = ggc_alloc (length);
10273 insert_float (rtl, array);
10274 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
10276 else
10278 /* ??? We really should be using HOST_WIDE_INT throughout. */
10279 gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
10281 add_AT_long_long (die, DW_AT_const_value,
10282 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
10285 break;
10287 case CONST_VECTOR:
10289 enum machine_mode mode = GET_MODE (rtl);
10290 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
10291 unsigned int length = CONST_VECTOR_NUNITS (rtl);
10292 unsigned char *array = ggc_alloc (length * elt_size);
10293 unsigned int i;
10294 unsigned char *p;
10296 switch (GET_MODE_CLASS (mode))
10298 case MODE_VECTOR_INT:
10299 for (i = 0, p = array; i < length; i++, p += elt_size)
10301 rtx elt = CONST_VECTOR_ELT (rtl, i);
10302 HOST_WIDE_INT lo, hi;
10304 switch (GET_CODE (elt))
10306 case CONST_INT:
10307 lo = INTVAL (elt);
10308 hi = -(lo < 0);
10309 break;
10311 case CONST_DOUBLE:
10312 lo = CONST_DOUBLE_LOW (elt);
10313 hi = CONST_DOUBLE_HIGH (elt);
10314 break;
10316 default:
10317 gcc_unreachable ();
10320 if (elt_size <= sizeof (HOST_WIDE_INT))
10321 insert_int (lo, elt_size, p);
10322 else
10324 unsigned char *p0 = p;
10325 unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
10327 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
10328 if (WORDS_BIG_ENDIAN)
10330 p0 = p1;
10331 p1 = p;
10333 insert_int (lo, sizeof (HOST_WIDE_INT), p0);
10334 insert_int (hi, sizeof (HOST_WIDE_INT), p1);
10337 break;
10339 case MODE_VECTOR_FLOAT:
10340 for (i = 0, p = array; i < length; i++, p += elt_size)
10342 rtx elt = CONST_VECTOR_ELT (rtl, i);
10343 insert_float (elt, p);
10345 break;
10347 default:
10348 gcc_unreachable ();
10351 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
10353 break;
10355 case CONST_STRING:
10356 add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
10357 break;
10359 case SYMBOL_REF:
10360 case LABEL_REF:
10361 case CONST:
10362 add_AT_addr (die, DW_AT_const_value, rtl);
10363 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
10364 break;
10366 case PLUS:
10367 /* In cases where an inlined instance of an inline function is passed
10368 the address of an `auto' variable (which is local to the caller) we
10369 can get a situation where the DECL_RTL of the artificial local
10370 variable (for the inlining) which acts as a stand-in for the
10371 corresponding formal parameter (of the inline function) will look
10372 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
10373 exactly a compile-time constant expression, but it isn't the address
10374 of the (artificial) local variable either. Rather, it represents the
10375 *value* which the artificial local variable always has during its
10376 lifetime. We currently have no way to represent such quasi-constant
10377 values in Dwarf, so for now we just punt and generate nothing. */
10378 break;
10380 default:
10381 /* No other kinds of rtx should be possible here. */
10382 gcc_unreachable ();
10387 /* Determine whether the evaluation of EXPR references any variables
10388 or functions which aren't otherwise used (and therefore may not be
10389 output). */
10390 static tree
10391 reference_to_unused (tree * tp, int * walk_subtrees,
10392 void * data ATTRIBUTE_UNUSED)
10394 if (! EXPR_P (*tp) && ! GIMPLE_STMT_P (*tp) && ! CONSTANT_CLASS_P (*tp))
10395 *walk_subtrees = 0;
10397 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
10398 && ! TREE_ASM_WRITTEN (*tp))
10399 return *tp;
10400 else if (!flag_unit_at_a_time)
10401 return NULL_TREE;
10402 /* ??? The C++ FE emits debug information for using decls, so
10403 putting gcc_unreachable here falls over. See PR31899. For now
10404 be conservative. */
10405 else if (!cgraph_global_info_ready
10406 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
10407 return *tp;
10408 else if (DECL_P (*tp) && TREE_CODE (*tp) == VAR_DECL)
10410 struct varpool_node *node = varpool_node (*tp);
10411 if (!node->needed)
10412 return *tp;
10414 else if (DECL_P (*tp) && TREE_CODE (*tp) == FUNCTION_DECL
10415 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
10417 struct cgraph_node *node = cgraph_node (*tp);
10418 if (!node->output)
10419 return *tp;
10421 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
10422 return *tp;
10424 return NULL_TREE;
10427 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
10428 for use in a later add_const_value_attribute call. */
10430 static rtx
10431 rtl_for_decl_init (tree init, tree type)
10433 rtx rtl = NULL_RTX;
10435 /* If a variable is initialized with a string constant without embedded
10436 zeros, build CONST_STRING. */
10437 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
10439 tree enttype = TREE_TYPE (type);
10440 tree domain = TYPE_DOMAIN (type);
10441 enum machine_mode mode = TYPE_MODE (enttype);
10443 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
10444 && domain
10445 && integer_zerop (TYPE_MIN_VALUE (domain))
10446 && compare_tree_int (TYPE_MAX_VALUE (domain),
10447 TREE_STRING_LENGTH (init) - 1) == 0
10448 && ((size_t) TREE_STRING_LENGTH (init)
10449 == strlen (TREE_STRING_POINTER (init)) + 1))
10450 rtl = gen_rtx_CONST_STRING (VOIDmode,
10451 ggc_strdup (TREE_STRING_POINTER (init)));
10453 /* Other aggregates, and complex values, could be represented using
10454 CONCAT: FIXME! */
10455 else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
10457 /* Vectors only work if their mode is supported by the target.
10458 FIXME: generic vectors ought to work too. */
10459 else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
10461 /* If the initializer is something that we know will expand into an
10462 immediate RTL constant, expand it now. We must be careful not to
10463 reference variables which won't be output. */
10464 else if (initializer_constant_valid_p (init, type)
10465 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
10467 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
10468 possible. */
10469 if (TREE_CODE (type) == VECTOR_TYPE)
10470 switch (TREE_CODE (init))
10472 case VECTOR_CST:
10473 break;
10474 case CONSTRUCTOR:
10475 if (TREE_CONSTANT (init))
10477 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
10478 bool constant_p = true;
10479 tree value;
10480 unsigned HOST_WIDE_INT ix;
10482 /* Even when ctor is constant, it might contain non-*_CST
10483 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
10484 belong into VECTOR_CST nodes. */
10485 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
10486 if (!CONSTANT_CLASS_P (value))
10488 constant_p = false;
10489 break;
10492 if (constant_p)
10494 init = build_vector_from_ctor (type, elts);
10495 break;
10498 /* FALLTHRU */
10500 default:
10501 return NULL;
10504 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
10506 /* If expand_expr returns a MEM, it wasn't immediate. */
10507 gcc_assert (!rtl || !MEM_P (rtl));
10510 return rtl;
10513 /* This is a specialized subset of expand_expr to evaluate a DECL_VALUE_EXPR.
10514 We stop if we find decls that haven't been expanded, or if the expression is
10515 getting so complex we won't be able to represent it anyway. Returns NULL on
10516 failure. */
10518 static rtx
10519 dw_expand_expr (tree expr)
10521 switch (TREE_CODE (expr))
10523 case VAR_DECL:
10524 case PARM_DECL:
10525 if (DECL_HAS_VALUE_EXPR_P (expr))
10526 return dw_expand_expr (DECL_VALUE_EXPR (expr));
10527 /* FALLTHRU */
10529 case CONST_DECL:
10530 case RESULT_DECL:
10531 return DECL_RTL_IF_SET (expr);
10533 case INTEGER_CST:
10534 return expand_expr (expr, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
10536 case COMPONENT_REF:
10537 case ARRAY_REF:
10538 case ARRAY_RANGE_REF:
10539 case BIT_FIELD_REF:
10541 enum machine_mode mode;
10542 HOST_WIDE_INT bitsize, bitpos;
10543 tree offset, tem;
10544 int volatilep = 0, unsignedp = 0;
10545 rtx x;
10547 tem = get_inner_reference (expr, &bitsize, &bitpos, &offset,
10548 &mode, &unsignedp, &volatilep, true);
10550 x = dw_expand_expr (tem);
10551 if (x == NULL || !MEM_P (x))
10552 return NULL;
10553 if (offset != NULL)
10555 if (!host_integerp (offset, 0))
10556 return NULL;
10557 x = adjust_address_nv (x, mode, tree_low_cst (offset, 0));
10559 if (bitpos != 0)
10560 x = adjust_address_nv (x, mode, bitpos / BITS_PER_UNIT);
10562 return x;
10565 default:
10566 return NULL;
10570 /* Generate RTL for the variable DECL to represent its location. */
10572 static rtx
10573 rtl_for_decl_location (tree decl)
10575 rtx rtl;
10577 /* Here we have to decide where we are going to say the parameter "lives"
10578 (as far as the debugger is concerned). We only have a couple of
10579 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
10581 DECL_RTL normally indicates where the parameter lives during most of the
10582 activation of the function. If optimization is enabled however, this
10583 could be either NULL or else a pseudo-reg. Both of those cases indicate
10584 that the parameter doesn't really live anywhere (as far as the code
10585 generation parts of GCC are concerned) during most of the function's
10586 activation. That will happen (for example) if the parameter is never
10587 referenced within the function.
10589 We could just generate a location descriptor here for all non-NULL
10590 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
10591 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
10592 where DECL_RTL is NULL or is a pseudo-reg.
10594 Note however that we can only get away with using DECL_INCOMING_RTL as
10595 a backup substitute for DECL_RTL in certain limited cases. In cases
10596 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
10597 we can be sure that the parameter was passed using the same type as it is
10598 declared to have within the function, and that its DECL_INCOMING_RTL
10599 points us to a place where a value of that type is passed.
10601 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
10602 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
10603 because in these cases DECL_INCOMING_RTL points us to a value of some
10604 type which is *different* from the type of the parameter itself. Thus,
10605 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
10606 such cases, the debugger would end up (for example) trying to fetch a
10607 `float' from a place which actually contains the first part of a
10608 `double'. That would lead to really incorrect and confusing
10609 output at debug-time.
10611 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
10612 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
10613 are a couple of exceptions however. On little-endian machines we can
10614 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
10615 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
10616 an integral type that is smaller than TREE_TYPE (decl). These cases arise
10617 when (on a little-endian machine) a non-prototyped function has a
10618 parameter declared to be of type `short' or `char'. In such cases,
10619 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
10620 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
10621 passed `int' value. If the debugger then uses that address to fetch
10622 a `short' or a `char' (on a little-endian machine) the result will be
10623 the correct data, so we allow for such exceptional cases below.
10625 Note that our goal here is to describe the place where the given formal
10626 parameter lives during most of the function's activation (i.e. between the
10627 end of the prologue and the start of the epilogue). We'll do that as best
10628 as we can. Note however that if the given formal parameter is modified
10629 sometime during the execution of the function, then a stack backtrace (at
10630 debug-time) will show the function as having been called with the *new*
10631 value rather than the value which was originally passed in. This happens
10632 rarely enough that it is not a major problem, but it *is* a problem, and
10633 I'd like to fix it.
10635 A future version of dwarf2out.c may generate two additional attributes for
10636 any given DW_TAG_formal_parameter DIE which will describe the "passed
10637 type" and the "passed location" for the given formal parameter in addition
10638 to the attributes we now generate to indicate the "declared type" and the
10639 "active location" for each parameter. This additional set of attributes
10640 could be used by debuggers for stack backtraces. Separately, note that
10641 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
10642 This happens (for example) for inlined-instances of inline function formal
10643 parameters which are never referenced. This really shouldn't be
10644 happening. All PARM_DECL nodes should get valid non-NULL
10645 DECL_INCOMING_RTL values. FIXME. */
10647 /* Use DECL_RTL as the "location" unless we find something better. */
10648 rtl = DECL_RTL_IF_SET (decl);
10650 /* When generating abstract instances, ignore everything except
10651 constants, symbols living in memory, and symbols living in
10652 fixed registers. */
10653 if (! reload_completed)
10655 if (rtl
10656 && (CONSTANT_P (rtl)
10657 || (MEM_P (rtl)
10658 && CONSTANT_P (XEXP (rtl, 0)))
10659 || (REG_P (rtl)
10660 && TREE_CODE (decl) == VAR_DECL
10661 && TREE_STATIC (decl))))
10663 rtl = targetm.delegitimize_address (rtl);
10664 return rtl;
10666 rtl = NULL_RTX;
10668 else if (TREE_CODE (decl) == PARM_DECL)
10670 if (rtl == NULL_RTX || is_pseudo_reg (rtl))
10672 tree declared_type = TREE_TYPE (decl);
10673 tree passed_type = DECL_ARG_TYPE (decl);
10674 enum machine_mode dmode = TYPE_MODE (declared_type);
10675 enum machine_mode pmode = TYPE_MODE (passed_type);
10677 /* This decl represents a formal parameter which was optimized out.
10678 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
10679 all cases where (rtl == NULL_RTX) just below. */
10680 if (dmode == pmode)
10681 rtl = DECL_INCOMING_RTL (decl);
10682 else if (SCALAR_INT_MODE_P (dmode)
10683 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
10684 && DECL_INCOMING_RTL (decl))
10686 rtx inc = DECL_INCOMING_RTL (decl);
10687 if (REG_P (inc))
10688 rtl = inc;
10689 else if (MEM_P (inc))
10691 if (BYTES_BIG_ENDIAN)
10692 rtl = adjust_address_nv (inc, dmode,
10693 GET_MODE_SIZE (pmode)
10694 - GET_MODE_SIZE (dmode));
10695 else
10696 rtl = inc;
10701 /* If the parm was passed in registers, but lives on the stack, then
10702 make a big endian correction if the mode of the type of the
10703 parameter is not the same as the mode of the rtl. */
10704 /* ??? This is the same series of checks that are made in dbxout.c before
10705 we reach the big endian correction code there. It isn't clear if all
10706 of these checks are necessary here, but keeping them all is the safe
10707 thing to do. */
10708 else if (MEM_P (rtl)
10709 && XEXP (rtl, 0) != const0_rtx
10710 && ! CONSTANT_P (XEXP (rtl, 0))
10711 /* Not passed in memory. */
10712 && !MEM_P (DECL_INCOMING_RTL (decl))
10713 /* Not passed by invisible reference. */
10714 && (!REG_P (XEXP (rtl, 0))
10715 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
10716 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
10717 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
10718 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
10719 #endif
10721 /* Big endian correction check. */
10722 && BYTES_BIG_ENDIAN
10723 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
10724 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
10725 < UNITS_PER_WORD))
10727 int offset = (UNITS_PER_WORD
10728 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
10730 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10731 plus_constant (XEXP (rtl, 0), offset));
10734 else if (TREE_CODE (decl) == VAR_DECL
10735 && rtl
10736 && MEM_P (rtl)
10737 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
10738 && BYTES_BIG_ENDIAN)
10740 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
10741 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
10743 /* If a variable is declared "register" yet is smaller than
10744 a register, then if we store the variable to memory, it
10745 looks like we're storing a register-sized value, when in
10746 fact we are not. We need to adjust the offset of the
10747 storage location to reflect the actual value's bytes,
10748 else gdb will not be able to display it. */
10749 if (rsize > dsize)
10750 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10751 plus_constant (XEXP (rtl, 0), rsize-dsize));
10754 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
10755 and will have been substituted directly into all expressions that use it.
10756 C does not have such a concept, but C++ and other languages do. */
10757 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
10758 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
10760 if (rtl)
10761 rtl = targetm.delegitimize_address (rtl);
10763 /* If we don't look past the constant pool, we risk emitting a
10764 reference to a constant pool entry that isn't referenced from
10765 code, and thus is not emitted. */
10766 if (rtl)
10767 rtl = avoid_constant_pool_reference (rtl);
10769 return rtl;
10772 /* We need to figure out what section we should use as the base for the
10773 address ranges where a given location is valid.
10774 1. If this particular DECL has a section associated with it, use that.
10775 2. If this function has a section associated with it, use that.
10776 3. Otherwise, use the text section.
10777 XXX: If you split a variable across multiple sections, we won't notice. */
10779 static const char *
10780 secname_for_decl (const_tree decl)
10782 const char *secname;
10784 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
10786 tree sectree = DECL_SECTION_NAME (decl);
10787 secname = TREE_STRING_POINTER (sectree);
10789 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
10791 tree sectree = DECL_SECTION_NAME (current_function_decl);
10792 secname = TREE_STRING_POINTER (sectree);
10794 else if (cfun && in_cold_section_p)
10795 secname = crtl->subsections.cold_section_label;
10796 else
10797 secname = text_section_label;
10799 return secname;
10802 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_RTX is returned.
10803 If so, the rtx for the SYMBOL_REF for the COMMON block is returned, and the
10804 value is the offset into the common block for the symbol. */
10806 static rtx
10807 common_check (tree decl, HOST_WIDE_INT *value)
10809 rtx home;
10810 rtx sym_addr;
10811 rtx res = NULL_RTX;
10813 /* If the decl isn't a VAR_DECL, or if it isn't public or static, or if
10814 it does not have a value (the offset into the common area), or if it
10815 is thread local (as opposed to global) then it isn't common, and shouldn't
10816 be handled as such. */
10817 if (TREE_CODE (decl) != VAR_DECL
10818 || !TREE_PUBLIC(decl)
10819 || !TREE_STATIC(decl)
10820 || !DECL_HAS_VALUE_EXPR_P(decl)
10821 || DECL_THREAD_LOCAL_P (decl)
10822 || !is_fortran())
10823 return NULL;
10825 home = DECL_RTL (decl);
10826 if (home == NULL_RTX || GET_CODE (home) != MEM)
10827 return NULL;
10829 sym_addr = dw_expand_expr (DECL_VALUE_EXPR (decl));
10830 if (sym_addr == NULL_RTX || GET_CODE (sym_addr) != MEM)
10831 return NULL;
10833 sym_addr = XEXP (sym_addr, 0);
10834 if (GET_CODE (sym_addr) == CONST)
10835 sym_addr = XEXP (sym_addr, 0);
10836 if ((GET_CODE (sym_addr) == SYMBOL_REF || GET_CODE (sym_addr) == PLUS)
10837 && DECL_INITIAL (decl) == 0)
10840 /* We have a sym that will go into a common area, meaning that it
10841 will get storage reserved with a .comm/.lcomm assembler pseudo-op.
10843 Determine name of common area this symbol will be an offset into,
10844 and offset into that area. Also retrieve the decl for the area
10845 that the symbol is offset into. */
10846 tree cdecl = NULL;
10848 switch (GET_CODE (sym_addr))
10850 case PLUS:
10851 if (GET_CODE (XEXP (sym_addr, 0)) == CONST_INT)
10853 res = XEXP (sym_addr, 1);
10854 *value = INTVAL (XEXP (sym_addr, 0));
10855 cdecl = SYMBOL_REF_DECL (XEXP (sym_addr, 1));
10857 else
10859 res = XEXP (sym_addr, 0);
10860 *value = INTVAL (XEXP (sym_addr, 1));
10861 cdecl = SYMBOL_REF_DECL (XEXP (sym_addr, 0));
10863 break;
10865 case SYMBOL_REF:
10866 res = sym_addr;
10867 *value = 0;
10868 cdecl = SYMBOL_REF_DECL (sym_addr);
10869 break;
10871 default:
10872 error ("common symbol debug info is not structured as "
10873 "symbol+offset");
10876 /* Check area common symbol is offset into. If this is not public, then
10877 it is not a symbol in a common block. It must be a .lcomm symbol, not
10878 a .comm symbol. */
10879 if (cdecl == NULL || !TREE_PUBLIC(cdecl))
10880 res = NULL_RTX;
10882 else
10883 res = NULL_RTX;
10885 return res;
10889 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
10890 data attribute for a variable or a parameter. We generate the
10891 DW_AT_const_value attribute only in those cases where the given variable
10892 or parameter does not have a true "location" either in memory or in a
10893 register. This can happen (for example) when a constant is passed as an
10894 actual argument in a call to an inline function. (It's possible that
10895 these things can crop up in other ways also.) Note that one type of
10896 constant value which can be passed into an inlined function is a constant
10897 pointer. This can happen for example if an actual argument in an inlined
10898 function call evaluates to a compile-time constant address. */
10900 static void
10901 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
10902 enum dwarf_attribute attr)
10904 rtx rtl;
10905 dw_loc_descr_ref descr;
10906 var_loc_list *loc_list;
10907 struct var_loc_node *node;
10908 if (TREE_CODE (decl) == ERROR_MARK)
10909 return;
10911 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
10912 || TREE_CODE (decl) == RESULT_DECL);
10914 /* See if we possibly have multiple locations for this variable. */
10915 loc_list = lookup_decl_loc (decl);
10917 /* If it truly has multiple locations, the first and last node will
10918 differ. */
10919 if (loc_list && loc_list->first != loc_list->last)
10921 const char *endname, *secname;
10922 dw_loc_list_ref list;
10923 rtx varloc;
10924 enum var_init_status initialized;
10926 /* Now that we know what section we are using for a base,
10927 actually construct the list of locations.
10928 The first location information is what is passed to the
10929 function that creates the location list, and the remaining
10930 locations just get added on to that list.
10931 Note that we only know the start address for a location
10932 (IE location changes), so to build the range, we use
10933 the range [current location start, next location start].
10934 This means we have to special case the last node, and generate
10935 a range of [last location start, end of function label]. */
10937 node = loc_list->first;
10938 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10939 secname = secname_for_decl (decl);
10941 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note))
10942 initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10943 else
10944 initialized = VAR_INIT_STATUS_INITIALIZED;
10946 list = new_loc_list (loc_descriptor (varloc, initialized),
10947 node->label, node->next->label, secname, 1);
10948 node = node->next;
10950 for (; node->next; node = node->next)
10951 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10953 /* The variable has a location between NODE->LABEL and
10954 NODE->NEXT->LABEL. */
10955 enum var_init_status initialized =
10956 NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10957 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10958 add_loc_descr_to_loc_list (&list,
10959 loc_descriptor (varloc, initialized),
10960 node->label, node->next->label, secname);
10963 /* If the variable has a location at the last label
10964 it keeps its location until the end of function. */
10965 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10967 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
10968 enum var_init_status initialized =
10969 NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10971 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10972 if (!current_function_decl)
10973 endname = text_end_label;
10974 else
10976 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
10977 current_function_funcdef_no);
10978 endname = ggc_strdup (label_id);
10980 add_loc_descr_to_loc_list (&list,
10981 loc_descriptor (varloc, initialized),
10982 node->label, endname, secname);
10985 /* Finally, add the location list to the DIE, and we are done. */
10986 add_AT_loc_list (die, attr, list);
10987 return;
10990 /* Try to get some constant RTL for this decl, and use that as the value of
10991 the location. */
10993 rtl = rtl_for_decl_location (decl);
10994 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
10996 add_const_value_attribute (die, rtl);
10997 return;
11000 /* If we have tried to generate the location otherwise, and it
11001 didn't work out (we wouldn't be here if we did), and we have a one entry
11002 location list, try generating a location from that. */
11003 if (loc_list && loc_list->first)
11005 enum var_init_status status;
11006 node = loc_list->first;
11007 status = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11008 descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note), status);
11009 if (descr)
11011 add_AT_location_description (die, attr, descr);
11012 return;
11016 /* We couldn't get any rtl, so try directly generating the location
11017 description from the tree. */
11018 descr = loc_descriptor_from_tree (decl);
11019 if (descr)
11021 add_AT_location_description (die, attr, descr);
11022 return;
11024 /* None of that worked, so it must not really have a location;
11025 try adding a constant value attribute from the DECL_INITIAL. */
11026 tree_add_const_value_attribute (die, decl);
11029 /* If we don't have a copy of this variable in memory for some reason (such
11030 as a C++ member constant that doesn't have an out-of-line definition),
11031 we should tell the debugger about the constant value. */
11033 static void
11034 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
11036 tree init = DECL_INITIAL (decl);
11037 tree type = TREE_TYPE (decl);
11038 rtx rtl;
11040 if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
11041 /* OK */;
11042 else
11043 return;
11045 rtl = rtl_for_decl_init (init, type);
11046 if (rtl)
11047 add_const_value_attribute (var_die, rtl);
11050 /* Convert the CFI instructions for the current function into a
11051 location list. This is used for DW_AT_frame_base when we targeting
11052 a dwarf2 consumer that does not support the dwarf3
11053 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
11054 expressions. */
11056 static dw_loc_list_ref
11057 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
11059 dw_fde_ref fde;
11060 dw_loc_list_ref list, *list_tail;
11061 dw_cfi_ref cfi;
11062 dw_cfa_location last_cfa, next_cfa;
11063 const char *start_label, *last_label, *section;
11065 fde = &fde_table[fde_table_in_use - 1];
11067 section = secname_for_decl (current_function_decl);
11068 list_tail = &list;
11069 list = NULL;
11071 next_cfa.reg = INVALID_REGNUM;
11072 next_cfa.offset = 0;
11073 next_cfa.indirect = 0;
11074 next_cfa.base_offset = 0;
11076 start_label = fde->dw_fde_begin;
11078 /* ??? Bald assumption that the CIE opcode list does not contain
11079 advance opcodes. */
11080 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
11081 lookup_cfa_1 (cfi, &next_cfa);
11083 last_cfa = next_cfa;
11084 last_label = start_label;
11086 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
11087 switch (cfi->dw_cfi_opc)
11089 case DW_CFA_set_loc:
11090 case DW_CFA_advance_loc1:
11091 case DW_CFA_advance_loc2:
11092 case DW_CFA_advance_loc4:
11093 if (!cfa_equal_p (&last_cfa, &next_cfa))
11095 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
11096 start_label, last_label, section,
11097 list == NULL);
11099 list_tail = &(*list_tail)->dw_loc_next;
11100 last_cfa = next_cfa;
11101 start_label = last_label;
11103 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
11104 break;
11106 case DW_CFA_advance_loc:
11107 /* The encoding is complex enough that we should never emit this. */
11108 case DW_CFA_remember_state:
11109 case DW_CFA_restore_state:
11110 /* We don't handle these two in this function. It would be possible
11111 if it were to be required. */
11112 gcc_unreachable ();
11114 default:
11115 lookup_cfa_1 (cfi, &next_cfa);
11116 break;
11119 if (!cfa_equal_p (&last_cfa, &next_cfa))
11121 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
11122 start_label, last_label, section,
11123 list == NULL);
11124 list_tail = &(*list_tail)->dw_loc_next;
11125 start_label = last_label;
11127 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
11128 start_label, fde->dw_fde_end, section,
11129 list == NULL);
11131 return list;
11134 /* Compute a displacement from the "steady-state frame pointer" to the
11135 frame base (often the same as the CFA), and store it in
11136 frame_pointer_fb_offset. OFFSET is added to the displacement
11137 before the latter is negated. */
11139 static void
11140 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
11142 rtx reg, elim;
11144 #ifdef FRAME_POINTER_CFA_OFFSET
11145 reg = frame_pointer_rtx;
11146 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
11147 #else
11148 reg = arg_pointer_rtx;
11149 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
11150 #endif
11152 elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
11153 if (GET_CODE (elim) == PLUS)
11155 offset += INTVAL (XEXP (elim, 1));
11156 elim = XEXP (elim, 0);
11158 gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
11159 : stack_pointer_rtx));
11161 frame_pointer_fb_offset = -offset;
11164 /* Generate a DW_AT_name attribute given some string value to be included as
11165 the value of the attribute. */
11167 static void
11168 add_name_attribute (dw_die_ref die, const char *name_string)
11170 if (name_string != NULL && *name_string != 0)
11172 if (demangle_name_func)
11173 name_string = (*demangle_name_func) (name_string);
11175 add_AT_string (die, DW_AT_name, name_string);
11179 /* Generate a DW_AT_comp_dir attribute for DIE. */
11181 static void
11182 add_comp_dir_attribute (dw_die_ref die)
11184 const char *wd = get_src_pwd ();
11185 if (wd != NULL)
11186 add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
11189 /* Given a tree node describing an array bound (either lower or upper) output
11190 a representation for that bound. */
11192 static void
11193 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
11195 switch (TREE_CODE (bound))
11197 case ERROR_MARK:
11198 return;
11200 /* All fixed-bounds are represented by INTEGER_CST nodes. */
11201 case INTEGER_CST:
11202 if (! host_integerp (bound, 0)
11203 || (bound_attr == DW_AT_lower_bound
11204 && (((is_c_family () || is_java ()) && integer_zerop (bound))
11205 || (is_fortran () && integer_onep (bound)))))
11206 /* Use the default. */
11208 else
11209 add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
11210 break;
11212 case CONVERT_EXPR:
11213 case NOP_EXPR:
11214 case NON_LVALUE_EXPR:
11215 case VIEW_CONVERT_EXPR:
11216 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
11217 break;
11219 case SAVE_EXPR:
11220 break;
11222 case VAR_DECL:
11223 case PARM_DECL:
11224 case RESULT_DECL:
11226 dw_die_ref decl_die = lookup_decl_die (bound);
11228 /* ??? Can this happen, or should the variable have been bound
11229 first? Probably it can, since I imagine that we try to create
11230 the types of parameters in the order in which they exist in
11231 the list, and won't have created a forward reference to a
11232 later parameter. */
11233 if (decl_die != NULL)
11234 add_AT_die_ref (subrange_die, bound_attr, decl_die);
11235 break;
11238 default:
11240 /* Otherwise try to create a stack operation procedure to
11241 evaluate the value of the array bound. */
11243 dw_die_ref ctx, decl_die;
11244 dw_loc_descr_ref loc;
11246 loc = loc_descriptor_from_tree (bound);
11247 if (loc == NULL)
11248 break;
11250 if (current_function_decl == 0)
11251 ctx = comp_unit_die;
11252 else
11253 ctx = lookup_decl_die (current_function_decl);
11255 decl_die = new_die (DW_TAG_variable, ctx, bound);
11256 add_AT_flag (decl_die, DW_AT_artificial, 1);
11257 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
11258 add_AT_loc (decl_die, DW_AT_location, loc);
11260 add_AT_die_ref (subrange_die, bound_attr, decl_die);
11261 break;
11266 /* Note that the block of subscript information for an array type also
11267 includes information about the element type of type given array type. */
11269 static void
11270 add_subscript_info (dw_die_ref type_die, tree type)
11272 #ifndef MIPS_DEBUGGING_INFO
11273 unsigned dimension_number;
11274 #endif
11275 tree lower, upper;
11276 dw_die_ref subrange_die;
11278 /* The GNU compilers represent multidimensional array types as sequences of
11279 one dimensional array types whose element types are themselves array
11280 types. Here we squish that down, so that each multidimensional array
11281 type gets only one array_type DIE in the Dwarf debugging info. The draft
11282 Dwarf specification say that we are allowed to do this kind of
11283 compression in C (because there is no difference between an array or
11284 arrays and a multidimensional array in C) but for other source languages
11285 (e.g. Ada) we probably shouldn't do this. */
11287 /* ??? The SGI dwarf reader fails for multidimensional arrays with a
11288 const enum type. E.g. const enum machine_mode insn_operand_mode[2][10].
11289 We work around this by disabling this feature. See also
11290 gen_array_type_die. */
11291 #ifndef MIPS_DEBUGGING_INFO
11292 for (dimension_number = 0;
11293 TREE_CODE (type) == ARRAY_TYPE;
11294 type = TREE_TYPE (type), dimension_number++)
11295 #endif
11297 tree domain = TYPE_DOMAIN (type);
11299 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
11300 and (in GNU C only) variable bounds. Handle all three forms
11301 here. */
11302 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
11303 if (domain)
11305 /* We have an array type with specified bounds. */
11306 lower = TYPE_MIN_VALUE (domain);
11307 upper = TYPE_MAX_VALUE (domain);
11309 /* Define the index type. */
11310 if (TREE_TYPE (domain))
11312 /* ??? This is probably an Ada unnamed subrange type. Ignore the
11313 TREE_TYPE field. We can't emit debug info for this
11314 because it is an unnamed integral type. */
11315 if (TREE_CODE (domain) == INTEGER_TYPE
11316 && TYPE_NAME (domain) == NULL_TREE
11317 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
11318 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
11320 else
11321 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
11322 type_die);
11325 /* ??? If upper is NULL, the array has unspecified length,
11326 but it does have a lower bound. This happens with Fortran
11327 dimension arr(N:*)
11328 Since the debugger is definitely going to need to know N
11329 to produce useful results, go ahead and output the lower
11330 bound solo, and hope the debugger can cope. */
11332 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
11333 if (upper)
11334 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
11337 /* Otherwise we have an array type with an unspecified length. The
11338 DWARF-2 spec does not say how to handle this; let's just leave out the
11339 bounds. */
11343 static void
11344 add_byte_size_attribute (dw_die_ref die, tree tree_node)
11346 unsigned size;
11348 switch (TREE_CODE (tree_node))
11350 case ERROR_MARK:
11351 size = 0;
11352 break;
11353 case ENUMERAL_TYPE:
11354 case RECORD_TYPE:
11355 case UNION_TYPE:
11356 case QUAL_UNION_TYPE:
11357 size = int_size_in_bytes (tree_node);
11358 break;
11359 case FIELD_DECL:
11360 /* For a data member of a struct or union, the DW_AT_byte_size is
11361 generally given as the number of bytes normally allocated for an
11362 object of the *declared* type of the member itself. This is true
11363 even for bit-fields. */
11364 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
11365 break;
11366 default:
11367 gcc_unreachable ();
11370 /* Note that `size' might be -1 when we get to this point. If it is, that
11371 indicates that the byte size of the entity in question is variable. We
11372 have no good way of expressing this fact in Dwarf at the present time,
11373 so just let the -1 pass on through. */
11374 add_AT_unsigned (die, DW_AT_byte_size, size);
11377 /* For a FIELD_DECL node which represents a bit-field, output an attribute
11378 which specifies the distance in bits from the highest order bit of the
11379 "containing object" for the bit-field to the highest order bit of the
11380 bit-field itself.
11382 For any given bit-field, the "containing object" is a hypothetical object
11383 (of some integral or enum type) within which the given bit-field lives. The
11384 type of this hypothetical "containing object" is always the same as the
11385 declared type of the individual bit-field itself. The determination of the
11386 exact location of the "containing object" for a bit-field is rather
11387 complicated. It's handled by the `field_byte_offset' function (above).
11389 Note that it is the size (in bytes) of the hypothetical "containing object"
11390 which will be given in the DW_AT_byte_size attribute for this bit-field.
11391 (See `byte_size_attribute' above). */
11393 static inline void
11394 add_bit_offset_attribute (dw_die_ref die, tree decl)
11396 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
11397 tree type = DECL_BIT_FIELD_TYPE (decl);
11398 HOST_WIDE_INT bitpos_int;
11399 HOST_WIDE_INT highest_order_object_bit_offset;
11400 HOST_WIDE_INT highest_order_field_bit_offset;
11401 HOST_WIDE_INT unsigned bit_offset;
11403 /* Must be a field and a bit field. */
11404 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
11406 /* We can't yet handle bit-fields whose offsets are variable, so if we
11407 encounter such things, just return without generating any attribute
11408 whatsoever. Likewise for variable or too large size. */
11409 if (! host_integerp (bit_position (decl), 0)
11410 || ! host_integerp (DECL_SIZE (decl), 1))
11411 return;
11413 bitpos_int = int_bit_position (decl);
11415 /* Note that the bit offset is always the distance (in bits) from the
11416 highest-order bit of the "containing object" to the highest-order bit of
11417 the bit-field itself. Since the "high-order end" of any object or field
11418 is different on big-endian and little-endian machines, the computation
11419 below must take account of these differences. */
11420 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
11421 highest_order_field_bit_offset = bitpos_int;
11423 if (! BYTES_BIG_ENDIAN)
11425 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
11426 highest_order_object_bit_offset += simple_type_size_in_bits (type);
11429 bit_offset
11430 = (! BYTES_BIG_ENDIAN
11431 ? highest_order_object_bit_offset - highest_order_field_bit_offset
11432 : highest_order_field_bit_offset - highest_order_object_bit_offset);
11434 add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
11437 /* For a FIELD_DECL node which represents a bit field, output an attribute
11438 which specifies the length in bits of the given field. */
11440 static inline void
11441 add_bit_size_attribute (dw_die_ref die, tree decl)
11443 /* Must be a field and a bit field. */
11444 gcc_assert (TREE_CODE (decl) == FIELD_DECL
11445 && DECL_BIT_FIELD_TYPE (decl));
11447 if (host_integerp (DECL_SIZE (decl), 1))
11448 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
11451 /* If the compiled language is ANSI C, then add a 'prototyped'
11452 attribute, if arg types are given for the parameters of a function. */
11454 static inline void
11455 add_prototyped_attribute (dw_die_ref die, tree func_type)
11457 if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
11458 && TYPE_ARG_TYPES (func_type) != NULL)
11459 add_AT_flag (die, DW_AT_prototyped, 1);
11462 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
11463 by looking in either the type declaration or object declaration
11464 equate table. */
11466 static inline void
11467 add_abstract_origin_attribute (dw_die_ref die, tree origin)
11469 dw_die_ref origin_die = NULL;
11471 if (TREE_CODE (origin) != FUNCTION_DECL)
11473 /* We may have gotten separated from the block for the inlined
11474 function, if we're in an exception handler or some such; make
11475 sure that the abstract function has been written out.
11477 Doing this for nested functions is wrong, however; functions are
11478 distinct units, and our context might not even be inline. */
11479 tree fn = origin;
11481 if (TYPE_P (fn))
11482 fn = TYPE_STUB_DECL (fn);
11484 fn = decl_function_context (fn);
11485 if (fn)
11486 dwarf2out_abstract_function (fn);
11489 if (DECL_P (origin))
11490 origin_die = lookup_decl_die (origin);
11491 else if (TYPE_P (origin))
11492 origin_die = lookup_type_die (origin);
11494 /* XXX: Functions that are never lowered don't always have correct block
11495 trees (in the case of java, they simply have no block tree, in some other
11496 languages). For these functions, there is nothing we can really do to
11497 output correct debug info for inlined functions in all cases. Rather
11498 than die, we'll just produce deficient debug info now, in that we will
11499 have variables without a proper abstract origin. In the future, when all
11500 functions are lowered, we should re-add a gcc_assert (origin_die)
11501 here. */
11503 if (origin_die)
11504 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
11507 /* We do not currently support the pure_virtual attribute. */
11509 static inline void
11510 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
11512 if (DECL_VINDEX (func_decl))
11514 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
11516 if (host_integerp (DECL_VINDEX (func_decl), 0))
11517 add_AT_loc (die, DW_AT_vtable_elem_location,
11518 new_loc_descr (DW_OP_constu,
11519 tree_low_cst (DECL_VINDEX (func_decl), 0),
11520 0));
11522 /* GNU extension: Record what type this method came from originally. */
11523 if (debug_info_level > DINFO_LEVEL_TERSE)
11524 add_AT_die_ref (die, DW_AT_containing_type,
11525 lookup_type_die (DECL_CONTEXT (func_decl)));
11529 /* Add source coordinate attributes for the given decl. */
11531 static void
11532 add_src_coords_attributes (dw_die_ref die, tree decl)
11534 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11536 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
11537 add_AT_unsigned (die, DW_AT_decl_line, s.line);
11540 /* Add a DW_AT_name attribute and source coordinate attribute for the
11541 given decl, but only if it actually has a name. */
11543 static void
11544 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
11546 tree decl_name;
11548 decl_name = DECL_NAME (decl);
11549 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
11551 add_name_attribute (die, dwarf2_name (decl, 0));
11552 if (! DECL_ARTIFICIAL (decl))
11553 add_src_coords_attributes (die, decl);
11555 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
11556 && TREE_PUBLIC (decl)
11557 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
11558 && !DECL_ABSTRACT (decl)
11559 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
11560 && !is_fortran ())
11561 add_AT_string (die, DW_AT_MIPS_linkage_name,
11562 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
11565 #ifdef VMS_DEBUGGING_INFO
11566 /* Get the function's name, as described by its RTL. This may be different
11567 from the DECL_NAME name used in the source file. */
11568 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
11570 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
11571 XEXP (DECL_RTL (decl), 0));
11572 VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
11574 #endif
11577 /* Push a new declaration scope. */
11579 static void
11580 push_decl_scope (tree scope)
11582 VEC_safe_push (tree, gc, decl_scope_table, scope);
11585 /* Pop a declaration scope. */
11587 static inline void
11588 pop_decl_scope (void)
11590 VEC_pop (tree, decl_scope_table);
11593 /* Return the DIE for the scope that immediately contains this type.
11594 Non-named types get global scope. Named types nested in other
11595 types get their containing scope if it's open, or global scope
11596 otherwise. All other types (i.e. function-local named types) get
11597 the current active scope. */
11599 static dw_die_ref
11600 scope_die_for (tree t, dw_die_ref context_die)
11602 dw_die_ref scope_die = NULL;
11603 tree containing_scope;
11604 int i;
11606 /* Non-types always go in the current scope. */
11607 gcc_assert (TYPE_P (t));
11609 containing_scope = TYPE_CONTEXT (t);
11611 /* Use the containing namespace if it was passed in (for a declaration). */
11612 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
11614 if (context_die == lookup_decl_die (containing_scope))
11615 /* OK */;
11616 else
11617 containing_scope = NULL_TREE;
11620 /* Ignore function type "scopes" from the C frontend. They mean that
11621 a tagged type is local to a parmlist of a function declarator, but
11622 that isn't useful to DWARF. */
11623 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
11624 containing_scope = NULL_TREE;
11626 if (containing_scope == NULL_TREE)
11627 scope_die = comp_unit_die;
11628 else if (TYPE_P (containing_scope))
11630 /* For types, we can just look up the appropriate DIE. But
11631 first we check to see if we're in the middle of emitting it
11632 so we know where the new DIE should go. */
11633 for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
11634 if (VEC_index (tree, decl_scope_table, i) == containing_scope)
11635 break;
11637 if (i < 0)
11639 gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
11640 || TREE_ASM_WRITTEN (containing_scope));
11642 /* If none of the current dies are suitable, we get file scope. */
11643 scope_die = comp_unit_die;
11645 else
11646 scope_die = lookup_type_die (containing_scope);
11648 else
11649 scope_die = context_die;
11651 return scope_die;
11654 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
11656 static inline int
11657 local_scope_p (dw_die_ref context_die)
11659 for (; context_die; context_die = context_die->die_parent)
11660 if (context_die->die_tag == DW_TAG_inlined_subroutine
11661 || context_die->die_tag == DW_TAG_subprogram)
11662 return 1;
11664 return 0;
11667 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
11668 whether or not to treat a DIE in this context as a declaration. */
11670 static inline int
11671 class_or_namespace_scope_p (dw_die_ref context_die)
11673 return (context_die
11674 && (context_die->die_tag == DW_TAG_structure_type
11675 || context_die->die_tag == DW_TAG_class_type
11676 || context_die->die_tag == DW_TAG_interface_type
11677 || context_die->die_tag == DW_TAG_union_type
11678 || context_die->die_tag == DW_TAG_namespace));
11681 /* Many forms of DIEs require a "type description" attribute. This
11682 routine locates the proper "type descriptor" die for the type given
11683 by 'type', and adds a DW_AT_type attribute below the given die. */
11685 static void
11686 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
11687 int decl_volatile, dw_die_ref context_die)
11689 enum tree_code code = TREE_CODE (type);
11690 dw_die_ref type_die = NULL;
11692 /* ??? If this type is an unnamed subrange type of an integral, floating-point
11693 or fixed-point type, use the inner type. This is because we have no
11694 support for unnamed types in base_type_die. This can happen if this is
11695 an Ada subrange type. Correct solution is emit a subrange type die. */
11696 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
11697 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
11698 type = TREE_TYPE (type), code = TREE_CODE (type);
11700 if (code == ERROR_MARK
11701 /* Handle a special case. For functions whose return type is void, we
11702 generate *no* type attribute. (Note that no object may have type
11703 `void', so this only applies to function return types). */
11704 || code == VOID_TYPE)
11705 return;
11707 type_die = modified_type_die (type,
11708 decl_const || TYPE_READONLY (type),
11709 decl_volatile || TYPE_VOLATILE (type),
11710 context_die);
11712 if (type_die != NULL)
11713 add_AT_die_ref (object_die, DW_AT_type, type_die);
11716 /* Given an object die, add the calling convention attribute for the
11717 function call type. */
11718 static void
11719 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
11721 enum dwarf_calling_convention value = DW_CC_normal;
11723 value = targetm.dwarf_calling_convention (TREE_TYPE (decl));
11725 /* DWARF doesn't provide a way to identify a program's source-level
11726 entry point. DW_AT_calling_convention attributes are only meant
11727 to describe functions' calling conventions. However, lacking a
11728 better way to signal the Fortran main program, we use this for the
11729 time being, following existing custom. */
11730 if (is_fortran ()
11731 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
11732 value = DW_CC_program;
11734 /* Only add the attribute if the backend requests it, and
11735 is not DW_CC_normal. */
11736 if (value && (value != DW_CC_normal))
11737 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
11740 /* Given a tree pointer to a struct, class, union, or enum type node, return
11741 a pointer to the (string) tag name for the given type, or zero if the type
11742 was declared without a tag. */
11744 static const char *
11745 type_tag (const_tree type)
11747 const char *name = 0;
11749 if (TYPE_NAME (type) != 0)
11751 tree t = 0;
11753 /* Find the IDENTIFIER_NODE for the type name. */
11754 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
11755 t = TYPE_NAME (type);
11757 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
11758 a TYPE_DECL node, regardless of whether or not a `typedef' was
11759 involved. */
11760 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11761 && ! DECL_IGNORED_P (TYPE_NAME (type)))
11763 /* We want to be extra verbose. Don't call dwarf_name if
11764 DECL_NAME isn't set. The default hook for decl_printable_name
11765 doesn't like that, and in this context it's correct to return
11766 0, instead of "<anonymous>" or the like. */
11767 if (DECL_NAME (TYPE_NAME (type)))
11768 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
11771 /* Now get the name as a string, or invent one. */
11772 if (!name && t != 0)
11773 name = IDENTIFIER_POINTER (t);
11776 return (name == 0 || *name == '\0') ? 0 : name;
11779 /* Return the type associated with a data member, make a special check
11780 for bit field types. */
11782 static inline tree
11783 member_declared_type (const_tree member)
11785 return (DECL_BIT_FIELD_TYPE (member)
11786 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
11789 /* Get the decl's label, as described by its RTL. This may be different
11790 from the DECL_NAME name used in the source file. */
11792 #if 0
11793 static const char *
11794 decl_start_label (tree decl)
11796 rtx x;
11797 const char *fnname;
11799 x = DECL_RTL (decl);
11800 gcc_assert (MEM_P (x));
11802 x = XEXP (x, 0);
11803 gcc_assert (GET_CODE (x) == SYMBOL_REF);
11805 fnname = XSTR (x, 0);
11806 return fnname;
11808 #endif
11810 /* These routines generate the internal representation of the DIE's for
11811 the compilation unit. Debugging information is collected by walking
11812 the declaration trees passed in from dwarf2out_decl(). */
11814 static void
11815 gen_array_type_die (tree type, dw_die_ref context_die)
11817 dw_die_ref scope_die = scope_die_for (type, context_die);
11818 dw_die_ref array_die;
11819 tree element_type;
11821 /* ??? The SGI dwarf reader fails for array of array of enum types unless
11822 the inner array type comes before the outer array type. Thus we must
11823 call gen_type_die before we call new_die. See below also. */
11824 #ifdef MIPS_DEBUGGING_INFO
11825 gen_type_die (TREE_TYPE (type), context_die);
11826 #endif
11828 array_die = new_die (DW_TAG_array_type, scope_die, type);
11829 add_name_attribute (array_die, type_tag (type));
11830 equate_type_number_to_die (type, array_die);
11832 if (TREE_CODE (type) == VECTOR_TYPE)
11834 /* The frontend feeds us a representation for the vector as a struct
11835 containing an array. Pull out the array type. */
11836 type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
11837 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
11840 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
11841 if (is_fortran ()
11842 && TREE_CODE (type) == ARRAY_TYPE
11843 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
11844 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
11846 #if 0
11847 /* We default the array ordering. SDB will probably do
11848 the right things even if DW_AT_ordering is not present. It's not even
11849 an issue until we start to get into multidimensional arrays anyway. If
11850 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
11851 then we'll have to put the DW_AT_ordering attribute back in. (But if
11852 and when we find out that we need to put these in, we will only do so
11853 for multidimensional arrays. */
11854 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
11855 #endif
11857 #ifdef MIPS_DEBUGGING_INFO
11858 /* The SGI compilers handle arrays of unknown bound by setting
11859 AT_declaration and not emitting any subrange DIEs. */
11860 if (! TYPE_DOMAIN (type))
11861 add_AT_flag (array_die, DW_AT_declaration, 1);
11862 else
11863 #endif
11864 add_subscript_info (array_die, type);
11866 /* Add representation of the type of the elements of this array type. */
11867 element_type = TREE_TYPE (type);
11869 /* ??? The SGI dwarf reader fails for multidimensional arrays with a
11870 const enum type. E.g. const enum machine_mode insn_operand_mode[2][10].
11871 We work around this by disabling this feature. See also
11872 add_subscript_info. */
11873 #ifndef MIPS_DEBUGGING_INFO
11874 while (TREE_CODE (element_type) == ARRAY_TYPE)
11875 element_type = TREE_TYPE (element_type);
11877 gen_type_die (element_type, context_die);
11878 #endif
11880 add_type_attribute (array_die, element_type, 0, 0, context_die);
11882 if (get_AT (array_die, DW_AT_name))
11883 add_pubtype (type, array_die);
11886 static dw_loc_descr_ref
11887 descr_info_loc (tree val, tree base_decl)
11889 HOST_WIDE_INT size;
11890 dw_loc_descr_ref loc, loc2;
11891 enum dwarf_location_atom op;
11893 if (val == base_decl)
11894 return new_loc_descr (DW_OP_push_object_address, 0, 0);
11896 switch (TREE_CODE (val))
11898 case NOP_EXPR:
11899 case CONVERT_EXPR:
11900 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
11901 case INTEGER_CST:
11902 if (host_integerp (val, 0))
11903 return int_loc_descriptor (tree_low_cst (val, 0));
11904 break;
11905 case INDIRECT_REF:
11906 size = int_size_in_bytes (TREE_TYPE (val));
11907 if (size < 0)
11908 break;
11909 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
11910 if (!loc)
11911 break;
11912 if (size == DWARF2_ADDR_SIZE)
11913 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
11914 else
11915 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
11916 return loc;
11917 case POINTER_PLUS_EXPR:
11918 case PLUS_EXPR:
11919 if (host_integerp (TREE_OPERAND (val, 1), 1)
11920 && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
11921 < 16384)
11923 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
11924 if (!loc)
11925 break;
11926 add_loc_descr (&loc,
11927 new_loc_descr (DW_OP_plus_uconst,
11928 tree_low_cst (TREE_OPERAND (val, 1),
11929 1), 0));
11931 else
11933 op = DW_OP_plus;
11934 do_binop:
11935 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
11936 if (!loc)
11937 break;
11938 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
11939 if (!loc2)
11940 break;
11941 add_loc_descr (&loc, loc2);
11942 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
11944 return loc;
11945 case MINUS_EXPR:
11946 op = DW_OP_minus;
11947 goto do_binop;
11948 case MULT_EXPR:
11949 op = DW_OP_mul;
11950 goto do_binop;
11951 case EQ_EXPR:
11952 op = DW_OP_eq;
11953 goto do_binop;
11954 case NE_EXPR:
11955 op = DW_OP_ne;
11956 goto do_binop;
11957 default:
11958 break;
11960 return NULL;
11963 static void
11964 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
11965 tree val, tree base_decl)
11967 dw_loc_descr_ref loc;
11969 if (host_integerp (val, 0))
11971 add_AT_unsigned (die, attr, tree_low_cst (val, 0));
11972 return;
11975 loc = descr_info_loc (val, base_decl);
11976 if (!loc)
11977 return;
11979 add_AT_loc (die, attr, loc);
11982 /* This routine generates DIE for array with hidden descriptor, details
11983 are filled into *info by a langhook. */
11985 static void
11986 gen_descr_array_type_die (tree type, struct array_descr_info *info,
11987 dw_die_ref context_die)
11989 dw_die_ref scope_die = scope_die_for (type, context_die);
11990 dw_die_ref array_die;
11991 int dim;
11993 array_die = new_die (DW_TAG_array_type, scope_die, type);
11994 add_name_attribute (array_die, type_tag (type));
11995 equate_type_number_to_die (type, array_die);
11997 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
11998 if (is_fortran ()
11999 && info->ndimensions >= 2)
12000 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
12002 if (info->data_location)
12003 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
12004 info->base_decl);
12005 if (info->associated)
12006 add_descr_info_field (array_die, DW_AT_associated, info->associated,
12007 info->base_decl);
12008 if (info->allocated)
12009 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
12010 info->base_decl);
12012 for (dim = 0; dim < info->ndimensions; dim++)
12014 dw_die_ref subrange_die
12015 = new_die (DW_TAG_subrange_type, array_die, NULL);
12017 if (info->dimen[dim].lower_bound)
12019 /* If it is the default value, omit it. */
12020 if ((is_c_family () || is_java ())
12021 && integer_zerop (info->dimen[dim].lower_bound))
12023 else if (is_fortran ()
12024 && integer_onep (info->dimen[dim].lower_bound))
12026 else
12027 add_descr_info_field (subrange_die, DW_AT_lower_bound,
12028 info->dimen[dim].lower_bound,
12029 info->base_decl);
12031 if (info->dimen[dim].upper_bound)
12032 add_descr_info_field (subrange_die, DW_AT_upper_bound,
12033 info->dimen[dim].upper_bound,
12034 info->base_decl);
12035 if (info->dimen[dim].stride)
12036 add_descr_info_field (subrange_die, DW_AT_byte_stride,
12037 info->dimen[dim].stride,
12038 info->base_decl);
12041 gen_type_die (info->element_type, context_die);
12042 add_type_attribute (array_die, info->element_type, 0, 0, context_die);
12044 if (get_AT (array_die, DW_AT_name))
12045 add_pubtype (type, array_die);
12048 #if 0
12049 static void
12050 gen_entry_point_die (tree decl, dw_die_ref context_die)
12052 tree origin = decl_ultimate_origin (decl);
12053 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
12055 if (origin != NULL)
12056 add_abstract_origin_attribute (decl_die, origin);
12057 else
12059 add_name_and_src_coords_attributes (decl_die, decl);
12060 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
12061 0, 0, context_die);
12064 if (DECL_ABSTRACT (decl))
12065 equate_decl_number_to_die (decl, decl_die);
12066 else
12067 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
12069 #endif
12071 /* Walk through the list of incomplete types again, trying once more to
12072 emit full debugging info for them. */
12074 static void
12075 retry_incomplete_types (void)
12077 int i;
12079 for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
12080 gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
12083 /* Generate a DIE to represent an inlined instance of an enumeration type. */
12085 static void
12086 gen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
12088 dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
12090 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
12091 be incomplete and such types are not marked. */
12092 add_abstract_origin_attribute (type_die, type);
12095 /* Determine what tag to use for a record type. */
12097 static enum dwarf_tag
12098 record_type_tag (tree type)
12100 if (! lang_hooks.types.classify_record)
12101 return DW_TAG_structure_type;
12103 switch (lang_hooks.types.classify_record (type))
12105 case RECORD_IS_STRUCT:
12106 return DW_TAG_structure_type;
12108 case RECORD_IS_CLASS:
12109 return DW_TAG_class_type;
12111 case RECORD_IS_INTERFACE:
12112 return DW_TAG_interface_type;
12114 default:
12115 gcc_unreachable ();
12119 /* Generate a DIE to represent an inlined instance of a structure type. */
12121 static void
12122 gen_inlined_structure_type_die (tree type, dw_die_ref context_die)
12124 dw_die_ref type_die = new_die (record_type_tag (type), context_die, type);
12126 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
12127 be incomplete and such types are not marked. */
12128 add_abstract_origin_attribute (type_die, type);
12131 /* Generate a DIE to represent an inlined instance of a union type. */
12133 static void
12134 gen_inlined_union_type_die (tree type, dw_die_ref context_die)
12136 dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
12138 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
12139 be incomplete and such types are not marked. */
12140 add_abstract_origin_attribute (type_die, type);
12143 /* Generate a DIE to represent an enumeration type. Note that these DIEs
12144 include all of the information about the enumeration values also. Each
12145 enumerated type name/value is listed as a child of the enumerated type
12146 DIE. */
12148 static dw_die_ref
12149 gen_enumeration_type_die (tree type, dw_die_ref context_die)
12151 dw_die_ref type_die = lookup_type_die (type);
12153 if (type_die == NULL)
12155 type_die = new_die (DW_TAG_enumeration_type,
12156 scope_die_for (type, context_die), type);
12157 equate_type_number_to_die (type, type_die);
12158 add_name_attribute (type_die, type_tag (type));
12160 else if (! TYPE_SIZE (type))
12161 return type_die;
12162 else
12163 remove_AT (type_die, DW_AT_declaration);
12165 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
12166 given enum type is incomplete, do not generate the DW_AT_byte_size
12167 attribute or the DW_AT_element_list attribute. */
12168 if (TYPE_SIZE (type))
12170 tree link;
12172 TREE_ASM_WRITTEN (type) = 1;
12173 add_byte_size_attribute (type_die, type);
12174 if (TYPE_STUB_DECL (type) != NULL_TREE)
12175 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
12177 /* If the first reference to this type was as the return type of an
12178 inline function, then it may not have a parent. Fix this now. */
12179 if (type_die->die_parent == NULL)
12180 add_child_die (scope_die_for (type, context_die), type_die);
12182 for (link = TYPE_VALUES (type);
12183 link != NULL; link = TREE_CHAIN (link))
12185 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
12186 tree value = TREE_VALUE (link);
12188 add_name_attribute (enum_die,
12189 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
12191 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
12192 /* DWARF2 does not provide a way of indicating whether or
12193 not enumeration constants are signed or unsigned. GDB
12194 always assumes the values are signed, so we output all
12195 values as if they were signed. That means that
12196 enumeration constants with very large unsigned values
12197 will appear to have negative values in the debugger. */
12198 add_AT_int (enum_die, DW_AT_const_value,
12199 tree_low_cst (value, tree_int_cst_sgn (value) > 0));
12202 else
12203 add_AT_flag (type_die, DW_AT_declaration, 1);
12205 if (get_AT (type_die, DW_AT_name))
12206 add_pubtype (type, type_die);
12208 return type_die;
12211 /* Generate a DIE to represent either a real live formal parameter decl or to
12212 represent just the type of some formal parameter position in some function
12213 type.
12215 Note that this routine is a bit unusual because its argument may be a
12216 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
12217 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
12218 node. If it's the former then this function is being called to output a
12219 DIE to represent a formal parameter object (or some inlining thereof). If
12220 it's the latter, then this function is only being called to output a
12221 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
12222 argument type of some subprogram type. */
12224 static dw_die_ref
12225 gen_formal_parameter_die (tree node, dw_die_ref context_die)
12227 dw_die_ref parm_die
12228 = new_die (DW_TAG_formal_parameter, context_die, node);
12229 tree origin;
12231 switch (TREE_CODE_CLASS (TREE_CODE (node)))
12233 case tcc_declaration:
12234 origin = decl_ultimate_origin (node);
12235 if (origin != NULL)
12236 add_abstract_origin_attribute (parm_die, origin);
12237 else
12239 tree type = TREE_TYPE (node);
12240 add_name_and_src_coords_attributes (parm_die, node);
12241 if (DECL_BY_REFERENCE (node))
12242 type = TREE_TYPE (type);
12243 add_type_attribute (parm_die, type,
12244 TREE_READONLY (node),
12245 TREE_THIS_VOLATILE (node),
12246 context_die);
12247 if (DECL_ARTIFICIAL (node))
12248 add_AT_flag (parm_die, DW_AT_artificial, 1);
12251 equate_decl_number_to_die (node, parm_die);
12252 if (! DECL_ABSTRACT (node))
12253 add_location_or_const_value_attribute (parm_die, node, DW_AT_location);
12255 break;
12257 case tcc_type:
12258 /* We were called with some kind of a ..._TYPE node. */
12259 add_type_attribute (parm_die, node, 0, 0, context_die);
12260 break;
12262 default:
12263 gcc_unreachable ();
12266 return parm_die;
12269 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
12270 at the end of an (ANSI prototyped) formal parameters list. */
12272 static void
12273 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
12275 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
12278 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
12279 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
12280 parameters as specified in some function type specification (except for
12281 those which appear as part of a function *definition*). */
12283 static void
12284 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
12286 tree link;
12287 tree formal_type = NULL;
12288 tree first_parm_type;
12289 tree arg;
12291 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
12293 arg = DECL_ARGUMENTS (function_or_method_type);
12294 function_or_method_type = TREE_TYPE (function_or_method_type);
12296 else
12297 arg = NULL_TREE;
12299 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
12301 /* Make our first pass over the list of formal parameter types and output a
12302 DW_TAG_formal_parameter DIE for each one. */
12303 for (link = first_parm_type; link; )
12305 dw_die_ref parm_die;
12307 formal_type = TREE_VALUE (link);
12308 if (formal_type == void_type_node)
12309 break;
12311 /* Output a (nameless) DIE to represent the formal parameter itself. */
12312 parm_die = gen_formal_parameter_die (formal_type, context_die);
12313 if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
12314 && link == first_parm_type)
12315 || (arg && DECL_ARTIFICIAL (arg)))
12316 add_AT_flag (parm_die, DW_AT_artificial, 1);
12318 link = TREE_CHAIN (link);
12319 if (arg)
12320 arg = TREE_CHAIN (arg);
12323 /* If this function type has an ellipsis, add a
12324 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
12325 if (formal_type != void_type_node)
12326 gen_unspecified_parameters_die (function_or_method_type, context_die);
12328 /* Make our second (and final) pass over the list of formal parameter types
12329 and output DIEs to represent those types (as necessary). */
12330 for (link = TYPE_ARG_TYPES (function_or_method_type);
12331 link && TREE_VALUE (link);
12332 link = TREE_CHAIN (link))
12333 gen_type_die (TREE_VALUE (link), context_die);
12336 /* We want to generate the DIE for TYPE so that we can generate the
12337 die for MEMBER, which has been defined; we will need to refer back
12338 to the member declaration nested within TYPE. If we're trying to
12339 generate minimal debug info for TYPE, processing TYPE won't do the
12340 trick; we need to attach the member declaration by hand. */
12342 static void
12343 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
12345 gen_type_die (type, context_die);
12347 /* If we're trying to avoid duplicate debug info, we may not have
12348 emitted the member decl for this function. Emit it now. */
12349 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
12350 && ! lookup_decl_die (member))
12352 dw_die_ref type_die;
12353 gcc_assert (!decl_ultimate_origin (member));
12355 push_decl_scope (type);
12356 type_die = lookup_type_die (type);
12357 if (TREE_CODE (member) == FUNCTION_DECL)
12358 gen_subprogram_die (member, type_die);
12359 else if (TREE_CODE (member) == FIELD_DECL)
12361 /* Ignore the nameless fields that are used to skip bits but handle
12362 C++ anonymous unions and structs. */
12363 if (DECL_NAME (member) != NULL_TREE
12364 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
12365 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
12367 gen_type_die (member_declared_type (member), type_die);
12368 gen_field_die (member, type_die);
12371 else
12372 gen_variable_die (member, type_die);
12374 pop_decl_scope ();
12378 /* Generate the DWARF2 info for the "abstract" instance of a function which we
12379 may later generate inlined and/or out-of-line instances of. */
12381 static void
12382 dwarf2out_abstract_function (tree decl)
12384 dw_die_ref old_die;
12385 tree save_fn;
12386 tree context;
12387 int was_abstract = DECL_ABSTRACT (decl);
12389 /* Make sure we have the actual abstract inline, not a clone. */
12390 decl = DECL_ORIGIN (decl);
12392 old_die = lookup_decl_die (decl);
12393 if (old_die && get_AT (old_die, DW_AT_inline))
12394 /* We've already generated the abstract instance. */
12395 return;
12397 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
12398 we don't get confused by DECL_ABSTRACT. */
12399 if (debug_info_level > DINFO_LEVEL_TERSE)
12401 context = decl_class_context (decl);
12402 if (context)
12403 gen_type_die_for_member
12404 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
12407 /* Pretend we've just finished compiling this function. */
12408 save_fn = current_function_decl;
12409 current_function_decl = decl;
12410 push_cfun (DECL_STRUCT_FUNCTION (decl));
12412 set_decl_abstract_flags (decl, 1);
12413 dwarf2out_decl (decl);
12414 if (! was_abstract)
12415 set_decl_abstract_flags (decl, 0);
12417 current_function_decl = save_fn;
12418 pop_cfun ();
12421 /* Helper function of premark_used_types() which gets called through
12422 htab_traverse_resize().
12424 Marks the DIE of a given type in *SLOT as perennial, so it never gets
12425 marked as unused by prune_unused_types. */
12426 static int
12427 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
12429 tree type;
12430 dw_die_ref die;
12432 type = *slot;
12433 die = lookup_type_die (type);
12434 if (die != NULL)
12435 die->die_perennial_p = 1;
12436 return 1;
12439 /* Mark all members of used_types_hash as perennial. */
12440 static void
12441 premark_used_types (void)
12443 if (cfun && cfun->used_types_hash)
12444 htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
12447 /* Generate a DIE to represent a declared function (either file-scope or
12448 block-local). */
12450 static void
12451 gen_subprogram_die (tree decl, dw_die_ref context_die)
12453 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
12454 tree origin = decl_ultimate_origin (decl);
12455 dw_die_ref subr_die;
12456 tree fn_arg_types;
12457 tree outer_scope;
12458 dw_die_ref old_die = lookup_decl_die (decl);
12459 int declaration = (current_function_decl != decl
12460 || class_or_namespace_scope_p (context_die));
12462 premark_used_types ();
12464 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
12465 started to generate the abstract instance of an inline, decided to output
12466 its containing class, and proceeded to emit the declaration of the inline
12467 from the member list for the class. If so, DECLARATION takes priority;
12468 we'll get back to the abstract instance when done with the class. */
12470 /* The class-scope declaration DIE must be the primary DIE. */
12471 if (origin && declaration && class_or_namespace_scope_p (context_die))
12473 origin = NULL;
12474 gcc_assert (!old_die);
12477 /* Now that the C++ front end lazily declares artificial member fns, we
12478 might need to retrofit the declaration into its class. */
12479 if (!declaration && !origin && !old_die
12480 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
12481 && !class_or_namespace_scope_p (context_die)
12482 && debug_info_level > DINFO_LEVEL_TERSE)
12483 old_die = force_decl_die (decl);
12485 if (origin != NULL)
12487 gcc_assert (!declaration || local_scope_p (context_die));
12489 /* Fixup die_parent for the abstract instance of a nested
12490 inline function. */
12491 if (old_die && old_die->die_parent == NULL)
12492 add_child_die (context_die, old_die);
12494 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
12495 add_abstract_origin_attribute (subr_die, origin);
12497 else if (old_die)
12499 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12500 struct dwarf_file_data * file_index = lookup_filename (s.file);
12502 if (!get_AT_flag (old_die, DW_AT_declaration)
12503 /* We can have a normal definition following an inline one in the
12504 case of redefinition of GNU C extern inlines.
12505 It seems reasonable to use AT_specification in this case. */
12506 && !get_AT (old_die, DW_AT_inline))
12508 /* Detect and ignore this case, where we are trying to output
12509 something we have already output. */
12510 return;
12513 /* If the definition comes from the same place as the declaration,
12514 maybe use the old DIE. We always want the DIE for this function
12515 that has the *_pc attributes to be under comp_unit_die so the
12516 debugger can find it. We also need to do this for abstract
12517 instances of inlines, since the spec requires the out-of-line copy
12518 to have the same parent. For local class methods, this doesn't
12519 apply; we just use the old DIE. */
12520 if ((old_die->die_parent == comp_unit_die || context_die == NULL)
12521 && (DECL_ARTIFICIAL (decl)
12522 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
12523 && (get_AT_unsigned (old_die, DW_AT_decl_line)
12524 == (unsigned) s.line))))
12526 subr_die = old_die;
12528 /* Clear out the declaration attribute and the formal parameters.
12529 Do not remove all children, because it is possible that this
12530 declaration die was forced using force_decl_die(). In such
12531 cases die that forced declaration die (e.g. TAG_imported_module)
12532 is one of the children that we do not want to remove. */
12533 remove_AT (subr_die, DW_AT_declaration);
12534 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
12536 else
12538 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
12539 add_AT_specification (subr_die, old_die);
12540 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
12541 add_AT_file (subr_die, DW_AT_decl_file, file_index);
12542 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
12543 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
12546 else
12548 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
12550 if (TREE_PUBLIC (decl))
12551 add_AT_flag (subr_die, DW_AT_external, 1);
12553 add_name_and_src_coords_attributes (subr_die, decl);
12554 if (debug_info_level > DINFO_LEVEL_TERSE)
12556 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
12557 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
12558 0, 0, context_die);
12561 add_pure_or_virtual_attribute (subr_die, decl);
12562 if (DECL_ARTIFICIAL (decl))
12563 add_AT_flag (subr_die, DW_AT_artificial, 1);
12565 if (TREE_PROTECTED (decl))
12566 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
12567 else if (TREE_PRIVATE (decl))
12568 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
12571 if (declaration)
12573 if (!old_die || !get_AT (old_die, DW_AT_inline))
12575 add_AT_flag (subr_die, DW_AT_declaration, 1);
12577 /* The first time we see a member function, it is in the context of
12578 the class to which it belongs. We make sure of this by emitting
12579 the class first. The next time is the definition, which is
12580 handled above. The two may come from the same source text.
12582 Note that force_decl_die() forces function declaration die. It is
12583 later reused to represent definition. */
12584 equate_decl_number_to_die (decl, subr_die);
12587 else if (DECL_ABSTRACT (decl))
12589 if (DECL_DECLARED_INLINE_P (decl))
12591 if (cgraph_function_possibly_inlined_p (decl))
12592 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
12593 else
12594 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
12596 else
12598 if (cgraph_function_possibly_inlined_p (decl))
12599 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
12600 else
12601 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
12604 if (DECL_DECLARED_INLINE_P (decl)
12605 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
12606 add_AT_flag (subr_die, DW_AT_artificial, 1);
12608 equate_decl_number_to_die (decl, subr_die);
12610 else if (!DECL_EXTERNAL (decl))
12612 HOST_WIDE_INT cfa_fb_offset;
12614 if (!old_die || !get_AT (old_die, DW_AT_inline))
12615 equate_decl_number_to_die (decl, subr_die);
12617 if (!flag_reorder_blocks_and_partition)
12619 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
12620 current_function_funcdef_no);
12621 add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
12622 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
12623 current_function_funcdef_no);
12624 add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
12626 add_pubname (decl, subr_die);
12627 add_arange (decl, subr_die);
12629 else
12630 { /* Do nothing for now; maybe need to duplicate die, one for
12631 hot section and ond for cold section, then use the hot/cold
12632 section begin/end labels to generate the aranges... */
12634 add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
12635 add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
12636 add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
12637 add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
12639 add_pubname (decl, subr_die);
12640 add_arange (decl, subr_die);
12641 add_arange (decl, subr_die);
12645 #ifdef MIPS_DEBUGGING_INFO
12646 /* Add a reference to the FDE for this routine. */
12647 add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
12648 #endif
12650 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
12652 /* We define the "frame base" as the function's CFA. This is more
12653 convenient for several reasons: (1) It's stable across the prologue
12654 and epilogue, which makes it better than just a frame pointer,
12655 (2) With dwarf3, there exists a one-byte encoding that allows us
12656 to reference the .debug_frame data by proxy, but failing that,
12657 (3) We can at least reuse the code inspection and interpretation
12658 code that determines the CFA position at various points in the
12659 function. */
12660 /* ??? Use some command-line or configury switch to enable the use
12661 of dwarf3 DW_OP_call_frame_cfa. At present there are no dwarf
12662 consumers that understand it; fall back to "pure" dwarf2 and
12663 convert the CFA data into a location list. */
12665 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
12666 if (list->dw_loc_next)
12667 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
12668 else
12669 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
12672 /* Compute a displacement from the "steady-state frame pointer" to
12673 the CFA. The former is what all stack slots and argument slots
12674 will reference in the rtl; the later is what we've told the
12675 debugger about. We'll need to adjust all frame_base references
12676 by this displacement. */
12677 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
12679 if (cfun->static_chain_decl)
12680 add_AT_location_description (subr_die, DW_AT_static_link,
12681 loc_descriptor_from_tree (cfun->static_chain_decl));
12684 /* Now output descriptions of the arguments for this function. This gets
12685 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
12686 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
12687 `...' at the end of the formal parameter list. In order to find out if
12688 there was a trailing ellipsis or not, we must instead look at the type
12689 associated with the FUNCTION_DECL. This will be a node of type
12690 FUNCTION_TYPE. If the chain of type nodes hanging off of this
12691 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
12692 an ellipsis at the end. */
12694 /* In the case where we are describing a mere function declaration, all we
12695 need to do here (and all we *can* do here) is to describe the *types* of
12696 its formal parameters. */
12697 if (debug_info_level <= DINFO_LEVEL_TERSE)
12699 else if (declaration)
12700 gen_formal_types_die (decl, subr_die);
12701 else
12703 /* Generate DIEs to represent all known formal parameters. */
12704 tree arg_decls = DECL_ARGUMENTS (decl);
12705 tree parm;
12707 /* When generating DIEs, generate the unspecified_parameters DIE
12708 instead if we come across the arg "__builtin_va_alist" */
12709 for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
12710 if (TREE_CODE (parm) == PARM_DECL)
12712 if (DECL_NAME (parm)
12713 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
12714 "__builtin_va_alist"))
12715 gen_unspecified_parameters_die (parm, subr_die);
12716 else
12717 gen_decl_die (parm, subr_die);
12720 /* Decide whether we need an unspecified_parameters DIE at the end.
12721 There are 2 more cases to do this for: 1) the ansi ... declaration -
12722 this is detectable when the end of the arg list is not a
12723 void_type_node 2) an unprototyped function declaration (not a
12724 definition). This just means that we have no info about the
12725 parameters at all. */
12726 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
12727 if (fn_arg_types != NULL)
12729 /* This is the prototyped case, check for.... */
12730 if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
12731 gen_unspecified_parameters_die (decl, subr_die);
12733 else if (DECL_INITIAL (decl) == NULL_TREE)
12734 gen_unspecified_parameters_die (decl, subr_die);
12737 /* Output Dwarf info for all of the stuff within the body of the function
12738 (if it has one - it may be just a declaration). */
12739 outer_scope = DECL_INITIAL (decl);
12741 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
12742 a function. This BLOCK actually represents the outermost binding contour
12743 for the function, i.e. the contour in which the function's formal
12744 parameters and labels get declared. Curiously, it appears that the front
12745 end doesn't actually put the PARM_DECL nodes for the current function onto
12746 the BLOCK_VARS list for this outer scope, but are strung off of the
12747 DECL_ARGUMENTS list for the function instead.
12749 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
12750 the LABEL_DECL nodes for the function however, and we output DWARF info
12751 for those in decls_for_scope. Just within the `outer_scope' there will be
12752 a BLOCK node representing the function's outermost pair of curly braces,
12753 and any blocks used for the base and member initializers of a C++
12754 constructor function. */
12755 if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
12757 /* Emit a DW_TAG_variable DIE for a named return value. */
12758 if (DECL_NAME (DECL_RESULT (decl)))
12759 gen_decl_die (DECL_RESULT (decl), subr_die);
12761 current_function_has_inlines = 0;
12762 decls_for_scope (outer_scope, subr_die, 0);
12764 #if 0 && defined (MIPS_DEBUGGING_INFO)
12765 if (current_function_has_inlines)
12767 add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
12768 if (! comp_unit_has_inlines)
12770 add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
12771 comp_unit_has_inlines = 1;
12774 #endif
12776 /* Add the calling convention attribute if requested. */
12777 add_calling_convention_attribute (subr_die, decl);
12781 /* Generate a DIE to represent a declared data object. */
12783 static void
12784 gen_variable_die (tree decl, dw_die_ref context_die)
12786 HOST_WIDE_INT off;
12787 rtx csym;
12788 dw_die_ref var_die;
12789 tree origin = decl_ultimate_origin (decl);
12790 dw_die_ref old_die = lookup_decl_die (decl);
12791 int declaration = (DECL_EXTERNAL (decl)
12792 /* If DECL is COMDAT and has not actually been
12793 emitted, we cannot take its address; there
12794 might end up being no definition anywhere in
12795 the program. For example, consider the C++
12796 test case:
12798 template <class T>
12799 struct S { static const int i = 7; };
12801 template <class T>
12802 const int S<T>::i;
12804 int f() { return S<int>::i; }
12806 Here, S<int>::i is not DECL_EXTERNAL, but no
12807 definition is required, so the compiler will
12808 not emit a definition. */
12809 || (TREE_CODE (decl) == VAR_DECL
12810 && DECL_COMDAT (decl) && !TREE_ASM_WRITTEN (decl))
12811 || class_or_namespace_scope_p (context_die));
12813 csym = common_check (decl, &off);
12815 /* Symbol in common gets emitted as a child of the common block, in the form
12816 of a data member.
12818 ??? This creates a new common block die for every common block symbol.
12819 Better to share same common block die for all symbols in that block. */
12820 if (csym)
12822 tree blok;
12823 dw_die_ref com_die;
12824 const char *cnam = targetm.strip_name_encoding(XSTR (csym, 0));
12825 dw_loc_descr_ref loc = mem_loc_descriptor (csym, dw_val_class_addr,
12826 VAR_INIT_STATUS_INITIALIZED);
12828 blok = (tree) TREE_OPERAND (DECL_VALUE_EXPR (decl), 0);
12829 var_die = new_die (DW_TAG_common_block, context_die, decl);
12830 add_name_and_src_coords_attributes (var_die, blok);
12831 add_AT_flag (var_die, DW_AT_external, 1);
12832 add_AT_loc (var_die, DW_AT_location, loc);
12833 com_die = new_die (DW_TAG_member, var_die, decl);
12834 add_name_and_src_coords_attributes (com_die, decl);
12835 add_type_attribute (com_die, TREE_TYPE (decl), TREE_READONLY (decl),
12836 TREE_THIS_VOLATILE (decl), context_die);
12837 add_AT_loc (com_die, DW_AT_data_member_location, int_loc_descriptor(off));
12838 add_pubname_string (cnam, var_die); /* ??? needed? */
12839 return;
12842 var_die = new_die (DW_TAG_variable, context_die, decl);
12844 if (origin != NULL)
12845 add_abstract_origin_attribute (var_die, origin);
12847 /* Loop unrolling can create multiple blocks that refer to the same
12848 static variable, so we must test for the DW_AT_declaration flag.
12850 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
12851 copy decls and set the DECL_ABSTRACT flag on them instead of
12852 sharing them.
12854 ??? Duplicated blocks have been rewritten to use .debug_ranges.
12856 ??? The declare_in_namespace support causes us to get two DIEs for one
12857 variable, both of which are declarations. We want to avoid considering
12858 one to be a specification, so we must test that this DIE is not a
12859 declaration. */
12860 else if (old_die && TREE_STATIC (decl) && ! declaration
12861 && get_AT_flag (old_die, DW_AT_declaration) == 1)
12863 /* This is a definition of a C++ class level static. */
12864 add_AT_specification (var_die, old_die);
12865 if (DECL_NAME (decl))
12867 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12868 struct dwarf_file_data * file_index = lookup_filename (s.file);
12870 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
12871 add_AT_file (var_die, DW_AT_decl_file, file_index);
12873 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
12874 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
12877 else
12879 tree type = TREE_TYPE (decl);
12880 if ((TREE_CODE (decl) == PARM_DECL
12881 || TREE_CODE (decl) == RESULT_DECL)
12882 && DECL_BY_REFERENCE (decl))
12883 type = TREE_TYPE (type);
12885 add_name_and_src_coords_attributes (var_die, decl);
12886 add_type_attribute (var_die, type, TREE_READONLY (decl),
12887 TREE_THIS_VOLATILE (decl), context_die);
12889 if (TREE_PUBLIC (decl))
12890 add_AT_flag (var_die, DW_AT_external, 1);
12892 if (DECL_ARTIFICIAL (decl))
12893 add_AT_flag (var_die, DW_AT_artificial, 1);
12895 if (TREE_PROTECTED (decl))
12896 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
12897 else if (TREE_PRIVATE (decl))
12898 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
12901 if (declaration)
12902 add_AT_flag (var_die, DW_AT_declaration, 1);
12904 if (DECL_ABSTRACT (decl) || declaration)
12905 equate_decl_number_to_die (decl, var_die);
12907 if (! declaration && ! DECL_ABSTRACT (decl))
12909 add_location_or_const_value_attribute (var_die, decl, DW_AT_location);
12910 add_pubname (decl, var_die);
12912 else
12913 tree_add_const_value_attribute (var_die, decl);
12916 /* Generate a DIE to represent a label identifier. */
12918 static void
12919 gen_label_die (tree decl, dw_die_ref context_die)
12921 tree origin = decl_ultimate_origin (decl);
12922 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
12923 rtx insn;
12924 char label[MAX_ARTIFICIAL_LABEL_BYTES];
12926 if (origin != NULL)
12927 add_abstract_origin_attribute (lbl_die, origin);
12928 else
12929 add_name_and_src_coords_attributes (lbl_die, decl);
12931 if (DECL_ABSTRACT (decl))
12932 equate_decl_number_to_die (decl, lbl_die);
12933 else
12935 insn = DECL_RTL_IF_SET (decl);
12937 /* Deleted labels are programmer specified labels which have been
12938 eliminated because of various optimizations. We still emit them
12939 here so that it is possible to put breakpoints on them. */
12940 if (insn
12941 && (LABEL_P (insn)
12942 || ((NOTE_P (insn)
12943 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
12945 /* When optimization is enabled (via -O) some parts of the compiler
12946 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
12947 represent source-level labels which were explicitly declared by
12948 the user. This really shouldn't be happening though, so catch
12949 it if it ever does happen. */
12950 gcc_assert (!INSN_DELETED_P (insn));
12952 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
12953 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
12958 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
12959 attributes to the DIE for a block STMT, to describe where the inlined
12960 function was called from. This is similar to add_src_coords_attributes. */
12962 static inline void
12963 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
12965 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
12967 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
12968 add_AT_unsigned (die, DW_AT_call_line, s.line);
12972 /* If STMT's abstract origin is a function declaration and STMT's
12973 first subblock's abstract origin is the function's outermost block,
12974 then we're looking at the main entry point. */
12975 static bool
12976 is_inlined_entry_point (const_tree stmt)
12978 tree decl, block;
12980 if (!stmt || TREE_CODE (stmt) != BLOCK)
12981 return false;
12983 decl = block_ultimate_origin (stmt);
12985 if (!decl || TREE_CODE (decl) != FUNCTION_DECL)
12986 return false;
12988 block = BLOCK_SUBBLOCKS (stmt);
12990 if (block)
12992 if (TREE_CODE (block) != BLOCK)
12993 return false;
12995 block = block_ultimate_origin (block);
12998 return block == DECL_INITIAL (decl);
13001 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
13002 Add low_pc and high_pc attributes to the DIE for a block STMT. */
13004 static inline void
13005 add_high_low_attributes (tree stmt, dw_die_ref die)
13007 char label[MAX_ARTIFICIAL_LABEL_BYTES];
13009 if (BLOCK_FRAGMENT_CHAIN (stmt))
13011 tree chain;
13013 if (is_inlined_entry_point (stmt))
13015 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
13016 BLOCK_NUMBER (stmt));
13017 add_AT_lbl_id (die, DW_AT_entry_pc, label);
13020 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
13022 chain = BLOCK_FRAGMENT_CHAIN (stmt);
13025 add_ranges (chain);
13026 chain = BLOCK_FRAGMENT_CHAIN (chain);
13028 while (chain);
13029 add_ranges (NULL);
13031 else
13033 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
13034 BLOCK_NUMBER (stmt));
13035 add_AT_lbl_id (die, DW_AT_low_pc, label);
13036 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
13037 BLOCK_NUMBER (stmt));
13038 add_AT_lbl_id (die, DW_AT_high_pc, label);
13042 /* Generate a DIE for a lexical block. */
13044 static void
13045 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
13047 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
13049 if (! BLOCK_ABSTRACT (stmt))
13050 add_high_low_attributes (stmt, stmt_die);
13052 decls_for_scope (stmt, stmt_die, depth);
13055 /* Generate a DIE for an inlined subprogram. */
13057 static void
13058 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
13060 tree decl = block_ultimate_origin (stmt);
13062 /* Emit info for the abstract instance first, if we haven't yet. We
13063 must emit this even if the block is abstract, otherwise when we
13064 emit the block below (or elsewhere), we may end up trying to emit
13065 a die whose origin die hasn't been emitted, and crashing. */
13066 dwarf2out_abstract_function (decl);
13068 if (! BLOCK_ABSTRACT (stmt))
13070 dw_die_ref subr_die
13071 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
13073 add_abstract_origin_attribute (subr_die, decl);
13074 add_high_low_attributes (stmt, subr_die);
13075 add_call_src_coords_attributes (stmt, subr_die);
13077 decls_for_scope (stmt, subr_die, depth);
13078 current_function_has_inlines = 1;
13080 else
13081 /* We may get here if we're the outer block of function A that was
13082 inlined into function B that was inlined into function C. When
13083 generating debugging info for C, dwarf2out_abstract_function(B)
13084 would mark all inlined blocks as abstract, including this one.
13085 So, we wouldn't (and shouldn't) expect labels to be generated
13086 for this one. Instead, just emit debugging info for
13087 declarations within the block. This is particularly important
13088 in the case of initializers of arguments passed from B to us:
13089 if they're statement expressions containing declarations, we
13090 wouldn't generate dies for their abstract variables, and then,
13091 when generating dies for the real variables, we'd die (pun
13092 intended :-) */
13093 gen_lexical_block_die (stmt, context_die, depth);
13096 /* Generate a DIE for a field in a record, or structure. */
13098 static void
13099 gen_field_die (tree decl, dw_die_ref context_die)
13101 dw_die_ref decl_die;
13103 if (TREE_TYPE (decl) == error_mark_node)
13104 return;
13106 decl_die = new_die (DW_TAG_member, context_die, decl);
13107 add_name_and_src_coords_attributes (decl_die, decl);
13108 add_type_attribute (decl_die, member_declared_type (decl),
13109 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
13110 context_die);
13112 if (DECL_BIT_FIELD_TYPE (decl))
13114 add_byte_size_attribute (decl_die, decl);
13115 add_bit_size_attribute (decl_die, decl);
13116 add_bit_offset_attribute (decl_die, decl);
13119 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
13120 add_data_member_location_attribute (decl_die, decl);
13122 if (DECL_ARTIFICIAL (decl))
13123 add_AT_flag (decl_die, DW_AT_artificial, 1);
13125 if (TREE_PROTECTED (decl))
13126 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
13127 else if (TREE_PRIVATE (decl))
13128 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
13130 /* Equate decl number to die, so that we can look up this decl later on. */
13131 equate_decl_number_to_die (decl, decl_die);
13134 #if 0
13135 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
13136 Use modified_type_die instead.
13137 We keep this code here just in case these types of DIEs may be needed to
13138 represent certain things in other languages (e.g. Pascal) someday. */
13140 static void
13141 gen_pointer_type_die (tree type, dw_die_ref context_die)
13143 dw_die_ref ptr_die
13144 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
13146 equate_type_number_to_die (type, ptr_die);
13147 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
13148 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
13151 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
13152 Use modified_type_die instead.
13153 We keep this code here just in case these types of DIEs may be needed to
13154 represent certain things in other languages (e.g. Pascal) someday. */
13156 static void
13157 gen_reference_type_die (tree type, dw_die_ref context_die)
13159 dw_die_ref ref_die
13160 = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
13162 equate_type_number_to_die (type, ref_die);
13163 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
13164 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
13166 #endif
13168 /* Generate a DIE for a pointer to a member type. */
13170 static void
13171 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
13173 dw_die_ref ptr_die
13174 = new_die (DW_TAG_ptr_to_member_type,
13175 scope_die_for (type, context_die), type);
13177 equate_type_number_to_die (type, ptr_die);
13178 add_AT_die_ref (ptr_die, DW_AT_containing_type,
13179 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
13180 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
13183 /* Generate the DIE for the compilation unit. */
13185 static dw_die_ref
13186 gen_compile_unit_die (const char *filename)
13188 dw_die_ref die;
13189 char producer[250];
13190 const char *language_string = lang_hooks.name;
13191 int language;
13193 die = new_die (DW_TAG_compile_unit, NULL, NULL);
13195 if (filename)
13197 add_name_attribute (die, filename);
13198 /* Don't add cwd for <built-in>. */
13199 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
13200 add_comp_dir_attribute (die);
13203 sprintf (producer, "%s %s", language_string, version_string);
13205 #ifdef MIPS_DEBUGGING_INFO
13206 /* The MIPS/SGI compilers place the 'cc' command line options in the producer
13207 string. The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
13208 not appear in the producer string, the debugger reaches the conclusion
13209 that the object file is stripped and has no debugging information.
13210 To get the MIPS/SGI debugger to believe that there is debugging
13211 information in the object file, we add a -g to the producer string. */
13212 if (debug_info_level > DINFO_LEVEL_TERSE)
13213 strcat (producer, " -g");
13214 #endif
13216 add_AT_string (die, DW_AT_producer, producer);
13218 if (strcmp (language_string, "GNU C++") == 0)
13219 language = DW_LANG_C_plus_plus;
13220 else if (strcmp (language_string, "GNU Ada") == 0)
13221 language = DW_LANG_Ada95;
13222 else if (strcmp (language_string, "GNU F77") == 0)
13223 language = DW_LANG_Fortran77;
13224 else if (strcmp (language_string, "GNU Fortran") == 0)
13225 language = DW_LANG_Fortran95;
13226 else if (strcmp (language_string, "GNU Pascal") == 0)
13227 language = DW_LANG_Pascal83;
13228 else if (strcmp (language_string, "GNU Java") == 0)
13229 language = DW_LANG_Java;
13230 else if (strcmp (language_string, "GNU Objective-C") == 0)
13231 language = DW_LANG_ObjC;
13232 else if (strcmp (language_string, "GNU Objective-C++") == 0)
13233 language = DW_LANG_ObjC_plus_plus;
13234 else
13235 language = DW_LANG_C89;
13237 add_AT_unsigned (die, DW_AT_language, language);
13238 return die;
13241 /* Generate the DIE for a base class. */
13243 static void
13244 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
13246 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
13248 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
13249 add_data_member_location_attribute (die, binfo);
13251 if (BINFO_VIRTUAL_P (binfo))
13252 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
13254 if (access == access_public_node)
13255 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
13256 else if (access == access_protected_node)
13257 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
13260 /* Generate a DIE for a class member. */
13262 static void
13263 gen_member_die (tree type, dw_die_ref context_die)
13265 tree member;
13266 tree binfo = TYPE_BINFO (type);
13267 dw_die_ref child;
13269 /* If this is not an incomplete type, output descriptions of each of its
13270 members. Note that as we output the DIEs necessary to represent the
13271 members of this record or union type, we will also be trying to output
13272 DIEs to represent the *types* of those members. However the `type'
13273 function (above) will specifically avoid generating type DIEs for member
13274 types *within* the list of member DIEs for this (containing) type except
13275 for those types (of members) which are explicitly marked as also being
13276 members of this (containing) type themselves. The g++ front- end can
13277 force any given type to be treated as a member of some other (containing)
13278 type by setting the TYPE_CONTEXT of the given (member) type to point to
13279 the TREE node representing the appropriate (containing) type. */
13281 /* First output info about the base classes. */
13282 if (binfo)
13284 VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
13285 int i;
13286 tree base;
13288 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
13289 gen_inheritance_die (base,
13290 (accesses ? VEC_index (tree, accesses, i)
13291 : access_public_node), context_die);
13294 /* Now output info about the data members and type members. */
13295 for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
13297 /* If we thought we were generating minimal debug info for TYPE
13298 and then changed our minds, some of the member declarations
13299 may have already been defined. Don't define them again, but
13300 do put them in the right order. */
13302 child = lookup_decl_die (member);
13303 if (child)
13304 splice_child_die (context_die, child);
13305 else
13306 gen_decl_die (member, context_die);
13309 /* Now output info about the function members (if any). */
13310 for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
13312 /* Don't include clones in the member list. */
13313 if (DECL_ABSTRACT_ORIGIN (member))
13314 continue;
13316 child = lookup_decl_die (member);
13317 if (child)
13318 splice_child_die (context_die, child);
13319 else
13320 gen_decl_die (member, context_die);
13324 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
13325 is set, we pretend that the type was never defined, so we only get the
13326 member DIEs needed by later specification DIEs. */
13328 static void
13329 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
13330 enum debug_info_usage usage)
13332 dw_die_ref type_die = lookup_type_die (type);
13333 dw_die_ref scope_die = 0;
13334 int nested = 0;
13335 int complete = (TYPE_SIZE (type)
13336 && (! TYPE_STUB_DECL (type)
13337 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
13338 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
13339 complete = complete && should_emit_struct_debug (type, usage);
13341 if (type_die && ! complete)
13342 return;
13344 if (TYPE_CONTEXT (type) != NULL_TREE
13345 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
13346 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
13347 nested = 1;
13349 scope_die = scope_die_for (type, context_die);
13351 if (! type_die || (nested && scope_die == comp_unit_die))
13352 /* First occurrence of type or toplevel definition of nested class. */
13354 dw_die_ref old_die = type_die;
13356 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
13357 ? record_type_tag (type) : DW_TAG_union_type,
13358 scope_die, type);
13359 equate_type_number_to_die (type, type_die);
13360 if (old_die)
13361 add_AT_specification (type_die, old_die);
13362 else
13363 add_name_attribute (type_die, type_tag (type));
13365 else
13366 remove_AT (type_die, DW_AT_declaration);
13368 /* If this type has been completed, then give it a byte_size attribute and
13369 then give a list of members. */
13370 if (complete && !ns_decl)
13372 /* Prevent infinite recursion in cases where the type of some member of
13373 this type is expressed in terms of this type itself. */
13374 TREE_ASM_WRITTEN (type) = 1;
13375 add_byte_size_attribute (type_die, type);
13376 if (TYPE_STUB_DECL (type) != NULL_TREE)
13377 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
13379 /* If the first reference to this type was as the return type of an
13380 inline function, then it may not have a parent. Fix this now. */
13381 if (type_die->die_parent == NULL)
13382 add_child_die (scope_die, type_die);
13384 push_decl_scope (type);
13385 gen_member_die (type, type_die);
13386 pop_decl_scope ();
13388 /* GNU extension: Record what type our vtable lives in. */
13389 if (TYPE_VFIELD (type))
13391 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
13393 gen_type_die (vtype, context_die);
13394 add_AT_die_ref (type_die, DW_AT_containing_type,
13395 lookup_type_die (vtype));
13398 else
13400 add_AT_flag (type_die, DW_AT_declaration, 1);
13402 /* We don't need to do this for function-local types. */
13403 if (TYPE_STUB_DECL (type)
13404 && ! decl_function_context (TYPE_STUB_DECL (type)))
13405 VEC_safe_push (tree, gc, incomplete_types, type);
13408 if (get_AT (type_die, DW_AT_name))
13409 add_pubtype (type, type_die);
13412 /* Generate a DIE for a subroutine _type_. */
13414 static void
13415 gen_subroutine_type_die (tree type, dw_die_ref context_die)
13417 tree return_type = TREE_TYPE (type);
13418 dw_die_ref subr_die
13419 = new_die (DW_TAG_subroutine_type,
13420 scope_die_for (type, context_die), type);
13422 equate_type_number_to_die (type, subr_die);
13423 add_prototyped_attribute (subr_die, type);
13424 add_type_attribute (subr_die, return_type, 0, 0, context_die);
13425 gen_formal_types_die (type, subr_die);
13427 if (get_AT (subr_die, DW_AT_name))
13428 add_pubtype (type, subr_die);
13431 /* Generate a DIE for a type definition. */
13433 static void
13434 gen_typedef_die (tree decl, dw_die_ref context_die)
13436 dw_die_ref type_die;
13437 tree origin;
13439 if (TREE_ASM_WRITTEN (decl))
13440 return;
13442 TREE_ASM_WRITTEN (decl) = 1;
13443 type_die = new_die (DW_TAG_typedef, context_die, decl);
13444 origin = decl_ultimate_origin (decl);
13445 if (origin != NULL)
13446 add_abstract_origin_attribute (type_die, origin);
13447 else
13449 tree type;
13451 add_name_and_src_coords_attributes (type_die, decl);
13452 if (DECL_ORIGINAL_TYPE (decl))
13454 type = DECL_ORIGINAL_TYPE (decl);
13456 gcc_assert (type != TREE_TYPE (decl));
13457 equate_type_number_to_die (TREE_TYPE (decl), type_die);
13459 else
13460 type = TREE_TYPE (decl);
13462 add_type_attribute (type_die, type, TREE_READONLY (decl),
13463 TREE_THIS_VOLATILE (decl), context_die);
13466 if (DECL_ABSTRACT (decl))
13467 equate_decl_number_to_die (decl, type_die);
13469 if (get_AT (type_die, DW_AT_name))
13470 add_pubtype (decl, type_die);
13473 /* Generate a type description DIE. */
13475 static void
13476 gen_type_die_with_usage (tree type, dw_die_ref context_die,
13477 enum debug_info_usage usage)
13479 int need_pop;
13480 struct array_descr_info info;
13482 if (type == NULL_TREE || type == error_mark_node)
13483 return;
13485 if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
13486 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
13488 if (TREE_ASM_WRITTEN (type))
13489 return;
13491 /* Prevent broken recursion; we can't hand off to the same type. */
13492 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
13494 TREE_ASM_WRITTEN (type) = 1;
13495 gen_decl_die (TYPE_NAME (type), context_die);
13496 return;
13499 /* If this is an array type with hidden descriptor, handle it first. */
13500 if (!TREE_ASM_WRITTEN (type)
13501 && lang_hooks.types.get_array_descr_info
13502 && lang_hooks.types.get_array_descr_info (type, &info))
13504 gen_descr_array_type_die (type, &info, context_die);
13505 TREE_ASM_WRITTEN (type) = 1;
13506 return;
13509 /* We are going to output a DIE to represent the unqualified version
13510 of this type (i.e. without any const or volatile qualifiers) so
13511 get the main variant (i.e. the unqualified version) of this type
13512 now. (Vectors are special because the debugging info is in the
13513 cloned type itself). */
13514 if (TREE_CODE (type) != VECTOR_TYPE)
13515 type = type_main_variant (type);
13517 if (TREE_ASM_WRITTEN (type))
13518 return;
13520 switch (TREE_CODE (type))
13522 case ERROR_MARK:
13523 break;
13525 case POINTER_TYPE:
13526 case REFERENCE_TYPE:
13527 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
13528 ensures that the gen_type_die recursion will terminate even if the
13529 type is recursive. Recursive types are possible in Ada. */
13530 /* ??? We could perhaps do this for all types before the switch
13531 statement. */
13532 TREE_ASM_WRITTEN (type) = 1;
13534 /* For these types, all that is required is that we output a DIE (or a
13535 set of DIEs) to represent the "basis" type. */
13536 gen_type_die_with_usage (TREE_TYPE (type), context_die,
13537 DINFO_USAGE_IND_USE);
13538 break;
13540 case OFFSET_TYPE:
13541 /* This code is used for C++ pointer-to-data-member types.
13542 Output a description of the relevant class type. */
13543 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
13544 DINFO_USAGE_IND_USE);
13546 /* Output a description of the type of the object pointed to. */
13547 gen_type_die_with_usage (TREE_TYPE (type), context_die,
13548 DINFO_USAGE_IND_USE);
13550 /* Now output a DIE to represent this pointer-to-data-member type
13551 itself. */
13552 gen_ptr_to_mbr_type_die (type, context_die);
13553 break;
13555 case FUNCTION_TYPE:
13556 /* Force out return type (in case it wasn't forced out already). */
13557 gen_type_die_with_usage (TREE_TYPE (type), context_die,
13558 DINFO_USAGE_DIR_USE);
13559 gen_subroutine_type_die (type, context_die);
13560 break;
13562 case METHOD_TYPE:
13563 /* Force out return type (in case it wasn't forced out already). */
13564 gen_type_die_with_usage (TREE_TYPE (type), context_die,
13565 DINFO_USAGE_DIR_USE);
13566 gen_subroutine_type_die (type, context_die);
13567 break;
13569 case ARRAY_TYPE:
13570 gen_array_type_die (type, context_die);
13571 break;
13573 case VECTOR_TYPE:
13574 gen_array_type_die (type, context_die);
13575 break;
13577 case ENUMERAL_TYPE:
13578 case RECORD_TYPE:
13579 case UNION_TYPE:
13580 case QUAL_UNION_TYPE:
13581 /* If this is a nested type whose containing class hasn't been written
13582 out yet, writing it out will cover this one, too. This does not apply
13583 to instantiations of member class templates; they need to be added to
13584 the containing class as they are generated. FIXME: This hurts the
13585 idea of combining type decls from multiple TUs, since we can't predict
13586 what set of template instantiations we'll get. */
13587 if (TYPE_CONTEXT (type)
13588 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
13589 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
13591 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
13593 if (TREE_ASM_WRITTEN (type))
13594 return;
13596 /* If that failed, attach ourselves to the stub. */
13597 push_decl_scope (TYPE_CONTEXT (type));
13598 context_die = lookup_type_die (TYPE_CONTEXT (type));
13599 need_pop = 1;
13601 else
13603 declare_in_namespace (type, context_die);
13604 need_pop = 0;
13607 if (TREE_CODE (type) == ENUMERAL_TYPE)
13609 /* This might have been written out by the call to
13610 declare_in_namespace. */
13611 if (!TREE_ASM_WRITTEN (type))
13612 gen_enumeration_type_die (type, context_die);
13614 else
13615 gen_struct_or_union_type_die (type, context_die, usage);
13617 if (need_pop)
13618 pop_decl_scope ();
13620 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
13621 it up if it is ever completed. gen_*_type_die will set it for us
13622 when appropriate. */
13623 return;
13625 case VOID_TYPE:
13626 case INTEGER_TYPE:
13627 case REAL_TYPE:
13628 case FIXED_POINT_TYPE:
13629 case COMPLEX_TYPE:
13630 case BOOLEAN_TYPE:
13631 /* No DIEs needed for fundamental types. */
13632 break;
13634 case LANG_TYPE:
13635 /* No Dwarf representation currently defined. */
13636 break;
13638 default:
13639 gcc_unreachable ();
13642 TREE_ASM_WRITTEN (type) = 1;
13645 static void
13646 gen_type_die (tree type, dw_die_ref context_die)
13648 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
13651 /* Generate a DIE for a tagged type instantiation. */
13653 static void
13654 gen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
13656 if (type == NULL_TREE || type == error_mark_node)
13657 return;
13659 /* We are going to output a DIE to represent the unqualified version of
13660 this type (i.e. without any const or volatile qualifiers) so make sure
13661 that we have the main variant (i.e. the unqualified version) of this
13662 type now. */
13663 gcc_assert (type == type_main_variant (type));
13665 /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
13666 an instance of an unresolved type. */
13668 switch (TREE_CODE (type))
13670 case ERROR_MARK:
13671 break;
13673 case ENUMERAL_TYPE:
13674 gen_inlined_enumeration_type_die (type, context_die);
13675 break;
13677 case RECORD_TYPE:
13678 gen_inlined_structure_type_die (type, context_die);
13679 break;
13681 case UNION_TYPE:
13682 case QUAL_UNION_TYPE:
13683 gen_inlined_union_type_die (type, context_die);
13684 break;
13686 default:
13687 gcc_unreachable ();
13691 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
13692 things which are local to the given block. */
13694 static void
13695 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
13697 int must_output_die = 0;
13698 tree origin;
13699 tree decl;
13700 enum tree_code origin_code;
13702 /* Ignore blocks that are NULL. */
13703 if (stmt == NULL_TREE)
13704 return;
13706 /* If the block is one fragment of a non-contiguous block, do not
13707 process the variables, since they will have been done by the
13708 origin block. Do process subblocks. */
13709 if (BLOCK_FRAGMENT_ORIGIN (stmt))
13711 tree sub;
13713 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
13714 gen_block_die (sub, context_die, depth + 1);
13716 return;
13719 /* Determine the "ultimate origin" of this block. This block may be an
13720 inlined instance of an inlined instance of inline function, so we have
13721 to trace all of the way back through the origin chain to find out what
13722 sort of node actually served as the original seed for the creation of
13723 the current block. */
13724 origin = block_ultimate_origin (stmt);
13725 origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
13727 /* Determine if we need to output any Dwarf DIEs at all to represent this
13728 block. */
13729 if (origin_code == FUNCTION_DECL)
13730 /* The outer scopes for inlinings *must* always be represented. We
13731 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
13732 must_output_die = 1;
13733 else
13735 /* In the case where the current block represents an inlining of the
13736 "body block" of an inline function, we must *NOT* output any DIE for
13737 this block because we have already output a DIE to represent the whole
13738 inlined function scope and the "body block" of any function doesn't
13739 really represent a different scope according to ANSI C rules. So we
13740 check here to make sure that this block does not represent a "body
13741 block inlining" before trying to set the MUST_OUTPUT_DIE flag. */
13742 if (! is_body_block (origin ? origin : stmt))
13744 /* Determine if this block directly contains any "significant"
13745 local declarations which we will need to output DIEs for. */
13746 if (debug_info_level > DINFO_LEVEL_TERSE)
13747 /* We are not in terse mode so *any* local declaration counts
13748 as being a "significant" one. */
13749 must_output_die = (BLOCK_VARS (stmt) != NULL
13750 && (TREE_USED (stmt)
13751 || TREE_ASM_WRITTEN (stmt)
13752 || BLOCK_ABSTRACT (stmt)));
13753 else
13754 /* We are in terse mode, so only local (nested) function
13755 definitions count as "significant" local declarations. */
13756 for (decl = BLOCK_VARS (stmt);
13757 decl != NULL; decl = TREE_CHAIN (decl))
13758 if (TREE_CODE (decl) == FUNCTION_DECL
13759 && DECL_INITIAL (decl))
13761 must_output_die = 1;
13762 break;
13767 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
13768 DIE for any block which contains no significant local declarations at
13769 all. Rather, in such cases we just call `decls_for_scope' so that any
13770 needed Dwarf info for any sub-blocks will get properly generated. Note
13771 that in terse mode, our definition of what constitutes a "significant"
13772 local declaration gets restricted to include only inlined function
13773 instances and local (nested) function definitions. */
13774 if (must_output_die)
13776 if (origin_code == FUNCTION_DECL)
13777 gen_inlined_subroutine_die (stmt, context_die, depth);
13778 else
13779 gen_lexical_block_die (stmt, context_die, depth);
13781 else
13782 decls_for_scope (stmt, context_die, depth);
13785 /* Generate all of the decls declared within a given scope and (recursively)
13786 all of its sub-blocks. */
13788 static void
13789 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
13791 tree decl;
13792 tree subblocks;
13794 /* Ignore NULL blocks. */
13795 if (stmt == NULL_TREE)
13796 return;
13798 if (TREE_USED (stmt))
13800 /* Output the DIEs to represent all of the data objects and typedefs
13801 declared directly within this block but not within any nested
13802 sub-blocks. Also, nested function and tag DIEs have been
13803 generated with a parent of NULL; fix that up now. */
13804 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
13806 dw_die_ref die;
13808 if (TREE_CODE (decl) == FUNCTION_DECL)
13809 die = lookup_decl_die (decl);
13810 else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
13811 die = lookup_type_die (TREE_TYPE (decl));
13812 else
13813 die = NULL;
13815 if (die != NULL && die->die_parent == NULL)
13816 add_child_die (context_die, die);
13817 /* Do not produce debug information for static variables since
13818 these might be optimized out. We are called for these later
13819 in varpool_analyze_pending_decls.
13821 But *do* produce it for Fortran COMMON variables because,
13822 even though they are static, their names can differ depending
13823 on the scope, which we need to preserve. */
13824 if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl)
13825 && !(is_fortran () && TREE_PUBLIC (decl)))
13827 else
13828 gen_decl_die (decl, context_die);
13832 /* If we're at -g1, we're not interested in subblocks. */
13833 if (debug_info_level <= DINFO_LEVEL_TERSE)
13834 return;
13836 /* Output the DIEs to represent all sub-blocks (and the items declared
13837 therein) of this block. */
13838 for (subblocks = BLOCK_SUBBLOCKS (stmt);
13839 subblocks != NULL;
13840 subblocks = BLOCK_CHAIN (subblocks))
13841 gen_block_die (subblocks, context_die, depth + 1);
13844 /* Is this a typedef we can avoid emitting? */
13846 static inline int
13847 is_redundant_typedef (const_tree decl)
13849 if (TYPE_DECL_IS_STUB (decl))
13850 return 1;
13852 if (DECL_ARTIFICIAL (decl)
13853 && DECL_CONTEXT (decl)
13854 && is_tagged_type (DECL_CONTEXT (decl))
13855 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
13856 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
13857 /* Also ignore the artificial member typedef for the class name. */
13858 return 1;
13860 return 0;
13863 /* Returns the DIE for decl. A DIE will always be returned. */
13865 static dw_die_ref
13866 force_decl_die (tree decl)
13868 dw_die_ref decl_die;
13869 unsigned saved_external_flag;
13870 tree save_fn = NULL_TREE;
13871 decl_die = lookup_decl_die (decl);
13872 if (!decl_die)
13874 dw_die_ref context_die;
13875 tree decl_context = DECL_CONTEXT (decl);
13876 if (decl_context)
13878 /* Find die that represents this context. */
13879 if (TYPE_P (decl_context))
13880 context_die = force_type_die (decl_context);
13881 else
13882 context_die = force_decl_die (decl_context);
13884 else
13885 context_die = comp_unit_die;
13887 decl_die = lookup_decl_die (decl);
13888 if (decl_die)
13889 return decl_die;
13891 switch (TREE_CODE (decl))
13893 case FUNCTION_DECL:
13894 /* Clear current_function_decl, so that gen_subprogram_die thinks
13895 that this is a declaration. At this point, we just want to force
13896 declaration die. */
13897 save_fn = current_function_decl;
13898 current_function_decl = NULL_TREE;
13899 gen_subprogram_die (decl, context_die);
13900 current_function_decl = save_fn;
13901 break;
13903 case VAR_DECL:
13904 /* Set external flag to force declaration die. Restore it after
13905 gen_decl_die() call. */
13906 saved_external_flag = DECL_EXTERNAL (decl);
13907 DECL_EXTERNAL (decl) = 1;
13908 gen_decl_die (decl, context_die);
13909 DECL_EXTERNAL (decl) = saved_external_flag;
13910 break;
13912 case NAMESPACE_DECL:
13913 dwarf2out_decl (decl);
13914 break;
13916 default:
13917 gcc_unreachable ();
13920 /* We should be able to find the DIE now. */
13921 if (!decl_die)
13922 decl_die = lookup_decl_die (decl);
13923 gcc_assert (decl_die);
13926 return decl_die;
13929 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
13930 always returned. */
13932 static dw_die_ref
13933 force_type_die (tree type)
13935 dw_die_ref type_die;
13937 type_die = lookup_type_die (type);
13938 if (!type_die)
13940 dw_die_ref context_die;
13941 if (TYPE_CONTEXT (type))
13943 if (TYPE_P (TYPE_CONTEXT (type)))
13944 context_die = force_type_die (TYPE_CONTEXT (type));
13945 else
13946 context_die = force_decl_die (TYPE_CONTEXT (type));
13948 else
13949 context_die = comp_unit_die;
13951 type_die = modified_type_die (type, TYPE_READONLY (type),
13952 TYPE_VOLATILE (type), context_die);
13953 gcc_assert (type_die);
13955 return type_die;
13958 /* Force out any required namespaces to be able to output DECL,
13959 and return the new context_die for it, if it's changed. */
13961 static dw_die_ref
13962 setup_namespace_context (tree thing, dw_die_ref context_die)
13964 tree context = (DECL_P (thing)
13965 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
13966 if (context && TREE_CODE (context) == NAMESPACE_DECL)
13967 /* Force out the namespace. */
13968 context_die = force_decl_die (context);
13970 return context_die;
13973 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
13974 type) within its namespace, if appropriate.
13976 For compatibility with older debuggers, namespace DIEs only contain
13977 declarations; all definitions are emitted at CU scope. */
13979 static void
13980 declare_in_namespace (tree thing, dw_die_ref context_die)
13982 dw_die_ref ns_context;
13984 if (debug_info_level <= DINFO_LEVEL_TERSE)
13985 return;
13987 /* If this decl is from an inlined function, then don't try to emit it in its
13988 namespace, as we will get confused. It would have already been emitted
13989 when the abstract instance of the inline function was emitted anyways. */
13990 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
13991 return;
13993 ns_context = setup_namespace_context (thing, context_die);
13995 if (ns_context != context_die)
13997 if (DECL_P (thing))
13998 gen_decl_die (thing, ns_context);
13999 else
14000 gen_type_die (thing, ns_context);
14004 /* Generate a DIE for a namespace or namespace alias. */
14006 static void
14007 gen_namespace_die (tree decl)
14009 dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
14011 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
14012 they are an alias of. */
14013 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
14015 /* Output a real namespace. */
14016 dw_die_ref namespace_die
14017 = new_die (DW_TAG_namespace, context_die, decl);
14018 add_name_and_src_coords_attributes (namespace_die, decl);
14019 equate_decl_number_to_die (decl, namespace_die);
14021 else
14023 /* Output a namespace alias. */
14025 /* Force out the namespace we are an alias of, if necessary. */
14026 dw_die_ref origin_die
14027 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
14029 /* Now create the namespace alias DIE. */
14030 dw_die_ref namespace_die
14031 = new_die (DW_TAG_imported_declaration, context_die, decl);
14032 add_name_and_src_coords_attributes (namespace_die, decl);
14033 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
14034 equate_decl_number_to_die (decl, namespace_die);
14038 /* Generate Dwarf debug information for a decl described by DECL. */
14040 static void
14041 gen_decl_die (tree decl, dw_die_ref context_die)
14043 tree origin;
14045 if (DECL_P (decl) && DECL_IGNORED_P (decl))
14046 return;
14048 switch (TREE_CODE (decl))
14050 case ERROR_MARK:
14051 break;
14053 case CONST_DECL:
14054 /* The individual enumerators of an enum type get output when we output
14055 the Dwarf representation of the relevant enum type itself. */
14056 break;
14058 case FUNCTION_DECL:
14059 /* Don't output any DIEs to represent mere function declarations,
14060 unless they are class members or explicit block externs. */
14061 if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
14062 && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
14063 break;
14065 #if 0
14066 /* FIXME */
14067 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
14068 on local redeclarations of global functions. That seems broken. */
14069 if (current_function_decl != decl)
14070 /* This is only a declaration. */;
14071 #endif
14073 /* If we're emitting a clone, emit info for the abstract instance. */
14074 if (DECL_ORIGIN (decl) != decl)
14075 dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
14077 /* If we're emitting an out-of-line copy of an inline function,
14078 emit info for the abstract instance and set up to refer to it. */
14079 else if (cgraph_function_possibly_inlined_p (decl)
14080 && ! DECL_ABSTRACT (decl)
14081 && ! class_or_namespace_scope_p (context_die)
14082 /* dwarf2out_abstract_function won't emit a die if this is just
14083 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
14084 that case, because that works only if we have a die. */
14085 && DECL_INITIAL (decl) != NULL_TREE)
14087 dwarf2out_abstract_function (decl);
14088 set_decl_origin_self (decl);
14091 /* Otherwise we're emitting the primary DIE for this decl. */
14092 else if (debug_info_level > DINFO_LEVEL_TERSE)
14094 /* Before we describe the FUNCTION_DECL itself, make sure that we
14095 have described its return type. */
14096 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
14098 /* And its virtual context. */
14099 if (DECL_VINDEX (decl) != NULL_TREE)
14100 gen_type_die (DECL_CONTEXT (decl), context_die);
14102 /* And its containing type. */
14103 origin = decl_class_context (decl);
14104 if (origin != NULL_TREE)
14105 gen_type_die_for_member (origin, decl, context_die);
14107 /* And its containing namespace. */
14108 declare_in_namespace (decl, context_die);
14111 /* Now output a DIE to represent the function itself. */
14112 gen_subprogram_die (decl, context_die);
14113 break;
14115 case TYPE_DECL:
14116 /* If we are in terse mode, don't generate any DIEs to represent any
14117 actual typedefs. */
14118 if (debug_info_level <= DINFO_LEVEL_TERSE)
14119 break;
14121 /* In the special case of a TYPE_DECL node representing the declaration
14122 of some type tag, if the given TYPE_DECL is marked as having been
14123 instantiated from some other (original) TYPE_DECL node (e.g. one which
14124 was generated within the original definition of an inline function) we
14125 have to generate a special (abbreviated) DW_TAG_structure_type,
14126 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. */
14127 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE
14128 && is_tagged_type (TREE_TYPE (decl)))
14130 gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
14131 break;
14134 if (is_redundant_typedef (decl))
14135 gen_type_die (TREE_TYPE (decl), context_die);
14136 else
14137 /* Output a DIE to represent the typedef itself. */
14138 gen_typedef_die (decl, context_die);
14139 break;
14141 case LABEL_DECL:
14142 if (debug_info_level >= DINFO_LEVEL_NORMAL)
14143 gen_label_die (decl, context_die);
14144 break;
14146 case VAR_DECL:
14147 case RESULT_DECL:
14148 /* If we are in terse mode, don't generate any DIEs to represent any
14149 variable declarations or definitions. */
14150 if (debug_info_level <= DINFO_LEVEL_TERSE)
14151 break;
14153 /* If this is the global definition of the Fortran COMMON block, we don't
14154 need to do anything. Syntactically, the block itself has no identity,
14155 just its constituent identifiers. */
14156 if (TREE_CODE (decl) == VAR_DECL
14157 && TREE_PUBLIC (decl)
14158 && TREE_STATIC (decl)
14159 && is_fortran ()
14160 && !DECL_HAS_VALUE_EXPR_P (decl))
14161 break;
14163 /* Output any DIEs that are needed to specify the type of this data
14164 object. */
14165 if (TREE_CODE (decl) == RESULT_DECL && DECL_BY_REFERENCE (decl))
14166 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
14167 else
14168 gen_type_die (TREE_TYPE (decl), context_die);
14170 /* And its containing type. */
14171 origin = decl_class_context (decl);
14172 if (origin != NULL_TREE)
14173 gen_type_die_for_member (origin, decl, context_die);
14175 /* And its containing namespace. */
14176 declare_in_namespace (decl, context_die);
14178 /* Now output the DIE to represent the data object itself. This gets
14179 complicated because of the possibility that the VAR_DECL really
14180 represents an inlined instance of a formal parameter for an inline
14181 function. */
14182 origin = decl_ultimate_origin (decl);
14183 if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
14184 gen_formal_parameter_die (decl, context_die);
14185 else
14186 gen_variable_die (decl, context_die);
14187 break;
14189 case FIELD_DECL:
14190 /* Ignore the nameless fields that are used to skip bits but handle C++
14191 anonymous unions and structs. */
14192 if (DECL_NAME (decl) != NULL_TREE
14193 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
14194 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
14196 gen_type_die (member_declared_type (decl), context_die);
14197 gen_field_die (decl, context_die);
14199 break;
14201 case PARM_DECL:
14202 if (DECL_BY_REFERENCE (decl))
14203 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
14204 else
14205 gen_type_die (TREE_TYPE (decl), context_die);
14206 gen_formal_parameter_die (decl, context_die);
14207 break;
14209 case NAMESPACE_DECL:
14210 gen_namespace_die (decl);
14211 break;
14213 default:
14214 /* Probably some frontend-internal decl. Assume we don't care. */
14215 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
14216 break;
14220 /* Output debug information for global decl DECL. Called from toplev.c after
14221 compilation proper has finished. */
14223 static void
14224 dwarf2out_global_decl (tree decl)
14226 /* Output DWARF2 information for file-scope tentative data object
14227 declarations, file-scope (extern) function declarations (which had no
14228 corresponding body) and file-scope tagged type declarations and
14229 definitions which have not yet been forced out.
14231 Ignore the global decl of any Fortran COMMON blocks which also wind up here
14232 though they have already been described in the local scope for the
14233 procedures using them. */
14234 if (TREE_CODE (decl) == VAR_DECL
14235 && TREE_PUBLIC (decl) && TREE_STATIC (decl) && is_fortran ())
14236 return;
14238 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
14239 dwarf2out_decl (decl);
14242 /* Output debug information for type decl DECL. Called from toplev.c
14243 and from language front ends (to record built-in types). */
14244 static void
14245 dwarf2out_type_decl (tree decl, int local)
14247 if (!local)
14248 dwarf2out_decl (decl);
14251 /* Output debug information for imported module or decl. */
14253 static void
14254 dwarf2out_imported_module_or_decl (tree decl, tree context)
14256 dw_die_ref imported_die, at_import_die;
14257 dw_die_ref scope_die;
14258 expanded_location xloc;
14260 if (debug_info_level <= DINFO_LEVEL_TERSE)
14261 return;
14263 gcc_assert (decl);
14265 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
14266 We need decl DIE for reference and scope die. First, get DIE for the decl
14267 itself. */
14269 /* Get the scope die for decl context. Use comp_unit_die for global module
14270 or decl. If die is not found for non globals, force new die. */
14271 if (!context)
14272 scope_die = comp_unit_die;
14273 else if (TYPE_P (context))
14275 if (!should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
14276 return;
14277 scope_die = force_type_die (context);
14279 else
14280 scope_die = force_decl_die (context);
14282 /* For TYPE_DECL or CONST_DECL, lookup TREE_TYPE. */
14283 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
14285 if (is_base_type (TREE_TYPE (decl)))
14286 at_import_die = base_type_die (TREE_TYPE (decl));
14287 else
14288 at_import_die = force_type_die (TREE_TYPE (decl));
14290 else
14292 at_import_die = lookup_decl_die (decl);
14293 if (!at_import_die)
14295 /* If we're trying to avoid duplicate debug info, we may not have
14296 emitted the member decl for this field. Emit it now. */
14297 if (TREE_CODE (decl) == FIELD_DECL)
14299 tree type = DECL_CONTEXT (decl);
14300 dw_die_ref type_context_die;
14302 if (TYPE_CONTEXT (type))
14303 if (TYPE_P (TYPE_CONTEXT (type)))
14305 if (!should_emit_struct_debug (TYPE_CONTEXT (type),
14306 DINFO_USAGE_DIR_USE))
14307 return;
14308 type_context_die = force_type_die (TYPE_CONTEXT (type));
14310 else
14311 type_context_die = force_decl_die (TYPE_CONTEXT (type));
14312 else
14313 type_context_die = comp_unit_die;
14314 gen_type_die_for_member (type, decl, type_context_die);
14316 at_import_die = force_decl_die (decl);
14320 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
14321 if (TREE_CODE (decl) == NAMESPACE_DECL)
14322 imported_die = new_die (DW_TAG_imported_module, scope_die, context);
14323 else
14324 imported_die = new_die (DW_TAG_imported_declaration, scope_die, context);
14326 xloc = expand_location (input_location);
14327 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
14328 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
14329 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
14332 /* Write the debugging output for DECL. */
14334 void
14335 dwarf2out_decl (tree decl)
14337 dw_die_ref context_die = comp_unit_die;
14339 switch (TREE_CODE (decl))
14341 case ERROR_MARK:
14342 return;
14344 case FUNCTION_DECL:
14345 /* What we would really like to do here is to filter out all mere
14346 file-scope declarations of file-scope functions which are never
14347 referenced later within this translation unit (and keep all of ones
14348 that *are* referenced later on) but we aren't clairvoyant, so we have
14349 no idea which functions will be referenced in the future (i.e. later
14350 on within the current translation unit). So here we just ignore all
14351 file-scope function declarations which are not also definitions. If
14352 and when the debugger needs to know something about these functions,
14353 it will have to hunt around and find the DWARF information associated
14354 with the definition of the function.
14356 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
14357 nodes represent definitions and which ones represent mere
14358 declarations. We have to check DECL_INITIAL instead. That's because
14359 the C front-end supports some weird semantics for "extern inline"
14360 function definitions. These can get inlined within the current
14361 translation unit (and thus, we need to generate Dwarf info for their
14362 abstract instances so that the Dwarf info for the concrete inlined
14363 instances can have something to refer to) but the compiler never
14364 generates any out-of-lines instances of such things (despite the fact
14365 that they *are* definitions).
14367 The important point is that the C front-end marks these "extern
14368 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
14369 them anyway. Note that the C++ front-end also plays some similar games
14370 for inline function definitions appearing within include files which
14371 also contain `#pragma interface' pragmas. */
14372 if (DECL_INITIAL (decl) == NULL_TREE)
14373 return;
14375 /* If we're a nested function, initially use a parent of NULL; if we're
14376 a plain function, this will be fixed up in decls_for_scope. If
14377 we're a method, it will be ignored, since we already have a DIE. */
14378 if (decl_function_context (decl)
14379 /* But if we're in terse mode, we don't care about scope. */
14380 && debug_info_level > DINFO_LEVEL_TERSE)
14381 context_die = NULL;
14382 break;
14384 case VAR_DECL:
14385 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
14386 declaration and if the declaration was never even referenced from
14387 within this entire compilation unit. We suppress these DIEs in
14388 order to save space in the .debug section (by eliminating entries
14389 which are probably useless). Note that we must not suppress
14390 block-local extern declarations (whether used or not) because that
14391 would screw-up the debugger's name lookup mechanism and cause it to
14392 miss things which really ought to be in scope at a given point. */
14393 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
14394 return;
14396 /* For local statics lookup proper context die. */
14397 if (TREE_STATIC (decl) && decl_function_context (decl))
14398 context_die = lookup_decl_die (DECL_CONTEXT (decl));
14400 /* If we are in terse mode, don't generate any DIEs to represent any
14401 variable declarations or definitions. */
14402 if (debug_info_level <= DINFO_LEVEL_TERSE)
14403 return;
14404 break;
14406 case NAMESPACE_DECL:
14407 if (debug_info_level <= DINFO_LEVEL_TERSE)
14408 return;
14409 if (lookup_decl_die (decl) != NULL)
14410 return;
14411 break;
14413 case TYPE_DECL:
14414 /* Don't emit stubs for types unless they are needed by other DIEs. */
14415 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
14416 return;
14418 /* Don't bother trying to generate any DIEs to represent any of the
14419 normal built-in types for the language we are compiling. */
14420 if (DECL_IS_BUILTIN (decl))
14422 /* OK, we need to generate one for `bool' so GDB knows what type
14423 comparisons have. */
14424 if (is_cxx ()
14425 && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
14426 && ! DECL_IGNORED_P (decl))
14427 modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
14429 return;
14432 /* If we are in terse mode, don't generate any DIEs for types. */
14433 if (debug_info_level <= DINFO_LEVEL_TERSE)
14434 return;
14436 /* If we're a function-scope tag, initially use a parent of NULL;
14437 this will be fixed up in decls_for_scope. */
14438 if (decl_function_context (decl))
14439 context_die = NULL;
14441 break;
14443 default:
14444 return;
14447 gen_decl_die (decl, context_die);
14450 /* Output a marker (i.e. a label) for the beginning of the generated code for
14451 a lexical block. */
14453 static void
14454 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
14455 unsigned int blocknum)
14457 switch_to_section (current_function_section ());
14458 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
14461 /* Output a marker (i.e. a label) for the end of the generated code for a
14462 lexical block. */
14464 static void
14465 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
14467 switch_to_section (current_function_section ());
14468 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
14471 /* Returns nonzero if it is appropriate not to emit any debugging
14472 information for BLOCK, because it doesn't contain any instructions.
14474 Don't allow this for blocks with nested functions or local classes
14475 as we would end up with orphans, and in the presence of scheduling
14476 we may end up calling them anyway. */
14478 static bool
14479 dwarf2out_ignore_block (const_tree block)
14481 tree decl;
14483 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
14484 if (TREE_CODE (decl) == FUNCTION_DECL
14485 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
14486 return 0;
14488 return 1;
14491 /* Hash table routines for file_hash. */
14493 static int
14494 file_table_eq (const void *p1_p, const void *p2_p)
14496 const struct dwarf_file_data * p1 = p1_p;
14497 const char * p2 = p2_p;
14498 return strcmp (p1->filename, p2) == 0;
14501 static hashval_t
14502 file_table_hash (const void *p_p)
14504 const struct dwarf_file_data * p = p_p;
14505 return htab_hash_string (p->filename);
14508 /* Lookup FILE_NAME (in the list of filenames that we know about here in
14509 dwarf2out.c) and return its "index". The index of each (known) filename is
14510 just a unique number which is associated with only that one filename. We
14511 need such numbers for the sake of generating labels (in the .debug_sfnames
14512 section) and references to those files numbers (in the .debug_srcinfo
14513 and.debug_macinfo sections). If the filename given as an argument is not
14514 found in our current list, add it to the list and assign it the next
14515 available unique index number. In order to speed up searches, we remember
14516 the index of the filename was looked up last. This handles the majority of
14517 all searches. */
14519 static struct dwarf_file_data *
14520 lookup_filename (const char *file_name)
14522 void ** slot;
14523 struct dwarf_file_data * created;
14525 /* Check to see if the file name that was searched on the previous
14526 call matches this file name. If so, return the index. */
14527 if (file_table_last_lookup
14528 && (file_name == file_table_last_lookup->filename
14529 || strcmp (file_table_last_lookup->filename, file_name) == 0))
14530 return file_table_last_lookup;
14532 /* Didn't match the previous lookup, search the table. */
14533 slot = htab_find_slot_with_hash (file_table, file_name,
14534 htab_hash_string (file_name), INSERT);
14535 if (*slot)
14536 return *slot;
14538 created = ggc_alloc (sizeof (struct dwarf_file_data));
14539 created->filename = file_name;
14540 created->emitted_number = 0;
14541 *slot = created;
14542 return created;
14545 /* If the assembler will construct the file table, then translate the compiler
14546 internal file table number into the assembler file table number, and emit
14547 a .file directive if we haven't already emitted one yet. The file table
14548 numbers are different because we prune debug info for unused variables and
14549 types, which may include filenames. */
14551 static int
14552 maybe_emit_file (struct dwarf_file_data * fd)
14554 if (! fd->emitted_number)
14556 if (last_emitted_file)
14557 fd->emitted_number = last_emitted_file->emitted_number + 1;
14558 else
14559 fd->emitted_number = 1;
14560 last_emitted_file = fd;
14562 if (DWARF2_ASM_LINE_DEBUG_INFO)
14564 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
14565 output_quoted_string (asm_out_file,
14566 remap_debug_filename (fd->filename));
14567 fputc ('\n', asm_out_file);
14571 return fd->emitted_number;
14574 /* Called by the final INSN scan whenever we see a var location. We
14575 use it to drop labels in the right places, and throw the location in
14576 our lookup table. */
14578 static void
14579 dwarf2out_var_location (rtx loc_note)
14581 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
14582 struct var_loc_node *newloc;
14583 rtx prev_insn;
14584 static rtx last_insn;
14585 static const char *last_label;
14586 tree decl;
14588 if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
14589 return;
14590 prev_insn = PREV_INSN (loc_note);
14592 newloc = ggc_alloc_cleared (sizeof (struct var_loc_node));
14593 /* If the insn we processed last time is the previous insn
14594 and it is also a var location note, use the label we emitted
14595 last time. */
14596 if (last_insn != NULL_RTX
14597 && last_insn == prev_insn
14598 && NOTE_P (prev_insn)
14599 && NOTE_KIND (prev_insn) == NOTE_INSN_VAR_LOCATION)
14601 newloc->label = last_label;
14603 else
14605 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
14606 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
14607 loclabel_num++;
14608 newloc->label = ggc_strdup (loclabel);
14610 newloc->var_loc_note = loc_note;
14611 newloc->next = NULL;
14613 if (cfun && in_cold_section_p)
14614 newloc->section_label = crtl->subsections.cold_section_label;
14615 else
14616 newloc->section_label = text_section_label;
14618 last_insn = loc_note;
14619 last_label = newloc->label;
14620 decl = NOTE_VAR_LOCATION_DECL (loc_note);
14621 add_var_loc_to_decl (decl, newloc);
14624 /* We need to reset the locations at the beginning of each
14625 function. We can't do this in the end_function hook, because the
14626 declarations that use the locations won't have been output when
14627 that hook is called. Also compute have_multiple_function_sections here. */
14629 static void
14630 dwarf2out_begin_function (tree fun)
14632 htab_empty (decl_loc_table);
14634 if (function_section (fun) != text_section)
14635 have_multiple_function_sections = true;
14637 dwarf2out_note_section_used ();
14640 /* Output a label to mark the beginning of a source code line entry
14641 and record information relating to this source line, in
14642 'line_info_table' for later output of the .debug_line section. */
14644 static void
14645 dwarf2out_source_line (unsigned int line, const char *filename)
14647 if (debug_info_level >= DINFO_LEVEL_NORMAL
14648 && line != 0)
14650 int file_num = maybe_emit_file (lookup_filename (filename));
14652 switch_to_section (current_function_section ());
14654 /* If requested, emit something human-readable. */
14655 if (flag_debug_asm)
14656 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
14657 filename, line);
14659 if (DWARF2_ASM_LINE_DEBUG_INFO)
14661 /* Emit the .loc directive understood by GNU as. */
14662 fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
14664 /* Indicate that line number info exists. */
14665 line_info_table_in_use++;
14667 else if (function_section (current_function_decl) != text_section)
14669 dw_separate_line_info_ref line_info;
14670 targetm.asm_out.internal_label (asm_out_file,
14671 SEPARATE_LINE_CODE_LABEL,
14672 separate_line_info_table_in_use);
14674 /* Expand the line info table if necessary. */
14675 if (separate_line_info_table_in_use
14676 == separate_line_info_table_allocated)
14678 separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
14679 separate_line_info_table
14680 = ggc_realloc (separate_line_info_table,
14681 separate_line_info_table_allocated
14682 * sizeof (dw_separate_line_info_entry));
14683 memset (separate_line_info_table
14684 + separate_line_info_table_in_use,
14686 (LINE_INFO_TABLE_INCREMENT
14687 * sizeof (dw_separate_line_info_entry)));
14690 /* Add the new entry at the end of the line_info_table. */
14691 line_info
14692 = &separate_line_info_table[separate_line_info_table_in_use++];
14693 line_info->dw_file_num = file_num;
14694 line_info->dw_line_num = line;
14695 line_info->function = current_function_funcdef_no;
14697 else
14699 dw_line_info_ref line_info;
14701 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
14702 line_info_table_in_use);
14704 /* Expand the line info table if necessary. */
14705 if (line_info_table_in_use == line_info_table_allocated)
14707 line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
14708 line_info_table
14709 = ggc_realloc (line_info_table,
14710 (line_info_table_allocated
14711 * sizeof (dw_line_info_entry)));
14712 memset (line_info_table + line_info_table_in_use, 0,
14713 LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
14716 /* Add the new entry at the end of the line_info_table. */
14717 line_info = &line_info_table[line_info_table_in_use++];
14718 line_info->dw_file_num = file_num;
14719 line_info->dw_line_num = line;
14724 /* Record the beginning of a new source file. */
14726 static void
14727 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
14729 if (flag_eliminate_dwarf2_dups)
14731 /* Record the beginning of the file for break_out_includes. */
14732 dw_die_ref bincl_die;
14734 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
14735 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
14738 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14740 int file_num = maybe_emit_file (lookup_filename (filename));
14742 switch_to_section (debug_macinfo_section);
14743 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
14744 dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
14745 lineno);
14747 dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
14751 /* Record the end of a source file. */
14753 static void
14754 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
14756 if (flag_eliminate_dwarf2_dups)
14757 /* Record the end of the file for break_out_includes. */
14758 new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
14760 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14762 switch_to_section (debug_macinfo_section);
14763 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
14767 /* Called from debug_define in toplev.c. The `buffer' parameter contains
14768 the tail part of the directive line, i.e. the part which is past the
14769 initial whitespace, #, whitespace, directive-name, whitespace part. */
14771 static void
14772 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
14773 const char *buffer ATTRIBUTE_UNUSED)
14775 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14777 switch_to_section (debug_macinfo_section);
14778 dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
14779 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
14780 dw2_asm_output_nstring (buffer, -1, "The macro");
14784 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
14785 the tail part of the directive line, i.e. the part which is past the
14786 initial whitespace, #, whitespace, directive-name, whitespace part. */
14788 static void
14789 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
14790 const char *buffer ATTRIBUTE_UNUSED)
14792 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14794 switch_to_section (debug_macinfo_section);
14795 dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
14796 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
14797 dw2_asm_output_nstring (buffer, -1, "The macro");
14801 /* Set up for Dwarf output at the start of compilation. */
14803 static void
14804 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
14806 /* Allocate the file_table. */
14807 file_table = htab_create_ggc (50, file_table_hash,
14808 file_table_eq, NULL);
14810 /* Allocate the decl_die_table. */
14811 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
14812 decl_die_table_eq, NULL);
14814 /* Allocate the decl_loc_table. */
14815 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
14816 decl_loc_table_eq, NULL);
14818 /* Allocate the initial hunk of the decl_scope_table. */
14819 decl_scope_table = VEC_alloc (tree, gc, 256);
14821 /* Allocate the initial hunk of the abbrev_die_table. */
14822 abbrev_die_table = ggc_alloc_cleared (ABBREV_DIE_TABLE_INCREMENT
14823 * sizeof (dw_die_ref));
14824 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
14825 /* Zero-th entry is allocated, but unused. */
14826 abbrev_die_table_in_use = 1;
14828 /* Allocate the initial hunk of the line_info_table. */
14829 line_info_table = ggc_alloc_cleared (LINE_INFO_TABLE_INCREMENT
14830 * sizeof (dw_line_info_entry));
14831 line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
14833 /* Zero-th entry is allocated, but unused. */
14834 line_info_table_in_use = 1;
14836 /* Allocate the pubtypes and pubnames vectors. */
14837 pubname_table = VEC_alloc (pubname_entry, gc, 32);
14838 pubtype_table = VEC_alloc (pubname_entry, gc, 32);
14840 /* Generate the initial DIE for the .debug section. Note that the (string)
14841 value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
14842 will (typically) be a relative pathname and that this pathname should be
14843 taken as being relative to the directory from which the compiler was
14844 invoked when the given (base) source file was compiled. We will fill
14845 in this value in dwarf2out_finish. */
14846 comp_unit_die = gen_compile_unit_die (NULL);
14848 incomplete_types = VEC_alloc (tree, gc, 64);
14850 used_rtx_array = VEC_alloc (rtx, gc, 32);
14852 debug_info_section = get_section (DEBUG_INFO_SECTION,
14853 SECTION_DEBUG, NULL);
14854 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
14855 SECTION_DEBUG, NULL);
14856 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
14857 SECTION_DEBUG, NULL);
14858 debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
14859 SECTION_DEBUG, NULL);
14860 debug_line_section = get_section (DEBUG_LINE_SECTION,
14861 SECTION_DEBUG, NULL);
14862 debug_loc_section = get_section (DEBUG_LOC_SECTION,
14863 SECTION_DEBUG, NULL);
14864 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
14865 SECTION_DEBUG, NULL);
14866 #ifdef DEBUG_PUBTYPES_SECTION
14867 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
14868 SECTION_DEBUG, NULL);
14869 #endif
14870 debug_str_section = get_section (DEBUG_STR_SECTION,
14871 DEBUG_STR_SECTION_FLAGS, NULL);
14872 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
14873 SECTION_DEBUG, NULL);
14874 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
14875 SECTION_DEBUG, NULL);
14877 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
14878 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
14879 DEBUG_ABBREV_SECTION_LABEL, 0);
14880 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
14881 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
14882 COLD_TEXT_SECTION_LABEL, 0);
14883 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
14885 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
14886 DEBUG_INFO_SECTION_LABEL, 0);
14887 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
14888 DEBUG_LINE_SECTION_LABEL, 0);
14889 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
14890 DEBUG_RANGES_SECTION_LABEL, 0);
14891 switch_to_section (debug_abbrev_section);
14892 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
14893 switch_to_section (debug_info_section);
14894 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
14895 switch_to_section (debug_line_section);
14896 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
14898 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14900 switch_to_section (debug_macinfo_section);
14901 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
14902 DEBUG_MACINFO_SECTION_LABEL, 0);
14903 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
14906 switch_to_section (text_section);
14907 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
14908 if (flag_reorder_blocks_and_partition)
14910 cold_text_section = unlikely_text_section ();
14911 switch_to_section (cold_text_section);
14912 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
14916 /* A helper function for dwarf2out_finish called through
14917 ht_forall. Emit one queued .debug_str string. */
14919 static int
14920 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
14922 struct indirect_string_node *node = (struct indirect_string_node *) *h;
14924 if (node->form == DW_FORM_strp)
14926 switch_to_section (debug_str_section);
14927 ASM_OUTPUT_LABEL (asm_out_file, node->label);
14928 assemble_string (node->str, strlen (node->str) + 1);
14931 return 1;
14934 #if ENABLE_ASSERT_CHECKING
14935 /* Verify that all marks are clear. */
14937 static void
14938 verify_marks_clear (dw_die_ref die)
14940 dw_die_ref c;
14942 gcc_assert (! die->die_mark);
14943 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
14945 #endif /* ENABLE_ASSERT_CHECKING */
14947 /* Clear the marks for a die and its children.
14948 Be cool if the mark isn't set. */
14950 static void
14951 prune_unmark_dies (dw_die_ref die)
14953 dw_die_ref c;
14955 if (die->die_mark)
14956 die->die_mark = 0;
14957 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
14960 /* Given DIE that we're marking as used, find any other dies
14961 it references as attributes and mark them as used. */
14963 static void
14964 prune_unused_types_walk_attribs (dw_die_ref die)
14966 dw_attr_ref a;
14967 unsigned ix;
14969 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
14971 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
14973 /* A reference to another DIE.
14974 Make sure that it will get emitted. */
14975 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
14977 /* Set the string's refcount to 0 so that prune_unused_types_mark
14978 accounts properly for it. */
14979 if (AT_class (a) == dw_val_class_str)
14980 a->dw_attr_val.v.val_str->refcount = 0;
14985 /* Mark DIE as being used. If DOKIDS is true, then walk down
14986 to DIE's children. */
14988 static void
14989 prune_unused_types_mark (dw_die_ref die, int dokids)
14991 dw_die_ref c;
14993 if (die->die_mark == 0)
14995 /* We haven't done this node yet. Mark it as used. */
14996 die->die_mark = 1;
14998 /* We also have to mark its parents as used.
14999 (But we don't want to mark our parents' kids due to this.) */
15000 if (die->die_parent)
15001 prune_unused_types_mark (die->die_parent, 0);
15003 /* Mark any referenced nodes. */
15004 prune_unused_types_walk_attribs (die);
15006 /* If this node is a specification,
15007 also mark the definition, if it exists. */
15008 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
15009 prune_unused_types_mark (die->die_definition, 1);
15012 if (dokids && die->die_mark != 2)
15014 /* We need to walk the children, but haven't done so yet.
15015 Remember that we've walked the kids. */
15016 die->die_mark = 2;
15018 /* If this is an array type, we need to make sure our
15019 kids get marked, even if they're types. */
15020 if (die->die_tag == DW_TAG_array_type)
15021 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
15022 else
15023 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
15028 /* Walk the tree DIE and mark types that we actually use. */
15030 static void
15031 prune_unused_types_walk (dw_die_ref die)
15033 dw_die_ref c;
15035 /* Don't do anything if this node is already marked. */
15036 if (die->die_mark)
15037 return;
15039 switch (die->die_tag)
15041 case DW_TAG_const_type:
15042 case DW_TAG_packed_type:
15043 case DW_TAG_pointer_type:
15044 case DW_TAG_reference_type:
15045 case DW_TAG_volatile_type:
15046 case DW_TAG_typedef:
15047 case DW_TAG_array_type:
15048 case DW_TAG_structure_type:
15049 case DW_TAG_union_type:
15050 case DW_TAG_class_type:
15051 case DW_TAG_interface_type:
15052 case DW_TAG_friend:
15053 case DW_TAG_variant_part:
15054 case DW_TAG_enumeration_type:
15055 case DW_TAG_subroutine_type:
15056 case DW_TAG_string_type:
15057 case DW_TAG_set_type:
15058 case DW_TAG_subrange_type:
15059 case DW_TAG_ptr_to_member_type:
15060 case DW_TAG_file_type:
15061 if (die->die_perennial_p)
15062 break;
15064 /* It's a type node --- don't mark it. */
15065 return;
15067 default:
15068 /* Mark everything else. */
15069 break;
15072 die->die_mark = 1;
15074 /* Now, mark any dies referenced from here. */
15075 prune_unused_types_walk_attribs (die);
15077 /* Mark children. */
15078 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
15081 /* Increment the string counts on strings referred to from DIE's
15082 attributes. */
15084 static void
15085 prune_unused_types_update_strings (dw_die_ref die)
15087 dw_attr_ref a;
15088 unsigned ix;
15090 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
15091 if (AT_class (a) == dw_val_class_str)
15093 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
15094 s->refcount++;
15095 /* Avoid unnecessarily putting strings that are used less than
15096 twice in the hash table. */
15097 if (s->refcount
15098 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
15100 void ** slot;
15101 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
15102 htab_hash_string (s->str),
15103 INSERT);
15104 gcc_assert (*slot == NULL);
15105 *slot = s;
15110 /* Remove from the tree DIE any dies that aren't marked. */
15112 static void
15113 prune_unused_types_prune (dw_die_ref die)
15115 dw_die_ref c;
15117 gcc_assert (die->die_mark);
15118 prune_unused_types_update_strings (die);
15120 if (! die->die_child)
15121 return;
15123 c = die->die_child;
15124 do {
15125 dw_die_ref prev = c;
15126 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
15127 if (c == die->die_child)
15129 /* No marked children between 'prev' and the end of the list. */
15130 if (prev == c)
15131 /* No marked children at all. */
15132 die->die_child = NULL;
15133 else
15135 prev->die_sib = c->die_sib;
15136 die->die_child = prev;
15138 return;
15141 if (c != prev->die_sib)
15142 prev->die_sib = c;
15143 prune_unused_types_prune (c);
15144 } while (c != die->die_child);
15148 /* Remove dies representing declarations that we never use. */
15150 static void
15151 prune_unused_types (void)
15153 unsigned int i;
15154 limbo_die_node *node;
15155 pubname_ref pub;
15157 #if ENABLE_ASSERT_CHECKING
15158 /* All the marks should already be clear. */
15159 verify_marks_clear (comp_unit_die);
15160 for (node = limbo_die_list; node; node = node->next)
15161 verify_marks_clear (node->die);
15162 #endif /* ENABLE_ASSERT_CHECKING */
15164 /* Set the mark on nodes that are actually used. */
15165 prune_unused_types_walk (comp_unit_die);
15166 for (node = limbo_die_list; node; node = node->next)
15167 prune_unused_types_walk (node->die);
15169 /* Also set the mark on nodes referenced from the
15170 pubname_table or arange_table. */
15171 for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
15172 prune_unused_types_mark (pub->die, 1);
15173 for (i = 0; i < arange_table_in_use; i++)
15174 prune_unused_types_mark (arange_table[i], 1);
15176 /* Get rid of nodes that aren't marked; and update the string counts. */
15177 if (debug_str_hash)
15178 htab_empty (debug_str_hash);
15179 prune_unused_types_prune (comp_unit_die);
15180 for (node = limbo_die_list; node; node = node->next)
15181 prune_unused_types_prune (node->die);
15183 /* Leave the marks clear. */
15184 prune_unmark_dies (comp_unit_die);
15185 for (node = limbo_die_list; node; node = node->next)
15186 prune_unmark_dies (node->die);
15189 /* Set the parameter to true if there are any relative pathnames in
15190 the file table. */
15191 static int
15192 file_table_relative_p (void ** slot, void *param)
15194 bool *p = param;
15195 struct dwarf_file_data *d = *slot;
15196 if (!IS_ABSOLUTE_PATH (d->filename))
15198 *p = true;
15199 return 0;
15201 return 1;
15204 /* Output stuff that dwarf requires at the end of every file,
15205 and generate the DWARF-2 debugging info. */
15207 static void
15208 dwarf2out_finish (const char *filename)
15210 limbo_die_node *node, *next_node;
15211 dw_die_ref die = 0;
15213 /* Add the name for the main input file now. We delayed this from
15214 dwarf2out_init to avoid complications with PCH. */
15215 add_name_attribute (comp_unit_die, remap_debug_filename (filename));
15216 if (!IS_ABSOLUTE_PATH (filename))
15217 add_comp_dir_attribute (comp_unit_die);
15218 else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
15220 bool p = false;
15221 htab_traverse (file_table, file_table_relative_p, &p);
15222 if (p)
15223 add_comp_dir_attribute (comp_unit_die);
15226 /* Traverse the limbo die list, and add parent/child links. The only
15227 dies without parents that should be here are concrete instances of
15228 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
15229 For concrete instances, we can get the parent die from the abstract
15230 instance. */
15231 for (node = limbo_die_list; node; node = next_node)
15233 next_node = node->next;
15234 die = node->die;
15236 if (die->die_parent == NULL)
15238 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
15240 if (origin)
15241 add_child_die (origin->die_parent, die);
15242 else if (die == comp_unit_die)
15244 else if (errorcount > 0 || sorrycount > 0)
15245 /* It's OK to be confused by errors in the input. */
15246 add_child_die (comp_unit_die, die);
15247 else
15249 /* In certain situations, the lexical block containing a
15250 nested function can be optimized away, which results
15251 in the nested function die being orphaned. Likewise
15252 with the return type of that nested function. Force
15253 this to be a child of the containing function.
15255 It may happen that even the containing function got fully
15256 inlined and optimized out. In that case we are lost and
15257 assign the empty child. This should not be big issue as
15258 the function is likely unreachable too. */
15259 tree context = NULL_TREE;
15261 gcc_assert (node->created_for);
15263 if (DECL_P (node->created_for))
15264 context = DECL_CONTEXT (node->created_for);
15265 else if (TYPE_P (node->created_for))
15266 context = TYPE_CONTEXT (node->created_for);
15268 gcc_assert (context
15269 && (TREE_CODE (context) == FUNCTION_DECL
15270 || TREE_CODE (context) == NAMESPACE_DECL));
15272 origin = lookup_decl_die (context);
15273 if (origin)
15274 add_child_die (origin, die);
15275 else
15276 add_child_die (comp_unit_die, die);
15281 limbo_die_list = NULL;
15283 /* Walk through the list of incomplete types again, trying once more to
15284 emit full debugging info for them. */
15285 retry_incomplete_types ();
15287 if (flag_eliminate_unused_debug_types)
15288 prune_unused_types ();
15290 /* Generate separate CUs for each of the include files we've seen.
15291 They will go into limbo_die_list. */
15292 if (flag_eliminate_dwarf2_dups)
15293 break_out_includes (comp_unit_die);
15295 /* Traverse the DIE's and add add sibling attributes to those DIE's
15296 that have children. */
15297 add_sibling_attributes (comp_unit_die);
15298 for (node = limbo_die_list; node; node = node->next)
15299 add_sibling_attributes (node->die);
15301 /* Output a terminator label for the .text section. */
15302 switch_to_section (text_section);
15303 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
15304 if (flag_reorder_blocks_and_partition)
15306 switch_to_section (unlikely_text_section ());
15307 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
15310 /* We can only use the low/high_pc attributes if all of the code was
15311 in .text. */
15312 if (!have_multiple_function_sections)
15314 add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
15315 add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
15318 else
15320 unsigned fde_idx = 0;
15322 /* We need to give .debug_loc and .debug_ranges an appropriate
15323 "base address". Use zero so that these addresses become
15324 absolute. Historically, we've emitted the unexpected
15325 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
15326 Emit both to give time for other tools to adapt. */
15327 add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
15328 add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
15330 add_AT_range_list (comp_unit_die, DW_AT_ranges,
15331 add_ranges_by_labels (text_section_label,
15332 text_end_label));
15333 if (flag_reorder_blocks_and_partition)
15334 add_ranges_by_labels (cold_text_section_label,
15335 cold_end_label);
15337 for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
15339 dw_fde_ref fde = &fde_table[fde_idx];
15341 if (fde->dw_fde_switched_sections)
15343 add_ranges_by_labels (fde->dw_fde_hot_section_label,
15344 fde->dw_fde_hot_section_end_label);
15345 add_ranges_by_labels (fde->dw_fde_unlikely_section_label,
15346 fde->dw_fde_unlikely_section_end_label);
15348 else
15349 add_ranges_by_labels (fde->dw_fde_begin,
15350 fde->dw_fde_end);
15353 add_ranges (NULL);
15356 /* Output location list section if necessary. */
15357 if (have_location_lists)
15359 /* Output the location lists info. */
15360 switch_to_section (debug_loc_section);
15361 ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
15362 DEBUG_LOC_SECTION_LABEL, 0);
15363 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
15364 output_location_lists (die);
15367 if (debug_info_level >= DINFO_LEVEL_NORMAL)
15368 add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
15369 debug_line_section_label);
15371 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15372 add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
15374 /* Output all of the compilation units. We put the main one last so that
15375 the offsets are available to output_pubnames. */
15376 for (node = limbo_die_list; node; node = node->next)
15377 output_comp_unit (node->die, 0);
15379 output_comp_unit (comp_unit_die, 0);
15381 /* Output the abbreviation table. */
15382 switch_to_section (debug_abbrev_section);
15383 output_abbrev_section ();
15385 /* Output public names table if necessary. */
15386 if (!VEC_empty (pubname_entry, pubname_table))
15388 switch_to_section (debug_pubnames_section);
15389 output_pubnames (pubname_table);
15392 #ifdef DEBUG_PUBTYPES_SECTION
15393 /* Output public types table if necessary. */
15394 if (!VEC_empty (pubname_entry, pubtype_table))
15396 switch_to_section (debug_pubtypes_section);
15397 output_pubnames (pubtype_table);
15399 #endif
15401 /* Output the address range information. We only put functions in the arange
15402 table, so don't write it out if we don't have any. */
15403 if (fde_table_in_use)
15405 switch_to_section (debug_aranges_section);
15406 output_aranges ();
15409 /* Output ranges section if necessary. */
15410 if (ranges_table_in_use)
15412 switch_to_section (debug_ranges_section);
15413 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
15414 output_ranges ();
15417 /* Output the source line correspondence table. We must do this
15418 even if there is no line information. Otherwise, on an empty
15419 translation unit, we will generate a present, but empty,
15420 .debug_info section. IRIX 6.5 `nm' will then complain when
15421 examining the file. This is done late so that any filenames
15422 used by the debug_info section are marked as 'used'. */
15423 if (! DWARF2_ASM_LINE_DEBUG_INFO)
15425 switch_to_section (debug_line_section);
15426 output_line_info ();
15429 /* Have to end the macro section. */
15430 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15432 switch_to_section (debug_macinfo_section);
15433 dw2_asm_output_data (1, 0, "End compilation unit");
15436 /* If we emitted any DW_FORM_strp form attribute, output the string
15437 table too. */
15438 if (debug_str_hash)
15439 htab_traverse (debug_str_hash, output_indirect_string, NULL);
15441 #else
15443 /* This should never be used, but its address is needed for comparisons. */
15444 const struct gcc_debug_hooks dwarf2_debug_hooks;
15446 #endif /* DWARF2_DEBUGGING_INFO */
15448 #include "gt-dwarf2out.h"