* config/rs6000/rs6000-c.c (altivec_resolve_overloaded_builtin):
[official-gcc.git] / gcc / dwarf2out.c
blobf2da4ac1f50768588455bcf71b1177ca408919a4
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, 2009 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 /* Save the result of dwarf2out_do_frame across PCH. */
114 static GTY(()) bool saved_do_cfi_asm = 0;
116 /* Decide whether we want to emit frame unwind information for the current
117 translation unit. */
120 dwarf2out_do_frame (void)
122 /* We want to emit correct CFA location expressions or lists, so we
123 have to return true if we're going to output debug info, even if
124 we're not going to output frame or unwind info. */
125 return (write_symbols == DWARF2_DEBUG
126 || write_symbols == VMS_AND_DWARF2_DEBUG
127 || DWARF2_FRAME_INFO || saved_do_cfi_asm
128 #ifdef DWARF2_UNWIND_INFO
129 || (DWARF2_UNWIND_INFO
130 && (flag_unwind_tables
131 || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)))
132 #endif
136 /* Decide whether to emit frame unwind via assembler directives. */
139 dwarf2out_do_cfi_asm (void)
141 int enc;
143 #ifdef MIPS_DEBUGGING_INFO
144 return false;
145 #endif
146 if (!flag_dwarf2_cfi_asm || !dwarf2out_do_frame ())
147 return false;
148 if (saved_do_cfi_asm || !eh_personality_libfunc)
149 return true;
150 if (!HAVE_GAS_CFI_PERSONALITY_DIRECTIVE)
151 return false;
153 /* Make sure the personality encoding is one the assembler can support.
154 In particular, aligned addresses can't be handled. */
155 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,/*global=*/1);
156 if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
157 return false;
158 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,/*global=*/0);
159 if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
160 return false;
162 saved_do_cfi_asm = true;
163 return true;
166 /* The size of the target's pointer type. */
167 #ifndef PTR_SIZE
168 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
169 #endif
171 /* Array of RTXes referenced by the debugging information, which therefore
172 must be kept around forever. */
173 static GTY(()) VEC(rtx,gc) *used_rtx_array;
175 /* A pointer to the base of a list of incomplete types which might be
176 completed at some later time. incomplete_types_list needs to be a
177 VEC(tree,gc) because we want to tell the garbage collector about
178 it. */
179 static GTY(()) VEC(tree,gc) *incomplete_types;
181 /* A pointer to the base of a table of references to declaration
182 scopes. This table is a display which tracks the nesting
183 of declaration scopes at the current scope and containing
184 scopes. This table is used to find the proper place to
185 define type declaration DIE's. */
186 static GTY(()) VEC(tree,gc) *decl_scope_table;
188 /* Pointers to various DWARF2 sections. */
189 static GTY(()) section *debug_info_section;
190 static GTY(()) section *debug_abbrev_section;
191 static GTY(()) section *debug_aranges_section;
192 static GTY(()) section *debug_macinfo_section;
193 static GTY(()) section *debug_line_section;
194 static GTY(()) section *debug_loc_section;
195 static GTY(()) section *debug_pubnames_section;
196 static GTY(()) section *debug_pubtypes_section;
197 static GTY(()) section *debug_str_section;
198 static GTY(()) section *debug_ranges_section;
199 static GTY(()) section *debug_frame_section;
201 /* How to start an assembler comment. */
202 #ifndef ASM_COMMENT_START
203 #define ASM_COMMENT_START ";#"
204 #endif
206 typedef struct dw_cfi_struct *dw_cfi_ref;
207 typedef struct dw_fde_struct *dw_fde_ref;
208 typedef union dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
210 /* Call frames are described using a sequence of Call Frame
211 Information instructions. The register number, offset
212 and address fields are provided as possible operands;
213 their use is selected by the opcode field. */
215 enum dw_cfi_oprnd_type {
216 dw_cfi_oprnd_unused,
217 dw_cfi_oprnd_reg_num,
218 dw_cfi_oprnd_offset,
219 dw_cfi_oprnd_addr,
220 dw_cfi_oprnd_loc
223 typedef union dw_cfi_oprnd_struct GTY(())
225 unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
226 HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
227 const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
228 struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
230 dw_cfi_oprnd;
232 typedef struct dw_cfi_struct GTY(())
234 dw_cfi_ref dw_cfi_next;
235 enum dwarf_call_frame_info dw_cfi_opc;
236 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
237 dw_cfi_oprnd1;
238 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
239 dw_cfi_oprnd2;
241 dw_cfi_node;
243 /* This is how we define the location of the CFA. We use to handle it
244 as REG + OFFSET all the time, but now it can be more complex.
245 It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
246 Instead of passing around REG and OFFSET, we pass a copy
247 of this structure. */
248 typedef struct cfa_loc GTY(())
250 HOST_WIDE_INT offset;
251 HOST_WIDE_INT base_offset;
252 unsigned int reg;
253 int indirect; /* 1 if CFA is accessed via a dereference. */
254 } dw_cfa_location;
256 /* All call frame descriptions (FDE's) in the GCC generated DWARF
257 refer to a single Common Information Entry (CIE), defined at
258 the beginning of the .debug_frame section. This use of a single
259 CIE obviates the need to keep track of multiple CIE's
260 in the DWARF generation routines below. */
262 typedef struct dw_fde_struct GTY(())
264 tree decl;
265 const char *dw_fde_begin;
266 const char *dw_fde_current_label;
267 const char *dw_fde_end;
268 const char *dw_fde_hot_section_label;
269 const char *dw_fde_hot_section_end_label;
270 const char *dw_fde_unlikely_section_label;
271 const char *dw_fde_unlikely_section_end_label;
272 bool dw_fde_switched_sections;
273 dw_cfi_ref dw_fde_cfi;
274 unsigned funcdef_number;
275 HOST_WIDE_INT stack_realignment;
276 /* Dynamic realign argument pointer register. */
277 unsigned int drap_reg;
278 /* Virtual dynamic realign argument pointer register. */
279 unsigned int vdrap_reg;
280 unsigned all_throwers_are_sibcalls : 1;
281 unsigned nothrow : 1;
282 unsigned uses_eh_lsda : 1;
283 /* Whether we did stack realign in this call frame. */
284 unsigned stack_realign : 1;
285 /* Whether dynamic realign argument pointer register has been saved. */
286 unsigned drap_reg_saved: 1;
288 dw_fde_node;
290 /* Maximum size (in bytes) of an artificially generated label. */
291 #define MAX_ARTIFICIAL_LABEL_BYTES 30
293 /* The size of addresses as they appear in the Dwarf 2 data.
294 Some architectures use word addresses to refer to code locations,
295 but Dwarf 2 info always uses byte addresses. On such machines,
296 Dwarf 2 addresses need to be larger than the architecture's
297 pointers. */
298 #ifndef DWARF2_ADDR_SIZE
299 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
300 #endif
302 /* The size in bytes of a DWARF field indicating an offset or length
303 relative to a debug info section, specified to be 4 bytes in the
304 DWARF-2 specification. The SGI/MIPS ABI defines it to be the same
305 as PTR_SIZE. */
307 #ifndef DWARF_OFFSET_SIZE
308 #define DWARF_OFFSET_SIZE 4
309 #endif
311 /* According to the (draft) DWARF 3 specification, the initial length
312 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
313 bytes are 0xffffffff, followed by the length stored in the next 8
314 bytes.
316 However, the SGI/MIPS ABI uses an initial length which is equal to
317 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
319 #ifndef DWARF_INITIAL_LENGTH_SIZE
320 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
321 #endif
323 #define DWARF_VERSION 2
325 /* Round SIZE up to the nearest BOUNDARY. */
326 #define DWARF_ROUND(SIZE,BOUNDARY) \
327 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
329 /* Offsets recorded in opcodes are a multiple of this alignment factor. */
330 #ifndef DWARF_CIE_DATA_ALIGNMENT
331 #ifdef STACK_GROWS_DOWNWARD
332 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
333 #else
334 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
335 #endif
336 #endif
338 /* CIE identifier. */
339 #if HOST_BITS_PER_WIDE_INT >= 64
340 #define DWARF_CIE_ID \
341 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
342 #else
343 #define DWARF_CIE_ID DW_CIE_ID
344 #endif
346 /* A pointer to the base of a table that contains frame description
347 information for each routine. */
348 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
350 /* Number of elements currently allocated for fde_table. */
351 static GTY(()) unsigned fde_table_allocated;
353 /* Number of elements in fde_table currently in use. */
354 static GTY(()) unsigned fde_table_in_use;
356 /* Size (in elements) of increments by which we may expand the
357 fde_table. */
358 #define FDE_TABLE_INCREMENT 256
360 /* Get the current fde_table entry we should use. */
362 static inline dw_fde_ref
363 current_fde (void)
365 return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
368 /* A list of call frame insns for the CIE. */
369 static GTY(()) dw_cfi_ref cie_cfi_head;
371 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
372 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
373 attribute that accelerates the lookup of the FDE associated
374 with the subprogram. This variable holds the table index of the FDE
375 associated with the current function (body) definition. */
376 static unsigned current_funcdef_fde;
377 #endif
379 struct indirect_string_node GTY(())
381 const char *str;
382 unsigned int refcount;
383 unsigned int form;
384 char *label;
387 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
389 static GTY(()) int dw2_string_counter;
390 static GTY(()) unsigned long dwarf2out_cfi_label_num;
392 /* True if the compilation unit places functions in more than one section. */
393 static GTY(()) bool have_multiple_function_sections = false;
395 /* Whether the default text and cold text sections have been used at all. */
397 static GTY(()) bool text_section_used = false;
398 static GTY(()) bool cold_text_section_used = false;
400 /* The default cold text section. */
401 static GTY(()) section *cold_text_section;
403 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
405 /* Forward declarations for functions defined in this file. */
407 static char *stripattributes (const char *);
408 static const char *dwarf_cfi_name (unsigned);
409 static dw_cfi_ref new_cfi (void);
410 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
411 static void add_fde_cfi (const char *, dw_cfi_ref);
412 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *);
413 static void lookup_cfa (dw_cfa_location *);
414 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
415 #ifdef DWARF2_UNWIND_INFO
416 static void initial_return_save (rtx);
417 #endif
418 static HOST_WIDE_INT stack_adjust_offset (const_rtx, HOST_WIDE_INT,
419 HOST_WIDE_INT);
420 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
421 static void output_cfi_directive (dw_cfi_ref);
422 static void output_call_frame_info (int);
423 static void dwarf2out_note_section_used (void);
424 static void dwarf2out_stack_adjust (rtx, bool);
425 static void dwarf2out_args_size_adjust (HOST_WIDE_INT, const char *);
426 static void flush_queued_reg_saves (void);
427 static bool clobbers_queued_reg_save (const_rtx);
428 static void dwarf2out_frame_debug_expr (rtx, const char *);
430 /* Support for complex CFA locations. */
431 static void output_cfa_loc (dw_cfi_ref);
432 static void output_cfa_loc_raw (dw_cfi_ref);
433 static void get_cfa_from_loc_descr (dw_cfa_location *,
434 struct dw_loc_descr_struct *);
435 static struct dw_loc_descr_struct *build_cfa_loc
436 (dw_cfa_location *, HOST_WIDE_INT);
437 static struct dw_loc_descr_struct *build_cfa_aligned_loc
438 (HOST_WIDE_INT, HOST_WIDE_INT);
439 static void def_cfa_1 (const char *, dw_cfa_location *);
441 /* How to start an assembler comment. */
442 #ifndef ASM_COMMENT_START
443 #define ASM_COMMENT_START ";#"
444 #endif
446 /* Data and reference forms for relocatable data. */
447 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
448 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
450 #ifndef DEBUG_FRAME_SECTION
451 #define DEBUG_FRAME_SECTION ".debug_frame"
452 #endif
454 #ifndef FUNC_BEGIN_LABEL
455 #define FUNC_BEGIN_LABEL "LFB"
456 #endif
458 #ifndef FUNC_END_LABEL
459 #define FUNC_END_LABEL "LFE"
460 #endif
462 #ifndef FRAME_BEGIN_LABEL
463 #define FRAME_BEGIN_LABEL "Lframe"
464 #endif
465 #define CIE_AFTER_SIZE_LABEL "LSCIE"
466 #define CIE_END_LABEL "LECIE"
467 #define FDE_LABEL "LSFDE"
468 #define FDE_AFTER_SIZE_LABEL "LASFDE"
469 #define FDE_END_LABEL "LEFDE"
470 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
471 #define LINE_NUMBER_END_LABEL "LELT"
472 #define LN_PROLOG_AS_LABEL "LASLTP"
473 #define LN_PROLOG_END_LABEL "LELTP"
474 #define DIE_LABEL_PREFIX "DW"
476 /* The DWARF 2 CFA column which tracks the return address. Normally this
477 is the column for PC, or the first column after all of the hard
478 registers. */
479 #ifndef DWARF_FRAME_RETURN_COLUMN
480 #ifdef PC_REGNUM
481 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGNUM (PC_REGNUM)
482 #else
483 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGISTERS
484 #endif
485 #endif
487 /* The mapping from gcc register number to DWARF 2 CFA column number. By
488 default, we just provide columns for all registers. */
489 #ifndef DWARF_FRAME_REGNUM
490 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
491 #endif
493 /* Hook used by __throw. */
496 expand_builtin_dwarf_sp_column (void)
498 unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
499 return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
502 /* Return a pointer to a copy of the section string name S with all
503 attributes stripped off, and an asterisk prepended (for assemble_name). */
505 static inline char *
506 stripattributes (const char *s)
508 char *stripped = XNEWVEC (char, strlen (s) + 2);
509 char *p = stripped;
511 *p++ = '*';
513 while (*s && *s != ',')
514 *p++ = *s++;
516 *p = '\0';
517 return stripped;
520 /* MEM is a memory reference for the register size table, each element of
521 which has mode MODE. Initialize column C as a return address column. */
523 static void
524 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
526 HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
527 HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
528 emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
531 /* Generate code to initialize the register size table. */
533 void
534 expand_builtin_init_dwarf_reg_sizes (tree address)
536 unsigned int i;
537 enum machine_mode mode = TYPE_MODE (char_type_node);
538 rtx addr = expand_normal (address);
539 rtx mem = gen_rtx_MEM (BLKmode, addr);
540 bool wrote_return_column = false;
542 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
544 int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
546 if (rnum < DWARF_FRAME_REGISTERS)
548 HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
549 enum machine_mode save_mode = reg_raw_mode[i];
550 HOST_WIDE_INT size;
552 if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
553 save_mode = choose_hard_reg_mode (i, 1, true);
554 if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
556 if (save_mode == VOIDmode)
557 continue;
558 wrote_return_column = true;
560 size = GET_MODE_SIZE (save_mode);
561 if (offset < 0)
562 continue;
564 emit_move_insn (adjust_address (mem, mode, offset),
565 gen_int_mode (size, mode));
569 if (!wrote_return_column)
570 init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
572 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
573 init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
574 #endif
576 targetm.init_dwarf_reg_sizes_extra (address);
579 /* Convert a DWARF call frame info. operation to its string name */
581 static const char *
582 dwarf_cfi_name (unsigned int cfi_opc)
584 switch (cfi_opc)
586 case DW_CFA_advance_loc:
587 return "DW_CFA_advance_loc";
588 case DW_CFA_offset:
589 return "DW_CFA_offset";
590 case DW_CFA_restore:
591 return "DW_CFA_restore";
592 case DW_CFA_nop:
593 return "DW_CFA_nop";
594 case DW_CFA_set_loc:
595 return "DW_CFA_set_loc";
596 case DW_CFA_advance_loc1:
597 return "DW_CFA_advance_loc1";
598 case DW_CFA_advance_loc2:
599 return "DW_CFA_advance_loc2";
600 case DW_CFA_advance_loc4:
601 return "DW_CFA_advance_loc4";
602 case DW_CFA_offset_extended:
603 return "DW_CFA_offset_extended";
604 case DW_CFA_restore_extended:
605 return "DW_CFA_restore_extended";
606 case DW_CFA_undefined:
607 return "DW_CFA_undefined";
608 case DW_CFA_same_value:
609 return "DW_CFA_same_value";
610 case DW_CFA_register:
611 return "DW_CFA_register";
612 case DW_CFA_remember_state:
613 return "DW_CFA_remember_state";
614 case DW_CFA_restore_state:
615 return "DW_CFA_restore_state";
616 case DW_CFA_def_cfa:
617 return "DW_CFA_def_cfa";
618 case DW_CFA_def_cfa_register:
619 return "DW_CFA_def_cfa_register";
620 case DW_CFA_def_cfa_offset:
621 return "DW_CFA_def_cfa_offset";
623 /* DWARF 3 */
624 case DW_CFA_def_cfa_expression:
625 return "DW_CFA_def_cfa_expression";
626 case DW_CFA_expression:
627 return "DW_CFA_expression";
628 case DW_CFA_offset_extended_sf:
629 return "DW_CFA_offset_extended_sf";
630 case DW_CFA_def_cfa_sf:
631 return "DW_CFA_def_cfa_sf";
632 case DW_CFA_def_cfa_offset_sf:
633 return "DW_CFA_def_cfa_offset_sf";
635 /* SGI/MIPS specific */
636 case DW_CFA_MIPS_advance_loc8:
637 return "DW_CFA_MIPS_advance_loc8";
639 /* GNU extensions */
640 case DW_CFA_GNU_window_save:
641 return "DW_CFA_GNU_window_save";
642 case DW_CFA_GNU_args_size:
643 return "DW_CFA_GNU_args_size";
644 case DW_CFA_GNU_negative_offset_extended:
645 return "DW_CFA_GNU_negative_offset_extended";
647 default:
648 return "DW_CFA_<unknown>";
652 /* Return a pointer to a newly allocated Call Frame Instruction. */
654 static inline dw_cfi_ref
655 new_cfi (void)
657 dw_cfi_ref cfi = GGC_NEW (dw_cfi_node);
659 cfi->dw_cfi_next = NULL;
660 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
661 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
663 return cfi;
666 /* Add a Call Frame Instruction to list of instructions. */
668 static inline void
669 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
671 dw_cfi_ref *p;
672 dw_fde_ref fde = current_fde ();
674 /* When DRAP is used, CFA is defined with an expression. Redefine
675 CFA may lead to a different CFA value. */
676 if (fde && fde->drap_reg != INVALID_REGNUM)
677 switch (cfi->dw_cfi_opc)
679 case DW_CFA_def_cfa_register:
680 case DW_CFA_def_cfa_offset:
681 case DW_CFA_def_cfa_offset_sf:
682 case DW_CFA_def_cfa:
683 case DW_CFA_def_cfa_sf:
684 gcc_unreachable ();
686 default:
687 break;
690 /* Find the end of the chain. */
691 for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
694 *p = cfi;
697 /* Generate a new label for the CFI info to refer to. */
699 char *
700 dwarf2out_cfi_label (void)
702 static char label[20];
704 if (dwarf2out_do_cfi_asm ())
706 /* In this case, we will be emitting the asm directive instead of
707 the label, so just return a placeholder to keep the rest of the
708 interfaces happy. */
709 strcpy (label, "<do not output>");
711 else
713 ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
714 ASM_OUTPUT_LABEL (asm_out_file, label);
717 return label;
720 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
721 or to the CIE if LABEL is NULL. */
723 static void
724 add_fde_cfi (const char *label, dw_cfi_ref cfi)
726 dw_cfi_ref *list_head = &cie_cfi_head;
728 if (dwarf2out_do_cfi_asm ())
730 if (label)
732 output_cfi_directive (cfi);
734 /* We still have to add the cfi to the list so that
735 lookup_cfa works later on. */
736 list_head = &current_fde ()->dw_fde_cfi;
738 /* ??? If this is a CFI for the CIE, we don't emit. This
739 assumes that the standard CIE contents that the assembler
740 uses matches the standard CIE contents that the compiler
741 uses. This is probably a bad assumption. I'm not quite
742 sure how to address this for now. */
744 else if (label)
746 dw_fde_ref fde = current_fde ();
748 gcc_assert (fde != NULL);
750 if (*label == 0)
751 label = dwarf2out_cfi_label ();
753 if (fde->dw_fde_current_label == NULL
754 || strcmp (label, fde->dw_fde_current_label) != 0)
756 dw_cfi_ref xcfi;
758 label = xstrdup (label);
760 /* Set the location counter to the new label. */
761 xcfi = new_cfi ();
762 /* If we have a current label, advance from there, otherwise
763 set the location directly using set_loc. */
764 xcfi->dw_cfi_opc = fde->dw_fde_current_label
765 ? DW_CFA_advance_loc4
766 : DW_CFA_set_loc;
767 xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
768 add_cfi (&fde->dw_fde_cfi, xcfi);
770 fde->dw_fde_current_label = label;
773 list_head = &fde->dw_fde_cfi;
776 add_cfi (list_head, cfi);
779 /* Subroutine of lookup_cfa. */
781 static void
782 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc)
784 switch (cfi->dw_cfi_opc)
786 case DW_CFA_def_cfa_offset:
787 case DW_CFA_def_cfa_offset_sf:
788 loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
789 break;
790 case DW_CFA_def_cfa_register:
791 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
792 break;
793 case DW_CFA_def_cfa:
794 case DW_CFA_def_cfa_sf:
795 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
796 loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
797 break;
798 case DW_CFA_def_cfa_expression:
799 get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
800 break;
801 default:
802 break;
806 /* Find the previous value for the CFA. */
808 static void
809 lookup_cfa (dw_cfa_location *loc)
811 dw_cfi_ref cfi;
812 dw_fde_ref fde;
814 loc->reg = INVALID_REGNUM;
815 loc->offset = 0;
816 loc->indirect = 0;
817 loc->base_offset = 0;
819 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
820 lookup_cfa_1 (cfi, loc);
822 fde = current_fde ();
823 if (fde)
824 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
825 lookup_cfa_1 (cfi, loc);
828 /* The current rule for calculating the DWARF2 canonical frame address. */
829 static dw_cfa_location cfa;
831 /* The register used for saving registers to the stack, and its offset
832 from the CFA. */
833 static dw_cfa_location cfa_store;
835 /* The running total of the size of arguments pushed onto the stack. */
836 static HOST_WIDE_INT args_size;
838 /* The last args_size we actually output. */
839 static HOST_WIDE_INT old_args_size;
841 /* Entry point to update the canonical frame address (CFA).
842 LABEL is passed to add_fde_cfi. The value of CFA is now to be
843 calculated from REG+OFFSET. */
845 void
846 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
848 dw_cfa_location loc;
849 loc.indirect = 0;
850 loc.base_offset = 0;
851 loc.reg = reg;
852 loc.offset = offset;
853 def_cfa_1 (label, &loc);
856 /* Determine if two dw_cfa_location structures define the same data. */
858 static bool
859 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
861 return (loc1->reg == loc2->reg
862 && loc1->offset == loc2->offset
863 && loc1->indirect == loc2->indirect
864 && (loc1->indirect == 0
865 || loc1->base_offset == loc2->base_offset));
868 /* This routine does the actual work. The CFA is now calculated from
869 the dw_cfa_location structure. */
871 static void
872 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
874 dw_cfi_ref cfi;
875 dw_cfa_location old_cfa, loc;
877 cfa = *loc_p;
878 loc = *loc_p;
880 if (cfa_store.reg == loc.reg && loc.indirect == 0)
881 cfa_store.offset = loc.offset;
883 loc.reg = DWARF_FRAME_REGNUM (loc.reg);
884 lookup_cfa (&old_cfa);
886 /* If nothing changed, no need to issue any call frame instructions. */
887 if (cfa_equal_p (&loc, &old_cfa))
888 return;
890 cfi = new_cfi ();
892 if (loc.reg == old_cfa.reg && !loc.indirect)
894 /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
895 the CFA register did not change but the offset did. The data
896 factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
897 in the assembler via the .cfi_def_cfa_offset directive. */
898 if (loc.offset < 0)
899 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
900 else
901 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
902 cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
905 #ifndef MIPS_DEBUGGING_INFO /* SGI dbx thinks this means no offset. */
906 else if (loc.offset == old_cfa.offset
907 && old_cfa.reg != INVALID_REGNUM
908 && !loc.indirect)
910 /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
911 indicating the CFA register has changed to <register> but the
912 offset has not changed. */
913 cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
914 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
916 #endif
918 else if (loc.indirect == 0)
920 /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
921 indicating the CFA register has changed to <register> with
922 the specified offset. The data factoring for DW_CFA_def_cfa_sf
923 happens in output_cfi, or in the assembler via the .cfi_def_cfa
924 directive. */
925 if (loc.offset < 0)
926 cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
927 else
928 cfi->dw_cfi_opc = DW_CFA_def_cfa;
929 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
930 cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
932 else
934 /* Construct a DW_CFA_def_cfa_expression instruction to
935 calculate the CFA using a full location expression since no
936 register-offset pair is available. */
937 struct dw_loc_descr_struct *loc_list;
939 cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
940 loc_list = build_cfa_loc (&loc, 0);
941 cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
944 add_fde_cfi (label, cfi);
947 /* Add the CFI for saving a register. REG is the CFA column number.
948 LABEL is passed to add_fde_cfi.
949 If SREG is -1, the register is saved at OFFSET from the CFA;
950 otherwise it is saved in SREG. */
952 static void
953 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
955 dw_cfi_ref cfi = new_cfi ();
956 dw_fde_ref fde = current_fde ();
958 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
960 /* When stack is aligned, store REG using DW_CFA_expression with
961 FP. */
962 if (fde
963 && fde->stack_realign
964 && sreg == INVALID_REGNUM)
966 cfi->dw_cfi_opc = DW_CFA_expression;
967 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = reg;
968 cfi->dw_cfi_oprnd1.dw_cfi_loc
969 = build_cfa_aligned_loc (offset, fde->stack_realignment);
971 else if (sreg == INVALID_REGNUM)
973 if (offset < 0)
974 cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
975 else if (reg & ~0x3f)
976 cfi->dw_cfi_opc = DW_CFA_offset_extended;
977 else
978 cfi->dw_cfi_opc = DW_CFA_offset;
979 cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
981 else if (sreg == reg)
982 cfi->dw_cfi_opc = DW_CFA_same_value;
983 else
985 cfi->dw_cfi_opc = DW_CFA_register;
986 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
989 add_fde_cfi (label, cfi);
992 /* Add the CFI for saving a register window. LABEL is passed to reg_save.
993 This CFI tells the unwinder that it needs to restore the window registers
994 from the previous frame's window save area.
996 ??? Perhaps we should note in the CIE where windows are saved (instead of
997 assuming 0(cfa)) and what registers are in the window. */
999 void
1000 dwarf2out_window_save (const char *label)
1002 dw_cfi_ref cfi = new_cfi ();
1004 cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1005 add_fde_cfi (label, cfi);
1008 /* Add a CFI to update the running total of the size of arguments
1009 pushed onto the stack. */
1011 void
1012 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1014 dw_cfi_ref cfi;
1016 if (size == old_args_size)
1017 return;
1019 old_args_size = size;
1021 cfi = new_cfi ();
1022 cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1023 cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1024 add_fde_cfi (label, cfi);
1027 /* Entry point for saving a register to the stack. REG is the GCC register
1028 number. LABEL and OFFSET are passed to reg_save. */
1030 void
1031 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1033 reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1036 /* Entry point for saving the return address in the stack.
1037 LABEL and OFFSET are passed to reg_save. */
1039 void
1040 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1042 reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1045 /* Entry point for saving the return address in a register.
1046 LABEL and SREG are passed to reg_save. */
1048 void
1049 dwarf2out_return_reg (const char *label, unsigned int sreg)
1051 reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1054 #ifdef DWARF2_UNWIND_INFO
1055 /* Record the initial position of the return address. RTL is
1056 INCOMING_RETURN_ADDR_RTX. */
1058 static void
1059 initial_return_save (rtx rtl)
1061 unsigned int reg = INVALID_REGNUM;
1062 HOST_WIDE_INT offset = 0;
1064 switch (GET_CODE (rtl))
1066 case REG:
1067 /* RA is in a register. */
1068 reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1069 break;
1071 case MEM:
1072 /* RA is on the stack. */
1073 rtl = XEXP (rtl, 0);
1074 switch (GET_CODE (rtl))
1076 case REG:
1077 gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1078 offset = 0;
1079 break;
1081 case PLUS:
1082 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1083 offset = INTVAL (XEXP (rtl, 1));
1084 break;
1086 case MINUS:
1087 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1088 offset = -INTVAL (XEXP (rtl, 1));
1089 break;
1091 default:
1092 gcc_unreachable ();
1095 break;
1097 case PLUS:
1098 /* The return address is at some offset from any value we can
1099 actually load. For instance, on the SPARC it is in %i7+8. Just
1100 ignore the offset for now; it doesn't matter for unwinding frames. */
1101 gcc_assert (GET_CODE (XEXP (rtl, 1)) == CONST_INT);
1102 initial_return_save (XEXP (rtl, 0));
1103 return;
1105 default:
1106 gcc_unreachable ();
1109 if (reg != DWARF_FRAME_RETURN_COLUMN)
1110 reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1112 #endif
1114 /* Given a SET, calculate the amount of stack adjustment it
1115 contains. */
1117 static HOST_WIDE_INT
1118 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1119 HOST_WIDE_INT cur_offset)
1121 const_rtx src = SET_SRC (pattern);
1122 const_rtx dest = SET_DEST (pattern);
1123 HOST_WIDE_INT offset = 0;
1124 enum rtx_code code;
1126 if (dest == stack_pointer_rtx)
1128 code = GET_CODE (src);
1130 /* Assume (set (reg sp) (reg whatever)) sets args_size
1131 level to 0. */
1132 if (code == REG && src != stack_pointer_rtx)
1134 offset = -cur_args_size;
1135 #ifndef STACK_GROWS_DOWNWARD
1136 offset = -offset;
1137 #endif
1138 return offset - cur_offset;
1141 if (! (code == PLUS || code == MINUS)
1142 || XEXP (src, 0) != stack_pointer_rtx
1143 || GET_CODE (XEXP (src, 1)) != CONST_INT)
1144 return 0;
1146 /* (set (reg sp) (plus (reg sp) (const_int))) */
1147 offset = INTVAL (XEXP (src, 1));
1148 if (code == PLUS)
1149 offset = -offset;
1150 return offset;
1153 if (MEM_P (src) && !MEM_P (dest))
1154 dest = src;
1155 if (MEM_P (dest))
1157 /* (set (mem (pre_dec (reg sp))) (foo)) */
1158 src = XEXP (dest, 0);
1159 code = GET_CODE (src);
1161 switch (code)
1163 case PRE_MODIFY:
1164 case POST_MODIFY:
1165 if (XEXP (src, 0) == stack_pointer_rtx)
1167 rtx val = XEXP (XEXP (src, 1), 1);
1168 /* We handle only adjustments by constant amount. */
1169 gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1170 && GET_CODE (val) == CONST_INT);
1171 offset = -INTVAL (val);
1172 break;
1174 return 0;
1176 case PRE_DEC:
1177 case POST_DEC:
1178 if (XEXP (src, 0) == stack_pointer_rtx)
1180 offset = GET_MODE_SIZE (GET_MODE (dest));
1181 break;
1183 return 0;
1185 case PRE_INC:
1186 case POST_INC:
1187 if (XEXP (src, 0) == stack_pointer_rtx)
1189 offset = -GET_MODE_SIZE (GET_MODE (dest));
1190 break;
1192 return 0;
1194 default:
1195 return 0;
1198 else
1199 return 0;
1201 return offset;
1204 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1205 indexed by INSN_UID. */
1207 static HOST_WIDE_INT *barrier_args_size;
1209 /* Helper function for compute_barrier_args_size. Handle one insn. */
1211 static HOST_WIDE_INT
1212 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1213 VEC (rtx, heap) **next)
1215 HOST_WIDE_INT offset = 0;
1216 int i;
1218 if (! RTX_FRAME_RELATED_P (insn))
1220 if (prologue_epilogue_contains (insn)
1221 || sibcall_epilogue_contains (insn))
1222 /* Nothing */;
1223 else if (GET_CODE (PATTERN (insn)) == SET)
1224 offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1225 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1226 || GET_CODE (PATTERN (insn)) == SEQUENCE)
1228 /* There may be stack adjustments inside compound insns. Search
1229 for them. */
1230 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1231 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1232 offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1233 cur_args_size, offset);
1236 else
1238 rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1240 if (expr)
1242 expr = XEXP (expr, 0);
1243 if (GET_CODE (expr) == PARALLEL
1244 || GET_CODE (expr) == SEQUENCE)
1245 for (i = 1; i < XVECLEN (expr, 0); i++)
1247 rtx elem = XVECEXP (expr, 0, i);
1249 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1250 offset += stack_adjust_offset (elem, cur_args_size, offset);
1255 #ifndef STACK_GROWS_DOWNWARD
1256 offset = -offset;
1257 #endif
1259 cur_args_size += offset;
1260 if (cur_args_size < 0)
1261 cur_args_size = 0;
1263 if (JUMP_P (insn))
1265 rtx dest = JUMP_LABEL (insn);
1267 if (dest)
1269 if (barrier_args_size [INSN_UID (dest)] < 0)
1271 barrier_args_size [INSN_UID (dest)] = cur_args_size;
1272 VEC_safe_push (rtx, heap, *next, dest);
1277 return cur_args_size;
1280 /* Walk the whole function and compute args_size on BARRIERs. */
1282 static void
1283 compute_barrier_args_size (void)
1285 int max_uid = get_max_uid (), i;
1286 rtx insn;
1287 VEC (rtx, heap) *worklist, *next, *tmp;
1289 barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1290 for (i = 0; i < max_uid; i++)
1291 barrier_args_size[i] = -1;
1293 worklist = VEC_alloc (rtx, heap, 20);
1294 next = VEC_alloc (rtx, heap, 20);
1295 insn = get_insns ();
1296 barrier_args_size[INSN_UID (insn)] = 0;
1297 VEC_quick_push (rtx, worklist, insn);
1298 for (;;)
1300 while (!VEC_empty (rtx, worklist))
1302 rtx prev, body, first_insn;
1303 HOST_WIDE_INT cur_args_size;
1305 first_insn = insn = VEC_pop (rtx, worklist);
1306 cur_args_size = barrier_args_size[INSN_UID (insn)];
1307 prev = prev_nonnote_insn (insn);
1308 if (prev && BARRIER_P (prev))
1309 barrier_args_size[INSN_UID (prev)] = cur_args_size;
1311 for (; insn; insn = NEXT_INSN (insn))
1313 if (INSN_DELETED_P (insn) || NOTE_P (insn))
1314 continue;
1315 if (BARRIER_P (insn))
1316 break;
1318 if (LABEL_P (insn))
1320 if (insn == first_insn)
1321 continue;
1322 else if (barrier_args_size[INSN_UID (insn)] < 0)
1324 barrier_args_size[INSN_UID (insn)] = cur_args_size;
1325 continue;
1327 else
1329 /* The insns starting with this label have been
1330 already scanned or are in the worklist. */
1331 break;
1335 body = PATTERN (insn);
1336 if (GET_CODE (body) == SEQUENCE)
1338 HOST_WIDE_INT dest_args_size = cur_args_size;
1339 for (i = 1; i < XVECLEN (body, 0); i++)
1340 if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1341 && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1342 dest_args_size
1343 = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1344 dest_args_size, &next);
1345 else
1346 cur_args_size
1347 = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1348 cur_args_size, &next);
1350 if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1351 compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1352 dest_args_size, &next);
1353 else
1354 cur_args_size
1355 = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1356 cur_args_size, &next);
1358 else
1359 cur_args_size
1360 = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1364 if (VEC_empty (rtx, next))
1365 break;
1367 /* Swap WORKLIST with NEXT and truncate NEXT for next iteration. */
1368 tmp = next;
1369 next = worklist;
1370 worklist = tmp;
1371 VEC_truncate (rtx, next, 0);
1374 VEC_free (rtx, heap, worklist);
1375 VEC_free (rtx, heap, next);
1379 /* Check INSN to see if it looks like a push or a stack adjustment, and
1380 make a note of it if it does. EH uses this information to find out how
1381 much extra space it needs to pop off the stack. */
1383 static void
1384 dwarf2out_stack_adjust (rtx insn, bool after_p)
1386 HOST_WIDE_INT offset;
1387 const char *label;
1388 int i;
1390 /* Don't handle epilogues at all. Certainly it would be wrong to do so
1391 with this function. Proper support would require all frame-related
1392 insns to be marked, and to be able to handle saving state around
1393 epilogues textually in the middle of the function. */
1394 if (prologue_epilogue_contains (insn) || sibcall_epilogue_contains (insn))
1395 return;
1397 /* If INSN is an instruction from target of an annulled branch, the
1398 effects are for the target only and so current argument size
1399 shouldn't change at all. */
1400 if (final_sequence
1401 && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1402 && INSN_FROM_TARGET_P (insn))
1403 return;
1405 /* If only calls can throw, and we have a frame pointer,
1406 save up adjustments until we see the CALL_INSN. */
1407 if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1409 if (CALL_P (insn) && !after_p)
1411 /* Extract the size of the args from the CALL rtx itself. */
1412 insn = PATTERN (insn);
1413 if (GET_CODE (insn) == PARALLEL)
1414 insn = XVECEXP (insn, 0, 0);
1415 if (GET_CODE (insn) == SET)
1416 insn = SET_SRC (insn);
1417 gcc_assert (GET_CODE (insn) == CALL);
1418 dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1420 return;
1423 if (CALL_P (insn) && !after_p)
1425 if (!flag_asynchronous_unwind_tables)
1426 dwarf2out_args_size ("", args_size);
1427 return;
1429 else if (BARRIER_P (insn))
1431 /* Don't call compute_barrier_args_size () if the only
1432 BARRIER is at the end of function. */
1433 if (barrier_args_size == NULL && next_nonnote_insn (insn))
1434 compute_barrier_args_size ();
1435 if (barrier_args_size == NULL)
1436 offset = 0;
1437 else
1439 offset = barrier_args_size[INSN_UID (insn)];
1440 if (offset < 0)
1441 offset = 0;
1444 offset -= args_size;
1445 #ifndef STACK_GROWS_DOWNWARD
1446 offset = -offset;
1447 #endif
1449 else if (GET_CODE (PATTERN (insn)) == SET)
1450 offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1451 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1452 || GET_CODE (PATTERN (insn)) == SEQUENCE)
1454 /* There may be stack adjustments inside compound insns. Search
1455 for them. */
1456 for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1457 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1458 offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1459 args_size, offset);
1461 else
1462 return;
1464 if (offset == 0)
1465 return;
1467 label = dwarf2out_cfi_label ();
1468 dwarf2out_args_size_adjust (offset, label);
1471 /* Adjust args_size based on stack adjustment OFFSET. */
1473 static void
1474 dwarf2out_args_size_adjust (HOST_WIDE_INT offset, const char *label)
1476 if (cfa.reg == STACK_POINTER_REGNUM)
1477 cfa.offset += offset;
1479 if (cfa_store.reg == STACK_POINTER_REGNUM)
1480 cfa_store.offset += offset;
1482 #ifndef STACK_GROWS_DOWNWARD
1483 offset = -offset;
1484 #endif
1486 args_size += offset;
1487 if (args_size < 0)
1488 args_size = 0;
1490 def_cfa_1 (label, &cfa);
1491 if (flag_asynchronous_unwind_tables)
1492 dwarf2out_args_size (label, args_size);
1495 #endif
1497 /* We delay emitting a register save until either (a) we reach the end
1498 of the prologue or (b) the register is clobbered. This clusters
1499 register saves so that there are fewer pc advances. */
1501 struct queued_reg_save GTY(())
1503 struct queued_reg_save *next;
1504 rtx reg;
1505 HOST_WIDE_INT cfa_offset;
1506 rtx saved_reg;
1509 static GTY(()) struct queued_reg_save *queued_reg_saves;
1511 /* The caller's ORIG_REG is saved in SAVED_IN_REG. */
1512 struct reg_saved_in_data GTY(()) {
1513 rtx orig_reg;
1514 rtx saved_in_reg;
1517 /* A list of registers saved in other registers.
1518 The list intentionally has a small maximum capacity of 4; if your
1519 port needs more than that, you might consider implementing a
1520 more efficient data structure. */
1521 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1522 static GTY(()) size_t num_regs_saved_in_regs;
1524 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1525 static const char *last_reg_save_label;
1527 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1528 SREG, or if SREG is NULL then it is saved at OFFSET to the CFA. */
1530 static void
1531 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1533 struct queued_reg_save *q;
1535 /* Duplicates waste space, but it's also necessary to remove them
1536 for correctness, since the queue gets output in reverse
1537 order. */
1538 for (q = queued_reg_saves; q != NULL; q = q->next)
1539 if (REGNO (q->reg) == REGNO (reg))
1540 break;
1542 if (q == NULL)
1544 q = GGC_NEW (struct queued_reg_save);
1545 q->next = queued_reg_saves;
1546 queued_reg_saves = q;
1549 q->reg = reg;
1550 q->cfa_offset = offset;
1551 q->saved_reg = sreg;
1553 last_reg_save_label = label;
1556 /* Output all the entries in QUEUED_REG_SAVES. */
1558 static void
1559 flush_queued_reg_saves (void)
1561 struct queued_reg_save *q;
1563 for (q = queued_reg_saves; q; q = q->next)
1565 size_t i;
1566 unsigned int reg, sreg;
1568 for (i = 0; i < num_regs_saved_in_regs; i++)
1569 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1570 break;
1571 if (q->saved_reg && i == num_regs_saved_in_regs)
1573 gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1574 num_regs_saved_in_regs++;
1576 if (i != num_regs_saved_in_regs)
1578 regs_saved_in_regs[i].orig_reg = q->reg;
1579 regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1582 reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1583 if (q->saved_reg)
1584 sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1585 else
1586 sreg = INVALID_REGNUM;
1587 reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1590 queued_reg_saves = NULL;
1591 last_reg_save_label = NULL;
1594 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1595 location for? Or, does it clobber a register which we've previously
1596 said that some other register is saved in, and for which we now
1597 have a new location for? */
1599 static bool
1600 clobbers_queued_reg_save (const_rtx insn)
1602 struct queued_reg_save *q;
1604 for (q = queued_reg_saves; q; q = q->next)
1606 size_t i;
1607 if (modified_in_p (q->reg, insn))
1608 return true;
1609 for (i = 0; i < num_regs_saved_in_regs; i++)
1610 if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1611 && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1612 return true;
1615 return false;
1618 /* Entry point for saving the first register into the second. */
1620 void
1621 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1623 size_t i;
1624 unsigned int regno, sregno;
1626 for (i = 0; i < num_regs_saved_in_regs; i++)
1627 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1628 break;
1629 if (i == num_regs_saved_in_regs)
1631 gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1632 num_regs_saved_in_regs++;
1634 regs_saved_in_regs[i].orig_reg = reg;
1635 regs_saved_in_regs[i].saved_in_reg = sreg;
1637 regno = DWARF_FRAME_REGNUM (REGNO (reg));
1638 sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1639 reg_save (label, regno, sregno, 0);
1642 /* What register, if any, is currently saved in REG? */
1644 static rtx
1645 reg_saved_in (rtx reg)
1647 unsigned int regn = REGNO (reg);
1648 size_t i;
1649 struct queued_reg_save *q;
1651 for (q = queued_reg_saves; q; q = q->next)
1652 if (q->saved_reg && regn == REGNO (q->saved_reg))
1653 return q->reg;
1655 for (i = 0; i < num_regs_saved_in_regs; i++)
1656 if (regs_saved_in_regs[i].saved_in_reg
1657 && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1658 return regs_saved_in_regs[i].orig_reg;
1660 return NULL_RTX;
1664 /* A temporary register holding an integral value used in adjusting SP
1665 or setting up the store_reg. The "offset" field holds the integer
1666 value, not an offset. */
1667 static dw_cfa_location cfa_temp;
1669 /* Record call frame debugging information for an expression EXPR,
1670 which either sets SP or FP (adjusting how we calculate the frame
1671 address) or saves a register to the stack or another register.
1672 LABEL indicates the address of EXPR.
1674 This function encodes a state machine mapping rtxes to actions on
1675 cfa, cfa_store, and cfa_temp.reg. We describe these rules so
1676 users need not read the source code.
1678 The High-Level Picture
1680 Changes in the register we use to calculate the CFA: Currently we
1681 assume that if you copy the CFA register into another register, we
1682 should take the other one as the new CFA register; this seems to
1683 work pretty well. If it's wrong for some target, it's simple
1684 enough not to set RTX_FRAME_RELATED_P on the insn in question.
1686 Changes in the register we use for saving registers to the stack:
1687 This is usually SP, but not always. Again, we deduce that if you
1688 copy SP into another register (and SP is not the CFA register),
1689 then the new register is the one we will be using for register
1690 saves. This also seems to work.
1692 Register saves: There's not much guesswork about this one; if
1693 RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1694 register save, and the register used to calculate the destination
1695 had better be the one we think we're using for this purpose.
1696 It's also assumed that a copy from a call-saved register to another
1697 register is saving that register if RTX_FRAME_RELATED_P is set on
1698 that instruction. If the copy is from a call-saved register to
1699 the *same* register, that means that the register is now the same
1700 value as in the caller.
1702 Except: If the register being saved is the CFA register, and the
1703 offset is nonzero, we are saving the CFA, so we assume we have to
1704 use DW_CFA_def_cfa_expression. If the offset is 0, we assume that
1705 the intent is to save the value of SP from the previous frame.
1707 In addition, if a register has previously been saved to a different
1708 register,
1710 Invariants / Summaries of Rules
1712 cfa current rule for calculating the CFA. It usually
1713 consists of a register and an offset.
1714 cfa_store register used by prologue code to save things to the stack
1715 cfa_store.offset is the offset from the value of
1716 cfa_store.reg to the actual CFA
1717 cfa_temp register holding an integral value. cfa_temp.offset
1718 stores the value, which will be used to adjust the
1719 stack pointer. cfa_temp is also used like cfa_store,
1720 to track stores to the stack via fp or a temp reg.
1722 Rules 1- 4: Setting a register's value to cfa.reg or an expression
1723 with cfa.reg as the first operand changes the cfa.reg and its
1724 cfa.offset. Rule 1 and 4 also set cfa_temp.reg and
1725 cfa_temp.offset.
1727 Rules 6- 9: Set a non-cfa.reg register value to a constant or an
1728 expression yielding a constant. This sets cfa_temp.reg
1729 and cfa_temp.offset.
1731 Rule 5: Create a new register cfa_store used to save items to the
1732 stack.
1734 Rules 10-14: Save a register to the stack. Define offset as the
1735 difference of the original location and cfa_store's
1736 location (or cfa_temp's location if cfa_temp is used).
1738 Rules 16-20: If AND operation happens on sp in prologue, we assume
1739 stack is realigned. We will use a group of DW_OP_XXX
1740 expressions to represent the location of the stored
1741 register instead of CFA+offset.
1743 The Rules
1745 "{a,b}" indicates a choice of a xor b.
1746 "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1748 Rule 1:
1749 (set <reg1> <reg2>:cfa.reg)
1750 effects: cfa.reg = <reg1>
1751 cfa.offset unchanged
1752 cfa_temp.reg = <reg1>
1753 cfa_temp.offset = cfa.offset
1755 Rule 2:
1756 (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1757 {<const_int>,<reg>:cfa_temp.reg}))
1758 effects: cfa.reg = sp if fp used
1759 cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
1760 cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1761 if cfa_store.reg==sp
1763 Rule 3:
1764 (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
1765 effects: cfa.reg = fp
1766 cfa_offset += +/- <const_int>
1768 Rule 4:
1769 (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
1770 constraints: <reg1> != fp
1771 <reg1> != sp
1772 effects: cfa.reg = <reg1>
1773 cfa_temp.reg = <reg1>
1774 cfa_temp.offset = cfa.offset
1776 Rule 5:
1777 (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1778 constraints: <reg1> != fp
1779 <reg1> != sp
1780 effects: cfa_store.reg = <reg1>
1781 cfa_store.offset = cfa.offset - cfa_temp.offset
1783 Rule 6:
1784 (set <reg> <const_int>)
1785 effects: cfa_temp.reg = <reg>
1786 cfa_temp.offset = <const_int>
1788 Rule 7:
1789 (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1790 effects: cfa_temp.reg = <reg1>
1791 cfa_temp.offset |= <const_int>
1793 Rule 8:
1794 (set <reg> (high <exp>))
1795 effects: none
1797 Rule 9:
1798 (set <reg> (lo_sum <exp> <const_int>))
1799 effects: cfa_temp.reg = <reg>
1800 cfa_temp.offset = <const_int>
1802 Rule 10:
1803 (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1804 effects: cfa_store.offset -= <const_int>
1805 cfa.offset = cfa_store.offset if cfa.reg == sp
1806 cfa.reg = sp
1807 cfa.base_offset = -cfa_store.offset
1809 Rule 11:
1810 (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1811 effects: cfa_store.offset += -/+ mode_size(mem)
1812 cfa.offset = cfa_store.offset if cfa.reg == sp
1813 cfa.reg = sp
1814 cfa.base_offset = -cfa_store.offset
1816 Rule 12:
1817 (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1819 <reg2>)
1820 effects: cfa.reg = <reg1>
1821 cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
1823 Rule 13:
1824 (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1825 effects: cfa.reg = <reg1>
1826 cfa.base_offset = -{cfa_store,cfa_temp}.offset
1828 Rule 14:
1829 (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
1830 effects: cfa.reg = <reg1>
1831 cfa.base_offset = -cfa_temp.offset
1832 cfa_temp.offset -= mode_size(mem)
1834 Rule 15:
1835 (set <reg> {unspec, unspec_volatile})
1836 effects: target-dependent
1838 Rule 16:
1839 (set sp (and: sp <const_int>))
1840 constraints: cfa_store.reg == sp
1841 effects: current_fde.stack_realign = 1
1842 cfa_store.offset = 0
1843 fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
1845 Rule 17:
1846 (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
1847 effects: cfa_store.offset += -/+ mode_size(mem)
1849 Rule 18:
1850 (set (mem ({pre_inc, pre_dec} sp)) fp)
1851 constraints: fde->stack_realign == 1
1852 effects: cfa_store.offset = 0
1853 cfa.reg != HARD_FRAME_POINTER_REGNUM
1855 Rule 19:
1856 (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
1857 constraints: fde->stack_realign == 1
1858 && cfa.offset == 0
1859 && cfa.indirect == 0
1860 && cfa.reg != HARD_FRAME_POINTER_REGNUM
1861 effects: Use DW_CFA_def_cfa_expression to define cfa
1862 cfa.reg == fde->drap_reg
1864 Rule 20:
1865 (set reg fde->drap_reg)
1866 constraints: fde->vdrap_reg == INVALID_REGNUM
1867 effects: fde->vdrap_reg = reg.
1868 (set mem fde->drap_reg)
1869 constraints: fde->drap_reg_saved == 1
1870 effects: none. */
1872 static void
1873 dwarf2out_frame_debug_expr (rtx expr, const char *label)
1875 rtx src, dest, span;
1876 HOST_WIDE_INT offset;
1877 dw_fde_ref fde;
1879 /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1880 the PARALLEL independently. The first element is always processed if
1881 it is a SET. This is for backward compatibility. Other elements
1882 are processed only if they are SETs and the RTX_FRAME_RELATED_P
1883 flag is set in them. */
1884 if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
1886 int par_index;
1887 int limit = XVECLEN (expr, 0);
1888 rtx elem;
1890 /* PARALLELs have strict read-modify-write semantics, so we
1891 ought to evaluate every rvalue before changing any lvalue.
1892 It's cumbersome to do that in general, but there's an
1893 easy approximation that is enough for all current users:
1894 handle register saves before register assignments. */
1895 if (GET_CODE (expr) == PARALLEL)
1896 for (par_index = 0; par_index < limit; par_index++)
1898 elem = XVECEXP (expr, 0, par_index);
1899 if (GET_CODE (elem) == SET
1900 && MEM_P (SET_DEST (elem))
1901 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1902 dwarf2out_frame_debug_expr (elem, label);
1905 for (par_index = 0; par_index < limit; par_index++)
1907 elem = XVECEXP (expr, 0, par_index);
1908 if (GET_CODE (elem) == SET
1909 && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
1910 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1911 dwarf2out_frame_debug_expr (elem, label);
1912 else if (GET_CODE (elem) == SET
1913 && par_index != 0
1914 && !RTX_FRAME_RELATED_P (elem))
1916 /* Stack adjustment combining might combine some post-prologue
1917 stack adjustment into a prologue stack adjustment. */
1918 HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
1920 if (offset != 0)
1921 dwarf2out_args_size_adjust (offset, label);
1924 return;
1927 gcc_assert (GET_CODE (expr) == SET);
1929 src = SET_SRC (expr);
1930 dest = SET_DEST (expr);
1932 if (REG_P (src))
1934 rtx rsi = reg_saved_in (src);
1935 if (rsi)
1936 src = rsi;
1939 fde = current_fde ();
1941 if (GET_CODE (src) == REG
1942 && fde
1943 && fde->drap_reg == REGNO (src)
1944 && (fde->drap_reg_saved
1945 || GET_CODE (dest) == REG))
1947 /* Rule 20 */
1948 /* If we are saving dynamic realign argument pointer to a
1949 register, the destination is virtual dynamic realign
1950 argument pointer. It may be used to access argument. */
1951 if (GET_CODE (dest) == REG)
1953 gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
1954 fde->vdrap_reg = REGNO (dest);
1956 return;
1959 switch (GET_CODE (dest))
1961 case REG:
1962 switch (GET_CODE (src))
1964 /* Setting FP from SP. */
1965 case REG:
1966 if (cfa.reg == (unsigned) REGNO (src))
1968 /* Rule 1 */
1969 /* Update the CFA rule wrt SP or FP. Make sure src is
1970 relative to the current CFA register.
1972 We used to require that dest be either SP or FP, but the
1973 ARM copies SP to a temporary register, and from there to
1974 FP. So we just rely on the backends to only set
1975 RTX_FRAME_RELATED_P on appropriate insns. */
1976 cfa.reg = REGNO (dest);
1977 cfa_temp.reg = cfa.reg;
1978 cfa_temp.offset = cfa.offset;
1980 else
1982 /* Saving a register in a register. */
1983 gcc_assert (!fixed_regs [REGNO (dest)]
1984 /* For the SPARC and its register window. */
1985 || (DWARF_FRAME_REGNUM (REGNO (src))
1986 == DWARF_FRAME_RETURN_COLUMN));
1988 /* After stack is aligned, we can only save SP in FP
1989 if drap register is used. In this case, we have
1990 to restore stack pointer with the CFA value and we
1991 don't generate this DWARF information. */
1992 if (fde
1993 && fde->stack_realign
1994 && REGNO (src) == STACK_POINTER_REGNUM)
1995 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
1996 && fde->drap_reg != INVALID_REGNUM
1997 && cfa.reg != REGNO (src));
1998 else
1999 queue_reg_save (label, src, dest, 0);
2001 break;
2003 case PLUS:
2004 case MINUS:
2005 case LO_SUM:
2006 if (dest == stack_pointer_rtx)
2008 /* Rule 2 */
2009 /* Adjusting SP. */
2010 switch (GET_CODE (XEXP (src, 1)))
2012 case CONST_INT:
2013 offset = INTVAL (XEXP (src, 1));
2014 break;
2015 case REG:
2016 gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2017 == cfa_temp.reg);
2018 offset = cfa_temp.offset;
2019 break;
2020 default:
2021 gcc_unreachable ();
2024 if (XEXP (src, 0) == hard_frame_pointer_rtx)
2026 /* Restoring SP from FP in the epilogue. */
2027 gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2028 cfa.reg = STACK_POINTER_REGNUM;
2030 else if (GET_CODE (src) == LO_SUM)
2031 /* Assume we've set the source reg of the LO_SUM from sp. */
2033 else
2034 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2036 if (GET_CODE (src) != MINUS)
2037 offset = -offset;
2038 if (cfa.reg == STACK_POINTER_REGNUM)
2039 cfa.offset += offset;
2040 if (cfa_store.reg == STACK_POINTER_REGNUM)
2041 cfa_store.offset += offset;
2043 else if (dest == hard_frame_pointer_rtx)
2045 /* Rule 3 */
2046 /* Either setting the FP from an offset of the SP,
2047 or adjusting the FP */
2048 gcc_assert (frame_pointer_needed);
2050 gcc_assert (REG_P (XEXP (src, 0))
2051 && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2052 && GET_CODE (XEXP (src, 1)) == CONST_INT);
2053 offset = INTVAL (XEXP (src, 1));
2054 if (GET_CODE (src) != MINUS)
2055 offset = -offset;
2056 cfa.offset += offset;
2057 cfa.reg = HARD_FRAME_POINTER_REGNUM;
2059 else
2061 gcc_assert (GET_CODE (src) != MINUS);
2063 /* Rule 4 */
2064 if (REG_P (XEXP (src, 0))
2065 && REGNO (XEXP (src, 0)) == cfa.reg
2066 && GET_CODE (XEXP (src, 1)) == CONST_INT)
2068 /* Setting a temporary CFA register that will be copied
2069 into the FP later on. */
2070 offset = - INTVAL (XEXP (src, 1));
2071 cfa.offset += offset;
2072 cfa.reg = REGNO (dest);
2073 /* Or used to save regs to the stack. */
2074 cfa_temp.reg = cfa.reg;
2075 cfa_temp.offset = cfa.offset;
2078 /* Rule 5 */
2079 else if (REG_P (XEXP (src, 0))
2080 && REGNO (XEXP (src, 0)) == cfa_temp.reg
2081 && XEXP (src, 1) == stack_pointer_rtx)
2083 /* Setting a scratch register that we will use instead
2084 of SP for saving registers to the stack. */
2085 gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2086 cfa_store.reg = REGNO (dest);
2087 cfa_store.offset = cfa.offset - cfa_temp.offset;
2090 /* Rule 9 */
2091 else if (GET_CODE (src) == LO_SUM
2092 && GET_CODE (XEXP (src, 1)) == CONST_INT)
2094 cfa_temp.reg = REGNO (dest);
2095 cfa_temp.offset = INTVAL (XEXP (src, 1));
2097 else
2098 gcc_unreachable ();
2100 break;
2102 /* Rule 6 */
2103 case CONST_INT:
2104 cfa_temp.reg = REGNO (dest);
2105 cfa_temp.offset = INTVAL (src);
2106 break;
2108 /* Rule 7 */
2109 case IOR:
2110 gcc_assert (REG_P (XEXP (src, 0))
2111 && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2112 && GET_CODE (XEXP (src, 1)) == CONST_INT);
2114 if ((unsigned) REGNO (dest) != cfa_temp.reg)
2115 cfa_temp.reg = REGNO (dest);
2116 cfa_temp.offset |= INTVAL (XEXP (src, 1));
2117 break;
2119 /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2120 which will fill in all of the bits. */
2121 /* Rule 8 */
2122 case HIGH:
2123 break;
2125 /* Rule 15 */
2126 case UNSPEC:
2127 case UNSPEC_VOLATILE:
2128 gcc_assert (targetm.dwarf_handle_frame_unspec);
2129 targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2130 return;
2132 /* Rule 16 */
2133 case AND:
2134 /* If this AND operation happens on stack pointer in prologue,
2135 we assume the stack is realigned and we extract the
2136 alignment. */
2137 if (fde && XEXP (src, 0) == stack_pointer_rtx)
2139 gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2140 fde->stack_realign = 1;
2141 fde->stack_realignment = INTVAL (XEXP (src, 1));
2142 cfa_store.offset = 0;
2144 if (cfa.reg != STACK_POINTER_REGNUM
2145 && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2146 fde->drap_reg = cfa.reg;
2148 return;
2150 default:
2151 gcc_unreachable ();
2154 def_cfa_1 (label, &cfa);
2155 break;
2157 case MEM:
2159 /* Saving a register to the stack. Make sure dest is relative to the
2160 CFA register. */
2161 switch (GET_CODE (XEXP (dest, 0)))
2163 /* Rule 10 */
2164 /* With a push. */
2165 case PRE_MODIFY:
2166 /* We can't handle variable size modifications. */
2167 gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2168 == CONST_INT);
2169 offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2171 gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2172 && cfa_store.reg == STACK_POINTER_REGNUM);
2174 cfa_store.offset += offset;
2175 if (cfa.reg == STACK_POINTER_REGNUM)
2176 cfa.offset = cfa_store.offset;
2178 offset = -cfa_store.offset;
2179 break;
2181 /* Rule 11 */
2182 case PRE_INC:
2183 case PRE_DEC:
2184 offset = GET_MODE_SIZE (GET_MODE (dest));
2185 if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2186 offset = -offset;
2188 gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2189 == STACK_POINTER_REGNUM)
2190 && cfa_store.reg == STACK_POINTER_REGNUM);
2192 cfa_store.offset += offset;
2194 /* Rule 18: If stack is aligned, we will use FP as a
2195 reference to represent the address of the stored
2196 regiser. */
2197 if (fde
2198 && fde->stack_realign
2199 && src == hard_frame_pointer_rtx)
2201 gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2202 cfa_store.offset = 0;
2205 if (cfa.reg == STACK_POINTER_REGNUM)
2206 cfa.offset = cfa_store.offset;
2208 offset = -cfa_store.offset;
2209 break;
2211 /* Rule 12 */
2212 /* With an offset. */
2213 case PLUS:
2214 case MINUS:
2215 case LO_SUM:
2217 int regno;
2219 gcc_assert (GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT
2220 && REG_P (XEXP (XEXP (dest, 0), 0)));
2221 offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2222 if (GET_CODE (XEXP (dest, 0)) == MINUS)
2223 offset = -offset;
2225 regno = REGNO (XEXP (XEXP (dest, 0), 0));
2227 if (cfa_store.reg == (unsigned) regno)
2228 offset -= cfa_store.offset;
2229 else
2231 gcc_assert (cfa_temp.reg == (unsigned) regno);
2232 offset -= cfa_temp.offset;
2235 break;
2237 /* Rule 13 */
2238 /* Without an offset. */
2239 case REG:
2241 int regno = REGNO (XEXP (dest, 0));
2243 if (cfa_store.reg == (unsigned) regno)
2244 offset = -cfa_store.offset;
2245 else
2247 gcc_assert (cfa_temp.reg == (unsigned) regno);
2248 offset = -cfa_temp.offset;
2251 break;
2253 /* Rule 14 */
2254 case POST_INC:
2255 gcc_assert (cfa_temp.reg
2256 == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2257 offset = -cfa_temp.offset;
2258 cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2259 break;
2261 default:
2262 gcc_unreachable ();
2265 /* Rule 17 */
2266 /* If the source operand of this MEM operation is not a
2267 register, basically the source is return address. Here
2268 we only care how much stack grew and we don't save it. */
2269 if (!REG_P (src))
2270 break;
2272 if (REGNO (src) != STACK_POINTER_REGNUM
2273 && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2274 && (unsigned) REGNO (src) == cfa.reg)
2276 /* We're storing the current CFA reg into the stack. */
2278 if (cfa.offset == 0)
2280 /* Rule 19 */
2281 /* If stack is aligned, putting CFA reg into stack means
2282 we can no longer use reg + offset to represent CFA.
2283 Here we use DW_CFA_def_cfa_expression instead. The
2284 result of this expression equals to the original CFA
2285 value. */
2286 if (fde
2287 && fde->stack_realign
2288 && cfa.indirect == 0
2289 && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2291 dw_cfa_location cfa_exp;
2293 gcc_assert (fde->drap_reg == cfa.reg);
2295 cfa_exp.indirect = 1;
2296 cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2297 cfa_exp.base_offset = offset;
2298 cfa_exp.offset = 0;
2300 fde->drap_reg_saved = 1;
2302 def_cfa_1 (label, &cfa_exp);
2303 break;
2306 /* If the source register is exactly the CFA, assume
2307 we're saving SP like any other register; this happens
2308 on the ARM. */
2309 def_cfa_1 (label, &cfa);
2310 queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2311 break;
2313 else
2315 /* Otherwise, we'll need to look in the stack to
2316 calculate the CFA. */
2317 rtx x = XEXP (dest, 0);
2319 if (!REG_P (x))
2320 x = XEXP (x, 0);
2321 gcc_assert (REG_P (x));
2323 cfa.reg = REGNO (x);
2324 cfa.base_offset = offset;
2325 cfa.indirect = 1;
2326 def_cfa_1 (label, &cfa);
2327 break;
2331 def_cfa_1 (label, &cfa);
2333 span = targetm.dwarf_register_span (src);
2335 if (!span)
2336 queue_reg_save (label, src, NULL_RTX, offset);
2337 else
2339 /* We have a PARALLEL describing where the contents of SRC
2340 live. Queue register saves for each piece of the
2341 PARALLEL. */
2342 int par_index;
2343 int limit;
2344 HOST_WIDE_INT span_offset = offset;
2346 gcc_assert (GET_CODE (span) == PARALLEL);
2348 limit = XVECLEN (span, 0);
2349 for (par_index = 0; par_index < limit; par_index++)
2351 rtx elem = XVECEXP (span, 0, par_index);
2353 queue_reg_save (label, elem, NULL_RTX, span_offset);
2354 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2358 break;
2360 default:
2361 gcc_unreachable ();
2365 /* Record call frame debugging information for INSN, which either
2366 sets SP or FP (adjusting how we calculate the frame address) or saves a
2367 register to the stack. If INSN is NULL_RTX, initialize our state.
2369 If AFTER_P is false, we're being called before the insn is emitted,
2370 otherwise after. Call instructions get invoked twice. */
2372 void
2373 dwarf2out_frame_debug (rtx insn, bool after_p)
2375 const char *label;
2376 rtx src;
2378 if (insn == NULL_RTX)
2380 size_t i;
2382 /* Flush any queued register saves. */
2383 flush_queued_reg_saves ();
2385 /* Set up state for generating call frame debug info. */
2386 lookup_cfa (&cfa);
2387 gcc_assert (cfa.reg
2388 == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2390 cfa.reg = STACK_POINTER_REGNUM;
2391 cfa_store = cfa;
2392 cfa_temp.reg = -1;
2393 cfa_temp.offset = 0;
2395 for (i = 0; i < num_regs_saved_in_regs; i++)
2397 regs_saved_in_regs[i].orig_reg = NULL_RTX;
2398 regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2400 num_regs_saved_in_regs = 0;
2402 if (barrier_args_size)
2404 XDELETEVEC (barrier_args_size);
2405 barrier_args_size = NULL;
2407 return;
2410 if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2411 flush_queued_reg_saves ();
2413 if (! RTX_FRAME_RELATED_P (insn))
2415 if (!ACCUMULATE_OUTGOING_ARGS)
2416 dwarf2out_stack_adjust (insn, after_p);
2417 return;
2420 label = dwarf2out_cfi_label ();
2421 src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
2422 if (src)
2423 insn = XEXP (src, 0);
2424 else
2425 insn = PATTERN (insn);
2427 dwarf2out_frame_debug_expr (insn, label);
2430 #endif
2432 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
2433 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
2434 (enum dwarf_call_frame_info cfi);
2436 static enum dw_cfi_oprnd_type
2437 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
2439 switch (cfi)
2441 case DW_CFA_nop:
2442 case DW_CFA_GNU_window_save:
2443 return dw_cfi_oprnd_unused;
2445 case DW_CFA_set_loc:
2446 case DW_CFA_advance_loc1:
2447 case DW_CFA_advance_loc2:
2448 case DW_CFA_advance_loc4:
2449 case DW_CFA_MIPS_advance_loc8:
2450 return dw_cfi_oprnd_addr;
2452 case DW_CFA_offset:
2453 case DW_CFA_offset_extended:
2454 case DW_CFA_def_cfa:
2455 case DW_CFA_offset_extended_sf:
2456 case DW_CFA_def_cfa_sf:
2457 case DW_CFA_restore_extended:
2458 case DW_CFA_undefined:
2459 case DW_CFA_same_value:
2460 case DW_CFA_def_cfa_register:
2461 case DW_CFA_register:
2462 return dw_cfi_oprnd_reg_num;
2464 case DW_CFA_def_cfa_offset:
2465 case DW_CFA_GNU_args_size:
2466 case DW_CFA_def_cfa_offset_sf:
2467 return dw_cfi_oprnd_offset;
2469 case DW_CFA_def_cfa_expression:
2470 case DW_CFA_expression:
2471 return dw_cfi_oprnd_loc;
2473 default:
2474 gcc_unreachable ();
2478 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
2479 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
2480 (enum dwarf_call_frame_info cfi);
2482 static enum dw_cfi_oprnd_type
2483 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
2485 switch (cfi)
2487 case DW_CFA_def_cfa:
2488 case DW_CFA_def_cfa_sf:
2489 case DW_CFA_offset:
2490 case DW_CFA_offset_extended_sf:
2491 case DW_CFA_offset_extended:
2492 return dw_cfi_oprnd_offset;
2494 case DW_CFA_register:
2495 return dw_cfi_oprnd_reg_num;
2497 default:
2498 return dw_cfi_oprnd_unused;
2502 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2504 /* Switch to eh_frame_section. If we don't have an eh_frame_section,
2505 switch to the data section instead, and write out a synthetic label
2506 for collect2. */
2508 static void
2509 switch_to_eh_frame_section (void)
2511 tree label;
2513 #ifdef EH_FRAME_SECTION_NAME
2514 if (eh_frame_section == 0)
2516 int flags;
2518 if (EH_TABLES_CAN_BE_READ_ONLY)
2520 int fde_encoding;
2521 int per_encoding;
2522 int lsda_encoding;
2524 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2525 /*global=*/0);
2526 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2527 /*global=*/1);
2528 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2529 /*global=*/0);
2530 flags = ((! flag_pic
2531 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2532 && (fde_encoding & 0x70) != DW_EH_PE_aligned
2533 && (per_encoding & 0x70) != DW_EH_PE_absptr
2534 && (per_encoding & 0x70) != DW_EH_PE_aligned
2535 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2536 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2537 ? 0 : SECTION_WRITE);
2539 else
2540 flags = SECTION_WRITE;
2541 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2543 #endif
2545 if (eh_frame_section)
2546 switch_to_section (eh_frame_section);
2547 else
2549 /* We have no special eh_frame section. Put the information in
2550 the data section and emit special labels to guide collect2. */
2551 switch_to_section (data_section);
2552 label = get_file_function_name ("F");
2553 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2554 targetm.asm_out.globalize_label (asm_out_file,
2555 IDENTIFIER_POINTER (label));
2556 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
2560 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder. */
2562 static HOST_WIDE_INT
2563 div_data_align (HOST_WIDE_INT off)
2565 HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
2566 gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
2567 return r;
2570 /* Output a Call Frame Information opcode and its operand(s). */
2572 static void
2573 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
2575 unsigned long r;
2576 HOST_WIDE_INT off;
2578 if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
2579 dw2_asm_output_data (1, (cfi->dw_cfi_opc
2580 | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
2581 "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
2582 ((unsigned HOST_WIDE_INT)
2583 cfi->dw_cfi_oprnd1.dw_cfi_offset));
2584 else if (cfi->dw_cfi_opc == DW_CFA_offset)
2586 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2587 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2588 "DW_CFA_offset, column 0x%lx", r);
2589 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2590 dw2_asm_output_data_uleb128 (off, NULL);
2592 else if (cfi->dw_cfi_opc == DW_CFA_restore)
2594 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2595 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2596 "DW_CFA_restore, column 0x%lx", r);
2598 else
2600 dw2_asm_output_data (1, cfi->dw_cfi_opc,
2601 "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
2603 switch (cfi->dw_cfi_opc)
2605 case DW_CFA_set_loc:
2606 if (for_eh)
2607 dw2_asm_output_encoded_addr_rtx (
2608 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
2609 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
2610 false, NULL);
2611 else
2612 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2613 cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
2614 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2615 break;
2617 case DW_CFA_advance_loc1:
2618 dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2619 fde->dw_fde_current_label, NULL);
2620 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2621 break;
2623 case DW_CFA_advance_loc2:
2624 dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2625 fde->dw_fde_current_label, NULL);
2626 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2627 break;
2629 case DW_CFA_advance_loc4:
2630 dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2631 fde->dw_fde_current_label, NULL);
2632 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2633 break;
2635 case DW_CFA_MIPS_advance_loc8:
2636 dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2637 fde->dw_fde_current_label, NULL);
2638 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2639 break;
2641 case DW_CFA_offset_extended:
2642 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2643 dw2_asm_output_data_uleb128 (r, NULL);
2644 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2645 dw2_asm_output_data_uleb128 (off, NULL);
2646 break;
2648 case DW_CFA_def_cfa:
2649 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2650 dw2_asm_output_data_uleb128 (r, NULL);
2651 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2652 break;
2654 case DW_CFA_offset_extended_sf:
2655 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2656 dw2_asm_output_data_uleb128 (r, NULL);
2657 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2658 dw2_asm_output_data_sleb128 (off, NULL);
2659 break;
2661 case DW_CFA_def_cfa_sf:
2662 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2663 dw2_asm_output_data_uleb128 (r, NULL);
2664 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2665 dw2_asm_output_data_sleb128 (off, NULL);
2666 break;
2668 case DW_CFA_restore_extended:
2669 case DW_CFA_undefined:
2670 case DW_CFA_same_value:
2671 case DW_CFA_def_cfa_register:
2672 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2673 dw2_asm_output_data_uleb128 (r, NULL);
2674 break;
2676 case DW_CFA_register:
2677 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2678 dw2_asm_output_data_uleb128 (r, NULL);
2679 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
2680 dw2_asm_output_data_uleb128 (r, NULL);
2681 break;
2683 case DW_CFA_def_cfa_offset:
2684 case DW_CFA_GNU_args_size:
2685 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2686 break;
2688 case DW_CFA_def_cfa_offset_sf:
2689 off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
2690 dw2_asm_output_data_sleb128 (off, NULL);
2691 break;
2693 case DW_CFA_GNU_window_save:
2694 break;
2696 case DW_CFA_def_cfa_expression:
2697 case DW_CFA_expression:
2698 output_cfa_loc (cfi);
2699 break;
2701 case DW_CFA_GNU_negative_offset_extended:
2702 /* Obsoleted by DW_CFA_offset_extended_sf. */
2703 gcc_unreachable ();
2705 default:
2706 break;
2711 /* Similar, but do it via assembler directives instead. */
2713 static void
2714 output_cfi_directive (dw_cfi_ref cfi)
2716 unsigned long r, r2;
2718 switch (cfi->dw_cfi_opc)
2720 case DW_CFA_advance_loc:
2721 case DW_CFA_advance_loc1:
2722 case DW_CFA_advance_loc2:
2723 case DW_CFA_advance_loc4:
2724 case DW_CFA_MIPS_advance_loc8:
2725 case DW_CFA_set_loc:
2726 /* Should only be created by add_fde_cfi in a code path not
2727 followed when emitting via directives. The assembler is
2728 going to take care of this for us. */
2729 gcc_unreachable ();
2731 case DW_CFA_offset:
2732 case DW_CFA_offset_extended:
2733 case DW_CFA_offset_extended_sf:
2734 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2735 fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
2736 r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
2737 break;
2739 case DW_CFA_restore:
2740 case DW_CFA_restore_extended:
2741 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2742 fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
2743 break;
2745 case DW_CFA_undefined:
2746 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2747 fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
2748 break;
2750 case DW_CFA_same_value:
2751 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2752 fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
2753 break;
2755 case DW_CFA_def_cfa:
2756 case DW_CFA_def_cfa_sf:
2757 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2758 fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
2759 r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
2760 break;
2762 case DW_CFA_def_cfa_register:
2763 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2764 fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
2765 break;
2767 case DW_CFA_register:
2768 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2769 r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 0);
2770 fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
2771 break;
2773 case DW_CFA_def_cfa_offset:
2774 case DW_CFA_def_cfa_offset_sf:
2775 fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
2776 HOST_WIDE_INT_PRINT_DEC"\n",
2777 cfi->dw_cfi_oprnd1.dw_cfi_offset);
2778 break;
2780 case DW_CFA_GNU_args_size:
2781 fprintf (asm_out_file, "\t.cfi_escape 0x%x,", DW_CFA_GNU_args_size);
2782 dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
2783 if (flag_debug_asm)
2784 fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
2785 ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
2786 fputc ('\n', asm_out_file);
2787 break;
2789 case DW_CFA_GNU_window_save:
2790 fprintf (asm_out_file, "\t.cfi_window_save\n");
2791 break;
2793 case DW_CFA_def_cfa_expression:
2794 case DW_CFA_expression:
2795 fprintf (asm_out_file, "\t.cfi_escape 0x%x,", cfi->dw_cfi_opc);
2796 output_cfa_loc_raw (cfi);
2797 fputc ('\n', asm_out_file);
2798 break;
2800 default:
2801 gcc_unreachable ();
2805 /* Output the call frame information used to record information
2806 that relates to calculating the frame pointer, and records the
2807 location of saved registers. */
2809 static void
2810 output_call_frame_info (int for_eh)
2812 unsigned int i;
2813 dw_fde_ref fde;
2814 dw_cfi_ref cfi;
2815 char l1[20], l2[20], section_start_label[20];
2816 bool any_lsda_needed = false;
2817 char augmentation[6];
2818 int augmentation_size;
2819 int fde_encoding = DW_EH_PE_absptr;
2820 int per_encoding = DW_EH_PE_absptr;
2821 int lsda_encoding = DW_EH_PE_absptr;
2822 int return_reg;
2824 /* Don't emit a CIE if there won't be any FDEs. */
2825 if (fde_table_in_use == 0)
2826 return;
2828 /* Nothing to do if the assembler's doing it all. */
2829 if (dwarf2out_do_cfi_asm ())
2830 return;
2832 /* If we make FDEs linkonce, we may have to emit an empty label for
2833 an FDE that wouldn't otherwise be emitted. We want to avoid
2834 having an FDE kept around when the function it refers to is
2835 discarded. Example where this matters: a primary function
2836 template in C++ requires EH information, but an explicit
2837 specialization doesn't. */
2838 if (TARGET_USES_WEAK_UNWIND_INFO
2839 && ! flag_asynchronous_unwind_tables
2840 && flag_exceptions
2841 && for_eh)
2842 for (i = 0; i < fde_table_in_use; i++)
2843 if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
2844 && !fde_table[i].uses_eh_lsda
2845 && ! DECL_WEAK (fde_table[i].decl))
2846 targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
2847 for_eh, /* empty */ 1);
2849 /* If we don't have any functions we'll want to unwind out of, don't
2850 emit any EH unwind information. Note that if exceptions aren't
2851 enabled, we won't have collected nothrow information, and if we
2852 asked for asynchronous tables, we always want this info. */
2853 if (for_eh)
2855 bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
2857 for (i = 0; i < fde_table_in_use; i++)
2858 if (fde_table[i].uses_eh_lsda)
2859 any_eh_needed = any_lsda_needed = true;
2860 else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2861 any_eh_needed = true;
2862 else if (! fde_table[i].nothrow
2863 && ! fde_table[i].all_throwers_are_sibcalls)
2864 any_eh_needed = true;
2866 if (! any_eh_needed)
2867 return;
2870 /* We're going to be generating comments, so turn on app. */
2871 if (flag_debug_asm)
2872 app_enable ();
2874 if (for_eh)
2875 switch_to_eh_frame_section ();
2876 else
2878 if (!debug_frame_section)
2879 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
2880 SECTION_DEBUG, NULL);
2881 switch_to_section (debug_frame_section);
2884 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
2885 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
2887 /* Output the CIE. */
2888 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
2889 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
2890 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2891 dw2_asm_output_data (4, 0xffffffff,
2892 "Initial length escape value indicating 64-bit DWARF extension");
2893 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2894 "Length of Common Information Entry");
2895 ASM_OUTPUT_LABEL (asm_out_file, l1);
2897 /* Now that the CIE pointer is PC-relative for EH,
2898 use 0 to identify the CIE. */
2899 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
2900 (for_eh ? 0 : DWARF_CIE_ID),
2901 "CIE Identifier Tag");
2903 dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
2905 augmentation[0] = 0;
2906 augmentation_size = 0;
2907 if (for_eh)
2909 char *p;
2911 /* Augmentation:
2912 z Indicates that a uleb128 is present to size the
2913 augmentation section.
2914 L Indicates the encoding (and thus presence) of
2915 an LSDA pointer in the FDE augmentation.
2916 R Indicates a non-default pointer encoding for
2917 FDE code pointers.
2918 P Indicates the presence of an encoding + language
2919 personality routine in the CIE augmentation. */
2921 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
2922 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
2923 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
2925 p = augmentation + 1;
2926 if (eh_personality_libfunc)
2928 *p++ = 'P';
2929 augmentation_size += 1 + size_of_encoded_value (per_encoding);
2930 assemble_external_libcall (eh_personality_libfunc);
2932 if (any_lsda_needed)
2934 *p++ = 'L';
2935 augmentation_size += 1;
2937 if (fde_encoding != DW_EH_PE_absptr)
2939 *p++ = 'R';
2940 augmentation_size += 1;
2942 if (p > augmentation + 1)
2944 augmentation[0] = 'z';
2945 *p = '\0';
2948 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
2949 if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
2951 int offset = ( 4 /* Length */
2952 + 4 /* CIE Id */
2953 + 1 /* CIE version */
2954 + strlen (augmentation) + 1 /* Augmentation */
2955 + size_of_uleb128 (1) /* Code alignment */
2956 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
2957 + 1 /* RA column */
2958 + 1 /* Augmentation size */
2959 + 1 /* Personality encoding */ );
2960 int pad = -offset & (PTR_SIZE - 1);
2962 augmentation_size += pad;
2964 /* Augmentations should be small, so there's scarce need to
2965 iterate for a solution. Die if we exceed one uleb128 byte. */
2966 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
2970 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
2971 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
2972 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
2973 "CIE Data Alignment Factor");
2975 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
2976 if (DW_CIE_VERSION == 1)
2977 dw2_asm_output_data (1, return_reg, "CIE RA Column");
2978 else
2979 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
2981 if (augmentation[0])
2983 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
2984 if (eh_personality_libfunc)
2986 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
2987 eh_data_format_name (per_encoding));
2988 dw2_asm_output_encoded_addr_rtx (per_encoding,
2989 eh_personality_libfunc,
2990 true, NULL);
2993 if (any_lsda_needed)
2994 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
2995 eh_data_format_name (lsda_encoding));
2997 if (fde_encoding != DW_EH_PE_absptr)
2998 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
2999 eh_data_format_name (fde_encoding));
3002 for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
3003 output_cfi (cfi, NULL, for_eh);
3005 /* Pad the CIE out to an address sized boundary. */
3006 ASM_OUTPUT_ALIGN (asm_out_file,
3007 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3008 ASM_OUTPUT_LABEL (asm_out_file, l2);
3010 /* Loop through all of the FDE's. */
3011 for (i = 0; i < fde_table_in_use; i++)
3013 fde = &fde_table[i];
3015 /* Don't emit EH unwind info for leaf functions that don't need it. */
3016 if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
3017 && (fde->nothrow || fde->all_throwers_are_sibcalls)
3018 && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
3019 && !fde->uses_eh_lsda)
3020 continue;
3022 targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
3023 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
3024 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
3025 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
3026 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3027 dw2_asm_output_data (4, 0xffffffff,
3028 "Initial length escape value indicating 64-bit DWARF extension");
3029 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3030 "FDE Length");
3031 ASM_OUTPUT_LABEL (asm_out_file, l1);
3033 if (for_eh)
3034 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3035 else
3036 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3037 debug_frame_section, "FDE CIE offset");
3039 if (for_eh)
3041 if (fde->dw_fde_switched_sections)
3043 rtx sym_ref2 = gen_rtx_SYMBOL_REF (Pmode,
3044 fde->dw_fde_unlikely_section_label);
3045 rtx sym_ref3= gen_rtx_SYMBOL_REF (Pmode,
3046 fde->dw_fde_hot_section_label);
3047 SYMBOL_REF_FLAGS (sym_ref2) |= SYMBOL_FLAG_LOCAL;
3048 SYMBOL_REF_FLAGS (sym_ref3) |= SYMBOL_FLAG_LOCAL;
3049 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref3, false,
3050 "FDE initial location");
3051 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3052 fde->dw_fde_hot_section_end_label,
3053 fde->dw_fde_hot_section_label,
3054 "FDE address range");
3055 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref2, false,
3056 "FDE initial location");
3057 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3058 fde->dw_fde_unlikely_section_end_label,
3059 fde->dw_fde_unlikely_section_label,
3060 "FDE address range");
3062 else
3064 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
3065 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3066 dw2_asm_output_encoded_addr_rtx (fde_encoding,
3067 sym_ref,
3068 false,
3069 "FDE initial location");
3070 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3071 fde->dw_fde_end, fde->dw_fde_begin,
3072 "FDE address range");
3075 else
3077 if (fde->dw_fde_switched_sections)
3079 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3080 fde->dw_fde_hot_section_label,
3081 "FDE initial location");
3082 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3083 fde->dw_fde_hot_section_end_label,
3084 fde->dw_fde_hot_section_label,
3085 "FDE address range");
3086 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3087 fde->dw_fde_unlikely_section_label,
3088 "FDE initial location");
3089 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3090 fde->dw_fde_unlikely_section_end_label,
3091 fde->dw_fde_unlikely_section_label,
3092 "FDE address range");
3094 else
3096 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
3097 "FDE initial location");
3098 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3099 fde->dw_fde_end, fde->dw_fde_begin,
3100 "FDE address range");
3104 if (augmentation[0])
3106 if (any_lsda_needed)
3108 int size = size_of_encoded_value (lsda_encoding);
3110 if (lsda_encoding == DW_EH_PE_aligned)
3112 int offset = ( 4 /* Length */
3113 + 4 /* CIE offset */
3114 + 2 * size_of_encoded_value (fde_encoding)
3115 + 1 /* Augmentation size */ );
3116 int pad = -offset & (PTR_SIZE - 1);
3118 size += pad;
3119 gcc_assert (size_of_uleb128 (size) == 1);
3122 dw2_asm_output_data_uleb128 (size, "Augmentation size");
3124 if (fde->uses_eh_lsda)
3126 ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
3127 fde->funcdef_number);
3128 dw2_asm_output_encoded_addr_rtx (
3129 lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
3130 false, "Language Specific Data Area");
3132 else
3134 if (lsda_encoding == DW_EH_PE_aligned)
3135 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3136 dw2_asm_output_data
3137 (size_of_encoded_value (lsda_encoding), 0,
3138 "Language Specific Data Area (none)");
3141 else
3142 dw2_asm_output_data_uleb128 (0, "Augmentation size");
3145 /* Loop through the Call Frame Instructions associated with
3146 this FDE. */
3147 fde->dw_fde_current_label = fde->dw_fde_begin;
3148 for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3149 output_cfi (cfi, fde, for_eh);
3151 /* Pad the FDE out to an address sized boundary. */
3152 ASM_OUTPUT_ALIGN (asm_out_file,
3153 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3154 ASM_OUTPUT_LABEL (asm_out_file, l2);
3157 if (for_eh && targetm.terminate_dw2_eh_frame_info)
3158 dw2_asm_output_data (4, 0, "End of Table");
3159 #ifdef MIPS_DEBUGGING_INFO
3160 /* Work around Irix 6 assembler bug whereby labels at the end of a section
3161 get a value of 0. Putting .align 0 after the label fixes it. */
3162 ASM_OUTPUT_ALIGN (asm_out_file, 0);
3163 #endif
3165 /* Turn off app to make assembly quicker. */
3166 if (flag_debug_asm)
3167 app_disable ();
3170 /* Output a marker (i.e. a label) for the beginning of a function, before
3171 the prologue. */
3173 void
3174 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
3175 const char *file ATTRIBUTE_UNUSED)
3177 char label[MAX_ARTIFICIAL_LABEL_BYTES];
3178 char * dup_label;
3179 dw_fde_ref fde;
3181 current_function_func_begin_label = NULL;
3183 #ifdef TARGET_UNWIND_INFO
3184 /* ??? current_function_func_begin_label is also used by except.c
3185 for call-site information. We must emit this label if it might
3186 be used. */
3187 if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
3188 && ! dwarf2out_do_frame ())
3189 return;
3190 #else
3191 if (! dwarf2out_do_frame ())
3192 return;
3193 #endif
3195 switch_to_section (function_section (current_function_decl));
3196 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
3197 current_function_funcdef_no);
3198 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
3199 current_function_funcdef_no);
3200 dup_label = xstrdup (label);
3201 current_function_func_begin_label = dup_label;
3203 #ifdef TARGET_UNWIND_INFO
3204 /* We can elide the fde allocation if we're not emitting debug info. */
3205 if (! dwarf2out_do_frame ())
3206 return;
3207 #endif
3209 /* Expand the fde table if necessary. */
3210 if (fde_table_in_use == fde_table_allocated)
3212 fde_table_allocated += FDE_TABLE_INCREMENT;
3213 fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
3214 memset (fde_table + fde_table_in_use, 0,
3215 FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
3218 /* Record the FDE associated with this function. */
3219 current_funcdef_fde = fde_table_in_use;
3221 /* Add the new FDE at the end of the fde_table. */
3222 fde = &fde_table[fde_table_in_use++];
3223 fde->decl = current_function_decl;
3224 fde->dw_fde_begin = dup_label;
3225 fde->dw_fde_current_label = dup_label;
3226 fde->dw_fde_hot_section_label = NULL;
3227 fde->dw_fde_hot_section_end_label = NULL;
3228 fde->dw_fde_unlikely_section_label = NULL;
3229 fde->dw_fde_unlikely_section_end_label = NULL;
3230 fde->dw_fde_switched_sections = false;
3231 fde->dw_fde_end = NULL;
3232 fde->dw_fde_cfi = NULL;
3233 fde->funcdef_number = current_function_funcdef_no;
3234 fde->nothrow = crtl->nothrow;
3235 fde->uses_eh_lsda = crtl->uses_eh_lsda;
3236 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
3237 fde->drap_reg = INVALID_REGNUM;
3238 fde->vdrap_reg = INVALID_REGNUM;
3240 args_size = old_args_size = 0;
3242 /* We only want to output line number information for the genuine dwarf2
3243 prologue case, not the eh frame case. */
3244 #ifdef DWARF2_DEBUGGING_INFO
3245 if (file)
3246 dwarf2out_source_line (line, file);
3247 #endif
3249 if (dwarf2out_do_cfi_asm ())
3251 int enc;
3252 rtx ref;
3254 fprintf (asm_out_file, "\t.cfi_startproc\n");
3256 if (eh_personality_libfunc)
3258 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3259 ref = eh_personality_libfunc;
3261 /* ??? The GAS support isn't entirely consistent. We have to
3262 handle indirect support ourselves, but PC-relative is done
3263 in the assembler. Further, the assembler can't handle any
3264 of the weirder relocation types. */
3265 if (enc & DW_EH_PE_indirect)
3266 ref = dw2_force_const_mem (ref, true);
3268 fprintf (asm_out_file, "\t.cfi_personality 0x%x,", enc);
3269 output_addr_const (asm_out_file, ref);
3270 fputc ('\n', asm_out_file);
3273 if (crtl->uses_eh_lsda)
3275 char lab[20];
3277 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3278 ASM_GENERATE_INTERNAL_LABEL (lab, "LLSDA",
3279 current_function_funcdef_no);
3280 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
3281 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
3283 if (enc & DW_EH_PE_indirect)
3284 ref = dw2_force_const_mem (ref, true);
3286 fprintf (asm_out_file, "\t.cfi_lsda 0x%x,", enc);
3287 output_addr_const (asm_out_file, ref);
3288 fputc ('\n', asm_out_file);
3293 /* Output a marker (i.e. a label) for the absolute end of the generated code
3294 for a function definition. This gets called *after* the epilogue code has
3295 been generated. */
3297 void
3298 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
3299 const char *file ATTRIBUTE_UNUSED)
3301 dw_fde_ref fde;
3302 char label[MAX_ARTIFICIAL_LABEL_BYTES];
3304 if (dwarf2out_do_cfi_asm ())
3305 fprintf (asm_out_file, "\t.cfi_endproc\n");
3307 /* Output a label to mark the endpoint of the code generated for this
3308 function. */
3309 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
3310 current_function_funcdef_no);
3311 ASM_OUTPUT_LABEL (asm_out_file, label);
3312 fde = current_fde ();
3313 gcc_assert (fde != NULL);
3314 fde->dw_fde_end = xstrdup (label);
3317 void
3318 dwarf2out_frame_init (void)
3320 /* Allocate the initial hunk of the fde_table. */
3321 fde_table = GGC_CNEWVEC (dw_fde_node, FDE_TABLE_INCREMENT);
3322 fde_table_allocated = FDE_TABLE_INCREMENT;
3323 fde_table_in_use = 0;
3325 /* Generate the CFA instructions common to all FDE's. Do it now for the
3326 sake of lookup_cfa. */
3328 /* On entry, the Canonical Frame Address is at SP. */
3329 dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
3331 #ifdef DWARF2_UNWIND_INFO
3332 if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
3333 initial_return_save (INCOMING_RETURN_ADDR_RTX);
3334 #endif
3337 void
3338 dwarf2out_frame_finish (void)
3340 /* Output call frame information. */
3341 if (DWARF2_FRAME_INFO)
3342 output_call_frame_info (0);
3344 #ifndef TARGET_UNWIND_INFO
3345 /* Output another copy for the unwinder. */
3346 if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
3347 output_call_frame_info (1);
3348 #endif
3351 /* Note that the current function section is being used for code. */
3353 static void
3354 dwarf2out_note_section_used (void)
3356 section *sec = current_function_section ();
3357 if (sec == text_section)
3358 text_section_used = true;
3359 else if (sec == cold_text_section)
3360 cold_text_section_used = true;
3363 void
3364 dwarf2out_switch_text_section (void)
3366 dw_fde_ref fde = current_fde ();
3368 gcc_assert (cfun && fde);
3370 fde->dw_fde_switched_sections = true;
3371 fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
3372 fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
3373 fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
3374 fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
3375 have_multiple_function_sections = true;
3377 /* Reset the current label on switching text sections, so that we
3378 don't attempt to advance_loc4 between labels in different sections. */
3379 fde->dw_fde_current_label = NULL;
3381 /* There is no need to mark used sections when not debugging. */
3382 if (cold_text_section != NULL)
3383 dwarf2out_note_section_used ();
3385 #endif
3387 /* And now, the subset of the debugging information support code necessary
3388 for emitting location expressions. */
3390 /* Data about a single source file. */
3391 struct dwarf_file_data GTY(())
3393 const char * filename;
3394 int emitted_number;
3397 /* We need some way to distinguish DW_OP_addr with a direct symbol
3398 relocation from DW_OP_addr with a dtp-relative symbol relocation. */
3399 #define INTERNAL_DW_OP_tls_addr (0x100 + DW_OP_addr)
3402 typedef struct dw_val_struct *dw_val_ref;
3403 typedef struct die_struct *dw_die_ref;
3404 typedef const struct die_struct *const_dw_die_ref;
3405 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
3406 typedef struct dw_loc_list_struct *dw_loc_list_ref;
3408 typedef struct deferred_locations_struct GTY(())
3410 tree variable;
3411 dw_die_ref die;
3412 } deferred_locations;
3414 DEF_VEC_O(deferred_locations);
3415 DEF_VEC_ALLOC_O(deferred_locations,gc);
3417 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
3419 /* Each DIE may have a series of attribute/value pairs. Values
3420 can take on several forms. The forms that are used in this
3421 implementation are listed below. */
3423 enum dw_val_class
3425 dw_val_class_addr,
3426 dw_val_class_offset,
3427 dw_val_class_loc,
3428 dw_val_class_loc_list,
3429 dw_val_class_range_list,
3430 dw_val_class_const,
3431 dw_val_class_unsigned_const,
3432 dw_val_class_long_long,
3433 dw_val_class_vec,
3434 dw_val_class_flag,
3435 dw_val_class_die_ref,
3436 dw_val_class_fde_ref,
3437 dw_val_class_lbl_id,
3438 dw_val_class_lineptr,
3439 dw_val_class_str,
3440 dw_val_class_macptr,
3441 dw_val_class_file
3444 /* Describe a double word constant value. */
3445 /* ??? Every instance of long_long in the code really means CONST_DOUBLE. */
3447 typedef struct dw_long_long_struct GTY(())
3449 unsigned long hi;
3450 unsigned long low;
3452 dw_long_long_const;
3454 /* Describe a floating point constant value, or a vector constant value. */
3456 typedef struct dw_vec_struct GTY(())
3458 unsigned char * GTY((length ("%h.length"))) array;
3459 unsigned length;
3460 unsigned elt_size;
3462 dw_vec_const;
3464 /* The dw_val_node describes an attribute's value, as it is
3465 represented internally. */
3467 typedef struct dw_val_struct GTY(())
3469 enum dw_val_class val_class;
3470 union dw_val_struct_union
3472 rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
3473 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
3474 dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
3475 dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
3476 HOST_WIDE_INT GTY ((default)) val_int;
3477 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
3478 dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
3479 dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
3480 struct dw_val_die_union
3482 dw_die_ref die;
3483 int external;
3484 } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
3485 unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
3486 struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
3487 char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
3488 unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
3489 struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
3491 GTY ((desc ("%1.val_class"))) v;
3493 dw_val_node;
3495 /* Locations in memory are described using a sequence of stack machine
3496 operations. */
3498 typedef struct dw_loc_descr_struct GTY(())
3500 dw_loc_descr_ref dw_loc_next;
3501 enum dwarf_location_atom dw_loc_opc;
3502 int dw_loc_addr;
3503 dw_val_node dw_loc_oprnd1;
3504 dw_val_node dw_loc_oprnd2;
3506 dw_loc_descr_node;
3508 /* Location lists are ranges + location descriptions for that range,
3509 so you can track variables that are in different places over
3510 their entire life. */
3511 typedef struct dw_loc_list_struct GTY(())
3513 dw_loc_list_ref dw_loc_next;
3514 const char *begin; /* Label for begin address of range */
3515 const char *end; /* Label for end address of range */
3516 char *ll_symbol; /* Label for beginning of location list.
3517 Only on head of list */
3518 const char *section; /* Section this loclist is relative to */
3519 dw_loc_descr_ref expr;
3520 } dw_loc_list_node;
3522 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
3524 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
3526 /* Convert a DWARF stack opcode into its string name. */
3528 static const char *
3529 dwarf_stack_op_name (unsigned int op)
3531 switch (op)
3533 case DW_OP_addr:
3534 case INTERNAL_DW_OP_tls_addr:
3535 return "DW_OP_addr";
3536 case DW_OP_deref:
3537 return "DW_OP_deref";
3538 case DW_OP_const1u:
3539 return "DW_OP_const1u";
3540 case DW_OP_const1s:
3541 return "DW_OP_const1s";
3542 case DW_OP_const2u:
3543 return "DW_OP_const2u";
3544 case DW_OP_const2s:
3545 return "DW_OP_const2s";
3546 case DW_OP_const4u:
3547 return "DW_OP_const4u";
3548 case DW_OP_const4s:
3549 return "DW_OP_const4s";
3550 case DW_OP_const8u:
3551 return "DW_OP_const8u";
3552 case DW_OP_const8s:
3553 return "DW_OP_const8s";
3554 case DW_OP_constu:
3555 return "DW_OP_constu";
3556 case DW_OP_consts:
3557 return "DW_OP_consts";
3558 case DW_OP_dup:
3559 return "DW_OP_dup";
3560 case DW_OP_drop:
3561 return "DW_OP_drop";
3562 case DW_OP_over:
3563 return "DW_OP_over";
3564 case DW_OP_pick:
3565 return "DW_OP_pick";
3566 case DW_OP_swap:
3567 return "DW_OP_swap";
3568 case DW_OP_rot:
3569 return "DW_OP_rot";
3570 case DW_OP_xderef:
3571 return "DW_OP_xderef";
3572 case DW_OP_abs:
3573 return "DW_OP_abs";
3574 case DW_OP_and:
3575 return "DW_OP_and";
3576 case DW_OP_div:
3577 return "DW_OP_div";
3578 case DW_OP_minus:
3579 return "DW_OP_minus";
3580 case DW_OP_mod:
3581 return "DW_OP_mod";
3582 case DW_OP_mul:
3583 return "DW_OP_mul";
3584 case DW_OP_neg:
3585 return "DW_OP_neg";
3586 case DW_OP_not:
3587 return "DW_OP_not";
3588 case DW_OP_or:
3589 return "DW_OP_or";
3590 case DW_OP_plus:
3591 return "DW_OP_plus";
3592 case DW_OP_plus_uconst:
3593 return "DW_OP_plus_uconst";
3594 case DW_OP_shl:
3595 return "DW_OP_shl";
3596 case DW_OP_shr:
3597 return "DW_OP_shr";
3598 case DW_OP_shra:
3599 return "DW_OP_shra";
3600 case DW_OP_xor:
3601 return "DW_OP_xor";
3602 case DW_OP_bra:
3603 return "DW_OP_bra";
3604 case DW_OP_eq:
3605 return "DW_OP_eq";
3606 case DW_OP_ge:
3607 return "DW_OP_ge";
3608 case DW_OP_gt:
3609 return "DW_OP_gt";
3610 case DW_OP_le:
3611 return "DW_OP_le";
3612 case DW_OP_lt:
3613 return "DW_OP_lt";
3614 case DW_OP_ne:
3615 return "DW_OP_ne";
3616 case DW_OP_skip:
3617 return "DW_OP_skip";
3618 case DW_OP_lit0:
3619 return "DW_OP_lit0";
3620 case DW_OP_lit1:
3621 return "DW_OP_lit1";
3622 case DW_OP_lit2:
3623 return "DW_OP_lit2";
3624 case DW_OP_lit3:
3625 return "DW_OP_lit3";
3626 case DW_OP_lit4:
3627 return "DW_OP_lit4";
3628 case DW_OP_lit5:
3629 return "DW_OP_lit5";
3630 case DW_OP_lit6:
3631 return "DW_OP_lit6";
3632 case DW_OP_lit7:
3633 return "DW_OP_lit7";
3634 case DW_OP_lit8:
3635 return "DW_OP_lit8";
3636 case DW_OP_lit9:
3637 return "DW_OP_lit9";
3638 case DW_OP_lit10:
3639 return "DW_OP_lit10";
3640 case DW_OP_lit11:
3641 return "DW_OP_lit11";
3642 case DW_OP_lit12:
3643 return "DW_OP_lit12";
3644 case DW_OP_lit13:
3645 return "DW_OP_lit13";
3646 case DW_OP_lit14:
3647 return "DW_OP_lit14";
3648 case DW_OP_lit15:
3649 return "DW_OP_lit15";
3650 case DW_OP_lit16:
3651 return "DW_OP_lit16";
3652 case DW_OP_lit17:
3653 return "DW_OP_lit17";
3654 case DW_OP_lit18:
3655 return "DW_OP_lit18";
3656 case DW_OP_lit19:
3657 return "DW_OP_lit19";
3658 case DW_OP_lit20:
3659 return "DW_OP_lit20";
3660 case DW_OP_lit21:
3661 return "DW_OP_lit21";
3662 case DW_OP_lit22:
3663 return "DW_OP_lit22";
3664 case DW_OP_lit23:
3665 return "DW_OP_lit23";
3666 case DW_OP_lit24:
3667 return "DW_OP_lit24";
3668 case DW_OP_lit25:
3669 return "DW_OP_lit25";
3670 case DW_OP_lit26:
3671 return "DW_OP_lit26";
3672 case DW_OP_lit27:
3673 return "DW_OP_lit27";
3674 case DW_OP_lit28:
3675 return "DW_OP_lit28";
3676 case DW_OP_lit29:
3677 return "DW_OP_lit29";
3678 case DW_OP_lit30:
3679 return "DW_OP_lit30";
3680 case DW_OP_lit31:
3681 return "DW_OP_lit31";
3682 case DW_OP_reg0:
3683 return "DW_OP_reg0";
3684 case DW_OP_reg1:
3685 return "DW_OP_reg1";
3686 case DW_OP_reg2:
3687 return "DW_OP_reg2";
3688 case DW_OP_reg3:
3689 return "DW_OP_reg3";
3690 case DW_OP_reg4:
3691 return "DW_OP_reg4";
3692 case DW_OP_reg5:
3693 return "DW_OP_reg5";
3694 case DW_OP_reg6:
3695 return "DW_OP_reg6";
3696 case DW_OP_reg7:
3697 return "DW_OP_reg7";
3698 case DW_OP_reg8:
3699 return "DW_OP_reg8";
3700 case DW_OP_reg9:
3701 return "DW_OP_reg9";
3702 case DW_OP_reg10:
3703 return "DW_OP_reg10";
3704 case DW_OP_reg11:
3705 return "DW_OP_reg11";
3706 case DW_OP_reg12:
3707 return "DW_OP_reg12";
3708 case DW_OP_reg13:
3709 return "DW_OP_reg13";
3710 case DW_OP_reg14:
3711 return "DW_OP_reg14";
3712 case DW_OP_reg15:
3713 return "DW_OP_reg15";
3714 case DW_OP_reg16:
3715 return "DW_OP_reg16";
3716 case DW_OP_reg17:
3717 return "DW_OP_reg17";
3718 case DW_OP_reg18:
3719 return "DW_OP_reg18";
3720 case DW_OP_reg19:
3721 return "DW_OP_reg19";
3722 case DW_OP_reg20:
3723 return "DW_OP_reg20";
3724 case DW_OP_reg21:
3725 return "DW_OP_reg21";
3726 case DW_OP_reg22:
3727 return "DW_OP_reg22";
3728 case DW_OP_reg23:
3729 return "DW_OP_reg23";
3730 case DW_OP_reg24:
3731 return "DW_OP_reg24";
3732 case DW_OP_reg25:
3733 return "DW_OP_reg25";
3734 case DW_OP_reg26:
3735 return "DW_OP_reg26";
3736 case DW_OP_reg27:
3737 return "DW_OP_reg27";
3738 case DW_OP_reg28:
3739 return "DW_OP_reg28";
3740 case DW_OP_reg29:
3741 return "DW_OP_reg29";
3742 case DW_OP_reg30:
3743 return "DW_OP_reg30";
3744 case DW_OP_reg31:
3745 return "DW_OP_reg31";
3746 case DW_OP_breg0:
3747 return "DW_OP_breg0";
3748 case DW_OP_breg1:
3749 return "DW_OP_breg1";
3750 case DW_OP_breg2:
3751 return "DW_OP_breg2";
3752 case DW_OP_breg3:
3753 return "DW_OP_breg3";
3754 case DW_OP_breg4:
3755 return "DW_OP_breg4";
3756 case DW_OP_breg5:
3757 return "DW_OP_breg5";
3758 case DW_OP_breg6:
3759 return "DW_OP_breg6";
3760 case DW_OP_breg7:
3761 return "DW_OP_breg7";
3762 case DW_OP_breg8:
3763 return "DW_OP_breg8";
3764 case DW_OP_breg9:
3765 return "DW_OP_breg9";
3766 case DW_OP_breg10:
3767 return "DW_OP_breg10";
3768 case DW_OP_breg11:
3769 return "DW_OP_breg11";
3770 case DW_OP_breg12:
3771 return "DW_OP_breg12";
3772 case DW_OP_breg13:
3773 return "DW_OP_breg13";
3774 case DW_OP_breg14:
3775 return "DW_OP_breg14";
3776 case DW_OP_breg15:
3777 return "DW_OP_breg15";
3778 case DW_OP_breg16:
3779 return "DW_OP_breg16";
3780 case DW_OP_breg17:
3781 return "DW_OP_breg17";
3782 case DW_OP_breg18:
3783 return "DW_OP_breg18";
3784 case DW_OP_breg19:
3785 return "DW_OP_breg19";
3786 case DW_OP_breg20:
3787 return "DW_OP_breg20";
3788 case DW_OP_breg21:
3789 return "DW_OP_breg21";
3790 case DW_OP_breg22:
3791 return "DW_OP_breg22";
3792 case DW_OP_breg23:
3793 return "DW_OP_breg23";
3794 case DW_OP_breg24:
3795 return "DW_OP_breg24";
3796 case DW_OP_breg25:
3797 return "DW_OP_breg25";
3798 case DW_OP_breg26:
3799 return "DW_OP_breg26";
3800 case DW_OP_breg27:
3801 return "DW_OP_breg27";
3802 case DW_OP_breg28:
3803 return "DW_OP_breg28";
3804 case DW_OP_breg29:
3805 return "DW_OP_breg29";
3806 case DW_OP_breg30:
3807 return "DW_OP_breg30";
3808 case DW_OP_breg31:
3809 return "DW_OP_breg31";
3810 case DW_OP_regx:
3811 return "DW_OP_regx";
3812 case DW_OP_fbreg:
3813 return "DW_OP_fbreg";
3814 case DW_OP_bregx:
3815 return "DW_OP_bregx";
3816 case DW_OP_piece:
3817 return "DW_OP_piece";
3818 case DW_OP_deref_size:
3819 return "DW_OP_deref_size";
3820 case DW_OP_xderef_size:
3821 return "DW_OP_xderef_size";
3822 case DW_OP_nop:
3823 return "DW_OP_nop";
3824 case DW_OP_push_object_address:
3825 return "DW_OP_push_object_address";
3826 case DW_OP_call2:
3827 return "DW_OP_call2";
3828 case DW_OP_call4:
3829 return "DW_OP_call4";
3830 case DW_OP_call_ref:
3831 return "DW_OP_call_ref";
3832 case DW_OP_GNU_push_tls_address:
3833 return "DW_OP_GNU_push_tls_address";
3834 case DW_OP_GNU_uninit:
3835 return "DW_OP_GNU_uninit";
3836 default:
3837 return "OP_<unknown>";
3841 /* Return a pointer to a newly allocated location description. Location
3842 descriptions are simple expression terms that can be strung
3843 together to form more complicated location (address) descriptions. */
3845 static inline dw_loc_descr_ref
3846 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
3847 unsigned HOST_WIDE_INT oprnd2)
3849 dw_loc_descr_ref descr = GGC_CNEW (dw_loc_descr_node);
3851 descr->dw_loc_opc = op;
3852 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
3853 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
3854 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
3855 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
3857 return descr;
3860 /* Return a pointer to a newly allocated location description for
3861 REG and OFFSET. */
3863 static inline dw_loc_descr_ref
3864 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
3866 if (offset)
3868 if (reg <= 31)
3869 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
3870 offset, 0);
3871 else
3872 return new_loc_descr (DW_OP_bregx, reg, offset);
3874 else if (reg <= 31)
3875 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + reg), 0, 0);
3876 else
3877 return new_loc_descr (DW_OP_regx, reg, 0);
3880 /* Add a location description term to a location description expression. */
3882 static inline void
3883 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
3885 dw_loc_descr_ref *d;
3887 /* Find the end of the chain. */
3888 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
3891 *d = descr;
3894 /* Return the size of a location descriptor. */
3896 static unsigned long
3897 size_of_loc_descr (dw_loc_descr_ref loc)
3899 unsigned long size = 1;
3901 switch (loc->dw_loc_opc)
3903 case DW_OP_addr:
3904 case INTERNAL_DW_OP_tls_addr:
3905 size += DWARF2_ADDR_SIZE;
3906 break;
3907 case DW_OP_const1u:
3908 case DW_OP_const1s:
3909 size += 1;
3910 break;
3911 case DW_OP_const2u:
3912 case DW_OP_const2s:
3913 size += 2;
3914 break;
3915 case DW_OP_const4u:
3916 case DW_OP_const4s:
3917 size += 4;
3918 break;
3919 case DW_OP_const8u:
3920 case DW_OP_const8s:
3921 size += 8;
3922 break;
3923 case DW_OP_constu:
3924 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3925 break;
3926 case DW_OP_consts:
3927 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3928 break;
3929 case DW_OP_pick:
3930 size += 1;
3931 break;
3932 case DW_OP_plus_uconst:
3933 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3934 break;
3935 case DW_OP_skip:
3936 case DW_OP_bra:
3937 size += 2;
3938 break;
3939 case DW_OP_breg0:
3940 case DW_OP_breg1:
3941 case DW_OP_breg2:
3942 case DW_OP_breg3:
3943 case DW_OP_breg4:
3944 case DW_OP_breg5:
3945 case DW_OP_breg6:
3946 case DW_OP_breg7:
3947 case DW_OP_breg8:
3948 case DW_OP_breg9:
3949 case DW_OP_breg10:
3950 case DW_OP_breg11:
3951 case DW_OP_breg12:
3952 case DW_OP_breg13:
3953 case DW_OP_breg14:
3954 case DW_OP_breg15:
3955 case DW_OP_breg16:
3956 case DW_OP_breg17:
3957 case DW_OP_breg18:
3958 case DW_OP_breg19:
3959 case DW_OP_breg20:
3960 case DW_OP_breg21:
3961 case DW_OP_breg22:
3962 case DW_OP_breg23:
3963 case DW_OP_breg24:
3964 case DW_OP_breg25:
3965 case DW_OP_breg26:
3966 case DW_OP_breg27:
3967 case DW_OP_breg28:
3968 case DW_OP_breg29:
3969 case DW_OP_breg30:
3970 case DW_OP_breg31:
3971 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3972 break;
3973 case DW_OP_regx:
3974 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3975 break;
3976 case DW_OP_fbreg:
3977 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3978 break;
3979 case DW_OP_bregx:
3980 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3981 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
3982 break;
3983 case DW_OP_piece:
3984 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3985 break;
3986 case DW_OP_deref_size:
3987 case DW_OP_xderef_size:
3988 size += 1;
3989 break;
3990 case DW_OP_call2:
3991 size += 2;
3992 break;
3993 case DW_OP_call4:
3994 size += 4;
3995 break;
3996 case DW_OP_call_ref:
3997 size += DWARF2_ADDR_SIZE;
3998 break;
3999 default:
4000 break;
4003 return size;
4006 /* Return the size of a series of location descriptors. */
4008 static unsigned long
4009 size_of_locs (dw_loc_descr_ref loc)
4011 dw_loc_descr_ref l;
4012 unsigned long size;
4014 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
4015 field, to avoid writing to a PCH file. */
4016 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4018 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
4019 break;
4020 size += size_of_loc_descr (l);
4022 if (! l)
4023 return size;
4025 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4027 l->dw_loc_addr = size;
4028 size += size_of_loc_descr (l);
4031 return size;
4034 /* Output location description stack opcode's operands (if any). */
4036 static void
4037 output_loc_operands (dw_loc_descr_ref loc)
4039 dw_val_ref val1 = &loc->dw_loc_oprnd1;
4040 dw_val_ref val2 = &loc->dw_loc_oprnd2;
4042 switch (loc->dw_loc_opc)
4044 #ifdef DWARF2_DEBUGGING_INFO
4045 case DW_OP_addr:
4046 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
4047 break;
4048 case DW_OP_const2u:
4049 case DW_OP_const2s:
4050 dw2_asm_output_data (2, val1->v.val_int, NULL);
4051 break;
4052 case DW_OP_const4u:
4053 case DW_OP_const4s:
4054 dw2_asm_output_data (4, val1->v.val_int, NULL);
4055 break;
4056 case DW_OP_const8u:
4057 case DW_OP_const8s:
4058 gcc_assert (HOST_BITS_PER_LONG >= 64);
4059 dw2_asm_output_data (8, val1->v.val_int, NULL);
4060 break;
4061 case DW_OP_skip:
4062 case DW_OP_bra:
4064 int offset;
4066 gcc_assert (val1->val_class == dw_val_class_loc);
4067 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4069 dw2_asm_output_data (2, offset, NULL);
4071 break;
4072 #else
4073 case DW_OP_addr:
4074 case DW_OP_const2u:
4075 case DW_OP_const2s:
4076 case DW_OP_const4u:
4077 case DW_OP_const4s:
4078 case DW_OP_const8u:
4079 case DW_OP_const8s:
4080 case DW_OP_skip:
4081 case DW_OP_bra:
4082 /* We currently don't make any attempt to make sure these are
4083 aligned properly like we do for the main unwind info, so
4084 don't support emitting things larger than a byte if we're
4085 only doing unwinding. */
4086 gcc_unreachable ();
4087 #endif
4088 case DW_OP_const1u:
4089 case DW_OP_const1s:
4090 dw2_asm_output_data (1, val1->v.val_int, NULL);
4091 break;
4092 case DW_OP_constu:
4093 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4094 break;
4095 case DW_OP_consts:
4096 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4097 break;
4098 case DW_OP_pick:
4099 dw2_asm_output_data (1, val1->v.val_int, NULL);
4100 break;
4101 case DW_OP_plus_uconst:
4102 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4103 break;
4104 case DW_OP_breg0:
4105 case DW_OP_breg1:
4106 case DW_OP_breg2:
4107 case DW_OP_breg3:
4108 case DW_OP_breg4:
4109 case DW_OP_breg5:
4110 case DW_OP_breg6:
4111 case DW_OP_breg7:
4112 case DW_OP_breg8:
4113 case DW_OP_breg9:
4114 case DW_OP_breg10:
4115 case DW_OP_breg11:
4116 case DW_OP_breg12:
4117 case DW_OP_breg13:
4118 case DW_OP_breg14:
4119 case DW_OP_breg15:
4120 case DW_OP_breg16:
4121 case DW_OP_breg17:
4122 case DW_OP_breg18:
4123 case DW_OP_breg19:
4124 case DW_OP_breg20:
4125 case DW_OP_breg21:
4126 case DW_OP_breg22:
4127 case DW_OP_breg23:
4128 case DW_OP_breg24:
4129 case DW_OP_breg25:
4130 case DW_OP_breg26:
4131 case DW_OP_breg27:
4132 case DW_OP_breg28:
4133 case DW_OP_breg29:
4134 case DW_OP_breg30:
4135 case DW_OP_breg31:
4136 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4137 break;
4138 case DW_OP_regx:
4139 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4140 break;
4141 case DW_OP_fbreg:
4142 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4143 break;
4144 case DW_OP_bregx:
4145 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4146 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
4147 break;
4148 case DW_OP_piece:
4149 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4150 break;
4151 case DW_OP_deref_size:
4152 case DW_OP_xderef_size:
4153 dw2_asm_output_data (1, val1->v.val_int, NULL);
4154 break;
4156 case INTERNAL_DW_OP_tls_addr:
4157 if (targetm.asm_out.output_dwarf_dtprel)
4159 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
4160 DWARF2_ADDR_SIZE,
4161 val1->v.val_addr);
4162 fputc ('\n', asm_out_file);
4164 else
4165 gcc_unreachable ();
4166 break;
4168 default:
4169 /* Other codes have no operands. */
4170 break;
4174 /* Output a sequence of location operations. */
4176 static void
4177 output_loc_sequence (dw_loc_descr_ref loc)
4179 for (; loc != NULL; loc = loc->dw_loc_next)
4181 /* Output the opcode. */
4182 dw2_asm_output_data (1, loc->dw_loc_opc,
4183 "%s", dwarf_stack_op_name (loc->dw_loc_opc));
4185 /* Output the operand(s) (if any). */
4186 output_loc_operands (loc);
4190 /* Output location description stack opcode's operands (if any).
4191 The output is single bytes on a line, suitable for .cfi_escape. */
4193 static void
4194 output_loc_operands_raw (dw_loc_descr_ref loc)
4196 dw_val_ref val1 = &loc->dw_loc_oprnd1;
4197 dw_val_ref val2 = &loc->dw_loc_oprnd2;
4199 switch (loc->dw_loc_opc)
4201 case DW_OP_addr:
4202 /* We cannot output addresses in .cfi_escape, only bytes. */
4203 gcc_unreachable ();
4205 case DW_OP_const1u:
4206 case DW_OP_const1s:
4207 case DW_OP_pick:
4208 case DW_OP_deref_size:
4209 case DW_OP_xderef_size:
4210 fputc (',', asm_out_file);
4211 dw2_asm_output_data_raw (1, val1->v.val_int);
4212 break;
4214 case DW_OP_const2u:
4215 case DW_OP_const2s:
4216 fputc (',', asm_out_file);
4217 dw2_asm_output_data_raw (2, val1->v.val_int);
4218 break;
4220 case DW_OP_const4u:
4221 case DW_OP_const4s:
4222 fputc (',', asm_out_file);
4223 dw2_asm_output_data_raw (4, val1->v.val_int);
4224 break;
4226 case DW_OP_const8u:
4227 case DW_OP_const8s:
4228 gcc_assert (HOST_BITS_PER_LONG >= 64);
4229 fputc (',', asm_out_file);
4230 dw2_asm_output_data_raw (8, val1->v.val_int);
4231 break;
4233 case DW_OP_skip:
4234 case DW_OP_bra:
4236 int offset;
4238 gcc_assert (val1->val_class == dw_val_class_loc);
4239 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4241 fputc (',', asm_out_file);
4242 dw2_asm_output_data_raw (2, offset);
4244 break;
4246 case DW_OP_constu:
4247 case DW_OP_plus_uconst:
4248 case DW_OP_regx:
4249 case DW_OP_piece:
4250 fputc (',', asm_out_file);
4251 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
4252 break;
4254 case DW_OP_consts:
4255 case DW_OP_breg0:
4256 case DW_OP_breg1:
4257 case DW_OP_breg2:
4258 case DW_OP_breg3:
4259 case DW_OP_breg4:
4260 case DW_OP_breg5:
4261 case DW_OP_breg6:
4262 case DW_OP_breg7:
4263 case DW_OP_breg8:
4264 case DW_OP_breg9:
4265 case DW_OP_breg10:
4266 case DW_OP_breg11:
4267 case DW_OP_breg12:
4268 case DW_OP_breg13:
4269 case DW_OP_breg14:
4270 case DW_OP_breg15:
4271 case DW_OP_breg16:
4272 case DW_OP_breg17:
4273 case DW_OP_breg18:
4274 case DW_OP_breg19:
4275 case DW_OP_breg20:
4276 case DW_OP_breg21:
4277 case DW_OP_breg22:
4278 case DW_OP_breg23:
4279 case DW_OP_breg24:
4280 case DW_OP_breg25:
4281 case DW_OP_breg26:
4282 case DW_OP_breg27:
4283 case DW_OP_breg28:
4284 case DW_OP_breg29:
4285 case DW_OP_breg30:
4286 case DW_OP_breg31:
4287 case DW_OP_fbreg:
4288 fputc (',', asm_out_file);
4289 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
4290 break;
4292 case DW_OP_bregx:
4293 fputc (',', asm_out_file);
4294 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
4295 fputc (',', asm_out_file);
4296 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
4297 break;
4299 case INTERNAL_DW_OP_tls_addr:
4300 gcc_unreachable ();
4302 default:
4303 /* Other codes have no operands. */
4304 break;
4308 static void
4309 output_loc_sequence_raw (dw_loc_descr_ref loc)
4311 while (1)
4313 /* Output the opcode. */
4314 fprintf (asm_out_file, "0x%x", loc->dw_loc_opc);
4315 output_loc_operands_raw (loc);
4317 if (!loc->dw_loc_next)
4318 break;
4319 loc = loc->dw_loc_next;
4321 fputc (',', asm_out_file);
4325 /* This routine will generate the correct assembly data for a location
4326 description based on a cfi entry with a complex address. */
4328 static void
4329 output_cfa_loc (dw_cfi_ref cfi)
4331 dw_loc_descr_ref loc;
4332 unsigned long size;
4334 if (cfi->dw_cfi_opc == DW_CFA_expression)
4335 dw2_asm_output_data (1, cfi->dw_cfi_oprnd2.dw_cfi_reg_num, NULL);
4337 /* Output the size of the block. */
4338 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
4339 size = size_of_locs (loc);
4340 dw2_asm_output_data_uleb128 (size, NULL);
4342 /* Now output the operations themselves. */
4343 output_loc_sequence (loc);
4346 /* Similar, but used for .cfi_escape. */
4348 static void
4349 output_cfa_loc_raw (dw_cfi_ref cfi)
4351 dw_loc_descr_ref loc;
4352 unsigned long size;
4354 if (cfi->dw_cfi_opc == DW_CFA_expression)
4355 fprintf (asm_out_file, "0x%x,", cfi->dw_cfi_oprnd2.dw_cfi_reg_num);
4357 /* Output the size of the block. */
4358 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
4359 size = size_of_locs (loc);
4360 dw2_asm_output_data_uleb128_raw (size);
4361 fputc (',', asm_out_file);
4363 /* Now output the operations themselves. */
4364 output_loc_sequence_raw (loc);
4367 /* This function builds a dwarf location descriptor sequence from a
4368 dw_cfa_location, adding the given OFFSET to the result of the
4369 expression. */
4371 static struct dw_loc_descr_struct *
4372 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
4374 struct dw_loc_descr_struct *head, *tmp;
4376 offset += cfa->offset;
4378 if (cfa->indirect)
4380 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
4381 head->dw_loc_oprnd1.val_class = dw_val_class_const;
4382 tmp = new_loc_descr (DW_OP_deref, 0, 0);
4383 add_loc_descr (&head, tmp);
4384 if (offset != 0)
4386 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4387 add_loc_descr (&head, tmp);
4390 else
4391 head = new_reg_loc_descr (cfa->reg, offset);
4393 return head;
4396 /* This function builds a dwarf location descriptor sequence for
4397 the address at OFFSET from the CFA when stack is aligned to
4398 ALIGNMENT byte. */
4400 static struct dw_loc_descr_struct *
4401 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
4403 struct dw_loc_descr_struct *head;
4404 unsigned int dwarf_fp
4405 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
4407 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
4408 if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
4410 head = new_reg_loc_descr (dwarf_fp, 0);
4411 add_loc_descr (&head, int_loc_descriptor (alignment));
4412 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
4414 add_loc_descr (&head, int_loc_descriptor (offset));
4415 add_loc_descr (&head, new_loc_descr (DW_OP_plus, 0, 0));
4417 else
4418 head = new_reg_loc_descr (dwarf_fp, offset);
4419 return head;
4422 /* This function fills in aa dw_cfa_location structure from a dwarf location
4423 descriptor sequence. */
4425 static void
4426 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
4428 struct dw_loc_descr_struct *ptr;
4429 cfa->offset = 0;
4430 cfa->base_offset = 0;
4431 cfa->indirect = 0;
4432 cfa->reg = -1;
4434 for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
4436 enum dwarf_location_atom op = ptr->dw_loc_opc;
4438 switch (op)
4440 case DW_OP_reg0:
4441 case DW_OP_reg1:
4442 case DW_OP_reg2:
4443 case DW_OP_reg3:
4444 case DW_OP_reg4:
4445 case DW_OP_reg5:
4446 case DW_OP_reg6:
4447 case DW_OP_reg7:
4448 case DW_OP_reg8:
4449 case DW_OP_reg9:
4450 case DW_OP_reg10:
4451 case DW_OP_reg11:
4452 case DW_OP_reg12:
4453 case DW_OP_reg13:
4454 case DW_OP_reg14:
4455 case DW_OP_reg15:
4456 case DW_OP_reg16:
4457 case DW_OP_reg17:
4458 case DW_OP_reg18:
4459 case DW_OP_reg19:
4460 case DW_OP_reg20:
4461 case DW_OP_reg21:
4462 case DW_OP_reg22:
4463 case DW_OP_reg23:
4464 case DW_OP_reg24:
4465 case DW_OP_reg25:
4466 case DW_OP_reg26:
4467 case DW_OP_reg27:
4468 case DW_OP_reg28:
4469 case DW_OP_reg29:
4470 case DW_OP_reg30:
4471 case DW_OP_reg31:
4472 cfa->reg = op - DW_OP_reg0;
4473 break;
4474 case DW_OP_regx:
4475 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
4476 break;
4477 case DW_OP_breg0:
4478 case DW_OP_breg1:
4479 case DW_OP_breg2:
4480 case DW_OP_breg3:
4481 case DW_OP_breg4:
4482 case DW_OP_breg5:
4483 case DW_OP_breg6:
4484 case DW_OP_breg7:
4485 case DW_OP_breg8:
4486 case DW_OP_breg9:
4487 case DW_OP_breg10:
4488 case DW_OP_breg11:
4489 case DW_OP_breg12:
4490 case DW_OP_breg13:
4491 case DW_OP_breg14:
4492 case DW_OP_breg15:
4493 case DW_OP_breg16:
4494 case DW_OP_breg17:
4495 case DW_OP_breg18:
4496 case DW_OP_breg19:
4497 case DW_OP_breg20:
4498 case DW_OP_breg21:
4499 case DW_OP_breg22:
4500 case DW_OP_breg23:
4501 case DW_OP_breg24:
4502 case DW_OP_breg25:
4503 case DW_OP_breg26:
4504 case DW_OP_breg27:
4505 case DW_OP_breg28:
4506 case DW_OP_breg29:
4507 case DW_OP_breg30:
4508 case DW_OP_breg31:
4509 cfa->reg = op - DW_OP_breg0;
4510 cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
4511 break;
4512 case DW_OP_bregx:
4513 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
4514 cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
4515 break;
4516 case DW_OP_deref:
4517 cfa->indirect = 1;
4518 break;
4519 case DW_OP_plus_uconst:
4520 cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
4521 break;
4522 default:
4523 internal_error ("DW_LOC_OP %s not implemented",
4524 dwarf_stack_op_name (ptr->dw_loc_opc));
4528 #endif /* .debug_frame support */
4530 /* And now, the support for symbolic debugging information. */
4531 #ifdef DWARF2_DEBUGGING_INFO
4533 /* .debug_str support. */
4534 static int output_indirect_string (void **, void *);
4536 static void dwarf2out_init (const char *);
4537 static void dwarf2out_finish (const char *);
4538 static void dwarf2out_define (unsigned int, const char *);
4539 static void dwarf2out_undef (unsigned int, const char *);
4540 static void dwarf2out_start_source_file (unsigned, const char *);
4541 static void dwarf2out_end_source_file (unsigned);
4542 static void dwarf2out_begin_block (unsigned, unsigned);
4543 static void dwarf2out_end_block (unsigned, unsigned);
4544 static bool dwarf2out_ignore_block (const_tree);
4545 static void dwarf2out_global_decl (tree);
4546 static void dwarf2out_type_decl (tree, int);
4547 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
4548 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
4549 dw_die_ref);
4550 static void dwarf2out_abstract_function (tree);
4551 static void dwarf2out_var_location (rtx);
4552 static void dwarf2out_begin_function (tree);
4553 static void dwarf2out_set_name (tree, tree);
4555 /* The debug hooks structure. */
4557 const struct gcc_debug_hooks dwarf2_debug_hooks =
4559 dwarf2out_init,
4560 dwarf2out_finish,
4561 dwarf2out_define,
4562 dwarf2out_undef,
4563 dwarf2out_start_source_file,
4564 dwarf2out_end_source_file,
4565 dwarf2out_begin_block,
4566 dwarf2out_end_block,
4567 dwarf2out_ignore_block,
4568 dwarf2out_source_line,
4569 dwarf2out_begin_prologue,
4570 debug_nothing_int_charstar, /* end_prologue */
4571 dwarf2out_end_epilogue,
4572 dwarf2out_begin_function,
4573 debug_nothing_int, /* end_function */
4574 dwarf2out_decl, /* function_decl */
4575 dwarf2out_global_decl,
4576 dwarf2out_type_decl, /* type_decl */
4577 dwarf2out_imported_module_or_decl,
4578 debug_nothing_tree, /* deferred_inline_function */
4579 /* The DWARF 2 backend tries to reduce debugging bloat by not
4580 emitting the abstract description of inline functions until
4581 something tries to reference them. */
4582 dwarf2out_abstract_function, /* outlining_inline_function */
4583 debug_nothing_rtx, /* label */
4584 debug_nothing_int, /* handle_pch */
4585 dwarf2out_var_location,
4586 dwarf2out_switch_text_section,
4587 dwarf2out_set_name,
4588 1 /* start_end_main_source_file */
4590 #endif
4592 /* NOTE: In the comments in this file, many references are made to
4593 "Debugging Information Entries". This term is abbreviated as `DIE'
4594 throughout the remainder of this file. */
4596 /* An internal representation of the DWARF output is built, and then
4597 walked to generate the DWARF debugging info. The walk of the internal
4598 representation is done after the entire program has been compiled.
4599 The types below are used to describe the internal representation. */
4601 /* Various DIE's use offsets relative to the beginning of the
4602 .debug_info section to refer to each other. */
4604 typedef long int dw_offset;
4606 /* Define typedefs here to avoid circular dependencies. */
4608 typedef struct dw_attr_struct *dw_attr_ref;
4609 typedef struct dw_line_info_struct *dw_line_info_ref;
4610 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
4611 typedef struct pubname_struct *pubname_ref;
4612 typedef struct dw_ranges_struct *dw_ranges_ref;
4613 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
4615 /* Each entry in the line_info_table maintains the file and
4616 line number associated with the label generated for that
4617 entry. The label gives the PC value associated with
4618 the line number entry. */
4620 typedef struct dw_line_info_struct GTY(())
4622 unsigned long dw_file_num;
4623 unsigned long dw_line_num;
4625 dw_line_info_entry;
4627 /* Line information for functions in separate sections; each one gets its
4628 own sequence. */
4629 typedef struct dw_separate_line_info_struct GTY(())
4631 unsigned long dw_file_num;
4632 unsigned long dw_line_num;
4633 unsigned long function;
4635 dw_separate_line_info_entry;
4637 /* Each DIE attribute has a field specifying the attribute kind,
4638 a link to the next attribute in the chain, and an attribute value.
4639 Attributes are typically linked below the DIE they modify. */
4641 typedef struct dw_attr_struct GTY(())
4643 enum dwarf_attribute dw_attr;
4644 dw_val_node dw_attr_val;
4646 dw_attr_node;
4648 DEF_VEC_O(dw_attr_node);
4649 DEF_VEC_ALLOC_O(dw_attr_node,gc);
4651 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
4652 The children of each node form a circular list linked by
4653 die_sib. die_child points to the node *before* the "first" child node. */
4655 typedef struct die_struct GTY((chain_circular ("%h.die_sib")))
4657 enum dwarf_tag die_tag;
4658 char *die_symbol;
4659 VEC(dw_attr_node,gc) * die_attr;
4660 dw_die_ref die_parent;
4661 dw_die_ref die_child;
4662 dw_die_ref die_sib;
4663 dw_die_ref die_definition; /* ref from a specification to its definition */
4664 dw_offset die_offset;
4665 unsigned long die_abbrev;
4666 int die_mark;
4667 /* Die is used and must not be pruned as unused. */
4668 int die_perennial_p;
4669 unsigned int decl_id;
4671 die_node;
4673 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
4674 #define FOR_EACH_CHILD(die, c, expr) do { \
4675 c = die->die_child; \
4676 if (c) do { \
4677 c = c->die_sib; \
4678 expr; \
4679 } while (c != die->die_child); \
4680 } while (0)
4682 /* The pubname structure */
4684 typedef struct pubname_struct GTY(())
4686 dw_die_ref die;
4687 const char *name;
4689 pubname_entry;
4691 DEF_VEC_O(pubname_entry);
4692 DEF_VEC_ALLOC_O(pubname_entry, gc);
4694 struct dw_ranges_struct GTY(())
4696 /* If this is positive, it's a block number, otherwise it's a
4697 bitwise-negated index into dw_ranges_by_label. */
4698 int num;
4701 struct dw_ranges_by_label_struct GTY(())
4703 const char *begin;
4704 const char *end;
4707 /* The limbo die list structure. */
4708 typedef struct limbo_die_struct GTY(())
4710 dw_die_ref die;
4711 tree created_for;
4712 struct limbo_die_struct *next;
4714 limbo_die_node;
4716 /* How to start an assembler comment. */
4717 #ifndef ASM_COMMENT_START
4718 #define ASM_COMMENT_START ";#"
4719 #endif
4721 /* Define a macro which returns nonzero for a TYPE_DECL which was
4722 implicitly generated for a tagged type.
4724 Note that unlike the gcc front end (which generates a NULL named
4725 TYPE_DECL node for each complete tagged type, each array type, and
4726 each function type node created) the g++ front end generates a
4727 _named_ TYPE_DECL node for each tagged type node created.
4728 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
4729 generate a DW_TAG_typedef DIE for them. */
4731 #define TYPE_DECL_IS_STUB(decl) \
4732 (DECL_NAME (decl) == NULL_TREE \
4733 || (DECL_ARTIFICIAL (decl) \
4734 && is_tagged_type (TREE_TYPE (decl)) \
4735 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
4736 /* This is necessary for stub decls that \
4737 appear in nested inline functions. */ \
4738 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
4739 && (decl_ultimate_origin (decl) \
4740 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
4742 /* Information concerning the compilation unit's programming
4743 language, and compiler version. */
4745 /* Fixed size portion of the DWARF compilation unit header. */
4746 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
4747 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
4749 /* Fixed size portion of public names info. */
4750 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
4752 /* Fixed size portion of the address range info. */
4753 #define DWARF_ARANGES_HEADER_SIZE \
4754 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
4755 DWARF2_ADDR_SIZE * 2) \
4756 - DWARF_INITIAL_LENGTH_SIZE)
4758 /* Size of padding portion in the address range info. It must be
4759 aligned to twice the pointer size. */
4760 #define DWARF_ARANGES_PAD_SIZE \
4761 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
4762 DWARF2_ADDR_SIZE * 2) \
4763 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
4765 /* Use assembler line directives if available. */
4766 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
4767 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
4768 #define DWARF2_ASM_LINE_DEBUG_INFO 1
4769 #else
4770 #define DWARF2_ASM_LINE_DEBUG_INFO 0
4771 #endif
4772 #endif
4774 /* Minimum line offset in a special line info. opcode.
4775 This value was chosen to give a reasonable range of values. */
4776 #define DWARF_LINE_BASE -10
4778 /* First special line opcode - leave room for the standard opcodes. */
4779 #define DWARF_LINE_OPCODE_BASE 10
4781 /* Range of line offsets in a special line info. opcode. */
4782 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
4784 /* Flag that indicates the initial value of the is_stmt_start flag.
4785 In the present implementation, we do not mark any lines as
4786 the beginning of a source statement, because that information
4787 is not made available by the GCC front-end. */
4788 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
4790 #ifdef DWARF2_DEBUGGING_INFO
4791 /* This location is used by calc_die_sizes() to keep track
4792 the offset of each DIE within the .debug_info section. */
4793 static unsigned long next_die_offset;
4794 #endif
4796 /* Record the root of the DIE's built for the current compilation unit. */
4797 static GTY(()) dw_die_ref comp_unit_die;
4799 /* A list of DIEs with a NULL parent waiting to be relocated. */
4800 static GTY(()) limbo_die_node *limbo_die_list;
4802 /* Filenames referenced by this compilation unit. */
4803 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
4805 /* A hash table of references to DIE's that describe declarations.
4806 The key is a DECL_UID() which is a unique number identifying each decl. */
4807 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
4809 /* A hash table of references to DIE's that describe COMMON blocks.
4810 The key is DECL_UID() ^ die_parent. */
4811 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
4813 /* Node of the variable location list. */
4814 struct var_loc_node GTY ((chain_next ("%h.next")))
4816 rtx GTY (()) var_loc_note;
4817 const char * GTY (()) label;
4818 const char * GTY (()) section_label;
4819 struct var_loc_node * GTY (()) next;
4822 /* Variable location list. */
4823 struct var_loc_list_def GTY (())
4825 struct var_loc_node * GTY (()) first;
4827 /* Do not mark the last element of the chained list because
4828 it is marked through the chain. */
4829 struct var_loc_node * GTY ((skip ("%h"))) last;
4831 /* DECL_UID of the variable decl. */
4832 unsigned int decl_id;
4834 typedef struct var_loc_list_def var_loc_list;
4837 /* Table of decl location linked lists. */
4838 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
4840 /* A pointer to the base of a list of references to DIE's that
4841 are uniquely identified by their tag, presence/absence of
4842 children DIE's, and list of attribute/value pairs. */
4843 static GTY((length ("abbrev_die_table_allocated")))
4844 dw_die_ref *abbrev_die_table;
4846 /* Number of elements currently allocated for abbrev_die_table. */
4847 static GTY(()) unsigned abbrev_die_table_allocated;
4849 /* Number of elements in type_die_table currently in use. */
4850 static GTY(()) unsigned abbrev_die_table_in_use;
4852 /* Size (in elements) of increments by which we may expand the
4853 abbrev_die_table. */
4854 #define ABBREV_DIE_TABLE_INCREMENT 256
4856 /* A pointer to the base of a table that contains line information
4857 for each source code line in .text in the compilation unit. */
4858 static GTY((length ("line_info_table_allocated")))
4859 dw_line_info_ref line_info_table;
4861 /* Number of elements currently allocated for line_info_table. */
4862 static GTY(()) unsigned line_info_table_allocated;
4864 /* Number of elements in line_info_table currently in use. */
4865 static GTY(()) unsigned line_info_table_in_use;
4867 /* A pointer to the base of a table that contains line information
4868 for each source code line outside of .text in the compilation unit. */
4869 static GTY ((length ("separate_line_info_table_allocated")))
4870 dw_separate_line_info_ref separate_line_info_table;
4872 /* Number of elements currently allocated for separate_line_info_table. */
4873 static GTY(()) unsigned separate_line_info_table_allocated;
4875 /* Number of elements in separate_line_info_table currently in use. */
4876 static GTY(()) unsigned separate_line_info_table_in_use;
4878 /* Size (in elements) of increments by which we may expand the
4879 line_info_table. */
4880 #define LINE_INFO_TABLE_INCREMENT 1024
4882 /* A pointer to the base of a table that contains a list of publicly
4883 accessible names. */
4884 static GTY (()) VEC (pubname_entry, gc) * pubname_table;
4886 /* A pointer to the base of a table that contains a list of publicly
4887 accessible types. */
4888 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
4890 /* Array of dies for which we should generate .debug_arange info. */
4891 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
4893 /* Number of elements currently allocated for arange_table. */
4894 static GTY(()) unsigned arange_table_allocated;
4896 /* Number of elements in arange_table currently in use. */
4897 static GTY(()) unsigned arange_table_in_use;
4899 /* Size (in elements) of increments by which we may expand the
4900 arange_table. */
4901 #define ARANGE_TABLE_INCREMENT 64
4903 /* Array of dies for which we should generate .debug_ranges info. */
4904 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
4906 /* Number of elements currently allocated for ranges_table. */
4907 static GTY(()) unsigned ranges_table_allocated;
4909 /* Number of elements in ranges_table currently in use. */
4910 static GTY(()) unsigned ranges_table_in_use;
4912 /* Array of pairs of labels referenced in ranges_table. */
4913 static GTY ((length ("ranges_by_label_allocated")))
4914 dw_ranges_by_label_ref ranges_by_label;
4916 /* Number of elements currently allocated for ranges_by_label. */
4917 static GTY(()) unsigned ranges_by_label_allocated;
4919 /* Number of elements in ranges_by_label currently in use. */
4920 static GTY(()) unsigned ranges_by_label_in_use;
4922 /* Size (in elements) of increments by which we may expand the
4923 ranges_table. */
4924 #define RANGES_TABLE_INCREMENT 64
4926 /* Whether we have location lists that need outputting */
4927 static GTY(()) bool have_location_lists;
4929 /* Unique label counter. */
4930 static GTY(()) unsigned int loclabel_num;
4932 #ifdef DWARF2_DEBUGGING_INFO
4933 /* Record whether the function being analyzed contains inlined functions. */
4934 static int current_function_has_inlines;
4935 #endif
4936 #if 0 && defined (MIPS_DEBUGGING_INFO)
4937 static int comp_unit_has_inlines;
4938 #endif
4940 /* The last file entry emitted by maybe_emit_file(). */
4941 static GTY(()) struct dwarf_file_data * last_emitted_file;
4943 /* Number of internal labels generated by gen_internal_sym(). */
4944 static GTY(()) int label_num;
4946 /* Cached result of previous call to lookup_filename. */
4947 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
4949 #ifdef DWARF2_DEBUGGING_INFO
4951 /* Offset from the "steady-state frame pointer" to the frame base,
4952 within the current function. */
4953 static HOST_WIDE_INT frame_pointer_fb_offset;
4955 /* Forward declarations for functions defined in this file. */
4957 static int is_pseudo_reg (const_rtx);
4958 static tree type_main_variant (tree);
4959 static int is_tagged_type (const_tree);
4960 static const char *dwarf_tag_name (unsigned);
4961 static const char *dwarf_attr_name (unsigned);
4962 static const char *dwarf_form_name (unsigned);
4963 static tree decl_ultimate_origin (const_tree);
4964 static tree decl_class_context (tree);
4965 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
4966 static inline enum dw_val_class AT_class (dw_attr_ref);
4967 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
4968 static inline unsigned AT_flag (dw_attr_ref);
4969 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
4970 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
4971 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
4972 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
4973 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
4974 unsigned long);
4975 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
4976 unsigned int, unsigned char *);
4977 static hashval_t debug_str_do_hash (const void *);
4978 static int debug_str_eq (const void *, const void *);
4979 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
4980 static inline const char *AT_string (dw_attr_ref);
4981 static int AT_string_form (dw_attr_ref);
4982 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
4983 static void add_AT_specification (dw_die_ref, dw_die_ref);
4984 static inline dw_die_ref AT_ref (dw_attr_ref);
4985 static inline int AT_ref_external (dw_attr_ref);
4986 static inline void set_AT_ref_external (dw_attr_ref, int);
4987 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
4988 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
4989 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
4990 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
4991 dw_loc_list_ref);
4992 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
4993 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
4994 static inline rtx AT_addr (dw_attr_ref);
4995 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
4996 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
4997 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
4998 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
4999 unsigned HOST_WIDE_INT);
5000 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
5001 unsigned long);
5002 static inline const char *AT_lbl (dw_attr_ref);
5003 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
5004 static const char *get_AT_low_pc (dw_die_ref);
5005 static const char *get_AT_hi_pc (dw_die_ref);
5006 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
5007 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
5008 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
5009 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
5010 static bool is_c_family (void);
5011 static bool is_cxx (void);
5012 static bool is_java (void);
5013 static bool is_fortran (void);
5014 static bool is_ada (void);
5015 static void remove_AT (dw_die_ref, enum dwarf_attribute);
5016 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
5017 static void add_child_die (dw_die_ref, dw_die_ref);
5018 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
5019 static dw_die_ref lookup_type_die (tree);
5020 static void equate_type_number_to_die (tree, dw_die_ref);
5021 static hashval_t decl_die_table_hash (const void *);
5022 static int decl_die_table_eq (const void *, const void *);
5023 static dw_die_ref lookup_decl_die (tree);
5024 static hashval_t common_block_die_table_hash (const void *);
5025 static int common_block_die_table_eq (const void *, const void *);
5026 static hashval_t decl_loc_table_hash (const void *);
5027 static int decl_loc_table_eq (const void *, const void *);
5028 static var_loc_list *lookup_decl_loc (const_tree);
5029 static void equate_decl_number_to_die (tree, dw_die_ref);
5030 static void add_var_loc_to_decl (tree, struct var_loc_node *);
5031 static void print_spaces (FILE *);
5032 static void print_die (dw_die_ref, FILE *);
5033 static void print_dwarf_line_table (FILE *);
5034 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
5035 static dw_die_ref pop_compile_unit (dw_die_ref);
5036 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
5037 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
5038 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
5039 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
5040 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
5041 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
5042 static int same_die_p (dw_die_ref, dw_die_ref, int *);
5043 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
5044 static void compute_section_prefix (dw_die_ref);
5045 static int is_type_die (dw_die_ref);
5046 static int is_comdat_die (dw_die_ref);
5047 static int is_symbol_die (dw_die_ref);
5048 static void assign_symbol_names (dw_die_ref);
5049 static void break_out_includes (dw_die_ref);
5050 static hashval_t htab_cu_hash (const void *);
5051 static int htab_cu_eq (const void *, const void *);
5052 static void htab_cu_del (void *);
5053 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
5054 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
5055 static void add_sibling_attributes (dw_die_ref);
5056 static void build_abbrev_table (dw_die_ref);
5057 static void output_location_lists (dw_die_ref);
5058 static int constant_size (unsigned HOST_WIDE_INT);
5059 static unsigned long size_of_die (dw_die_ref);
5060 static void calc_die_sizes (dw_die_ref);
5061 static void mark_dies (dw_die_ref);
5062 static void unmark_dies (dw_die_ref);
5063 static void unmark_all_dies (dw_die_ref);
5064 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
5065 static unsigned long size_of_aranges (void);
5066 static enum dwarf_form value_format (dw_attr_ref);
5067 static void output_value_format (dw_attr_ref);
5068 static void output_abbrev_section (void);
5069 static void output_die_symbol (dw_die_ref);
5070 static void output_die (dw_die_ref);
5071 static void output_compilation_unit_header (void);
5072 static void output_comp_unit (dw_die_ref, int);
5073 static const char *dwarf2_name (tree, int);
5074 static void add_pubname (tree, dw_die_ref);
5075 static void add_pubname_string (const char *, dw_die_ref);
5076 static void add_pubtype (tree, dw_die_ref);
5077 static void output_pubnames (VEC (pubname_entry,gc) *);
5078 static void add_arange (tree, dw_die_ref);
5079 static void output_aranges (void);
5080 static unsigned int add_ranges_num (int);
5081 static unsigned int add_ranges (const_tree);
5082 static unsigned int add_ranges_by_labels (const char *, const char *);
5083 static void output_ranges (void);
5084 static void output_line_info (void);
5085 static void output_file_names (void);
5086 static dw_die_ref base_type_die (tree);
5087 static int is_base_type (tree);
5088 static bool is_subrange_type (const_tree);
5089 static dw_die_ref subrange_type_die (tree, dw_die_ref);
5090 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
5091 static int type_is_enum (const_tree);
5092 static unsigned int dbx_reg_number (const_rtx);
5093 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
5094 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
5095 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
5096 enum var_init_status);
5097 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
5098 enum var_init_status);
5099 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
5100 enum var_init_status);
5101 static int is_based_loc (const_rtx);
5102 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
5103 enum var_init_status);
5104 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
5105 enum var_init_status);
5106 static dw_loc_descr_ref loc_descriptor (rtx, enum var_init_status);
5107 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
5108 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
5109 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
5110 static tree field_type (const_tree);
5111 static unsigned int simple_type_align_in_bits (const_tree);
5112 static unsigned int simple_decl_align_in_bits (const_tree);
5113 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
5114 static HOST_WIDE_INT field_byte_offset (const_tree);
5115 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
5116 dw_loc_descr_ref);
5117 static void add_data_member_location_attribute (dw_die_ref, tree);
5118 static void add_const_value_attribute (dw_die_ref, rtx);
5119 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
5120 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
5121 static void insert_float (const_rtx, unsigned char *);
5122 static rtx rtl_for_decl_location (tree);
5123 static void add_location_or_const_value_attribute (dw_die_ref, tree,
5124 enum dwarf_attribute);
5125 static void tree_add_const_value_attribute (dw_die_ref, tree);
5126 static void add_name_attribute (dw_die_ref, const char *);
5127 static void add_comp_dir_attribute (dw_die_ref);
5128 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
5129 static void add_subscript_info (dw_die_ref, tree, bool);
5130 static void add_byte_size_attribute (dw_die_ref, tree);
5131 static void add_bit_offset_attribute (dw_die_ref, tree);
5132 static void add_bit_size_attribute (dw_die_ref, tree);
5133 static void add_prototyped_attribute (dw_die_ref, tree);
5134 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
5135 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
5136 static void add_src_coords_attributes (dw_die_ref, tree);
5137 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
5138 static void push_decl_scope (tree);
5139 static void pop_decl_scope (void);
5140 static dw_die_ref scope_die_for (tree, dw_die_ref);
5141 static inline int local_scope_p (dw_die_ref);
5142 static inline int class_scope_p (dw_die_ref);
5143 static inline int class_or_namespace_scope_p (dw_die_ref);
5144 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
5145 static void add_calling_convention_attribute (dw_die_ref, tree);
5146 static const char *type_tag (const_tree);
5147 static tree member_declared_type (const_tree);
5148 #if 0
5149 static const char *decl_start_label (tree);
5150 #endif
5151 static void gen_array_type_die (tree, dw_die_ref);
5152 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
5153 #if 0
5154 static void gen_entry_point_die (tree, dw_die_ref);
5155 #endif
5156 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
5157 static dw_die_ref gen_formal_parameter_die (tree, tree, dw_die_ref);
5158 static void gen_unspecified_parameters_die (tree, dw_die_ref);
5159 static void gen_formal_types_die (tree, dw_die_ref);
5160 static void gen_subprogram_die (tree, dw_die_ref);
5161 static void gen_variable_die (tree, tree, dw_die_ref);
5162 static void gen_const_die (tree, dw_die_ref);
5163 static void gen_label_die (tree, dw_die_ref);
5164 static void gen_lexical_block_die (tree, dw_die_ref, int);
5165 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
5166 static void gen_field_die (tree, dw_die_ref);
5167 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
5168 static dw_die_ref gen_compile_unit_die (const char *);
5169 static void gen_inheritance_die (tree, tree, dw_die_ref);
5170 static void gen_member_die (tree, dw_die_ref);
5171 static void gen_struct_or_union_type_die (tree, dw_die_ref,
5172 enum debug_info_usage);
5173 static void gen_subroutine_type_die (tree, dw_die_ref);
5174 static void gen_typedef_die (tree, dw_die_ref);
5175 static void gen_type_die (tree, dw_die_ref);
5176 static void gen_block_die (tree, dw_die_ref, int);
5177 static void decls_for_scope (tree, dw_die_ref, int);
5178 static int is_redundant_typedef (const_tree);
5179 static void gen_namespace_die (tree, dw_die_ref);
5180 static void gen_decl_die (tree, tree, dw_die_ref);
5181 static dw_die_ref force_decl_die (tree);
5182 static dw_die_ref force_type_die (tree);
5183 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
5184 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
5185 static struct dwarf_file_data * lookup_filename (const char *);
5186 static void retry_incomplete_types (void);
5187 static void gen_type_die_for_member (tree, tree, dw_die_ref);
5188 static void splice_child_die (dw_die_ref, dw_die_ref);
5189 static int file_info_cmp (const void *, const void *);
5190 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
5191 const char *, const char *, unsigned);
5192 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
5193 const char *, const char *,
5194 const char *);
5195 static void output_loc_list (dw_loc_list_ref);
5196 static char *gen_internal_sym (const char *);
5198 static void prune_unmark_dies (dw_die_ref);
5199 static void prune_unused_types_mark (dw_die_ref, int);
5200 static void prune_unused_types_walk (dw_die_ref);
5201 static void prune_unused_types_walk_attribs (dw_die_ref);
5202 static void prune_unused_types_prune (dw_die_ref);
5203 static void prune_unused_types (void);
5204 static int maybe_emit_file (struct dwarf_file_data *fd);
5206 /* Section names used to hold DWARF debugging information. */
5207 #ifndef DEBUG_INFO_SECTION
5208 #define DEBUG_INFO_SECTION ".debug_info"
5209 #endif
5210 #ifndef DEBUG_ABBREV_SECTION
5211 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
5212 #endif
5213 #ifndef DEBUG_ARANGES_SECTION
5214 #define DEBUG_ARANGES_SECTION ".debug_aranges"
5215 #endif
5216 #ifndef DEBUG_MACINFO_SECTION
5217 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
5218 #endif
5219 #ifndef DEBUG_LINE_SECTION
5220 #define DEBUG_LINE_SECTION ".debug_line"
5221 #endif
5222 #ifndef DEBUG_LOC_SECTION
5223 #define DEBUG_LOC_SECTION ".debug_loc"
5224 #endif
5225 #ifndef DEBUG_PUBNAMES_SECTION
5226 #define DEBUG_PUBNAMES_SECTION ".debug_pubnames"
5227 #endif
5228 #ifndef DEBUG_STR_SECTION
5229 #define DEBUG_STR_SECTION ".debug_str"
5230 #endif
5231 #ifndef DEBUG_RANGES_SECTION
5232 #define DEBUG_RANGES_SECTION ".debug_ranges"
5233 #endif
5235 /* Standard ELF section names for compiled code and data. */
5236 #ifndef TEXT_SECTION_NAME
5237 #define TEXT_SECTION_NAME ".text"
5238 #endif
5240 /* Section flags for .debug_str section. */
5241 #define DEBUG_STR_SECTION_FLAGS \
5242 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
5243 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
5244 : SECTION_DEBUG)
5246 /* Labels we insert at beginning sections we can reference instead of
5247 the section names themselves. */
5249 #ifndef TEXT_SECTION_LABEL
5250 #define TEXT_SECTION_LABEL "Ltext"
5251 #endif
5252 #ifndef COLD_TEXT_SECTION_LABEL
5253 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
5254 #endif
5255 #ifndef DEBUG_LINE_SECTION_LABEL
5256 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
5257 #endif
5258 #ifndef DEBUG_INFO_SECTION_LABEL
5259 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
5260 #endif
5261 #ifndef DEBUG_ABBREV_SECTION_LABEL
5262 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
5263 #endif
5264 #ifndef DEBUG_LOC_SECTION_LABEL
5265 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
5266 #endif
5267 #ifndef DEBUG_RANGES_SECTION_LABEL
5268 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
5269 #endif
5270 #ifndef DEBUG_MACINFO_SECTION_LABEL
5271 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
5272 #endif
5274 /* Definitions of defaults for formats and names of various special
5275 (artificial) labels which may be generated within this file (when the -g
5276 options is used and DWARF2_DEBUGGING_INFO is in effect.
5277 If necessary, these may be overridden from within the tm.h file, but
5278 typically, overriding these defaults is unnecessary. */
5280 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
5281 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5282 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5283 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
5284 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5285 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5286 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5287 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5288 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5289 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
5291 #ifndef TEXT_END_LABEL
5292 #define TEXT_END_LABEL "Letext"
5293 #endif
5294 #ifndef COLD_END_LABEL
5295 #define COLD_END_LABEL "Letext_cold"
5296 #endif
5297 #ifndef BLOCK_BEGIN_LABEL
5298 #define BLOCK_BEGIN_LABEL "LBB"
5299 #endif
5300 #ifndef BLOCK_END_LABEL
5301 #define BLOCK_END_LABEL "LBE"
5302 #endif
5303 #ifndef LINE_CODE_LABEL
5304 #define LINE_CODE_LABEL "LM"
5305 #endif
5306 #ifndef SEPARATE_LINE_CODE_LABEL
5307 #define SEPARATE_LINE_CODE_LABEL "LSM"
5308 #endif
5311 /* We allow a language front-end to designate a function that is to be
5312 called to "demangle" any name before it is put into a DIE. */
5314 static const char *(*demangle_name_func) (const char *);
5316 void
5317 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
5319 demangle_name_func = func;
5322 /* Test if rtl node points to a pseudo register. */
5324 static inline int
5325 is_pseudo_reg (const_rtx rtl)
5327 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
5328 || (GET_CODE (rtl) == SUBREG
5329 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
5332 /* Return a reference to a type, with its const and volatile qualifiers
5333 removed. */
5335 static inline tree
5336 type_main_variant (tree type)
5338 type = TYPE_MAIN_VARIANT (type);
5340 /* ??? There really should be only one main variant among any group of
5341 variants of a given type (and all of the MAIN_VARIANT values for all
5342 members of the group should point to that one type) but sometimes the C
5343 front-end messes this up for array types, so we work around that bug
5344 here. */
5345 if (TREE_CODE (type) == ARRAY_TYPE)
5346 while (type != TYPE_MAIN_VARIANT (type))
5347 type = TYPE_MAIN_VARIANT (type);
5349 return type;
5352 /* Return nonzero if the given type node represents a tagged type. */
5354 static inline int
5355 is_tagged_type (const_tree type)
5357 enum tree_code code = TREE_CODE (type);
5359 return (code == RECORD_TYPE || code == UNION_TYPE
5360 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
5363 /* Convert a DIE tag into its string name. */
5365 static const char *
5366 dwarf_tag_name (unsigned int tag)
5368 switch (tag)
5370 case DW_TAG_padding:
5371 return "DW_TAG_padding";
5372 case DW_TAG_array_type:
5373 return "DW_TAG_array_type";
5374 case DW_TAG_class_type:
5375 return "DW_TAG_class_type";
5376 case DW_TAG_entry_point:
5377 return "DW_TAG_entry_point";
5378 case DW_TAG_enumeration_type:
5379 return "DW_TAG_enumeration_type";
5380 case DW_TAG_formal_parameter:
5381 return "DW_TAG_formal_parameter";
5382 case DW_TAG_imported_declaration:
5383 return "DW_TAG_imported_declaration";
5384 case DW_TAG_label:
5385 return "DW_TAG_label";
5386 case DW_TAG_lexical_block:
5387 return "DW_TAG_lexical_block";
5388 case DW_TAG_member:
5389 return "DW_TAG_member";
5390 case DW_TAG_pointer_type:
5391 return "DW_TAG_pointer_type";
5392 case DW_TAG_reference_type:
5393 return "DW_TAG_reference_type";
5394 case DW_TAG_compile_unit:
5395 return "DW_TAG_compile_unit";
5396 case DW_TAG_string_type:
5397 return "DW_TAG_string_type";
5398 case DW_TAG_structure_type:
5399 return "DW_TAG_structure_type";
5400 case DW_TAG_subroutine_type:
5401 return "DW_TAG_subroutine_type";
5402 case DW_TAG_typedef:
5403 return "DW_TAG_typedef";
5404 case DW_TAG_union_type:
5405 return "DW_TAG_union_type";
5406 case DW_TAG_unspecified_parameters:
5407 return "DW_TAG_unspecified_parameters";
5408 case DW_TAG_variant:
5409 return "DW_TAG_variant";
5410 case DW_TAG_common_block:
5411 return "DW_TAG_common_block";
5412 case DW_TAG_common_inclusion:
5413 return "DW_TAG_common_inclusion";
5414 case DW_TAG_inheritance:
5415 return "DW_TAG_inheritance";
5416 case DW_TAG_inlined_subroutine:
5417 return "DW_TAG_inlined_subroutine";
5418 case DW_TAG_module:
5419 return "DW_TAG_module";
5420 case DW_TAG_ptr_to_member_type:
5421 return "DW_TAG_ptr_to_member_type";
5422 case DW_TAG_set_type:
5423 return "DW_TAG_set_type";
5424 case DW_TAG_subrange_type:
5425 return "DW_TAG_subrange_type";
5426 case DW_TAG_with_stmt:
5427 return "DW_TAG_with_stmt";
5428 case DW_TAG_access_declaration:
5429 return "DW_TAG_access_declaration";
5430 case DW_TAG_base_type:
5431 return "DW_TAG_base_type";
5432 case DW_TAG_catch_block:
5433 return "DW_TAG_catch_block";
5434 case DW_TAG_const_type:
5435 return "DW_TAG_const_type";
5436 case DW_TAG_constant:
5437 return "DW_TAG_constant";
5438 case DW_TAG_enumerator:
5439 return "DW_TAG_enumerator";
5440 case DW_TAG_file_type:
5441 return "DW_TAG_file_type";
5442 case DW_TAG_friend:
5443 return "DW_TAG_friend";
5444 case DW_TAG_namelist:
5445 return "DW_TAG_namelist";
5446 case DW_TAG_namelist_item:
5447 return "DW_TAG_namelist_item";
5448 case DW_TAG_packed_type:
5449 return "DW_TAG_packed_type";
5450 case DW_TAG_subprogram:
5451 return "DW_TAG_subprogram";
5452 case DW_TAG_template_type_param:
5453 return "DW_TAG_template_type_param";
5454 case DW_TAG_template_value_param:
5455 return "DW_TAG_template_value_param";
5456 case DW_TAG_thrown_type:
5457 return "DW_TAG_thrown_type";
5458 case DW_TAG_try_block:
5459 return "DW_TAG_try_block";
5460 case DW_TAG_variant_part:
5461 return "DW_TAG_variant_part";
5462 case DW_TAG_variable:
5463 return "DW_TAG_variable";
5464 case DW_TAG_volatile_type:
5465 return "DW_TAG_volatile_type";
5466 case DW_TAG_dwarf_procedure:
5467 return "DW_TAG_dwarf_procedure";
5468 case DW_TAG_restrict_type:
5469 return "DW_TAG_restrict_type";
5470 case DW_TAG_interface_type:
5471 return "DW_TAG_interface_type";
5472 case DW_TAG_namespace:
5473 return "DW_TAG_namespace";
5474 case DW_TAG_imported_module:
5475 return "DW_TAG_imported_module";
5476 case DW_TAG_unspecified_type:
5477 return "DW_TAG_unspecified_type";
5478 case DW_TAG_partial_unit:
5479 return "DW_TAG_partial_unit";
5480 case DW_TAG_imported_unit:
5481 return "DW_TAG_imported_unit";
5482 case DW_TAG_condition:
5483 return "DW_TAG_condition";
5484 case DW_TAG_shared_type:
5485 return "DW_TAG_shared_type";
5486 case DW_TAG_MIPS_loop:
5487 return "DW_TAG_MIPS_loop";
5488 case DW_TAG_format_label:
5489 return "DW_TAG_format_label";
5490 case DW_TAG_function_template:
5491 return "DW_TAG_function_template";
5492 case DW_TAG_class_template:
5493 return "DW_TAG_class_template";
5494 case DW_TAG_GNU_BINCL:
5495 return "DW_TAG_GNU_BINCL";
5496 case DW_TAG_GNU_EINCL:
5497 return "DW_TAG_GNU_EINCL";
5498 default:
5499 return "DW_TAG_<unknown>";
5503 /* Convert a DWARF attribute code into its string name. */
5505 static const char *
5506 dwarf_attr_name (unsigned int attr)
5508 switch (attr)
5510 case DW_AT_sibling:
5511 return "DW_AT_sibling";
5512 case DW_AT_location:
5513 return "DW_AT_location";
5514 case DW_AT_name:
5515 return "DW_AT_name";
5516 case DW_AT_ordering:
5517 return "DW_AT_ordering";
5518 case DW_AT_subscr_data:
5519 return "DW_AT_subscr_data";
5520 case DW_AT_byte_size:
5521 return "DW_AT_byte_size";
5522 case DW_AT_bit_offset:
5523 return "DW_AT_bit_offset";
5524 case DW_AT_bit_size:
5525 return "DW_AT_bit_size";
5526 case DW_AT_element_list:
5527 return "DW_AT_element_list";
5528 case DW_AT_stmt_list:
5529 return "DW_AT_stmt_list";
5530 case DW_AT_low_pc:
5531 return "DW_AT_low_pc";
5532 case DW_AT_high_pc:
5533 return "DW_AT_high_pc";
5534 case DW_AT_language:
5535 return "DW_AT_language";
5536 case DW_AT_member:
5537 return "DW_AT_member";
5538 case DW_AT_discr:
5539 return "DW_AT_discr";
5540 case DW_AT_discr_value:
5541 return "DW_AT_discr_value";
5542 case DW_AT_visibility:
5543 return "DW_AT_visibility";
5544 case DW_AT_import:
5545 return "DW_AT_import";
5546 case DW_AT_string_length:
5547 return "DW_AT_string_length";
5548 case DW_AT_common_reference:
5549 return "DW_AT_common_reference";
5550 case DW_AT_comp_dir:
5551 return "DW_AT_comp_dir";
5552 case DW_AT_const_value:
5553 return "DW_AT_const_value";
5554 case DW_AT_containing_type:
5555 return "DW_AT_containing_type";
5556 case DW_AT_default_value:
5557 return "DW_AT_default_value";
5558 case DW_AT_inline:
5559 return "DW_AT_inline";
5560 case DW_AT_is_optional:
5561 return "DW_AT_is_optional";
5562 case DW_AT_lower_bound:
5563 return "DW_AT_lower_bound";
5564 case DW_AT_producer:
5565 return "DW_AT_producer";
5566 case DW_AT_prototyped:
5567 return "DW_AT_prototyped";
5568 case DW_AT_return_addr:
5569 return "DW_AT_return_addr";
5570 case DW_AT_start_scope:
5571 return "DW_AT_start_scope";
5572 case DW_AT_bit_stride:
5573 return "DW_AT_bit_stride";
5574 case DW_AT_upper_bound:
5575 return "DW_AT_upper_bound";
5576 case DW_AT_abstract_origin:
5577 return "DW_AT_abstract_origin";
5578 case DW_AT_accessibility:
5579 return "DW_AT_accessibility";
5580 case DW_AT_address_class:
5581 return "DW_AT_address_class";
5582 case DW_AT_artificial:
5583 return "DW_AT_artificial";
5584 case DW_AT_base_types:
5585 return "DW_AT_base_types";
5586 case DW_AT_calling_convention:
5587 return "DW_AT_calling_convention";
5588 case DW_AT_count:
5589 return "DW_AT_count";
5590 case DW_AT_data_member_location:
5591 return "DW_AT_data_member_location";
5592 case DW_AT_decl_column:
5593 return "DW_AT_decl_column";
5594 case DW_AT_decl_file:
5595 return "DW_AT_decl_file";
5596 case DW_AT_decl_line:
5597 return "DW_AT_decl_line";
5598 case DW_AT_declaration:
5599 return "DW_AT_declaration";
5600 case DW_AT_discr_list:
5601 return "DW_AT_discr_list";
5602 case DW_AT_encoding:
5603 return "DW_AT_encoding";
5604 case DW_AT_external:
5605 return "DW_AT_external";
5606 case DW_AT_explicit:
5607 return "DW_AT_explicit";
5608 case DW_AT_frame_base:
5609 return "DW_AT_frame_base";
5610 case DW_AT_friend:
5611 return "DW_AT_friend";
5612 case DW_AT_identifier_case:
5613 return "DW_AT_identifier_case";
5614 case DW_AT_macro_info:
5615 return "DW_AT_macro_info";
5616 case DW_AT_namelist_items:
5617 return "DW_AT_namelist_items";
5618 case DW_AT_priority:
5619 return "DW_AT_priority";
5620 case DW_AT_segment:
5621 return "DW_AT_segment";
5622 case DW_AT_specification:
5623 return "DW_AT_specification";
5624 case DW_AT_static_link:
5625 return "DW_AT_static_link";
5626 case DW_AT_type:
5627 return "DW_AT_type";
5628 case DW_AT_use_location:
5629 return "DW_AT_use_location";
5630 case DW_AT_variable_parameter:
5631 return "DW_AT_variable_parameter";
5632 case DW_AT_virtuality:
5633 return "DW_AT_virtuality";
5634 case DW_AT_vtable_elem_location:
5635 return "DW_AT_vtable_elem_location";
5637 case DW_AT_allocated:
5638 return "DW_AT_allocated";
5639 case DW_AT_associated:
5640 return "DW_AT_associated";
5641 case DW_AT_data_location:
5642 return "DW_AT_data_location";
5643 case DW_AT_byte_stride:
5644 return "DW_AT_byte_stride";
5645 case DW_AT_entry_pc:
5646 return "DW_AT_entry_pc";
5647 case DW_AT_use_UTF8:
5648 return "DW_AT_use_UTF8";
5649 case DW_AT_extension:
5650 return "DW_AT_extension";
5651 case DW_AT_ranges:
5652 return "DW_AT_ranges";
5653 case DW_AT_trampoline:
5654 return "DW_AT_trampoline";
5655 case DW_AT_call_column:
5656 return "DW_AT_call_column";
5657 case DW_AT_call_file:
5658 return "DW_AT_call_file";
5659 case DW_AT_call_line:
5660 return "DW_AT_call_line";
5662 case DW_AT_MIPS_fde:
5663 return "DW_AT_MIPS_fde";
5664 case DW_AT_MIPS_loop_begin:
5665 return "DW_AT_MIPS_loop_begin";
5666 case DW_AT_MIPS_tail_loop_begin:
5667 return "DW_AT_MIPS_tail_loop_begin";
5668 case DW_AT_MIPS_epilog_begin:
5669 return "DW_AT_MIPS_epilog_begin";
5670 case DW_AT_MIPS_loop_unroll_factor:
5671 return "DW_AT_MIPS_loop_unroll_factor";
5672 case DW_AT_MIPS_software_pipeline_depth:
5673 return "DW_AT_MIPS_software_pipeline_depth";
5674 case DW_AT_MIPS_linkage_name:
5675 return "DW_AT_MIPS_linkage_name";
5676 case DW_AT_MIPS_stride:
5677 return "DW_AT_MIPS_stride";
5678 case DW_AT_MIPS_abstract_name:
5679 return "DW_AT_MIPS_abstract_name";
5680 case DW_AT_MIPS_clone_origin:
5681 return "DW_AT_MIPS_clone_origin";
5682 case DW_AT_MIPS_has_inlines:
5683 return "DW_AT_MIPS_has_inlines";
5685 case DW_AT_sf_names:
5686 return "DW_AT_sf_names";
5687 case DW_AT_src_info:
5688 return "DW_AT_src_info";
5689 case DW_AT_mac_info:
5690 return "DW_AT_mac_info";
5691 case DW_AT_src_coords:
5692 return "DW_AT_src_coords";
5693 case DW_AT_body_begin:
5694 return "DW_AT_body_begin";
5695 case DW_AT_body_end:
5696 return "DW_AT_body_end";
5697 case DW_AT_GNU_vector:
5698 return "DW_AT_GNU_vector";
5700 case DW_AT_VMS_rtnbeg_pd_address:
5701 return "DW_AT_VMS_rtnbeg_pd_address";
5703 default:
5704 return "DW_AT_<unknown>";
5708 /* Convert a DWARF value form code into its string name. */
5710 static const char *
5711 dwarf_form_name (unsigned int form)
5713 switch (form)
5715 case DW_FORM_addr:
5716 return "DW_FORM_addr";
5717 case DW_FORM_block2:
5718 return "DW_FORM_block2";
5719 case DW_FORM_block4:
5720 return "DW_FORM_block4";
5721 case DW_FORM_data2:
5722 return "DW_FORM_data2";
5723 case DW_FORM_data4:
5724 return "DW_FORM_data4";
5725 case DW_FORM_data8:
5726 return "DW_FORM_data8";
5727 case DW_FORM_string:
5728 return "DW_FORM_string";
5729 case DW_FORM_block:
5730 return "DW_FORM_block";
5731 case DW_FORM_block1:
5732 return "DW_FORM_block1";
5733 case DW_FORM_data1:
5734 return "DW_FORM_data1";
5735 case DW_FORM_flag:
5736 return "DW_FORM_flag";
5737 case DW_FORM_sdata:
5738 return "DW_FORM_sdata";
5739 case DW_FORM_strp:
5740 return "DW_FORM_strp";
5741 case DW_FORM_udata:
5742 return "DW_FORM_udata";
5743 case DW_FORM_ref_addr:
5744 return "DW_FORM_ref_addr";
5745 case DW_FORM_ref1:
5746 return "DW_FORM_ref1";
5747 case DW_FORM_ref2:
5748 return "DW_FORM_ref2";
5749 case DW_FORM_ref4:
5750 return "DW_FORM_ref4";
5751 case DW_FORM_ref8:
5752 return "DW_FORM_ref8";
5753 case DW_FORM_ref_udata:
5754 return "DW_FORM_ref_udata";
5755 case DW_FORM_indirect:
5756 return "DW_FORM_indirect";
5757 default:
5758 return "DW_FORM_<unknown>";
5762 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
5763 instance of an inlined instance of a decl which is local to an inline
5764 function, so we have to trace all of the way back through the origin chain
5765 to find out what sort of node actually served as the original seed for the
5766 given block. */
5768 static tree
5769 decl_ultimate_origin (const_tree decl)
5771 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
5772 return NULL_TREE;
5774 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
5775 nodes in the function to point to themselves; ignore that if
5776 we're trying to output the abstract instance of this function. */
5777 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
5778 return NULL_TREE;
5780 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
5781 most distant ancestor, this should never happen. */
5782 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
5784 return DECL_ABSTRACT_ORIGIN (decl);
5787 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
5788 of a virtual function may refer to a base class, so we check the 'this'
5789 parameter. */
5791 static tree
5792 decl_class_context (tree decl)
5794 tree context = NULL_TREE;
5796 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
5797 context = DECL_CONTEXT (decl);
5798 else
5799 context = TYPE_MAIN_VARIANT
5800 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
5802 if (context && !TYPE_P (context))
5803 context = NULL_TREE;
5805 return context;
5808 /* Add an attribute/value pair to a DIE. */
5810 static inline void
5811 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
5813 /* Maybe this should be an assert? */
5814 if (die == NULL)
5815 return;
5817 if (die->die_attr == NULL)
5818 die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
5819 VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
5822 static inline enum dw_val_class
5823 AT_class (dw_attr_ref a)
5825 return a->dw_attr_val.val_class;
5828 /* Add a flag value attribute to a DIE. */
5830 static inline void
5831 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
5833 dw_attr_node attr;
5835 attr.dw_attr = attr_kind;
5836 attr.dw_attr_val.val_class = dw_val_class_flag;
5837 attr.dw_attr_val.v.val_flag = flag;
5838 add_dwarf_attr (die, &attr);
5841 static inline unsigned
5842 AT_flag (dw_attr_ref a)
5844 gcc_assert (a && AT_class (a) == dw_val_class_flag);
5845 return a->dw_attr_val.v.val_flag;
5848 /* Add a signed integer attribute value to a DIE. */
5850 static inline void
5851 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
5853 dw_attr_node attr;
5855 attr.dw_attr = attr_kind;
5856 attr.dw_attr_val.val_class = dw_val_class_const;
5857 attr.dw_attr_val.v.val_int = int_val;
5858 add_dwarf_attr (die, &attr);
5861 static inline HOST_WIDE_INT
5862 AT_int (dw_attr_ref a)
5864 gcc_assert (a && AT_class (a) == dw_val_class_const);
5865 return a->dw_attr_val.v.val_int;
5868 /* Add an unsigned integer attribute value to a DIE. */
5870 static inline void
5871 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
5872 unsigned HOST_WIDE_INT unsigned_val)
5874 dw_attr_node attr;
5876 attr.dw_attr = attr_kind;
5877 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
5878 attr.dw_attr_val.v.val_unsigned = unsigned_val;
5879 add_dwarf_attr (die, &attr);
5882 static inline unsigned HOST_WIDE_INT
5883 AT_unsigned (dw_attr_ref a)
5885 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
5886 return a->dw_attr_val.v.val_unsigned;
5889 /* Add an unsigned double integer attribute value to a DIE. */
5891 static inline void
5892 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
5893 long unsigned int val_hi, long unsigned int val_low)
5895 dw_attr_node attr;
5897 attr.dw_attr = attr_kind;
5898 attr.dw_attr_val.val_class = dw_val_class_long_long;
5899 attr.dw_attr_val.v.val_long_long.hi = val_hi;
5900 attr.dw_attr_val.v.val_long_long.low = val_low;
5901 add_dwarf_attr (die, &attr);
5904 /* Add a floating point attribute value to a DIE and return it. */
5906 static inline void
5907 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
5908 unsigned int length, unsigned int elt_size, unsigned char *array)
5910 dw_attr_node attr;
5912 attr.dw_attr = attr_kind;
5913 attr.dw_attr_val.val_class = dw_val_class_vec;
5914 attr.dw_attr_val.v.val_vec.length = length;
5915 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
5916 attr.dw_attr_val.v.val_vec.array = array;
5917 add_dwarf_attr (die, &attr);
5920 /* Hash and equality functions for debug_str_hash. */
5922 static hashval_t
5923 debug_str_do_hash (const void *x)
5925 return htab_hash_string (((const struct indirect_string_node *)x)->str);
5928 static int
5929 debug_str_eq (const void *x1, const void *x2)
5931 return strcmp ((((const struct indirect_string_node *)x1)->str),
5932 (const char *)x2) == 0;
5935 static struct indirect_string_node *
5936 find_AT_string (const char *str)
5938 struct indirect_string_node *node;
5939 void **slot;
5941 if (! debug_str_hash)
5942 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
5943 debug_str_eq, NULL);
5945 slot = htab_find_slot_with_hash (debug_str_hash, str,
5946 htab_hash_string (str), INSERT);
5947 if (*slot == NULL)
5949 node = (struct indirect_string_node *)
5950 ggc_alloc_cleared (sizeof (struct indirect_string_node));
5951 node->str = ggc_strdup (str);
5952 *slot = node;
5954 else
5955 node = (struct indirect_string_node *) *slot;
5957 node->refcount++;
5958 return node;
5961 /* Add a string attribute value to a DIE. */
5963 static inline void
5964 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
5966 dw_attr_node attr;
5967 struct indirect_string_node *node;
5969 node = find_AT_string (str);
5971 attr.dw_attr = attr_kind;
5972 attr.dw_attr_val.val_class = dw_val_class_str;
5973 attr.dw_attr_val.v.val_str = node;
5974 add_dwarf_attr (die, &attr);
5977 static inline const char *
5978 AT_string (dw_attr_ref a)
5980 gcc_assert (a && AT_class (a) == dw_val_class_str);
5981 return a->dw_attr_val.v.val_str->str;
5984 /* Find out whether a string should be output inline in DIE
5985 or out-of-line in .debug_str section. */
5987 static int
5988 AT_string_form (dw_attr_ref a)
5990 struct indirect_string_node *node;
5991 unsigned int len;
5992 char label[32];
5994 gcc_assert (a && AT_class (a) == dw_val_class_str);
5996 node = a->dw_attr_val.v.val_str;
5997 if (node->form)
5998 return node->form;
6000 len = strlen (node->str) + 1;
6002 /* If the string is shorter or equal to the size of the reference, it is
6003 always better to put it inline. */
6004 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
6005 return node->form = DW_FORM_string;
6007 /* If we cannot expect the linker to merge strings in .debug_str
6008 section, only put it into .debug_str if it is worth even in this
6009 single module. */
6010 if ((debug_str_section->common.flags & SECTION_MERGE) == 0
6011 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
6012 return node->form = DW_FORM_string;
6014 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
6015 ++dw2_string_counter;
6016 node->label = xstrdup (label);
6018 return node->form = DW_FORM_strp;
6021 /* Add a DIE reference attribute value to a DIE. */
6023 static inline void
6024 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
6026 dw_attr_node attr;
6028 attr.dw_attr = attr_kind;
6029 attr.dw_attr_val.val_class = dw_val_class_die_ref;
6030 attr.dw_attr_val.v.val_die_ref.die = targ_die;
6031 attr.dw_attr_val.v.val_die_ref.external = 0;
6032 add_dwarf_attr (die, &attr);
6035 /* Add an AT_specification attribute to a DIE, and also make the back
6036 pointer from the specification to the definition. */
6038 static inline void
6039 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
6041 add_AT_die_ref (die, DW_AT_specification, targ_die);
6042 gcc_assert (!targ_die->die_definition);
6043 targ_die->die_definition = die;
6046 static inline dw_die_ref
6047 AT_ref (dw_attr_ref a)
6049 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6050 return a->dw_attr_val.v.val_die_ref.die;
6053 static inline int
6054 AT_ref_external (dw_attr_ref a)
6056 if (a && AT_class (a) == dw_val_class_die_ref)
6057 return a->dw_attr_val.v.val_die_ref.external;
6059 return 0;
6062 static inline void
6063 set_AT_ref_external (dw_attr_ref a, int i)
6065 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6066 a->dw_attr_val.v.val_die_ref.external = i;
6069 /* Add an FDE reference attribute value to a DIE. */
6071 static inline void
6072 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
6074 dw_attr_node attr;
6076 attr.dw_attr = attr_kind;
6077 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
6078 attr.dw_attr_val.v.val_fde_index = targ_fde;
6079 add_dwarf_attr (die, &attr);
6082 /* Add a location description attribute value to a DIE. */
6084 static inline void
6085 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
6087 dw_attr_node attr;
6089 attr.dw_attr = attr_kind;
6090 attr.dw_attr_val.val_class = dw_val_class_loc;
6091 attr.dw_attr_val.v.val_loc = loc;
6092 add_dwarf_attr (die, &attr);
6095 static inline dw_loc_descr_ref
6096 AT_loc (dw_attr_ref a)
6098 gcc_assert (a && AT_class (a) == dw_val_class_loc);
6099 return a->dw_attr_val.v.val_loc;
6102 static inline void
6103 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
6105 dw_attr_node attr;
6107 attr.dw_attr = attr_kind;
6108 attr.dw_attr_val.val_class = dw_val_class_loc_list;
6109 attr.dw_attr_val.v.val_loc_list = loc_list;
6110 add_dwarf_attr (die, &attr);
6111 have_location_lists = true;
6114 static inline dw_loc_list_ref
6115 AT_loc_list (dw_attr_ref a)
6117 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
6118 return a->dw_attr_val.v.val_loc_list;
6121 /* Add an address constant attribute value to a DIE. */
6123 static inline void
6124 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
6126 dw_attr_node attr;
6128 attr.dw_attr = attr_kind;
6129 attr.dw_attr_val.val_class = dw_val_class_addr;
6130 attr.dw_attr_val.v.val_addr = addr;
6131 add_dwarf_attr (die, &attr);
6134 /* Get the RTX from to an address DIE attribute. */
6136 static inline rtx
6137 AT_addr (dw_attr_ref a)
6139 gcc_assert (a && AT_class (a) == dw_val_class_addr);
6140 return a->dw_attr_val.v.val_addr;
6143 /* Add a file attribute value to a DIE. */
6145 static inline void
6146 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
6147 struct dwarf_file_data *fd)
6149 dw_attr_node attr;
6151 attr.dw_attr = attr_kind;
6152 attr.dw_attr_val.val_class = dw_val_class_file;
6153 attr.dw_attr_val.v.val_file = fd;
6154 add_dwarf_attr (die, &attr);
6157 /* Get the dwarf_file_data from a file DIE attribute. */
6159 static inline struct dwarf_file_data *
6160 AT_file (dw_attr_ref a)
6162 gcc_assert (a && AT_class (a) == dw_val_class_file);
6163 return a->dw_attr_val.v.val_file;
6166 /* Add a label identifier attribute value to a DIE. */
6168 static inline void
6169 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
6171 dw_attr_node attr;
6173 attr.dw_attr = attr_kind;
6174 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
6175 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
6176 add_dwarf_attr (die, &attr);
6179 /* Add a section offset attribute value to a DIE, an offset into the
6180 debug_line section. */
6182 static inline void
6183 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
6184 const char *label)
6186 dw_attr_node attr;
6188 attr.dw_attr = attr_kind;
6189 attr.dw_attr_val.val_class = dw_val_class_lineptr;
6190 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
6191 add_dwarf_attr (die, &attr);
6194 /* Add a section offset attribute value to a DIE, an offset into the
6195 debug_macinfo section. */
6197 static inline void
6198 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
6199 const char *label)
6201 dw_attr_node attr;
6203 attr.dw_attr = attr_kind;
6204 attr.dw_attr_val.val_class = dw_val_class_macptr;
6205 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
6206 add_dwarf_attr (die, &attr);
6209 /* Add an offset attribute value to a DIE. */
6211 static inline void
6212 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
6213 unsigned HOST_WIDE_INT offset)
6215 dw_attr_node attr;
6217 attr.dw_attr = attr_kind;
6218 attr.dw_attr_val.val_class = dw_val_class_offset;
6219 attr.dw_attr_val.v.val_offset = offset;
6220 add_dwarf_attr (die, &attr);
6223 /* Add an range_list attribute value to a DIE. */
6225 static void
6226 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
6227 long unsigned int offset)
6229 dw_attr_node attr;
6231 attr.dw_attr = attr_kind;
6232 attr.dw_attr_val.val_class = dw_val_class_range_list;
6233 attr.dw_attr_val.v.val_offset = offset;
6234 add_dwarf_attr (die, &attr);
6237 static inline const char *
6238 AT_lbl (dw_attr_ref a)
6240 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
6241 || AT_class (a) == dw_val_class_lineptr
6242 || AT_class (a) == dw_val_class_macptr));
6243 return a->dw_attr_val.v.val_lbl_id;
6246 /* Get the attribute of type attr_kind. */
6248 static dw_attr_ref
6249 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
6251 dw_attr_ref a;
6252 unsigned ix;
6253 dw_die_ref spec = NULL;
6255 if (! die)
6256 return NULL;
6258 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6259 if (a->dw_attr == attr_kind)
6260 return a;
6261 else if (a->dw_attr == DW_AT_specification
6262 || a->dw_attr == DW_AT_abstract_origin)
6263 spec = AT_ref (a);
6265 if (spec)
6266 return get_AT (spec, attr_kind);
6268 return NULL;
6271 /* Return the "low pc" attribute value, typically associated with a subprogram
6272 DIE. Return null if the "low pc" attribute is either not present, or if it
6273 cannot be represented as an assembler label identifier. */
6275 static inline const char *
6276 get_AT_low_pc (dw_die_ref die)
6278 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
6280 return a ? AT_lbl (a) : NULL;
6283 /* Return the "high pc" attribute value, typically associated with a subprogram
6284 DIE. Return null if the "high pc" attribute is either not present, or if it
6285 cannot be represented as an assembler label identifier. */
6287 static inline const char *
6288 get_AT_hi_pc (dw_die_ref die)
6290 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
6292 return a ? AT_lbl (a) : NULL;
6295 /* Return the value of the string attribute designated by ATTR_KIND, or
6296 NULL if it is not present. */
6298 static inline const char *
6299 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
6301 dw_attr_ref a = get_AT (die, attr_kind);
6303 return a ? AT_string (a) : NULL;
6306 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
6307 if it is not present. */
6309 static inline int
6310 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
6312 dw_attr_ref a = get_AT (die, attr_kind);
6314 return a ? AT_flag (a) : 0;
6317 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
6318 if it is not present. */
6320 static inline unsigned
6321 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
6323 dw_attr_ref a = get_AT (die, attr_kind);
6325 return a ? AT_unsigned (a) : 0;
6328 static inline dw_die_ref
6329 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
6331 dw_attr_ref a = get_AT (die, attr_kind);
6333 return a ? AT_ref (a) : NULL;
6336 static inline struct dwarf_file_data *
6337 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
6339 dw_attr_ref a = get_AT (die, attr_kind);
6341 return a ? AT_file (a) : NULL;
6344 /* Return TRUE if the language is C or C++. */
6346 static inline bool
6347 is_c_family (void)
6349 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6351 return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
6352 || lang == DW_LANG_C99
6353 || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
6356 /* Return TRUE if the language is C++. */
6358 static inline bool
6359 is_cxx (void)
6361 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6363 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
6366 /* Return TRUE if the language is Fortran. */
6368 static inline bool
6369 is_fortran (void)
6371 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6373 return (lang == DW_LANG_Fortran77
6374 || lang == DW_LANG_Fortran90
6375 || lang == DW_LANG_Fortran95);
6378 /* Return TRUE if the language is Java. */
6380 static inline bool
6381 is_java (void)
6383 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6385 return lang == DW_LANG_Java;
6388 /* Return TRUE if the language is Ada. */
6390 static inline bool
6391 is_ada (void)
6393 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6395 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
6398 /* Remove the specified attribute if present. */
6400 static void
6401 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
6403 dw_attr_ref a;
6404 unsigned ix;
6406 if (! die)
6407 return;
6409 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6410 if (a->dw_attr == attr_kind)
6412 if (AT_class (a) == dw_val_class_str)
6413 if (a->dw_attr_val.v.val_str->refcount)
6414 a->dw_attr_val.v.val_str->refcount--;
6416 /* VEC_ordered_remove should help reduce the number of abbrevs
6417 that are needed. */
6418 VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
6419 return;
6423 /* Remove CHILD from its parent. PREV must have the property that
6424 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
6426 static void
6427 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
6429 gcc_assert (child->die_parent == prev->die_parent);
6430 gcc_assert (prev->die_sib == child);
6431 if (prev == child)
6433 gcc_assert (child->die_parent->die_child == child);
6434 prev = NULL;
6436 else
6437 prev->die_sib = child->die_sib;
6438 if (child->die_parent->die_child == child)
6439 child->die_parent->die_child = prev;
6442 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
6443 matches TAG. */
6445 static void
6446 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
6448 dw_die_ref c;
6450 c = die->die_child;
6451 if (c) do {
6452 dw_die_ref prev = c;
6453 c = c->die_sib;
6454 while (c->die_tag == tag)
6456 remove_child_with_prev (c, prev);
6457 /* Might have removed every child. */
6458 if (c == c->die_sib)
6459 return;
6460 c = c->die_sib;
6462 } while (c != die->die_child);
6465 /* Add a CHILD_DIE as the last child of DIE. */
6467 static void
6468 add_child_die (dw_die_ref die, dw_die_ref child_die)
6470 /* FIXME this should probably be an assert. */
6471 if (! die || ! child_die)
6472 return;
6473 gcc_assert (die != child_die);
6475 child_die->die_parent = die;
6476 if (die->die_child)
6478 child_die->die_sib = die->die_child->die_sib;
6479 die->die_child->die_sib = child_die;
6481 else
6482 child_die->die_sib = child_die;
6483 die->die_child = child_die;
6486 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
6487 is the specification, to the end of PARENT's list of children.
6488 This is done by removing and re-adding it. */
6490 static void
6491 splice_child_die (dw_die_ref parent, dw_die_ref child)
6493 dw_die_ref p;
6495 /* We want the declaration DIE from inside the class, not the
6496 specification DIE at toplevel. */
6497 if (child->die_parent != parent)
6499 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
6501 if (tmp)
6502 child = tmp;
6505 gcc_assert (child->die_parent == parent
6506 || (child->die_parent
6507 == get_AT_ref (parent, DW_AT_specification)));
6509 for (p = child->die_parent->die_child; ; p = p->die_sib)
6510 if (p->die_sib == child)
6512 remove_child_with_prev (child, p);
6513 break;
6516 add_child_die (parent, child);
6519 /* Return a pointer to a newly created DIE node. */
6521 static inline dw_die_ref
6522 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
6524 dw_die_ref die = GGC_CNEW (die_node);
6526 die->die_tag = tag_value;
6528 if (parent_die != NULL)
6529 add_child_die (parent_die, die);
6530 else
6532 limbo_die_node *limbo_node;
6534 limbo_node = GGC_CNEW (limbo_die_node);
6535 limbo_node->die = die;
6536 limbo_node->created_for = t;
6537 limbo_node->next = limbo_die_list;
6538 limbo_die_list = limbo_node;
6541 return die;
6544 /* Return the DIE associated with the given type specifier. */
6546 static inline dw_die_ref
6547 lookup_type_die (tree type)
6549 return TYPE_SYMTAB_DIE (type);
6552 /* Equate a DIE to a given type specifier. */
6554 static inline void
6555 equate_type_number_to_die (tree type, dw_die_ref type_die)
6557 TYPE_SYMTAB_DIE (type) = type_die;
6560 /* Returns a hash value for X (which really is a die_struct). */
6562 static hashval_t
6563 decl_die_table_hash (const void *x)
6565 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
6568 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
6570 static int
6571 decl_die_table_eq (const void *x, const void *y)
6573 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
6576 /* Return the DIE associated with a given declaration. */
6578 static inline dw_die_ref
6579 lookup_decl_die (tree decl)
6581 return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
6584 /* Returns a hash value for X (which really is a var_loc_list). */
6586 static hashval_t
6587 decl_loc_table_hash (const void *x)
6589 return (hashval_t) ((const var_loc_list *) x)->decl_id;
6592 /* Return nonzero if decl_id of var_loc_list X is the same as
6593 UID of decl *Y. */
6595 static int
6596 decl_loc_table_eq (const void *x, const void *y)
6598 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
6601 /* Return the var_loc list associated with a given declaration. */
6603 static inline var_loc_list *
6604 lookup_decl_loc (const_tree decl)
6606 return (var_loc_list *)
6607 htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
6610 /* Equate a DIE to a particular declaration. */
6612 static void
6613 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
6615 unsigned int decl_id = DECL_UID (decl);
6616 void **slot;
6618 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
6619 *slot = decl_die;
6620 decl_die->decl_id = decl_id;
6623 /* Add a variable location node to the linked list for DECL. */
6625 static void
6626 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
6628 unsigned int decl_id = DECL_UID (decl);
6629 var_loc_list *temp;
6630 void **slot;
6632 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
6633 if (*slot == NULL)
6635 temp = GGC_CNEW (var_loc_list);
6636 temp->decl_id = decl_id;
6637 *slot = temp;
6639 else
6640 temp = (var_loc_list *) *slot;
6642 if (temp->last)
6644 /* If the current location is the same as the end of the list,
6645 and either both or neither of the locations is uninitialized,
6646 we have nothing to do. */
6647 if ((!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
6648 NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
6649 || ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
6650 != NOTE_VAR_LOCATION_STATUS (loc->var_loc_note))
6651 && ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
6652 == VAR_INIT_STATUS_UNINITIALIZED)
6653 || (NOTE_VAR_LOCATION_STATUS (loc->var_loc_note)
6654 == VAR_INIT_STATUS_UNINITIALIZED))))
6656 /* Add LOC to the end of list and update LAST. */
6657 temp->last->next = loc;
6658 temp->last = loc;
6661 /* Do not add empty location to the beginning of the list. */
6662 else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
6664 temp->first = loc;
6665 temp->last = loc;
6669 /* Keep track of the number of spaces used to indent the
6670 output of the debugging routines that print the structure of
6671 the DIE internal representation. */
6672 static int print_indent;
6674 /* Indent the line the number of spaces given by print_indent. */
6676 static inline void
6677 print_spaces (FILE *outfile)
6679 fprintf (outfile, "%*s", print_indent, "");
6682 /* Print the information associated with a given DIE, and its children.
6683 This routine is a debugging aid only. */
6685 static void
6686 print_die (dw_die_ref die, FILE *outfile)
6688 dw_attr_ref a;
6689 dw_die_ref c;
6690 unsigned ix;
6692 print_spaces (outfile);
6693 fprintf (outfile, "DIE %4ld: %s\n",
6694 die->die_offset, dwarf_tag_name (die->die_tag));
6695 print_spaces (outfile);
6696 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
6697 fprintf (outfile, " offset: %ld\n", die->die_offset);
6699 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6701 print_spaces (outfile);
6702 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
6704 switch (AT_class (a))
6706 case dw_val_class_addr:
6707 fprintf (outfile, "address");
6708 break;
6709 case dw_val_class_offset:
6710 fprintf (outfile, "offset");
6711 break;
6712 case dw_val_class_loc:
6713 fprintf (outfile, "location descriptor");
6714 break;
6715 case dw_val_class_loc_list:
6716 fprintf (outfile, "location list -> label:%s",
6717 AT_loc_list (a)->ll_symbol);
6718 break;
6719 case dw_val_class_range_list:
6720 fprintf (outfile, "range list");
6721 break;
6722 case dw_val_class_const:
6723 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
6724 break;
6725 case dw_val_class_unsigned_const:
6726 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
6727 break;
6728 case dw_val_class_long_long:
6729 fprintf (outfile, "constant (%lu,%lu)",
6730 a->dw_attr_val.v.val_long_long.hi,
6731 a->dw_attr_val.v.val_long_long.low);
6732 break;
6733 case dw_val_class_vec:
6734 fprintf (outfile, "floating-point or vector constant");
6735 break;
6736 case dw_val_class_flag:
6737 fprintf (outfile, "%u", AT_flag (a));
6738 break;
6739 case dw_val_class_die_ref:
6740 if (AT_ref (a) != NULL)
6742 if (AT_ref (a)->die_symbol)
6743 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
6744 else
6745 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
6747 else
6748 fprintf (outfile, "die -> <null>");
6749 break;
6750 case dw_val_class_lbl_id:
6751 case dw_val_class_lineptr:
6752 case dw_val_class_macptr:
6753 fprintf (outfile, "label: %s", AT_lbl (a));
6754 break;
6755 case dw_val_class_str:
6756 if (AT_string (a) != NULL)
6757 fprintf (outfile, "\"%s\"", AT_string (a));
6758 else
6759 fprintf (outfile, "<null>");
6760 break;
6761 case dw_val_class_file:
6762 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
6763 AT_file (a)->emitted_number);
6764 break;
6765 default:
6766 break;
6769 fprintf (outfile, "\n");
6772 if (die->die_child != NULL)
6774 print_indent += 4;
6775 FOR_EACH_CHILD (die, c, print_die (c, outfile));
6776 print_indent -= 4;
6778 if (print_indent == 0)
6779 fprintf (outfile, "\n");
6782 /* Print the contents of the source code line number correspondence table.
6783 This routine is a debugging aid only. */
6785 static void
6786 print_dwarf_line_table (FILE *outfile)
6788 unsigned i;
6789 dw_line_info_ref line_info;
6791 fprintf (outfile, "\n\nDWARF source line information\n");
6792 for (i = 1; i < line_info_table_in_use; i++)
6794 line_info = &line_info_table[i];
6795 fprintf (outfile, "%5d: %4ld %6ld\n", i,
6796 line_info->dw_file_num,
6797 line_info->dw_line_num);
6800 fprintf (outfile, "\n\n");
6803 /* Print the information collected for a given DIE. */
6805 void
6806 debug_dwarf_die (dw_die_ref die)
6808 print_die (die, stderr);
6811 /* Print all DWARF information collected for the compilation unit.
6812 This routine is a debugging aid only. */
6814 void
6815 debug_dwarf (void)
6817 print_indent = 0;
6818 print_die (comp_unit_die, stderr);
6819 if (! DWARF2_ASM_LINE_DEBUG_INFO)
6820 print_dwarf_line_table (stderr);
6823 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
6824 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
6825 DIE that marks the start of the DIEs for this include file. */
6827 static dw_die_ref
6828 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
6830 const char *filename = get_AT_string (bincl_die, DW_AT_name);
6831 dw_die_ref new_unit = gen_compile_unit_die (filename);
6833 new_unit->die_sib = old_unit;
6834 return new_unit;
6837 /* Close an include-file CU and reopen the enclosing one. */
6839 static dw_die_ref
6840 pop_compile_unit (dw_die_ref old_unit)
6842 dw_die_ref new_unit = old_unit->die_sib;
6844 old_unit->die_sib = NULL;
6845 return new_unit;
6848 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6849 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6851 /* Calculate the checksum of a location expression. */
6853 static inline void
6854 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6856 CHECKSUM (loc->dw_loc_opc);
6857 CHECKSUM (loc->dw_loc_oprnd1);
6858 CHECKSUM (loc->dw_loc_oprnd2);
6861 /* Calculate the checksum of an attribute. */
6863 static void
6864 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
6866 dw_loc_descr_ref loc;
6867 rtx r;
6869 CHECKSUM (at->dw_attr);
6871 /* We don't care that this was compiled with a different compiler
6872 snapshot; if the output is the same, that's what matters. */
6873 if (at->dw_attr == DW_AT_producer)
6874 return;
6876 switch (AT_class (at))
6878 case dw_val_class_const:
6879 CHECKSUM (at->dw_attr_val.v.val_int);
6880 break;
6881 case dw_val_class_unsigned_const:
6882 CHECKSUM (at->dw_attr_val.v.val_unsigned);
6883 break;
6884 case dw_val_class_long_long:
6885 CHECKSUM (at->dw_attr_val.v.val_long_long);
6886 break;
6887 case dw_val_class_vec:
6888 CHECKSUM (at->dw_attr_val.v.val_vec);
6889 break;
6890 case dw_val_class_flag:
6891 CHECKSUM (at->dw_attr_val.v.val_flag);
6892 break;
6893 case dw_val_class_str:
6894 CHECKSUM_STRING (AT_string (at));
6895 break;
6897 case dw_val_class_addr:
6898 r = AT_addr (at);
6899 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6900 CHECKSUM_STRING (XSTR (r, 0));
6901 break;
6903 case dw_val_class_offset:
6904 CHECKSUM (at->dw_attr_val.v.val_offset);
6905 break;
6907 case dw_val_class_loc:
6908 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6909 loc_checksum (loc, ctx);
6910 break;
6912 case dw_val_class_die_ref:
6913 die_checksum (AT_ref (at), ctx, mark);
6914 break;
6916 case dw_val_class_fde_ref:
6917 case dw_val_class_lbl_id:
6918 case dw_val_class_lineptr:
6919 case dw_val_class_macptr:
6920 break;
6922 case dw_val_class_file:
6923 CHECKSUM_STRING (AT_file (at)->filename);
6924 break;
6926 default:
6927 break;
6931 /* Calculate the checksum of a DIE. */
6933 static void
6934 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6936 dw_die_ref c;
6937 dw_attr_ref a;
6938 unsigned ix;
6940 /* To avoid infinite recursion. */
6941 if (die->die_mark)
6943 CHECKSUM (die->die_mark);
6944 return;
6946 die->die_mark = ++(*mark);
6948 CHECKSUM (die->die_tag);
6950 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6951 attr_checksum (a, ctx, mark);
6953 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6956 #undef CHECKSUM
6957 #undef CHECKSUM_STRING
6959 /* Do the location expressions look same? */
6960 static inline int
6961 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6963 return loc1->dw_loc_opc == loc2->dw_loc_opc
6964 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6965 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6968 /* Do the values look the same? */
6969 static int
6970 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6972 dw_loc_descr_ref loc1, loc2;
6973 rtx r1, r2;
6975 if (v1->val_class != v2->val_class)
6976 return 0;
6978 switch (v1->val_class)
6980 case dw_val_class_const:
6981 return v1->v.val_int == v2->v.val_int;
6982 case dw_val_class_unsigned_const:
6983 return v1->v.val_unsigned == v2->v.val_unsigned;
6984 case dw_val_class_long_long:
6985 return v1->v.val_long_long.hi == v2->v.val_long_long.hi
6986 && v1->v.val_long_long.low == v2->v.val_long_long.low;
6987 case dw_val_class_vec:
6988 if (v1->v.val_vec.length != v2->v.val_vec.length
6989 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6990 return 0;
6991 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6992 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6993 return 0;
6994 return 1;
6995 case dw_val_class_flag:
6996 return v1->v.val_flag == v2->v.val_flag;
6997 case dw_val_class_str:
6998 return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
7000 case dw_val_class_addr:
7001 r1 = v1->v.val_addr;
7002 r2 = v2->v.val_addr;
7003 if (GET_CODE (r1) != GET_CODE (r2))
7004 return 0;
7005 gcc_assert (GET_CODE (r1) == SYMBOL_REF);
7006 return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
7008 case dw_val_class_offset:
7009 return v1->v.val_offset == v2->v.val_offset;
7011 case dw_val_class_loc:
7012 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7013 loc1 && loc2;
7014 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7015 if (!same_loc_p (loc1, loc2, mark))
7016 return 0;
7017 return !loc1 && !loc2;
7019 case dw_val_class_die_ref:
7020 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7022 case dw_val_class_fde_ref:
7023 case dw_val_class_lbl_id:
7024 case dw_val_class_lineptr:
7025 case dw_val_class_macptr:
7026 return 1;
7028 case dw_val_class_file:
7029 return v1->v.val_file == v2->v.val_file;
7031 default:
7032 return 1;
7036 /* Do the attributes look the same? */
7038 static int
7039 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
7041 if (at1->dw_attr != at2->dw_attr)
7042 return 0;
7044 /* We don't care that this was compiled with a different compiler
7045 snapshot; if the output is the same, that's what matters. */
7046 if (at1->dw_attr == DW_AT_producer)
7047 return 1;
7049 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7052 /* Do the dies look the same? */
7054 static int
7055 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7057 dw_die_ref c1, c2;
7058 dw_attr_ref a1;
7059 unsigned ix;
7061 /* To avoid infinite recursion. */
7062 if (die1->die_mark)
7063 return die1->die_mark == die2->die_mark;
7064 die1->die_mark = die2->die_mark = ++(*mark);
7066 if (die1->die_tag != die2->die_tag)
7067 return 0;
7069 if (VEC_length (dw_attr_node, die1->die_attr)
7070 != VEC_length (dw_attr_node, die2->die_attr))
7071 return 0;
7073 for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
7074 if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
7075 return 0;
7077 c1 = die1->die_child;
7078 c2 = die2->die_child;
7079 if (! c1)
7081 if (c2)
7082 return 0;
7084 else
7085 for (;;)
7087 if (!same_die_p (c1, c2, mark))
7088 return 0;
7089 c1 = c1->die_sib;
7090 c2 = c2->die_sib;
7091 if (c1 == die1->die_child)
7093 if (c2 == die2->die_child)
7094 break;
7095 else
7096 return 0;
7100 return 1;
7103 /* Do the dies look the same? Wrapper around same_die_p. */
7105 static int
7106 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
7108 int mark = 0;
7109 int ret = same_die_p (die1, die2, &mark);
7111 unmark_all_dies (die1);
7112 unmark_all_dies (die2);
7114 return ret;
7117 /* The prefix to attach to symbols on DIEs in the current comdat debug
7118 info section. */
7119 static char *comdat_symbol_id;
7121 /* The index of the current symbol within the current comdat CU. */
7122 static unsigned int comdat_symbol_number;
7124 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7125 children, and set comdat_symbol_id accordingly. */
7127 static void
7128 compute_section_prefix (dw_die_ref unit_die)
7130 const char *die_name = get_AT_string (unit_die, DW_AT_name);
7131 const char *base = die_name ? lbasename (die_name) : "anonymous";
7132 char *name = XALLOCAVEC (char, strlen (base) + 64);
7133 char *p;
7134 int i, mark;
7135 unsigned char checksum[16];
7136 struct md5_ctx ctx;
7138 /* Compute the checksum of the DIE, then append part of it as hex digits to
7139 the name filename of the unit. */
7141 md5_init_ctx (&ctx);
7142 mark = 0;
7143 die_checksum (unit_die, &ctx, &mark);
7144 unmark_all_dies (unit_die);
7145 md5_finish_ctx (&ctx, checksum);
7147 sprintf (name, "%s.", base);
7148 clean_symbol_name (name);
7150 p = name + strlen (name);
7151 for (i = 0; i < 4; i++)
7153 sprintf (p, "%.2x", checksum[i]);
7154 p += 2;
7157 comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
7158 comdat_symbol_number = 0;
7161 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
7163 static int
7164 is_type_die (dw_die_ref die)
7166 switch (die->die_tag)
7168 case DW_TAG_array_type:
7169 case DW_TAG_class_type:
7170 case DW_TAG_interface_type:
7171 case DW_TAG_enumeration_type:
7172 case DW_TAG_pointer_type:
7173 case DW_TAG_reference_type:
7174 case DW_TAG_string_type:
7175 case DW_TAG_structure_type:
7176 case DW_TAG_subroutine_type:
7177 case DW_TAG_union_type:
7178 case DW_TAG_ptr_to_member_type:
7179 case DW_TAG_set_type:
7180 case DW_TAG_subrange_type:
7181 case DW_TAG_base_type:
7182 case DW_TAG_const_type:
7183 case DW_TAG_file_type:
7184 case DW_TAG_packed_type:
7185 case DW_TAG_volatile_type:
7186 case DW_TAG_typedef:
7187 return 1;
7188 default:
7189 return 0;
7193 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7194 Basically, we want to choose the bits that are likely to be shared between
7195 compilations (types) and leave out the bits that are specific to individual
7196 compilations (functions). */
7198 static int
7199 is_comdat_die (dw_die_ref c)
7201 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7202 we do for stabs. The advantage is a greater likelihood of sharing between
7203 objects that don't include headers in the same order (and therefore would
7204 put the base types in a different comdat). jason 8/28/00 */
7206 if (c->die_tag == DW_TAG_base_type)
7207 return 0;
7209 if (c->die_tag == DW_TAG_pointer_type
7210 || c->die_tag == DW_TAG_reference_type
7211 || c->die_tag == DW_TAG_const_type
7212 || c->die_tag == DW_TAG_volatile_type)
7214 dw_die_ref t = get_AT_ref (c, DW_AT_type);
7216 return t ? is_comdat_die (t) : 0;
7219 return is_type_die (c);
7222 /* Returns 1 iff C is the sort of DIE that might be referred to from another
7223 compilation unit. */
7225 static int
7226 is_symbol_die (dw_die_ref c)
7228 return (is_type_die (c)
7229 || (get_AT (c, DW_AT_declaration)
7230 && !get_AT (c, DW_AT_specification))
7231 || c->die_tag == DW_TAG_namespace
7232 || c->die_tag == DW_TAG_module);
7235 static char *
7236 gen_internal_sym (const char *prefix)
7238 char buf[256];
7240 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7241 return xstrdup (buf);
7244 /* Assign symbols to all worthy DIEs under DIE. */
7246 static void
7247 assign_symbol_names (dw_die_ref die)
7249 dw_die_ref c;
7251 if (is_symbol_die (die))
7253 if (comdat_symbol_id)
7255 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
7257 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
7258 comdat_symbol_id, comdat_symbol_number++);
7259 die->die_symbol = xstrdup (p);
7261 else
7262 die->die_symbol = gen_internal_sym ("LDIE");
7265 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
7268 struct cu_hash_table_entry
7270 dw_die_ref cu;
7271 unsigned min_comdat_num, max_comdat_num;
7272 struct cu_hash_table_entry *next;
7275 /* Routines to manipulate hash table of CUs. */
7276 static hashval_t
7277 htab_cu_hash (const void *of)
7279 const struct cu_hash_table_entry *const entry =
7280 (const struct cu_hash_table_entry *) of;
7282 return htab_hash_string (entry->cu->die_symbol);
7285 static int
7286 htab_cu_eq (const void *of1, const void *of2)
7288 const struct cu_hash_table_entry *const entry1 =
7289 (const struct cu_hash_table_entry *) of1;
7290 const struct die_struct *const entry2 = (const struct die_struct *) of2;
7292 return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
7295 static void
7296 htab_cu_del (void *what)
7298 struct cu_hash_table_entry *next,
7299 *entry = (struct cu_hash_table_entry *) what;
7301 while (entry)
7303 next = entry->next;
7304 free (entry);
7305 entry = next;
7309 /* Check whether we have already seen this CU and set up SYM_NUM
7310 accordingly. */
7311 static int
7312 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
7314 struct cu_hash_table_entry dummy;
7315 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7317 dummy.max_comdat_num = 0;
7319 slot = (struct cu_hash_table_entry **)
7320 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
7321 INSERT);
7322 entry = *slot;
7324 for (; entry; last = entry, entry = entry->next)
7326 if (same_die_p_wrap (cu, entry->cu))
7327 break;
7330 if (entry)
7332 *sym_num = entry->min_comdat_num;
7333 return 1;
7336 entry = XCNEW (struct cu_hash_table_entry);
7337 entry->cu = cu;
7338 entry->min_comdat_num = *sym_num = last->max_comdat_num;
7339 entry->next = *slot;
7340 *slot = entry;
7342 return 0;
7345 /* Record SYM_NUM to record of CU in HTABLE. */
7346 static void
7347 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
7349 struct cu_hash_table_entry **slot, *entry;
7351 slot = (struct cu_hash_table_entry **)
7352 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
7353 NO_INSERT);
7354 entry = *slot;
7356 entry->max_comdat_num = sym_num;
7359 /* Traverse the DIE (which is always comp_unit_die), and set up
7360 additional compilation units for each of the include files we see
7361 bracketed by BINCL/EINCL. */
7363 static void
7364 break_out_includes (dw_die_ref die)
7366 dw_die_ref c;
7367 dw_die_ref unit = NULL;
7368 limbo_die_node *node, **pnode;
7369 htab_t cu_hash_table;
7371 c = die->die_child;
7372 if (c) do {
7373 dw_die_ref prev = c;
7374 c = c->die_sib;
7375 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7376 || (unit && is_comdat_die (c)))
7378 dw_die_ref next = c->die_sib;
7380 /* This DIE is for a secondary CU; remove it from the main one. */
7381 remove_child_with_prev (c, prev);
7383 if (c->die_tag == DW_TAG_GNU_BINCL)
7384 unit = push_new_compile_unit (unit, c);
7385 else if (c->die_tag == DW_TAG_GNU_EINCL)
7386 unit = pop_compile_unit (unit);
7387 else
7388 add_child_die (unit, c);
7389 c = next;
7390 if (c == die->die_child)
7391 break;
7393 } while (c != die->die_child);
7395 #if 0
7396 /* We can only use this in debugging, since the frontend doesn't check
7397 to make sure that we leave every include file we enter. */
7398 gcc_assert (!unit);
7399 #endif
7401 assign_symbol_names (die);
7402 cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
7403 for (node = limbo_die_list, pnode = &limbo_die_list;
7404 node;
7405 node = node->next)
7407 int is_dupl;
7409 compute_section_prefix (node->die);
7410 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
7411 &comdat_symbol_number);
7412 assign_symbol_names (node->die);
7413 if (is_dupl)
7414 *pnode = node->next;
7415 else
7417 pnode = &node->next;
7418 record_comdat_symbol_number (node->die, cu_hash_table,
7419 comdat_symbol_number);
7422 htab_delete (cu_hash_table);
7425 /* Traverse the DIE and add a sibling attribute if it may have the
7426 effect of speeding up access to siblings. To save some space,
7427 avoid generating sibling attributes for DIE's without children. */
7429 static void
7430 add_sibling_attributes (dw_die_ref die)
7432 dw_die_ref c;
7434 if (! die->die_child)
7435 return;
7437 if (die->die_parent && die != die->die_parent->die_child)
7438 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7440 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7443 /* Output all location lists for the DIE and its children. */
7445 static void
7446 output_location_lists (dw_die_ref die)
7448 dw_die_ref c;
7449 dw_attr_ref a;
7450 unsigned ix;
7452 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7453 if (AT_class (a) == dw_val_class_loc_list)
7454 output_loc_list (AT_loc_list (a));
7456 FOR_EACH_CHILD (die, c, output_location_lists (c));
7459 /* The format of each DIE (and its attribute value pairs) is encoded in an
7460 abbreviation table. This routine builds the abbreviation table and assigns
7461 a unique abbreviation id for each abbreviation entry. The children of each
7462 die are visited recursively. */
7464 static void
7465 build_abbrev_table (dw_die_ref die)
7467 unsigned long abbrev_id;
7468 unsigned int n_alloc;
7469 dw_die_ref c;
7470 dw_attr_ref a;
7471 unsigned ix;
7473 /* Scan the DIE references, and mark as external any that refer to
7474 DIEs from other CUs (i.e. those which are not marked). */
7475 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7476 if (AT_class (a) == dw_val_class_die_ref
7477 && AT_ref (a)->die_mark == 0)
7479 gcc_assert (AT_ref (a)->die_symbol);
7481 set_AT_ref_external (a, 1);
7484 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7486 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7487 dw_attr_ref die_a, abbrev_a;
7488 unsigned ix;
7489 bool ok = true;
7491 if (abbrev->die_tag != die->die_tag)
7492 continue;
7493 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7494 continue;
7496 if (VEC_length (dw_attr_node, abbrev->die_attr)
7497 != VEC_length (dw_attr_node, die->die_attr))
7498 continue;
7500 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
7502 abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
7503 if ((abbrev_a->dw_attr != die_a->dw_attr)
7504 || (value_format (abbrev_a) != value_format (die_a)))
7506 ok = false;
7507 break;
7510 if (ok)
7511 break;
7514 if (abbrev_id >= abbrev_die_table_in_use)
7516 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7518 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7519 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7520 n_alloc);
7522 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7523 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7524 abbrev_die_table_allocated = n_alloc;
7527 ++abbrev_die_table_in_use;
7528 abbrev_die_table[abbrev_id] = die;
7531 die->die_abbrev = abbrev_id;
7532 FOR_EACH_CHILD (die, c, build_abbrev_table (c));
7535 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7537 static int
7538 constant_size (unsigned HOST_WIDE_INT value)
7540 int log;
7542 if (value == 0)
7543 log = 0;
7544 else
7545 log = floor_log2 (value);
7547 log = log / 8;
7548 log = 1 << (floor_log2 (log) + 1);
7550 return log;
7553 /* Return the size of a DIE as it is represented in the
7554 .debug_info section. */
7556 static unsigned long
7557 size_of_die (dw_die_ref die)
7559 unsigned long size = 0;
7560 dw_attr_ref a;
7561 unsigned ix;
7563 size += size_of_uleb128 (die->die_abbrev);
7564 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7566 switch (AT_class (a))
7568 case dw_val_class_addr:
7569 size += DWARF2_ADDR_SIZE;
7570 break;
7571 case dw_val_class_offset:
7572 size += DWARF_OFFSET_SIZE;
7573 break;
7574 case dw_val_class_loc:
7576 unsigned long lsize = size_of_locs (AT_loc (a));
7578 /* Block length. */
7579 size += constant_size (lsize);
7580 size += lsize;
7582 break;
7583 case dw_val_class_loc_list:
7584 size += DWARF_OFFSET_SIZE;
7585 break;
7586 case dw_val_class_range_list:
7587 size += DWARF_OFFSET_SIZE;
7588 break;
7589 case dw_val_class_const:
7590 size += size_of_sleb128 (AT_int (a));
7591 break;
7592 case dw_val_class_unsigned_const:
7593 size += constant_size (AT_unsigned (a));
7594 break;
7595 case dw_val_class_long_long:
7596 size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
7597 break;
7598 case dw_val_class_vec:
7599 size += constant_size (a->dw_attr_val.v.val_vec.length
7600 * a->dw_attr_val.v.val_vec.elt_size)
7601 + a->dw_attr_val.v.val_vec.length
7602 * a->dw_attr_val.v.val_vec.elt_size; /* block */
7603 break;
7604 case dw_val_class_flag:
7605 size += 1;
7606 break;
7607 case dw_val_class_die_ref:
7608 if (AT_ref_external (a))
7609 size += DWARF2_ADDR_SIZE;
7610 else
7611 size += DWARF_OFFSET_SIZE;
7612 break;
7613 case dw_val_class_fde_ref:
7614 size += DWARF_OFFSET_SIZE;
7615 break;
7616 case dw_val_class_lbl_id:
7617 size += DWARF2_ADDR_SIZE;
7618 break;
7619 case dw_val_class_lineptr:
7620 case dw_val_class_macptr:
7621 size += DWARF_OFFSET_SIZE;
7622 break;
7623 case dw_val_class_str:
7624 if (AT_string_form (a) == DW_FORM_strp)
7625 size += DWARF_OFFSET_SIZE;
7626 else
7627 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7628 break;
7629 case dw_val_class_file:
7630 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7631 break;
7632 default:
7633 gcc_unreachable ();
7637 return size;
7640 /* Size the debugging information associated with a given DIE. Visits the
7641 DIE's children recursively. Updates the global variable next_die_offset, on
7642 each time through. Uses the current value of next_die_offset to update the
7643 die_offset field in each DIE. */
7645 static void
7646 calc_die_sizes (dw_die_ref die)
7648 dw_die_ref c;
7650 die->die_offset = next_die_offset;
7651 next_die_offset += size_of_die (die);
7653 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7655 if (die->die_child != NULL)
7656 /* Count the null byte used to terminate sibling lists. */
7657 next_die_offset += 1;
7660 /* Set the marks for a die and its children. We do this so
7661 that we know whether or not a reference needs to use FORM_ref_addr; only
7662 DIEs in the same CU will be marked. We used to clear out the offset
7663 and use that as the flag, but ran into ordering problems. */
7665 static void
7666 mark_dies (dw_die_ref die)
7668 dw_die_ref c;
7670 gcc_assert (!die->die_mark);
7672 die->die_mark = 1;
7673 FOR_EACH_CHILD (die, c, mark_dies (c));
7676 /* Clear the marks for a die and its children. */
7678 static void
7679 unmark_dies (dw_die_ref die)
7681 dw_die_ref c;
7683 gcc_assert (die->die_mark);
7685 die->die_mark = 0;
7686 FOR_EACH_CHILD (die, c, unmark_dies (c));
7689 /* Clear the marks for a die, its children and referred dies. */
7691 static void
7692 unmark_all_dies (dw_die_ref die)
7694 dw_die_ref c;
7695 dw_attr_ref a;
7696 unsigned ix;
7698 if (!die->die_mark)
7699 return;
7700 die->die_mark = 0;
7702 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7704 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7705 if (AT_class (a) == dw_val_class_die_ref)
7706 unmark_all_dies (AT_ref (a));
7709 /* Return the size of the .debug_pubnames or .debug_pubtypes table
7710 generated for the compilation unit. */
7712 static unsigned long
7713 size_of_pubnames (VEC (pubname_entry, gc) * names)
7715 unsigned long size;
7716 unsigned i;
7717 pubname_ref p;
7719 size = DWARF_PUBNAMES_HEADER_SIZE;
7720 for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
7721 if (names != pubtype_table
7722 || p->die->die_offset != 0
7723 || !flag_eliminate_unused_debug_types)
7724 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
7726 size += DWARF_OFFSET_SIZE;
7727 return size;
7730 /* Return the size of the information in the .debug_aranges section. */
7732 static unsigned long
7733 size_of_aranges (void)
7735 unsigned long size;
7737 size = DWARF_ARANGES_HEADER_SIZE;
7739 /* Count the address/length pair for this compilation unit. */
7740 if (text_section_used)
7741 size += 2 * DWARF2_ADDR_SIZE;
7742 if (cold_text_section_used)
7743 size += 2 * DWARF2_ADDR_SIZE;
7744 size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
7746 /* Count the two zero words used to terminated the address range table. */
7747 size += 2 * DWARF2_ADDR_SIZE;
7748 return size;
7751 /* Select the encoding of an attribute value. */
7753 static enum dwarf_form
7754 value_format (dw_attr_ref a)
7756 switch (a->dw_attr_val.val_class)
7758 case dw_val_class_addr:
7759 return DW_FORM_addr;
7760 case dw_val_class_range_list:
7761 case dw_val_class_offset:
7762 case dw_val_class_loc_list:
7763 switch (DWARF_OFFSET_SIZE)
7765 case 4:
7766 return DW_FORM_data4;
7767 case 8:
7768 return DW_FORM_data8;
7769 default:
7770 gcc_unreachable ();
7772 case dw_val_class_loc:
7773 switch (constant_size (size_of_locs (AT_loc (a))))
7775 case 1:
7776 return DW_FORM_block1;
7777 case 2:
7778 return DW_FORM_block2;
7779 default:
7780 gcc_unreachable ();
7782 case dw_val_class_const:
7783 return DW_FORM_sdata;
7784 case dw_val_class_unsigned_const:
7785 switch (constant_size (AT_unsigned (a)))
7787 case 1:
7788 return DW_FORM_data1;
7789 case 2:
7790 return DW_FORM_data2;
7791 case 4:
7792 return DW_FORM_data4;
7793 case 8:
7794 return DW_FORM_data8;
7795 default:
7796 gcc_unreachable ();
7798 case dw_val_class_long_long:
7799 return DW_FORM_block1;
7800 case dw_val_class_vec:
7801 switch (constant_size (a->dw_attr_val.v.val_vec.length
7802 * a->dw_attr_val.v.val_vec.elt_size))
7804 case 1:
7805 return DW_FORM_block1;
7806 case 2:
7807 return DW_FORM_block2;
7808 case 4:
7809 return DW_FORM_block4;
7810 default:
7811 gcc_unreachable ();
7813 case dw_val_class_flag:
7814 return DW_FORM_flag;
7815 case dw_val_class_die_ref:
7816 if (AT_ref_external (a))
7817 return DW_FORM_ref_addr;
7818 else
7819 return DW_FORM_ref;
7820 case dw_val_class_fde_ref:
7821 return DW_FORM_data;
7822 case dw_val_class_lbl_id:
7823 return DW_FORM_addr;
7824 case dw_val_class_lineptr:
7825 case dw_val_class_macptr:
7826 return DW_FORM_data;
7827 case dw_val_class_str:
7828 return AT_string_form (a);
7829 case dw_val_class_file:
7830 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
7832 case 1:
7833 return DW_FORM_data1;
7834 case 2:
7835 return DW_FORM_data2;
7836 case 4:
7837 return DW_FORM_data4;
7838 default:
7839 gcc_unreachable ();
7842 default:
7843 gcc_unreachable ();
7847 /* Output the encoding of an attribute value. */
7849 static void
7850 output_value_format (dw_attr_ref a)
7852 enum dwarf_form form = value_format (a);
7854 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
7857 /* Output the .debug_abbrev section which defines the DIE abbreviation
7858 table. */
7860 static void
7861 output_abbrev_section (void)
7863 unsigned long abbrev_id;
7865 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7867 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7868 unsigned ix;
7869 dw_attr_ref a_attr;
7871 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
7872 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
7873 dwarf_tag_name (abbrev->die_tag));
7875 if (abbrev->die_child != NULL)
7876 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
7877 else
7878 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
7880 for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
7881 ix++)
7883 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
7884 dwarf_attr_name (a_attr->dw_attr));
7885 output_value_format (a_attr);
7888 dw2_asm_output_data (1, 0, NULL);
7889 dw2_asm_output_data (1, 0, NULL);
7892 /* Terminate the table. */
7893 dw2_asm_output_data (1, 0, NULL);
7896 /* Output a symbol we can use to refer to this DIE from another CU. */
7898 static inline void
7899 output_die_symbol (dw_die_ref die)
7901 char *sym = die->die_symbol;
7903 if (sym == 0)
7904 return;
7906 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
7907 /* We make these global, not weak; if the target doesn't support
7908 .linkonce, it doesn't support combining the sections, so debugging
7909 will break. */
7910 targetm.asm_out.globalize_label (asm_out_file, sym);
7912 ASM_OUTPUT_LABEL (asm_out_file, sym);
7915 /* Return a new location list, given the begin and end range, and the
7916 expression. gensym tells us whether to generate a new internal symbol for
7917 this location list node, which is done for the head of the list only. */
7919 static inline dw_loc_list_ref
7920 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
7921 const char *section, unsigned int gensym)
7923 dw_loc_list_ref retlist = GGC_CNEW (dw_loc_list_node);
7925 retlist->begin = begin;
7926 retlist->end = end;
7927 retlist->expr = expr;
7928 retlist->section = section;
7929 if (gensym)
7930 retlist->ll_symbol = gen_internal_sym ("LLST");
7932 return retlist;
7935 /* Add a location description expression to a location list. */
7937 static inline void
7938 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
7939 const char *begin, const char *end,
7940 const char *section)
7942 dw_loc_list_ref *d;
7944 /* Find the end of the chain. */
7945 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
7948 /* Add a new location list node to the list. */
7949 *d = new_loc_list (descr, begin, end, section, 0);
7952 /* Output the location list given to us. */
7954 static void
7955 output_loc_list (dw_loc_list_ref list_head)
7957 dw_loc_list_ref curr = list_head;
7959 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
7961 /* Walk the location list, and output each range + expression. */
7962 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
7964 unsigned long size;
7965 /* Don't output an entry that starts and ends at the same address. */
7966 if (strcmp (curr->begin, curr->end) == 0)
7967 continue;
7968 if (!have_multiple_function_sections)
7970 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
7971 "Location list begin address (%s)",
7972 list_head->ll_symbol);
7973 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
7974 "Location list end address (%s)",
7975 list_head->ll_symbol);
7977 else
7979 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
7980 "Location list begin address (%s)",
7981 list_head->ll_symbol);
7982 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
7983 "Location list end address (%s)",
7984 list_head->ll_symbol);
7986 size = size_of_locs (curr->expr);
7988 /* Output the block length for this list of location operations. */
7989 gcc_assert (size <= 0xffff);
7990 dw2_asm_output_data (2, size, "%s", "Location expression size");
7992 output_loc_sequence (curr->expr);
7995 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7996 "Location list terminator begin (%s)",
7997 list_head->ll_symbol);
7998 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7999 "Location list terminator end (%s)",
8000 list_head->ll_symbol);
8003 /* Output the DIE and its attributes. Called recursively to generate
8004 the definitions of each child DIE. */
8006 static void
8007 output_die (dw_die_ref die)
8009 dw_attr_ref a;
8010 dw_die_ref c;
8011 unsigned long size;
8012 unsigned ix;
8014 /* If someone in another CU might refer to us, set up a symbol for
8015 them to point to. */
8016 if (die->die_symbol)
8017 output_die_symbol (die);
8019 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
8020 (unsigned long)die->die_offset,
8021 dwarf_tag_name (die->die_tag));
8023 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8025 const char *name = dwarf_attr_name (a->dw_attr);
8027 switch (AT_class (a))
8029 case dw_val_class_addr:
8030 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8031 break;
8033 case dw_val_class_offset:
8034 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8035 "%s", name);
8036 break;
8038 case dw_val_class_range_list:
8040 char *p = strchr (ranges_section_label, '\0');
8042 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
8043 a->dw_attr_val.v.val_offset);
8044 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8045 debug_ranges_section, "%s", name);
8046 *p = '\0';
8048 break;
8050 case dw_val_class_loc:
8051 size = size_of_locs (AT_loc (a));
8053 /* Output the block length for this list of location operations. */
8054 dw2_asm_output_data (constant_size (size), size, "%s", name);
8056 output_loc_sequence (AT_loc (a));
8057 break;
8059 case dw_val_class_const:
8060 /* ??? It would be slightly more efficient to use a scheme like is
8061 used for unsigned constants below, but gdb 4.x does not sign
8062 extend. Gdb 5.x does sign extend. */
8063 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8064 break;
8066 case dw_val_class_unsigned_const:
8067 dw2_asm_output_data (constant_size (AT_unsigned (a)),
8068 AT_unsigned (a), "%s", name);
8069 break;
8071 case dw_val_class_long_long:
8073 unsigned HOST_WIDE_INT first, second;
8075 dw2_asm_output_data (1,
8076 2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8077 "%s", name);
8079 if (WORDS_BIG_ENDIAN)
8081 first = a->dw_attr_val.v.val_long_long.hi;
8082 second = a->dw_attr_val.v.val_long_long.low;
8084 else
8086 first = a->dw_attr_val.v.val_long_long.low;
8087 second = a->dw_attr_val.v.val_long_long.hi;
8090 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8091 first, "long long constant");
8092 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8093 second, NULL);
8095 break;
8097 case dw_val_class_vec:
8099 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8100 unsigned int len = a->dw_attr_val.v.val_vec.length;
8101 unsigned int i;
8102 unsigned char *p;
8104 dw2_asm_output_data (constant_size (len * elt_size),
8105 len * elt_size, "%s", name);
8106 if (elt_size > sizeof (HOST_WIDE_INT))
8108 elt_size /= 2;
8109 len *= 2;
8111 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8112 i < len;
8113 i++, p += elt_size)
8114 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8115 "fp or vector constant word %u", i);
8116 break;
8119 case dw_val_class_flag:
8120 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8121 break;
8123 case dw_val_class_loc_list:
8125 char *sym = AT_loc_list (a)->ll_symbol;
8127 gcc_assert (sym);
8128 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8129 "%s", name);
8131 break;
8133 case dw_val_class_die_ref:
8134 if (AT_ref_external (a))
8136 char *sym = AT_ref (a)->die_symbol;
8138 gcc_assert (sym);
8139 dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, debug_info_section,
8140 "%s", name);
8142 else
8144 gcc_assert (AT_ref (a)->die_offset);
8145 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8146 "%s", name);
8148 break;
8150 case dw_val_class_fde_ref:
8152 char l1[20];
8154 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8155 a->dw_attr_val.v.val_fde_index * 2);
8156 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8157 "%s", name);
8159 break;
8161 case dw_val_class_lbl_id:
8162 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8163 break;
8165 case dw_val_class_lineptr:
8166 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8167 debug_line_section, "%s", name);
8168 break;
8170 case dw_val_class_macptr:
8171 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8172 debug_macinfo_section, "%s", name);
8173 break;
8175 case dw_val_class_str:
8176 if (AT_string_form (a) == DW_FORM_strp)
8177 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8178 a->dw_attr_val.v.val_str->label,
8179 debug_str_section,
8180 "%s: \"%s\"", name, AT_string (a));
8181 else
8182 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8183 break;
8185 case dw_val_class_file:
8187 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8189 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8190 a->dw_attr_val.v.val_file->filename);
8191 break;
8194 default:
8195 gcc_unreachable ();
8199 FOR_EACH_CHILD (die, c, output_die (c));
8201 /* Add null byte to terminate sibling list. */
8202 if (die->die_child != NULL)
8203 dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
8204 (unsigned long) die->die_offset);
8207 /* Output the compilation unit that appears at the beginning of the
8208 .debug_info section, and precedes the DIE descriptions. */
8210 static void
8211 output_compilation_unit_header (void)
8213 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8214 dw2_asm_output_data (4, 0xffffffff,
8215 "Initial length escape value indicating 64-bit DWARF extension");
8216 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8217 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8218 "Length of Compilation Unit Info");
8219 dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
8220 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8221 debug_abbrev_section,
8222 "Offset Into Abbrev. Section");
8223 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8226 /* Output the compilation unit DIE and its children. */
8228 static void
8229 output_comp_unit (dw_die_ref die, int output_if_empty)
8231 const char *secname;
8232 char *oldsym, *tmp;
8234 /* Unless we are outputting main CU, we may throw away empty ones. */
8235 if (!output_if_empty && die->die_child == NULL)
8236 return;
8238 /* Even if there are no children of this DIE, we must output the information
8239 about the compilation unit. Otherwise, on an empty translation unit, we
8240 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
8241 will then complain when examining the file. First mark all the DIEs in
8242 this CU so we know which get local refs. */
8243 mark_dies (die);
8245 build_abbrev_table (die);
8247 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8248 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8249 calc_die_sizes (die);
8251 oldsym = die->die_symbol;
8252 if (oldsym)
8254 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8256 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8257 secname = tmp;
8258 die->die_symbol = NULL;
8259 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8261 else
8262 switch_to_section (debug_info_section);
8264 /* Output debugging information. */
8265 output_compilation_unit_header ();
8266 output_die (die);
8268 /* Leave the marks on the main CU, so we can check them in
8269 output_pubnames. */
8270 if (oldsym)
8272 unmark_dies (die);
8273 die->die_symbol = oldsym;
8277 /* Return the DWARF2/3 pubname associated with a decl. */
8279 static const char *
8280 dwarf2_name (tree decl, int scope)
8282 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
8285 /* Add a new entry to .debug_pubnames if appropriate. */
8287 static void
8288 add_pubname_string (const char *str, dw_die_ref die)
8290 pubname_entry e;
8292 e.die = die;
8293 e.name = xstrdup (str);
8294 VEC_safe_push (pubname_entry, gc, pubname_table, &e);
8297 static void
8298 add_pubname (tree decl, dw_die_ref die)
8301 if (TREE_PUBLIC (decl))
8302 add_pubname_string (dwarf2_name (decl, 1), die);
8305 /* Add a new entry to .debug_pubtypes if appropriate. */
8307 static void
8308 add_pubtype (tree decl, dw_die_ref die)
8310 pubname_entry e;
8312 e.name = NULL;
8313 if ((TREE_PUBLIC (decl)
8314 || die->die_parent == comp_unit_die)
8315 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
8317 e.die = die;
8318 if (TYPE_P (decl))
8320 if (TYPE_NAME (decl))
8322 if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
8323 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
8324 else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
8325 && DECL_NAME (TYPE_NAME (decl)))
8326 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
8327 else
8328 e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
8331 else
8332 e.name = xstrdup (dwarf2_name (decl, 1));
8334 /* If we don't have a name for the type, there's no point in adding
8335 it to the table. */
8336 if (e.name && e.name[0] != '\0')
8337 VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
8341 /* Output the public names table used to speed up access to externally
8342 visible names; or the public types table used to find type definitions. */
8344 static void
8345 output_pubnames (VEC (pubname_entry, gc) * names)
8347 unsigned i;
8348 unsigned long pubnames_length = size_of_pubnames (names);
8349 pubname_ref pub;
8351 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8352 dw2_asm_output_data (4, 0xffffffff,
8353 "Initial length escape value indicating 64-bit DWARF extension");
8354 if (names == pubname_table)
8355 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8356 "Length of Public Names Info");
8357 else
8358 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8359 "Length of Public Type Names Info");
8360 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8361 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8362 debug_info_section,
8363 "Offset of Compilation Unit Info");
8364 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
8365 "Compilation Unit Length");
8367 for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
8369 /* We shouldn't see pubnames for DIEs outside of the main CU. */
8370 if (names == pubname_table)
8371 gcc_assert (pub->die->die_mark);
8373 if (names != pubtype_table
8374 || pub->die->die_offset != 0
8375 || !flag_eliminate_unused_debug_types)
8377 dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
8378 "DIE offset");
8380 dw2_asm_output_nstring (pub->name, -1, "external name");
8384 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
8387 /* Add a new entry to .debug_aranges if appropriate. */
8389 static void
8390 add_arange (tree decl, dw_die_ref die)
8392 if (! DECL_SECTION_NAME (decl))
8393 return;
8395 if (arange_table_in_use == arange_table_allocated)
8397 arange_table_allocated += ARANGE_TABLE_INCREMENT;
8398 arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
8399 arange_table_allocated);
8400 memset (arange_table + arange_table_in_use, 0,
8401 ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
8404 arange_table[arange_table_in_use++] = die;
8407 /* Output the information that goes into the .debug_aranges table.
8408 Namely, define the beginning and ending address range of the
8409 text section generated for this compilation unit. */
8411 static void
8412 output_aranges (void)
8414 unsigned i;
8415 unsigned long aranges_length = size_of_aranges ();
8417 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8418 dw2_asm_output_data (4, 0xffffffff,
8419 "Initial length escape value indicating 64-bit DWARF extension");
8420 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
8421 "Length of Address Ranges Info");
8422 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8423 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8424 debug_info_section,
8425 "Offset of Compilation Unit Info");
8426 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
8427 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
8429 /* We need to align to twice the pointer size here. */
8430 if (DWARF_ARANGES_PAD_SIZE)
8432 /* Pad using a 2 byte words so that padding is correct for any
8433 pointer size. */
8434 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
8435 2 * DWARF2_ADDR_SIZE);
8436 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
8437 dw2_asm_output_data (2, 0, NULL);
8440 /* It is necessary not to output these entries if the sections were
8441 not used; if the sections were not used, the length will be 0 and
8442 the address may end up as 0 if the section is discarded by ld
8443 --gc-sections, leaving an invalid (0, 0) entry that can be
8444 confused with the terminator. */
8445 if (text_section_used)
8447 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
8448 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
8449 text_section_label, "Length");
8451 if (cold_text_section_used)
8453 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
8454 "Address");
8455 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
8456 cold_text_section_label, "Length");
8459 for (i = 0; i < arange_table_in_use; i++)
8461 dw_die_ref die = arange_table[i];
8463 /* We shouldn't see aranges for DIEs outside of the main CU. */
8464 gcc_assert (die->die_mark);
8466 if (die->die_tag == DW_TAG_subprogram)
8468 dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
8469 "Address");
8470 dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
8471 get_AT_low_pc (die), "Length");
8473 else
8475 /* A static variable; extract the symbol from DW_AT_location.
8476 Note that this code isn't currently hit, as we only emit
8477 aranges for functions (jason 9/23/99). */
8478 dw_attr_ref a = get_AT (die, DW_AT_location);
8479 dw_loc_descr_ref loc;
8481 gcc_assert (a && AT_class (a) == dw_val_class_loc);
8483 loc = AT_loc (a);
8484 gcc_assert (loc->dw_loc_opc == DW_OP_addr);
8486 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
8487 loc->dw_loc_oprnd1.v.val_addr, "Address");
8488 dw2_asm_output_data (DWARF2_ADDR_SIZE,
8489 get_AT_unsigned (die, DW_AT_byte_size),
8490 "Length");
8494 /* Output the terminator words. */
8495 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8496 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8499 /* Add a new entry to .debug_ranges. Return the offset at which it
8500 was placed. */
8502 static unsigned int
8503 add_ranges_num (int num)
8505 unsigned int in_use = ranges_table_in_use;
8507 if (in_use == ranges_table_allocated)
8509 ranges_table_allocated += RANGES_TABLE_INCREMENT;
8510 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
8511 ranges_table_allocated);
8512 memset (ranges_table + ranges_table_in_use, 0,
8513 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
8516 ranges_table[in_use].num = num;
8517 ranges_table_in_use = in_use + 1;
8519 return in_use * 2 * DWARF2_ADDR_SIZE;
8522 /* Add a new entry to .debug_ranges corresponding to a block, or a
8523 range terminator if BLOCK is NULL. */
8525 static unsigned int
8526 add_ranges (const_tree block)
8528 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
8531 /* Add a new entry to .debug_ranges corresponding to a pair of
8532 labels. */
8534 static unsigned int
8535 add_ranges_by_labels (const char *begin, const char *end)
8537 unsigned int in_use = ranges_by_label_in_use;
8539 if (in_use == ranges_by_label_allocated)
8541 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
8542 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
8543 ranges_by_label,
8544 ranges_by_label_allocated);
8545 memset (ranges_by_label + ranges_by_label_in_use, 0,
8546 RANGES_TABLE_INCREMENT
8547 * sizeof (struct dw_ranges_by_label_struct));
8550 ranges_by_label[in_use].begin = begin;
8551 ranges_by_label[in_use].end = end;
8552 ranges_by_label_in_use = in_use + 1;
8554 return add_ranges_num (-(int)in_use - 1);
8557 static void
8558 output_ranges (void)
8560 unsigned i;
8561 static const char *const start_fmt = "Offset 0x%x";
8562 const char *fmt = start_fmt;
8564 for (i = 0; i < ranges_table_in_use; i++)
8566 int block_num = ranges_table[i].num;
8568 if (block_num > 0)
8570 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
8571 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
8573 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
8574 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
8576 /* If all code is in the text section, then the compilation
8577 unit base address defaults to DW_AT_low_pc, which is the
8578 base of the text section. */
8579 if (!have_multiple_function_sections)
8581 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
8582 text_section_label,
8583 fmt, i * 2 * DWARF2_ADDR_SIZE);
8584 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
8585 text_section_label, NULL);
8588 /* Otherwise, the compilation unit base address is zero,
8589 which allows us to use absolute addresses, and not worry
8590 about whether the target supports cross-section
8591 arithmetic. */
8592 else
8594 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
8595 fmt, i * 2 * DWARF2_ADDR_SIZE);
8596 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
8599 fmt = NULL;
8602 /* Negative block_num stands for an index into ranges_by_label. */
8603 else if (block_num < 0)
8605 int lab_idx = - block_num - 1;
8607 if (!have_multiple_function_sections)
8609 gcc_unreachable ();
8610 #if 0
8611 /* If we ever use add_ranges_by_labels () for a single
8612 function section, all we have to do is to take out
8613 the #if 0 above. */
8614 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8615 ranges_by_label[lab_idx].begin,
8616 text_section_label,
8617 fmt, i * 2 * DWARF2_ADDR_SIZE);
8618 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8619 ranges_by_label[lab_idx].end,
8620 text_section_label, NULL);
8621 #endif
8623 else
8625 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8626 ranges_by_label[lab_idx].begin,
8627 fmt, i * 2 * DWARF2_ADDR_SIZE);
8628 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8629 ranges_by_label[lab_idx].end,
8630 NULL);
8633 else
8635 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8636 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8637 fmt = start_fmt;
8642 /* Data structure containing information about input files. */
8643 struct file_info
8645 const char *path; /* Complete file name. */
8646 const char *fname; /* File name part. */
8647 int length; /* Length of entire string. */
8648 struct dwarf_file_data * file_idx; /* Index in input file table. */
8649 int dir_idx; /* Index in directory table. */
8652 /* Data structure containing information about directories with source
8653 files. */
8654 struct dir_info
8656 const char *path; /* Path including directory name. */
8657 int length; /* Path length. */
8658 int prefix; /* Index of directory entry which is a prefix. */
8659 int count; /* Number of files in this directory. */
8660 int dir_idx; /* Index of directory used as base. */
8663 /* Callback function for file_info comparison. We sort by looking at
8664 the directories in the path. */
8666 static int
8667 file_info_cmp (const void *p1, const void *p2)
8669 const struct file_info *const s1 = (const struct file_info *) p1;
8670 const struct file_info *const s2 = (const struct file_info *) p2;
8671 const unsigned char *cp1;
8672 const unsigned char *cp2;
8674 /* Take care of file names without directories. We need to make sure that
8675 we return consistent values to qsort since some will get confused if
8676 we return the same value when identical operands are passed in opposite
8677 orders. So if neither has a directory, return 0 and otherwise return
8678 1 or -1 depending on which one has the directory. */
8679 if ((s1->path == s1->fname || s2->path == s2->fname))
8680 return (s2->path == s2->fname) - (s1->path == s1->fname);
8682 cp1 = (const unsigned char *) s1->path;
8683 cp2 = (const unsigned char *) s2->path;
8685 while (1)
8687 ++cp1;
8688 ++cp2;
8689 /* Reached the end of the first path? If so, handle like above. */
8690 if ((cp1 == (const unsigned char *) s1->fname)
8691 || (cp2 == (const unsigned char *) s2->fname))
8692 return ((cp2 == (const unsigned char *) s2->fname)
8693 - (cp1 == (const unsigned char *) s1->fname));
8695 /* Character of current path component the same? */
8696 else if (*cp1 != *cp2)
8697 return *cp1 - *cp2;
8701 struct file_name_acquire_data
8703 struct file_info *files;
8704 int used_files;
8705 int max_files;
8708 /* Traversal function for the hash table. */
8710 static int
8711 file_name_acquire (void ** slot, void *data)
8713 struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
8714 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
8715 struct file_info *fi;
8716 const char *f;
8718 gcc_assert (fnad->max_files >= d->emitted_number);
8720 if (! d->emitted_number)
8721 return 1;
8723 gcc_assert (fnad->max_files != fnad->used_files);
8725 fi = fnad->files + fnad->used_files++;
8727 /* Skip all leading "./". */
8728 f = d->filename;
8729 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
8730 f += 2;
8732 /* Create a new array entry. */
8733 fi->path = f;
8734 fi->length = strlen (f);
8735 fi->file_idx = d;
8737 /* Search for the file name part. */
8738 f = strrchr (f, DIR_SEPARATOR);
8739 #if defined (DIR_SEPARATOR_2)
8741 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
8743 if (g != NULL)
8745 if (f == NULL || f < g)
8746 f = g;
8749 #endif
8751 fi->fname = f == NULL ? fi->path : f + 1;
8752 return 1;
8755 /* Output the directory table and the file name table. We try to minimize
8756 the total amount of memory needed. A heuristic is used to avoid large
8757 slowdowns with many input files. */
8759 static void
8760 output_file_names (void)
8762 struct file_name_acquire_data fnad;
8763 int numfiles;
8764 struct file_info *files;
8765 struct dir_info *dirs;
8766 int *saved;
8767 int *savehere;
8768 int *backmap;
8769 int ndirs;
8770 int idx_offset;
8771 int i;
8772 int idx;
8774 if (!last_emitted_file)
8776 dw2_asm_output_data (1, 0, "End directory table");
8777 dw2_asm_output_data (1, 0, "End file name table");
8778 return;
8781 numfiles = last_emitted_file->emitted_number;
8783 /* Allocate the various arrays we need. */
8784 files = XALLOCAVEC (struct file_info, numfiles);
8785 dirs = XALLOCAVEC (struct dir_info, numfiles);
8787 fnad.files = files;
8788 fnad.used_files = 0;
8789 fnad.max_files = numfiles;
8790 htab_traverse (file_table, file_name_acquire, &fnad);
8791 gcc_assert (fnad.used_files == fnad.max_files);
8793 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
8795 /* Find all the different directories used. */
8796 dirs[0].path = files[0].path;
8797 dirs[0].length = files[0].fname - files[0].path;
8798 dirs[0].prefix = -1;
8799 dirs[0].count = 1;
8800 dirs[0].dir_idx = 0;
8801 files[0].dir_idx = 0;
8802 ndirs = 1;
8804 for (i = 1; i < numfiles; i++)
8805 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
8806 && memcmp (dirs[ndirs - 1].path, files[i].path,
8807 dirs[ndirs - 1].length) == 0)
8809 /* Same directory as last entry. */
8810 files[i].dir_idx = ndirs - 1;
8811 ++dirs[ndirs - 1].count;
8813 else
8815 int j;
8817 /* This is a new directory. */
8818 dirs[ndirs].path = files[i].path;
8819 dirs[ndirs].length = files[i].fname - files[i].path;
8820 dirs[ndirs].count = 1;
8821 dirs[ndirs].dir_idx = ndirs;
8822 files[i].dir_idx = ndirs;
8824 /* Search for a prefix. */
8825 dirs[ndirs].prefix = -1;
8826 for (j = 0; j < ndirs; j++)
8827 if (dirs[j].length < dirs[ndirs].length
8828 && dirs[j].length > 1
8829 && (dirs[ndirs].prefix == -1
8830 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
8831 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
8832 dirs[ndirs].prefix = j;
8834 ++ndirs;
8837 /* Now to the actual work. We have to find a subset of the directories which
8838 allow expressing the file name using references to the directory table
8839 with the least amount of characters. We do not do an exhaustive search
8840 where we would have to check out every combination of every single
8841 possible prefix. Instead we use a heuristic which provides nearly optimal
8842 results in most cases and never is much off. */
8843 saved = XALLOCAVEC (int, ndirs);
8844 savehere = XALLOCAVEC (int, ndirs);
8846 memset (saved, '\0', ndirs * sizeof (saved[0]));
8847 for (i = 0; i < ndirs; i++)
8849 int j;
8850 int total;
8852 /* We can always save some space for the current directory. But this
8853 does not mean it will be enough to justify adding the directory. */
8854 savehere[i] = dirs[i].length;
8855 total = (savehere[i] - saved[i]) * dirs[i].count;
8857 for (j = i + 1; j < ndirs; j++)
8859 savehere[j] = 0;
8860 if (saved[j] < dirs[i].length)
8862 /* Determine whether the dirs[i] path is a prefix of the
8863 dirs[j] path. */
8864 int k;
8866 k = dirs[j].prefix;
8867 while (k != -1 && k != (int) i)
8868 k = dirs[k].prefix;
8870 if (k == (int) i)
8872 /* Yes it is. We can possibly save some memory by
8873 writing the filenames in dirs[j] relative to
8874 dirs[i]. */
8875 savehere[j] = dirs[i].length;
8876 total += (savehere[j] - saved[j]) * dirs[j].count;
8881 /* Check whether we can save enough to justify adding the dirs[i]
8882 directory. */
8883 if (total > dirs[i].length + 1)
8885 /* It's worthwhile adding. */
8886 for (j = i; j < ndirs; j++)
8887 if (savehere[j] > 0)
8889 /* Remember how much we saved for this directory so far. */
8890 saved[j] = savehere[j];
8892 /* Remember the prefix directory. */
8893 dirs[j].dir_idx = i;
8898 /* Emit the directory name table. */
8899 idx = 1;
8900 idx_offset = dirs[0].length > 0 ? 1 : 0;
8901 for (i = 1 - idx_offset; i < ndirs; i++)
8902 dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
8903 "Directory Entry: 0x%x", i + idx_offset);
8905 dw2_asm_output_data (1, 0, "End directory table");
8907 /* We have to emit them in the order of emitted_number since that's
8908 used in the debug info generation. To do this efficiently we
8909 generate a back-mapping of the indices first. */
8910 backmap = XALLOCAVEC (int, numfiles);
8911 for (i = 0; i < numfiles; i++)
8912 backmap[files[i].file_idx->emitted_number - 1] = i;
8914 /* Now write all the file names. */
8915 for (i = 0; i < numfiles; i++)
8917 int file_idx = backmap[i];
8918 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
8920 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
8921 "File Entry: 0x%x", (unsigned) i + 1);
8923 /* Include directory index. */
8924 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
8926 /* Modification time. */
8927 dw2_asm_output_data_uleb128 (0, NULL);
8929 /* File length in bytes. */
8930 dw2_asm_output_data_uleb128 (0, NULL);
8933 dw2_asm_output_data (1, 0, "End file name table");
8937 /* Output the source line number correspondence information. This
8938 information goes into the .debug_line section. */
8940 static void
8941 output_line_info (void)
8943 char l1[20], l2[20], p1[20], p2[20];
8944 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8945 char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8946 unsigned opc;
8947 unsigned n_op_args;
8948 unsigned long lt_index;
8949 unsigned long current_line;
8950 long line_offset;
8951 long line_delta;
8952 unsigned long current_file;
8953 unsigned long function;
8955 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
8956 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
8957 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
8958 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
8960 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8961 dw2_asm_output_data (4, 0xffffffff,
8962 "Initial length escape value indicating 64-bit DWARF extension");
8963 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
8964 "Length of Source Line Info");
8965 ASM_OUTPUT_LABEL (asm_out_file, l1);
8967 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8968 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
8969 ASM_OUTPUT_LABEL (asm_out_file, p1);
8971 /* Define the architecture-dependent minimum instruction length (in
8972 bytes). In this implementation of DWARF, this field is used for
8973 information purposes only. Since GCC generates assembly language,
8974 we have no a priori knowledge of how many instruction bytes are
8975 generated for each source line, and therefore can use only the
8976 DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
8977 commands. Accordingly, we fix this as `1', which is "correct
8978 enough" for all architectures, and don't let the target override. */
8979 dw2_asm_output_data (1, 1,
8980 "Minimum Instruction Length");
8982 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
8983 "Default is_stmt_start flag");
8984 dw2_asm_output_data (1, DWARF_LINE_BASE,
8985 "Line Base Value (Special Opcodes)");
8986 dw2_asm_output_data (1, DWARF_LINE_RANGE,
8987 "Line Range Value (Special Opcodes)");
8988 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
8989 "Special Opcode Base");
8991 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
8993 switch (opc)
8995 case DW_LNS_advance_pc:
8996 case DW_LNS_advance_line:
8997 case DW_LNS_set_file:
8998 case DW_LNS_set_column:
8999 case DW_LNS_fixed_advance_pc:
9000 n_op_args = 1;
9001 break;
9002 default:
9003 n_op_args = 0;
9004 break;
9007 dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
9008 opc, n_op_args);
9011 /* Write out the information about the files we use. */
9012 output_file_names ();
9013 ASM_OUTPUT_LABEL (asm_out_file, p2);
9015 /* We used to set the address register to the first location in the text
9016 section here, but that didn't accomplish anything since we already
9017 have a line note for the opening brace of the first function. */
9019 /* Generate the line number to PC correspondence table, encoded as
9020 a series of state machine operations. */
9021 current_file = 1;
9022 current_line = 1;
9024 if (cfun && in_cold_section_p)
9025 strcpy (prev_line_label, crtl->subsections.cold_section_label);
9026 else
9027 strcpy (prev_line_label, text_section_label);
9028 for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
9030 dw_line_info_ref line_info = &line_info_table[lt_index];
9032 #if 0
9033 /* Disable this optimization for now; GDB wants to see two line notes
9034 at the beginning of a function so it can find the end of the
9035 prologue. */
9037 /* Don't emit anything for redundant notes. Just updating the
9038 address doesn't accomplish anything, because we already assume
9039 that anything after the last address is this line. */
9040 if (line_info->dw_line_num == current_line
9041 && line_info->dw_file_num == current_file)
9042 continue;
9043 #endif
9045 /* Emit debug info for the address of the current line.
9047 Unfortunately, we have little choice here currently, and must always
9048 use the most general form. GCC does not know the address delta
9049 itself, so we can't use DW_LNS_advance_pc. Many ports do have length
9050 attributes which will give an upper bound on the address range. We
9051 could perhaps use length attributes to determine when it is safe to
9052 use DW_LNS_fixed_advance_pc. */
9054 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
9055 if (0)
9057 /* This can handle deltas up to 0xffff. This takes 3 bytes. */
9058 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9059 "DW_LNS_fixed_advance_pc");
9060 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9062 else
9064 /* This can handle any delta. This takes
9065 4+DWARF2_ADDR_SIZE bytes. */
9066 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9067 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9068 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9069 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9072 strcpy (prev_line_label, line_label);
9074 /* Emit debug info for the source file of the current line, if
9075 different from the previous line. */
9076 if (line_info->dw_file_num != current_file)
9078 current_file = line_info->dw_file_num;
9079 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
9080 dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
9083 /* Emit debug info for the current line number, choosing the encoding
9084 that uses the least amount of space. */
9085 if (line_info->dw_line_num != current_line)
9087 line_offset = line_info->dw_line_num - current_line;
9088 line_delta = line_offset - DWARF_LINE_BASE;
9089 current_line = line_info->dw_line_num;
9090 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9091 /* This can handle deltas from -10 to 234, using the current
9092 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE. This
9093 takes 1 byte. */
9094 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9095 "line %lu", current_line);
9096 else
9098 /* This can handle any delta. This takes at least 4 bytes,
9099 depending on the value being encoded. */
9100 dw2_asm_output_data (1, DW_LNS_advance_line,
9101 "advance to line %lu", current_line);
9102 dw2_asm_output_data_sleb128 (line_offset, NULL);
9103 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9106 else
9107 /* We still need to start a new row, so output a copy insn. */
9108 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9111 /* Emit debug info for the address of the end of the function. */
9112 if (0)
9114 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9115 "DW_LNS_fixed_advance_pc");
9116 dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
9118 else
9120 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9121 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9122 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9123 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
9126 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
9127 dw2_asm_output_data_uleb128 (1, NULL);
9128 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9130 function = 0;
9131 current_file = 1;
9132 current_line = 1;
9133 for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
9135 dw_separate_line_info_ref line_info
9136 = &separate_line_info_table[lt_index];
9138 #if 0
9139 /* Don't emit anything for redundant notes. */
9140 if (line_info->dw_line_num == current_line
9141 && line_info->dw_file_num == current_file
9142 && line_info->function == function)
9143 goto cont;
9144 #endif
9146 /* Emit debug info for the address of the current line. If this is
9147 a new function, or the first line of a function, then we need
9148 to handle it differently. */
9149 ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
9150 lt_index);
9151 if (function != line_info->function)
9153 function = line_info->function;
9155 /* Set the address register to the first line in the function. */
9156 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9157 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9158 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9159 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9161 else
9163 /* ??? See the DW_LNS_advance_pc comment above. */
9164 if (0)
9166 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9167 "DW_LNS_fixed_advance_pc");
9168 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9170 else
9172 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9173 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9174 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9175 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9179 strcpy (prev_line_label, line_label);
9181 /* Emit debug info for the source file of the current line, if
9182 different from the previous line. */
9183 if (line_info->dw_file_num != current_file)
9185 current_file = line_info->dw_file_num;
9186 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
9187 dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
9190 /* Emit debug info for the current line number, choosing the encoding
9191 that uses the least amount of space. */
9192 if (line_info->dw_line_num != current_line)
9194 line_offset = line_info->dw_line_num - current_line;
9195 line_delta = line_offset - DWARF_LINE_BASE;
9196 current_line = line_info->dw_line_num;
9197 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9198 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9199 "line %lu", current_line);
9200 else
9202 dw2_asm_output_data (1, DW_LNS_advance_line,
9203 "advance to line %lu", current_line);
9204 dw2_asm_output_data_sleb128 (line_offset, NULL);
9205 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9208 else
9209 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9211 #if 0
9212 cont:
9213 #endif
9215 lt_index++;
9217 /* If we're done with a function, end its sequence. */
9218 if (lt_index == separate_line_info_table_in_use
9219 || separate_line_info_table[lt_index].function != function)
9221 current_file = 1;
9222 current_line = 1;
9224 /* Emit debug info for the address of the end of the function. */
9225 ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
9226 if (0)
9228 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9229 "DW_LNS_fixed_advance_pc");
9230 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9232 else
9234 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9235 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9236 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9237 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9240 /* Output the marker for the end of this sequence. */
9241 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
9242 dw2_asm_output_data_uleb128 (1, NULL);
9243 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9247 /* Output the marker for the end of the line number info. */
9248 ASM_OUTPUT_LABEL (asm_out_file, l2);
9251 /* Given a pointer to a tree node for some base type, return a pointer to
9252 a DIE that describes the given type.
9254 This routine must only be called for GCC type nodes that correspond to
9255 Dwarf base (fundamental) types. */
9257 static dw_die_ref
9258 base_type_die (tree type)
9260 dw_die_ref base_type_result;
9261 enum dwarf_type encoding;
9263 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
9264 return 0;
9266 switch (TREE_CODE (type))
9268 case INTEGER_TYPE:
9269 if (TYPE_STRING_FLAG (type))
9271 if (TYPE_UNSIGNED (type))
9272 encoding = DW_ATE_unsigned_char;
9273 else
9274 encoding = DW_ATE_signed_char;
9276 else if (TYPE_UNSIGNED (type))
9277 encoding = DW_ATE_unsigned;
9278 else
9279 encoding = DW_ATE_signed;
9280 break;
9282 case REAL_TYPE:
9283 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
9284 encoding = DW_ATE_decimal_float;
9285 else
9286 encoding = DW_ATE_float;
9287 break;
9289 case FIXED_POINT_TYPE:
9290 if (TYPE_UNSIGNED (type))
9291 encoding = DW_ATE_unsigned_fixed;
9292 else
9293 encoding = DW_ATE_signed_fixed;
9294 break;
9296 /* Dwarf2 doesn't know anything about complex ints, so use
9297 a user defined type for it. */
9298 case COMPLEX_TYPE:
9299 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
9300 encoding = DW_ATE_complex_float;
9301 else
9302 encoding = DW_ATE_lo_user;
9303 break;
9305 case BOOLEAN_TYPE:
9306 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
9307 encoding = DW_ATE_boolean;
9308 break;
9310 default:
9311 /* No other TREE_CODEs are Dwarf fundamental types. */
9312 gcc_unreachable ();
9315 base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
9317 /* This probably indicates a bug. */
9318 if (! TYPE_NAME (type))
9319 add_name_attribute (base_type_result, "__unknown__");
9321 add_AT_unsigned (base_type_result, DW_AT_byte_size,
9322 int_size_in_bytes (type));
9323 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
9325 return base_type_result;
9328 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
9329 given input type is a Dwarf "fundamental" type. Otherwise return null. */
9331 static inline int
9332 is_base_type (tree type)
9334 switch (TREE_CODE (type))
9336 case ERROR_MARK:
9337 case VOID_TYPE:
9338 case INTEGER_TYPE:
9339 case REAL_TYPE:
9340 case FIXED_POINT_TYPE:
9341 case COMPLEX_TYPE:
9342 case BOOLEAN_TYPE:
9343 return 1;
9345 case ARRAY_TYPE:
9346 case RECORD_TYPE:
9347 case UNION_TYPE:
9348 case QUAL_UNION_TYPE:
9349 case ENUMERAL_TYPE:
9350 case FUNCTION_TYPE:
9351 case METHOD_TYPE:
9352 case POINTER_TYPE:
9353 case REFERENCE_TYPE:
9354 case OFFSET_TYPE:
9355 case LANG_TYPE:
9356 case VECTOR_TYPE:
9357 return 0;
9359 default:
9360 gcc_unreachable ();
9363 return 0;
9366 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
9367 node, return the size in bits for the type if it is a constant, or else
9368 return the alignment for the type if the type's size is not constant, or
9369 else return BITS_PER_WORD if the type actually turns out to be an
9370 ERROR_MARK node. */
9372 static inline unsigned HOST_WIDE_INT
9373 simple_type_size_in_bits (const_tree type)
9375 if (TREE_CODE (type) == ERROR_MARK)
9376 return BITS_PER_WORD;
9377 else if (TYPE_SIZE (type) == NULL_TREE)
9378 return 0;
9379 else if (host_integerp (TYPE_SIZE (type), 1))
9380 return tree_low_cst (TYPE_SIZE (type), 1);
9381 else
9382 return TYPE_ALIGN (type);
9385 /* Return true if the debug information for the given type should be
9386 emitted as a subrange type. */
9388 static inline bool
9389 is_subrange_type (const_tree type)
9391 tree subtype = TREE_TYPE (type);
9393 /* Subrange types are identified by the fact that they are integer
9394 types, and that they have a subtype which is either an integer type
9395 or an enumeral type. */
9397 if (TREE_CODE (type) != INTEGER_TYPE
9398 || subtype == NULL_TREE)
9399 return false;
9401 if (TREE_CODE (subtype) != INTEGER_TYPE
9402 && TREE_CODE (subtype) != ENUMERAL_TYPE
9403 && TREE_CODE (subtype) != BOOLEAN_TYPE)
9404 return false;
9406 if (TREE_CODE (type) == TREE_CODE (subtype)
9407 && int_size_in_bytes (type) == int_size_in_bytes (subtype)
9408 && TYPE_MIN_VALUE (type) != NULL
9409 && TYPE_MIN_VALUE (subtype) != NULL
9410 && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
9411 && TYPE_MAX_VALUE (type) != NULL
9412 && TYPE_MAX_VALUE (subtype) != NULL
9413 && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
9415 /* The type and its subtype have the same representation. If in
9416 addition the two types also have the same name, then the given
9417 type is not a subrange type, but rather a plain base type. */
9418 /* FIXME: brobecker/2004-03-22:
9419 Sizetype INTEGER_CSTs nodes are canonicalized. It should
9420 therefore be sufficient to check the TYPE_SIZE node pointers
9421 rather than checking the actual size. Unfortunately, we have
9422 found some cases, such as in the Ada "integer" type, where
9423 this is not the case. Until this problem is solved, we need to
9424 keep checking the actual size. */
9425 tree type_name = TYPE_NAME (type);
9426 tree subtype_name = TYPE_NAME (subtype);
9428 if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
9429 type_name = DECL_NAME (type_name);
9431 if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
9432 subtype_name = DECL_NAME (subtype_name);
9434 if (type_name == subtype_name)
9435 return false;
9438 return true;
9441 /* Given a pointer to a tree node for a subrange type, return a pointer
9442 to a DIE that describes the given type. */
9444 static dw_die_ref
9445 subrange_type_die (tree type, dw_die_ref context_die)
9447 dw_die_ref subrange_die;
9448 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
9450 if (context_die == NULL)
9451 context_die = comp_unit_die;
9453 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
9455 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
9457 /* The size of the subrange type and its base type do not match,
9458 so we need to generate a size attribute for the subrange type. */
9459 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
9462 if (TYPE_MIN_VALUE (type) != NULL)
9463 add_bound_info (subrange_die, DW_AT_lower_bound,
9464 TYPE_MIN_VALUE (type));
9465 if (TYPE_MAX_VALUE (type) != NULL)
9466 add_bound_info (subrange_die, DW_AT_upper_bound,
9467 TYPE_MAX_VALUE (type));
9469 return subrange_die;
9472 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
9473 entry that chains various modifiers in front of the given type. */
9475 static dw_die_ref
9476 modified_type_die (tree type, int is_const_type, int is_volatile_type,
9477 dw_die_ref context_die)
9479 enum tree_code code = TREE_CODE (type);
9480 dw_die_ref mod_type_die;
9481 dw_die_ref sub_die = NULL;
9482 tree item_type = NULL;
9483 tree qualified_type;
9484 tree name;
9486 if (code == ERROR_MARK)
9487 return NULL;
9489 /* See if we already have the appropriately qualified variant of
9490 this type. */
9491 qualified_type
9492 = get_qualified_type (type,
9493 ((is_const_type ? TYPE_QUAL_CONST : 0)
9494 | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
9496 /* If we do, then we can just use its DIE, if it exists. */
9497 if (qualified_type)
9499 mod_type_die = lookup_type_die (qualified_type);
9500 if (mod_type_die)
9501 return mod_type_die;
9504 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
9506 /* Handle C typedef types. */
9507 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
9509 tree dtype = TREE_TYPE (name);
9511 if (qualified_type == dtype)
9513 /* For a named type, use the typedef. */
9514 gen_type_die (qualified_type, context_die);
9515 return lookup_type_die (qualified_type);
9517 else if (is_const_type < TYPE_READONLY (dtype)
9518 || is_volatile_type < TYPE_VOLATILE (dtype)
9519 || (is_const_type <= TYPE_READONLY (dtype)
9520 && is_volatile_type <= TYPE_VOLATILE (dtype)
9521 && DECL_ORIGINAL_TYPE (name) != type))
9522 /* cv-unqualified version of named type. Just use the unnamed
9523 type to which it refers. */
9524 return modified_type_die (DECL_ORIGINAL_TYPE (name),
9525 is_const_type, is_volatile_type,
9526 context_die);
9527 /* Else cv-qualified version of named type; fall through. */
9530 if (is_const_type)
9532 mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
9533 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
9535 else if (is_volatile_type)
9537 mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
9538 sub_die = modified_type_die (type, 0, 0, context_die);
9540 else if (code == POINTER_TYPE)
9542 mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
9543 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9544 simple_type_size_in_bits (type) / BITS_PER_UNIT);
9545 item_type = TREE_TYPE (type);
9547 else if (code == REFERENCE_TYPE)
9549 mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
9550 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9551 simple_type_size_in_bits (type) / BITS_PER_UNIT);
9552 item_type = TREE_TYPE (type);
9554 else if (is_subrange_type (type))
9556 mod_type_die = subrange_type_die (type, context_die);
9557 item_type = TREE_TYPE (type);
9559 else if (is_base_type (type))
9560 mod_type_die = base_type_die (type);
9561 else
9563 gen_type_die (type, context_die);
9565 /* We have to get the type_main_variant here (and pass that to the
9566 `lookup_type_die' routine) because the ..._TYPE node we have
9567 might simply be a *copy* of some original type node (where the
9568 copy was created to help us keep track of typedef names) and
9569 that copy might have a different TYPE_UID from the original
9570 ..._TYPE node. */
9571 if (TREE_CODE (type) != VECTOR_TYPE)
9572 return lookup_type_die (type_main_variant (type));
9573 else
9574 /* Vectors have the debugging information in the type,
9575 not the main variant. */
9576 return lookup_type_die (type);
9579 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
9580 don't output a DW_TAG_typedef, since there isn't one in the
9581 user's program; just attach a DW_AT_name to the type. */
9582 if (name
9583 && (TREE_CODE (name) != TYPE_DECL
9584 || (TREE_TYPE (name) == qualified_type && DECL_NAME (name))))
9586 if (TREE_CODE (name) == TYPE_DECL)
9587 /* Could just call add_name_and_src_coords_attributes here,
9588 but since this is a builtin type it doesn't have any
9589 useful source coordinates anyway. */
9590 name = DECL_NAME (name);
9591 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
9594 if (qualified_type)
9595 equate_type_number_to_die (qualified_type, mod_type_die);
9597 if (item_type)
9598 /* We must do this after the equate_type_number_to_die call, in case
9599 this is a recursive type. This ensures that the modified_type_die
9600 recursion will terminate even if the type is recursive. Recursive
9601 types are possible in Ada. */
9602 sub_die = modified_type_die (item_type,
9603 TYPE_READONLY (item_type),
9604 TYPE_VOLATILE (item_type),
9605 context_die);
9607 if (sub_die != NULL)
9608 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
9610 return mod_type_die;
9613 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
9614 an enumerated type. */
9616 static inline int
9617 type_is_enum (const_tree type)
9619 return TREE_CODE (type) == ENUMERAL_TYPE;
9622 /* Return the DBX register number described by a given RTL node. */
9624 static unsigned int
9625 dbx_reg_number (const_rtx rtl)
9627 unsigned regno = REGNO (rtl);
9629 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
9631 #ifdef LEAF_REG_REMAP
9632 if (current_function_uses_only_leaf_regs)
9634 int leaf_reg = LEAF_REG_REMAP (regno);
9635 if (leaf_reg != -1)
9636 regno = (unsigned) leaf_reg;
9638 #endif
9640 return DBX_REGISTER_NUMBER (regno);
9643 /* Optionally add a DW_OP_piece term to a location description expression.
9644 DW_OP_piece is only added if the location description expression already
9645 doesn't end with DW_OP_piece. */
9647 static void
9648 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
9650 dw_loc_descr_ref loc;
9652 if (*list_head != NULL)
9654 /* Find the end of the chain. */
9655 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
9658 if (loc->dw_loc_opc != DW_OP_piece)
9659 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
9663 /* Return a location descriptor that designates a machine register or
9664 zero if there is none. */
9666 static dw_loc_descr_ref
9667 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
9669 rtx regs;
9671 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
9672 return 0;
9674 regs = targetm.dwarf_register_span (rtl);
9676 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
9677 return multiple_reg_loc_descriptor (rtl, regs, initialized);
9678 else
9679 return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
9682 /* Return a location descriptor that designates a machine register for
9683 a given hard register number. */
9685 static dw_loc_descr_ref
9686 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
9688 dw_loc_descr_ref reg_loc_descr = new_reg_loc_descr (regno, 0);
9690 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9691 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9693 return reg_loc_descr;
9696 /* Given an RTL of a register, return a location descriptor that
9697 designates a value that spans more than one register. */
9699 static dw_loc_descr_ref
9700 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
9701 enum var_init_status initialized)
9703 int nregs, size, i;
9704 unsigned reg;
9705 dw_loc_descr_ref loc_result = NULL;
9707 reg = REGNO (rtl);
9708 #ifdef LEAF_REG_REMAP
9709 if (current_function_uses_only_leaf_regs)
9711 int leaf_reg = LEAF_REG_REMAP (reg);
9712 if (leaf_reg != -1)
9713 reg = (unsigned) leaf_reg;
9715 #endif
9716 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
9717 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
9719 /* Simple, contiguous registers. */
9720 if (regs == NULL_RTX)
9722 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
9724 loc_result = NULL;
9725 while (nregs--)
9727 dw_loc_descr_ref t;
9729 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
9730 VAR_INIT_STATUS_INITIALIZED);
9731 add_loc_descr (&loc_result, t);
9732 add_loc_descr_op_piece (&loc_result, size);
9733 ++reg;
9735 return loc_result;
9738 /* Now onto stupid register sets in non contiguous locations. */
9740 gcc_assert (GET_CODE (regs) == PARALLEL);
9742 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
9743 loc_result = NULL;
9745 for (i = 0; i < XVECLEN (regs, 0); ++i)
9747 dw_loc_descr_ref t;
9749 t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
9750 VAR_INIT_STATUS_INITIALIZED);
9751 add_loc_descr (&loc_result, t);
9752 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
9753 add_loc_descr_op_piece (&loc_result, size);
9756 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9757 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9758 return loc_result;
9761 #endif /* DWARF2_DEBUGGING_INFO */
9763 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
9765 /* Return a location descriptor that designates a constant. */
9767 static dw_loc_descr_ref
9768 int_loc_descriptor (HOST_WIDE_INT i)
9770 enum dwarf_location_atom op;
9772 /* Pick the smallest representation of a constant, rather than just
9773 defaulting to the LEB encoding. */
9774 if (i >= 0)
9776 if (i <= 31)
9777 op = DW_OP_lit0 + i;
9778 else if (i <= 0xff)
9779 op = DW_OP_const1u;
9780 else if (i <= 0xffff)
9781 op = DW_OP_const2u;
9782 else if (HOST_BITS_PER_WIDE_INT == 32
9783 || i <= 0xffffffff)
9784 op = DW_OP_const4u;
9785 else
9786 op = DW_OP_constu;
9788 else
9790 if (i >= -0x80)
9791 op = DW_OP_const1s;
9792 else if (i >= -0x8000)
9793 op = DW_OP_const2s;
9794 else if (HOST_BITS_PER_WIDE_INT == 32
9795 || i >= -0x80000000)
9796 op = DW_OP_const4s;
9797 else
9798 op = DW_OP_consts;
9801 return new_loc_descr (op, i, 0);
9803 #endif
9805 #ifdef DWARF2_DEBUGGING_INFO
9807 /* Return a location descriptor that designates a base+offset location. */
9809 static dw_loc_descr_ref
9810 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
9811 enum var_init_status initialized)
9813 unsigned int regno;
9814 dw_loc_descr_ref result;
9815 dw_fde_ref fde = current_fde ();
9817 /* We only use "frame base" when we're sure we're talking about the
9818 post-prologue local stack frame. We do this by *not* running
9819 register elimination until this point, and recognizing the special
9820 argument pointer and soft frame pointer rtx's. */
9821 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
9823 rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
9825 if (elim != reg)
9827 if (GET_CODE (elim) == PLUS)
9829 offset += INTVAL (XEXP (elim, 1));
9830 elim = XEXP (elim, 0);
9832 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
9833 && (elim == hard_frame_pointer_rtx
9834 || elim == stack_pointer_rtx))
9835 || elim == (frame_pointer_needed
9836 ? hard_frame_pointer_rtx
9837 : stack_pointer_rtx));
9839 /* If drap register is used to align stack, use frame
9840 pointer + offset to access stack variables. If stack
9841 is aligned without drap, use stack pointer + offset to
9842 access stack variables. */
9843 if (crtl->stack_realign_tried
9844 && cfa.reg == HARD_FRAME_POINTER_REGNUM
9845 && reg == frame_pointer_rtx)
9847 int base_reg
9848 = DWARF_FRAME_REGNUM (cfa.indirect
9849 ? HARD_FRAME_POINTER_REGNUM
9850 : STACK_POINTER_REGNUM);
9851 return new_reg_loc_descr (base_reg, offset);
9854 offset += frame_pointer_fb_offset;
9855 return new_loc_descr (DW_OP_fbreg, offset, 0);
9858 else if (fde
9859 && fde->drap_reg != INVALID_REGNUM
9860 && (fde->drap_reg == REGNO (reg)
9861 || fde->vdrap_reg == REGNO (reg)))
9863 /* Use cfa+offset to represent the location of arguments passed
9864 on stack when drap is used to align stack. */
9865 return new_loc_descr (DW_OP_fbreg, offset, 0);
9868 regno = dbx_reg_number (reg);
9869 if (regno <= 31)
9870 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
9871 offset, 0);
9872 else
9873 result = new_loc_descr (DW_OP_bregx, regno, offset);
9875 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9876 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9878 return result;
9881 /* Return true if this RTL expression describes a base+offset calculation. */
9883 static inline int
9884 is_based_loc (const_rtx rtl)
9886 return (GET_CODE (rtl) == PLUS
9887 && ((REG_P (XEXP (rtl, 0))
9888 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
9889 && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
9892 /* Return a descriptor that describes the concatenation of N locations
9893 used to form the address of a memory location. */
9895 static dw_loc_descr_ref
9896 concatn_mem_loc_descriptor (rtx concatn, enum machine_mode mode,
9897 enum var_init_status initialized)
9899 unsigned int i;
9900 dw_loc_descr_ref cc_loc_result = NULL;
9901 unsigned int n = XVECLEN (concatn, 0);
9903 for (i = 0; i < n; ++i)
9905 dw_loc_descr_ref ref;
9906 rtx x = XVECEXP (concatn, 0, i);
9908 ref = mem_loc_descriptor (x, mode, VAR_INIT_STATUS_INITIALIZED);
9909 if (ref == NULL)
9910 return NULL;
9912 add_loc_descr (&cc_loc_result, ref);
9913 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
9916 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9917 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9919 return cc_loc_result;
9922 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
9923 failed. */
9925 static dw_loc_descr_ref
9926 tls_mem_loc_descriptor (rtx mem)
9928 tree base;
9929 dw_loc_descr_ref loc_result, loc_result2;
9931 if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
9932 return NULL;
9934 base = get_base_address (MEM_EXPR (mem));
9935 if (base == NULL
9936 || TREE_CODE (base) != VAR_DECL
9937 || !DECL_THREAD_LOCAL_P (base))
9938 return NULL;
9940 loc_result = loc_descriptor_from_tree_1 (MEM_EXPR (mem), 2);
9941 if (loc_result == NULL)
9942 return NULL;
9944 if (INTVAL (MEM_OFFSET (mem)))
9946 if (INTVAL (MEM_OFFSET (mem)) >= 0)
9947 add_loc_descr (&loc_result,
9948 new_loc_descr (DW_OP_plus_uconst,
9949 INTVAL (MEM_OFFSET (mem)), 0));
9950 else
9952 loc_result2 = mem_loc_descriptor (MEM_OFFSET (mem), GET_MODE (mem),
9953 VAR_INIT_STATUS_INITIALIZED);
9954 if (loc_result2 == 0)
9955 return NULL;
9956 add_loc_descr (&loc_result, loc_result2);
9957 add_loc_descr (&loc_result, new_loc_descr (DW_OP_plus, 0, 0));
9961 return loc_result;
9964 /* The following routine converts the RTL for a variable or parameter
9965 (resident in memory) into an equivalent Dwarf representation of a
9966 mechanism for getting the address of that same variable onto the top of a
9967 hypothetical "address evaluation" stack.
9969 When creating memory location descriptors, we are effectively transforming
9970 the RTL for a memory-resident object into its Dwarf postfix expression
9971 equivalent. This routine recursively descends an RTL tree, turning
9972 it into Dwarf postfix code as it goes.
9974 MODE is the mode of the memory reference, needed to handle some
9975 autoincrement addressing modes.
9977 CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
9978 location list for RTL.
9980 Return 0 if we can't represent the location. */
9982 static dw_loc_descr_ref
9983 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
9984 enum var_init_status initialized)
9986 dw_loc_descr_ref mem_loc_result = NULL;
9987 enum dwarf_location_atom op;
9989 /* Note that for a dynamically sized array, the location we will generate a
9990 description of here will be the lowest numbered location which is
9991 actually within the array. That's *not* necessarily the same as the
9992 zeroth element of the array. */
9994 rtl = targetm.delegitimize_address (rtl);
9996 switch (GET_CODE (rtl))
9998 case POST_INC:
9999 case POST_DEC:
10000 case POST_MODIFY:
10001 /* POST_INC and POST_DEC can be handled just like a SUBREG. So we
10002 just fall into the SUBREG code. */
10004 /* ... fall through ... */
10006 case SUBREG:
10007 /* The case of a subreg may arise when we have a local (register)
10008 variable or a formal (register) parameter which doesn't quite fill
10009 up an entire register. For now, just assume that it is
10010 legitimate to make the Dwarf info refer to the whole register which
10011 contains the given subreg. */
10012 rtl = XEXP (rtl, 0);
10014 /* ... fall through ... */
10016 case REG:
10017 /* Whenever a register number forms a part of the description of the
10018 method for calculating the (dynamic) address of a memory resident
10019 object, DWARF rules require the register number be referred to as
10020 a "base register". This distinction is not based in any way upon
10021 what category of register the hardware believes the given register
10022 belongs to. This is strictly DWARF terminology we're dealing with
10023 here. Note that in cases where the location of a memory-resident
10024 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
10025 OP_CONST (0)) the actual DWARF location descriptor that we generate
10026 may just be OP_BASEREG (basereg). This may look deceptively like
10027 the object in question was allocated to a register (rather than in
10028 memory) so DWARF consumers need to be aware of the subtle
10029 distinction between OP_REG and OP_BASEREG. */
10030 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
10031 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
10032 else if (stack_realign_drap
10033 && crtl->drap_reg
10034 && crtl->args.internal_arg_pointer == rtl
10035 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
10037 /* If RTL is internal_arg_pointer, which has been optimized
10038 out, use DRAP instead. */
10039 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
10040 VAR_INIT_STATUS_INITIALIZED);
10042 break;
10044 case MEM:
10045 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
10046 VAR_INIT_STATUS_INITIALIZED);
10047 if (mem_loc_result == NULL)
10048 mem_loc_result = tls_mem_loc_descriptor (rtl);
10049 if (mem_loc_result != 0)
10050 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
10051 break;
10053 case LO_SUM:
10054 rtl = XEXP (rtl, 1);
10056 /* ... fall through ... */
10058 case LABEL_REF:
10059 /* Some ports can transform a symbol ref into a label ref, because
10060 the symbol ref is too far away and has to be dumped into a constant
10061 pool. */
10062 case CONST:
10063 case SYMBOL_REF:
10064 /* Alternatively, the symbol in the constant pool might be referenced
10065 by a different symbol. */
10066 if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
10068 bool marked;
10069 rtx tmp = get_pool_constant_mark (rtl, &marked);
10071 if (GET_CODE (tmp) == SYMBOL_REF)
10073 rtl = tmp;
10074 if (CONSTANT_POOL_ADDRESS_P (tmp))
10075 get_pool_constant_mark (tmp, &marked);
10076 else
10077 marked = true;
10080 /* If all references to this pool constant were optimized away,
10081 it was not output and thus we can't represent it.
10082 FIXME: might try to use DW_OP_const_value here, though
10083 DW_OP_piece complicates it. */
10084 if (!marked)
10085 return 0;
10088 mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
10089 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
10090 mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
10091 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
10092 break;
10094 case PRE_MODIFY:
10095 /* Extract the PLUS expression nested inside and fall into
10096 PLUS code below. */
10097 rtl = XEXP (rtl, 1);
10098 goto plus;
10100 case PRE_INC:
10101 case PRE_DEC:
10102 /* Turn these into a PLUS expression and fall into the PLUS code
10103 below. */
10104 rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
10105 GEN_INT (GET_CODE (rtl) == PRE_INC
10106 ? GET_MODE_UNIT_SIZE (mode)
10107 : -GET_MODE_UNIT_SIZE (mode)));
10109 /* ... fall through ... */
10111 case PLUS:
10112 plus:
10113 if (is_based_loc (rtl))
10114 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
10115 INTVAL (XEXP (rtl, 1)),
10116 VAR_INIT_STATUS_INITIALIZED);
10117 else
10119 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
10120 VAR_INIT_STATUS_INITIALIZED);
10121 if (mem_loc_result == 0)
10122 break;
10124 if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
10125 && INTVAL (XEXP (rtl, 1)) >= 0)
10126 add_loc_descr (&mem_loc_result,
10127 new_loc_descr (DW_OP_plus_uconst,
10128 INTVAL (XEXP (rtl, 1)), 0));
10129 else
10131 dw_loc_descr_ref mem_loc_result2
10132 = mem_loc_descriptor (XEXP (rtl, 1), mode,
10133 VAR_INIT_STATUS_INITIALIZED);
10134 if (mem_loc_result2 == 0)
10135 break;
10136 add_loc_descr (&mem_loc_result, mem_loc_result2);
10137 add_loc_descr (&mem_loc_result,
10138 new_loc_descr (DW_OP_plus, 0, 0));
10141 break;
10143 /* If a pseudo-reg is optimized away, it is possible for it to
10144 be replaced with a MEM containing a multiply or shift. */
10145 case MULT:
10146 op = DW_OP_mul;
10147 goto do_binop;
10149 case ASHIFT:
10150 op = DW_OP_shl;
10151 goto do_binop;
10153 case ASHIFTRT:
10154 op = DW_OP_shra;
10155 goto do_binop;
10157 case LSHIFTRT:
10158 op = DW_OP_shr;
10159 goto do_binop;
10161 do_binop:
10163 dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
10164 VAR_INIT_STATUS_INITIALIZED);
10165 dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
10166 VAR_INIT_STATUS_INITIALIZED);
10168 if (op0 == 0 || op1 == 0)
10169 break;
10171 mem_loc_result = op0;
10172 add_loc_descr (&mem_loc_result, op1);
10173 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
10174 break;
10177 case CONST_INT:
10178 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
10179 break;
10181 case CONCATN:
10182 mem_loc_result = concatn_mem_loc_descriptor (rtl, mode,
10183 VAR_INIT_STATUS_INITIALIZED);
10184 break;
10186 case UNSPEC:
10187 /* If delegitimize_address couldn't do anything with the UNSPEC, we
10188 can't express it in the debug info. This can happen e.g. with some
10189 TLS UNSPECs. */
10190 break;
10192 default:
10193 gcc_unreachable ();
10196 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10197 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10199 return mem_loc_result;
10202 /* Return a descriptor that describes the concatenation of two locations.
10203 This is typically a complex variable. */
10205 static dw_loc_descr_ref
10206 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
10208 dw_loc_descr_ref cc_loc_result = NULL;
10209 dw_loc_descr_ref x0_ref = loc_descriptor (x0, VAR_INIT_STATUS_INITIALIZED);
10210 dw_loc_descr_ref x1_ref = loc_descriptor (x1, VAR_INIT_STATUS_INITIALIZED);
10212 if (x0_ref == 0 || x1_ref == 0)
10213 return 0;
10215 cc_loc_result = x0_ref;
10216 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
10218 add_loc_descr (&cc_loc_result, x1_ref);
10219 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
10221 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10222 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10224 return cc_loc_result;
10227 /* Return a descriptor that describes the concatenation of N
10228 locations. */
10230 static dw_loc_descr_ref
10231 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
10233 unsigned int i;
10234 dw_loc_descr_ref cc_loc_result = NULL;
10235 unsigned int n = XVECLEN (concatn, 0);
10237 for (i = 0; i < n; ++i)
10239 dw_loc_descr_ref ref;
10240 rtx x = XVECEXP (concatn, 0, i);
10242 ref = loc_descriptor (x, VAR_INIT_STATUS_INITIALIZED);
10243 if (ref == NULL)
10244 return NULL;
10246 add_loc_descr (&cc_loc_result, ref);
10247 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
10250 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10251 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10253 return cc_loc_result;
10256 /* Output a proper Dwarf location descriptor for a variable or parameter
10257 which is either allocated in a register or in a memory location. For a
10258 register, we just generate an OP_REG and the register number. For a
10259 memory location we provide a Dwarf postfix expression describing how to
10260 generate the (dynamic) address of the object onto the address stack.
10262 If we don't know how to describe it, return 0. */
10264 static dw_loc_descr_ref
10265 loc_descriptor (rtx rtl, enum var_init_status initialized)
10267 dw_loc_descr_ref loc_result = NULL;
10269 switch (GET_CODE (rtl))
10271 case SUBREG:
10272 /* The case of a subreg may arise when we have a local (register)
10273 variable or a formal (register) parameter which doesn't quite fill
10274 up an entire register. For now, just assume that it is
10275 legitimate to make the Dwarf info refer to the whole register which
10276 contains the given subreg. */
10277 rtl = SUBREG_REG (rtl);
10279 /* ... fall through ... */
10281 case REG:
10282 loc_result = reg_loc_descriptor (rtl, initialized);
10283 break;
10285 case MEM:
10286 loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
10287 initialized);
10288 if (loc_result == NULL)
10289 loc_result = tls_mem_loc_descriptor (rtl);
10290 break;
10292 case CONCAT:
10293 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
10294 initialized);
10295 break;
10297 case CONCATN:
10298 loc_result = concatn_loc_descriptor (rtl, initialized);
10299 break;
10301 case VAR_LOCATION:
10302 /* Single part. */
10303 if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
10305 loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), initialized);
10306 break;
10309 rtl = XEXP (rtl, 1);
10310 /* FALLTHRU */
10312 case PARALLEL:
10314 rtvec par_elems = XVEC (rtl, 0);
10315 int num_elem = GET_NUM_ELEM (par_elems);
10316 enum machine_mode mode;
10317 int i;
10319 /* Create the first one, so we have something to add to. */
10320 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
10321 initialized);
10322 if (loc_result == NULL)
10323 return NULL;
10324 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
10325 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
10326 for (i = 1; i < num_elem; i++)
10328 dw_loc_descr_ref temp;
10330 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
10331 initialized);
10332 if (temp == NULL)
10333 return NULL;
10334 add_loc_descr (&loc_result, temp);
10335 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
10336 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
10339 break;
10341 default:
10342 gcc_unreachable ();
10345 return loc_result;
10348 /* Similar, but generate the descriptor from trees instead of rtl. This comes
10349 up particularly with variable length arrays. WANT_ADDRESS is 2 if this is
10350 a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
10351 top-level invocation, and we require the address of LOC; is 0 if we require
10352 the value of LOC. */
10354 static dw_loc_descr_ref
10355 loc_descriptor_from_tree_1 (tree loc, int want_address)
10357 dw_loc_descr_ref ret, ret1;
10358 int have_address = 0;
10359 enum dwarf_location_atom op;
10361 /* ??? Most of the time we do not take proper care for sign/zero
10362 extending the values properly. Hopefully this won't be a real
10363 problem... */
10365 switch (TREE_CODE (loc))
10367 case ERROR_MARK:
10368 return 0;
10370 case PLACEHOLDER_EXPR:
10371 /* This case involves extracting fields from an object to determine the
10372 position of other fields. We don't try to encode this here. The
10373 only user of this is Ada, which encodes the needed information using
10374 the names of types. */
10375 return 0;
10377 case CALL_EXPR:
10378 return 0;
10380 case PREINCREMENT_EXPR:
10381 case PREDECREMENT_EXPR:
10382 case POSTINCREMENT_EXPR:
10383 case POSTDECREMENT_EXPR:
10384 /* There are no opcodes for these operations. */
10385 return 0;
10387 case ADDR_EXPR:
10388 /* If we already want an address, there's nothing we can do. */
10389 if (want_address)
10390 return 0;
10392 /* Otherwise, process the argument and look for the address. */
10393 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
10395 case VAR_DECL:
10396 if (DECL_THREAD_LOCAL_P (loc))
10398 rtx rtl;
10399 enum dwarf_location_atom first_op;
10400 enum dwarf_location_atom second_op;
10402 if (targetm.have_tls)
10404 /* If this is not defined, we have no way to emit the
10405 data. */
10406 if (!targetm.asm_out.output_dwarf_dtprel)
10407 return 0;
10409 /* The way DW_OP_GNU_push_tls_address is specified, we
10410 can only look up addresses of objects in the current
10411 module. */
10412 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
10413 return 0;
10414 first_op = (enum dwarf_location_atom) INTERNAL_DW_OP_tls_addr;
10415 second_op = DW_OP_GNU_push_tls_address;
10417 else
10419 if (!targetm.emutls.debug_form_tls_address)
10420 return 0;
10421 loc = emutls_decl (loc);
10422 first_op = DW_OP_addr;
10423 second_op = DW_OP_form_tls_address;
10426 rtl = rtl_for_decl_location (loc);
10427 if (rtl == NULL_RTX)
10428 return 0;
10430 if (!MEM_P (rtl))
10431 return 0;
10432 rtl = XEXP (rtl, 0);
10433 if (! CONSTANT_P (rtl))
10434 return 0;
10436 ret = new_loc_descr (first_op, 0, 0);
10437 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
10438 ret->dw_loc_oprnd1.v.val_addr = rtl;
10440 ret1 = new_loc_descr (second_op, 0, 0);
10441 add_loc_descr (&ret, ret1);
10443 have_address = 1;
10444 break;
10446 /* FALLTHRU */
10448 case PARM_DECL:
10449 if (DECL_HAS_VALUE_EXPR_P (loc))
10450 return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
10451 want_address);
10452 /* FALLTHRU */
10454 case RESULT_DECL:
10455 case FUNCTION_DECL:
10457 rtx rtl = rtl_for_decl_location (loc);
10459 if (rtl == NULL_RTX)
10460 return 0;
10461 else if (GET_CODE (rtl) == CONST_INT)
10463 HOST_WIDE_INT val = INTVAL (rtl);
10464 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
10465 val &= GET_MODE_MASK (DECL_MODE (loc));
10466 ret = int_loc_descriptor (val);
10468 else if (GET_CODE (rtl) == CONST_STRING)
10469 return 0;
10470 else if (CONSTANT_P (rtl))
10472 ret = new_loc_descr (DW_OP_addr, 0, 0);
10473 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
10474 ret->dw_loc_oprnd1.v.val_addr = rtl;
10476 else
10478 enum machine_mode mode;
10480 /* Certain constructs can only be represented at top-level. */
10481 if (want_address == 2)
10482 return loc_descriptor (rtl, VAR_INIT_STATUS_INITIALIZED);
10484 mode = GET_MODE (rtl);
10485 if (MEM_P (rtl))
10487 rtl = XEXP (rtl, 0);
10488 have_address = 1;
10490 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
10493 break;
10495 case INDIRECT_REF:
10496 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10497 have_address = 1;
10498 break;
10500 case COMPOUND_EXPR:
10501 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
10503 CASE_CONVERT:
10504 case VIEW_CONVERT_EXPR:
10505 case SAVE_EXPR:
10506 case MODIFY_EXPR:
10507 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), want_address);
10509 case COMPONENT_REF:
10510 case BIT_FIELD_REF:
10511 case ARRAY_REF:
10512 case ARRAY_RANGE_REF:
10514 tree obj, offset;
10515 HOST_WIDE_INT bitsize, bitpos, bytepos;
10516 enum machine_mode mode;
10517 int volatilep;
10518 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
10520 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
10521 &unsignedp, &volatilep, false);
10523 if (obj == loc)
10524 return 0;
10526 ret = loc_descriptor_from_tree_1 (obj, 1);
10527 if (ret == 0
10528 || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
10529 return 0;
10531 if (offset != NULL_TREE)
10533 /* Variable offset. */
10534 ret1 = loc_descriptor_from_tree_1 (offset, 0);
10535 if (ret1 == 0)
10536 return 0;
10537 add_loc_descr (&ret, ret1);
10538 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
10541 bytepos = bitpos / BITS_PER_UNIT;
10542 if (bytepos > 0)
10543 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
10544 else if (bytepos < 0)
10546 add_loc_descr (&ret, int_loc_descriptor (bytepos));
10547 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
10550 have_address = 1;
10551 break;
10554 case INTEGER_CST:
10555 if (host_integerp (loc, 0))
10556 ret = int_loc_descriptor (tree_low_cst (loc, 0));
10557 else
10558 return 0;
10559 break;
10561 case CONSTRUCTOR:
10563 /* Get an RTL for this, if something has been emitted. */
10564 rtx rtl = lookup_constant_def (loc);
10565 enum machine_mode mode;
10567 if (!rtl || !MEM_P (rtl))
10568 return 0;
10569 mode = GET_MODE (rtl);
10570 rtl = XEXP (rtl, 0);
10571 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
10572 have_address = 1;
10573 break;
10576 case TRUTH_AND_EXPR:
10577 case TRUTH_ANDIF_EXPR:
10578 case BIT_AND_EXPR:
10579 op = DW_OP_and;
10580 goto do_binop;
10582 case TRUTH_XOR_EXPR:
10583 case BIT_XOR_EXPR:
10584 op = DW_OP_xor;
10585 goto do_binop;
10587 case TRUTH_OR_EXPR:
10588 case TRUTH_ORIF_EXPR:
10589 case BIT_IOR_EXPR:
10590 op = DW_OP_or;
10591 goto do_binop;
10593 case FLOOR_DIV_EXPR:
10594 case CEIL_DIV_EXPR:
10595 case ROUND_DIV_EXPR:
10596 case TRUNC_DIV_EXPR:
10597 op = DW_OP_div;
10598 goto do_binop;
10600 case MINUS_EXPR:
10601 op = DW_OP_minus;
10602 goto do_binop;
10604 case FLOOR_MOD_EXPR:
10605 case CEIL_MOD_EXPR:
10606 case ROUND_MOD_EXPR:
10607 case TRUNC_MOD_EXPR:
10608 op = DW_OP_mod;
10609 goto do_binop;
10611 case MULT_EXPR:
10612 op = DW_OP_mul;
10613 goto do_binop;
10615 case LSHIFT_EXPR:
10616 op = DW_OP_shl;
10617 goto do_binop;
10619 case RSHIFT_EXPR:
10620 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
10621 goto do_binop;
10623 case POINTER_PLUS_EXPR:
10624 case PLUS_EXPR:
10625 if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
10626 && host_integerp (TREE_OPERAND (loc, 1), 0))
10628 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10629 if (ret == 0)
10630 return 0;
10632 add_loc_descr (&ret,
10633 new_loc_descr (DW_OP_plus_uconst,
10634 tree_low_cst (TREE_OPERAND (loc, 1),
10636 0));
10637 break;
10640 op = DW_OP_plus;
10641 goto do_binop;
10643 case LE_EXPR:
10644 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10645 return 0;
10647 op = DW_OP_le;
10648 goto do_binop;
10650 case GE_EXPR:
10651 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10652 return 0;
10654 op = DW_OP_ge;
10655 goto do_binop;
10657 case LT_EXPR:
10658 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10659 return 0;
10661 op = DW_OP_lt;
10662 goto do_binop;
10664 case GT_EXPR:
10665 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10666 return 0;
10668 op = DW_OP_gt;
10669 goto do_binop;
10671 case EQ_EXPR:
10672 op = DW_OP_eq;
10673 goto do_binop;
10675 case NE_EXPR:
10676 op = DW_OP_ne;
10677 goto do_binop;
10679 do_binop:
10680 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10681 ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
10682 if (ret == 0 || ret1 == 0)
10683 return 0;
10685 add_loc_descr (&ret, ret1);
10686 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10687 break;
10689 case TRUTH_NOT_EXPR:
10690 case BIT_NOT_EXPR:
10691 op = DW_OP_not;
10692 goto do_unop;
10694 case ABS_EXPR:
10695 op = DW_OP_abs;
10696 goto do_unop;
10698 case NEGATE_EXPR:
10699 op = DW_OP_neg;
10700 goto do_unop;
10702 do_unop:
10703 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10704 if (ret == 0)
10705 return 0;
10707 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10708 break;
10710 case MIN_EXPR:
10711 case MAX_EXPR:
10713 const enum tree_code code =
10714 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
10716 loc = build3 (COND_EXPR, TREE_TYPE (loc),
10717 build2 (code, integer_type_node,
10718 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
10719 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
10722 /* ... fall through ... */
10724 case COND_EXPR:
10726 dw_loc_descr_ref lhs
10727 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
10728 dw_loc_descr_ref rhs
10729 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
10730 dw_loc_descr_ref bra_node, jump_node, tmp;
10732 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10733 if (ret == 0 || lhs == 0 || rhs == 0)
10734 return 0;
10736 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
10737 add_loc_descr (&ret, bra_node);
10739 add_loc_descr (&ret, rhs);
10740 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
10741 add_loc_descr (&ret, jump_node);
10743 add_loc_descr (&ret, lhs);
10744 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
10745 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
10747 /* ??? Need a node to point the skip at. Use a nop. */
10748 tmp = new_loc_descr (DW_OP_nop, 0, 0);
10749 add_loc_descr (&ret, tmp);
10750 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
10751 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
10753 break;
10755 case FIX_TRUNC_EXPR:
10756 return 0;
10758 default:
10759 /* Leave front-end specific codes as simply unknown. This comes
10760 up, for instance, with the C STMT_EXPR. */
10761 if ((unsigned int) TREE_CODE (loc)
10762 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
10763 return 0;
10765 #ifdef ENABLE_CHECKING
10766 /* Otherwise this is a generic code; we should just lists all of
10767 these explicitly. We forgot one. */
10768 gcc_unreachable ();
10769 #else
10770 /* In a release build, we want to degrade gracefully: better to
10771 generate incomplete debugging information than to crash. */
10772 return NULL;
10773 #endif
10776 /* Show if we can't fill the request for an address. */
10777 if (want_address && !have_address)
10778 return 0;
10780 /* If we've got an address and don't want one, dereference. */
10781 if (!want_address && have_address && ret)
10783 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
10785 if (size > DWARF2_ADDR_SIZE || size == -1)
10786 return 0;
10787 else if (size == DWARF2_ADDR_SIZE)
10788 op = DW_OP_deref;
10789 else
10790 op = DW_OP_deref_size;
10792 add_loc_descr (&ret, new_loc_descr (op, size, 0));
10795 return ret;
10798 static inline dw_loc_descr_ref
10799 loc_descriptor_from_tree (tree loc)
10801 return loc_descriptor_from_tree_1 (loc, 2);
10804 /* Given a value, round it up to the lowest multiple of `boundary'
10805 which is not less than the value itself. */
10807 static inline HOST_WIDE_INT
10808 ceiling (HOST_WIDE_INT value, unsigned int boundary)
10810 return (((value + boundary - 1) / boundary) * boundary);
10813 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
10814 pointer to the declared type for the relevant field variable, or return
10815 `integer_type_node' if the given node turns out to be an
10816 ERROR_MARK node. */
10818 static inline tree
10819 field_type (const_tree decl)
10821 tree type;
10823 if (TREE_CODE (decl) == ERROR_MARK)
10824 return integer_type_node;
10826 type = DECL_BIT_FIELD_TYPE (decl);
10827 if (type == NULL_TREE)
10828 type = TREE_TYPE (decl);
10830 return type;
10833 /* Given a pointer to a tree node, return the alignment in bits for
10834 it, or else return BITS_PER_WORD if the node actually turns out to
10835 be an ERROR_MARK node. */
10837 static inline unsigned
10838 simple_type_align_in_bits (const_tree type)
10840 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
10843 static inline unsigned
10844 simple_decl_align_in_bits (const_tree decl)
10846 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
10849 /* Return the result of rounding T up to ALIGN. */
10851 static inline HOST_WIDE_INT
10852 round_up_to_align (HOST_WIDE_INT t, unsigned int align)
10854 /* We must be careful if T is negative because HOST_WIDE_INT can be
10855 either "above" or "below" unsigned int as per the C promotion
10856 rules, depending on the host, thus making the signedness of the
10857 direct multiplication and division unpredictable. */
10858 unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
10860 u += align - 1;
10861 u /= align;
10862 u *= align;
10864 return (HOST_WIDE_INT) u;
10867 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
10868 lowest addressed byte of the "containing object" for the given FIELD_DECL,
10869 or return 0 if we are unable to determine what that offset is, either
10870 because the argument turns out to be a pointer to an ERROR_MARK node, or
10871 because the offset is actually variable. (We can't handle the latter case
10872 just yet). */
10874 static HOST_WIDE_INT
10875 field_byte_offset (const_tree decl)
10877 HOST_WIDE_INT object_offset_in_bits;
10878 HOST_WIDE_INT bitpos_int;
10880 if (TREE_CODE (decl) == ERROR_MARK)
10881 return 0;
10883 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
10885 /* We cannot yet cope with fields whose positions are variable, so
10886 for now, when we see such things, we simply return 0. Someday, we may
10887 be able to handle such cases, but it will be damn difficult. */
10888 if (! host_integerp (bit_position (decl), 0))
10889 return 0;
10891 bitpos_int = int_bit_position (decl);
10893 #ifdef PCC_BITFIELD_TYPE_MATTERS
10894 if (PCC_BITFIELD_TYPE_MATTERS)
10896 tree type;
10897 tree field_size_tree;
10898 HOST_WIDE_INT deepest_bitpos;
10899 unsigned HOST_WIDE_INT field_size_in_bits;
10900 unsigned int type_align_in_bits;
10901 unsigned int decl_align_in_bits;
10902 unsigned HOST_WIDE_INT type_size_in_bits;
10904 type = field_type (decl);
10905 type_size_in_bits = simple_type_size_in_bits (type);
10906 type_align_in_bits = simple_type_align_in_bits (type);
10908 field_size_tree = DECL_SIZE (decl);
10910 /* The size could be unspecified if there was an error, or for
10911 a flexible array member. */
10912 if (!field_size_tree)
10913 field_size_tree = bitsize_zero_node;
10915 /* If the size of the field is not constant, use the type size. */
10916 if (host_integerp (field_size_tree, 1))
10917 field_size_in_bits = tree_low_cst (field_size_tree, 1);
10918 else
10919 field_size_in_bits = type_size_in_bits;
10921 decl_align_in_bits = simple_decl_align_in_bits (decl);
10923 /* The GCC front-end doesn't make any attempt to keep track of the
10924 starting bit offset (relative to the start of the containing
10925 structure type) of the hypothetical "containing object" for a
10926 bit-field. Thus, when computing the byte offset value for the
10927 start of the "containing object" of a bit-field, we must deduce
10928 this information on our own. This can be rather tricky to do in
10929 some cases. For example, handling the following structure type
10930 definition when compiling for an i386/i486 target (which only
10931 aligns long long's to 32-bit boundaries) can be very tricky:
10933 struct S { int field1; long long field2:31; };
10935 Fortunately, there is a simple rule-of-thumb which can be used
10936 in such cases. When compiling for an i386/i486, GCC will
10937 allocate 8 bytes for the structure shown above. It decides to
10938 do this based upon one simple rule for bit-field allocation.
10939 GCC allocates each "containing object" for each bit-field at
10940 the first (i.e. lowest addressed) legitimate alignment boundary
10941 (based upon the required minimum alignment for the declared
10942 type of the field) which it can possibly use, subject to the
10943 condition that there is still enough available space remaining
10944 in the containing object (when allocated at the selected point)
10945 to fully accommodate all of the bits of the bit-field itself.
10947 This simple rule makes it obvious why GCC allocates 8 bytes for
10948 each object of the structure type shown above. When looking
10949 for a place to allocate the "containing object" for `field2',
10950 the compiler simply tries to allocate a 64-bit "containing
10951 object" at each successive 32-bit boundary (starting at zero)
10952 until it finds a place to allocate that 64- bit field such that
10953 at least 31 contiguous (and previously unallocated) bits remain
10954 within that selected 64 bit field. (As it turns out, for the
10955 example above, the compiler finds it is OK to allocate the
10956 "containing object" 64-bit field at bit-offset zero within the
10957 structure type.)
10959 Here we attempt to work backwards from the limited set of facts
10960 we're given, and we try to deduce from those facts, where GCC
10961 must have believed that the containing object started (within
10962 the structure type). The value we deduce is then used (by the
10963 callers of this routine) to generate DW_AT_location and
10964 DW_AT_bit_offset attributes for fields (both bit-fields and, in
10965 the case of DW_AT_location, regular fields as well). */
10967 /* Figure out the bit-distance from the start of the structure to
10968 the "deepest" bit of the bit-field. */
10969 deepest_bitpos = bitpos_int + field_size_in_bits;
10971 /* This is the tricky part. Use some fancy footwork to deduce
10972 where the lowest addressed bit of the containing object must
10973 be. */
10974 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
10976 /* Round up to type_align by default. This works best for
10977 bitfields. */
10978 object_offset_in_bits
10979 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
10981 if (object_offset_in_bits > bitpos_int)
10983 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
10985 /* Round up to decl_align instead. */
10986 object_offset_in_bits
10987 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
10990 else
10991 #endif
10992 object_offset_in_bits = bitpos_int;
10994 return object_offset_in_bits / BITS_PER_UNIT;
10997 /* The following routines define various Dwarf attributes and any data
10998 associated with them. */
11000 /* Add a location description attribute value to a DIE.
11002 This emits location attributes suitable for whole variables and
11003 whole parameters. Note that the location attributes for struct fields are
11004 generated by the routine `data_member_location_attribute' below. */
11006 static inline void
11007 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
11008 dw_loc_descr_ref descr)
11010 if (descr != 0)
11011 add_AT_loc (die, attr_kind, descr);
11014 /* Attach the specialized form of location attribute used for data members of
11015 struct and union types. In the special case of a FIELD_DECL node which
11016 represents a bit-field, the "offset" part of this special location
11017 descriptor must indicate the distance in bytes from the lowest-addressed
11018 byte of the containing struct or union type to the lowest-addressed byte of
11019 the "containing object" for the bit-field. (See the `field_byte_offset'
11020 function above).
11022 For any given bit-field, the "containing object" is a hypothetical object
11023 (of some integral or enum type) within which the given bit-field lives. The
11024 type of this hypothetical "containing object" is always the same as the
11025 declared type of the individual bit-field itself (for GCC anyway... the
11026 DWARF spec doesn't actually mandate this). Note that it is the size (in
11027 bytes) of the hypothetical "containing object" which will be given in the
11028 DW_AT_byte_size attribute for this bit-field. (See the
11029 `byte_size_attribute' function below.) It is also used when calculating the
11030 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
11031 function below.) */
11033 static void
11034 add_data_member_location_attribute (dw_die_ref die, tree decl)
11036 HOST_WIDE_INT offset;
11037 dw_loc_descr_ref loc_descr = 0;
11039 if (TREE_CODE (decl) == TREE_BINFO)
11041 /* We're working on the TAG_inheritance for a base class. */
11042 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
11044 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
11045 aren't at a fixed offset from all (sub)objects of the same
11046 type. We need to extract the appropriate offset from our
11047 vtable. The following dwarf expression means
11049 BaseAddr = ObAddr + *((*ObAddr) - Offset)
11051 This is specific to the V3 ABI, of course. */
11053 dw_loc_descr_ref tmp;
11055 /* Make a copy of the object address. */
11056 tmp = new_loc_descr (DW_OP_dup, 0, 0);
11057 add_loc_descr (&loc_descr, tmp);
11059 /* Extract the vtable address. */
11060 tmp = new_loc_descr (DW_OP_deref, 0, 0);
11061 add_loc_descr (&loc_descr, tmp);
11063 /* Calculate the address of the offset. */
11064 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
11065 gcc_assert (offset < 0);
11067 tmp = int_loc_descriptor (-offset);
11068 add_loc_descr (&loc_descr, tmp);
11069 tmp = new_loc_descr (DW_OP_minus, 0, 0);
11070 add_loc_descr (&loc_descr, tmp);
11072 /* Extract the offset. */
11073 tmp = new_loc_descr (DW_OP_deref, 0, 0);
11074 add_loc_descr (&loc_descr, tmp);
11076 /* Add it to the object address. */
11077 tmp = new_loc_descr (DW_OP_plus, 0, 0);
11078 add_loc_descr (&loc_descr, tmp);
11080 else
11081 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
11083 else
11084 offset = field_byte_offset (decl);
11086 if (! loc_descr)
11088 enum dwarf_location_atom op;
11090 /* The DWARF2 standard says that we should assume that the structure
11091 address is already on the stack, so we can specify a structure field
11092 address by using DW_OP_plus_uconst. */
11094 #ifdef MIPS_DEBUGGING_INFO
11095 /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
11096 operator correctly. It works only if we leave the offset on the
11097 stack. */
11098 op = DW_OP_constu;
11099 #else
11100 op = DW_OP_plus_uconst;
11101 #endif
11103 loc_descr = new_loc_descr (op, offset, 0);
11106 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
11109 /* Writes integer values to dw_vec_const array. */
11111 static void
11112 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
11114 while (size != 0)
11116 *dest++ = val & 0xff;
11117 val >>= 8;
11118 --size;
11122 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
11124 static HOST_WIDE_INT
11125 extract_int (const unsigned char *src, unsigned int size)
11127 HOST_WIDE_INT val = 0;
11129 src += size;
11130 while (size != 0)
11132 val <<= 8;
11133 val |= *--src & 0xff;
11134 --size;
11136 return val;
11139 /* Writes floating point values to dw_vec_const array. */
11141 static void
11142 insert_float (const_rtx rtl, unsigned char *array)
11144 REAL_VALUE_TYPE rv;
11145 long val[4];
11146 int i;
11148 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
11149 real_to_target (val, &rv, GET_MODE (rtl));
11151 /* real_to_target puts 32-bit pieces in each long. Pack them. */
11152 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
11154 insert_int (val[i], 4, array);
11155 array += 4;
11159 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
11160 does not have a "location" either in memory or in a register. These
11161 things can arise in GNU C when a constant is passed as an actual parameter
11162 to an inlined function. They can also arise in C++ where declared
11163 constants do not necessarily get memory "homes". */
11165 static void
11166 add_const_value_attribute (dw_die_ref die, rtx rtl)
11168 switch (GET_CODE (rtl))
11170 case CONST_INT:
11172 HOST_WIDE_INT val = INTVAL (rtl);
11174 if (val < 0)
11175 add_AT_int (die, DW_AT_const_value, val);
11176 else
11177 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
11179 break;
11181 case CONST_DOUBLE:
11182 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
11183 floating-point constant. A CONST_DOUBLE is used whenever the
11184 constant requires more than one word in order to be adequately
11185 represented. We output CONST_DOUBLEs as blocks. */
11187 enum machine_mode mode = GET_MODE (rtl);
11189 if (SCALAR_FLOAT_MODE_P (mode))
11191 unsigned int length = GET_MODE_SIZE (mode);
11192 unsigned char *array = GGC_NEWVEC (unsigned char, length);
11194 insert_float (rtl, array);
11195 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
11197 else
11199 /* ??? We really should be using HOST_WIDE_INT throughout. */
11200 gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
11202 add_AT_long_long (die, DW_AT_const_value,
11203 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
11206 break;
11208 case CONST_VECTOR:
11210 enum machine_mode mode = GET_MODE (rtl);
11211 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
11212 unsigned int length = CONST_VECTOR_NUNITS (rtl);
11213 unsigned char *array = GGC_NEWVEC (unsigned char, length * elt_size);
11214 unsigned int i;
11215 unsigned char *p;
11217 switch (GET_MODE_CLASS (mode))
11219 case MODE_VECTOR_INT:
11220 for (i = 0, p = array; i < length; i++, p += elt_size)
11222 rtx elt = CONST_VECTOR_ELT (rtl, i);
11223 HOST_WIDE_INT lo, hi;
11225 switch (GET_CODE (elt))
11227 case CONST_INT:
11228 lo = INTVAL (elt);
11229 hi = -(lo < 0);
11230 break;
11232 case CONST_DOUBLE:
11233 lo = CONST_DOUBLE_LOW (elt);
11234 hi = CONST_DOUBLE_HIGH (elt);
11235 break;
11237 default:
11238 gcc_unreachable ();
11241 if (elt_size <= sizeof (HOST_WIDE_INT))
11242 insert_int (lo, elt_size, p);
11243 else
11245 unsigned char *p0 = p;
11246 unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
11248 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
11249 if (WORDS_BIG_ENDIAN)
11251 p0 = p1;
11252 p1 = p;
11254 insert_int (lo, sizeof (HOST_WIDE_INT), p0);
11255 insert_int (hi, sizeof (HOST_WIDE_INT), p1);
11258 break;
11260 case MODE_VECTOR_FLOAT:
11261 for (i = 0, p = array; i < length; i++, p += elt_size)
11263 rtx elt = CONST_VECTOR_ELT (rtl, i);
11264 insert_float (elt, p);
11266 break;
11268 default:
11269 gcc_unreachable ();
11272 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
11274 break;
11276 case CONST_STRING:
11277 add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
11278 break;
11280 case SYMBOL_REF:
11281 case LABEL_REF:
11282 case CONST:
11283 add_AT_addr (die, DW_AT_const_value, rtl);
11284 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
11285 break;
11287 case PLUS:
11288 /* In cases where an inlined instance of an inline function is passed
11289 the address of an `auto' variable (which is local to the caller) we
11290 can get a situation where the DECL_RTL of the artificial local
11291 variable (for the inlining) which acts as a stand-in for the
11292 corresponding formal parameter (of the inline function) will look
11293 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
11294 exactly a compile-time constant expression, but it isn't the address
11295 of the (artificial) local variable either. Rather, it represents the
11296 *value* which the artificial local variable always has during its
11297 lifetime. We currently have no way to represent such quasi-constant
11298 values in Dwarf, so for now we just punt and generate nothing. */
11299 break;
11301 default:
11302 /* No other kinds of rtx should be possible here. */
11303 gcc_unreachable ();
11308 /* Determine whether the evaluation of EXPR references any variables
11309 or functions which aren't otherwise used (and therefore may not be
11310 output). */
11311 static tree
11312 reference_to_unused (tree * tp, int * walk_subtrees,
11313 void * data ATTRIBUTE_UNUSED)
11315 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
11316 *walk_subtrees = 0;
11318 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
11319 && ! TREE_ASM_WRITTEN (*tp))
11320 return *tp;
11321 /* ??? The C++ FE emits debug information for using decls, so
11322 putting gcc_unreachable here falls over. See PR31899. For now
11323 be conservative. */
11324 else if (!cgraph_global_info_ready
11325 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
11326 return *tp;
11327 else if (DECL_P (*tp) && TREE_CODE (*tp) == VAR_DECL)
11329 struct varpool_node *node = varpool_node (*tp);
11330 if (!node->needed)
11331 return *tp;
11333 else if (DECL_P (*tp) && TREE_CODE (*tp) == FUNCTION_DECL
11334 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
11336 struct cgraph_node *node = cgraph_node (*tp);
11337 if (node->process || TREE_ASM_WRITTEN (*tp))
11338 return *tp;
11340 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
11341 return *tp;
11343 return NULL_TREE;
11346 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
11347 for use in a later add_const_value_attribute call. */
11349 static rtx
11350 rtl_for_decl_init (tree init, tree type)
11352 rtx rtl = NULL_RTX;
11354 /* If a variable is initialized with a string constant without embedded
11355 zeros, build CONST_STRING. */
11356 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
11358 tree enttype = TREE_TYPE (type);
11359 tree domain = TYPE_DOMAIN (type);
11360 enum machine_mode mode = TYPE_MODE (enttype);
11362 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
11363 && domain
11364 && integer_zerop (TYPE_MIN_VALUE (domain))
11365 && compare_tree_int (TYPE_MAX_VALUE (domain),
11366 TREE_STRING_LENGTH (init) - 1) == 0
11367 && ((size_t) TREE_STRING_LENGTH (init)
11368 == strlen (TREE_STRING_POINTER (init)) + 1))
11369 rtl = gen_rtx_CONST_STRING (VOIDmode,
11370 ggc_strdup (TREE_STRING_POINTER (init)));
11372 /* Other aggregates, and complex values, could be represented using
11373 CONCAT: FIXME! */
11374 else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
11376 /* Vectors only work if their mode is supported by the target.
11377 FIXME: generic vectors ought to work too. */
11378 else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
11380 /* If the initializer is something that we know will expand into an
11381 immediate RTL constant, expand it now. We must be careful not to
11382 reference variables which won't be output. */
11383 else if (initializer_constant_valid_p (init, type)
11384 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
11386 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
11387 possible. */
11388 if (TREE_CODE (type) == VECTOR_TYPE)
11389 switch (TREE_CODE (init))
11391 case VECTOR_CST:
11392 break;
11393 case CONSTRUCTOR:
11394 if (TREE_CONSTANT (init))
11396 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
11397 bool constant_p = true;
11398 tree value;
11399 unsigned HOST_WIDE_INT ix;
11401 /* Even when ctor is constant, it might contain non-*_CST
11402 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
11403 belong into VECTOR_CST nodes. */
11404 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
11405 if (!CONSTANT_CLASS_P (value))
11407 constant_p = false;
11408 break;
11411 if (constant_p)
11413 init = build_vector_from_ctor (type, elts);
11414 break;
11417 /* FALLTHRU */
11419 default:
11420 return NULL;
11423 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
11425 /* If expand_expr returns a MEM, it wasn't immediate. */
11426 gcc_assert (!rtl || !MEM_P (rtl));
11429 return rtl;
11432 /* Generate RTL for the variable DECL to represent its location. */
11434 static rtx
11435 rtl_for_decl_location (tree decl)
11437 rtx rtl;
11439 /* Here we have to decide where we are going to say the parameter "lives"
11440 (as far as the debugger is concerned). We only have a couple of
11441 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
11443 DECL_RTL normally indicates where the parameter lives during most of the
11444 activation of the function. If optimization is enabled however, this
11445 could be either NULL or else a pseudo-reg. Both of those cases indicate
11446 that the parameter doesn't really live anywhere (as far as the code
11447 generation parts of GCC are concerned) during most of the function's
11448 activation. That will happen (for example) if the parameter is never
11449 referenced within the function.
11451 We could just generate a location descriptor here for all non-NULL
11452 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
11453 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
11454 where DECL_RTL is NULL or is a pseudo-reg.
11456 Note however that we can only get away with using DECL_INCOMING_RTL as
11457 a backup substitute for DECL_RTL in certain limited cases. In cases
11458 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
11459 we can be sure that the parameter was passed using the same type as it is
11460 declared to have within the function, and that its DECL_INCOMING_RTL
11461 points us to a place where a value of that type is passed.
11463 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
11464 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
11465 because in these cases DECL_INCOMING_RTL points us to a value of some
11466 type which is *different* from the type of the parameter itself. Thus,
11467 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
11468 such cases, the debugger would end up (for example) trying to fetch a
11469 `float' from a place which actually contains the first part of a
11470 `double'. That would lead to really incorrect and confusing
11471 output at debug-time.
11473 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
11474 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
11475 are a couple of exceptions however. On little-endian machines we can
11476 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
11477 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
11478 an integral type that is smaller than TREE_TYPE (decl). These cases arise
11479 when (on a little-endian machine) a non-prototyped function has a
11480 parameter declared to be of type `short' or `char'. In such cases,
11481 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
11482 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
11483 passed `int' value. If the debugger then uses that address to fetch
11484 a `short' or a `char' (on a little-endian machine) the result will be
11485 the correct data, so we allow for such exceptional cases below.
11487 Note that our goal here is to describe the place where the given formal
11488 parameter lives during most of the function's activation (i.e. between the
11489 end of the prologue and the start of the epilogue). We'll do that as best
11490 as we can. Note however that if the given formal parameter is modified
11491 sometime during the execution of the function, then a stack backtrace (at
11492 debug-time) will show the function as having been called with the *new*
11493 value rather than the value which was originally passed in. This happens
11494 rarely enough that it is not a major problem, but it *is* a problem, and
11495 I'd like to fix it.
11497 A future version of dwarf2out.c may generate two additional attributes for
11498 any given DW_TAG_formal_parameter DIE which will describe the "passed
11499 type" and the "passed location" for the given formal parameter in addition
11500 to the attributes we now generate to indicate the "declared type" and the
11501 "active location" for each parameter. This additional set of attributes
11502 could be used by debuggers for stack backtraces. Separately, note that
11503 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
11504 This happens (for example) for inlined-instances of inline function formal
11505 parameters which are never referenced. This really shouldn't be
11506 happening. All PARM_DECL nodes should get valid non-NULL
11507 DECL_INCOMING_RTL values. FIXME. */
11509 /* Use DECL_RTL as the "location" unless we find something better. */
11510 rtl = DECL_RTL_IF_SET (decl);
11512 /* When generating abstract instances, ignore everything except
11513 constants, symbols living in memory, and symbols living in
11514 fixed registers. */
11515 if (! reload_completed)
11517 if (rtl
11518 && (CONSTANT_P (rtl)
11519 || (MEM_P (rtl)
11520 && CONSTANT_P (XEXP (rtl, 0)))
11521 || (REG_P (rtl)
11522 && TREE_CODE (decl) == VAR_DECL
11523 && TREE_STATIC (decl))))
11525 rtl = targetm.delegitimize_address (rtl);
11526 return rtl;
11528 rtl = NULL_RTX;
11530 else if (TREE_CODE (decl) == PARM_DECL)
11532 if (rtl == NULL_RTX || is_pseudo_reg (rtl))
11534 tree declared_type = TREE_TYPE (decl);
11535 tree passed_type = DECL_ARG_TYPE (decl);
11536 enum machine_mode dmode = TYPE_MODE (declared_type);
11537 enum machine_mode pmode = TYPE_MODE (passed_type);
11539 /* This decl represents a formal parameter which was optimized out.
11540 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
11541 all cases where (rtl == NULL_RTX) just below. */
11542 if (dmode == pmode)
11543 rtl = DECL_INCOMING_RTL (decl);
11544 else if (SCALAR_INT_MODE_P (dmode)
11545 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
11546 && DECL_INCOMING_RTL (decl))
11548 rtx inc = DECL_INCOMING_RTL (decl);
11549 if (REG_P (inc))
11550 rtl = inc;
11551 else if (MEM_P (inc))
11553 if (BYTES_BIG_ENDIAN)
11554 rtl = adjust_address_nv (inc, dmode,
11555 GET_MODE_SIZE (pmode)
11556 - GET_MODE_SIZE (dmode));
11557 else
11558 rtl = inc;
11563 /* If the parm was passed in registers, but lives on the stack, then
11564 make a big endian correction if the mode of the type of the
11565 parameter is not the same as the mode of the rtl. */
11566 /* ??? This is the same series of checks that are made in dbxout.c before
11567 we reach the big endian correction code there. It isn't clear if all
11568 of these checks are necessary here, but keeping them all is the safe
11569 thing to do. */
11570 else if (MEM_P (rtl)
11571 && XEXP (rtl, 0) != const0_rtx
11572 && ! CONSTANT_P (XEXP (rtl, 0))
11573 /* Not passed in memory. */
11574 && !MEM_P (DECL_INCOMING_RTL (decl))
11575 /* Not passed by invisible reference. */
11576 && (!REG_P (XEXP (rtl, 0))
11577 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
11578 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
11579 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
11580 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
11581 #endif
11583 /* Big endian correction check. */
11584 && BYTES_BIG_ENDIAN
11585 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
11586 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
11587 < UNITS_PER_WORD))
11589 int offset = (UNITS_PER_WORD
11590 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
11592 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
11593 plus_constant (XEXP (rtl, 0), offset));
11596 else if (TREE_CODE (decl) == VAR_DECL
11597 && rtl
11598 && MEM_P (rtl)
11599 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
11600 && BYTES_BIG_ENDIAN)
11602 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
11603 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
11605 /* If a variable is declared "register" yet is smaller than
11606 a register, then if we store the variable to memory, it
11607 looks like we're storing a register-sized value, when in
11608 fact we are not. We need to adjust the offset of the
11609 storage location to reflect the actual value's bytes,
11610 else gdb will not be able to display it. */
11611 if (rsize > dsize)
11612 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
11613 plus_constant (XEXP (rtl, 0), rsize-dsize));
11616 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
11617 and will have been substituted directly into all expressions that use it.
11618 C does not have such a concept, but C++ and other languages do. */
11619 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
11620 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
11622 if (rtl)
11623 rtl = targetm.delegitimize_address (rtl);
11625 /* If we don't look past the constant pool, we risk emitting a
11626 reference to a constant pool entry that isn't referenced from
11627 code, and thus is not emitted. */
11628 if (rtl)
11629 rtl = avoid_constant_pool_reference (rtl);
11631 return rtl;
11634 /* We need to figure out what section we should use as the base for the
11635 address ranges where a given location is valid.
11636 1. If this particular DECL has a section associated with it, use that.
11637 2. If this function has a section associated with it, use that.
11638 3. Otherwise, use the text section.
11639 XXX: If you split a variable across multiple sections, we won't notice. */
11641 static const char *
11642 secname_for_decl (const_tree decl)
11644 const char *secname;
11646 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
11648 tree sectree = DECL_SECTION_NAME (decl);
11649 secname = TREE_STRING_POINTER (sectree);
11651 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
11653 tree sectree = DECL_SECTION_NAME (current_function_decl);
11654 secname = TREE_STRING_POINTER (sectree);
11656 else if (cfun && in_cold_section_p)
11657 secname = crtl->subsections.cold_section_label;
11658 else
11659 secname = text_section_label;
11661 return secname;
11664 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
11665 returned. If so, the decl for the COMMON block is returned, and the
11666 value is the offset into the common block for the symbol. */
11668 static tree
11669 fortran_common (tree decl, HOST_WIDE_INT *value)
11671 tree val_expr, cvar;
11672 enum machine_mode mode;
11673 HOST_WIDE_INT bitsize, bitpos;
11674 tree offset;
11675 int volatilep = 0, unsignedp = 0;
11677 /* If the decl isn't a VAR_DECL, or if it isn't public or static, or if
11678 it does not have a value (the offset into the common area), or if it
11679 is thread local (as opposed to global) then it isn't common, and shouldn't
11680 be handled as such. */
11681 if (TREE_CODE (decl) != VAR_DECL
11682 || !TREE_PUBLIC (decl)
11683 || !TREE_STATIC (decl)
11684 || !DECL_HAS_VALUE_EXPR_P (decl)
11685 || !is_fortran ())
11686 return NULL_TREE;
11688 val_expr = DECL_VALUE_EXPR (decl);
11689 if (TREE_CODE (val_expr) != COMPONENT_REF)
11690 return NULL_TREE;
11692 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
11693 &mode, &unsignedp, &volatilep, true);
11695 if (cvar == NULL_TREE
11696 || TREE_CODE (cvar) != VAR_DECL
11697 || DECL_ARTIFICIAL (cvar)
11698 || !TREE_PUBLIC (cvar))
11699 return NULL_TREE;
11701 *value = 0;
11702 if (offset != NULL)
11704 if (!host_integerp (offset, 0))
11705 return NULL_TREE;
11706 *value = tree_low_cst (offset, 0);
11708 if (bitpos != 0)
11709 *value += bitpos / BITS_PER_UNIT;
11711 return cvar;
11714 /* Dereference a location expression LOC if DECL is passed by invisible
11715 reference. */
11717 static dw_loc_descr_ref
11718 loc_by_reference (dw_loc_descr_ref loc, tree decl)
11720 HOST_WIDE_INT size;
11721 enum dwarf_location_atom op;
11723 if (loc == NULL)
11724 return NULL;
11726 if ((TREE_CODE (decl) != PARM_DECL && TREE_CODE (decl) != RESULT_DECL)
11727 || !DECL_BY_REFERENCE (decl))
11728 return loc;
11730 size = int_size_in_bytes (TREE_TYPE (decl));
11731 if (size > DWARF2_ADDR_SIZE || size == -1)
11732 return 0;
11733 else if (size == DWARF2_ADDR_SIZE)
11734 op = DW_OP_deref;
11735 else
11736 op = DW_OP_deref_size;
11737 add_loc_descr (&loc, new_loc_descr (op, size, 0));
11738 return loc;
11741 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
11742 data attribute for a variable or a parameter. We generate the
11743 DW_AT_const_value attribute only in those cases where the given variable
11744 or parameter does not have a true "location" either in memory or in a
11745 register. This can happen (for example) when a constant is passed as an
11746 actual argument in a call to an inline function. (It's possible that
11747 these things can crop up in other ways also.) Note that one type of
11748 constant value which can be passed into an inlined function is a constant
11749 pointer. This can happen for example if an actual argument in an inlined
11750 function call evaluates to a compile-time constant address. */
11752 static void
11753 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
11754 enum dwarf_attribute attr)
11756 rtx rtl;
11757 dw_loc_descr_ref descr;
11758 var_loc_list *loc_list;
11759 struct var_loc_node *node;
11760 if (TREE_CODE (decl) == ERROR_MARK)
11761 return;
11763 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
11764 || TREE_CODE (decl) == RESULT_DECL);
11766 /* See if we possibly have multiple locations for this variable. */
11767 loc_list = lookup_decl_loc (decl);
11769 /* If it truly has multiple locations, the first and last node will
11770 differ. */
11771 if (loc_list && loc_list->first != loc_list->last)
11773 const char *endname, *secname;
11774 dw_loc_list_ref list;
11775 rtx varloc;
11776 enum var_init_status initialized;
11778 /* Now that we know what section we are using for a base,
11779 actually construct the list of locations.
11780 The first location information is what is passed to the
11781 function that creates the location list, and the remaining
11782 locations just get added on to that list.
11783 Note that we only know the start address for a location
11784 (IE location changes), so to build the range, we use
11785 the range [current location start, next location start].
11786 This means we have to special case the last node, and generate
11787 a range of [last location start, end of function label]. */
11789 node = loc_list->first;
11790 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
11791 secname = secname_for_decl (decl);
11793 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note))
11794 initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11795 else
11796 initialized = VAR_INIT_STATUS_INITIALIZED;
11798 descr = loc_by_reference (loc_descriptor (varloc, initialized), decl);
11799 list = new_loc_list (descr, node->label, node->next->label, secname, 1);
11800 node = node->next;
11802 for (; node->next; node = node->next)
11803 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
11805 /* The variable has a location between NODE->LABEL and
11806 NODE->NEXT->LABEL. */
11807 enum var_init_status initialized =
11808 NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11809 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
11810 descr = loc_by_reference (loc_descriptor (varloc, initialized),
11811 decl);
11812 add_loc_descr_to_loc_list (&list, descr,
11813 node->label, node->next->label, secname);
11816 /* If the variable has a location at the last label
11817 it keeps its location until the end of function. */
11818 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
11820 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
11821 enum var_init_status initialized =
11822 NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11824 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
11825 if (!current_function_decl)
11826 endname = text_end_label;
11827 else
11829 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
11830 current_function_funcdef_no);
11831 endname = ggc_strdup (label_id);
11833 descr = loc_by_reference (loc_descriptor (varloc, initialized),
11834 decl);
11835 add_loc_descr_to_loc_list (&list, descr,
11836 node->label, endname, secname);
11839 /* Finally, add the location list to the DIE, and we are done. */
11840 add_AT_loc_list (die, attr, list);
11841 return;
11844 /* Try to get some constant RTL for this decl, and use that as the value of
11845 the location. */
11847 rtl = rtl_for_decl_location (decl);
11848 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
11850 add_const_value_attribute (die, rtl);
11851 return;
11854 /* If we have tried to generate the location otherwise, and it
11855 didn't work out (we wouldn't be here if we did), and we have a one entry
11856 location list, try generating a location from that. */
11857 if (loc_list && loc_list->first)
11859 enum var_init_status status;
11860 node = loc_list->first;
11861 status = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11862 descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note), status);
11863 if (descr)
11865 descr = loc_by_reference (descr, decl);
11866 add_AT_location_description (die, attr, descr);
11867 return;
11871 /* We couldn't get any rtl, so try directly generating the location
11872 description from the tree. */
11873 descr = loc_descriptor_from_tree (decl);
11874 if (descr)
11876 descr = loc_by_reference (descr, decl);
11877 add_AT_location_description (die, attr, descr);
11878 return;
11880 /* None of that worked, so it must not really have a location;
11881 try adding a constant value attribute from the DECL_INITIAL. */
11882 tree_add_const_value_attribute (die, decl);
11885 /* Add VARIABLE and DIE into deferred locations list. */
11887 static void
11888 defer_location (tree variable, dw_die_ref die)
11890 deferred_locations entry;
11891 entry.variable = variable;
11892 entry.die = die;
11893 VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
11896 /* Helper function for tree_add_const_value_attribute. Natively encode
11897 initializer INIT into an array. Return true if successful. */
11899 static bool
11900 native_encode_initializer (tree init, unsigned char *array, int size)
11902 tree type;
11904 if (init == NULL_TREE)
11905 return false;
11907 STRIP_NOPS (init);
11908 switch (TREE_CODE (init))
11910 case STRING_CST:
11911 type = TREE_TYPE (init);
11912 if (TREE_CODE (type) == ARRAY_TYPE)
11914 tree enttype = TREE_TYPE (type);
11915 enum machine_mode mode = TYPE_MODE (enttype);
11917 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
11918 return false;
11919 if (int_size_in_bytes (type) != size)
11920 return false;
11921 if (size > TREE_STRING_LENGTH (init))
11923 memcpy (array, TREE_STRING_POINTER (init),
11924 TREE_STRING_LENGTH (init));
11925 memset (array + TREE_STRING_LENGTH (init),
11926 '\0', size - TREE_STRING_LENGTH (init));
11928 else
11929 memcpy (array, TREE_STRING_POINTER (init), size);
11930 return true;
11932 return false;
11933 case CONSTRUCTOR:
11934 type = TREE_TYPE (init);
11935 if (int_size_in_bytes (type) != size)
11936 return false;
11937 if (TREE_CODE (type) == ARRAY_TYPE)
11939 HOST_WIDE_INT min_index;
11940 unsigned HOST_WIDE_INT cnt;
11941 int curpos = 0, fieldsize;
11942 constructor_elt *ce;
11944 if (TYPE_DOMAIN (type) == NULL_TREE
11945 || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
11946 return false;
11948 fieldsize = int_size_in_bytes (TREE_TYPE (type));
11949 if (fieldsize <= 0)
11950 return false;
11952 min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
11953 memset (array, '\0', size);
11954 for (cnt = 0;
11955 VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
11956 cnt++)
11958 tree val = ce->value;
11959 tree index = ce->index;
11960 int pos = curpos;
11961 if (index && TREE_CODE (index) == RANGE_EXPR)
11962 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
11963 * fieldsize;
11964 else if (index)
11965 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
11967 if (val)
11969 STRIP_NOPS (val);
11970 if (!native_encode_initializer (val, array + pos, fieldsize))
11971 return false;
11973 curpos = pos + fieldsize;
11974 if (index && TREE_CODE (index) == RANGE_EXPR)
11976 int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
11977 - tree_low_cst (TREE_OPERAND (index, 0), 0);
11978 while (count > 0)
11980 if (val)
11981 memcpy (array + curpos, array + pos, fieldsize);
11982 curpos += fieldsize;
11985 gcc_assert (curpos <= size);
11987 return true;
11989 else if (TREE_CODE (type) == RECORD_TYPE
11990 || TREE_CODE (type) == UNION_TYPE)
11992 tree field = NULL_TREE;
11993 unsigned HOST_WIDE_INT cnt;
11994 constructor_elt *ce;
11996 if (int_size_in_bytes (type) != size)
11997 return false;
11999 if (TREE_CODE (type) == RECORD_TYPE)
12000 field = TYPE_FIELDS (type);
12002 for (cnt = 0;
12003 VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
12004 cnt++, field = field ? TREE_CHAIN (field) : 0)
12006 tree val = ce->value;
12007 int pos, fieldsize;
12009 if (ce->index != 0)
12010 field = ce->index;
12012 if (val)
12013 STRIP_NOPS (val);
12015 if (field == NULL_TREE || DECL_BIT_FIELD (field))
12016 return false;
12018 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
12019 && TYPE_DOMAIN (TREE_TYPE (field))
12020 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
12021 return false;
12022 else if (DECL_SIZE_UNIT (field) == NULL_TREE
12023 || !host_integerp (DECL_SIZE_UNIT (field), 0))
12024 return false;
12025 fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
12026 pos = int_byte_position (field);
12027 gcc_assert (pos + fieldsize <= size);
12028 if (val
12029 && !native_encode_initializer (val, array + pos, fieldsize))
12030 return false;
12032 return true;
12034 return false;
12035 case VIEW_CONVERT_EXPR:
12036 case NON_LVALUE_EXPR:
12037 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
12038 default:
12039 return native_encode_expr (init, array, size) == size;
12043 /* If we don't have a copy of this variable in memory for some reason (such
12044 as a C++ member constant that doesn't have an out-of-line definition),
12045 we should tell the debugger about the constant value. */
12047 static void
12048 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
12050 tree init;
12051 tree type = TREE_TYPE (decl);
12052 rtx rtl;
12054 if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != CONST_DECL)
12055 return;
12057 init = DECL_INITIAL (decl);
12058 if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
12059 /* OK */;
12060 else
12061 return;
12063 rtl = rtl_for_decl_init (init, type);
12064 if (rtl)
12065 add_const_value_attribute (var_die, rtl);
12066 /* If the host and target are sane, try harder. */
12067 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
12068 && initializer_constant_valid_p (init, type))
12070 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
12071 if (size > 0 && (int) size == size)
12073 unsigned char *array = GGC_CNEWVEC (unsigned char, size);
12075 if (native_encode_initializer (init, array, size))
12076 add_AT_vec (var_die, DW_AT_const_value, size, 1, array);
12081 /* Convert the CFI instructions for the current function into a
12082 location list. This is used for DW_AT_frame_base when we targeting
12083 a dwarf2 consumer that does not support the dwarf3
12084 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
12085 expressions. */
12087 static dw_loc_list_ref
12088 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
12090 dw_fde_ref fde;
12091 dw_loc_list_ref list, *list_tail;
12092 dw_cfi_ref cfi;
12093 dw_cfa_location last_cfa, next_cfa;
12094 const char *start_label, *last_label, *section;
12096 fde = current_fde ();
12097 gcc_assert (fde != NULL);
12099 section = secname_for_decl (current_function_decl);
12100 list_tail = &list;
12101 list = NULL;
12103 next_cfa.reg = INVALID_REGNUM;
12104 next_cfa.offset = 0;
12105 next_cfa.indirect = 0;
12106 next_cfa.base_offset = 0;
12108 start_label = fde->dw_fde_begin;
12110 /* ??? Bald assumption that the CIE opcode list does not contain
12111 advance opcodes. */
12112 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
12113 lookup_cfa_1 (cfi, &next_cfa);
12115 last_cfa = next_cfa;
12116 last_label = start_label;
12118 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
12119 switch (cfi->dw_cfi_opc)
12121 case DW_CFA_set_loc:
12122 case DW_CFA_advance_loc1:
12123 case DW_CFA_advance_loc2:
12124 case DW_CFA_advance_loc4:
12125 if (!cfa_equal_p (&last_cfa, &next_cfa))
12127 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
12128 start_label, last_label, section,
12129 list == NULL);
12131 list_tail = &(*list_tail)->dw_loc_next;
12132 last_cfa = next_cfa;
12133 start_label = last_label;
12135 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
12136 break;
12138 case DW_CFA_advance_loc:
12139 /* The encoding is complex enough that we should never emit this. */
12140 case DW_CFA_remember_state:
12141 case DW_CFA_restore_state:
12142 /* We don't handle these two in this function. It would be possible
12143 if it were to be required. */
12144 gcc_unreachable ();
12146 default:
12147 lookup_cfa_1 (cfi, &next_cfa);
12148 break;
12151 if (!cfa_equal_p (&last_cfa, &next_cfa))
12153 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
12154 start_label, last_label, section,
12155 list == NULL);
12156 list_tail = &(*list_tail)->dw_loc_next;
12157 start_label = last_label;
12159 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
12160 start_label, fde->dw_fde_end, section,
12161 list == NULL);
12163 return list;
12166 /* Compute a displacement from the "steady-state frame pointer" to the
12167 frame base (often the same as the CFA), and store it in
12168 frame_pointer_fb_offset. OFFSET is added to the displacement
12169 before the latter is negated. */
12171 static void
12172 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
12174 rtx reg, elim;
12176 #ifdef FRAME_POINTER_CFA_OFFSET
12177 reg = frame_pointer_rtx;
12178 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
12179 #else
12180 reg = arg_pointer_rtx;
12181 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
12182 #endif
12184 elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
12185 if (GET_CODE (elim) == PLUS)
12187 offset += INTVAL (XEXP (elim, 1));
12188 elim = XEXP (elim, 0);
12191 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
12192 && (elim == hard_frame_pointer_rtx
12193 || elim == stack_pointer_rtx))
12194 || elim == (frame_pointer_needed
12195 ? hard_frame_pointer_rtx
12196 : stack_pointer_rtx));
12198 frame_pointer_fb_offset = -offset;
12201 /* Generate a DW_AT_name attribute given some string value to be included as
12202 the value of the attribute. */
12204 static void
12205 add_name_attribute (dw_die_ref die, const char *name_string)
12207 if (name_string != NULL && *name_string != 0)
12209 if (demangle_name_func)
12210 name_string = (*demangle_name_func) (name_string);
12212 add_AT_string (die, DW_AT_name, name_string);
12216 /* Generate a DW_AT_comp_dir attribute for DIE. */
12218 static void
12219 add_comp_dir_attribute (dw_die_ref die)
12221 const char *wd = get_src_pwd ();
12222 if (wd != NULL)
12223 add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
12226 /* Given a tree node describing an array bound (either lower or upper) output
12227 a representation for that bound. */
12229 static void
12230 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
12232 switch (TREE_CODE (bound))
12234 case ERROR_MARK:
12235 return;
12237 /* All fixed-bounds are represented by INTEGER_CST nodes. */
12238 case INTEGER_CST:
12239 if (! host_integerp (bound, 0)
12240 || (bound_attr == DW_AT_lower_bound
12241 && (((is_c_family () || is_java ()) && integer_zerop (bound))
12242 || (is_fortran () && integer_onep (bound)))))
12243 /* Use the default. */
12245 else
12246 add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
12247 break;
12249 CASE_CONVERT:
12250 case VIEW_CONVERT_EXPR:
12251 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
12252 break;
12254 case SAVE_EXPR:
12255 break;
12257 case VAR_DECL:
12258 case PARM_DECL:
12259 case RESULT_DECL:
12261 dw_die_ref decl_die = lookup_decl_die (bound);
12262 dw_loc_descr_ref loc;
12264 /* ??? Can this happen, or should the variable have been bound
12265 first? Probably it can, since I imagine that we try to create
12266 the types of parameters in the order in which they exist in
12267 the list, and won't have created a forward reference to a
12268 later parameter. */
12269 if (decl_die != NULL)
12270 add_AT_die_ref (subrange_die, bound_attr, decl_die);
12271 else
12273 loc = loc_descriptor_from_tree_1 (bound, 0);
12274 add_AT_location_description (subrange_die, bound_attr, loc);
12276 break;
12279 default:
12281 /* Otherwise try to create a stack operation procedure to
12282 evaluate the value of the array bound. */
12284 dw_die_ref ctx, decl_die;
12285 dw_loc_descr_ref loc;
12287 loc = loc_descriptor_from_tree (bound);
12288 if (loc == NULL)
12289 break;
12291 if (current_function_decl == 0)
12292 ctx = comp_unit_die;
12293 else
12294 ctx = lookup_decl_die (current_function_decl);
12296 decl_die = new_die (DW_TAG_variable, ctx, bound);
12297 add_AT_flag (decl_die, DW_AT_artificial, 1);
12298 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
12299 add_AT_loc (decl_die, DW_AT_location, loc);
12301 add_AT_die_ref (subrange_die, bound_attr, decl_die);
12302 break;
12307 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
12308 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
12309 Note that the block of subscript information for an array type also
12310 includes information about the element type of the given array type. */
12312 static void
12313 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
12315 unsigned dimension_number;
12316 tree lower, upper;
12317 dw_die_ref subrange_die;
12319 for (dimension_number = 0;
12320 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
12321 type = TREE_TYPE (type), dimension_number++)
12323 tree domain = TYPE_DOMAIN (type);
12325 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
12326 break;
12328 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
12329 and (in GNU C only) variable bounds. Handle all three forms
12330 here. */
12331 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
12332 if (domain)
12334 /* We have an array type with specified bounds. */
12335 lower = TYPE_MIN_VALUE (domain);
12336 upper = TYPE_MAX_VALUE (domain);
12338 /* Define the index type. */
12339 if (TREE_TYPE (domain))
12341 /* ??? This is probably an Ada unnamed subrange type. Ignore the
12342 TREE_TYPE field. We can't emit debug info for this
12343 because it is an unnamed integral type. */
12344 if (TREE_CODE (domain) == INTEGER_TYPE
12345 && TYPE_NAME (domain) == NULL_TREE
12346 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
12347 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
12349 else
12350 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
12351 type_die);
12354 /* ??? If upper is NULL, the array has unspecified length,
12355 but it does have a lower bound. This happens with Fortran
12356 dimension arr(N:*)
12357 Since the debugger is definitely going to need to know N
12358 to produce useful results, go ahead and output the lower
12359 bound solo, and hope the debugger can cope. */
12361 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
12362 if (upper)
12363 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
12366 /* Otherwise we have an array type with an unspecified length. The
12367 DWARF-2 spec does not say how to handle this; let's just leave out the
12368 bounds. */
12372 static void
12373 add_byte_size_attribute (dw_die_ref die, tree tree_node)
12375 unsigned size;
12377 switch (TREE_CODE (tree_node))
12379 case ERROR_MARK:
12380 size = 0;
12381 break;
12382 case ENUMERAL_TYPE:
12383 case RECORD_TYPE:
12384 case UNION_TYPE:
12385 case QUAL_UNION_TYPE:
12386 size = int_size_in_bytes (tree_node);
12387 break;
12388 case FIELD_DECL:
12389 /* For a data member of a struct or union, the DW_AT_byte_size is
12390 generally given as the number of bytes normally allocated for an
12391 object of the *declared* type of the member itself. This is true
12392 even for bit-fields. */
12393 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
12394 break;
12395 default:
12396 gcc_unreachable ();
12399 /* Note that `size' might be -1 when we get to this point. If it is, that
12400 indicates that the byte size of the entity in question is variable. We
12401 have no good way of expressing this fact in Dwarf at the present time,
12402 so just let the -1 pass on through. */
12403 add_AT_unsigned (die, DW_AT_byte_size, size);
12406 /* For a FIELD_DECL node which represents a bit-field, output an attribute
12407 which specifies the distance in bits from the highest order bit of the
12408 "containing object" for the bit-field to the highest order bit of the
12409 bit-field itself.
12411 For any given bit-field, the "containing object" is a hypothetical object
12412 (of some integral or enum type) within which the given bit-field lives. The
12413 type of this hypothetical "containing object" is always the same as the
12414 declared type of the individual bit-field itself. The determination of the
12415 exact location of the "containing object" for a bit-field is rather
12416 complicated. It's handled by the `field_byte_offset' function (above).
12418 Note that it is the size (in bytes) of the hypothetical "containing object"
12419 which will be given in the DW_AT_byte_size attribute for this bit-field.
12420 (See `byte_size_attribute' above). */
12422 static inline void
12423 add_bit_offset_attribute (dw_die_ref die, tree decl)
12425 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
12426 tree type = DECL_BIT_FIELD_TYPE (decl);
12427 HOST_WIDE_INT bitpos_int;
12428 HOST_WIDE_INT highest_order_object_bit_offset;
12429 HOST_WIDE_INT highest_order_field_bit_offset;
12430 HOST_WIDE_INT unsigned bit_offset;
12432 /* Must be a field and a bit field. */
12433 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
12435 /* We can't yet handle bit-fields whose offsets are variable, so if we
12436 encounter such things, just return without generating any attribute
12437 whatsoever. Likewise for variable or too large size. */
12438 if (! host_integerp (bit_position (decl), 0)
12439 || ! host_integerp (DECL_SIZE (decl), 1))
12440 return;
12442 bitpos_int = int_bit_position (decl);
12444 /* Note that the bit offset is always the distance (in bits) from the
12445 highest-order bit of the "containing object" to the highest-order bit of
12446 the bit-field itself. Since the "high-order end" of any object or field
12447 is different on big-endian and little-endian machines, the computation
12448 below must take account of these differences. */
12449 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
12450 highest_order_field_bit_offset = bitpos_int;
12452 if (! BYTES_BIG_ENDIAN)
12454 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
12455 highest_order_object_bit_offset += simple_type_size_in_bits (type);
12458 bit_offset
12459 = (! BYTES_BIG_ENDIAN
12460 ? highest_order_object_bit_offset - highest_order_field_bit_offset
12461 : highest_order_field_bit_offset - highest_order_object_bit_offset);
12463 add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
12466 /* For a FIELD_DECL node which represents a bit field, output an attribute
12467 which specifies the length in bits of the given field. */
12469 static inline void
12470 add_bit_size_attribute (dw_die_ref die, tree decl)
12472 /* Must be a field and a bit field. */
12473 gcc_assert (TREE_CODE (decl) == FIELD_DECL
12474 && DECL_BIT_FIELD_TYPE (decl));
12476 if (host_integerp (DECL_SIZE (decl), 1))
12477 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
12480 /* If the compiled language is ANSI C, then add a 'prototyped'
12481 attribute, if arg types are given for the parameters of a function. */
12483 static inline void
12484 add_prototyped_attribute (dw_die_ref die, tree func_type)
12486 if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
12487 && TYPE_ARG_TYPES (func_type) != NULL)
12488 add_AT_flag (die, DW_AT_prototyped, 1);
12491 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
12492 by looking in either the type declaration or object declaration
12493 equate table. */
12495 static inline dw_die_ref
12496 add_abstract_origin_attribute (dw_die_ref die, tree origin)
12498 dw_die_ref origin_die = NULL;
12500 if (TREE_CODE (origin) != FUNCTION_DECL)
12502 /* We may have gotten separated from the block for the inlined
12503 function, if we're in an exception handler or some such; make
12504 sure that the abstract function has been written out.
12506 Doing this for nested functions is wrong, however; functions are
12507 distinct units, and our context might not even be inline. */
12508 tree fn = origin;
12510 if (TYPE_P (fn))
12511 fn = TYPE_STUB_DECL (fn);
12513 fn = decl_function_context (fn);
12514 if (fn)
12515 dwarf2out_abstract_function (fn);
12518 if (DECL_P (origin))
12519 origin_die = lookup_decl_die (origin);
12520 else if (TYPE_P (origin))
12521 origin_die = lookup_type_die (origin);
12523 /* XXX: Functions that are never lowered don't always have correct block
12524 trees (in the case of java, they simply have no block tree, in some other
12525 languages). For these functions, there is nothing we can really do to
12526 output correct debug info for inlined functions in all cases. Rather
12527 than die, we'll just produce deficient debug info now, in that we will
12528 have variables without a proper abstract origin. In the future, when all
12529 functions are lowered, we should re-add a gcc_assert (origin_die)
12530 here. */
12532 if (origin_die)
12533 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
12534 return origin_die;
12537 /* We do not currently support the pure_virtual attribute. */
12539 static inline void
12540 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
12542 if (DECL_VINDEX (func_decl))
12544 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
12546 if (host_integerp (DECL_VINDEX (func_decl), 0))
12547 add_AT_loc (die, DW_AT_vtable_elem_location,
12548 new_loc_descr (DW_OP_constu,
12549 tree_low_cst (DECL_VINDEX (func_decl), 0),
12550 0));
12552 /* GNU extension: Record what type this method came from originally. */
12553 if (debug_info_level > DINFO_LEVEL_TERSE)
12554 add_AT_die_ref (die, DW_AT_containing_type,
12555 lookup_type_die (DECL_CONTEXT (func_decl)));
12559 /* Add source coordinate attributes for the given decl. */
12561 static void
12562 add_src_coords_attributes (dw_die_ref die, tree decl)
12564 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12566 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
12567 add_AT_unsigned (die, DW_AT_decl_line, s.line);
12570 /* Add a DW_AT_name attribute and source coordinate attribute for the
12571 given decl, but only if it actually has a name. */
12573 static void
12574 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
12576 tree decl_name;
12578 decl_name = DECL_NAME (decl);
12579 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
12581 add_name_attribute (die, dwarf2_name (decl, 0));
12582 if (! DECL_ARTIFICIAL (decl))
12583 add_src_coords_attributes (die, decl);
12585 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
12586 && TREE_PUBLIC (decl)
12587 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
12588 && !DECL_ABSTRACT (decl)
12589 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
12590 && !is_fortran ())
12591 add_AT_string (die, DW_AT_MIPS_linkage_name,
12592 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
12595 #ifdef VMS_DEBUGGING_INFO
12596 /* Get the function's name, as described by its RTL. This may be different
12597 from the DECL_NAME name used in the source file. */
12598 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
12600 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
12601 XEXP (DECL_RTL (decl), 0));
12602 VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
12604 #endif
12607 /* Push a new declaration scope. */
12609 static void
12610 push_decl_scope (tree scope)
12612 VEC_safe_push (tree, gc, decl_scope_table, scope);
12615 /* Pop a declaration scope. */
12617 static inline void
12618 pop_decl_scope (void)
12620 VEC_pop (tree, decl_scope_table);
12623 /* Return the DIE for the scope that immediately contains this type.
12624 Non-named types get global scope. Named types nested in other
12625 types get their containing scope if it's open, or global scope
12626 otherwise. All other types (i.e. function-local named types) get
12627 the current active scope. */
12629 static dw_die_ref
12630 scope_die_for (tree t, dw_die_ref context_die)
12632 dw_die_ref scope_die = NULL;
12633 tree containing_scope;
12634 int i;
12636 /* Non-types always go in the current scope. */
12637 gcc_assert (TYPE_P (t));
12639 containing_scope = TYPE_CONTEXT (t);
12641 /* Use the containing namespace if it was passed in (for a declaration). */
12642 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
12644 if (context_die == lookup_decl_die (containing_scope))
12645 /* OK */;
12646 else
12647 containing_scope = NULL_TREE;
12650 /* Ignore function type "scopes" from the C frontend. They mean that
12651 a tagged type is local to a parmlist of a function declarator, but
12652 that isn't useful to DWARF. */
12653 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
12654 containing_scope = NULL_TREE;
12656 if (containing_scope == NULL_TREE)
12657 scope_die = comp_unit_die;
12658 else if (TYPE_P (containing_scope))
12660 /* For types, we can just look up the appropriate DIE. But
12661 first we check to see if we're in the middle of emitting it
12662 so we know where the new DIE should go. */
12663 for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
12664 if (VEC_index (tree, decl_scope_table, i) == containing_scope)
12665 break;
12667 if (i < 0)
12669 gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
12670 || TREE_ASM_WRITTEN (containing_scope));
12672 /* If none of the current dies are suitable, we get file scope. */
12673 scope_die = comp_unit_die;
12675 else
12676 scope_die = lookup_type_die (containing_scope);
12678 else
12679 scope_die = context_die;
12681 return scope_die;
12684 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
12686 static inline int
12687 local_scope_p (dw_die_ref context_die)
12689 for (; context_die; context_die = context_die->die_parent)
12690 if (context_die->die_tag == DW_TAG_inlined_subroutine
12691 || context_die->die_tag == DW_TAG_subprogram)
12692 return 1;
12694 return 0;
12697 /* Returns nonzero if CONTEXT_DIE is a class. */
12699 static inline int
12700 class_scope_p (dw_die_ref context_die)
12702 return (context_die
12703 && (context_die->die_tag == DW_TAG_structure_type
12704 || context_die->die_tag == DW_TAG_class_type
12705 || context_die->die_tag == DW_TAG_interface_type
12706 || context_die->die_tag == DW_TAG_union_type));
12709 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
12710 whether or not to treat a DIE in this context as a declaration. */
12712 static inline int
12713 class_or_namespace_scope_p (dw_die_ref context_die)
12715 return (class_scope_p (context_die)
12716 || (context_die && context_die->die_tag == DW_TAG_namespace));
12719 /* Many forms of DIEs require a "type description" attribute. This
12720 routine locates the proper "type descriptor" die for the type given
12721 by 'type', and adds a DW_AT_type attribute below the given die. */
12723 static void
12724 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
12725 int decl_volatile, dw_die_ref context_die)
12727 enum tree_code code = TREE_CODE (type);
12728 dw_die_ref type_die = NULL;
12730 /* ??? If this type is an unnamed subrange type of an integral, floating-point
12731 or fixed-point type, use the inner type. This is because we have no
12732 support for unnamed types in base_type_die. This can happen if this is
12733 an Ada subrange type. Correct solution is emit a subrange type die. */
12734 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
12735 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
12736 type = TREE_TYPE (type), code = TREE_CODE (type);
12738 if (code == ERROR_MARK
12739 /* Handle a special case. For functions whose return type is void, we
12740 generate *no* type attribute. (Note that no object may have type
12741 `void', so this only applies to function return types). */
12742 || code == VOID_TYPE)
12743 return;
12745 type_die = modified_type_die (type,
12746 decl_const || TYPE_READONLY (type),
12747 decl_volatile || TYPE_VOLATILE (type),
12748 context_die);
12750 if (type_die != NULL)
12751 add_AT_die_ref (object_die, DW_AT_type, type_die);
12754 /* Given an object die, add the calling convention attribute for the
12755 function call type. */
12756 static void
12757 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
12759 enum dwarf_calling_convention value = DW_CC_normal;
12761 value = targetm.dwarf_calling_convention (TREE_TYPE (decl));
12763 /* DWARF doesn't provide a way to identify a program's source-level
12764 entry point. DW_AT_calling_convention attributes are only meant
12765 to describe functions' calling conventions. However, lacking a
12766 better way to signal the Fortran main program, we use this for the
12767 time being, following existing custom. */
12768 if (is_fortran ()
12769 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
12770 value = DW_CC_program;
12772 /* Only add the attribute if the backend requests it, and
12773 is not DW_CC_normal. */
12774 if (value && (value != DW_CC_normal))
12775 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
12778 /* Given a tree pointer to a struct, class, union, or enum type node, return
12779 a pointer to the (string) tag name for the given type, or zero if the type
12780 was declared without a tag. */
12782 static const char *
12783 type_tag (const_tree type)
12785 const char *name = 0;
12787 if (TYPE_NAME (type) != 0)
12789 tree t = 0;
12791 /* Find the IDENTIFIER_NODE for the type name. */
12792 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
12793 t = TYPE_NAME (type);
12795 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
12796 a TYPE_DECL node, regardless of whether or not a `typedef' was
12797 involved. */
12798 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12799 && ! DECL_IGNORED_P (TYPE_NAME (type)))
12801 /* We want to be extra verbose. Don't call dwarf_name if
12802 DECL_NAME isn't set. The default hook for decl_printable_name
12803 doesn't like that, and in this context it's correct to return
12804 0, instead of "<anonymous>" or the like. */
12805 if (DECL_NAME (TYPE_NAME (type)))
12806 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
12809 /* Now get the name as a string, or invent one. */
12810 if (!name && t != 0)
12811 name = IDENTIFIER_POINTER (t);
12814 return (name == 0 || *name == '\0') ? 0 : name;
12817 /* Return the type associated with a data member, make a special check
12818 for bit field types. */
12820 static inline tree
12821 member_declared_type (const_tree member)
12823 return (DECL_BIT_FIELD_TYPE (member)
12824 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
12827 /* Get the decl's label, as described by its RTL. This may be different
12828 from the DECL_NAME name used in the source file. */
12830 #if 0
12831 static const char *
12832 decl_start_label (tree decl)
12834 rtx x;
12835 const char *fnname;
12837 x = DECL_RTL (decl);
12838 gcc_assert (MEM_P (x));
12840 x = XEXP (x, 0);
12841 gcc_assert (GET_CODE (x) == SYMBOL_REF);
12843 fnname = XSTR (x, 0);
12844 return fnname;
12846 #endif
12848 /* These routines generate the internal representation of the DIE's for
12849 the compilation unit. Debugging information is collected by walking
12850 the declaration trees passed in from dwarf2out_decl(). */
12852 static void
12853 gen_array_type_die (tree type, dw_die_ref context_die)
12855 dw_die_ref scope_die = scope_die_for (type, context_die);
12856 dw_die_ref array_die;
12858 /* GNU compilers represent multidimensional array types as sequences of one
12859 dimensional array types whose element types are themselves array types.
12860 We sometimes squish that down to a single array_type DIE with multiple
12861 subscripts in the Dwarf debugging info. The draft Dwarf specification
12862 say that we are allowed to do this kind of compression in C, because
12863 there is no difference between an array of arrays and a multidimensional
12864 array. We don't do this for Ada to remain as close as possible to the
12865 actual representation, which is especially important against the language
12866 flexibilty wrt arrays of variable size. */
12868 bool collapse_nested_arrays = !is_ada ();
12869 tree element_type;
12871 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
12872 DW_TAG_string_type doesn't have DW_AT_type attribute). */
12873 if (TYPE_STRING_FLAG (type)
12874 && TREE_CODE (type) == ARRAY_TYPE
12875 && is_fortran ()
12876 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
12878 HOST_WIDE_INT size;
12880 array_die = new_die (DW_TAG_string_type, scope_die, type);
12881 add_name_attribute (array_die, type_tag (type));
12882 equate_type_number_to_die (type, array_die);
12883 size = int_size_in_bytes (type);
12884 if (size >= 0)
12885 add_AT_unsigned (array_die, DW_AT_byte_size, size);
12886 else if (TYPE_DOMAIN (type) != NULL_TREE
12887 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
12888 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
12890 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
12891 dw_loc_descr_ref loc = loc_descriptor_from_tree (szdecl);
12893 size = int_size_in_bytes (TREE_TYPE (szdecl));
12894 if (loc && size > 0)
12896 add_AT_loc (array_die, DW_AT_string_length, loc);
12897 if (size != DWARF2_ADDR_SIZE)
12898 add_AT_unsigned (array_die, DW_AT_byte_size, size);
12901 return;
12904 /* ??? The SGI dwarf reader fails for array of array of enum types
12905 (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
12906 array type comes before the outer array type. We thus call gen_type_die
12907 before we new_die and must prevent nested array types collapsing for this
12908 target. */
12910 #ifdef MIPS_DEBUGGING_INFO
12911 gen_type_die (TREE_TYPE (type), context_die);
12912 collapse_nested_arrays = false;
12913 #endif
12915 array_die = new_die (DW_TAG_array_type, scope_die, type);
12916 add_name_attribute (array_die, type_tag (type));
12917 equate_type_number_to_die (type, array_die);
12919 if (TREE_CODE (type) == VECTOR_TYPE)
12921 /* The frontend feeds us a representation for the vector as a struct
12922 containing an array. Pull out the array type. */
12923 type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
12924 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
12927 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
12928 if (is_fortran ()
12929 && TREE_CODE (type) == ARRAY_TYPE
12930 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
12931 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
12932 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
12934 #if 0
12935 /* We default the array ordering. SDB will probably do
12936 the right things even if DW_AT_ordering is not present. It's not even
12937 an issue until we start to get into multidimensional arrays anyway. If
12938 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
12939 then we'll have to put the DW_AT_ordering attribute back in. (But if
12940 and when we find out that we need to put these in, we will only do so
12941 for multidimensional arrays. */
12942 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
12943 #endif
12945 #ifdef MIPS_DEBUGGING_INFO
12946 /* The SGI compilers handle arrays of unknown bound by setting
12947 AT_declaration and not emitting any subrange DIEs. */
12948 if (! TYPE_DOMAIN (type))
12949 add_AT_flag (array_die, DW_AT_declaration, 1);
12950 else
12951 #endif
12952 add_subscript_info (array_die, type, collapse_nested_arrays);
12954 /* Add representation of the type of the elements of this array type and
12955 emit the corresponding DIE if we haven't done it already. */
12956 element_type = TREE_TYPE (type);
12957 if (collapse_nested_arrays)
12958 while (TREE_CODE (element_type) == ARRAY_TYPE)
12960 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
12961 break;
12962 element_type = TREE_TYPE (element_type);
12965 #ifndef MIPS_DEBUGGING_INFO
12966 gen_type_die (element_type, context_die);
12967 #endif
12969 add_type_attribute (array_die, element_type, 0, 0, context_die);
12971 if (get_AT (array_die, DW_AT_name))
12972 add_pubtype (type, array_die);
12975 static dw_loc_descr_ref
12976 descr_info_loc (tree val, tree base_decl)
12978 HOST_WIDE_INT size;
12979 dw_loc_descr_ref loc, loc2;
12980 enum dwarf_location_atom op;
12982 if (val == base_decl)
12983 return new_loc_descr (DW_OP_push_object_address, 0, 0);
12985 switch (TREE_CODE (val))
12987 CASE_CONVERT:
12988 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
12989 case VAR_DECL:
12990 return loc_descriptor_from_tree_1 (val, 0);
12991 case INTEGER_CST:
12992 if (host_integerp (val, 0))
12993 return int_loc_descriptor (tree_low_cst (val, 0));
12994 break;
12995 case INDIRECT_REF:
12996 size = int_size_in_bytes (TREE_TYPE (val));
12997 if (size < 0)
12998 break;
12999 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13000 if (!loc)
13001 break;
13002 if (size == DWARF2_ADDR_SIZE)
13003 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
13004 else
13005 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
13006 return loc;
13007 case POINTER_PLUS_EXPR:
13008 case PLUS_EXPR:
13009 if (host_integerp (TREE_OPERAND (val, 1), 1)
13010 && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
13011 < 16384)
13013 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13014 if (!loc)
13015 break;
13016 add_loc_descr (&loc,
13017 new_loc_descr (DW_OP_plus_uconst,
13018 tree_low_cst (TREE_OPERAND (val, 1),
13019 1), 0));
13021 else
13023 op = DW_OP_plus;
13024 do_binop:
13025 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13026 if (!loc)
13027 break;
13028 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
13029 if (!loc2)
13030 break;
13031 add_loc_descr (&loc, loc2);
13032 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
13034 return loc;
13035 case MINUS_EXPR:
13036 op = DW_OP_minus;
13037 goto do_binop;
13038 case MULT_EXPR:
13039 op = DW_OP_mul;
13040 goto do_binop;
13041 case EQ_EXPR:
13042 op = DW_OP_eq;
13043 goto do_binop;
13044 case NE_EXPR:
13045 op = DW_OP_ne;
13046 goto do_binop;
13047 default:
13048 break;
13050 return NULL;
13053 static void
13054 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
13055 tree val, tree base_decl)
13057 dw_loc_descr_ref loc;
13059 if (host_integerp (val, 0))
13061 add_AT_unsigned (die, attr, tree_low_cst (val, 0));
13062 return;
13065 loc = descr_info_loc (val, base_decl);
13066 if (!loc)
13067 return;
13069 add_AT_loc (die, attr, loc);
13072 /* This routine generates DIE for array with hidden descriptor, details
13073 are filled into *info by a langhook. */
13075 static void
13076 gen_descr_array_type_die (tree type, struct array_descr_info *info,
13077 dw_die_ref context_die)
13079 dw_die_ref scope_die = scope_die_for (type, context_die);
13080 dw_die_ref array_die;
13081 int dim;
13083 array_die = new_die (DW_TAG_array_type, scope_die, type);
13084 add_name_attribute (array_die, type_tag (type));
13085 equate_type_number_to_die (type, array_die);
13087 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
13088 if (is_fortran ()
13089 && info->ndimensions >= 2)
13090 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
13092 if (info->data_location)
13093 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
13094 info->base_decl);
13095 if (info->associated)
13096 add_descr_info_field (array_die, DW_AT_associated, info->associated,
13097 info->base_decl);
13098 if (info->allocated)
13099 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
13100 info->base_decl);
13102 for (dim = 0; dim < info->ndimensions; dim++)
13104 dw_die_ref subrange_die
13105 = new_die (DW_TAG_subrange_type, array_die, NULL);
13107 if (info->dimen[dim].lower_bound)
13109 /* If it is the default value, omit it. */
13110 if ((is_c_family () || is_java ())
13111 && integer_zerop (info->dimen[dim].lower_bound))
13113 else if (is_fortran ()
13114 && integer_onep (info->dimen[dim].lower_bound))
13116 else
13117 add_descr_info_field (subrange_die, DW_AT_lower_bound,
13118 info->dimen[dim].lower_bound,
13119 info->base_decl);
13121 if (info->dimen[dim].upper_bound)
13122 add_descr_info_field (subrange_die, DW_AT_upper_bound,
13123 info->dimen[dim].upper_bound,
13124 info->base_decl);
13125 if (info->dimen[dim].stride)
13126 add_descr_info_field (subrange_die, DW_AT_byte_stride,
13127 info->dimen[dim].stride,
13128 info->base_decl);
13131 gen_type_die (info->element_type, context_die);
13132 add_type_attribute (array_die, info->element_type, 0, 0, context_die);
13134 if (get_AT (array_die, DW_AT_name))
13135 add_pubtype (type, array_die);
13138 #if 0
13139 static void
13140 gen_entry_point_die (tree decl, dw_die_ref context_die)
13142 tree origin = decl_ultimate_origin (decl);
13143 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
13145 if (origin != NULL)
13146 add_abstract_origin_attribute (decl_die, origin);
13147 else
13149 add_name_and_src_coords_attributes (decl_die, decl);
13150 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
13151 0, 0, context_die);
13154 if (DECL_ABSTRACT (decl))
13155 equate_decl_number_to_die (decl, decl_die);
13156 else
13157 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
13159 #endif
13161 /* Walk through the list of incomplete types again, trying once more to
13162 emit full debugging info for them. */
13164 static void
13165 retry_incomplete_types (void)
13167 int i;
13169 for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
13170 gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
13173 /* Determine what tag to use for a record type. */
13175 static enum dwarf_tag
13176 record_type_tag (tree type)
13178 if (! lang_hooks.types.classify_record)
13179 return DW_TAG_structure_type;
13181 switch (lang_hooks.types.classify_record (type))
13183 case RECORD_IS_STRUCT:
13184 return DW_TAG_structure_type;
13186 case RECORD_IS_CLASS:
13187 return DW_TAG_class_type;
13189 case RECORD_IS_INTERFACE:
13190 return DW_TAG_interface_type;
13192 default:
13193 gcc_unreachable ();
13197 /* Generate a DIE to represent an enumeration type. Note that these DIEs
13198 include all of the information about the enumeration values also. Each
13199 enumerated type name/value is listed as a child of the enumerated type
13200 DIE. */
13202 static dw_die_ref
13203 gen_enumeration_type_die (tree type, dw_die_ref context_die)
13205 dw_die_ref type_die = lookup_type_die (type);
13207 if (type_die == NULL)
13209 type_die = new_die (DW_TAG_enumeration_type,
13210 scope_die_for (type, context_die), type);
13211 equate_type_number_to_die (type, type_die);
13212 add_name_attribute (type_die, type_tag (type));
13214 else if (! TYPE_SIZE (type))
13215 return type_die;
13216 else
13217 remove_AT (type_die, DW_AT_declaration);
13219 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
13220 given enum type is incomplete, do not generate the DW_AT_byte_size
13221 attribute or the DW_AT_element_list attribute. */
13222 if (TYPE_SIZE (type))
13224 tree link;
13226 TREE_ASM_WRITTEN (type) = 1;
13227 add_byte_size_attribute (type_die, type);
13228 if (TYPE_STUB_DECL (type) != NULL_TREE)
13229 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
13231 /* If the first reference to this type was as the return type of an
13232 inline function, then it may not have a parent. Fix this now. */
13233 if (type_die->die_parent == NULL)
13234 add_child_die (scope_die_for (type, context_die), type_die);
13236 for (link = TYPE_VALUES (type);
13237 link != NULL; link = TREE_CHAIN (link))
13239 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
13240 tree value = TREE_VALUE (link);
13242 add_name_attribute (enum_die,
13243 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
13245 if (TREE_CODE (value) == CONST_DECL)
13246 value = DECL_INITIAL (value);
13248 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
13249 /* DWARF2 does not provide a way of indicating whether or
13250 not enumeration constants are signed or unsigned. GDB
13251 always assumes the values are signed, so we output all
13252 values as if they were signed. That means that
13253 enumeration constants with very large unsigned values
13254 will appear to have negative values in the debugger. */
13255 add_AT_int (enum_die, DW_AT_const_value,
13256 tree_low_cst (value, tree_int_cst_sgn (value) > 0));
13259 else
13260 add_AT_flag (type_die, DW_AT_declaration, 1);
13262 if (get_AT (type_die, DW_AT_name))
13263 add_pubtype (type, type_die);
13265 return type_die;
13268 /* Generate a DIE to represent either a real live formal parameter decl or to
13269 represent just the type of some formal parameter position in some function
13270 type.
13272 Note that this routine is a bit unusual because its argument may be a
13273 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
13274 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
13275 node. If it's the former then this function is being called to output a
13276 DIE to represent a formal parameter object (or some inlining thereof). If
13277 it's the latter, then this function is only being called to output a
13278 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
13279 argument type of some subprogram type. */
13281 static dw_die_ref
13282 gen_formal_parameter_die (tree node, tree origin, dw_die_ref context_die)
13284 tree node_or_origin = node ? node : origin;
13285 dw_die_ref parm_die
13286 = new_die (DW_TAG_formal_parameter, context_die, node);
13288 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
13290 case tcc_declaration:
13291 if (!origin)
13292 origin = decl_ultimate_origin (node);
13293 if (origin != NULL)
13294 add_abstract_origin_attribute (parm_die, origin);
13295 else
13297 tree type = TREE_TYPE (node);
13298 add_name_and_src_coords_attributes (parm_die, node);
13299 if (DECL_BY_REFERENCE (node))
13300 add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
13301 context_die);
13302 else
13303 add_type_attribute (parm_die, type,
13304 TREE_READONLY (node),
13305 TREE_THIS_VOLATILE (node),
13306 context_die);
13307 if (DECL_ARTIFICIAL (node))
13308 add_AT_flag (parm_die, DW_AT_artificial, 1);
13311 if (node)
13312 equate_decl_number_to_die (node, parm_die);
13313 if (! DECL_ABSTRACT (node_or_origin))
13314 add_location_or_const_value_attribute (parm_die, node_or_origin,
13315 DW_AT_location);
13317 break;
13319 case tcc_type:
13320 /* We were called with some kind of a ..._TYPE node. */
13321 add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
13322 break;
13324 default:
13325 gcc_unreachable ();
13328 return parm_die;
13331 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
13332 at the end of an (ANSI prototyped) formal parameters list. */
13334 static void
13335 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
13337 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
13340 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
13341 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
13342 parameters as specified in some function type specification (except for
13343 those which appear as part of a function *definition*). */
13345 static void
13346 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
13348 tree link;
13349 tree formal_type = NULL;
13350 tree first_parm_type;
13351 tree arg;
13353 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
13355 arg = DECL_ARGUMENTS (function_or_method_type);
13356 function_or_method_type = TREE_TYPE (function_or_method_type);
13358 else
13359 arg = NULL_TREE;
13361 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
13363 /* Make our first pass over the list of formal parameter types and output a
13364 DW_TAG_formal_parameter DIE for each one. */
13365 for (link = first_parm_type; link; )
13367 dw_die_ref parm_die;
13369 formal_type = TREE_VALUE (link);
13370 if (formal_type == void_type_node)
13371 break;
13373 /* Output a (nameless) DIE to represent the formal parameter itself. */
13374 parm_die = gen_formal_parameter_die (formal_type, NULL, context_die);
13375 if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
13376 && link == first_parm_type)
13377 || (arg && DECL_ARTIFICIAL (arg)))
13378 add_AT_flag (parm_die, DW_AT_artificial, 1);
13380 link = TREE_CHAIN (link);
13381 if (arg)
13382 arg = TREE_CHAIN (arg);
13385 /* If this function type has an ellipsis, add a
13386 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
13387 if (formal_type != void_type_node)
13388 gen_unspecified_parameters_die (function_or_method_type, context_die);
13390 /* Make our second (and final) pass over the list of formal parameter types
13391 and output DIEs to represent those types (as necessary). */
13392 for (link = TYPE_ARG_TYPES (function_or_method_type);
13393 link && TREE_VALUE (link);
13394 link = TREE_CHAIN (link))
13395 gen_type_die (TREE_VALUE (link), context_die);
13398 /* We want to generate the DIE for TYPE so that we can generate the
13399 die for MEMBER, which has been defined; we will need to refer back
13400 to the member declaration nested within TYPE. If we're trying to
13401 generate minimal debug info for TYPE, processing TYPE won't do the
13402 trick; we need to attach the member declaration by hand. */
13404 static void
13405 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
13407 gen_type_die (type, context_die);
13409 /* If we're trying to avoid duplicate debug info, we may not have
13410 emitted the member decl for this function. Emit it now. */
13411 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
13412 && ! lookup_decl_die (member))
13414 dw_die_ref type_die;
13415 gcc_assert (!decl_ultimate_origin (member));
13417 push_decl_scope (type);
13418 type_die = lookup_type_die (type);
13419 if (TREE_CODE (member) == FUNCTION_DECL)
13420 gen_subprogram_die (member, type_die);
13421 else if (TREE_CODE (member) == FIELD_DECL)
13423 /* Ignore the nameless fields that are used to skip bits but handle
13424 C++ anonymous unions and structs. */
13425 if (DECL_NAME (member) != NULL_TREE
13426 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
13427 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
13429 gen_type_die (member_declared_type (member), type_die);
13430 gen_field_die (member, type_die);
13433 else
13434 gen_variable_die (member, NULL_TREE, type_die);
13436 pop_decl_scope ();
13440 /* Generate the DWARF2 info for the "abstract" instance of a function which we
13441 may later generate inlined and/or out-of-line instances of. */
13443 static void
13444 dwarf2out_abstract_function (tree decl)
13446 dw_die_ref old_die;
13447 tree save_fn;
13448 tree context;
13449 int was_abstract = DECL_ABSTRACT (decl);
13451 /* Make sure we have the actual abstract inline, not a clone. */
13452 decl = DECL_ORIGIN (decl);
13454 old_die = lookup_decl_die (decl);
13455 if (old_die && get_AT (old_die, DW_AT_inline))
13456 /* We've already generated the abstract instance. */
13457 return;
13459 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
13460 we don't get confused by DECL_ABSTRACT. */
13461 if (debug_info_level > DINFO_LEVEL_TERSE)
13463 context = decl_class_context (decl);
13464 if (context)
13465 gen_type_die_for_member
13466 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
13469 /* Pretend we've just finished compiling this function. */
13470 save_fn = current_function_decl;
13471 current_function_decl = decl;
13472 push_cfun (DECL_STRUCT_FUNCTION (decl));
13474 set_decl_abstract_flags (decl, 1);
13475 dwarf2out_decl (decl);
13476 if (! was_abstract)
13477 set_decl_abstract_flags (decl, 0);
13479 current_function_decl = save_fn;
13480 pop_cfun ();
13483 /* Helper function of premark_used_types() which gets called through
13484 htab_traverse_resize().
13486 Marks the DIE of a given type in *SLOT as perennial, so it never gets
13487 marked as unused by prune_unused_types. */
13488 static int
13489 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
13491 tree type;
13492 dw_die_ref die;
13494 type = (tree) *slot;
13495 die = lookup_type_die (type);
13496 if (die != NULL)
13497 die->die_perennial_p = 1;
13498 return 1;
13501 /* Mark all members of used_types_hash as perennial. */
13502 static void
13503 premark_used_types (void)
13505 if (cfun && cfun->used_types_hash)
13506 htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
13509 /* Generate a DIE to represent a declared function (either file-scope or
13510 block-local). */
13512 static void
13513 gen_subprogram_die (tree decl, dw_die_ref context_die)
13515 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13516 tree origin = decl_ultimate_origin (decl);
13517 dw_die_ref subr_die;
13518 tree fn_arg_types;
13519 tree outer_scope;
13520 dw_die_ref old_die = lookup_decl_die (decl);
13521 int declaration = (current_function_decl != decl
13522 || class_or_namespace_scope_p (context_die));
13524 premark_used_types ();
13526 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
13527 started to generate the abstract instance of an inline, decided to output
13528 its containing class, and proceeded to emit the declaration of the inline
13529 from the member list for the class. If so, DECLARATION takes priority;
13530 we'll get back to the abstract instance when done with the class. */
13532 /* The class-scope declaration DIE must be the primary DIE. */
13533 if (origin && declaration && class_or_namespace_scope_p (context_die))
13535 origin = NULL;
13536 gcc_assert (!old_die);
13539 /* Now that the C++ front end lazily declares artificial member fns, we
13540 might need to retrofit the declaration into its class. */
13541 if (!declaration && !origin && !old_die
13542 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
13543 && !class_or_namespace_scope_p (context_die)
13544 && debug_info_level > DINFO_LEVEL_TERSE)
13545 old_die = force_decl_die (decl);
13547 if (origin != NULL)
13549 gcc_assert (!declaration || local_scope_p (context_die));
13551 /* Fixup die_parent for the abstract instance of a nested
13552 inline function. */
13553 if (old_die && old_die->die_parent == NULL)
13554 add_child_die (context_die, old_die);
13556 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13557 add_abstract_origin_attribute (subr_die, origin);
13559 else if (old_die)
13561 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
13562 struct dwarf_file_data * file_index = lookup_filename (s.file);
13564 if (!get_AT_flag (old_die, DW_AT_declaration)
13565 /* We can have a normal definition following an inline one in the
13566 case of redefinition of GNU C extern inlines.
13567 It seems reasonable to use AT_specification in this case. */
13568 && !get_AT (old_die, DW_AT_inline))
13570 /* Detect and ignore this case, where we are trying to output
13571 something we have already output. */
13572 return;
13575 /* If the definition comes from the same place as the declaration,
13576 maybe use the old DIE. We always want the DIE for this function
13577 that has the *_pc attributes to be under comp_unit_die so the
13578 debugger can find it. We also need to do this for abstract
13579 instances of inlines, since the spec requires the out-of-line copy
13580 to have the same parent. For local class methods, this doesn't
13581 apply; we just use the old DIE. */
13582 if ((old_die->die_parent == comp_unit_die || context_die == NULL)
13583 && (DECL_ARTIFICIAL (decl)
13584 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
13585 && (get_AT_unsigned (old_die, DW_AT_decl_line)
13586 == (unsigned) s.line))))
13588 subr_die = old_die;
13590 /* Clear out the declaration attribute and the formal parameters.
13591 Do not remove all children, because it is possible that this
13592 declaration die was forced using force_decl_die(). In such
13593 cases die that forced declaration die (e.g. TAG_imported_module)
13594 is one of the children that we do not want to remove. */
13595 remove_AT (subr_die, DW_AT_declaration);
13596 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
13598 else
13600 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13601 add_AT_specification (subr_die, old_die);
13602 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
13603 add_AT_file (subr_die, DW_AT_decl_file, file_index);
13604 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
13605 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
13608 else
13610 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13612 if (TREE_PUBLIC (decl))
13613 add_AT_flag (subr_die, DW_AT_external, 1);
13615 add_name_and_src_coords_attributes (subr_die, decl);
13616 if (debug_info_level > DINFO_LEVEL_TERSE)
13618 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
13619 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
13620 0, 0, context_die);
13623 add_pure_or_virtual_attribute (subr_die, decl);
13624 if (DECL_ARTIFICIAL (decl))
13625 add_AT_flag (subr_die, DW_AT_artificial, 1);
13627 if (TREE_PROTECTED (decl))
13628 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
13629 else if (TREE_PRIVATE (decl))
13630 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
13633 if (declaration)
13635 if (!old_die || !get_AT (old_die, DW_AT_inline))
13637 add_AT_flag (subr_die, DW_AT_declaration, 1);
13639 /* If this is an explicit function declaration then generate
13640 a DW_AT_explicit attribute. */
13641 if (lang_hooks.decls.function_decl_explicit_p (decl))
13642 add_AT_flag (subr_die, DW_AT_explicit, 1);
13644 /* The first time we see a member function, it is in the context of
13645 the class to which it belongs. We make sure of this by emitting
13646 the class first. The next time is the definition, which is
13647 handled above. The two may come from the same source text.
13649 Note that force_decl_die() forces function declaration die. It is
13650 later reused to represent definition. */
13651 equate_decl_number_to_die (decl, subr_die);
13654 else if (DECL_ABSTRACT (decl))
13656 if (DECL_DECLARED_INLINE_P (decl))
13658 if (cgraph_function_possibly_inlined_p (decl))
13659 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
13660 else
13661 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
13663 else
13665 if (cgraph_function_possibly_inlined_p (decl))
13666 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
13667 else
13668 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
13671 if (DECL_DECLARED_INLINE_P (decl)
13672 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
13673 add_AT_flag (subr_die, DW_AT_artificial, 1);
13675 equate_decl_number_to_die (decl, subr_die);
13677 else if (!DECL_EXTERNAL (decl))
13679 HOST_WIDE_INT cfa_fb_offset;
13681 if (!old_die || !get_AT (old_die, DW_AT_inline))
13682 equate_decl_number_to_die (decl, subr_die);
13684 if (!flag_reorder_blocks_and_partition)
13686 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
13687 current_function_funcdef_no);
13688 add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
13689 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13690 current_function_funcdef_no);
13691 add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
13693 add_pubname (decl, subr_die);
13694 add_arange (decl, subr_die);
13696 else
13697 { /* Do nothing for now; maybe need to duplicate die, one for
13698 hot section and one for cold section, then use the hot/cold
13699 section begin/end labels to generate the aranges... */
13701 add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
13702 add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
13703 add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
13704 add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
13706 add_pubname (decl, subr_die);
13707 add_arange (decl, subr_die);
13708 add_arange (decl, subr_die);
13712 #ifdef MIPS_DEBUGGING_INFO
13713 /* Add a reference to the FDE for this routine. */
13714 add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
13715 #endif
13717 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
13719 /* We define the "frame base" as the function's CFA. This is more
13720 convenient for several reasons: (1) It's stable across the prologue
13721 and epilogue, which makes it better than just a frame pointer,
13722 (2) With dwarf3, there exists a one-byte encoding that allows us
13723 to reference the .debug_frame data by proxy, but failing that,
13724 (3) We can at least reuse the code inspection and interpretation
13725 code that determines the CFA position at various points in the
13726 function. */
13727 /* ??? Use some command-line or configury switch to enable the use
13728 of dwarf3 DW_OP_call_frame_cfa. At present there are no dwarf
13729 consumers that understand it; fall back to "pure" dwarf2 and
13730 convert the CFA data into a location list. */
13732 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
13733 if (list->dw_loc_next)
13734 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
13735 else
13736 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
13739 /* Compute a displacement from the "steady-state frame pointer" to
13740 the CFA. The former is what all stack slots and argument slots
13741 will reference in the rtl; the later is what we've told the
13742 debugger about. We'll need to adjust all frame_base references
13743 by this displacement. */
13744 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
13746 if (cfun->static_chain_decl)
13747 add_AT_location_description (subr_die, DW_AT_static_link,
13748 loc_descriptor_from_tree (cfun->static_chain_decl));
13751 /* Now output descriptions of the arguments for this function. This gets
13752 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
13753 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
13754 `...' at the end of the formal parameter list. In order to find out if
13755 there was a trailing ellipsis or not, we must instead look at the type
13756 associated with the FUNCTION_DECL. This will be a node of type
13757 FUNCTION_TYPE. If the chain of type nodes hanging off of this
13758 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
13759 an ellipsis at the end. */
13761 /* In the case where we are describing a mere function declaration, all we
13762 need to do here (and all we *can* do here) is to describe the *types* of
13763 its formal parameters. */
13764 if (debug_info_level <= DINFO_LEVEL_TERSE)
13766 else if (declaration)
13767 gen_formal_types_die (decl, subr_die);
13768 else
13770 /* Generate DIEs to represent all known formal parameters. */
13771 tree arg_decls = DECL_ARGUMENTS (decl);
13772 tree parm;
13774 /* When generating DIEs, generate the unspecified_parameters DIE
13775 instead if we come across the arg "__builtin_va_alist" */
13776 for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
13777 if (TREE_CODE (parm) == PARM_DECL)
13779 if (DECL_NAME (parm)
13780 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
13781 "__builtin_va_alist"))
13782 gen_unspecified_parameters_die (parm, subr_die);
13783 else
13784 gen_decl_die (parm, NULL, subr_die);
13787 /* Decide whether we need an unspecified_parameters DIE at the end.
13788 There are 2 more cases to do this for: 1) the ansi ... declaration -
13789 this is detectable when the end of the arg list is not a
13790 void_type_node 2) an unprototyped function declaration (not a
13791 definition). This just means that we have no info about the
13792 parameters at all. */
13793 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
13794 if (fn_arg_types != NULL)
13796 /* This is the prototyped case, check for.... */
13797 if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
13798 gen_unspecified_parameters_die (decl, subr_die);
13800 else if (DECL_INITIAL (decl) == NULL_TREE)
13801 gen_unspecified_parameters_die (decl, subr_die);
13804 /* Output Dwarf info for all of the stuff within the body of the function
13805 (if it has one - it may be just a declaration). */
13806 outer_scope = DECL_INITIAL (decl);
13808 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
13809 a function. This BLOCK actually represents the outermost binding contour
13810 for the function, i.e. the contour in which the function's formal
13811 parameters and labels get declared. Curiously, it appears that the front
13812 end doesn't actually put the PARM_DECL nodes for the current function onto
13813 the BLOCK_VARS list for this outer scope, but are strung off of the
13814 DECL_ARGUMENTS list for the function instead.
13816 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
13817 the LABEL_DECL nodes for the function however, and we output DWARF info
13818 for those in decls_for_scope. Just within the `outer_scope' there will be
13819 a BLOCK node representing the function's outermost pair of curly braces,
13820 and any blocks used for the base and member initializers of a C++
13821 constructor function. */
13822 if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
13824 /* Emit a DW_TAG_variable DIE for a named return value. */
13825 if (DECL_NAME (DECL_RESULT (decl)))
13826 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
13828 current_function_has_inlines = 0;
13829 decls_for_scope (outer_scope, subr_die, 0);
13831 #if 0 && defined (MIPS_DEBUGGING_INFO)
13832 if (current_function_has_inlines)
13834 add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
13835 if (! comp_unit_has_inlines)
13837 add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
13838 comp_unit_has_inlines = 1;
13841 #endif
13843 /* Add the calling convention attribute if requested. */
13844 add_calling_convention_attribute (subr_die, decl);
13848 /* Returns a hash value for X (which really is a die_struct). */
13850 static hashval_t
13851 common_block_die_table_hash (const void *x)
13853 const_dw_die_ref d = (const_dw_die_ref) x;
13854 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
13857 /* Return nonzero if decl_id and die_parent of die_struct X is the same
13858 as decl_id and die_parent of die_struct Y. */
13860 static int
13861 common_block_die_table_eq (const void *x, const void *y)
13863 const_dw_die_ref d = (const_dw_die_ref) x;
13864 const_dw_die_ref e = (const_dw_die_ref) y;
13865 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
13868 /* Generate a DIE to represent a declared data object.
13869 Either DECL or ORIGIN must be non-null. */
13871 static void
13872 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
13874 HOST_WIDE_INT off;
13875 tree com_decl;
13876 tree decl_or_origin = decl ? decl : origin;
13877 dw_die_ref var_die;
13878 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
13879 dw_die_ref origin_die;
13880 int declaration = (DECL_EXTERNAL (decl_or_origin)
13881 /* If DECL is COMDAT and has not actually been
13882 emitted, we cannot take its address; there
13883 might end up being no definition anywhere in
13884 the program. For example, consider the C++
13885 test case:
13887 template <class T>
13888 struct S { static const int i = 7; };
13890 template <class T>
13891 const int S<T>::i;
13893 int f() { return S<int>::i; }
13895 Here, S<int>::i is not DECL_EXTERNAL, but no
13896 definition is required, so the compiler will
13897 not emit a definition. */
13898 || (TREE_CODE (decl_or_origin) == VAR_DECL
13899 && DECL_COMDAT (decl_or_origin)
13900 && !TREE_ASM_WRITTEN (decl_or_origin))
13901 || class_or_namespace_scope_p (context_die));
13903 if (!origin)
13904 origin = decl_ultimate_origin (decl);
13906 com_decl = fortran_common (decl_or_origin, &off);
13908 /* Symbol in common gets emitted as a child of the common block, in the form
13909 of a data member. */
13910 if (com_decl)
13912 tree field;
13913 dw_die_ref com_die;
13914 dw_loc_descr_ref loc;
13915 die_node com_die_arg;
13917 var_die = lookup_decl_die (decl_or_origin);
13918 if (var_die)
13920 if (get_AT (var_die, DW_AT_location) == NULL)
13922 loc = loc_descriptor_from_tree (com_decl);
13923 if (loc)
13925 if (off)
13927 /* Optimize the common case. */
13928 if (loc->dw_loc_opc == DW_OP_addr
13929 && loc->dw_loc_next == NULL
13930 && GET_CODE (loc->dw_loc_oprnd1.v.val_addr)
13931 == SYMBOL_REF)
13932 loc->dw_loc_oprnd1.v.val_addr
13933 = plus_constant (loc->dw_loc_oprnd1.v.val_addr, off);
13934 else
13935 add_loc_descr (&loc,
13936 new_loc_descr (DW_OP_plus_uconst,
13937 off, 0));
13939 add_AT_loc (var_die, DW_AT_location, loc);
13940 remove_AT (var_die, DW_AT_declaration);
13943 return;
13946 if (common_block_die_table == NULL)
13947 common_block_die_table
13948 = htab_create_ggc (10, common_block_die_table_hash,
13949 common_block_die_table_eq, NULL);
13951 field = TREE_OPERAND (DECL_VALUE_EXPR (decl), 0);
13952 com_die_arg.decl_id = DECL_UID (com_decl);
13953 com_die_arg.die_parent = context_die;
13954 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
13955 loc = loc_descriptor_from_tree (com_decl);
13956 if (com_die == NULL)
13958 const char *cnam
13959 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
13960 void **slot;
13962 com_die = new_die (DW_TAG_common_block, context_die, decl);
13963 add_name_and_src_coords_attributes (com_die, com_decl);
13964 if (loc)
13966 add_AT_loc (com_die, DW_AT_location, loc);
13967 /* Avoid sharing the same loc descriptor between
13968 DW_TAG_common_block and DW_TAG_variable. */
13969 loc = loc_descriptor_from_tree (com_decl);
13971 else if (DECL_EXTERNAL (decl))
13972 add_AT_flag (com_die, DW_AT_declaration, 1);
13973 add_pubname_string (cnam, com_die); /* ??? needed? */
13974 com_die->decl_id = DECL_UID (com_decl);
13975 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
13976 *slot = (void *) com_die;
13978 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
13980 add_AT_loc (com_die, DW_AT_location, loc);
13981 loc = loc_descriptor_from_tree (com_decl);
13982 remove_AT (com_die, DW_AT_declaration);
13984 var_die = new_die (DW_TAG_variable, com_die, decl);
13985 add_name_and_src_coords_attributes (var_die, decl);
13986 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
13987 TREE_THIS_VOLATILE (decl), context_die);
13988 add_AT_flag (var_die, DW_AT_external, 1);
13989 if (loc)
13991 if (off)
13993 /* Optimize the common case. */
13994 if (loc->dw_loc_opc == DW_OP_addr
13995 && loc->dw_loc_next == NULL
13996 && GET_CODE (loc->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
13997 loc->dw_loc_oprnd1.v.val_addr
13998 = plus_constant (loc->dw_loc_oprnd1.v.val_addr, off);
13999 else
14000 add_loc_descr (&loc, new_loc_descr (DW_OP_plus_uconst,
14001 off, 0));
14003 add_AT_loc (var_die, DW_AT_location, loc);
14005 else if (DECL_EXTERNAL (decl))
14006 add_AT_flag (var_die, DW_AT_declaration, 1);
14007 equate_decl_number_to_die (decl, var_die);
14008 return;
14011 /* If the compiler emitted a definition for the DECL declaration
14012 and if we already emitted a DIE for it, don't emit a second
14013 DIE for it again. */
14014 if (old_die
14015 && declaration
14016 && old_die->die_parent == context_die)
14017 return;
14019 /* For static data members, the declaration in the class is supposed
14020 to have DW_TAG_member tag; the specification should still be
14021 DW_TAG_variable referencing the DW_TAG_member DIE. */
14022 if (declaration && class_scope_p (context_die))
14023 var_die = new_die (DW_TAG_member, context_die, decl);
14024 else
14025 var_die = new_die (DW_TAG_variable, context_die, decl);
14027 origin_die = NULL;
14028 if (origin != NULL)
14029 origin_die = add_abstract_origin_attribute (var_die, origin);
14031 /* Loop unrolling can create multiple blocks that refer to the same
14032 static variable, so we must test for the DW_AT_declaration flag.
14034 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
14035 copy decls and set the DECL_ABSTRACT flag on them instead of
14036 sharing them.
14038 ??? Duplicated blocks have been rewritten to use .debug_ranges.
14040 ??? The declare_in_namespace support causes us to get two DIEs for one
14041 variable, both of which are declarations. We want to avoid considering
14042 one to be a specification, so we must test that this DIE is not a
14043 declaration. */
14044 else if (old_die && TREE_STATIC (decl) && ! declaration
14045 && get_AT_flag (old_die, DW_AT_declaration) == 1)
14047 /* This is a definition of a C++ class level static. */
14048 add_AT_specification (var_die, old_die);
14049 if (DECL_NAME (decl))
14051 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
14052 struct dwarf_file_data * file_index = lookup_filename (s.file);
14054 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
14055 add_AT_file (var_die, DW_AT_decl_file, file_index);
14057 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
14058 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
14061 else
14063 tree type = TREE_TYPE (decl);
14065 add_name_and_src_coords_attributes (var_die, decl);
14066 if ((TREE_CODE (decl) == PARM_DECL
14067 || TREE_CODE (decl) == RESULT_DECL)
14068 && DECL_BY_REFERENCE (decl))
14069 add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
14070 else
14071 add_type_attribute (var_die, type, TREE_READONLY (decl),
14072 TREE_THIS_VOLATILE (decl), context_die);
14074 if (TREE_PUBLIC (decl))
14075 add_AT_flag (var_die, DW_AT_external, 1);
14077 if (DECL_ARTIFICIAL (decl))
14078 add_AT_flag (var_die, DW_AT_artificial, 1);
14080 if (TREE_PROTECTED (decl))
14081 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
14082 else if (TREE_PRIVATE (decl))
14083 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
14086 if (declaration)
14087 add_AT_flag (var_die, DW_AT_declaration, 1);
14089 if (decl && (DECL_ABSTRACT (decl) || declaration))
14090 equate_decl_number_to_die (decl, var_die);
14092 if (! declaration
14093 && (! DECL_ABSTRACT (decl_or_origin)
14094 /* Local static vars are shared between all clones/inlines,
14095 so emit DW_AT_location on the abstract DIE if DECL_RTL is
14096 already set. */
14097 || (TREE_CODE (decl_or_origin) == VAR_DECL
14098 && TREE_STATIC (decl_or_origin)
14099 && DECL_RTL_SET_P (decl_or_origin)))
14100 /* When abstract origin already has DW_AT_location attribute, no need
14101 to add it again. */
14102 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
14104 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
14105 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
14106 defer_location (decl_or_origin, var_die);
14107 else
14108 add_location_or_const_value_attribute (var_die,
14109 decl_or_origin,
14110 DW_AT_location);
14111 add_pubname (decl_or_origin, var_die);
14113 else
14114 tree_add_const_value_attribute (var_die, decl_or_origin);
14117 /* Generate a DIE to represent a named constant. */
14119 static void
14120 gen_const_die (tree decl, dw_die_ref context_die)
14122 dw_die_ref const_die;
14123 tree type = TREE_TYPE (decl);
14125 const_die = new_die (DW_TAG_constant, context_die, decl);
14126 add_name_and_src_coords_attributes (const_die, decl);
14127 add_type_attribute (const_die, type, 1, 0, context_die);
14128 if (TREE_PUBLIC (decl))
14129 add_AT_flag (const_die, DW_AT_external, 1);
14130 if (DECL_ARTIFICIAL (decl))
14131 add_AT_flag (const_die, DW_AT_artificial, 1);
14132 tree_add_const_value_attribute (const_die, decl);
14135 /* Generate a DIE to represent a label identifier. */
14137 static void
14138 gen_label_die (tree decl, dw_die_ref context_die)
14140 tree origin = decl_ultimate_origin (decl);
14141 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
14142 rtx insn;
14143 char label[MAX_ARTIFICIAL_LABEL_BYTES];
14145 if (origin != NULL)
14146 add_abstract_origin_attribute (lbl_die, origin);
14147 else
14148 add_name_and_src_coords_attributes (lbl_die, decl);
14150 if (DECL_ABSTRACT (decl))
14151 equate_decl_number_to_die (decl, lbl_die);
14152 else
14154 insn = DECL_RTL_IF_SET (decl);
14156 /* Deleted labels are programmer specified labels which have been
14157 eliminated because of various optimizations. We still emit them
14158 here so that it is possible to put breakpoints on them. */
14159 if (insn
14160 && (LABEL_P (insn)
14161 || ((NOTE_P (insn)
14162 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
14164 /* When optimization is enabled (via -O) some parts of the compiler
14165 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
14166 represent source-level labels which were explicitly declared by
14167 the user. This really shouldn't be happening though, so catch
14168 it if it ever does happen. */
14169 gcc_assert (!INSN_DELETED_P (insn));
14171 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
14172 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
14177 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
14178 attributes to the DIE for a block STMT, to describe where the inlined
14179 function was called from. This is similar to add_src_coords_attributes. */
14181 static inline void
14182 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
14184 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
14186 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
14187 add_AT_unsigned (die, DW_AT_call_line, s.line);
14191 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
14192 Add low_pc and high_pc attributes to the DIE for a block STMT. */
14194 static inline void
14195 add_high_low_attributes (tree stmt, dw_die_ref die)
14197 char label[MAX_ARTIFICIAL_LABEL_BYTES];
14199 if (BLOCK_FRAGMENT_CHAIN (stmt))
14201 tree chain;
14203 if (inlined_function_outer_scope_p (stmt))
14205 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
14206 BLOCK_NUMBER (stmt));
14207 add_AT_lbl_id (die, DW_AT_entry_pc, label);
14210 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
14212 chain = BLOCK_FRAGMENT_CHAIN (stmt);
14215 add_ranges (chain);
14216 chain = BLOCK_FRAGMENT_CHAIN (chain);
14218 while (chain);
14219 add_ranges (NULL);
14221 else
14223 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
14224 BLOCK_NUMBER (stmt));
14225 add_AT_lbl_id (die, DW_AT_low_pc, label);
14226 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
14227 BLOCK_NUMBER (stmt));
14228 add_AT_lbl_id (die, DW_AT_high_pc, label);
14232 /* Generate a DIE for a lexical block. */
14234 static void
14235 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
14237 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
14239 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
14240 add_high_low_attributes (stmt, stmt_die);
14242 decls_for_scope (stmt, stmt_die, depth);
14245 /* Generate a DIE for an inlined subprogram. */
14247 static void
14248 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
14250 tree decl = block_ultimate_origin (stmt);
14252 /* Emit info for the abstract instance first, if we haven't yet. We
14253 must emit this even if the block is abstract, otherwise when we
14254 emit the block below (or elsewhere), we may end up trying to emit
14255 a die whose origin die hasn't been emitted, and crashing. */
14256 dwarf2out_abstract_function (decl);
14258 if (! BLOCK_ABSTRACT (stmt))
14260 dw_die_ref subr_die
14261 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
14263 add_abstract_origin_attribute (subr_die, decl);
14264 if (TREE_ASM_WRITTEN (stmt))
14265 add_high_low_attributes (stmt, subr_die);
14266 add_call_src_coords_attributes (stmt, subr_die);
14268 decls_for_scope (stmt, subr_die, depth);
14269 current_function_has_inlines = 1;
14271 else
14272 /* We may get here if we're the outer block of function A that was
14273 inlined into function B that was inlined into function C. When
14274 generating debugging info for C, dwarf2out_abstract_function(B)
14275 would mark all inlined blocks as abstract, including this one.
14276 So, we wouldn't (and shouldn't) expect labels to be generated
14277 for this one. Instead, just emit debugging info for
14278 declarations within the block. This is particularly important
14279 in the case of initializers of arguments passed from B to us:
14280 if they're statement expressions containing declarations, we
14281 wouldn't generate dies for their abstract variables, and then,
14282 when generating dies for the real variables, we'd die (pun
14283 intended :-) */
14284 gen_lexical_block_die (stmt, context_die, depth);
14287 /* Generate a DIE for a field in a record, or structure. */
14289 static void
14290 gen_field_die (tree decl, dw_die_ref context_die)
14292 dw_die_ref decl_die;
14294 if (TREE_TYPE (decl) == error_mark_node)
14295 return;
14297 decl_die = new_die (DW_TAG_member, context_die, decl);
14298 add_name_and_src_coords_attributes (decl_die, decl);
14299 add_type_attribute (decl_die, member_declared_type (decl),
14300 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
14301 context_die);
14303 if (DECL_BIT_FIELD_TYPE (decl))
14305 add_byte_size_attribute (decl_die, decl);
14306 add_bit_size_attribute (decl_die, decl);
14307 add_bit_offset_attribute (decl_die, decl);
14310 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
14311 add_data_member_location_attribute (decl_die, decl);
14313 if (DECL_ARTIFICIAL (decl))
14314 add_AT_flag (decl_die, DW_AT_artificial, 1);
14316 if (TREE_PROTECTED (decl))
14317 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
14318 else if (TREE_PRIVATE (decl))
14319 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
14321 /* Equate decl number to die, so that we can look up this decl later on. */
14322 equate_decl_number_to_die (decl, decl_die);
14325 #if 0
14326 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
14327 Use modified_type_die instead.
14328 We keep this code here just in case these types of DIEs may be needed to
14329 represent certain things in other languages (e.g. Pascal) someday. */
14331 static void
14332 gen_pointer_type_die (tree type, dw_die_ref context_die)
14334 dw_die_ref ptr_die
14335 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
14337 equate_type_number_to_die (type, ptr_die);
14338 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
14339 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
14342 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
14343 Use modified_type_die instead.
14344 We keep this code here just in case these types of DIEs may be needed to
14345 represent certain things in other languages (e.g. Pascal) someday. */
14347 static void
14348 gen_reference_type_die (tree type, dw_die_ref context_die)
14350 dw_die_ref ref_die
14351 = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
14353 equate_type_number_to_die (type, ref_die);
14354 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
14355 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
14357 #endif
14359 /* Generate a DIE for a pointer to a member type. */
14361 static void
14362 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
14364 dw_die_ref ptr_die
14365 = new_die (DW_TAG_ptr_to_member_type,
14366 scope_die_for (type, context_die), type);
14368 equate_type_number_to_die (type, ptr_die);
14369 add_AT_die_ref (ptr_die, DW_AT_containing_type,
14370 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
14371 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
14374 /* Generate the DIE for the compilation unit. */
14376 static dw_die_ref
14377 gen_compile_unit_die (const char *filename)
14379 dw_die_ref die;
14380 char producer[250];
14381 const char *language_string = lang_hooks.name;
14382 int language;
14384 die = new_die (DW_TAG_compile_unit, NULL, NULL);
14386 if (filename)
14388 add_name_attribute (die, filename);
14389 /* Don't add cwd for <built-in>. */
14390 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
14391 add_comp_dir_attribute (die);
14394 sprintf (producer, "%s %s", language_string, version_string);
14396 #ifdef MIPS_DEBUGGING_INFO
14397 /* The MIPS/SGI compilers place the 'cc' command line options in the producer
14398 string. The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
14399 not appear in the producer string, the debugger reaches the conclusion
14400 that the object file is stripped and has no debugging information.
14401 To get the MIPS/SGI debugger to believe that there is debugging
14402 information in the object file, we add a -g to the producer string. */
14403 if (debug_info_level > DINFO_LEVEL_TERSE)
14404 strcat (producer, " -g");
14405 #endif
14407 add_AT_string (die, DW_AT_producer, producer);
14409 if (strcmp (language_string, "GNU C++") == 0)
14410 language = DW_LANG_C_plus_plus;
14411 else if (strcmp (language_string, "GNU Ada") == 0)
14412 language = DW_LANG_Ada95;
14413 else if (strcmp (language_string, "GNU F77") == 0)
14414 language = DW_LANG_Fortran77;
14415 else if (strcmp (language_string, "GNU Fortran") == 0)
14416 language = DW_LANG_Fortran95;
14417 else if (strcmp (language_string, "GNU Pascal") == 0)
14418 language = DW_LANG_Pascal83;
14419 else if (strcmp (language_string, "GNU Java") == 0)
14420 language = DW_LANG_Java;
14421 else if (strcmp (language_string, "GNU Objective-C") == 0)
14422 language = DW_LANG_ObjC;
14423 else if (strcmp (language_string, "GNU Objective-C++") == 0)
14424 language = DW_LANG_ObjC_plus_plus;
14425 else
14426 language = DW_LANG_C89;
14428 add_AT_unsigned (die, DW_AT_language, language);
14429 return die;
14432 /* Generate the DIE for a base class. */
14434 static void
14435 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
14437 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
14439 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
14440 add_data_member_location_attribute (die, binfo);
14442 if (BINFO_VIRTUAL_P (binfo))
14443 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
14445 if (access == access_public_node)
14446 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14447 else if (access == access_protected_node)
14448 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14451 /* Generate a DIE for a class member. */
14453 static void
14454 gen_member_die (tree type, dw_die_ref context_die)
14456 tree member;
14457 tree binfo = TYPE_BINFO (type);
14458 dw_die_ref child;
14460 /* If this is not an incomplete type, output descriptions of each of its
14461 members. Note that as we output the DIEs necessary to represent the
14462 members of this record or union type, we will also be trying to output
14463 DIEs to represent the *types* of those members. However the `type'
14464 function (above) will specifically avoid generating type DIEs for member
14465 types *within* the list of member DIEs for this (containing) type except
14466 for those types (of members) which are explicitly marked as also being
14467 members of this (containing) type themselves. The g++ front- end can
14468 force any given type to be treated as a member of some other (containing)
14469 type by setting the TYPE_CONTEXT of the given (member) type to point to
14470 the TREE node representing the appropriate (containing) type. */
14472 /* First output info about the base classes. */
14473 if (binfo)
14475 VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
14476 int i;
14477 tree base;
14479 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
14480 gen_inheritance_die (base,
14481 (accesses ? VEC_index (tree, accesses, i)
14482 : access_public_node), context_die);
14485 /* Now output info about the data members and type members. */
14486 for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
14488 /* If we thought we were generating minimal debug info for TYPE
14489 and then changed our minds, some of the member declarations
14490 may have already been defined. Don't define them again, but
14491 do put them in the right order. */
14493 child = lookup_decl_die (member);
14494 if (child)
14495 splice_child_die (context_die, child);
14496 else
14497 gen_decl_die (member, NULL, context_die);
14500 /* Now output info about the function members (if any). */
14501 for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
14503 /* Don't include clones in the member list. */
14504 if (DECL_ABSTRACT_ORIGIN (member))
14505 continue;
14507 child = lookup_decl_die (member);
14508 if (child)
14509 splice_child_die (context_die, child);
14510 else
14511 gen_decl_die (member, NULL, context_die);
14515 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
14516 is set, we pretend that the type was never defined, so we only get the
14517 member DIEs needed by later specification DIEs. */
14519 static void
14520 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
14521 enum debug_info_usage usage)
14523 dw_die_ref type_die = lookup_type_die (type);
14524 dw_die_ref scope_die = 0;
14525 int nested = 0;
14526 int complete = (TYPE_SIZE (type)
14527 && (! TYPE_STUB_DECL (type)
14528 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
14529 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
14530 complete = complete && should_emit_struct_debug (type, usage);
14532 if (type_die && ! complete)
14533 return;
14535 if (TYPE_CONTEXT (type) != NULL_TREE
14536 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
14537 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
14538 nested = 1;
14540 scope_die = scope_die_for (type, context_die);
14542 if (! type_die || (nested && scope_die == comp_unit_die))
14543 /* First occurrence of type or toplevel definition of nested class. */
14545 dw_die_ref old_die = type_die;
14547 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
14548 ? record_type_tag (type) : DW_TAG_union_type,
14549 scope_die, type);
14550 equate_type_number_to_die (type, type_die);
14551 if (old_die)
14552 add_AT_specification (type_die, old_die);
14553 else
14554 add_name_attribute (type_die, type_tag (type));
14556 else
14557 remove_AT (type_die, DW_AT_declaration);
14559 /* If this type has been completed, then give it a byte_size attribute and
14560 then give a list of members. */
14561 if (complete && !ns_decl)
14563 /* Prevent infinite recursion in cases where the type of some member of
14564 this type is expressed in terms of this type itself. */
14565 TREE_ASM_WRITTEN (type) = 1;
14566 add_byte_size_attribute (type_die, type);
14567 if (TYPE_STUB_DECL (type) != NULL_TREE)
14568 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
14570 /* If the first reference to this type was as the return type of an
14571 inline function, then it may not have a parent. Fix this now. */
14572 if (type_die->die_parent == NULL)
14573 add_child_die (scope_die, type_die);
14575 push_decl_scope (type);
14576 gen_member_die (type, type_die);
14577 pop_decl_scope ();
14579 /* GNU extension: Record what type our vtable lives in. */
14580 if (TYPE_VFIELD (type))
14582 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
14584 gen_type_die (vtype, context_die);
14585 add_AT_die_ref (type_die, DW_AT_containing_type,
14586 lookup_type_die (vtype));
14589 else
14591 add_AT_flag (type_die, DW_AT_declaration, 1);
14593 /* We don't need to do this for function-local types. */
14594 if (TYPE_STUB_DECL (type)
14595 && ! decl_function_context (TYPE_STUB_DECL (type)))
14596 VEC_safe_push (tree, gc, incomplete_types, type);
14599 if (get_AT (type_die, DW_AT_name))
14600 add_pubtype (type, type_die);
14603 /* Generate a DIE for a subroutine _type_. */
14605 static void
14606 gen_subroutine_type_die (tree type, dw_die_ref context_die)
14608 tree return_type = TREE_TYPE (type);
14609 dw_die_ref subr_die
14610 = new_die (DW_TAG_subroutine_type,
14611 scope_die_for (type, context_die), type);
14613 equate_type_number_to_die (type, subr_die);
14614 add_prototyped_attribute (subr_die, type);
14615 add_type_attribute (subr_die, return_type, 0, 0, context_die);
14616 gen_formal_types_die (type, subr_die);
14618 if (get_AT (subr_die, DW_AT_name))
14619 add_pubtype (type, subr_die);
14622 /* Generate a DIE for a type definition. */
14624 static void
14625 gen_typedef_die (tree decl, dw_die_ref context_die)
14627 dw_die_ref type_die;
14628 tree origin;
14630 if (TREE_ASM_WRITTEN (decl))
14631 return;
14633 TREE_ASM_WRITTEN (decl) = 1;
14634 type_die = new_die (DW_TAG_typedef, context_die, decl);
14635 origin = decl_ultimate_origin (decl);
14636 if (origin != NULL)
14637 add_abstract_origin_attribute (type_die, origin);
14638 else
14640 tree type;
14642 add_name_and_src_coords_attributes (type_die, decl);
14643 if (DECL_ORIGINAL_TYPE (decl))
14645 type = DECL_ORIGINAL_TYPE (decl);
14647 gcc_assert (type != TREE_TYPE (decl));
14648 equate_type_number_to_die (TREE_TYPE (decl), type_die);
14650 else
14651 type = TREE_TYPE (decl);
14653 add_type_attribute (type_die, type, TREE_READONLY (decl),
14654 TREE_THIS_VOLATILE (decl), context_die);
14657 if (DECL_ABSTRACT (decl))
14658 equate_decl_number_to_die (decl, type_die);
14660 if (get_AT (type_die, DW_AT_name))
14661 add_pubtype (decl, type_die);
14664 /* Generate a type description DIE. */
14666 static void
14667 gen_type_die_with_usage (tree type, dw_die_ref context_die,
14668 enum debug_info_usage usage)
14670 int need_pop;
14671 struct array_descr_info info;
14673 if (type == NULL_TREE || type == error_mark_node)
14674 return;
14676 if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
14677 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
14679 if (TREE_ASM_WRITTEN (type))
14680 return;
14682 /* Prevent broken recursion; we can't hand off to the same type. */
14683 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
14685 TREE_ASM_WRITTEN (type) = 1;
14686 gen_decl_die (TYPE_NAME (type), NULL, context_die);
14687 return;
14690 /* If this is an array type with hidden descriptor, handle it first. */
14691 if (!TREE_ASM_WRITTEN (type)
14692 && lang_hooks.types.get_array_descr_info
14693 && lang_hooks.types.get_array_descr_info (type, &info))
14695 gen_descr_array_type_die (type, &info, context_die);
14696 TREE_ASM_WRITTEN (type) = 1;
14697 return;
14700 /* We are going to output a DIE to represent the unqualified version
14701 of this type (i.e. without any const or volatile qualifiers) so
14702 get the main variant (i.e. the unqualified version) of this type
14703 now. (Vectors are special because the debugging info is in the
14704 cloned type itself). */
14705 if (TREE_CODE (type) != VECTOR_TYPE)
14706 type = type_main_variant (type);
14708 if (TREE_ASM_WRITTEN (type))
14709 return;
14711 switch (TREE_CODE (type))
14713 case ERROR_MARK:
14714 break;
14716 case POINTER_TYPE:
14717 case REFERENCE_TYPE:
14718 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
14719 ensures that the gen_type_die recursion will terminate even if the
14720 type is recursive. Recursive types are possible in Ada. */
14721 /* ??? We could perhaps do this for all types before the switch
14722 statement. */
14723 TREE_ASM_WRITTEN (type) = 1;
14725 /* For these types, all that is required is that we output a DIE (or a
14726 set of DIEs) to represent the "basis" type. */
14727 gen_type_die_with_usage (TREE_TYPE (type), context_die,
14728 DINFO_USAGE_IND_USE);
14729 break;
14731 case OFFSET_TYPE:
14732 /* This code is used for C++ pointer-to-data-member types.
14733 Output a description of the relevant class type. */
14734 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
14735 DINFO_USAGE_IND_USE);
14737 /* Output a description of the type of the object pointed to. */
14738 gen_type_die_with_usage (TREE_TYPE (type), context_die,
14739 DINFO_USAGE_IND_USE);
14741 /* Now output a DIE to represent this pointer-to-data-member type
14742 itself. */
14743 gen_ptr_to_mbr_type_die (type, context_die);
14744 break;
14746 case FUNCTION_TYPE:
14747 /* Force out return type (in case it wasn't forced out already). */
14748 gen_type_die_with_usage (TREE_TYPE (type), context_die,
14749 DINFO_USAGE_DIR_USE);
14750 gen_subroutine_type_die (type, context_die);
14751 break;
14753 case METHOD_TYPE:
14754 /* Force out return type (in case it wasn't forced out already). */
14755 gen_type_die_with_usage (TREE_TYPE (type), context_die,
14756 DINFO_USAGE_DIR_USE);
14757 gen_subroutine_type_die (type, context_die);
14758 break;
14760 case ARRAY_TYPE:
14761 gen_array_type_die (type, context_die);
14762 break;
14764 case VECTOR_TYPE:
14765 gen_array_type_die (type, context_die);
14766 break;
14768 case ENUMERAL_TYPE:
14769 case RECORD_TYPE:
14770 case UNION_TYPE:
14771 case QUAL_UNION_TYPE:
14772 /* If this is a nested type whose containing class hasn't been written
14773 out yet, writing it out will cover this one, too. This does not apply
14774 to instantiations of member class templates; they need to be added to
14775 the containing class as they are generated. FIXME: This hurts the
14776 idea of combining type decls from multiple TUs, since we can't predict
14777 what set of template instantiations we'll get. */
14778 if (TYPE_CONTEXT (type)
14779 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
14780 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
14782 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
14784 if (TREE_ASM_WRITTEN (type))
14785 return;
14787 /* If that failed, attach ourselves to the stub. */
14788 push_decl_scope (TYPE_CONTEXT (type));
14789 context_die = lookup_type_die (TYPE_CONTEXT (type));
14790 need_pop = 1;
14792 else
14794 context_die = declare_in_namespace (type, context_die);
14795 need_pop = 0;
14798 if (TREE_CODE (type) == ENUMERAL_TYPE)
14800 /* This might have been written out by the call to
14801 declare_in_namespace. */
14802 if (!TREE_ASM_WRITTEN (type))
14803 gen_enumeration_type_die (type, context_die);
14805 else
14806 gen_struct_or_union_type_die (type, context_die, usage);
14808 if (need_pop)
14809 pop_decl_scope ();
14811 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
14812 it up if it is ever completed. gen_*_type_die will set it for us
14813 when appropriate. */
14814 return;
14816 case VOID_TYPE:
14817 case INTEGER_TYPE:
14818 case REAL_TYPE:
14819 case FIXED_POINT_TYPE:
14820 case COMPLEX_TYPE:
14821 case BOOLEAN_TYPE:
14822 /* No DIEs needed for fundamental types. */
14823 break;
14825 case LANG_TYPE:
14826 /* No Dwarf representation currently defined. */
14827 break;
14829 default:
14830 gcc_unreachable ();
14833 TREE_ASM_WRITTEN (type) = 1;
14836 static void
14837 gen_type_die (tree type, dw_die_ref context_die)
14839 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
14842 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
14843 things which are local to the given block. */
14845 static void
14846 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
14848 int must_output_die = 0;
14849 bool inlined_func;
14851 /* Ignore blocks that are NULL. */
14852 if (stmt == NULL_TREE)
14853 return;
14855 inlined_func = inlined_function_outer_scope_p (stmt);
14857 /* If the block is one fragment of a non-contiguous block, do not
14858 process the variables, since they will have been done by the
14859 origin block. Do process subblocks. */
14860 if (BLOCK_FRAGMENT_ORIGIN (stmt))
14862 tree sub;
14864 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
14865 gen_block_die (sub, context_die, depth + 1);
14867 return;
14870 /* Determine if we need to output any Dwarf DIEs at all to represent this
14871 block. */
14872 if (inlined_func)
14873 /* The outer scopes for inlinings *must* always be represented. We
14874 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
14875 must_output_die = 1;
14876 else
14878 /* Determine if this block directly contains any "significant"
14879 local declarations which we will need to output DIEs for. */
14880 if (debug_info_level > DINFO_LEVEL_TERSE)
14881 /* We are not in terse mode so *any* local declaration counts
14882 as being a "significant" one. */
14883 must_output_die = ((BLOCK_VARS (stmt) != NULL
14884 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
14885 && (TREE_USED (stmt)
14886 || TREE_ASM_WRITTEN (stmt)
14887 || BLOCK_ABSTRACT (stmt)));
14888 else if ((TREE_USED (stmt)
14889 || TREE_ASM_WRITTEN (stmt)
14890 || BLOCK_ABSTRACT (stmt))
14891 && !dwarf2out_ignore_block (stmt))
14892 must_output_die = 1;
14895 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
14896 DIE for any block which contains no significant local declarations at
14897 all. Rather, in such cases we just call `decls_for_scope' so that any
14898 needed Dwarf info for any sub-blocks will get properly generated. Note
14899 that in terse mode, our definition of what constitutes a "significant"
14900 local declaration gets restricted to include only inlined function
14901 instances and local (nested) function definitions. */
14902 if (must_output_die)
14904 if (inlined_func)
14905 gen_inlined_subroutine_die (stmt, context_die, depth);
14906 else
14907 gen_lexical_block_die (stmt, context_die, depth);
14909 else
14910 decls_for_scope (stmt, context_die, depth);
14913 /* Process variable DECL (or variable with origin ORIGIN) within
14914 block STMT and add it to CONTEXT_DIE. */
14915 static void
14916 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
14918 dw_die_ref die;
14919 tree decl_or_origin = decl ? decl : origin;
14920 tree ultimate_origin = origin ? decl_ultimate_origin (origin) : NULL;
14922 if (ultimate_origin)
14923 origin = ultimate_origin;
14925 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
14926 die = lookup_decl_die (decl_or_origin);
14927 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
14928 && TYPE_DECL_IS_STUB (decl_or_origin))
14929 die = lookup_type_die (TREE_TYPE (decl_or_origin));
14930 else
14931 die = NULL;
14933 if (die != NULL && die->die_parent == NULL)
14934 add_child_die (context_die, die);
14935 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
14936 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
14937 stmt, context_die);
14938 else
14939 gen_decl_die (decl, origin, context_die);
14942 /* Generate all of the decls declared within a given scope and (recursively)
14943 all of its sub-blocks. */
14945 static void
14946 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
14948 tree decl;
14949 unsigned int i;
14950 tree subblocks;
14952 /* Ignore NULL blocks. */
14953 if (stmt == NULL_TREE)
14954 return;
14956 /* Output the DIEs to represent all of the data objects and typedefs
14957 declared directly within this block but not within any nested
14958 sub-blocks. Also, nested function and tag DIEs have been
14959 generated with a parent of NULL; fix that up now. */
14960 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
14961 process_scope_var (stmt, decl, NULL_TREE, context_die);
14962 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
14963 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
14964 context_die);
14966 /* If we're at -g1, we're not interested in subblocks. */
14967 if (debug_info_level <= DINFO_LEVEL_TERSE)
14968 return;
14970 /* Output the DIEs to represent all sub-blocks (and the items declared
14971 therein) of this block. */
14972 for (subblocks = BLOCK_SUBBLOCKS (stmt);
14973 subblocks != NULL;
14974 subblocks = BLOCK_CHAIN (subblocks))
14975 gen_block_die (subblocks, context_die, depth + 1);
14978 /* Is this a typedef we can avoid emitting? */
14980 static inline int
14981 is_redundant_typedef (const_tree decl)
14983 if (TYPE_DECL_IS_STUB (decl))
14984 return 1;
14986 if (DECL_ARTIFICIAL (decl)
14987 && DECL_CONTEXT (decl)
14988 && is_tagged_type (DECL_CONTEXT (decl))
14989 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
14990 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
14991 /* Also ignore the artificial member typedef for the class name. */
14992 return 1;
14994 return 0;
14997 /* Returns the DIE for a context. */
14999 static inline dw_die_ref
15000 get_context_die (tree context)
15002 if (context)
15004 /* Find die that represents this context. */
15005 if (TYPE_P (context))
15006 return force_type_die (context);
15007 else
15008 return force_decl_die (context);
15010 return comp_unit_die;
15013 /* Returns the DIE for decl. A DIE will always be returned. */
15015 static dw_die_ref
15016 force_decl_die (tree decl)
15018 dw_die_ref decl_die;
15019 unsigned saved_external_flag;
15020 tree save_fn = NULL_TREE;
15021 decl_die = lookup_decl_die (decl);
15022 if (!decl_die)
15024 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
15026 decl_die = lookup_decl_die (decl);
15027 if (decl_die)
15028 return decl_die;
15030 switch (TREE_CODE (decl))
15032 case FUNCTION_DECL:
15033 /* Clear current_function_decl, so that gen_subprogram_die thinks
15034 that this is a declaration. At this point, we just want to force
15035 declaration die. */
15036 save_fn = current_function_decl;
15037 current_function_decl = NULL_TREE;
15038 gen_subprogram_die (decl, context_die);
15039 current_function_decl = save_fn;
15040 break;
15042 case VAR_DECL:
15043 /* Set external flag to force declaration die. Restore it after
15044 gen_decl_die() call. */
15045 saved_external_flag = DECL_EXTERNAL (decl);
15046 DECL_EXTERNAL (decl) = 1;
15047 gen_decl_die (decl, NULL, context_die);
15048 DECL_EXTERNAL (decl) = saved_external_flag;
15049 break;
15051 case NAMESPACE_DECL:
15052 dwarf2out_decl (decl);
15053 break;
15055 default:
15056 gcc_unreachable ();
15059 /* We should be able to find the DIE now. */
15060 if (!decl_die)
15061 decl_die = lookup_decl_die (decl);
15062 gcc_assert (decl_die);
15065 return decl_die;
15068 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
15069 always returned. */
15071 static dw_die_ref
15072 force_type_die (tree type)
15074 dw_die_ref type_die;
15076 type_die = lookup_type_die (type);
15077 if (!type_die)
15079 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
15081 type_die = modified_type_die (type, TYPE_READONLY (type),
15082 TYPE_VOLATILE (type), context_die);
15083 gcc_assert (type_die);
15085 return type_die;
15088 /* Force out any required namespaces to be able to output DECL,
15089 and return the new context_die for it, if it's changed. */
15091 static dw_die_ref
15092 setup_namespace_context (tree thing, dw_die_ref context_die)
15094 tree context = (DECL_P (thing)
15095 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
15096 if (context && TREE_CODE (context) == NAMESPACE_DECL)
15097 /* Force out the namespace. */
15098 context_die = force_decl_die (context);
15100 return context_die;
15103 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
15104 type) within its namespace, if appropriate.
15106 For compatibility with older debuggers, namespace DIEs only contain
15107 declarations; all definitions are emitted at CU scope. */
15109 static dw_die_ref
15110 declare_in_namespace (tree thing, dw_die_ref context_die)
15112 dw_die_ref ns_context;
15114 if (debug_info_level <= DINFO_LEVEL_TERSE)
15115 return context_die;
15117 /* If this decl is from an inlined function, then don't try to emit it in its
15118 namespace, as we will get confused. It would have already been emitted
15119 when the abstract instance of the inline function was emitted anyways. */
15120 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
15121 return context_die;
15123 ns_context = setup_namespace_context (thing, context_die);
15125 if (ns_context != context_die)
15127 if (is_fortran ())
15128 return ns_context;
15129 if (DECL_P (thing))
15130 gen_decl_die (thing, NULL, ns_context);
15131 else
15132 gen_type_die (thing, ns_context);
15134 return context_die;
15137 /* Generate a DIE for a namespace or namespace alias. */
15139 static void
15140 gen_namespace_die (tree decl, dw_die_ref context_die)
15142 dw_die_ref namespace_die;
15144 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
15145 they are an alias of. */
15146 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
15148 /* Output a real namespace or module. */
15149 context_die = setup_namespace_context (decl, comp_unit_die);
15150 namespace_die = new_die (is_fortran ()
15151 ? DW_TAG_module : DW_TAG_namespace,
15152 context_die, decl);
15153 /* For Fortran modules defined in different CU don't add src coords. */
15154 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
15155 add_name_attribute (namespace_die, dwarf2_name (decl, 0));
15156 else
15157 add_name_and_src_coords_attributes (namespace_die, decl);
15158 if (DECL_EXTERNAL (decl))
15159 add_AT_flag (namespace_die, DW_AT_declaration, 1);
15160 equate_decl_number_to_die (decl, namespace_die);
15162 else
15164 /* Output a namespace alias. */
15166 /* Force out the namespace we are an alias of, if necessary. */
15167 dw_die_ref origin_die
15168 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
15170 if (DECL_CONTEXT (decl) == NULL_TREE
15171 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
15172 context_die = setup_namespace_context (decl, comp_unit_die);
15173 /* Now create the namespace alias DIE. */
15174 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
15175 add_name_and_src_coords_attributes (namespace_die, decl);
15176 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
15177 equate_decl_number_to_die (decl, namespace_die);
15181 /* Generate Dwarf debug information for a decl described by DECL. */
15183 static void
15184 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
15186 tree decl_or_origin = decl ? decl : origin;
15187 tree class_origin = NULL;
15189 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
15190 return;
15192 switch (TREE_CODE (decl_or_origin))
15194 case ERROR_MARK:
15195 break;
15197 case CONST_DECL:
15198 if (!is_fortran ())
15200 /* The individual enumerators of an enum type get output when we output
15201 the Dwarf representation of the relevant enum type itself. */
15202 break;
15205 /* Emit its type. */
15206 gen_type_die (TREE_TYPE (decl), context_die);
15208 /* And its containing namespace. */
15209 context_die = declare_in_namespace (decl, context_die);
15211 gen_const_die (decl, context_die);
15212 break;
15214 case FUNCTION_DECL:
15215 /* Don't output any DIEs to represent mere function declarations,
15216 unless they are class members or explicit block externs. */
15217 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
15218 && DECL_CONTEXT (decl_or_origin) == NULL_TREE
15219 && (current_function_decl == NULL_TREE
15220 || DECL_ARTIFICIAL (decl_or_origin)))
15221 break;
15223 #if 0
15224 /* FIXME */
15225 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
15226 on local redeclarations of global functions. That seems broken. */
15227 if (current_function_decl != decl)
15228 /* This is only a declaration. */;
15229 #endif
15231 /* If we're emitting a clone, emit info for the abstract instance. */
15232 if (origin || DECL_ORIGIN (decl) != decl)
15233 dwarf2out_abstract_function (origin ? origin : DECL_ABSTRACT_ORIGIN (decl));
15235 /* If we're emitting an out-of-line copy of an inline function,
15236 emit info for the abstract instance and set up to refer to it. */
15237 else if (cgraph_function_possibly_inlined_p (decl)
15238 && ! DECL_ABSTRACT (decl)
15239 && ! class_or_namespace_scope_p (context_die)
15240 /* dwarf2out_abstract_function won't emit a die if this is just
15241 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
15242 that case, because that works only if we have a die. */
15243 && DECL_INITIAL (decl) != NULL_TREE)
15245 dwarf2out_abstract_function (decl);
15246 set_decl_origin_self (decl);
15249 /* Otherwise we're emitting the primary DIE for this decl. */
15250 else if (debug_info_level > DINFO_LEVEL_TERSE)
15252 /* Before we describe the FUNCTION_DECL itself, make sure that we
15253 have described its return type. */
15254 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
15256 /* And its virtual context. */
15257 if (DECL_VINDEX (decl) != NULL_TREE)
15258 gen_type_die (DECL_CONTEXT (decl), context_die);
15260 /* And its containing type. */
15261 if (!origin)
15262 origin = decl_class_context (decl);
15263 if (origin != NULL_TREE)
15264 gen_type_die_for_member (origin, decl, context_die);
15266 /* And its containing namespace. */
15267 context_die = declare_in_namespace (decl, context_die);
15270 /* Now output a DIE to represent the function itself. */
15271 if (decl)
15272 gen_subprogram_die (decl, context_die);
15273 break;
15275 case TYPE_DECL:
15276 /* If we are in terse mode, don't generate any DIEs to represent any
15277 actual typedefs. */
15278 if (debug_info_level <= DINFO_LEVEL_TERSE)
15279 break;
15281 /* In the special case of a TYPE_DECL node representing the declaration
15282 of some type tag, if the given TYPE_DECL is marked as having been
15283 instantiated from some other (original) TYPE_DECL node (e.g. one which
15284 was generated within the original definition of an inline function) we
15285 used to generate a special (abbreviated) DW_TAG_structure_type,
15286 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
15287 should be actually referencing those DIEs, as variable DIEs with that
15288 type would be emitted already in the abstract origin, so it was always
15289 removed during unused type prunning. Don't add anything in this
15290 case. */
15291 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
15292 break;
15294 if (is_redundant_typedef (decl))
15295 gen_type_die (TREE_TYPE (decl), context_die);
15296 else
15297 /* Output a DIE to represent the typedef itself. */
15298 gen_typedef_die (decl, context_die);
15299 break;
15301 case LABEL_DECL:
15302 if (debug_info_level >= DINFO_LEVEL_NORMAL)
15303 gen_label_die (decl, context_die);
15304 break;
15306 case VAR_DECL:
15307 case RESULT_DECL:
15308 /* If we are in terse mode, don't generate any DIEs to represent any
15309 variable declarations or definitions. */
15310 if (debug_info_level <= DINFO_LEVEL_TERSE)
15311 break;
15313 /* Output any DIEs that are needed to specify the type of this data
15314 object. */
15315 if (TREE_CODE (decl_or_origin) == RESULT_DECL
15316 && DECL_BY_REFERENCE (decl_or_origin))
15317 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
15318 else
15319 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
15321 /* And its containing type. */
15322 class_origin = decl_class_context (decl_or_origin);
15323 if (class_origin != NULL_TREE)
15324 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
15326 /* And its containing namespace. */
15327 context_die = declare_in_namespace (decl_or_origin, context_die);
15329 /* Now output the DIE to represent the data object itself. This gets
15330 complicated because of the possibility that the VAR_DECL really
15331 represents an inlined instance of a formal parameter for an inline
15332 function. */
15333 if (!origin)
15334 origin = decl_ultimate_origin (decl);
15335 if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
15336 gen_formal_parameter_die (decl, origin, context_die);
15337 else
15338 gen_variable_die (decl, origin, context_die);
15339 break;
15341 case FIELD_DECL:
15342 /* Ignore the nameless fields that are used to skip bits but handle C++
15343 anonymous unions and structs. */
15344 if (DECL_NAME (decl) != NULL_TREE
15345 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
15346 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
15348 gen_type_die (member_declared_type (decl), context_die);
15349 gen_field_die (decl, context_die);
15351 break;
15353 case PARM_DECL:
15354 if (DECL_BY_REFERENCE (decl_or_origin))
15355 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
15356 else
15357 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
15358 gen_formal_parameter_die (decl, origin, context_die);
15359 break;
15361 case NAMESPACE_DECL:
15362 case IMPORTED_DECL:
15363 gen_namespace_die (decl, context_die);
15364 break;
15366 default:
15367 /* Probably some frontend-internal decl. Assume we don't care. */
15368 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
15369 break;
15373 /* Output debug information for global decl DECL. Called from toplev.c after
15374 compilation proper has finished. */
15376 static void
15377 dwarf2out_global_decl (tree decl)
15379 /* Output DWARF2 information for file-scope tentative data object
15380 declarations, file-scope (extern) function declarations (which
15381 had no corresponding body) and file-scope tagged type declarations
15382 and definitions which have not yet been forced out. */
15383 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
15384 dwarf2out_decl (decl);
15387 /* Output debug information for type decl DECL. Called from toplev.c
15388 and from language front ends (to record built-in types). */
15389 static void
15390 dwarf2out_type_decl (tree decl, int local)
15392 if (!local)
15393 dwarf2out_decl (decl);
15396 /* Output debug information for imported module or decl DECL.
15397 NAME is non-NULL name in the lexical block if the decl has been renamed.
15398 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
15399 that DECL belongs to.
15400 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
15401 static void
15402 dwarf2out_imported_module_or_decl_1 (tree decl,
15403 tree name,
15404 tree lexical_block,
15405 dw_die_ref lexical_block_die)
15407 expanded_location xloc;
15408 dw_die_ref imported_die = NULL;
15409 dw_die_ref at_import_die;
15411 if (TREE_CODE (decl) == IMPORTED_DECL)
15413 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
15414 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
15415 gcc_assert (decl);
15417 else
15418 xloc = expand_location (input_location);
15420 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
15422 if (is_base_type (TREE_TYPE (decl)))
15423 at_import_die = base_type_die (TREE_TYPE (decl));
15424 else
15425 at_import_die = force_type_die (TREE_TYPE (decl));
15426 /* For namespace N { typedef void T; } using N::T; base_type_die
15427 returns NULL, but DW_TAG_imported_declaration requires
15428 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
15429 if (!at_import_die)
15431 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
15432 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
15433 at_import_die = lookup_type_die (TREE_TYPE (decl));
15434 gcc_assert (at_import_die);
15437 else
15439 at_import_die = lookup_decl_die (decl);
15440 if (!at_import_die)
15442 /* If we're trying to avoid duplicate debug info, we may not have
15443 emitted the member decl for this field. Emit it now. */
15444 if (TREE_CODE (decl) == FIELD_DECL)
15446 tree type = DECL_CONTEXT (decl);
15448 if (TYPE_CONTEXT (type)
15449 && TYPE_P (TYPE_CONTEXT (type))
15450 && !should_emit_struct_debug (TYPE_CONTEXT (type),
15451 DINFO_USAGE_DIR_USE))
15452 return;
15453 gen_type_die_for_member (type, decl,
15454 get_context_die (TYPE_CONTEXT (type)));
15456 at_import_die = force_decl_die (decl);
15460 if (TREE_CODE (decl) == NAMESPACE_DECL)
15461 imported_die = new_die (DW_TAG_imported_module,
15462 lexical_block_die,
15463 lexical_block);
15464 else
15465 imported_die = new_die (DW_TAG_imported_declaration,
15466 lexical_block_die,
15467 lexical_block);
15469 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
15470 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
15471 if (name)
15472 add_AT_string (imported_die, DW_AT_name,
15473 IDENTIFIER_POINTER (name));
15474 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
15477 /* Output debug information for imported module or decl DECL.
15478 NAME is non-NULL name in context if the decl has been renamed.
15479 CHILD is true if decl is one of the renamed decls as part of
15480 importing whole module. */
15482 static void
15483 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
15484 bool child)
15486 /* dw_die_ref at_import_die; */
15487 dw_die_ref scope_die;
15489 if (debug_info_level <= DINFO_LEVEL_TERSE)
15490 return;
15492 gcc_assert (decl);
15494 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
15495 We need decl DIE for reference and scope die. First, get DIE for the decl
15496 itself. */
15498 /* Get the scope die for decl context. Use comp_unit_die for global module
15499 or decl. If die is not found for non globals, force new die. */
15500 if (context
15501 && TYPE_P (context)
15502 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
15503 return;
15504 scope_die = get_context_die (context);
15506 if (child)
15508 gcc_assert (scope_die->die_child);
15509 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
15510 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
15511 scope_die = scope_die->die_child;
15514 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
15515 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
15519 /* Write the debugging output for DECL. */
15521 void
15522 dwarf2out_decl (tree decl)
15524 dw_die_ref context_die = comp_unit_die;
15526 switch (TREE_CODE (decl))
15528 case ERROR_MARK:
15529 return;
15531 case FUNCTION_DECL:
15532 /* What we would really like to do here is to filter out all mere
15533 file-scope declarations of file-scope functions which are never
15534 referenced later within this translation unit (and keep all of ones
15535 that *are* referenced later on) but we aren't clairvoyant, so we have
15536 no idea which functions will be referenced in the future (i.e. later
15537 on within the current translation unit). So here we just ignore all
15538 file-scope function declarations which are not also definitions. If
15539 and when the debugger needs to know something about these functions,
15540 it will have to hunt around and find the DWARF information associated
15541 with the definition of the function.
15543 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
15544 nodes represent definitions and which ones represent mere
15545 declarations. We have to check DECL_INITIAL instead. That's because
15546 the C front-end supports some weird semantics for "extern inline"
15547 function definitions. These can get inlined within the current
15548 translation unit (and thus, we need to generate Dwarf info for their
15549 abstract instances so that the Dwarf info for the concrete inlined
15550 instances can have something to refer to) but the compiler never
15551 generates any out-of-lines instances of such things (despite the fact
15552 that they *are* definitions).
15554 The important point is that the C front-end marks these "extern
15555 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
15556 them anyway. Note that the C++ front-end also plays some similar games
15557 for inline function definitions appearing within include files which
15558 also contain `#pragma interface' pragmas. */
15559 if (DECL_INITIAL (decl) == NULL_TREE)
15560 return;
15562 /* If we're a nested function, initially use a parent of NULL; if we're
15563 a plain function, this will be fixed up in decls_for_scope. If
15564 we're a method, it will be ignored, since we already have a DIE. */
15565 if (decl_function_context (decl)
15566 /* But if we're in terse mode, we don't care about scope. */
15567 && debug_info_level > DINFO_LEVEL_TERSE)
15568 context_die = NULL;
15569 break;
15571 case VAR_DECL:
15572 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
15573 declaration and if the declaration was never even referenced from
15574 within this entire compilation unit. We suppress these DIEs in
15575 order to save space in the .debug section (by eliminating entries
15576 which are probably useless). Note that we must not suppress
15577 block-local extern declarations (whether used or not) because that
15578 would screw-up the debugger's name lookup mechanism and cause it to
15579 miss things which really ought to be in scope at a given point. */
15580 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
15581 return;
15583 /* For local statics lookup proper context die. */
15584 if (TREE_STATIC (decl) && decl_function_context (decl))
15585 context_die = lookup_decl_die (DECL_CONTEXT (decl));
15587 /* If we are in terse mode, don't generate any DIEs to represent any
15588 variable declarations or definitions. */
15589 if (debug_info_level <= DINFO_LEVEL_TERSE)
15590 return;
15591 break;
15593 case CONST_DECL:
15594 if (debug_info_level <= DINFO_LEVEL_TERSE)
15595 return;
15596 if (!is_fortran ())
15597 return;
15598 if (TREE_STATIC (decl) && decl_function_context (decl))
15599 context_die = lookup_decl_die (DECL_CONTEXT (decl));
15600 break;
15602 case NAMESPACE_DECL:
15603 case IMPORTED_DECL:
15604 if (debug_info_level <= DINFO_LEVEL_TERSE)
15605 return;
15606 if (lookup_decl_die (decl) != NULL)
15607 return;
15608 break;
15610 case TYPE_DECL:
15611 /* Don't emit stubs for types unless they are needed by other DIEs. */
15612 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
15613 return;
15615 /* Don't bother trying to generate any DIEs to represent any of the
15616 normal built-in types for the language we are compiling. */
15617 if (DECL_IS_BUILTIN (decl))
15619 /* OK, we need to generate one for `bool' so GDB knows what type
15620 comparisons have. */
15621 if (is_cxx ()
15622 && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
15623 && ! DECL_IGNORED_P (decl))
15624 modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
15626 return;
15629 /* If we are in terse mode, don't generate any DIEs for types. */
15630 if (debug_info_level <= DINFO_LEVEL_TERSE)
15631 return;
15633 /* If we're a function-scope tag, initially use a parent of NULL;
15634 this will be fixed up in decls_for_scope. */
15635 if (decl_function_context (decl))
15636 context_die = NULL;
15638 break;
15640 default:
15641 return;
15644 gen_decl_die (decl, NULL, context_die);
15647 /* Output a marker (i.e. a label) for the beginning of the generated code for
15648 a lexical block. */
15650 static void
15651 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
15652 unsigned int blocknum)
15654 switch_to_section (current_function_section ());
15655 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
15658 /* Output a marker (i.e. a label) for the end of the generated code for a
15659 lexical block. */
15661 static void
15662 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
15664 switch_to_section (current_function_section ());
15665 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
15668 /* Returns nonzero if it is appropriate not to emit any debugging
15669 information for BLOCK, because it doesn't contain any instructions.
15671 Don't allow this for blocks with nested functions or local classes
15672 as we would end up with orphans, and in the presence of scheduling
15673 we may end up calling them anyway. */
15675 static bool
15676 dwarf2out_ignore_block (const_tree block)
15678 tree decl;
15679 unsigned int i;
15681 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
15682 if (TREE_CODE (decl) == FUNCTION_DECL
15683 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
15684 return 0;
15685 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
15687 decl = BLOCK_NONLOCALIZED_VAR (block, i);
15688 if (TREE_CODE (decl) == FUNCTION_DECL
15689 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
15690 return 0;
15693 return 1;
15696 /* Hash table routines for file_hash. */
15698 static int
15699 file_table_eq (const void *p1_p, const void *p2_p)
15701 const struct dwarf_file_data *const p1 =
15702 (const struct dwarf_file_data *) p1_p;
15703 const char *const p2 = (const char *) p2_p;
15704 return strcmp (p1->filename, p2) == 0;
15707 static hashval_t
15708 file_table_hash (const void *p_p)
15710 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
15711 return htab_hash_string (p->filename);
15714 /* Lookup FILE_NAME (in the list of filenames that we know about here in
15715 dwarf2out.c) and return its "index". The index of each (known) filename is
15716 just a unique number which is associated with only that one filename. We
15717 need such numbers for the sake of generating labels (in the .debug_sfnames
15718 section) and references to those files numbers (in the .debug_srcinfo
15719 and.debug_macinfo sections). If the filename given as an argument is not
15720 found in our current list, add it to the list and assign it the next
15721 available unique index number. In order to speed up searches, we remember
15722 the index of the filename was looked up last. This handles the majority of
15723 all searches. */
15725 static struct dwarf_file_data *
15726 lookup_filename (const char *file_name)
15728 void ** slot;
15729 struct dwarf_file_data * created;
15731 /* Check to see if the file name that was searched on the previous
15732 call matches this file name. If so, return the index. */
15733 if (file_table_last_lookup
15734 && (file_name == file_table_last_lookup->filename
15735 || strcmp (file_table_last_lookup->filename, file_name) == 0))
15736 return file_table_last_lookup;
15738 /* Didn't match the previous lookup, search the table. */
15739 slot = htab_find_slot_with_hash (file_table, file_name,
15740 htab_hash_string (file_name), INSERT);
15741 if (*slot)
15742 return (struct dwarf_file_data *) *slot;
15744 created = GGC_NEW (struct dwarf_file_data);
15745 created->filename = file_name;
15746 created->emitted_number = 0;
15747 *slot = created;
15748 return created;
15751 /* If the assembler will construct the file table, then translate the compiler
15752 internal file table number into the assembler file table number, and emit
15753 a .file directive if we haven't already emitted one yet. The file table
15754 numbers are different because we prune debug info for unused variables and
15755 types, which may include filenames. */
15757 static int
15758 maybe_emit_file (struct dwarf_file_data * fd)
15760 if (! fd->emitted_number)
15762 if (last_emitted_file)
15763 fd->emitted_number = last_emitted_file->emitted_number + 1;
15764 else
15765 fd->emitted_number = 1;
15766 last_emitted_file = fd;
15768 if (DWARF2_ASM_LINE_DEBUG_INFO)
15770 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
15771 output_quoted_string (asm_out_file,
15772 remap_debug_filename (fd->filename));
15773 fputc ('\n', asm_out_file);
15777 return fd->emitted_number;
15780 /* Replace DW_AT_name for the decl with name. */
15782 static void
15783 dwarf2out_set_name (tree decl, tree name)
15785 dw_die_ref die;
15786 dw_attr_ref attr;
15788 die = TYPE_SYMTAB_DIE (decl);
15789 if (!die)
15790 return;
15792 attr = get_AT (die, DW_AT_name);
15793 if (attr)
15795 struct indirect_string_node *node;
15797 node = find_AT_string (dwarf2_name (name, 0));
15798 /* replace the string. */
15799 attr->dw_attr_val.v.val_str = node;
15802 else
15803 add_name_attribute (die, dwarf2_name (name, 0));
15805 /* Called by the final INSN scan whenever we see a var location. We
15806 use it to drop labels in the right places, and throw the location in
15807 our lookup table. */
15809 static void
15810 dwarf2out_var_location (rtx loc_note)
15812 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
15813 struct var_loc_node *newloc;
15814 rtx prev_insn;
15815 static rtx last_insn;
15816 static const char *last_label;
15817 tree decl;
15819 if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
15820 return;
15821 prev_insn = PREV_INSN (loc_note);
15823 newloc = GGC_CNEW (struct var_loc_node);
15824 /* If the insn we processed last time is the previous insn
15825 and it is also a var location note, use the label we emitted
15826 last time. */
15827 if (last_insn != NULL_RTX
15828 && last_insn == prev_insn
15829 && NOTE_P (prev_insn)
15830 && NOTE_KIND (prev_insn) == NOTE_INSN_VAR_LOCATION)
15832 newloc->label = last_label;
15834 else
15836 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
15837 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
15838 loclabel_num++;
15839 newloc->label = ggc_strdup (loclabel);
15841 newloc->var_loc_note = loc_note;
15842 newloc->next = NULL;
15844 if (cfun && in_cold_section_p)
15845 newloc->section_label = crtl->subsections.cold_section_label;
15846 else
15847 newloc->section_label = text_section_label;
15849 last_insn = loc_note;
15850 last_label = newloc->label;
15851 decl = NOTE_VAR_LOCATION_DECL (loc_note);
15852 add_var_loc_to_decl (decl, newloc);
15855 /* We need to reset the locations at the beginning of each
15856 function. We can't do this in the end_function hook, because the
15857 declarations that use the locations won't have been output when
15858 that hook is called. Also compute have_multiple_function_sections here. */
15860 static void
15861 dwarf2out_begin_function (tree fun)
15863 htab_empty (decl_loc_table);
15865 if (function_section (fun) != text_section)
15866 have_multiple_function_sections = true;
15868 dwarf2out_note_section_used ();
15871 /* Output a label to mark the beginning of a source code line entry
15872 and record information relating to this source line, in
15873 'line_info_table' for later output of the .debug_line section. */
15875 static void
15876 dwarf2out_source_line (unsigned int line, const char *filename)
15878 if (debug_info_level >= DINFO_LEVEL_NORMAL
15879 && line != 0)
15881 int file_num = maybe_emit_file (lookup_filename (filename));
15883 switch_to_section (current_function_section ());
15885 /* If requested, emit something human-readable. */
15886 if (flag_debug_asm)
15887 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
15888 filename, line);
15890 if (DWARF2_ASM_LINE_DEBUG_INFO)
15892 /* Emit the .loc directive understood by GNU as. */
15893 fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
15895 /* Indicate that line number info exists. */
15896 line_info_table_in_use++;
15898 else if (function_section (current_function_decl) != text_section)
15900 dw_separate_line_info_ref line_info;
15901 targetm.asm_out.internal_label (asm_out_file,
15902 SEPARATE_LINE_CODE_LABEL,
15903 separate_line_info_table_in_use);
15905 /* Expand the line info table if necessary. */
15906 if (separate_line_info_table_in_use
15907 == separate_line_info_table_allocated)
15909 separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
15910 separate_line_info_table
15911 = GGC_RESIZEVEC (dw_separate_line_info_entry,
15912 separate_line_info_table,
15913 separate_line_info_table_allocated);
15914 memset (separate_line_info_table
15915 + separate_line_info_table_in_use,
15917 (LINE_INFO_TABLE_INCREMENT
15918 * sizeof (dw_separate_line_info_entry)));
15921 /* Add the new entry at the end of the line_info_table. */
15922 line_info
15923 = &separate_line_info_table[separate_line_info_table_in_use++];
15924 line_info->dw_file_num = file_num;
15925 line_info->dw_line_num = line;
15926 line_info->function = current_function_funcdef_no;
15928 else
15930 dw_line_info_ref line_info;
15932 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
15933 line_info_table_in_use);
15935 /* Expand the line info table if necessary. */
15936 if (line_info_table_in_use == line_info_table_allocated)
15938 line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
15939 line_info_table
15940 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
15941 line_info_table_allocated);
15942 memset (line_info_table + line_info_table_in_use, 0,
15943 LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
15946 /* Add the new entry at the end of the line_info_table. */
15947 line_info = &line_info_table[line_info_table_in_use++];
15948 line_info->dw_file_num = file_num;
15949 line_info->dw_line_num = line;
15954 /* Record the beginning of a new source file. */
15956 static void
15957 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
15959 if (flag_eliminate_dwarf2_dups)
15961 /* Record the beginning of the file for break_out_includes. */
15962 dw_die_ref bincl_die;
15964 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
15965 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
15968 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15970 int file_num = maybe_emit_file (lookup_filename (filename));
15972 switch_to_section (debug_macinfo_section);
15973 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
15974 dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
15975 lineno);
15977 dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
15981 /* Record the end of a source file. */
15983 static void
15984 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
15986 if (flag_eliminate_dwarf2_dups)
15987 /* Record the end of the file for break_out_includes. */
15988 new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
15990 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15992 switch_to_section (debug_macinfo_section);
15993 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
15997 /* Called from debug_define in toplev.c. The `buffer' parameter contains
15998 the tail part of the directive line, i.e. the part which is past the
15999 initial whitespace, #, whitespace, directive-name, whitespace part. */
16001 static void
16002 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
16003 const char *buffer ATTRIBUTE_UNUSED)
16005 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16007 switch_to_section (debug_macinfo_section);
16008 dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
16009 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
16010 dw2_asm_output_nstring (buffer, -1, "The macro");
16014 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
16015 the tail part of the directive line, i.e. the part which is past the
16016 initial whitespace, #, whitespace, directive-name, whitespace part. */
16018 static void
16019 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
16020 const char *buffer ATTRIBUTE_UNUSED)
16022 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16024 switch_to_section (debug_macinfo_section);
16025 dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
16026 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
16027 dw2_asm_output_nstring (buffer, -1, "The macro");
16031 /* Set up for Dwarf output at the start of compilation. */
16033 static void
16034 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
16036 /* Allocate the file_table. */
16037 file_table = htab_create_ggc (50, file_table_hash,
16038 file_table_eq, NULL);
16040 /* Allocate the decl_die_table. */
16041 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
16042 decl_die_table_eq, NULL);
16044 /* Allocate the decl_loc_table. */
16045 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
16046 decl_loc_table_eq, NULL);
16048 /* Allocate the initial hunk of the decl_scope_table. */
16049 decl_scope_table = VEC_alloc (tree, gc, 256);
16051 /* Allocate the initial hunk of the abbrev_die_table. */
16052 abbrev_die_table = GGC_CNEWVEC (dw_die_ref, ABBREV_DIE_TABLE_INCREMENT);
16053 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
16054 /* Zero-th entry is allocated, but unused. */
16055 abbrev_die_table_in_use = 1;
16057 /* Allocate the initial hunk of the line_info_table. */
16058 line_info_table = GGC_CNEWVEC (dw_line_info_entry, LINE_INFO_TABLE_INCREMENT);
16059 line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
16061 /* Zero-th entry is allocated, but unused. */
16062 line_info_table_in_use = 1;
16064 /* Allocate the pubtypes and pubnames vectors. */
16065 pubname_table = VEC_alloc (pubname_entry, gc, 32);
16066 pubtype_table = VEC_alloc (pubname_entry, gc, 32);
16068 /* Generate the initial DIE for the .debug section. Note that the (string)
16069 value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
16070 will (typically) be a relative pathname and that this pathname should be
16071 taken as being relative to the directory from which the compiler was
16072 invoked when the given (base) source file was compiled. We will fill
16073 in this value in dwarf2out_finish. */
16074 comp_unit_die = gen_compile_unit_die (NULL);
16076 incomplete_types = VEC_alloc (tree, gc, 64);
16078 used_rtx_array = VEC_alloc (rtx, gc, 32);
16080 debug_info_section = get_section (DEBUG_INFO_SECTION,
16081 SECTION_DEBUG, NULL);
16082 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
16083 SECTION_DEBUG, NULL);
16084 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
16085 SECTION_DEBUG, NULL);
16086 debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
16087 SECTION_DEBUG, NULL);
16088 debug_line_section = get_section (DEBUG_LINE_SECTION,
16089 SECTION_DEBUG, NULL);
16090 debug_loc_section = get_section (DEBUG_LOC_SECTION,
16091 SECTION_DEBUG, NULL);
16092 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
16093 SECTION_DEBUG, NULL);
16094 #ifdef DEBUG_PUBTYPES_SECTION
16095 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
16096 SECTION_DEBUG, NULL);
16097 #endif
16098 debug_str_section = get_section (DEBUG_STR_SECTION,
16099 DEBUG_STR_SECTION_FLAGS, NULL);
16100 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
16101 SECTION_DEBUG, NULL);
16102 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
16103 SECTION_DEBUG, NULL);
16105 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
16106 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
16107 DEBUG_ABBREV_SECTION_LABEL, 0);
16108 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
16109 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
16110 COLD_TEXT_SECTION_LABEL, 0);
16111 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
16113 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
16114 DEBUG_INFO_SECTION_LABEL, 0);
16115 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
16116 DEBUG_LINE_SECTION_LABEL, 0);
16117 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
16118 DEBUG_RANGES_SECTION_LABEL, 0);
16119 switch_to_section (debug_abbrev_section);
16120 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
16121 switch_to_section (debug_info_section);
16122 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
16123 switch_to_section (debug_line_section);
16124 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
16126 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16128 switch_to_section (debug_macinfo_section);
16129 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
16130 DEBUG_MACINFO_SECTION_LABEL, 0);
16131 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
16134 switch_to_section (text_section);
16135 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
16136 if (flag_reorder_blocks_and_partition)
16138 cold_text_section = unlikely_text_section ();
16139 switch_to_section (cold_text_section);
16140 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
16144 /* A helper function for dwarf2out_finish called through
16145 ht_forall. Emit one queued .debug_str string. */
16147 static int
16148 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
16150 struct indirect_string_node *node = (struct indirect_string_node *) *h;
16152 if (node->form == DW_FORM_strp)
16154 switch_to_section (debug_str_section);
16155 ASM_OUTPUT_LABEL (asm_out_file, node->label);
16156 assemble_string (node->str, strlen (node->str) + 1);
16159 return 1;
16162 #if ENABLE_ASSERT_CHECKING
16163 /* Verify that all marks are clear. */
16165 static void
16166 verify_marks_clear (dw_die_ref die)
16168 dw_die_ref c;
16170 gcc_assert (! die->die_mark);
16171 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
16173 #endif /* ENABLE_ASSERT_CHECKING */
16175 /* Clear the marks for a die and its children.
16176 Be cool if the mark isn't set. */
16178 static void
16179 prune_unmark_dies (dw_die_ref die)
16181 dw_die_ref c;
16183 if (die->die_mark)
16184 die->die_mark = 0;
16185 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
16188 /* Given DIE that we're marking as used, find any other dies
16189 it references as attributes and mark them as used. */
16191 static void
16192 prune_unused_types_walk_attribs (dw_die_ref die)
16194 dw_attr_ref a;
16195 unsigned ix;
16197 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
16199 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
16201 /* A reference to another DIE.
16202 Make sure that it will get emitted. */
16203 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
16205 /* Set the string's refcount to 0 so that prune_unused_types_mark
16206 accounts properly for it. */
16207 if (AT_class (a) == dw_val_class_str)
16208 a->dw_attr_val.v.val_str->refcount = 0;
16213 /* Mark DIE as being used. If DOKIDS is true, then walk down
16214 to DIE's children. */
16216 static void
16217 prune_unused_types_mark (dw_die_ref die, int dokids)
16219 dw_die_ref c;
16221 if (die->die_mark == 0)
16223 /* We haven't done this node yet. Mark it as used. */
16224 die->die_mark = 1;
16226 /* We also have to mark its parents as used.
16227 (But we don't want to mark our parents' kids due to this.) */
16228 if (die->die_parent)
16229 prune_unused_types_mark (die->die_parent, 0);
16231 /* Mark any referenced nodes. */
16232 prune_unused_types_walk_attribs (die);
16234 /* If this node is a specification,
16235 also mark the definition, if it exists. */
16236 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
16237 prune_unused_types_mark (die->die_definition, 1);
16240 if (dokids && die->die_mark != 2)
16242 /* We need to walk the children, but haven't done so yet.
16243 Remember that we've walked the kids. */
16244 die->die_mark = 2;
16246 /* If this is an array type, we need to make sure our
16247 kids get marked, even if they're types. */
16248 if (die->die_tag == DW_TAG_array_type)
16249 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
16250 else
16251 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
16255 /* For local classes, look if any static member functions were emitted
16256 and if so, mark them. */
16258 static void
16259 prune_unused_types_walk_local_classes (dw_die_ref die)
16261 dw_die_ref c;
16263 if (die->die_mark == 2)
16264 return;
16266 switch (die->die_tag)
16268 case DW_TAG_structure_type:
16269 case DW_TAG_union_type:
16270 case DW_TAG_class_type:
16271 break;
16273 case DW_TAG_subprogram:
16274 if (!get_AT_flag (die, DW_AT_declaration)
16275 || die->die_definition != NULL)
16276 prune_unused_types_mark (die, 1);
16277 return;
16279 default:
16280 return;
16283 /* Mark children. */
16284 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
16287 /* Walk the tree DIE and mark types that we actually use. */
16289 static void
16290 prune_unused_types_walk (dw_die_ref die)
16292 dw_die_ref c;
16294 /* Don't do anything if this node is already marked and
16295 children have been marked as well. */
16296 if (die->die_mark == 2)
16297 return;
16299 switch (die->die_tag)
16301 case DW_TAG_structure_type:
16302 case DW_TAG_union_type:
16303 case DW_TAG_class_type:
16304 if (die->die_perennial_p)
16305 break;
16307 for (c = die->die_parent; c; c = c->die_parent)
16308 if (c->die_tag == DW_TAG_subprogram)
16309 break;
16311 /* Finding used static member functions inside of classes
16312 is needed just for local classes, because for other classes
16313 static member function DIEs with DW_AT_specification
16314 are emitted outside of the DW_TAG_*_type. If we ever change
16315 it, we'd need to call this even for non-local classes. */
16316 if (c)
16317 prune_unused_types_walk_local_classes (die);
16319 /* It's a type node --- don't mark it. */
16320 return;
16322 case DW_TAG_const_type:
16323 case DW_TAG_packed_type:
16324 case DW_TAG_pointer_type:
16325 case DW_TAG_reference_type:
16326 case DW_TAG_volatile_type:
16327 case DW_TAG_typedef:
16328 case DW_TAG_array_type:
16329 case DW_TAG_interface_type:
16330 case DW_TAG_friend:
16331 case DW_TAG_variant_part:
16332 case DW_TAG_enumeration_type:
16333 case DW_TAG_subroutine_type:
16334 case DW_TAG_string_type:
16335 case DW_TAG_set_type:
16336 case DW_TAG_subrange_type:
16337 case DW_TAG_ptr_to_member_type:
16338 case DW_TAG_file_type:
16339 if (die->die_perennial_p)
16340 break;
16342 /* It's a type node --- don't mark it. */
16343 return;
16345 default:
16346 /* Mark everything else. */
16347 break;
16350 if (die->die_mark == 0)
16352 die->die_mark = 1;
16354 /* Now, mark any dies referenced from here. */
16355 prune_unused_types_walk_attribs (die);
16358 die->die_mark = 2;
16360 /* Mark children. */
16361 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
16364 /* Increment the string counts on strings referred to from DIE's
16365 attributes. */
16367 static void
16368 prune_unused_types_update_strings (dw_die_ref die)
16370 dw_attr_ref a;
16371 unsigned ix;
16373 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
16374 if (AT_class (a) == dw_val_class_str)
16376 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
16377 s->refcount++;
16378 /* Avoid unnecessarily putting strings that are used less than
16379 twice in the hash table. */
16380 if (s->refcount
16381 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
16383 void ** slot;
16384 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
16385 htab_hash_string (s->str),
16386 INSERT);
16387 gcc_assert (*slot == NULL);
16388 *slot = s;
16393 /* Remove from the tree DIE any dies that aren't marked. */
16395 static void
16396 prune_unused_types_prune (dw_die_ref die)
16398 dw_die_ref c;
16400 gcc_assert (die->die_mark);
16401 prune_unused_types_update_strings (die);
16403 if (! die->die_child)
16404 return;
16406 c = die->die_child;
16407 do {
16408 dw_die_ref prev = c;
16409 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
16410 if (c == die->die_child)
16412 /* No marked children between 'prev' and the end of the list. */
16413 if (prev == c)
16414 /* No marked children at all. */
16415 die->die_child = NULL;
16416 else
16418 prev->die_sib = c->die_sib;
16419 die->die_child = prev;
16421 return;
16424 if (c != prev->die_sib)
16425 prev->die_sib = c;
16426 prune_unused_types_prune (c);
16427 } while (c != die->die_child);
16431 /* Remove dies representing declarations that we never use. */
16433 static void
16434 prune_unused_types (void)
16436 unsigned int i;
16437 limbo_die_node *node;
16438 pubname_ref pub;
16440 #if ENABLE_ASSERT_CHECKING
16441 /* All the marks should already be clear. */
16442 verify_marks_clear (comp_unit_die);
16443 for (node = limbo_die_list; node; node = node->next)
16444 verify_marks_clear (node->die);
16445 #endif /* ENABLE_ASSERT_CHECKING */
16447 /* Set the mark on nodes that are actually used. */
16448 prune_unused_types_walk (comp_unit_die);
16449 for (node = limbo_die_list; node; node = node->next)
16450 prune_unused_types_walk (node->die);
16452 /* Also set the mark on nodes referenced from the
16453 pubname_table or arange_table. */
16454 for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
16455 prune_unused_types_mark (pub->die, 1);
16456 for (i = 0; i < arange_table_in_use; i++)
16457 prune_unused_types_mark (arange_table[i], 1);
16459 /* Get rid of nodes that aren't marked; and update the string counts. */
16460 if (debug_str_hash)
16461 htab_empty (debug_str_hash);
16462 prune_unused_types_prune (comp_unit_die);
16463 for (node = limbo_die_list; node; node = node->next)
16464 prune_unused_types_prune (node->die);
16466 /* Leave the marks clear. */
16467 prune_unmark_dies (comp_unit_die);
16468 for (node = limbo_die_list; node; node = node->next)
16469 prune_unmark_dies (node->die);
16472 /* Set the parameter to true if there are any relative pathnames in
16473 the file table. */
16474 static int
16475 file_table_relative_p (void ** slot, void *param)
16477 bool *p = (bool *) param;
16478 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
16479 if (!IS_ABSOLUTE_PATH (d->filename))
16481 *p = true;
16482 return 0;
16484 return 1;
16487 /* Output stuff that dwarf requires at the end of every file,
16488 and generate the DWARF-2 debugging info. */
16490 static void
16491 dwarf2out_finish (const char *filename)
16493 limbo_die_node *node, *next_node;
16494 dw_die_ref die = 0;
16495 unsigned int i;
16497 /* Add the name for the main input file now. We delayed this from
16498 dwarf2out_init to avoid complications with PCH. */
16499 add_name_attribute (comp_unit_die, remap_debug_filename (filename));
16500 if (!IS_ABSOLUTE_PATH (filename))
16501 add_comp_dir_attribute (comp_unit_die);
16502 else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
16504 bool p = false;
16505 htab_traverse (file_table, file_table_relative_p, &p);
16506 if (p)
16507 add_comp_dir_attribute (comp_unit_die);
16510 for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
16512 add_location_or_const_value_attribute (
16513 VEC_index (deferred_locations, deferred_locations_list, i)->die,
16514 VEC_index (deferred_locations, deferred_locations_list, i)->variable,
16515 DW_AT_location);
16518 /* Traverse the limbo die list, and add parent/child links. The only
16519 dies without parents that should be here are concrete instances of
16520 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
16521 For concrete instances, we can get the parent die from the abstract
16522 instance. */
16523 for (node = limbo_die_list; node; node = next_node)
16525 next_node = node->next;
16526 die = node->die;
16528 if (die->die_parent == NULL)
16530 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
16532 if (origin)
16533 add_child_die (origin->die_parent, die);
16534 else if (die == comp_unit_die)
16536 else if (errorcount > 0 || sorrycount > 0)
16537 /* It's OK to be confused by errors in the input. */
16538 add_child_die (comp_unit_die, die);
16539 else
16541 /* In certain situations, the lexical block containing a
16542 nested function can be optimized away, which results
16543 in the nested function die being orphaned. Likewise
16544 with the return type of that nested function. Force
16545 this to be a child of the containing function.
16547 It may happen that even the containing function got fully
16548 inlined and optimized out. In that case we are lost and
16549 assign the empty child. This should not be big issue as
16550 the function is likely unreachable too. */
16551 tree context = NULL_TREE;
16553 gcc_assert (node->created_for);
16555 if (DECL_P (node->created_for))
16556 context = DECL_CONTEXT (node->created_for);
16557 else if (TYPE_P (node->created_for))
16558 context = TYPE_CONTEXT (node->created_for);
16560 gcc_assert (context
16561 && (TREE_CODE (context) == FUNCTION_DECL
16562 || TREE_CODE (context) == NAMESPACE_DECL));
16564 origin = lookup_decl_die (context);
16565 if (origin)
16566 add_child_die (origin, die);
16567 else
16568 add_child_die (comp_unit_die, die);
16573 limbo_die_list = NULL;
16575 /* Walk through the list of incomplete types again, trying once more to
16576 emit full debugging info for them. */
16577 retry_incomplete_types ();
16579 if (flag_eliminate_unused_debug_types)
16580 prune_unused_types ();
16582 /* Generate separate CUs for each of the include files we've seen.
16583 They will go into limbo_die_list. */
16584 if (flag_eliminate_dwarf2_dups)
16585 break_out_includes (comp_unit_die);
16587 /* Traverse the DIE's and add add sibling attributes to those DIE's
16588 that have children. */
16589 add_sibling_attributes (comp_unit_die);
16590 for (node = limbo_die_list; node; node = node->next)
16591 add_sibling_attributes (node->die);
16593 /* Output a terminator label for the .text section. */
16594 switch_to_section (text_section);
16595 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
16596 if (flag_reorder_blocks_and_partition)
16598 switch_to_section (unlikely_text_section ());
16599 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
16602 /* We can only use the low/high_pc attributes if all of the code was
16603 in .text. */
16604 if (!have_multiple_function_sections)
16606 add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
16607 add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
16610 else
16612 unsigned fde_idx = 0;
16614 /* We need to give .debug_loc and .debug_ranges an appropriate
16615 "base address". Use zero so that these addresses become
16616 absolute. Historically, we've emitted the unexpected
16617 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
16618 Emit both to give time for other tools to adapt. */
16619 add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
16620 add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
16622 add_AT_range_list (comp_unit_die, DW_AT_ranges,
16623 add_ranges_by_labels (text_section_label,
16624 text_end_label));
16625 if (flag_reorder_blocks_and_partition)
16626 add_ranges_by_labels (cold_text_section_label,
16627 cold_end_label);
16629 for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
16631 dw_fde_ref fde = &fde_table[fde_idx];
16633 if (fde->dw_fde_switched_sections)
16635 add_ranges_by_labels (fde->dw_fde_hot_section_label,
16636 fde->dw_fde_hot_section_end_label);
16637 add_ranges_by_labels (fde->dw_fde_unlikely_section_label,
16638 fde->dw_fde_unlikely_section_end_label);
16640 else
16641 add_ranges_by_labels (fde->dw_fde_begin,
16642 fde->dw_fde_end);
16645 add_ranges (NULL);
16648 /* Output location list section if necessary. */
16649 if (have_location_lists)
16651 /* Output the location lists info. */
16652 switch_to_section (debug_loc_section);
16653 ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
16654 DEBUG_LOC_SECTION_LABEL, 0);
16655 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
16656 output_location_lists (die);
16659 if (debug_info_level >= DINFO_LEVEL_NORMAL)
16660 add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
16661 debug_line_section_label);
16663 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16664 add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
16666 /* Output all of the compilation units. We put the main one last so that
16667 the offsets are available to output_pubnames. */
16668 for (node = limbo_die_list; node; node = node->next)
16669 output_comp_unit (node->die, 0);
16671 /* Output the main compilation unit if non-empty or if .debug_macinfo
16672 has been emitted. */
16673 output_comp_unit (comp_unit_die, debug_info_level >= DINFO_LEVEL_VERBOSE);
16675 /* Output the abbreviation table. */
16676 switch_to_section (debug_abbrev_section);
16677 output_abbrev_section ();
16679 /* Output public names table if necessary. */
16680 if (!VEC_empty (pubname_entry, pubname_table))
16682 switch_to_section (debug_pubnames_section);
16683 output_pubnames (pubname_table);
16686 #ifdef DEBUG_PUBTYPES_SECTION
16687 /* Output public types table if necessary. */
16688 if (!VEC_empty (pubname_entry, pubtype_table))
16690 switch_to_section (debug_pubtypes_section);
16691 output_pubnames (pubtype_table);
16693 #endif
16695 /* Output the address range information. We only put functions in the arange
16696 table, so don't write it out if we don't have any. */
16697 if (fde_table_in_use)
16699 switch_to_section (debug_aranges_section);
16700 output_aranges ();
16703 /* Output ranges section if necessary. */
16704 if (ranges_table_in_use)
16706 switch_to_section (debug_ranges_section);
16707 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
16708 output_ranges ();
16711 /* Output the source line correspondence table. We must do this
16712 even if there is no line information. Otherwise, on an empty
16713 translation unit, we will generate a present, but empty,
16714 .debug_info section. IRIX 6.5 `nm' will then complain when
16715 examining the file. This is done late so that any filenames
16716 used by the debug_info section are marked as 'used'. */
16717 if (! DWARF2_ASM_LINE_DEBUG_INFO)
16719 switch_to_section (debug_line_section);
16720 output_line_info ();
16723 /* Have to end the macro section. */
16724 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16726 switch_to_section (debug_macinfo_section);
16727 dw2_asm_output_data (1, 0, "End compilation unit");
16730 /* If we emitted any DW_FORM_strp form attribute, output the string
16731 table too. */
16732 if (debug_str_hash)
16733 htab_traverse (debug_str_hash, output_indirect_string, NULL);
16735 #else
16737 /* This should never be used, but its address is needed for comparisons. */
16738 const struct gcc_debug_hooks dwarf2_debug_hooks;
16740 #endif /* DWARF2_DEBUGGING_INFO */
16742 #include "gt-dwarf2out.h"