dma: bump man page date
[dragonfly.git] / contrib / gcc-4.4 / gcc / dwarf2out.c
blob7183c403c74519bfdb772fba4523c85dcd69edd1
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. FORCE is true
698 if a label needs to be output even when using .cfi_* directives. */
700 char *
701 dwarf2out_cfi_label (bool force)
703 static char label[20];
705 if (!force && dwarf2out_do_cfi_asm ())
707 /* In this case, we will be emitting the asm directive instead of
708 the label, so just return a placeholder to keep the rest of the
709 interfaces happy. */
710 strcpy (label, "<do not output>");
712 else
714 ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
715 ASM_OUTPUT_LABEL (asm_out_file, label);
718 return label;
721 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
722 or to the CIE if LABEL is NULL. */
724 static void
725 add_fde_cfi (const char *label, dw_cfi_ref cfi)
727 dw_cfi_ref *list_head = &cie_cfi_head;
729 if (dwarf2out_do_cfi_asm ())
731 if (label)
733 dw_fde_ref fde = current_fde ();
735 gcc_assert (fde != NULL);
737 /* We still have to add the cfi to the list so that
738 lookup_cfa works later on. When -g2 and above we
739 even need to force emitting of CFI labels and
740 add to list a DW_CFA_set_loc for convert_cfa_to_fb_loc_list
741 purposes. */
742 switch (cfi->dw_cfi_opc)
744 case DW_CFA_def_cfa_offset:
745 case DW_CFA_def_cfa_offset_sf:
746 case DW_CFA_def_cfa_register:
747 case DW_CFA_def_cfa:
748 case DW_CFA_def_cfa_sf:
749 case DW_CFA_def_cfa_expression:
750 case DW_CFA_restore_state:
751 if (write_symbols != DWARF2_DEBUG
752 && write_symbols != VMS_AND_DWARF2_DEBUG)
753 break;
754 if (debug_info_level <= DINFO_LEVEL_TERSE)
755 break;
757 if (*label == 0 || strcmp (label, "<do not output>") == 0)
758 label = dwarf2out_cfi_label (true);
760 if (fde->dw_fde_current_label == NULL
761 || strcmp (label, fde->dw_fde_current_label) != 0)
763 dw_cfi_ref xcfi;
765 label = xstrdup (label);
767 /* Set the location counter to the new label. */
768 xcfi = new_cfi ();
769 /* It doesn't metter whether DW_CFA_set_loc
770 or DW_CFA_advance_loc4 is added here, those aren't
771 emitted into assembly, only looked up by
772 convert_cfa_to_fb_loc_list. */
773 xcfi->dw_cfi_opc = DW_CFA_set_loc;
774 xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
775 add_cfi (&fde->dw_fde_cfi, xcfi);
776 fde->dw_fde_current_label = label;
778 break;
779 default:
780 break;
783 output_cfi_directive (cfi);
785 list_head = &fde->dw_fde_cfi;
787 /* ??? If this is a CFI for the CIE, we don't emit. This
788 assumes that the standard CIE contents that the assembler
789 uses matches the standard CIE contents that the compiler
790 uses. This is probably a bad assumption. I'm not quite
791 sure how to address this for now. */
793 else if (label)
795 dw_fde_ref fde = current_fde ();
797 gcc_assert (fde != NULL);
799 if (*label == 0)
800 label = dwarf2out_cfi_label (false);
802 if (fde->dw_fde_current_label == NULL
803 || strcmp (label, fde->dw_fde_current_label) != 0)
805 dw_cfi_ref xcfi;
807 label = xstrdup (label);
809 /* Set the location counter to the new label. */
810 xcfi = new_cfi ();
811 /* If we have a current label, advance from there, otherwise
812 set the location directly using set_loc. */
813 xcfi->dw_cfi_opc = fde->dw_fde_current_label
814 ? DW_CFA_advance_loc4
815 : DW_CFA_set_loc;
816 xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
817 add_cfi (&fde->dw_fde_cfi, xcfi);
819 fde->dw_fde_current_label = label;
822 list_head = &fde->dw_fde_cfi;
825 add_cfi (list_head, cfi);
828 /* Subroutine of lookup_cfa. */
830 static void
831 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc)
833 switch (cfi->dw_cfi_opc)
835 case DW_CFA_def_cfa_offset:
836 case DW_CFA_def_cfa_offset_sf:
837 loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
838 break;
839 case DW_CFA_def_cfa_register:
840 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
841 break;
842 case DW_CFA_def_cfa:
843 case DW_CFA_def_cfa_sf:
844 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
845 loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
846 break;
847 case DW_CFA_def_cfa_expression:
848 get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
849 break;
850 default:
851 break;
855 /* Find the previous value for the CFA. */
857 static void
858 lookup_cfa (dw_cfa_location *loc)
860 dw_cfi_ref cfi;
861 dw_fde_ref fde;
863 loc->reg = INVALID_REGNUM;
864 loc->offset = 0;
865 loc->indirect = 0;
866 loc->base_offset = 0;
868 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
869 lookup_cfa_1 (cfi, loc);
871 fde = current_fde ();
872 if (fde)
873 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
874 lookup_cfa_1 (cfi, loc);
877 /* The current rule for calculating the DWARF2 canonical frame address. */
878 static dw_cfa_location cfa;
880 /* The register used for saving registers to the stack, and its offset
881 from the CFA. */
882 static dw_cfa_location cfa_store;
884 /* The running total of the size of arguments pushed onto the stack. */
885 static HOST_WIDE_INT args_size;
887 /* The last args_size we actually output. */
888 static HOST_WIDE_INT old_args_size;
890 /* Entry point to update the canonical frame address (CFA).
891 LABEL is passed to add_fde_cfi. The value of CFA is now to be
892 calculated from REG+OFFSET. */
894 void
895 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
897 dw_cfa_location loc;
898 loc.indirect = 0;
899 loc.base_offset = 0;
900 loc.reg = reg;
901 loc.offset = offset;
902 def_cfa_1 (label, &loc);
905 /* Determine if two dw_cfa_location structures define the same data. */
907 static bool
908 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
910 return (loc1->reg == loc2->reg
911 && loc1->offset == loc2->offset
912 && loc1->indirect == loc2->indirect
913 && (loc1->indirect == 0
914 || loc1->base_offset == loc2->base_offset));
917 /* This routine does the actual work. The CFA is now calculated from
918 the dw_cfa_location structure. */
920 static void
921 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
923 dw_cfi_ref cfi;
924 dw_cfa_location old_cfa, loc;
926 cfa = *loc_p;
927 loc = *loc_p;
929 if (cfa_store.reg == loc.reg && loc.indirect == 0)
930 cfa_store.offset = loc.offset;
932 loc.reg = DWARF_FRAME_REGNUM (loc.reg);
933 lookup_cfa (&old_cfa);
935 /* If nothing changed, no need to issue any call frame instructions. */
936 if (cfa_equal_p (&loc, &old_cfa))
937 return;
939 cfi = new_cfi ();
941 if (loc.reg == old_cfa.reg && !loc.indirect)
943 /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
944 the CFA register did not change but the offset did. The data
945 factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
946 in the assembler via the .cfi_def_cfa_offset directive. */
947 if (loc.offset < 0)
948 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
949 else
950 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
951 cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
954 #ifndef MIPS_DEBUGGING_INFO /* SGI dbx thinks this means no offset. */
955 else if (loc.offset == old_cfa.offset
956 && old_cfa.reg != INVALID_REGNUM
957 && !loc.indirect)
959 /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
960 indicating the CFA register has changed to <register> but the
961 offset has not changed. */
962 cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
963 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
965 #endif
967 else if (loc.indirect == 0)
969 /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
970 indicating the CFA register has changed to <register> with
971 the specified offset. The data factoring for DW_CFA_def_cfa_sf
972 happens in output_cfi, or in the assembler via the .cfi_def_cfa
973 directive. */
974 if (loc.offset < 0)
975 cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
976 else
977 cfi->dw_cfi_opc = DW_CFA_def_cfa;
978 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
979 cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
981 else
983 /* Construct a DW_CFA_def_cfa_expression instruction to
984 calculate the CFA using a full location expression since no
985 register-offset pair is available. */
986 struct dw_loc_descr_struct *loc_list;
988 cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
989 loc_list = build_cfa_loc (&loc, 0);
990 cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
993 add_fde_cfi (label, cfi);
996 /* Add the CFI for saving a register. REG is the CFA column number.
997 LABEL is passed to add_fde_cfi.
998 If SREG is -1, the register is saved at OFFSET from the CFA;
999 otherwise it is saved in SREG. */
1001 static void
1002 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
1004 dw_cfi_ref cfi = new_cfi ();
1005 dw_fde_ref fde = current_fde ();
1007 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1009 /* When stack is aligned, store REG using DW_CFA_expression with
1010 FP. */
1011 if (fde
1012 && fde->stack_realign
1013 && sreg == INVALID_REGNUM)
1015 cfi->dw_cfi_opc = DW_CFA_expression;
1016 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = reg;
1017 cfi->dw_cfi_oprnd1.dw_cfi_loc
1018 = build_cfa_aligned_loc (offset, fde->stack_realignment);
1020 else if (sreg == INVALID_REGNUM)
1022 if (offset < 0)
1023 cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
1024 else if (reg & ~0x3f)
1025 cfi->dw_cfi_opc = DW_CFA_offset_extended;
1026 else
1027 cfi->dw_cfi_opc = DW_CFA_offset;
1028 cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
1030 else if (sreg == reg)
1031 cfi->dw_cfi_opc = DW_CFA_same_value;
1032 else
1034 cfi->dw_cfi_opc = DW_CFA_register;
1035 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
1038 add_fde_cfi (label, cfi);
1041 /* Add the CFI for saving a register window. LABEL is passed to reg_save.
1042 This CFI tells the unwinder that it needs to restore the window registers
1043 from the previous frame's window save area.
1045 ??? Perhaps we should note in the CIE where windows are saved (instead of
1046 assuming 0(cfa)) and what registers are in the window. */
1048 void
1049 dwarf2out_window_save (const char *label)
1051 dw_cfi_ref cfi = new_cfi ();
1053 cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1054 add_fde_cfi (label, cfi);
1057 /* Add a CFI to update the running total of the size of arguments
1058 pushed onto the stack. */
1060 void
1061 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1063 dw_cfi_ref cfi;
1065 if (size == old_args_size)
1066 return;
1068 old_args_size = size;
1070 cfi = new_cfi ();
1071 cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1072 cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1073 add_fde_cfi (label, cfi);
1076 /* Entry point for saving a register to the stack. REG is the GCC register
1077 number. LABEL and OFFSET are passed to reg_save. */
1079 void
1080 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1082 reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1085 /* Entry point for saving the return address in the stack.
1086 LABEL and OFFSET are passed to reg_save. */
1088 void
1089 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1091 reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1094 /* Entry point for saving the return address in a register.
1095 LABEL and SREG are passed to reg_save. */
1097 void
1098 dwarf2out_return_reg (const char *label, unsigned int sreg)
1100 reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1103 #ifdef DWARF2_UNWIND_INFO
1104 /* Record the initial position of the return address. RTL is
1105 INCOMING_RETURN_ADDR_RTX. */
1107 static void
1108 initial_return_save (rtx rtl)
1110 unsigned int reg = INVALID_REGNUM;
1111 HOST_WIDE_INT offset = 0;
1113 switch (GET_CODE (rtl))
1115 case REG:
1116 /* RA is in a register. */
1117 reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1118 break;
1120 case MEM:
1121 /* RA is on the stack. */
1122 rtl = XEXP (rtl, 0);
1123 switch (GET_CODE (rtl))
1125 case REG:
1126 gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1127 offset = 0;
1128 break;
1130 case PLUS:
1131 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1132 offset = INTVAL (XEXP (rtl, 1));
1133 break;
1135 case MINUS:
1136 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1137 offset = -INTVAL (XEXP (rtl, 1));
1138 break;
1140 default:
1141 gcc_unreachable ();
1144 break;
1146 case PLUS:
1147 /* The return address is at some offset from any value we can
1148 actually load. For instance, on the SPARC it is in %i7+8. Just
1149 ignore the offset for now; it doesn't matter for unwinding frames. */
1150 gcc_assert (GET_CODE (XEXP (rtl, 1)) == CONST_INT);
1151 initial_return_save (XEXP (rtl, 0));
1152 return;
1154 default:
1155 gcc_unreachable ();
1158 if (reg != DWARF_FRAME_RETURN_COLUMN)
1159 reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1161 #endif
1163 /* Given a SET, calculate the amount of stack adjustment it
1164 contains. */
1166 static HOST_WIDE_INT
1167 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1168 HOST_WIDE_INT cur_offset)
1170 const_rtx src = SET_SRC (pattern);
1171 const_rtx dest = SET_DEST (pattern);
1172 HOST_WIDE_INT offset = 0;
1173 enum rtx_code code;
1175 if (dest == stack_pointer_rtx)
1177 code = GET_CODE (src);
1179 /* Assume (set (reg sp) (reg whatever)) sets args_size
1180 level to 0. */
1181 if (code == REG && src != stack_pointer_rtx)
1183 offset = -cur_args_size;
1184 #ifndef STACK_GROWS_DOWNWARD
1185 offset = -offset;
1186 #endif
1187 return offset - cur_offset;
1190 if (! (code == PLUS || code == MINUS)
1191 || XEXP (src, 0) != stack_pointer_rtx
1192 || GET_CODE (XEXP (src, 1)) != CONST_INT)
1193 return 0;
1195 /* (set (reg sp) (plus (reg sp) (const_int))) */
1196 offset = INTVAL (XEXP (src, 1));
1197 if (code == PLUS)
1198 offset = -offset;
1199 return offset;
1202 if (MEM_P (src) && !MEM_P (dest))
1203 dest = src;
1204 if (MEM_P (dest))
1206 /* (set (mem (pre_dec (reg sp))) (foo)) */
1207 src = XEXP (dest, 0);
1208 code = GET_CODE (src);
1210 switch (code)
1212 case PRE_MODIFY:
1213 case POST_MODIFY:
1214 if (XEXP (src, 0) == stack_pointer_rtx)
1216 rtx val = XEXP (XEXP (src, 1), 1);
1217 /* We handle only adjustments by constant amount. */
1218 gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1219 && GET_CODE (val) == CONST_INT);
1220 offset = -INTVAL (val);
1221 break;
1223 return 0;
1225 case PRE_DEC:
1226 case POST_DEC:
1227 if (XEXP (src, 0) == stack_pointer_rtx)
1229 offset = GET_MODE_SIZE (GET_MODE (dest));
1230 break;
1232 return 0;
1234 case PRE_INC:
1235 case POST_INC:
1236 if (XEXP (src, 0) == stack_pointer_rtx)
1238 offset = -GET_MODE_SIZE (GET_MODE (dest));
1239 break;
1241 return 0;
1243 default:
1244 return 0;
1247 else
1248 return 0;
1250 return offset;
1253 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1254 indexed by INSN_UID. */
1256 static HOST_WIDE_INT *barrier_args_size;
1258 /* Helper function for compute_barrier_args_size. Handle one insn. */
1260 static HOST_WIDE_INT
1261 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1262 VEC (rtx, heap) **next)
1264 HOST_WIDE_INT offset = 0;
1265 int i;
1267 if (! RTX_FRAME_RELATED_P (insn))
1269 if (prologue_epilogue_contains (insn)
1270 || sibcall_epilogue_contains (insn))
1271 /* Nothing */;
1272 else if (GET_CODE (PATTERN (insn)) == SET)
1273 offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1274 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1275 || GET_CODE (PATTERN (insn)) == SEQUENCE)
1277 /* There may be stack adjustments inside compound insns. Search
1278 for them. */
1279 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1280 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1281 offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1282 cur_args_size, offset);
1285 else
1287 rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1289 if (expr)
1291 expr = XEXP (expr, 0);
1292 if (GET_CODE (expr) == PARALLEL
1293 || GET_CODE (expr) == SEQUENCE)
1294 for (i = 1; i < XVECLEN (expr, 0); i++)
1296 rtx elem = XVECEXP (expr, 0, i);
1298 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1299 offset += stack_adjust_offset (elem, cur_args_size, offset);
1304 #ifndef STACK_GROWS_DOWNWARD
1305 offset = -offset;
1306 #endif
1308 cur_args_size += offset;
1309 if (cur_args_size < 0)
1310 cur_args_size = 0;
1312 if (JUMP_P (insn))
1314 rtx dest = JUMP_LABEL (insn);
1316 if (dest)
1318 if (barrier_args_size [INSN_UID (dest)] < 0)
1320 barrier_args_size [INSN_UID (dest)] = cur_args_size;
1321 VEC_safe_push (rtx, heap, *next, dest);
1326 return cur_args_size;
1329 /* Walk the whole function and compute args_size on BARRIERs. */
1331 static void
1332 compute_barrier_args_size (void)
1334 int max_uid = get_max_uid (), i;
1335 rtx insn;
1336 VEC (rtx, heap) *worklist, *next, *tmp;
1338 barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1339 for (i = 0; i < max_uid; i++)
1340 barrier_args_size[i] = -1;
1342 worklist = VEC_alloc (rtx, heap, 20);
1343 next = VEC_alloc (rtx, heap, 20);
1344 insn = get_insns ();
1345 barrier_args_size[INSN_UID (insn)] = 0;
1346 VEC_quick_push (rtx, worklist, insn);
1347 for (;;)
1349 while (!VEC_empty (rtx, worklist))
1351 rtx prev, body, first_insn;
1352 HOST_WIDE_INT cur_args_size;
1354 first_insn = insn = VEC_pop (rtx, worklist);
1355 cur_args_size = barrier_args_size[INSN_UID (insn)];
1356 prev = prev_nonnote_insn (insn);
1357 if (prev && BARRIER_P (prev))
1358 barrier_args_size[INSN_UID (prev)] = cur_args_size;
1360 for (; insn; insn = NEXT_INSN (insn))
1362 if (INSN_DELETED_P (insn) || NOTE_P (insn))
1363 continue;
1364 if (BARRIER_P (insn))
1365 break;
1367 if (LABEL_P (insn))
1369 if (insn == first_insn)
1370 continue;
1371 else if (barrier_args_size[INSN_UID (insn)] < 0)
1373 barrier_args_size[INSN_UID (insn)] = cur_args_size;
1374 continue;
1376 else
1378 /* The insns starting with this label have been
1379 already scanned or are in the worklist. */
1380 break;
1384 body = PATTERN (insn);
1385 if (GET_CODE (body) == SEQUENCE)
1387 HOST_WIDE_INT dest_args_size = cur_args_size;
1388 for (i = 1; i < XVECLEN (body, 0); i++)
1389 if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1390 && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1391 dest_args_size
1392 = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1393 dest_args_size, &next);
1394 else
1395 cur_args_size
1396 = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1397 cur_args_size, &next);
1399 if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1400 compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1401 dest_args_size, &next);
1402 else
1403 cur_args_size
1404 = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1405 cur_args_size, &next);
1407 else
1408 cur_args_size
1409 = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1413 if (VEC_empty (rtx, next))
1414 break;
1416 /* Swap WORKLIST with NEXT and truncate NEXT for next iteration. */
1417 tmp = next;
1418 next = worklist;
1419 worklist = tmp;
1420 VEC_truncate (rtx, next, 0);
1423 VEC_free (rtx, heap, worklist);
1424 VEC_free (rtx, heap, next);
1428 /* Check INSN to see if it looks like a push or a stack adjustment, and
1429 make a note of it if it does. EH uses this information to find out how
1430 much extra space it needs to pop off the stack. */
1432 static void
1433 dwarf2out_stack_adjust (rtx insn, bool after_p)
1435 HOST_WIDE_INT offset;
1436 const char *label;
1437 int i;
1439 /* Don't handle epilogues at all. Certainly it would be wrong to do so
1440 with this function. Proper support would require all frame-related
1441 insns to be marked, and to be able to handle saving state around
1442 epilogues textually in the middle of the function. */
1443 if (prologue_epilogue_contains (insn) || sibcall_epilogue_contains (insn))
1444 return;
1446 /* If INSN is an instruction from target of an annulled branch, the
1447 effects are for the target only and so current argument size
1448 shouldn't change at all. */
1449 if (final_sequence
1450 && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1451 && INSN_FROM_TARGET_P (insn))
1452 return;
1454 /* If only calls can throw, and we have a frame pointer,
1455 save up adjustments until we see the CALL_INSN. */
1456 if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1458 if (CALL_P (insn) && !after_p)
1460 /* Extract the size of the args from the CALL rtx itself. */
1461 insn = PATTERN (insn);
1462 if (GET_CODE (insn) == PARALLEL)
1463 insn = XVECEXP (insn, 0, 0);
1464 if (GET_CODE (insn) == SET)
1465 insn = SET_SRC (insn);
1466 gcc_assert (GET_CODE (insn) == CALL);
1467 dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1469 return;
1472 if (CALL_P (insn) && !after_p)
1474 if (!flag_asynchronous_unwind_tables)
1475 dwarf2out_args_size ("", args_size);
1476 return;
1478 else if (BARRIER_P (insn))
1480 /* Don't call compute_barrier_args_size () if the only
1481 BARRIER is at the end of function. */
1482 if (barrier_args_size == NULL && next_nonnote_insn (insn))
1483 compute_barrier_args_size ();
1484 if (barrier_args_size == NULL)
1485 offset = 0;
1486 else
1488 offset = barrier_args_size[INSN_UID (insn)];
1489 if (offset < 0)
1490 offset = 0;
1493 offset -= args_size;
1494 #ifndef STACK_GROWS_DOWNWARD
1495 offset = -offset;
1496 #endif
1498 else if (GET_CODE (PATTERN (insn)) == SET)
1499 offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1500 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1501 || GET_CODE (PATTERN (insn)) == SEQUENCE)
1503 /* There may be stack adjustments inside compound insns. Search
1504 for them. */
1505 for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1506 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1507 offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1508 args_size, offset);
1510 else
1511 return;
1513 if (offset == 0)
1514 return;
1516 label = dwarf2out_cfi_label (false);
1517 dwarf2out_args_size_adjust (offset, label);
1520 /* Adjust args_size based on stack adjustment OFFSET. */
1522 static void
1523 dwarf2out_args_size_adjust (HOST_WIDE_INT offset, const char *label)
1525 if (cfa.reg == STACK_POINTER_REGNUM)
1526 cfa.offset += offset;
1528 if (cfa_store.reg == STACK_POINTER_REGNUM)
1529 cfa_store.offset += offset;
1531 #ifndef STACK_GROWS_DOWNWARD
1532 offset = -offset;
1533 #endif
1535 args_size += offset;
1536 if (args_size < 0)
1537 args_size = 0;
1539 def_cfa_1 (label, &cfa);
1540 if (flag_asynchronous_unwind_tables)
1541 dwarf2out_args_size (label, args_size);
1544 #endif
1546 /* We delay emitting a register save until either (a) we reach the end
1547 of the prologue or (b) the register is clobbered. This clusters
1548 register saves so that there are fewer pc advances. */
1550 struct queued_reg_save GTY(())
1552 struct queued_reg_save *next;
1553 rtx reg;
1554 HOST_WIDE_INT cfa_offset;
1555 rtx saved_reg;
1558 static GTY(()) struct queued_reg_save *queued_reg_saves;
1560 /* The caller's ORIG_REG is saved in SAVED_IN_REG. */
1561 struct reg_saved_in_data GTY(()) {
1562 rtx orig_reg;
1563 rtx saved_in_reg;
1566 /* A list of registers saved in other registers.
1567 The list intentionally has a small maximum capacity of 4; if your
1568 port needs more than that, you might consider implementing a
1569 more efficient data structure. */
1570 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1571 static GTY(()) size_t num_regs_saved_in_regs;
1573 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1574 static const char *last_reg_save_label;
1576 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1577 SREG, or if SREG is NULL then it is saved at OFFSET to the CFA. */
1579 static void
1580 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1582 struct queued_reg_save *q;
1584 /* Duplicates waste space, but it's also necessary to remove them
1585 for correctness, since the queue gets output in reverse
1586 order. */
1587 for (q = queued_reg_saves; q != NULL; q = q->next)
1588 if (REGNO (q->reg) == REGNO (reg))
1589 break;
1591 if (q == NULL)
1593 q = GGC_NEW (struct queued_reg_save);
1594 q->next = queued_reg_saves;
1595 queued_reg_saves = q;
1598 q->reg = reg;
1599 q->cfa_offset = offset;
1600 q->saved_reg = sreg;
1602 last_reg_save_label = label;
1605 /* Output all the entries in QUEUED_REG_SAVES. */
1607 static void
1608 flush_queued_reg_saves (void)
1610 struct queued_reg_save *q;
1612 for (q = queued_reg_saves; q; q = q->next)
1614 size_t i;
1615 unsigned int reg, sreg;
1617 for (i = 0; i < num_regs_saved_in_regs; i++)
1618 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1619 break;
1620 if (q->saved_reg && i == num_regs_saved_in_regs)
1622 gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1623 num_regs_saved_in_regs++;
1625 if (i != num_regs_saved_in_regs)
1627 regs_saved_in_regs[i].orig_reg = q->reg;
1628 regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1631 reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1632 if (q->saved_reg)
1633 sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1634 else
1635 sreg = INVALID_REGNUM;
1636 reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1639 queued_reg_saves = NULL;
1640 last_reg_save_label = NULL;
1643 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1644 location for? Or, does it clobber a register which we've previously
1645 said that some other register is saved in, and for which we now
1646 have a new location for? */
1648 static bool
1649 clobbers_queued_reg_save (const_rtx insn)
1651 struct queued_reg_save *q;
1653 for (q = queued_reg_saves; q; q = q->next)
1655 size_t i;
1656 if (modified_in_p (q->reg, insn))
1657 return true;
1658 for (i = 0; i < num_regs_saved_in_regs; i++)
1659 if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1660 && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1661 return true;
1664 return false;
1667 /* Entry point for saving the first register into the second. */
1669 void
1670 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1672 size_t i;
1673 unsigned int regno, sregno;
1675 for (i = 0; i < num_regs_saved_in_regs; i++)
1676 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1677 break;
1678 if (i == num_regs_saved_in_regs)
1680 gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1681 num_regs_saved_in_regs++;
1683 regs_saved_in_regs[i].orig_reg = reg;
1684 regs_saved_in_regs[i].saved_in_reg = sreg;
1686 regno = DWARF_FRAME_REGNUM (REGNO (reg));
1687 sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1688 reg_save (label, regno, sregno, 0);
1691 /* What register, if any, is currently saved in REG? */
1693 static rtx
1694 reg_saved_in (rtx reg)
1696 unsigned int regn = REGNO (reg);
1697 size_t i;
1698 struct queued_reg_save *q;
1700 for (q = queued_reg_saves; q; q = q->next)
1701 if (q->saved_reg && regn == REGNO (q->saved_reg))
1702 return q->reg;
1704 for (i = 0; i < num_regs_saved_in_regs; i++)
1705 if (regs_saved_in_regs[i].saved_in_reg
1706 && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1707 return regs_saved_in_regs[i].orig_reg;
1709 return NULL_RTX;
1713 /* A temporary register holding an integral value used in adjusting SP
1714 or setting up the store_reg. The "offset" field holds the integer
1715 value, not an offset. */
1716 static dw_cfa_location cfa_temp;
1718 /* Record call frame debugging information for an expression EXPR,
1719 which either sets SP or FP (adjusting how we calculate the frame
1720 address) or saves a register to the stack or another register.
1721 LABEL indicates the address of EXPR.
1723 This function encodes a state machine mapping rtxes to actions on
1724 cfa, cfa_store, and cfa_temp.reg. We describe these rules so
1725 users need not read the source code.
1727 The High-Level Picture
1729 Changes in the register we use to calculate the CFA: Currently we
1730 assume that if you copy the CFA register into another register, we
1731 should take the other one as the new CFA register; this seems to
1732 work pretty well. If it's wrong for some target, it's simple
1733 enough not to set RTX_FRAME_RELATED_P on the insn in question.
1735 Changes in the register we use for saving registers to the stack:
1736 This is usually SP, but not always. Again, we deduce that if you
1737 copy SP into another register (and SP is not the CFA register),
1738 then the new register is the one we will be using for register
1739 saves. This also seems to work.
1741 Register saves: There's not much guesswork about this one; if
1742 RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1743 register save, and the register used to calculate the destination
1744 had better be the one we think we're using for this purpose.
1745 It's also assumed that a copy from a call-saved register to another
1746 register is saving that register if RTX_FRAME_RELATED_P is set on
1747 that instruction. If the copy is from a call-saved register to
1748 the *same* register, that means that the register is now the same
1749 value as in the caller.
1751 Except: If the register being saved is the CFA register, and the
1752 offset is nonzero, we are saving the CFA, so we assume we have to
1753 use DW_CFA_def_cfa_expression. If the offset is 0, we assume that
1754 the intent is to save the value of SP from the previous frame.
1756 In addition, if a register has previously been saved to a different
1757 register,
1759 Invariants / Summaries of Rules
1761 cfa current rule for calculating the CFA. It usually
1762 consists of a register and an offset.
1763 cfa_store register used by prologue code to save things to the stack
1764 cfa_store.offset is the offset from the value of
1765 cfa_store.reg to the actual CFA
1766 cfa_temp register holding an integral value. cfa_temp.offset
1767 stores the value, which will be used to adjust the
1768 stack pointer. cfa_temp is also used like cfa_store,
1769 to track stores to the stack via fp or a temp reg.
1771 Rules 1- 4: Setting a register's value to cfa.reg or an expression
1772 with cfa.reg as the first operand changes the cfa.reg and its
1773 cfa.offset. Rule 1 and 4 also set cfa_temp.reg and
1774 cfa_temp.offset.
1776 Rules 6- 9: Set a non-cfa.reg register value to a constant or an
1777 expression yielding a constant. This sets cfa_temp.reg
1778 and cfa_temp.offset.
1780 Rule 5: Create a new register cfa_store used to save items to the
1781 stack.
1783 Rules 10-14: Save a register to the stack. Define offset as the
1784 difference of the original location and cfa_store's
1785 location (or cfa_temp's location if cfa_temp is used).
1787 Rules 16-20: If AND operation happens on sp in prologue, we assume
1788 stack is realigned. We will use a group of DW_OP_XXX
1789 expressions to represent the location of the stored
1790 register instead of CFA+offset.
1792 The Rules
1794 "{a,b}" indicates a choice of a xor b.
1795 "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1797 Rule 1:
1798 (set <reg1> <reg2>:cfa.reg)
1799 effects: cfa.reg = <reg1>
1800 cfa.offset unchanged
1801 cfa_temp.reg = <reg1>
1802 cfa_temp.offset = cfa.offset
1804 Rule 2:
1805 (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1806 {<const_int>,<reg>:cfa_temp.reg}))
1807 effects: cfa.reg = sp if fp used
1808 cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
1809 cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1810 if cfa_store.reg==sp
1812 Rule 3:
1813 (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
1814 effects: cfa.reg = fp
1815 cfa_offset += +/- <const_int>
1817 Rule 4:
1818 (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
1819 constraints: <reg1> != fp
1820 <reg1> != sp
1821 effects: cfa.reg = <reg1>
1822 cfa_temp.reg = <reg1>
1823 cfa_temp.offset = cfa.offset
1825 Rule 5:
1826 (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1827 constraints: <reg1> != fp
1828 <reg1> != sp
1829 effects: cfa_store.reg = <reg1>
1830 cfa_store.offset = cfa.offset - cfa_temp.offset
1832 Rule 6:
1833 (set <reg> <const_int>)
1834 effects: cfa_temp.reg = <reg>
1835 cfa_temp.offset = <const_int>
1837 Rule 7:
1838 (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1839 effects: cfa_temp.reg = <reg1>
1840 cfa_temp.offset |= <const_int>
1842 Rule 8:
1843 (set <reg> (high <exp>))
1844 effects: none
1846 Rule 9:
1847 (set <reg> (lo_sum <exp> <const_int>))
1848 effects: cfa_temp.reg = <reg>
1849 cfa_temp.offset = <const_int>
1851 Rule 10:
1852 (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1853 effects: cfa_store.offset -= <const_int>
1854 cfa.offset = cfa_store.offset if cfa.reg == sp
1855 cfa.reg = sp
1856 cfa.base_offset = -cfa_store.offset
1858 Rule 11:
1859 (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1860 effects: cfa_store.offset += -/+ mode_size(mem)
1861 cfa.offset = cfa_store.offset if cfa.reg == sp
1862 cfa.reg = sp
1863 cfa.base_offset = -cfa_store.offset
1865 Rule 12:
1866 (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1868 <reg2>)
1869 effects: cfa.reg = <reg1>
1870 cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
1872 Rule 13:
1873 (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1874 effects: cfa.reg = <reg1>
1875 cfa.base_offset = -{cfa_store,cfa_temp}.offset
1877 Rule 14:
1878 (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
1879 effects: cfa.reg = <reg1>
1880 cfa.base_offset = -cfa_temp.offset
1881 cfa_temp.offset -= mode_size(mem)
1883 Rule 15:
1884 (set <reg> {unspec, unspec_volatile})
1885 effects: target-dependent
1887 Rule 16:
1888 (set sp (and: sp <const_int>))
1889 constraints: cfa_store.reg == sp
1890 effects: current_fde.stack_realign = 1
1891 cfa_store.offset = 0
1892 fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
1894 Rule 17:
1895 (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
1896 effects: cfa_store.offset += -/+ mode_size(mem)
1898 Rule 18:
1899 (set (mem ({pre_inc, pre_dec} sp)) fp)
1900 constraints: fde->stack_realign == 1
1901 effects: cfa_store.offset = 0
1902 cfa.reg != HARD_FRAME_POINTER_REGNUM
1904 Rule 19:
1905 (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
1906 constraints: fde->stack_realign == 1
1907 && cfa.offset == 0
1908 && cfa.indirect == 0
1909 && cfa.reg != HARD_FRAME_POINTER_REGNUM
1910 effects: Use DW_CFA_def_cfa_expression to define cfa
1911 cfa.reg == fde->drap_reg
1913 Rule 20:
1914 (set reg fde->drap_reg)
1915 constraints: fde->vdrap_reg == INVALID_REGNUM
1916 effects: fde->vdrap_reg = reg.
1917 (set mem fde->drap_reg)
1918 constraints: fde->drap_reg_saved == 1
1919 effects: none. */
1921 static void
1922 dwarf2out_frame_debug_expr (rtx expr, const char *label)
1924 rtx src, dest, span;
1925 HOST_WIDE_INT offset;
1926 dw_fde_ref fde;
1928 /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1929 the PARALLEL independently. The first element is always processed if
1930 it is a SET. This is for backward compatibility. Other elements
1931 are processed only if they are SETs and the RTX_FRAME_RELATED_P
1932 flag is set in them. */
1933 if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
1935 int par_index;
1936 int limit = XVECLEN (expr, 0);
1937 rtx elem;
1939 /* PARALLELs have strict read-modify-write semantics, so we
1940 ought to evaluate every rvalue before changing any lvalue.
1941 It's cumbersome to do that in general, but there's an
1942 easy approximation that is enough for all current users:
1943 handle register saves before register assignments. */
1944 if (GET_CODE (expr) == PARALLEL)
1945 for (par_index = 0; par_index < limit; par_index++)
1947 elem = XVECEXP (expr, 0, par_index);
1948 if (GET_CODE (elem) == SET
1949 && MEM_P (SET_DEST (elem))
1950 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1951 dwarf2out_frame_debug_expr (elem, label);
1954 for (par_index = 0; par_index < limit; par_index++)
1956 elem = XVECEXP (expr, 0, par_index);
1957 if (GET_CODE (elem) == SET
1958 && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
1959 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1960 dwarf2out_frame_debug_expr (elem, label);
1961 else if (GET_CODE (elem) == SET
1962 && par_index != 0
1963 && !RTX_FRAME_RELATED_P (elem))
1965 /* Stack adjustment combining might combine some post-prologue
1966 stack adjustment into a prologue stack adjustment. */
1967 HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
1969 if (offset != 0)
1970 dwarf2out_args_size_adjust (offset, label);
1973 return;
1976 gcc_assert (GET_CODE (expr) == SET);
1978 src = SET_SRC (expr);
1979 dest = SET_DEST (expr);
1981 if (REG_P (src))
1983 rtx rsi = reg_saved_in (src);
1984 if (rsi)
1985 src = rsi;
1988 fde = current_fde ();
1990 if (GET_CODE (src) == REG
1991 && fde
1992 && fde->drap_reg == REGNO (src)
1993 && (fde->drap_reg_saved
1994 || GET_CODE (dest) == REG))
1996 /* Rule 20 */
1997 /* If we are saving dynamic realign argument pointer to a
1998 register, the destination is virtual dynamic realign
1999 argument pointer. It may be used to access argument. */
2000 if (GET_CODE (dest) == REG)
2002 gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2003 fde->vdrap_reg = REGNO (dest);
2005 return;
2008 switch (GET_CODE (dest))
2010 case REG:
2011 switch (GET_CODE (src))
2013 /* Setting FP from SP. */
2014 case REG:
2015 if (cfa.reg == (unsigned) REGNO (src))
2017 /* Rule 1 */
2018 /* Update the CFA rule wrt SP or FP. Make sure src is
2019 relative to the current CFA register.
2021 We used to require that dest be either SP or FP, but the
2022 ARM copies SP to a temporary register, and from there to
2023 FP. So we just rely on the backends to only set
2024 RTX_FRAME_RELATED_P on appropriate insns. */
2025 cfa.reg = REGNO (dest);
2026 cfa_temp.reg = cfa.reg;
2027 cfa_temp.offset = cfa.offset;
2029 else
2031 /* Saving a register in a register. */
2032 gcc_assert (!fixed_regs [REGNO (dest)]
2033 /* For the SPARC and its register window. */
2034 || (DWARF_FRAME_REGNUM (REGNO (src))
2035 == DWARF_FRAME_RETURN_COLUMN));
2037 /* After stack is aligned, we can only save SP in FP
2038 if drap register is used. In this case, we have
2039 to restore stack pointer with the CFA value and we
2040 don't generate this DWARF information. */
2041 if (fde
2042 && fde->stack_realign
2043 && REGNO (src) == STACK_POINTER_REGNUM)
2044 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
2045 && fde->drap_reg != INVALID_REGNUM
2046 && cfa.reg != REGNO (src));
2047 else
2048 queue_reg_save (label, src, dest, 0);
2050 break;
2052 case PLUS:
2053 case MINUS:
2054 case LO_SUM:
2055 if (dest == stack_pointer_rtx)
2057 /* Rule 2 */
2058 /* Adjusting SP. */
2059 switch (GET_CODE (XEXP (src, 1)))
2061 case CONST_INT:
2062 offset = INTVAL (XEXP (src, 1));
2063 break;
2064 case REG:
2065 gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2066 == cfa_temp.reg);
2067 offset = cfa_temp.offset;
2068 break;
2069 default:
2070 gcc_unreachable ();
2073 if (XEXP (src, 0) == hard_frame_pointer_rtx)
2075 /* Restoring SP from FP in the epilogue. */
2076 gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2077 cfa.reg = STACK_POINTER_REGNUM;
2079 else if (GET_CODE (src) == LO_SUM)
2080 /* Assume we've set the source reg of the LO_SUM from sp. */
2082 else
2083 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2085 if (GET_CODE (src) != MINUS)
2086 offset = -offset;
2087 if (cfa.reg == STACK_POINTER_REGNUM)
2088 cfa.offset += offset;
2089 if (cfa_store.reg == STACK_POINTER_REGNUM)
2090 cfa_store.offset += offset;
2092 else if (dest == hard_frame_pointer_rtx)
2094 /* Rule 3 */
2095 /* Either setting the FP from an offset of the SP,
2096 or adjusting the FP */
2097 gcc_assert (frame_pointer_needed);
2099 gcc_assert (REG_P (XEXP (src, 0))
2100 && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2101 && GET_CODE (XEXP (src, 1)) == CONST_INT);
2102 offset = INTVAL (XEXP (src, 1));
2103 if (GET_CODE (src) != MINUS)
2104 offset = -offset;
2105 cfa.offset += offset;
2106 cfa.reg = HARD_FRAME_POINTER_REGNUM;
2108 else
2110 gcc_assert (GET_CODE (src) != MINUS);
2112 /* Rule 4 */
2113 if (REG_P (XEXP (src, 0))
2114 && REGNO (XEXP (src, 0)) == cfa.reg
2115 && GET_CODE (XEXP (src, 1)) == CONST_INT)
2117 /* Setting a temporary CFA register that will be copied
2118 into the FP later on. */
2119 offset = - INTVAL (XEXP (src, 1));
2120 cfa.offset += offset;
2121 cfa.reg = REGNO (dest);
2122 /* Or used to save regs to the stack. */
2123 cfa_temp.reg = cfa.reg;
2124 cfa_temp.offset = cfa.offset;
2127 /* Rule 5 */
2128 else if (REG_P (XEXP (src, 0))
2129 && REGNO (XEXP (src, 0)) == cfa_temp.reg
2130 && XEXP (src, 1) == stack_pointer_rtx)
2132 /* Setting a scratch register that we will use instead
2133 of SP for saving registers to the stack. */
2134 gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2135 cfa_store.reg = REGNO (dest);
2136 cfa_store.offset = cfa.offset - cfa_temp.offset;
2139 /* Rule 9 */
2140 else if (GET_CODE (src) == LO_SUM
2141 && GET_CODE (XEXP (src, 1)) == CONST_INT)
2143 cfa_temp.reg = REGNO (dest);
2144 cfa_temp.offset = INTVAL (XEXP (src, 1));
2146 else
2147 gcc_unreachable ();
2149 break;
2151 /* Rule 6 */
2152 case CONST_INT:
2153 cfa_temp.reg = REGNO (dest);
2154 cfa_temp.offset = INTVAL (src);
2155 break;
2157 /* Rule 7 */
2158 case IOR:
2159 gcc_assert (REG_P (XEXP (src, 0))
2160 && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2161 && GET_CODE (XEXP (src, 1)) == CONST_INT);
2163 if ((unsigned) REGNO (dest) != cfa_temp.reg)
2164 cfa_temp.reg = REGNO (dest);
2165 cfa_temp.offset |= INTVAL (XEXP (src, 1));
2166 break;
2168 /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2169 which will fill in all of the bits. */
2170 /* Rule 8 */
2171 case HIGH:
2172 break;
2174 /* Rule 15 */
2175 case UNSPEC:
2176 case UNSPEC_VOLATILE:
2177 gcc_assert (targetm.dwarf_handle_frame_unspec);
2178 targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2179 return;
2181 /* Rule 16 */
2182 case AND:
2183 /* If this AND operation happens on stack pointer in prologue,
2184 we assume the stack is realigned and we extract the
2185 alignment. */
2186 if (fde && XEXP (src, 0) == stack_pointer_rtx)
2188 gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2189 fde->stack_realign = 1;
2190 fde->stack_realignment = INTVAL (XEXP (src, 1));
2191 cfa_store.offset = 0;
2193 if (cfa.reg != STACK_POINTER_REGNUM
2194 && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2195 fde->drap_reg = cfa.reg;
2197 return;
2199 default:
2200 gcc_unreachable ();
2203 def_cfa_1 (label, &cfa);
2204 break;
2206 case MEM:
2208 /* Saving a register to the stack. Make sure dest is relative to the
2209 CFA register. */
2210 switch (GET_CODE (XEXP (dest, 0)))
2212 /* Rule 10 */
2213 /* With a push. */
2214 case PRE_MODIFY:
2215 /* We can't handle variable size modifications. */
2216 gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2217 == CONST_INT);
2218 offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2220 gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2221 && cfa_store.reg == STACK_POINTER_REGNUM);
2223 cfa_store.offset += offset;
2224 if (cfa.reg == STACK_POINTER_REGNUM)
2225 cfa.offset = cfa_store.offset;
2227 offset = -cfa_store.offset;
2228 break;
2230 /* Rule 11 */
2231 case PRE_INC:
2232 case PRE_DEC:
2233 offset = GET_MODE_SIZE (GET_MODE (dest));
2234 if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2235 offset = -offset;
2237 gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2238 == STACK_POINTER_REGNUM)
2239 && cfa_store.reg == STACK_POINTER_REGNUM);
2241 cfa_store.offset += offset;
2243 /* Rule 18: If stack is aligned, we will use FP as a
2244 reference to represent the address of the stored
2245 regiser. */
2246 if (fde
2247 && fde->stack_realign
2248 && src == hard_frame_pointer_rtx)
2250 gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2251 cfa_store.offset = 0;
2254 if (cfa.reg == STACK_POINTER_REGNUM)
2255 cfa.offset = cfa_store.offset;
2257 offset = -cfa_store.offset;
2258 break;
2260 /* Rule 12 */
2261 /* With an offset. */
2262 case PLUS:
2263 case MINUS:
2264 case LO_SUM:
2266 int regno;
2268 gcc_assert (GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT
2269 && REG_P (XEXP (XEXP (dest, 0), 0)));
2270 offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2271 if (GET_CODE (XEXP (dest, 0)) == MINUS)
2272 offset = -offset;
2274 regno = REGNO (XEXP (XEXP (dest, 0), 0));
2276 if (cfa_store.reg == (unsigned) regno)
2277 offset -= cfa_store.offset;
2278 else
2280 gcc_assert (cfa_temp.reg == (unsigned) regno);
2281 offset -= cfa_temp.offset;
2284 break;
2286 /* Rule 13 */
2287 /* Without an offset. */
2288 case REG:
2290 int regno = REGNO (XEXP (dest, 0));
2292 if (cfa_store.reg == (unsigned) regno)
2293 offset = -cfa_store.offset;
2294 else
2296 gcc_assert (cfa_temp.reg == (unsigned) regno);
2297 offset = -cfa_temp.offset;
2300 break;
2302 /* Rule 14 */
2303 case POST_INC:
2304 gcc_assert (cfa_temp.reg
2305 == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2306 offset = -cfa_temp.offset;
2307 cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2308 break;
2310 default:
2311 gcc_unreachable ();
2314 /* Rule 17 */
2315 /* If the source operand of this MEM operation is not a
2316 register, basically the source is return address. Here
2317 we only care how much stack grew and we don't save it. */
2318 if (!REG_P (src))
2319 break;
2321 if (REGNO (src) != STACK_POINTER_REGNUM
2322 && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2323 && (unsigned) REGNO (src) == cfa.reg)
2325 /* We're storing the current CFA reg into the stack. */
2327 if (cfa.offset == 0)
2329 /* Rule 19 */
2330 /* If stack is aligned, putting CFA reg into stack means
2331 we can no longer use reg + offset to represent CFA.
2332 Here we use DW_CFA_def_cfa_expression instead. The
2333 result of this expression equals to the original CFA
2334 value. */
2335 if (fde
2336 && fde->stack_realign
2337 && cfa.indirect == 0
2338 && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2340 dw_cfa_location cfa_exp;
2342 gcc_assert (fde->drap_reg == cfa.reg);
2344 cfa_exp.indirect = 1;
2345 cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2346 cfa_exp.base_offset = offset;
2347 cfa_exp.offset = 0;
2349 fde->drap_reg_saved = 1;
2351 def_cfa_1 (label, &cfa_exp);
2352 break;
2355 /* If the source register is exactly the CFA, assume
2356 we're saving SP like any other register; this happens
2357 on the ARM. */
2358 def_cfa_1 (label, &cfa);
2359 queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2360 break;
2362 else
2364 /* Otherwise, we'll need to look in the stack to
2365 calculate the CFA. */
2366 rtx x = XEXP (dest, 0);
2368 if (!REG_P (x))
2369 x = XEXP (x, 0);
2370 gcc_assert (REG_P (x));
2372 cfa.reg = REGNO (x);
2373 cfa.base_offset = offset;
2374 cfa.indirect = 1;
2375 def_cfa_1 (label, &cfa);
2376 break;
2380 def_cfa_1 (label, &cfa);
2382 span = targetm.dwarf_register_span (src);
2384 if (!span)
2385 queue_reg_save (label, src, NULL_RTX, offset);
2386 else
2388 /* We have a PARALLEL describing where the contents of SRC
2389 live. Queue register saves for each piece of the
2390 PARALLEL. */
2391 int par_index;
2392 int limit;
2393 HOST_WIDE_INT span_offset = offset;
2395 gcc_assert (GET_CODE (span) == PARALLEL);
2397 limit = XVECLEN (span, 0);
2398 for (par_index = 0; par_index < limit; par_index++)
2400 rtx elem = XVECEXP (span, 0, par_index);
2402 queue_reg_save (label, elem, NULL_RTX, span_offset);
2403 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2407 break;
2409 default:
2410 gcc_unreachable ();
2414 /* Record call frame debugging information for INSN, which either
2415 sets SP or FP (adjusting how we calculate the frame address) or saves a
2416 register to the stack. If INSN is NULL_RTX, initialize our state.
2418 If AFTER_P is false, we're being called before the insn is emitted,
2419 otherwise after. Call instructions get invoked twice. */
2421 void
2422 dwarf2out_frame_debug (rtx insn, bool after_p)
2424 const char *label;
2425 rtx src;
2427 if (insn == NULL_RTX)
2429 size_t i;
2431 /* Flush any queued register saves. */
2432 flush_queued_reg_saves ();
2434 /* Set up state for generating call frame debug info. */
2435 lookup_cfa (&cfa);
2436 gcc_assert (cfa.reg
2437 == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2439 cfa.reg = STACK_POINTER_REGNUM;
2440 cfa_store = cfa;
2441 cfa_temp.reg = -1;
2442 cfa_temp.offset = 0;
2444 for (i = 0; i < num_regs_saved_in_regs; i++)
2446 regs_saved_in_regs[i].orig_reg = NULL_RTX;
2447 regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2449 num_regs_saved_in_regs = 0;
2451 if (barrier_args_size)
2453 XDELETEVEC (barrier_args_size);
2454 barrier_args_size = NULL;
2456 return;
2459 if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2460 flush_queued_reg_saves ();
2462 if (! RTX_FRAME_RELATED_P (insn))
2464 if (!ACCUMULATE_OUTGOING_ARGS)
2465 dwarf2out_stack_adjust (insn, after_p);
2466 return;
2469 label = dwarf2out_cfi_label (false);
2470 src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
2471 if (src)
2472 insn = XEXP (src, 0);
2473 else
2474 insn = PATTERN (insn);
2476 dwarf2out_frame_debug_expr (insn, label);
2479 #endif
2481 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
2482 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
2483 (enum dwarf_call_frame_info cfi);
2485 static enum dw_cfi_oprnd_type
2486 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
2488 switch (cfi)
2490 case DW_CFA_nop:
2491 case DW_CFA_GNU_window_save:
2492 return dw_cfi_oprnd_unused;
2494 case DW_CFA_set_loc:
2495 case DW_CFA_advance_loc1:
2496 case DW_CFA_advance_loc2:
2497 case DW_CFA_advance_loc4:
2498 case DW_CFA_MIPS_advance_loc8:
2499 return dw_cfi_oprnd_addr;
2501 case DW_CFA_offset:
2502 case DW_CFA_offset_extended:
2503 case DW_CFA_def_cfa:
2504 case DW_CFA_offset_extended_sf:
2505 case DW_CFA_def_cfa_sf:
2506 case DW_CFA_restore_extended:
2507 case DW_CFA_undefined:
2508 case DW_CFA_same_value:
2509 case DW_CFA_def_cfa_register:
2510 case DW_CFA_register:
2511 return dw_cfi_oprnd_reg_num;
2513 case DW_CFA_def_cfa_offset:
2514 case DW_CFA_GNU_args_size:
2515 case DW_CFA_def_cfa_offset_sf:
2516 return dw_cfi_oprnd_offset;
2518 case DW_CFA_def_cfa_expression:
2519 case DW_CFA_expression:
2520 return dw_cfi_oprnd_loc;
2522 default:
2523 gcc_unreachable ();
2527 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
2528 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
2529 (enum dwarf_call_frame_info cfi);
2531 static enum dw_cfi_oprnd_type
2532 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
2534 switch (cfi)
2536 case DW_CFA_def_cfa:
2537 case DW_CFA_def_cfa_sf:
2538 case DW_CFA_offset:
2539 case DW_CFA_offset_extended_sf:
2540 case DW_CFA_offset_extended:
2541 return dw_cfi_oprnd_offset;
2543 case DW_CFA_register:
2544 return dw_cfi_oprnd_reg_num;
2546 default:
2547 return dw_cfi_oprnd_unused;
2551 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2553 /* Switch to eh_frame_section. If we don't have an eh_frame_section,
2554 switch to the data section instead, and write out a synthetic label
2555 for collect2. */
2557 static void
2558 switch_to_eh_frame_section (void)
2560 tree label;
2562 #ifdef EH_FRAME_SECTION_NAME
2563 if (eh_frame_section == 0)
2565 int flags;
2567 if (EH_TABLES_CAN_BE_READ_ONLY)
2569 int fde_encoding;
2570 int per_encoding;
2571 int lsda_encoding;
2573 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2574 /*global=*/0);
2575 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2576 /*global=*/1);
2577 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2578 /*global=*/0);
2579 flags = ((! flag_pic
2580 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2581 && (fde_encoding & 0x70) != DW_EH_PE_aligned
2582 && (per_encoding & 0x70) != DW_EH_PE_absptr
2583 && (per_encoding & 0x70) != DW_EH_PE_aligned
2584 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2585 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2586 ? 0 : SECTION_WRITE);
2588 else
2589 flags = SECTION_WRITE;
2590 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2592 #endif
2594 if (eh_frame_section)
2595 switch_to_section (eh_frame_section);
2596 else
2598 /* We have no special eh_frame section. Put the information in
2599 the data section and emit special labels to guide collect2. */
2600 switch_to_section (data_section);
2601 label = get_file_function_name ("F");
2602 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2603 targetm.asm_out.globalize_label (asm_out_file,
2604 IDENTIFIER_POINTER (label));
2605 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
2609 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder. */
2611 static HOST_WIDE_INT
2612 div_data_align (HOST_WIDE_INT off)
2614 HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
2615 gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
2616 return r;
2619 /* Output a Call Frame Information opcode and its operand(s). */
2621 static void
2622 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
2624 unsigned long r;
2625 HOST_WIDE_INT off;
2627 if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
2628 dw2_asm_output_data (1, (cfi->dw_cfi_opc
2629 | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
2630 "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
2631 ((unsigned HOST_WIDE_INT)
2632 cfi->dw_cfi_oprnd1.dw_cfi_offset));
2633 else if (cfi->dw_cfi_opc == DW_CFA_offset)
2635 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2636 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2637 "DW_CFA_offset, column 0x%lx", r);
2638 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2639 dw2_asm_output_data_uleb128 (off, NULL);
2641 else if (cfi->dw_cfi_opc == DW_CFA_restore)
2643 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2644 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2645 "DW_CFA_restore, column 0x%lx", r);
2647 else
2649 dw2_asm_output_data (1, cfi->dw_cfi_opc,
2650 "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
2652 switch (cfi->dw_cfi_opc)
2654 case DW_CFA_set_loc:
2655 if (for_eh)
2656 dw2_asm_output_encoded_addr_rtx (
2657 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
2658 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
2659 false, NULL);
2660 else
2661 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2662 cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
2663 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2664 break;
2666 case DW_CFA_advance_loc1:
2667 dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2668 fde->dw_fde_current_label, NULL);
2669 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2670 break;
2672 case DW_CFA_advance_loc2:
2673 dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2674 fde->dw_fde_current_label, NULL);
2675 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2676 break;
2678 case DW_CFA_advance_loc4:
2679 dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2680 fde->dw_fde_current_label, NULL);
2681 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2682 break;
2684 case DW_CFA_MIPS_advance_loc8:
2685 dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2686 fde->dw_fde_current_label, NULL);
2687 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2688 break;
2690 case DW_CFA_offset_extended:
2691 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2692 dw2_asm_output_data_uleb128 (r, NULL);
2693 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2694 dw2_asm_output_data_uleb128 (off, NULL);
2695 break;
2697 case DW_CFA_def_cfa:
2698 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2699 dw2_asm_output_data_uleb128 (r, NULL);
2700 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2701 break;
2703 case DW_CFA_offset_extended_sf:
2704 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2705 dw2_asm_output_data_uleb128 (r, NULL);
2706 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2707 dw2_asm_output_data_sleb128 (off, NULL);
2708 break;
2710 case DW_CFA_def_cfa_sf:
2711 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2712 dw2_asm_output_data_uleb128 (r, NULL);
2713 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2714 dw2_asm_output_data_sleb128 (off, NULL);
2715 break;
2717 case DW_CFA_restore_extended:
2718 case DW_CFA_undefined:
2719 case DW_CFA_same_value:
2720 case DW_CFA_def_cfa_register:
2721 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2722 dw2_asm_output_data_uleb128 (r, NULL);
2723 break;
2725 case DW_CFA_register:
2726 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2727 dw2_asm_output_data_uleb128 (r, NULL);
2728 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
2729 dw2_asm_output_data_uleb128 (r, NULL);
2730 break;
2732 case DW_CFA_def_cfa_offset:
2733 case DW_CFA_GNU_args_size:
2734 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2735 break;
2737 case DW_CFA_def_cfa_offset_sf:
2738 off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
2739 dw2_asm_output_data_sleb128 (off, NULL);
2740 break;
2742 case DW_CFA_GNU_window_save:
2743 break;
2745 case DW_CFA_def_cfa_expression:
2746 case DW_CFA_expression:
2747 output_cfa_loc (cfi);
2748 break;
2750 case DW_CFA_GNU_negative_offset_extended:
2751 /* Obsoleted by DW_CFA_offset_extended_sf. */
2752 gcc_unreachable ();
2754 default:
2755 break;
2760 /* Similar, but do it via assembler directives instead. */
2762 static void
2763 output_cfi_directive (dw_cfi_ref cfi)
2765 unsigned long r, r2;
2767 switch (cfi->dw_cfi_opc)
2769 case DW_CFA_advance_loc:
2770 case DW_CFA_advance_loc1:
2771 case DW_CFA_advance_loc2:
2772 case DW_CFA_advance_loc4:
2773 case DW_CFA_MIPS_advance_loc8:
2774 case DW_CFA_set_loc:
2775 /* Should only be created by add_fde_cfi in a code path not
2776 followed when emitting via directives. The assembler is
2777 going to take care of this for us. */
2778 gcc_unreachable ();
2780 case DW_CFA_offset:
2781 case DW_CFA_offset_extended:
2782 case DW_CFA_offset_extended_sf:
2783 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
2784 fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
2785 r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
2786 break;
2788 case DW_CFA_restore:
2789 case DW_CFA_restore_extended:
2790 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
2791 fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
2792 break;
2794 case DW_CFA_undefined:
2795 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
2796 fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
2797 break;
2799 case DW_CFA_same_value:
2800 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
2801 fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
2802 break;
2804 case DW_CFA_def_cfa:
2805 case DW_CFA_def_cfa_sf:
2806 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
2807 fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
2808 r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
2809 break;
2811 case DW_CFA_def_cfa_register:
2812 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
2813 fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
2814 break;
2816 case DW_CFA_register:
2817 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
2818 r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
2819 fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
2820 break;
2822 case DW_CFA_def_cfa_offset:
2823 case DW_CFA_def_cfa_offset_sf:
2824 fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
2825 HOST_WIDE_INT_PRINT_DEC"\n",
2826 cfi->dw_cfi_oprnd1.dw_cfi_offset);
2827 break;
2829 case DW_CFA_GNU_args_size:
2830 fprintf (asm_out_file, "\t.cfi_escape 0x%x,", DW_CFA_GNU_args_size);
2831 dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
2832 if (flag_debug_asm)
2833 fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
2834 ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
2835 fputc ('\n', asm_out_file);
2836 break;
2838 case DW_CFA_GNU_window_save:
2839 fprintf (asm_out_file, "\t.cfi_window_save\n");
2840 break;
2842 case DW_CFA_def_cfa_expression:
2843 case DW_CFA_expression:
2844 fprintf (asm_out_file, "\t.cfi_escape 0x%x,", cfi->dw_cfi_opc);
2845 output_cfa_loc_raw (cfi);
2846 fputc ('\n', asm_out_file);
2847 break;
2849 default:
2850 gcc_unreachable ();
2854 /* Output the call frame information used to record information
2855 that relates to calculating the frame pointer, and records the
2856 location of saved registers. */
2858 static void
2859 output_call_frame_info (int for_eh)
2861 unsigned int i;
2862 dw_fde_ref fde;
2863 dw_cfi_ref cfi;
2864 char l1[20], l2[20], section_start_label[20];
2865 bool any_lsda_needed = false;
2866 char augmentation[6];
2867 int augmentation_size;
2868 int fde_encoding = DW_EH_PE_absptr;
2869 int per_encoding = DW_EH_PE_absptr;
2870 int lsda_encoding = DW_EH_PE_absptr;
2871 int return_reg;
2873 /* Don't emit a CIE if there won't be any FDEs. */
2874 if (fde_table_in_use == 0)
2875 return;
2877 /* Nothing to do if the assembler's doing it all. */
2878 if (dwarf2out_do_cfi_asm ())
2879 return;
2881 /* If we make FDEs linkonce, we may have to emit an empty label for
2882 an FDE that wouldn't otherwise be emitted. We want to avoid
2883 having an FDE kept around when the function it refers to is
2884 discarded. Example where this matters: a primary function
2885 template in C++ requires EH information, but an explicit
2886 specialization doesn't. */
2887 if (TARGET_USES_WEAK_UNWIND_INFO
2888 && ! flag_asynchronous_unwind_tables
2889 && flag_exceptions
2890 && for_eh)
2891 for (i = 0; i < fde_table_in_use; i++)
2892 if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
2893 && !fde_table[i].uses_eh_lsda
2894 && ! DECL_WEAK (fde_table[i].decl))
2895 targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
2896 for_eh, /* empty */ 1);
2898 /* If we don't have any functions we'll want to unwind out of, don't
2899 emit any EH unwind information. Note that if exceptions aren't
2900 enabled, we won't have collected nothrow information, and if we
2901 asked for asynchronous tables, we always want this info. */
2902 if (for_eh)
2904 bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
2906 for (i = 0; i < fde_table_in_use; i++)
2907 if (fde_table[i].uses_eh_lsda)
2908 any_eh_needed = any_lsda_needed = true;
2909 else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2910 any_eh_needed = true;
2911 else if (! fde_table[i].nothrow
2912 && ! fde_table[i].all_throwers_are_sibcalls)
2913 any_eh_needed = true;
2915 if (! any_eh_needed)
2916 return;
2919 /* We're going to be generating comments, so turn on app. */
2920 if (flag_debug_asm)
2921 app_enable ();
2923 if (for_eh)
2924 switch_to_eh_frame_section ();
2925 else
2927 if (!debug_frame_section)
2928 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
2929 SECTION_DEBUG, NULL);
2930 switch_to_section (debug_frame_section);
2933 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
2934 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
2936 /* Output the CIE. */
2937 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
2938 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
2939 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2940 dw2_asm_output_data (4, 0xffffffff,
2941 "Initial length escape value indicating 64-bit DWARF extension");
2942 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2943 "Length of Common Information Entry");
2944 ASM_OUTPUT_LABEL (asm_out_file, l1);
2946 /* Now that the CIE pointer is PC-relative for EH,
2947 use 0 to identify the CIE. */
2948 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
2949 (for_eh ? 0 : DWARF_CIE_ID),
2950 "CIE Identifier Tag");
2952 dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
2954 augmentation[0] = 0;
2955 augmentation_size = 0;
2956 if (for_eh)
2958 char *p;
2960 /* Augmentation:
2961 z Indicates that a uleb128 is present to size the
2962 augmentation section.
2963 L Indicates the encoding (and thus presence) of
2964 an LSDA pointer in the FDE augmentation.
2965 R Indicates a non-default pointer encoding for
2966 FDE code pointers.
2967 P Indicates the presence of an encoding + language
2968 personality routine in the CIE augmentation. */
2970 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
2971 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
2972 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
2974 p = augmentation + 1;
2975 if (eh_personality_libfunc)
2977 *p++ = 'P';
2978 augmentation_size += 1 + size_of_encoded_value (per_encoding);
2979 assemble_external_libcall (eh_personality_libfunc);
2981 if (any_lsda_needed)
2983 *p++ = 'L';
2984 augmentation_size += 1;
2986 if (fde_encoding != DW_EH_PE_absptr)
2988 *p++ = 'R';
2989 augmentation_size += 1;
2991 if (p > augmentation + 1)
2993 augmentation[0] = 'z';
2994 *p = '\0';
2997 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
2998 if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
3000 int offset = ( 4 /* Length */
3001 + 4 /* CIE Id */
3002 + 1 /* CIE version */
3003 + strlen (augmentation) + 1 /* Augmentation */
3004 + size_of_uleb128 (1) /* Code alignment */
3005 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3006 + 1 /* RA column */
3007 + 1 /* Augmentation size */
3008 + 1 /* Personality encoding */ );
3009 int pad = -offset & (PTR_SIZE - 1);
3011 augmentation_size += pad;
3013 /* Augmentations should be small, so there's scarce need to
3014 iterate for a solution. Die if we exceed one uleb128 byte. */
3015 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3019 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3020 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3021 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3022 "CIE Data Alignment Factor");
3024 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3025 if (DW_CIE_VERSION == 1)
3026 dw2_asm_output_data (1, return_reg, "CIE RA Column");
3027 else
3028 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3030 if (augmentation[0])
3032 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
3033 if (eh_personality_libfunc)
3035 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
3036 eh_data_format_name (per_encoding));
3037 dw2_asm_output_encoded_addr_rtx (per_encoding,
3038 eh_personality_libfunc,
3039 true, NULL);
3042 if (any_lsda_needed)
3043 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
3044 eh_data_format_name (lsda_encoding));
3046 if (fde_encoding != DW_EH_PE_absptr)
3047 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
3048 eh_data_format_name (fde_encoding));
3051 for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
3052 output_cfi (cfi, NULL, for_eh);
3054 /* Pad the CIE out to an address sized boundary. */
3055 ASM_OUTPUT_ALIGN (asm_out_file,
3056 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3057 ASM_OUTPUT_LABEL (asm_out_file, l2);
3059 /* Loop through all of the FDE's. */
3060 for (i = 0; i < fde_table_in_use; i++)
3062 fde = &fde_table[i];
3064 /* Don't emit EH unwind info for leaf functions that don't need it. */
3065 if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
3066 && (fde->nothrow || fde->all_throwers_are_sibcalls)
3067 && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
3068 && !fde->uses_eh_lsda)
3069 continue;
3071 targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
3072 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
3073 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
3074 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
3075 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3076 dw2_asm_output_data (4, 0xffffffff,
3077 "Initial length escape value indicating 64-bit DWARF extension");
3078 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3079 "FDE Length");
3080 ASM_OUTPUT_LABEL (asm_out_file, l1);
3082 if (for_eh)
3083 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3084 else
3085 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3086 debug_frame_section, "FDE CIE offset");
3088 if (for_eh)
3090 if (fde->dw_fde_switched_sections)
3092 rtx sym_ref2 = gen_rtx_SYMBOL_REF (Pmode,
3093 fde->dw_fde_unlikely_section_label);
3094 rtx sym_ref3= gen_rtx_SYMBOL_REF (Pmode,
3095 fde->dw_fde_hot_section_label);
3096 SYMBOL_REF_FLAGS (sym_ref2) |= SYMBOL_FLAG_LOCAL;
3097 SYMBOL_REF_FLAGS (sym_ref3) |= SYMBOL_FLAG_LOCAL;
3098 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref3, false,
3099 "FDE initial location");
3100 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3101 fde->dw_fde_hot_section_end_label,
3102 fde->dw_fde_hot_section_label,
3103 "FDE address range");
3104 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref2, false,
3105 "FDE initial location");
3106 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3107 fde->dw_fde_unlikely_section_end_label,
3108 fde->dw_fde_unlikely_section_label,
3109 "FDE address range");
3111 else
3113 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
3114 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3115 dw2_asm_output_encoded_addr_rtx (fde_encoding,
3116 sym_ref,
3117 false,
3118 "FDE initial location");
3119 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3120 fde->dw_fde_end, fde->dw_fde_begin,
3121 "FDE address range");
3124 else
3126 if (fde->dw_fde_switched_sections)
3128 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3129 fde->dw_fde_hot_section_label,
3130 "FDE initial location");
3131 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3132 fde->dw_fde_hot_section_end_label,
3133 fde->dw_fde_hot_section_label,
3134 "FDE address range");
3135 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3136 fde->dw_fde_unlikely_section_label,
3137 "FDE initial location");
3138 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3139 fde->dw_fde_unlikely_section_end_label,
3140 fde->dw_fde_unlikely_section_label,
3141 "FDE address range");
3143 else
3145 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
3146 "FDE initial location");
3147 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3148 fde->dw_fde_end, fde->dw_fde_begin,
3149 "FDE address range");
3153 if (augmentation[0])
3155 if (any_lsda_needed)
3157 int size = size_of_encoded_value (lsda_encoding);
3159 if (lsda_encoding == DW_EH_PE_aligned)
3161 int offset = ( 4 /* Length */
3162 + 4 /* CIE offset */
3163 + 2 * size_of_encoded_value (fde_encoding)
3164 + 1 /* Augmentation size */ );
3165 int pad = -offset & (PTR_SIZE - 1);
3167 size += pad;
3168 gcc_assert (size_of_uleb128 (size) == 1);
3171 dw2_asm_output_data_uleb128 (size, "Augmentation size");
3173 if (fde->uses_eh_lsda)
3175 ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
3176 fde->funcdef_number);
3177 dw2_asm_output_encoded_addr_rtx (
3178 lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
3179 false, "Language Specific Data Area");
3181 else
3183 if (lsda_encoding == DW_EH_PE_aligned)
3184 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3185 dw2_asm_output_data
3186 (size_of_encoded_value (lsda_encoding), 0,
3187 "Language Specific Data Area (none)");
3190 else
3191 dw2_asm_output_data_uleb128 (0, "Augmentation size");
3194 /* Loop through the Call Frame Instructions associated with
3195 this FDE. */
3196 fde->dw_fde_current_label = fde->dw_fde_begin;
3197 for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3198 output_cfi (cfi, fde, for_eh);
3200 /* Pad the FDE out to an address sized boundary. */
3201 ASM_OUTPUT_ALIGN (asm_out_file,
3202 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3203 ASM_OUTPUT_LABEL (asm_out_file, l2);
3206 if (for_eh && targetm.terminate_dw2_eh_frame_info)
3207 dw2_asm_output_data (4, 0, "End of Table");
3208 #ifdef MIPS_DEBUGGING_INFO
3209 /* Work around Irix 6 assembler bug whereby labels at the end of a section
3210 get a value of 0. Putting .align 0 after the label fixes it. */
3211 ASM_OUTPUT_ALIGN (asm_out_file, 0);
3212 #endif
3214 /* Turn off app to make assembly quicker. */
3215 if (flag_debug_asm)
3216 app_disable ();
3219 /* Output a marker (i.e. a label) for the beginning of a function, before
3220 the prologue. */
3222 void
3223 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
3224 const char *file ATTRIBUTE_UNUSED)
3226 char label[MAX_ARTIFICIAL_LABEL_BYTES];
3227 char * dup_label;
3228 dw_fde_ref fde;
3230 current_function_func_begin_label = NULL;
3232 #ifdef TARGET_UNWIND_INFO
3233 /* ??? current_function_func_begin_label is also used by except.c
3234 for call-site information. We must emit this label if it might
3235 be used. */
3236 if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
3237 && ! dwarf2out_do_frame ())
3238 return;
3239 #else
3240 if (! dwarf2out_do_frame ())
3241 return;
3242 #endif
3244 switch_to_section (function_section (current_function_decl));
3245 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
3246 current_function_funcdef_no);
3247 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
3248 current_function_funcdef_no);
3249 dup_label = xstrdup (label);
3250 current_function_func_begin_label = dup_label;
3252 #ifdef TARGET_UNWIND_INFO
3253 /* We can elide the fde allocation if we're not emitting debug info. */
3254 if (! dwarf2out_do_frame ())
3255 return;
3256 #endif
3258 /* Expand the fde table if necessary. */
3259 if (fde_table_in_use == fde_table_allocated)
3261 fde_table_allocated += FDE_TABLE_INCREMENT;
3262 fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
3263 memset (fde_table + fde_table_in_use, 0,
3264 FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
3267 /* Record the FDE associated with this function. */
3268 current_funcdef_fde = fde_table_in_use;
3270 /* Add the new FDE at the end of the fde_table. */
3271 fde = &fde_table[fde_table_in_use++];
3272 fde->decl = current_function_decl;
3273 fde->dw_fde_begin = dup_label;
3274 fde->dw_fde_current_label = dup_label;
3275 fde->dw_fde_hot_section_label = NULL;
3276 fde->dw_fde_hot_section_end_label = NULL;
3277 fde->dw_fde_unlikely_section_label = NULL;
3278 fde->dw_fde_unlikely_section_end_label = NULL;
3279 fde->dw_fde_switched_sections = false;
3280 fde->dw_fde_end = NULL;
3281 fde->dw_fde_cfi = NULL;
3282 fde->funcdef_number = current_function_funcdef_no;
3283 fde->nothrow = TREE_NOTHROW (current_function_decl);
3284 fde->uses_eh_lsda = crtl->uses_eh_lsda;
3285 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
3286 fde->drap_reg = INVALID_REGNUM;
3287 fde->vdrap_reg = INVALID_REGNUM;
3289 args_size = old_args_size = 0;
3291 /* We only want to output line number information for the genuine dwarf2
3292 prologue case, not the eh frame case. */
3293 #ifdef DWARF2_DEBUGGING_INFO
3294 if (file)
3295 dwarf2out_source_line (line, file);
3296 #endif
3298 if (dwarf2out_do_cfi_asm ())
3300 int enc;
3301 rtx ref;
3303 fprintf (asm_out_file, "\t.cfi_startproc\n");
3305 if (eh_personality_libfunc)
3307 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3308 ref = eh_personality_libfunc;
3310 /* ??? The GAS support isn't entirely consistent. We have to
3311 handle indirect support ourselves, but PC-relative is done
3312 in the assembler. Further, the assembler can't handle any
3313 of the weirder relocation types. */
3314 if (enc & DW_EH_PE_indirect)
3315 ref = dw2_force_const_mem (ref, true);
3317 fprintf (asm_out_file, "\t.cfi_personality 0x%x,", enc);
3318 output_addr_const (asm_out_file, ref);
3319 fputc ('\n', asm_out_file);
3322 if (crtl->uses_eh_lsda)
3324 char lab[20];
3326 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3327 ASM_GENERATE_INTERNAL_LABEL (lab, "LLSDA",
3328 current_function_funcdef_no);
3329 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
3330 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
3332 if (enc & DW_EH_PE_indirect)
3333 ref = dw2_force_const_mem (ref, true);
3335 fprintf (asm_out_file, "\t.cfi_lsda 0x%x,", enc);
3336 output_addr_const (asm_out_file, ref);
3337 fputc ('\n', asm_out_file);
3342 /* Output a marker (i.e. a label) for the absolute end of the generated code
3343 for a function definition. This gets called *after* the epilogue code has
3344 been generated. */
3346 void
3347 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
3348 const char *file ATTRIBUTE_UNUSED)
3350 dw_fde_ref fde;
3351 char label[MAX_ARTIFICIAL_LABEL_BYTES];
3353 if (dwarf2out_do_cfi_asm ())
3354 fprintf (asm_out_file, "\t.cfi_endproc\n");
3356 /* Output a label to mark the endpoint of the code generated for this
3357 function. */
3358 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
3359 current_function_funcdef_no);
3360 ASM_OUTPUT_LABEL (asm_out_file, label);
3361 fde = current_fde ();
3362 gcc_assert (fde != NULL);
3363 fde->dw_fde_end = xstrdup (label);
3366 void
3367 dwarf2out_frame_init (void)
3369 /* Allocate the initial hunk of the fde_table. */
3370 fde_table = GGC_CNEWVEC (dw_fde_node, FDE_TABLE_INCREMENT);
3371 fde_table_allocated = FDE_TABLE_INCREMENT;
3372 fde_table_in_use = 0;
3374 /* Generate the CFA instructions common to all FDE's. Do it now for the
3375 sake of lookup_cfa. */
3377 /* On entry, the Canonical Frame Address is at SP. */
3378 dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
3380 #ifdef DWARF2_UNWIND_INFO
3381 if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
3382 initial_return_save (INCOMING_RETURN_ADDR_RTX);
3383 #endif
3386 void
3387 dwarf2out_frame_finish (void)
3389 /* Output call frame information. */
3390 if (DWARF2_FRAME_INFO)
3391 output_call_frame_info (0);
3393 #ifndef TARGET_UNWIND_INFO
3394 /* Output another copy for the unwinder. */
3395 if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
3396 output_call_frame_info (1);
3397 #endif
3400 /* Note that the current function section is being used for code. */
3402 static void
3403 dwarf2out_note_section_used (void)
3405 section *sec = current_function_section ();
3406 if (sec == text_section)
3407 text_section_used = true;
3408 else if (sec == cold_text_section)
3409 cold_text_section_used = true;
3412 void
3413 dwarf2out_switch_text_section (void)
3415 dw_fde_ref fde = current_fde ();
3417 gcc_assert (cfun && fde);
3419 fde->dw_fde_switched_sections = true;
3420 fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
3421 fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
3422 fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
3423 fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
3424 have_multiple_function_sections = true;
3426 /* Reset the current label on switching text sections, so that we
3427 don't attempt to advance_loc4 between labels in different sections. */
3428 fde->dw_fde_current_label = NULL;
3430 /* There is no need to mark used sections when not debugging. */
3431 if (cold_text_section != NULL)
3432 dwarf2out_note_section_used ();
3434 #endif
3436 /* And now, the subset of the debugging information support code necessary
3437 for emitting location expressions. */
3439 /* Data about a single source file. */
3440 struct dwarf_file_data GTY(())
3442 const char * filename;
3443 int emitted_number;
3446 /* We need some way to distinguish DW_OP_addr with a direct symbol
3447 relocation from DW_OP_addr with a dtp-relative symbol relocation. */
3448 #define INTERNAL_DW_OP_tls_addr (0x100 + DW_OP_addr)
3451 typedef struct dw_val_struct *dw_val_ref;
3452 typedef struct die_struct *dw_die_ref;
3453 typedef const struct die_struct *const_dw_die_ref;
3454 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
3455 typedef struct dw_loc_list_struct *dw_loc_list_ref;
3457 typedef struct deferred_locations_struct GTY(())
3459 tree variable;
3460 dw_die_ref die;
3461 } deferred_locations;
3463 DEF_VEC_O(deferred_locations);
3464 DEF_VEC_ALLOC_O(deferred_locations,gc);
3466 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
3468 /* Each DIE may have a series of attribute/value pairs. Values
3469 can take on several forms. The forms that are used in this
3470 implementation are listed below. */
3472 enum dw_val_class
3474 dw_val_class_addr,
3475 dw_val_class_offset,
3476 dw_val_class_loc,
3477 dw_val_class_loc_list,
3478 dw_val_class_range_list,
3479 dw_val_class_const,
3480 dw_val_class_unsigned_const,
3481 dw_val_class_long_long,
3482 dw_val_class_vec,
3483 dw_val_class_flag,
3484 dw_val_class_die_ref,
3485 dw_val_class_fde_ref,
3486 dw_val_class_lbl_id,
3487 dw_val_class_lineptr,
3488 dw_val_class_str,
3489 dw_val_class_macptr,
3490 dw_val_class_file
3493 /* Describe a double word constant value. */
3494 /* ??? Every instance of long_long in the code really means CONST_DOUBLE. */
3496 typedef struct dw_long_long_struct GTY(())
3498 unsigned long hi;
3499 unsigned long low;
3501 dw_long_long_const;
3503 /* Describe a floating point constant value, or a vector constant value. */
3505 typedef struct dw_vec_struct GTY(())
3507 unsigned char * GTY((length ("%h.length"))) array;
3508 unsigned length;
3509 unsigned elt_size;
3511 dw_vec_const;
3513 /* The dw_val_node describes an attribute's value, as it is
3514 represented internally. */
3516 typedef struct dw_val_struct GTY(())
3518 enum dw_val_class val_class;
3519 union dw_val_struct_union
3521 rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
3522 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
3523 dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
3524 dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
3525 HOST_WIDE_INT GTY ((default)) val_int;
3526 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
3527 dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
3528 dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
3529 struct dw_val_die_union
3531 dw_die_ref die;
3532 int external;
3533 } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
3534 unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
3535 struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
3536 char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
3537 unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
3538 struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
3540 GTY ((desc ("%1.val_class"))) v;
3542 dw_val_node;
3544 /* Locations in memory are described using a sequence of stack machine
3545 operations. */
3547 typedef struct dw_loc_descr_struct GTY(())
3549 dw_loc_descr_ref dw_loc_next;
3550 enum dwarf_location_atom dw_loc_opc;
3551 int dw_loc_addr;
3552 dw_val_node dw_loc_oprnd1;
3553 dw_val_node dw_loc_oprnd2;
3555 dw_loc_descr_node;
3557 /* Location lists are ranges + location descriptions for that range,
3558 so you can track variables that are in different places over
3559 their entire life. */
3560 typedef struct dw_loc_list_struct GTY(())
3562 dw_loc_list_ref dw_loc_next;
3563 const char *begin; /* Label for begin address of range */
3564 const char *end; /* Label for end address of range */
3565 char *ll_symbol; /* Label for beginning of location list.
3566 Only on head of list */
3567 const char *section; /* Section this loclist is relative to */
3568 dw_loc_descr_ref expr;
3569 } dw_loc_list_node;
3571 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
3573 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
3575 /* Convert a DWARF stack opcode into its string name. */
3577 static const char *
3578 dwarf_stack_op_name (unsigned int op)
3580 switch (op)
3582 case DW_OP_addr:
3583 case INTERNAL_DW_OP_tls_addr:
3584 return "DW_OP_addr";
3585 case DW_OP_deref:
3586 return "DW_OP_deref";
3587 case DW_OP_const1u:
3588 return "DW_OP_const1u";
3589 case DW_OP_const1s:
3590 return "DW_OP_const1s";
3591 case DW_OP_const2u:
3592 return "DW_OP_const2u";
3593 case DW_OP_const2s:
3594 return "DW_OP_const2s";
3595 case DW_OP_const4u:
3596 return "DW_OP_const4u";
3597 case DW_OP_const4s:
3598 return "DW_OP_const4s";
3599 case DW_OP_const8u:
3600 return "DW_OP_const8u";
3601 case DW_OP_const8s:
3602 return "DW_OP_const8s";
3603 case DW_OP_constu:
3604 return "DW_OP_constu";
3605 case DW_OP_consts:
3606 return "DW_OP_consts";
3607 case DW_OP_dup:
3608 return "DW_OP_dup";
3609 case DW_OP_drop:
3610 return "DW_OP_drop";
3611 case DW_OP_over:
3612 return "DW_OP_over";
3613 case DW_OP_pick:
3614 return "DW_OP_pick";
3615 case DW_OP_swap:
3616 return "DW_OP_swap";
3617 case DW_OP_rot:
3618 return "DW_OP_rot";
3619 case DW_OP_xderef:
3620 return "DW_OP_xderef";
3621 case DW_OP_abs:
3622 return "DW_OP_abs";
3623 case DW_OP_and:
3624 return "DW_OP_and";
3625 case DW_OP_div:
3626 return "DW_OP_div";
3627 case DW_OP_minus:
3628 return "DW_OP_minus";
3629 case DW_OP_mod:
3630 return "DW_OP_mod";
3631 case DW_OP_mul:
3632 return "DW_OP_mul";
3633 case DW_OP_neg:
3634 return "DW_OP_neg";
3635 case DW_OP_not:
3636 return "DW_OP_not";
3637 case DW_OP_or:
3638 return "DW_OP_or";
3639 case DW_OP_plus:
3640 return "DW_OP_plus";
3641 case DW_OP_plus_uconst:
3642 return "DW_OP_plus_uconst";
3643 case DW_OP_shl:
3644 return "DW_OP_shl";
3645 case DW_OP_shr:
3646 return "DW_OP_shr";
3647 case DW_OP_shra:
3648 return "DW_OP_shra";
3649 case DW_OP_xor:
3650 return "DW_OP_xor";
3651 case DW_OP_bra:
3652 return "DW_OP_bra";
3653 case DW_OP_eq:
3654 return "DW_OP_eq";
3655 case DW_OP_ge:
3656 return "DW_OP_ge";
3657 case DW_OP_gt:
3658 return "DW_OP_gt";
3659 case DW_OP_le:
3660 return "DW_OP_le";
3661 case DW_OP_lt:
3662 return "DW_OP_lt";
3663 case DW_OP_ne:
3664 return "DW_OP_ne";
3665 case DW_OP_skip:
3666 return "DW_OP_skip";
3667 case DW_OP_lit0:
3668 return "DW_OP_lit0";
3669 case DW_OP_lit1:
3670 return "DW_OP_lit1";
3671 case DW_OP_lit2:
3672 return "DW_OP_lit2";
3673 case DW_OP_lit3:
3674 return "DW_OP_lit3";
3675 case DW_OP_lit4:
3676 return "DW_OP_lit4";
3677 case DW_OP_lit5:
3678 return "DW_OP_lit5";
3679 case DW_OP_lit6:
3680 return "DW_OP_lit6";
3681 case DW_OP_lit7:
3682 return "DW_OP_lit7";
3683 case DW_OP_lit8:
3684 return "DW_OP_lit8";
3685 case DW_OP_lit9:
3686 return "DW_OP_lit9";
3687 case DW_OP_lit10:
3688 return "DW_OP_lit10";
3689 case DW_OP_lit11:
3690 return "DW_OP_lit11";
3691 case DW_OP_lit12:
3692 return "DW_OP_lit12";
3693 case DW_OP_lit13:
3694 return "DW_OP_lit13";
3695 case DW_OP_lit14:
3696 return "DW_OP_lit14";
3697 case DW_OP_lit15:
3698 return "DW_OP_lit15";
3699 case DW_OP_lit16:
3700 return "DW_OP_lit16";
3701 case DW_OP_lit17:
3702 return "DW_OP_lit17";
3703 case DW_OP_lit18:
3704 return "DW_OP_lit18";
3705 case DW_OP_lit19:
3706 return "DW_OP_lit19";
3707 case DW_OP_lit20:
3708 return "DW_OP_lit20";
3709 case DW_OP_lit21:
3710 return "DW_OP_lit21";
3711 case DW_OP_lit22:
3712 return "DW_OP_lit22";
3713 case DW_OP_lit23:
3714 return "DW_OP_lit23";
3715 case DW_OP_lit24:
3716 return "DW_OP_lit24";
3717 case DW_OP_lit25:
3718 return "DW_OP_lit25";
3719 case DW_OP_lit26:
3720 return "DW_OP_lit26";
3721 case DW_OP_lit27:
3722 return "DW_OP_lit27";
3723 case DW_OP_lit28:
3724 return "DW_OP_lit28";
3725 case DW_OP_lit29:
3726 return "DW_OP_lit29";
3727 case DW_OP_lit30:
3728 return "DW_OP_lit30";
3729 case DW_OP_lit31:
3730 return "DW_OP_lit31";
3731 case DW_OP_reg0:
3732 return "DW_OP_reg0";
3733 case DW_OP_reg1:
3734 return "DW_OP_reg1";
3735 case DW_OP_reg2:
3736 return "DW_OP_reg2";
3737 case DW_OP_reg3:
3738 return "DW_OP_reg3";
3739 case DW_OP_reg4:
3740 return "DW_OP_reg4";
3741 case DW_OP_reg5:
3742 return "DW_OP_reg5";
3743 case DW_OP_reg6:
3744 return "DW_OP_reg6";
3745 case DW_OP_reg7:
3746 return "DW_OP_reg7";
3747 case DW_OP_reg8:
3748 return "DW_OP_reg8";
3749 case DW_OP_reg9:
3750 return "DW_OP_reg9";
3751 case DW_OP_reg10:
3752 return "DW_OP_reg10";
3753 case DW_OP_reg11:
3754 return "DW_OP_reg11";
3755 case DW_OP_reg12:
3756 return "DW_OP_reg12";
3757 case DW_OP_reg13:
3758 return "DW_OP_reg13";
3759 case DW_OP_reg14:
3760 return "DW_OP_reg14";
3761 case DW_OP_reg15:
3762 return "DW_OP_reg15";
3763 case DW_OP_reg16:
3764 return "DW_OP_reg16";
3765 case DW_OP_reg17:
3766 return "DW_OP_reg17";
3767 case DW_OP_reg18:
3768 return "DW_OP_reg18";
3769 case DW_OP_reg19:
3770 return "DW_OP_reg19";
3771 case DW_OP_reg20:
3772 return "DW_OP_reg20";
3773 case DW_OP_reg21:
3774 return "DW_OP_reg21";
3775 case DW_OP_reg22:
3776 return "DW_OP_reg22";
3777 case DW_OP_reg23:
3778 return "DW_OP_reg23";
3779 case DW_OP_reg24:
3780 return "DW_OP_reg24";
3781 case DW_OP_reg25:
3782 return "DW_OP_reg25";
3783 case DW_OP_reg26:
3784 return "DW_OP_reg26";
3785 case DW_OP_reg27:
3786 return "DW_OP_reg27";
3787 case DW_OP_reg28:
3788 return "DW_OP_reg28";
3789 case DW_OP_reg29:
3790 return "DW_OP_reg29";
3791 case DW_OP_reg30:
3792 return "DW_OP_reg30";
3793 case DW_OP_reg31:
3794 return "DW_OP_reg31";
3795 case DW_OP_breg0:
3796 return "DW_OP_breg0";
3797 case DW_OP_breg1:
3798 return "DW_OP_breg1";
3799 case DW_OP_breg2:
3800 return "DW_OP_breg2";
3801 case DW_OP_breg3:
3802 return "DW_OP_breg3";
3803 case DW_OP_breg4:
3804 return "DW_OP_breg4";
3805 case DW_OP_breg5:
3806 return "DW_OP_breg5";
3807 case DW_OP_breg6:
3808 return "DW_OP_breg6";
3809 case DW_OP_breg7:
3810 return "DW_OP_breg7";
3811 case DW_OP_breg8:
3812 return "DW_OP_breg8";
3813 case DW_OP_breg9:
3814 return "DW_OP_breg9";
3815 case DW_OP_breg10:
3816 return "DW_OP_breg10";
3817 case DW_OP_breg11:
3818 return "DW_OP_breg11";
3819 case DW_OP_breg12:
3820 return "DW_OP_breg12";
3821 case DW_OP_breg13:
3822 return "DW_OP_breg13";
3823 case DW_OP_breg14:
3824 return "DW_OP_breg14";
3825 case DW_OP_breg15:
3826 return "DW_OP_breg15";
3827 case DW_OP_breg16:
3828 return "DW_OP_breg16";
3829 case DW_OP_breg17:
3830 return "DW_OP_breg17";
3831 case DW_OP_breg18:
3832 return "DW_OP_breg18";
3833 case DW_OP_breg19:
3834 return "DW_OP_breg19";
3835 case DW_OP_breg20:
3836 return "DW_OP_breg20";
3837 case DW_OP_breg21:
3838 return "DW_OP_breg21";
3839 case DW_OP_breg22:
3840 return "DW_OP_breg22";
3841 case DW_OP_breg23:
3842 return "DW_OP_breg23";
3843 case DW_OP_breg24:
3844 return "DW_OP_breg24";
3845 case DW_OP_breg25:
3846 return "DW_OP_breg25";
3847 case DW_OP_breg26:
3848 return "DW_OP_breg26";
3849 case DW_OP_breg27:
3850 return "DW_OP_breg27";
3851 case DW_OP_breg28:
3852 return "DW_OP_breg28";
3853 case DW_OP_breg29:
3854 return "DW_OP_breg29";
3855 case DW_OP_breg30:
3856 return "DW_OP_breg30";
3857 case DW_OP_breg31:
3858 return "DW_OP_breg31";
3859 case DW_OP_regx:
3860 return "DW_OP_regx";
3861 case DW_OP_fbreg:
3862 return "DW_OP_fbreg";
3863 case DW_OP_bregx:
3864 return "DW_OP_bregx";
3865 case DW_OP_piece:
3866 return "DW_OP_piece";
3867 case DW_OP_deref_size:
3868 return "DW_OP_deref_size";
3869 case DW_OP_xderef_size:
3870 return "DW_OP_xderef_size";
3871 case DW_OP_nop:
3872 return "DW_OP_nop";
3873 case DW_OP_push_object_address:
3874 return "DW_OP_push_object_address";
3875 case DW_OP_call2:
3876 return "DW_OP_call2";
3877 case DW_OP_call4:
3878 return "DW_OP_call4";
3879 case DW_OP_call_ref:
3880 return "DW_OP_call_ref";
3881 case DW_OP_GNU_push_tls_address:
3882 return "DW_OP_GNU_push_tls_address";
3883 case DW_OP_GNU_uninit:
3884 return "DW_OP_GNU_uninit";
3885 default:
3886 return "OP_<unknown>";
3890 /* Return a pointer to a newly allocated location description. Location
3891 descriptions are simple expression terms that can be strung
3892 together to form more complicated location (address) descriptions. */
3894 static inline dw_loc_descr_ref
3895 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
3896 unsigned HOST_WIDE_INT oprnd2)
3898 dw_loc_descr_ref descr = GGC_CNEW (dw_loc_descr_node);
3900 descr->dw_loc_opc = op;
3901 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
3902 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
3903 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
3904 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
3906 return descr;
3909 /* Return a pointer to a newly allocated location description for
3910 REG and OFFSET. */
3912 static inline dw_loc_descr_ref
3913 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
3915 if (offset)
3917 if (reg <= 31)
3918 return new_loc_descr (DW_OP_breg0 + reg, offset, 0);
3919 else
3920 return new_loc_descr (DW_OP_bregx, reg, offset);
3922 else if (reg <= 31)
3923 return new_loc_descr (DW_OP_reg0 + reg, 0, 0);
3924 else
3925 return new_loc_descr (DW_OP_regx, reg, 0);
3928 /* Add a location description term to a location description expression. */
3930 static inline void
3931 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
3933 dw_loc_descr_ref *d;
3935 /* Find the end of the chain. */
3936 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
3939 *d = descr;
3942 /* Return the size of a location descriptor. */
3944 static unsigned long
3945 size_of_loc_descr (dw_loc_descr_ref loc)
3947 unsigned long size = 1;
3949 switch (loc->dw_loc_opc)
3951 case DW_OP_addr:
3952 case INTERNAL_DW_OP_tls_addr:
3953 size += DWARF2_ADDR_SIZE;
3954 break;
3955 case DW_OP_const1u:
3956 case DW_OP_const1s:
3957 size += 1;
3958 break;
3959 case DW_OP_const2u:
3960 case DW_OP_const2s:
3961 size += 2;
3962 break;
3963 case DW_OP_const4u:
3964 case DW_OP_const4s:
3965 size += 4;
3966 break;
3967 case DW_OP_const8u:
3968 case DW_OP_const8s:
3969 size += 8;
3970 break;
3971 case DW_OP_constu:
3972 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3973 break;
3974 case DW_OP_consts:
3975 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3976 break;
3977 case DW_OP_pick:
3978 size += 1;
3979 break;
3980 case DW_OP_plus_uconst:
3981 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3982 break;
3983 case DW_OP_skip:
3984 case DW_OP_bra:
3985 size += 2;
3986 break;
3987 case DW_OP_breg0:
3988 case DW_OP_breg1:
3989 case DW_OP_breg2:
3990 case DW_OP_breg3:
3991 case DW_OP_breg4:
3992 case DW_OP_breg5:
3993 case DW_OP_breg6:
3994 case DW_OP_breg7:
3995 case DW_OP_breg8:
3996 case DW_OP_breg9:
3997 case DW_OP_breg10:
3998 case DW_OP_breg11:
3999 case DW_OP_breg12:
4000 case DW_OP_breg13:
4001 case DW_OP_breg14:
4002 case DW_OP_breg15:
4003 case DW_OP_breg16:
4004 case DW_OP_breg17:
4005 case DW_OP_breg18:
4006 case DW_OP_breg19:
4007 case DW_OP_breg20:
4008 case DW_OP_breg21:
4009 case DW_OP_breg22:
4010 case DW_OP_breg23:
4011 case DW_OP_breg24:
4012 case DW_OP_breg25:
4013 case DW_OP_breg26:
4014 case DW_OP_breg27:
4015 case DW_OP_breg28:
4016 case DW_OP_breg29:
4017 case DW_OP_breg30:
4018 case DW_OP_breg31:
4019 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4020 break;
4021 case DW_OP_regx:
4022 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4023 break;
4024 case DW_OP_fbreg:
4025 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4026 break;
4027 case DW_OP_bregx:
4028 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4029 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4030 break;
4031 case DW_OP_piece:
4032 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4033 break;
4034 case DW_OP_deref_size:
4035 case DW_OP_xderef_size:
4036 size += 1;
4037 break;
4038 case DW_OP_call2:
4039 size += 2;
4040 break;
4041 case DW_OP_call4:
4042 size += 4;
4043 break;
4044 case DW_OP_call_ref:
4045 size += DWARF2_ADDR_SIZE;
4046 break;
4047 default:
4048 break;
4051 return size;
4054 /* Return the size of a series of location descriptors. */
4056 static unsigned long
4057 size_of_locs (dw_loc_descr_ref loc)
4059 dw_loc_descr_ref l;
4060 unsigned long size;
4062 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
4063 field, to avoid writing to a PCH file. */
4064 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4066 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
4067 break;
4068 size += size_of_loc_descr (l);
4070 if (! l)
4071 return size;
4073 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4075 l->dw_loc_addr = size;
4076 size += size_of_loc_descr (l);
4079 return size;
4082 /* Output location description stack opcode's operands (if any). */
4084 static void
4085 output_loc_operands (dw_loc_descr_ref loc)
4087 dw_val_ref val1 = &loc->dw_loc_oprnd1;
4088 dw_val_ref val2 = &loc->dw_loc_oprnd2;
4090 switch (loc->dw_loc_opc)
4092 #ifdef DWARF2_DEBUGGING_INFO
4093 case DW_OP_addr:
4094 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
4095 break;
4096 case DW_OP_const2u:
4097 case DW_OP_const2s:
4098 dw2_asm_output_data (2, val1->v.val_int, NULL);
4099 break;
4100 case DW_OP_const4u:
4101 case DW_OP_const4s:
4102 dw2_asm_output_data (4, val1->v.val_int, NULL);
4103 break;
4104 case DW_OP_const8u:
4105 case DW_OP_const8s:
4106 gcc_assert (HOST_BITS_PER_LONG >= 64);
4107 dw2_asm_output_data (8, val1->v.val_int, NULL);
4108 break;
4109 case DW_OP_skip:
4110 case DW_OP_bra:
4112 int offset;
4114 gcc_assert (val1->val_class == dw_val_class_loc);
4115 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4117 dw2_asm_output_data (2, offset, NULL);
4119 break;
4120 #else
4121 case DW_OP_addr:
4122 case DW_OP_const2u:
4123 case DW_OP_const2s:
4124 case DW_OP_const4u:
4125 case DW_OP_const4s:
4126 case DW_OP_const8u:
4127 case DW_OP_const8s:
4128 case DW_OP_skip:
4129 case DW_OP_bra:
4130 /* We currently don't make any attempt to make sure these are
4131 aligned properly like we do for the main unwind info, so
4132 don't support emitting things larger than a byte if we're
4133 only doing unwinding. */
4134 gcc_unreachable ();
4135 #endif
4136 case DW_OP_const1u:
4137 case DW_OP_const1s:
4138 dw2_asm_output_data (1, val1->v.val_int, NULL);
4139 break;
4140 case DW_OP_constu:
4141 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4142 break;
4143 case DW_OP_consts:
4144 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4145 break;
4146 case DW_OP_pick:
4147 dw2_asm_output_data (1, val1->v.val_int, NULL);
4148 break;
4149 case DW_OP_plus_uconst:
4150 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4151 break;
4152 case DW_OP_breg0:
4153 case DW_OP_breg1:
4154 case DW_OP_breg2:
4155 case DW_OP_breg3:
4156 case DW_OP_breg4:
4157 case DW_OP_breg5:
4158 case DW_OP_breg6:
4159 case DW_OP_breg7:
4160 case DW_OP_breg8:
4161 case DW_OP_breg9:
4162 case DW_OP_breg10:
4163 case DW_OP_breg11:
4164 case DW_OP_breg12:
4165 case DW_OP_breg13:
4166 case DW_OP_breg14:
4167 case DW_OP_breg15:
4168 case DW_OP_breg16:
4169 case DW_OP_breg17:
4170 case DW_OP_breg18:
4171 case DW_OP_breg19:
4172 case DW_OP_breg20:
4173 case DW_OP_breg21:
4174 case DW_OP_breg22:
4175 case DW_OP_breg23:
4176 case DW_OP_breg24:
4177 case DW_OP_breg25:
4178 case DW_OP_breg26:
4179 case DW_OP_breg27:
4180 case DW_OP_breg28:
4181 case DW_OP_breg29:
4182 case DW_OP_breg30:
4183 case DW_OP_breg31:
4184 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4185 break;
4186 case DW_OP_regx:
4187 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4188 break;
4189 case DW_OP_fbreg:
4190 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4191 break;
4192 case DW_OP_bregx:
4193 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4194 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
4195 break;
4196 case DW_OP_piece:
4197 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4198 break;
4199 case DW_OP_deref_size:
4200 case DW_OP_xderef_size:
4201 dw2_asm_output_data (1, val1->v.val_int, NULL);
4202 break;
4204 case INTERNAL_DW_OP_tls_addr:
4205 if (targetm.asm_out.output_dwarf_dtprel)
4207 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
4208 DWARF2_ADDR_SIZE,
4209 val1->v.val_addr);
4210 fputc ('\n', asm_out_file);
4212 else
4213 gcc_unreachable ();
4214 break;
4216 default:
4217 /* Other codes have no operands. */
4218 break;
4222 /* Output a sequence of location operations. */
4224 static void
4225 output_loc_sequence (dw_loc_descr_ref loc)
4227 for (; loc != NULL; loc = loc->dw_loc_next)
4229 /* Output the opcode. */
4230 dw2_asm_output_data (1, loc->dw_loc_opc,
4231 "%s", dwarf_stack_op_name (loc->dw_loc_opc));
4233 /* Output the operand(s) (if any). */
4234 output_loc_operands (loc);
4238 /* Output location description stack opcode's operands (if any).
4239 The output is single bytes on a line, suitable for .cfi_escape. */
4241 static void
4242 output_loc_operands_raw (dw_loc_descr_ref loc)
4244 dw_val_ref val1 = &loc->dw_loc_oprnd1;
4245 dw_val_ref val2 = &loc->dw_loc_oprnd2;
4247 switch (loc->dw_loc_opc)
4249 case DW_OP_addr:
4250 /* We cannot output addresses in .cfi_escape, only bytes. */
4251 gcc_unreachable ();
4253 case DW_OP_const1u:
4254 case DW_OP_const1s:
4255 case DW_OP_pick:
4256 case DW_OP_deref_size:
4257 case DW_OP_xderef_size:
4258 fputc (',', asm_out_file);
4259 dw2_asm_output_data_raw (1, val1->v.val_int);
4260 break;
4262 case DW_OP_const2u:
4263 case DW_OP_const2s:
4264 fputc (',', asm_out_file);
4265 dw2_asm_output_data_raw (2, val1->v.val_int);
4266 break;
4268 case DW_OP_const4u:
4269 case DW_OP_const4s:
4270 fputc (',', asm_out_file);
4271 dw2_asm_output_data_raw (4, val1->v.val_int);
4272 break;
4274 case DW_OP_const8u:
4275 case DW_OP_const8s:
4276 gcc_assert (HOST_BITS_PER_LONG >= 64);
4277 fputc (',', asm_out_file);
4278 dw2_asm_output_data_raw (8, val1->v.val_int);
4279 break;
4281 case DW_OP_skip:
4282 case DW_OP_bra:
4284 int offset;
4286 gcc_assert (val1->val_class == dw_val_class_loc);
4287 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4289 fputc (',', asm_out_file);
4290 dw2_asm_output_data_raw (2, offset);
4292 break;
4294 case DW_OP_constu:
4295 case DW_OP_plus_uconst:
4296 case DW_OP_regx:
4297 case DW_OP_piece:
4298 fputc (',', asm_out_file);
4299 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
4300 break;
4302 case DW_OP_consts:
4303 case DW_OP_breg0:
4304 case DW_OP_breg1:
4305 case DW_OP_breg2:
4306 case DW_OP_breg3:
4307 case DW_OP_breg4:
4308 case DW_OP_breg5:
4309 case DW_OP_breg6:
4310 case DW_OP_breg7:
4311 case DW_OP_breg8:
4312 case DW_OP_breg9:
4313 case DW_OP_breg10:
4314 case DW_OP_breg11:
4315 case DW_OP_breg12:
4316 case DW_OP_breg13:
4317 case DW_OP_breg14:
4318 case DW_OP_breg15:
4319 case DW_OP_breg16:
4320 case DW_OP_breg17:
4321 case DW_OP_breg18:
4322 case DW_OP_breg19:
4323 case DW_OP_breg20:
4324 case DW_OP_breg21:
4325 case DW_OP_breg22:
4326 case DW_OP_breg23:
4327 case DW_OP_breg24:
4328 case DW_OP_breg25:
4329 case DW_OP_breg26:
4330 case DW_OP_breg27:
4331 case DW_OP_breg28:
4332 case DW_OP_breg29:
4333 case DW_OP_breg30:
4334 case DW_OP_breg31:
4335 case DW_OP_fbreg:
4336 fputc (',', asm_out_file);
4337 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
4338 break;
4340 case DW_OP_bregx:
4341 fputc (',', asm_out_file);
4342 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
4343 fputc (',', asm_out_file);
4344 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
4345 break;
4347 case INTERNAL_DW_OP_tls_addr:
4348 gcc_unreachable ();
4350 default:
4351 /* Other codes have no operands. */
4352 break;
4356 static void
4357 output_loc_sequence_raw (dw_loc_descr_ref loc)
4359 while (1)
4361 /* Output the opcode. */
4362 fprintf (asm_out_file, "0x%x", loc->dw_loc_opc);
4363 output_loc_operands_raw (loc);
4365 if (!loc->dw_loc_next)
4366 break;
4367 loc = loc->dw_loc_next;
4369 fputc (',', asm_out_file);
4373 /* This routine will generate the correct assembly data for a location
4374 description based on a cfi entry with a complex address. */
4376 static void
4377 output_cfa_loc (dw_cfi_ref cfi)
4379 dw_loc_descr_ref loc;
4380 unsigned long size;
4382 if (cfi->dw_cfi_opc == DW_CFA_expression)
4383 dw2_asm_output_data (1, cfi->dw_cfi_oprnd2.dw_cfi_reg_num, NULL);
4385 /* Output the size of the block. */
4386 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
4387 size = size_of_locs (loc);
4388 dw2_asm_output_data_uleb128 (size, NULL);
4390 /* Now output the operations themselves. */
4391 output_loc_sequence (loc);
4394 /* Similar, but used for .cfi_escape. */
4396 static void
4397 output_cfa_loc_raw (dw_cfi_ref cfi)
4399 dw_loc_descr_ref loc;
4400 unsigned long size;
4402 if (cfi->dw_cfi_opc == DW_CFA_expression)
4403 fprintf (asm_out_file, "0x%x,", cfi->dw_cfi_oprnd2.dw_cfi_reg_num);
4405 /* Output the size of the block. */
4406 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
4407 size = size_of_locs (loc);
4408 dw2_asm_output_data_uleb128_raw (size);
4409 fputc (',', asm_out_file);
4411 /* Now output the operations themselves. */
4412 output_loc_sequence_raw (loc);
4415 /* This function builds a dwarf location descriptor sequence from a
4416 dw_cfa_location, adding the given OFFSET to the result of the
4417 expression. */
4419 static struct dw_loc_descr_struct *
4420 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
4422 struct dw_loc_descr_struct *head, *tmp;
4424 offset += cfa->offset;
4426 if (cfa->indirect)
4428 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
4429 head->dw_loc_oprnd1.val_class = dw_val_class_const;
4430 tmp = new_loc_descr (DW_OP_deref, 0, 0);
4431 add_loc_descr (&head, tmp);
4432 if (offset != 0)
4434 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4435 add_loc_descr (&head, tmp);
4438 else
4439 head = new_reg_loc_descr (cfa->reg, offset);
4441 return head;
4444 /* This function builds a dwarf location descriptor sequence for
4445 the address at OFFSET from the CFA when stack is aligned to
4446 ALIGNMENT byte. */
4448 static struct dw_loc_descr_struct *
4449 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
4451 struct dw_loc_descr_struct *head;
4452 unsigned int dwarf_fp
4453 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
4455 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
4456 if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
4458 head = new_reg_loc_descr (dwarf_fp, 0);
4459 add_loc_descr (&head, int_loc_descriptor (alignment));
4460 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
4462 add_loc_descr (&head, int_loc_descriptor (offset));
4463 add_loc_descr (&head, new_loc_descr (DW_OP_plus, 0, 0));
4465 else
4466 head = new_reg_loc_descr (dwarf_fp, offset);
4467 return head;
4470 /* This function fills in aa dw_cfa_location structure from a dwarf location
4471 descriptor sequence. */
4473 static void
4474 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
4476 struct dw_loc_descr_struct *ptr;
4477 cfa->offset = 0;
4478 cfa->base_offset = 0;
4479 cfa->indirect = 0;
4480 cfa->reg = -1;
4482 for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
4484 enum dwarf_location_atom op = ptr->dw_loc_opc;
4486 switch (op)
4488 case DW_OP_reg0:
4489 case DW_OP_reg1:
4490 case DW_OP_reg2:
4491 case DW_OP_reg3:
4492 case DW_OP_reg4:
4493 case DW_OP_reg5:
4494 case DW_OP_reg6:
4495 case DW_OP_reg7:
4496 case DW_OP_reg8:
4497 case DW_OP_reg9:
4498 case DW_OP_reg10:
4499 case DW_OP_reg11:
4500 case DW_OP_reg12:
4501 case DW_OP_reg13:
4502 case DW_OP_reg14:
4503 case DW_OP_reg15:
4504 case DW_OP_reg16:
4505 case DW_OP_reg17:
4506 case DW_OP_reg18:
4507 case DW_OP_reg19:
4508 case DW_OP_reg20:
4509 case DW_OP_reg21:
4510 case DW_OP_reg22:
4511 case DW_OP_reg23:
4512 case DW_OP_reg24:
4513 case DW_OP_reg25:
4514 case DW_OP_reg26:
4515 case DW_OP_reg27:
4516 case DW_OP_reg28:
4517 case DW_OP_reg29:
4518 case DW_OP_reg30:
4519 case DW_OP_reg31:
4520 cfa->reg = op - DW_OP_reg0;
4521 break;
4522 case DW_OP_regx:
4523 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
4524 break;
4525 case DW_OP_breg0:
4526 case DW_OP_breg1:
4527 case DW_OP_breg2:
4528 case DW_OP_breg3:
4529 case DW_OP_breg4:
4530 case DW_OP_breg5:
4531 case DW_OP_breg6:
4532 case DW_OP_breg7:
4533 case DW_OP_breg8:
4534 case DW_OP_breg9:
4535 case DW_OP_breg10:
4536 case DW_OP_breg11:
4537 case DW_OP_breg12:
4538 case DW_OP_breg13:
4539 case DW_OP_breg14:
4540 case DW_OP_breg15:
4541 case DW_OP_breg16:
4542 case DW_OP_breg17:
4543 case DW_OP_breg18:
4544 case DW_OP_breg19:
4545 case DW_OP_breg20:
4546 case DW_OP_breg21:
4547 case DW_OP_breg22:
4548 case DW_OP_breg23:
4549 case DW_OP_breg24:
4550 case DW_OP_breg25:
4551 case DW_OP_breg26:
4552 case DW_OP_breg27:
4553 case DW_OP_breg28:
4554 case DW_OP_breg29:
4555 case DW_OP_breg30:
4556 case DW_OP_breg31:
4557 cfa->reg = op - DW_OP_breg0;
4558 cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
4559 break;
4560 case DW_OP_bregx:
4561 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
4562 cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
4563 break;
4564 case DW_OP_deref:
4565 cfa->indirect = 1;
4566 break;
4567 case DW_OP_plus_uconst:
4568 cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
4569 break;
4570 default:
4571 internal_error ("DW_LOC_OP %s not implemented",
4572 dwarf_stack_op_name (ptr->dw_loc_opc));
4576 #endif /* .debug_frame support */
4578 /* And now, the support for symbolic debugging information. */
4579 #ifdef DWARF2_DEBUGGING_INFO
4581 /* .debug_str support. */
4582 static int output_indirect_string (void **, void *);
4584 static void dwarf2out_init (const char *);
4585 static void dwarf2out_finish (const char *);
4586 static void dwarf2out_define (unsigned int, const char *);
4587 static void dwarf2out_undef (unsigned int, const char *);
4588 static void dwarf2out_start_source_file (unsigned, const char *);
4589 static void dwarf2out_end_source_file (unsigned);
4590 static void dwarf2out_begin_block (unsigned, unsigned);
4591 static void dwarf2out_end_block (unsigned, unsigned);
4592 static bool dwarf2out_ignore_block (const_tree);
4593 static void dwarf2out_global_decl (tree);
4594 static void dwarf2out_type_decl (tree, int);
4595 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
4596 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
4597 dw_die_ref);
4598 static void dwarf2out_abstract_function (tree);
4599 static void dwarf2out_var_location (rtx);
4600 static void dwarf2out_begin_function (tree);
4602 /* The debug hooks structure. */
4604 const struct gcc_debug_hooks dwarf2_debug_hooks =
4606 dwarf2out_init,
4607 dwarf2out_finish,
4608 dwarf2out_define,
4609 dwarf2out_undef,
4610 dwarf2out_start_source_file,
4611 dwarf2out_end_source_file,
4612 dwarf2out_begin_block,
4613 dwarf2out_end_block,
4614 dwarf2out_ignore_block,
4615 dwarf2out_source_line,
4616 dwarf2out_begin_prologue,
4617 debug_nothing_int_charstar, /* end_prologue */
4618 dwarf2out_end_epilogue,
4619 dwarf2out_begin_function,
4620 debug_nothing_int, /* end_function */
4621 dwarf2out_decl, /* function_decl */
4622 dwarf2out_global_decl,
4623 dwarf2out_type_decl, /* type_decl */
4624 dwarf2out_imported_module_or_decl,
4625 debug_nothing_tree, /* deferred_inline_function */
4626 /* The DWARF 2 backend tries to reduce debugging bloat by not
4627 emitting the abstract description of inline functions until
4628 something tries to reference them. */
4629 dwarf2out_abstract_function, /* outlining_inline_function */
4630 debug_nothing_rtx, /* label */
4631 debug_nothing_int, /* handle_pch */
4632 dwarf2out_var_location,
4633 dwarf2out_switch_text_section,
4634 1 /* start_end_main_source_file */
4636 #endif
4638 /* NOTE: In the comments in this file, many references are made to
4639 "Debugging Information Entries". This term is abbreviated as `DIE'
4640 throughout the remainder of this file. */
4642 /* An internal representation of the DWARF output is built, and then
4643 walked to generate the DWARF debugging info. The walk of the internal
4644 representation is done after the entire program has been compiled.
4645 The types below are used to describe the internal representation. */
4647 /* Various DIE's use offsets relative to the beginning of the
4648 .debug_info section to refer to each other. */
4650 typedef long int dw_offset;
4652 /* Define typedefs here to avoid circular dependencies. */
4654 typedef struct dw_attr_struct *dw_attr_ref;
4655 typedef struct dw_line_info_struct *dw_line_info_ref;
4656 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
4657 typedef struct pubname_struct *pubname_ref;
4658 typedef struct dw_ranges_struct *dw_ranges_ref;
4659 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
4661 /* Each entry in the line_info_table maintains the file and
4662 line number associated with the label generated for that
4663 entry. The label gives the PC value associated with
4664 the line number entry. */
4666 typedef struct dw_line_info_struct GTY(())
4668 unsigned long dw_file_num;
4669 unsigned long dw_line_num;
4671 dw_line_info_entry;
4673 /* Line information for functions in separate sections; each one gets its
4674 own sequence. */
4675 typedef struct dw_separate_line_info_struct GTY(())
4677 unsigned long dw_file_num;
4678 unsigned long dw_line_num;
4679 unsigned long function;
4681 dw_separate_line_info_entry;
4683 /* Each DIE attribute has a field specifying the attribute kind,
4684 a link to the next attribute in the chain, and an attribute value.
4685 Attributes are typically linked below the DIE they modify. */
4687 typedef struct dw_attr_struct GTY(())
4689 enum dwarf_attribute dw_attr;
4690 dw_val_node dw_attr_val;
4692 dw_attr_node;
4694 DEF_VEC_O(dw_attr_node);
4695 DEF_VEC_ALLOC_O(dw_attr_node,gc);
4697 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
4698 The children of each node form a circular list linked by
4699 die_sib. die_child points to the node *before* the "first" child node. */
4701 typedef struct die_struct GTY((chain_circular ("%h.die_sib")))
4703 enum dwarf_tag die_tag;
4704 char *die_symbol;
4705 VEC(dw_attr_node,gc) * die_attr;
4706 dw_die_ref die_parent;
4707 dw_die_ref die_child;
4708 dw_die_ref die_sib;
4709 dw_die_ref die_definition; /* ref from a specification to its definition */
4710 dw_offset die_offset;
4711 unsigned long die_abbrev;
4712 int die_mark;
4713 /* Die is used and must not be pruned as unused. */
4714 int die_perennial_p;
4715 unsigned int decl_id;
4717 die_node;
4719 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
4720 #define FOR_EACH_CHILD(die, c, expr) do { \
4721 c = die->die_child; \
4722 if (c) do { \
4723 c = c->die_sib; \
4724 expr; \
4725 } while (c != die->die_child); \
4726 } while (0)
4728 /* The pubname structure */
4730 typedef struct pubname_struct GTY(())
4732 dw_die_ref die;
4733 const char *name;
4735 pubname_entry;
4737 DEF_VEC_O(pubname_entry);
4738 DEF_VEC_ALLOC_O(pubname_entry, gc);
4740 struct dw_ranges_struct GTY(())
4742 /* If this is positive, it's a block number, otherwise it's a
4743 bitwise-negated index into dw_ranges_by_label. */
4744 int num;
4747 struct dw_ranges_by_label_struct GTY(())
4749 const char *begin;
4750 const char *end;
4753 /* The limbo die list structure. */
4754 typedef struct limbo_die_struct GTY(())
4756 dw_die_ref die;
4757 tree created_for;
4758 struct limbo_die_struct *next;
4760 limbo_die_node;
4762 /* How to start an assembler comment. */
4763 #ifndef ASM_COMMENT_START
4764 #define ASM_COMMENT_START ";#"
4765 #endif
4767 /* Define a macro which returns nonzero for a TYPE_DECL which was
4768 implicitly generated for a tagged type.
4770 Note that unlike the gcc front end (which generates a NULL named
4771 TYPE_DECL node for each complete tagged type, each array type, and
4772 each function type node created) the g++ front end generates a
4773 _named_ TYPE_DECL node for each tagged type node created.
4774 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
4775 generate a DW_TAG_typedef DIE for them. */
4777 #define TYPE_DECL_IS_STUB(decl) \
4778 (DECL_NAME (decl) == NULL_TREE \
4779 || (DECL_ARTIFICIAL (decl) \
4780 && is_tagged_type (TREE_TYPE (decl)) \
4781 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
4782 /* This is necessary for stub decls that \
4783 appear in nested inline functions. */ \
4784 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
4785 && (decl_ultimate_origin (decl) \
4786 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
4788 /* Information concerning the compilation unit's programming
4789 language, and compiler version. */
4791 /* Fixed size portion of the DWARF compilation unit header. */
4792 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
4793 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
4795 /* Fixed size portion of public names info. */
4796 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
4798 /* Fixed size portion of the address range info. */
4799 #define DWARF_ARANGES_HEADER_SIZE \
4800 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
4801 DWARF2_ADDR_SIZE * 2) \
4802 - DWARF_INITIAL_LENGTH_SIZE)
4804 /* Size of padding portion in the address range info. It must be
4805 aligned to twice the pointer size. */
4806 #define DWARF_ARANGES_PAD_SIZE \
4807 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
4808 DWARF2_ADDR_SIZE * 2) \
4809 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
4811 /* Use assembler line directives if available. */
4812 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
4813 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
4814 #define DWARF2_ASM_LINE_DEBUG_INFO 1
4815 #else
4816 #define DWARF2_ASM_LINE_DEBUG_INFO 0
4817 #endif
4818 #endif
4820 /* Minimum line offset in a special line info. opcode.
4821 This value was chosen to give a reasonable range of values. */
4822 #define DWARF_LINE_BASE -10
4824 /* First special line opcode - leave room for the standard opcodes. */
4825 #define DWARF_LINE_OPCODE_BASE 10
4827 /* Range of line offsets in a special line info. opcode. */
4828 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
4830 /* Flag that indicates the initial value of the is_stmt_start flag.
4831 In the present implementation, we do not mark any lines as
4832 the beginning of a source statement, because that information
4833 is not made available by the GCC front-end. */
4834 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
4836 #ifdef DWARF2_DEBUGGING_INFO
4837 /* This location is used by calc_die_sizes() to keep track
4838 the offset of each DIE within the .debug_info section. */
4839 static unsigned long next_die_offset;
4840 #endif
4842 /* Record the root of the DIE's built for the current compilation unit. */
4843 static GTY(()) dw_die_ref comp_unit_die;
4845 /* A list of DIEs with a NULL parent waiting to be relocated. */
4846 static GTY(()) limbo_die_node *limbo_die_list;
4848 /* Filenames referenced by this compilation unit. */
4849 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
4851 /* A hash table of references to DIE's that describe declarations.
4852 The key is a DECL_UID() which is a unique number identifying each decl. */
4853 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
4855 /* A hash table of references to DIE's that describe COMMON blocks.
4856 The key is DECL_UID() ^ die_parent. */
4857 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
4859 /* Node of the variable location list. */
4860 struct var_loc_node GTY ((chain_next ("%h.next")))
4862 rtx GTY (()) var_loc_note;
4863 const char * GTY (()) label;
4864 const char * GTY (()) section_label;
4865 struct var_loc_node * GTY (()) next;
4868 /* Variable location list. */
4869 struct var_loc_list_def GTY (())
4871 struct var_loc_node * GTY (()) first;
4873 /* Do not mark the last element of the chained list because
4874 it is marked through the chain. */
4875 struct var_loc_node * GTY ((skip ("%h"))) last;
4877 /* DECL_UID of the variable decl. */
4878 unsigned int decl_id;
4880 typedef struct var_loc_list_def var_loc_list;
4883 /* Table of decl location linked lists. */
4884 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
4886 /* A pointer to the base of a list of references to DIE's that
4887 are uniquely identified by their tag, presence/absence of
4888 children DIE's, and list of attribute/value pairs. */
4889 static GTY((length ("abbrev_die_table_allocated")))
4890 dw_die_ref *abbrev_die_table;
4892 /* Number of elements currently allocated for abbrev_die_table. */
4893 static GTY(()) unsigned abbrev_die_table_allocated;
4895 /* Number of elements in type_die_table currently in use. */
4896 static GTY(()) unsigned abbrev_die_table_in_use;
4898 /* Size (in elements) of increments by which we may expand the
4899 abbrev_die_table. */
4900 #define ABBREV_DIE_TABLE_INCREMENT 256
4902 /* A pointer to the base of a table that contains line information
4903 for each source code line in .text in the compilation unit. */
4904 static GTY((length ("line_info_table_allocated")))
4905 dw_line_info_ref line_info_table;
4907 /* Number of elements currently allocated for line_info_table. */
4908 static GTY(()) unsigned line_info_table_allocated;
4910 /* Number of elements in line_info_table currently in use. */
4911 static GTY(()) unsigned line_info_table_in_use;
4913 /* A pointer to the base of a table that contains line information
4914 for each source code line outside of .text in the compilation unit. */
4915 static GTY ((length ("separate_line_info_table_allocated")))
4916 dw_separate_line_info_ref separate_line_info_table;
4918 /* Number of elements currently allocated for separate_line_info_table. */
4919 static GTY(()) unsigned separate_line_info_table_allocated;
4921 /* Number of elements in separate_line_info_table currently in use. */
4922 static GTY(()) unsigned separate_line_info_table_in_use;
4924 /* Size (in elements) of increments by which we may expand the
4925 line_info_table. */
4926 #define LINE_INFO_TABLE_INCREMENT 1024
4928 /* A pointer to the base of a table that contains a list of publicly
4929 accessible names. */
4930 static GTY (()) VEC (pubname_entry, gc) * pubname_table;
4932 /* A pointer to the base of a table that contains a list of publicly
4933 accessible types. */
4934 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
4936 /* Array of dies for which we should generate .debug_arange info. */
4937 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
4939 /* Number of elements currently allocated for arange_table. */
4940 static GTY(()) unsigned arange_table_allocated;
4942 /* Number of elements in arange_table currently in use. */
4943 static GTY(()) unsigned arange_table_in_use;
4945 /* Size (in elements) of increments by which we may expand the
4946 arange_table. */
4947 #define ARANGE_TABLE_INCREMENT 64
4949 /* Array of dies for which we should generate .debug_ranges info. */
4950 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
4952 /* Number of elements currently allocated for ranges_table. */
4953 static GTY(()) unsigned ranges_table_allocated;
4955 /* Number of elements in ranges_table currently in use. */
4956 static GTY(()) unsigned ranges_table_in_use;
4958 /* Array of pairs of labels referenced in ranges_table. */
4959 static GTY ((length ("ranges_by_label_allocated")))
4960 dw_ranges_by_label_ref ranges_by_label;
4962 /* Number of elements currently allocated for ranges_by_label. */
4963 static GTY(()) unsigned ranges_by_label_allocated;
4965 /* Number of elements in ranges_by_label currently in use. */
4966 static GTY(()) unsigned ranges_by_label_in_use;
4968 /* Size (in elements) of increments by which we may expand the
4969 ranges_table. */
4970 #define RANGES_TABLE_INCREMENT 64
4972 /* Whether we have location lists that need outputting */
4973 static GTY(()) bool have_location_lists;
4975 /* Unique label counter. */
4976 static GTY(()) unsigned int loclabel_num;
4978 #ifdef DWARF2_DEBUGGING_INFO
4979 /* Record whether the function being analyzed contains inlined functions. */
4980 static int current_function_has_inlines;
4981 #endif
4982 #if 0 && defined (MIPS_DEBUGGING_INFO)
4983 static int comp_unit_has_inlines;
4984 #endif
4986 /* The last file entry emitted by maybe_emit_file(). */
4987 static GTY(()) struct dwarf_file_data * last_emitted_file;
4989 /* Number of internal labels generated by gen_internal_sym(). */
4990 static GTY(()) int label_num;
4992 /* Cached result of previous call to lookup_filename. */
4993 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
4995 #ifdef DWARF2_DEBUGGING_INFO
4997 /* Offset from the "steady-state frame pointer" to the frame base,
4998 within the current function. */
4999 static HOST_WIDE_INT frame_pointer_fb_offset;
5001 /* Forward declarations for functions defined in this file. */
5003 static int is_pseudo_reg (const_rtx);
5004 static tree type_main_variant (tree);
5005 static int is_tagged_type (const_tree);
5006 static const char *dwarf_tag_name (unsigned);
5007 static const char *dwarf_attr_name (unsigned);
5008 static const char *dwarf_form_name (unsigned);
5009 static tree decl_ultimate_origin (const_tree);
5010 static tree decl_class_context (tree);
5011 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
5012 static inline enum dw_val_class AT_class (dw_attr_ref);
5013 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
5014 static inline unsigned AT_flag (dw_attr_ref);
5015 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
5016 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
5017 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
5018 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
5019 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
5020 unsigned long);
5021 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
5022 unsigned int, unsigned char *);
5023 static hashval_t debug_str_do_hash (const void *);
5024 static int debug_str_eq (const void *, const void *);
5025 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
5026 static inline const char *AT_string (dw_attr_ref);
5027 static int AT_string_form (dw_attr_ref);
5028 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
5029 static void add_AT_specification (dw_die_ref, dw_die_ref);
5030 static inline dw_die_ref AT_ref (dw_attr_ref);
5031 static inline int AT_ref_external (dw_attr_ref);
5032 static inline void set_AT_ref_external (dw_attr_ref, int);
5033 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
5034 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
5035 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
5036 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
5037 dw_loc_list_ref);
5038 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
5039 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
5040 static inline rtx AT_addr (dw_attr_ref);
5041 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
5042 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
5043 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
5044 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
5045 unsigned HOST_WIDE_INT);
5046 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
5047 unsigned long);
5048 static inline const char *AT_lbl (dw_attr_ref);
5049 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
5050 static const char *get_AT_low_pc (dw_die_ref);
5051 static const char *get_AT_hi_pc (dw_die_ref);
5052 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
5053 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
5054 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
5055 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
5056 static bool is_c_family (void);
5057 static bool is_cxx (void);
5058 static bool is_java (void);
5059 static bool is_fortran (void);
5060 static bool is_ada (void);
5061 static void remove_AT (dw_die_ref, enum dwarf_attribute);
5062 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
5063 static void add_child_die (dw_die_ref, dw_die_ref);
5064 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
5065 static dw_die_ref lookup_type_die (tree);
5066 static void equate_type_number_to_die (tree, dw_die_ref);
5067 static hashval_t decl_die_table_hash (const void *);
5068 static int decl_die_table_eq (const void *, const void *);
5069 static dw_die_ref lookup_decl_die (tree);
5070 static hashval_t common_block_die_table_hash (const void *);
5071 static int common_block_die_table_eq (const void *, const void *);
5072 static hashval_t decl_loc_table_hash (const void *);
5073 static int decl_loc_table_eq (const void *, const void *);
5074 static var_loc_list *lookup_decl_loc (const_tree);
5075 static void equate_decl_number_to_die (tree, dw_die_ref);
5076 static void add_var_loc_to_decl (tree, struct var_loc_node *);
5077 static void print_spaces (FILE *);
5078 static void print_die (dw_die_ref, FILE *);
5079 static void print_dwarf_line_table (FILE *);
5080 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
5081 static dw_die_ref pop_compile_unit (dw_die_ref);
5082 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
5083 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
5084 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
5085 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
5086 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
5087 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
5088 static int same_die_p (dw_die_ref, dw_die_ref, int *);
5089 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
5090 static void compute_section_prefix (dw_die_ref);
5091 static int is_type_die (dw_die_ref);
5092 static int is_comdat_die (dw_die_ref);
5093 static int is_symbol_die (dw_die_ref);
5094 static void assign_symbol_names (dw_die_ref);
5095 static void break_out_includes (dw_die_ref);
5096 static hashval_t htab_cu_hash (const void *);
5097 static int htab_cu_eq (const void *, const void *);
5098 static void htab_cu_del (void *);
5099 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
5100 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
5101 static void add_sibling_attributes (dw_die_ref);
5102 static void build_abbrev_table (dw_die_ref);
5103 static void output_location_lists (dw_die_ref);
5104 static int constant_size (unsigned HOST_WIDE_INT);
5105 static unsigned long size_of_die (dw_die_ref);
5106 static void calc_die_sizes (dw_die_ref);
5107 static void mark_dies (dw_die_ref);
5108 static void unmark_dies (dw_die_ref);
5109 static void unmark_all_dies (dw_die_ref);
5110 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
5111 static unsigned long size_of_aranges (void);
5112 static enum dwarf_form value_format (dw_attr_ref);
5113 static void output_value_format (dw_attr_ref);
5114 static void output_abbrev_section (void);
5115 static void output_die_symbol (dw_die_ref);
5116 static void output_die (dw_die_ref);
5117 static void output_compilation_unit_header (void);
5118 static void output_comp_unit (dw_die_ref, int);
5119 static const char *dwarf2_name (tree, int);
5120 static void add_pubname (tree, dw_die_ref);
5121 static void add_pubname_string (const char *, dw_die_ref);
5122 static void add_pubtype (tree, dw_die_ref);
5123 static void output_pubnames (VEC (pubname_entry,gc) *);
5124 static void add_arange (tree, dw_die_ref);
5125 static void output_aranges (void);
5126 static unsigned int add_ranges_num (int);
5127 static unsigned int add_ranges (const_tree);
5128 static unsigned int add_ranges_by_labels (const char *, const char *);
5129 static void output_ranges (void);
5130 static void output_line_info (void);
5131 static void output_file_names (void);
5132 static dw_die_ref base_type_die (tree);
5133 static int is_base_type (tree);
5134 static bool is_subrange_type (const_tree);
5135 static dw_die_ref subrange_type_die (tree, dw_die_ref);
5136 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
5137 static int type_is_enum (const_tree);
5138 static unsigned int dbx_reg_number (const_rtx);
5139 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
5140 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
5141 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
5142 enum var_init_status);
5143 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
5144 enum var_init_status);
5145 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
5146 enum var_init_status);
5147 static int is_based_loc (const_rtx);
5148 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
5149 enum var_init_status);
5150 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
5151 enum var_init_status);
5152 static dw_loc_descr_ref loc_descriptor (rtx, enum var_init_status);
5153 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
5154 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
5155 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
5156 static tree field_type (const_tree);
5157 static unsigned int simple_type_align_in_bits (const_tree);
5158 static unsigned int simple_decl_align_in_bits (const_tree);
5159 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
5160 static HOST_WIDE_INT field_byte_offset (const_tree);
5161 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
5162 dw_loc_descr_ref);
5163 static void add_data_member_location_attribute (dw_die_ref, tree);
5164 static void add_const_value_attribute (dw_die_ref, rtx);
5165 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
5166 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
5167 static void insert_float (const_rtx, unsigned char *);
5168 static rtx rtl_for_decl_location (tree);
5169 static void add_location_or_const_value_attribute (dw_die_ref, tree,
5170 enum dwarf_attribute);
5171 static void tree_add_const_value_attribute (dw_die_ref, tree);
5172 static void add_name_attribute (dw_die_ref, const char *);
5173 static void add_comp_dir_attribute (dw_die_ref);
5174 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
5175 static void add_subscript_info (dw_die_ref, tree, bool);
5176 static void add_byte_size_attribute (dw_die_ref, tree);
5177 static void add_bit_offset_attribute (dw_die_ref, tree);
5178 static void add_bit_size_attribute (dw_die_ref, tree);
5179 static void add_prototyped_attribute (dw_die_ref, tree);
5180 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
5181 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
5182 static void add_src_coords_attributes (dw_die_ref, tree);
5183 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
5184 static void push_decl_scope (tree);
5185 static void pop_decl_scope (void);
5186 static dw_die_ref scope_die_for (tree, dw_die_ref);
5187 static inline int local_scope_p (dw_die_ref);
5188 static inline int class_or_namespace_scope_p (dw_die_ref);
5189 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
5190 static void add_calling_convention_attribute (dw_die_ref, tree);
5191 static const char *type_tag (const_tree);
5192 static tree member_declared_type (const_tree);
5193 #if 0
5194 static const char *decl_start_label (tree);
5195 #endif
5196 static void gen_array_type_die (tree, dw_die_ref);
5197 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
5198 #if 0
5199 static void gen_entry_point_die (tree, dw_die_ref);
5200 #endif
5201 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
5202 static dw_die_ref gen_formal_parameter_die (tree, tree, dw_die_ref);
5203 static void gen_unspecified_parameters_die (tree, dw_die_ref);
5204 static void gen_formal_types_die (tree, dw_die_ref);
5205 static void gen_subprogram_die (tree, dw_die_ref);
5206 static void gen_variable_die (tree, tree, dw_die_ref);
5207 static void gen_const_die (tree, dw_die_ref);
5208 static void gen_label_die (tree, dw_die_ref);
5209 static void gen_lexical_block_die (tree, dw_die_ref, int);
5210 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
5211 static void gen_field_die (tree, dw_die_ref);
5212 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
5213 static dw_die_ref gen_compile_unit_die (const char *);
5214 static void gen_inheritance_die (tree, tree, dw_die_ref);
5215 static void gen_member_die (tree, dw_die_ref);
5216 static void gen_struct_or_union_type_die (tree, dw_die_ref,
5217 enum debug_info_usage);
5218 static void gen_subroutine_type_die (tree, dw_die_ref);
5219 static void gen_typedef_die (tree, dw_die_ref);
5220 static void gen_type_die (tree, dw_die_ref);
5221 static void gen_block_die (tree, dw_die_ref, int);
5222 static void decls_for_scope (tree, dw_die_ref, int);
5223 static int is_redundant_typedef (const_tree);
5224 static void gen_namespace_die (tree, dw_die_ref);
5225 static void gen_decl_die (tree, tree, dw_die_ref);
5226 static dw_die_ref force_decl_die (tree);
5227 static dw_die_ref force_type_die (tree);
5228 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
5229 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
5230 static struct dwarf_file_data * lookup_filename (const char *);
5231 static void retry_incomplete_types (void);
5232 static void gen_type_die_for_member (tree, tree, dw_die_ref);
5233 static void splice_child_die (dw_die_ref, dw_die_ref);
5234 static int file_info_cmp (const void *, const void *);
5235 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
5236 const char *, const char *, unsigned);
5237 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
5238 const char *, const char *,
5239 const char *);
5240 static void output_loc_list (dw_loc_list_ref);
5241 static char *gen_internal_sym (const char *);
5243 static void prune_unmark_dies (dw_die_ref);
5244 static void prune_unused_types_mark (dw_die_ref, int);
5245 static void prune_unused_types_walk (dw_die_ref);
5246 static void prune_unused_types_walk_attribs (dw_die_ref);
5247 static void prune_unused_types_prune (dw_die_ref);
5248 static void prune_unused_types (void);
5249 static int maybe_emit_file (struct dwarf_file_data *fd);
5251 /* Section names used to hold DWARF debugging information. */
5252 #ifndef DEBUG_INFO_SECTION
5253 #define DEBUG_INFO_SECTION ".debug_info"
5254 #endif
5255 #ifndef DEBUG_ABBREV_SECTION
5256 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
5257 #endif
5258 #ifndef DEBUG_ARANGES_SECTION
5259 #define DEBUG_ARANGES_SECTION ".debug_aranges"
5260 #endif
5261 #ifndef DEBUG_MACINFO_SECTION
5262 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
5263 #endif
5264 #ifndef DEBUG_LINE_SECTION
5265 #define DEBUG_LINE_SECTION ".debug_line"
5266 #endif
5267 #ifndef DEBUG_LOC_SECTION
5268 #define DEBUG_LOC_SECTION ".debug_loc"
5269 #endif
5270 #ifndef DEBUG_PUBNAMES_SECTION
5271 #define DEBUG_PUBNAMES_SECTION ".debug_pubnames"
5272 #endif
5273 #ifndef DEBUG_STR_SECTION
5274 #define DEBUG_STR_SECTION ".debug_str"
5275 #endif
5276 #ifndef DEBUG_RANGES_SECTION
5277 #define DEBUG_RANGES_SECTION ".debug_ranges"
5278 #endif
5280 /* Standard ELF section names for compiled code and data. */
5281 #ifndef TEXT_SECTION_NAME
5282 #define TEXT_SECTION_NAME ".text"
5283 #endif
5285 /* Section flags for .debug_str section. */
5286 #define DEBUG_STR_SECTION_FLAGS \
5287 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
5288 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
5289 : SECTION_DEBUG)
5291 /* Labels we insert at beginning sections we can reference instead of
5292 the section names themselves. */
5294 #ifndef TEXT_SECTION_LABEL
5295 #define TEXT_SECTION_LABEL "Ltext"
5296 #endif
5297 #ifndef COLD_TEXT_SECTION_LABEL
5298 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
5299 #endif
5300 #ifndef DEBUG_LINE_SECTION_LABEL
5301 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
5302 #endif
5303 #ifndef DEBUG_INFO_SECTION_LABEL
5304 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
5305 #endif
5306 #ifndef DEBUG_ABBREV_SECTION_LABEL
5307 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
5308 #endif
5309 #ifndef DEBUG_LOC_SECTION_LABEL
5310 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
5311 #endif
5312 #ifndef DEBUG_RANGES_SECTION_LABEL
5313 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
5314 #endif
5315 #ifndef DEBUG_MACINFO_SECTION_LABEL
5316 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
5317 #endif
5319 /* Definitions of defaults for formats and names of various special
5320 (artificial) labels which may be generated within this file (when the -g
5321 options is used and DWARF2_DEBUGGING_INFO is in effect.
5322 If necessary, these may be overridden from within the tm.h file, but
5323 typically, overriding these defaults is unnecessary. */
5325 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
5326 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5327 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5328 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
5329 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5330 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5331 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5332 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5333 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5334 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
5336 #ifndef TEXT_END_LABEL
5337 #define TEXT_END_LABEL "Letext"
5338 #endif
5339 #ifndef COLD_END_LABEL
5340 #define COLD_END_LABEL "Letext_cold"
5341 #endif
5342 #ifndef BLOCK_BEGIN_LABEL
5343 #define BLOCK_BEGIN_LABEL "LBB"
5344 #endif
5345 #ifndef BLOCK_END_LABEL
5346 #define BLOCK_END_LABEL "LBE"
5347 #endif
5348 #ifndef LINE_CODE_LABEL
5349 #define LINE_CODE_LABEL "LM"
5350 #endif
5351 #ifndef SEPARATE_LINE_CODE_LABEL
5352 #define SEPARATE_LINE_CODE_LABEL "LSM"
5353 #endif
5356 /* We allow a language front-end to designate a function that is to be
5357 called to "demangle" any name before it is put into a DIE. */
5359 static const char *(*demangle_name_func) (const char *);
5361 void
5362 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
5364 demangle_name_func = func;
5367 /* Test if rtl node points to a pseudo register. */
5369 static inline int
5370 is_pseudo_reg (const_rtx rtl)
5372 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
5373 || (GET_CODE (rtl) == SUBREG
5374 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
5377 /* Return a reference to a type, with its const and volatile qualifiers
5378 removed. */
5380 static inline tree
5381 type_main_variant (tree type)
5383 type = TYPE_MAIN_VARIANT (type);
5385 /* ??? There really should be only one main variant among any group of
5386 variants of a given type (and all of the MAIN_VARIANT values for all
5387 members of the group should point to that one type) but sometimes the C
5388 front-end messes this up for array types, so we work around that bug
5389 here. */
5390 if (TREE_CODE (type) == ARRAY_TYPE)
5391 while (type != TYPE_MAIN_VARIANT (type))
5392 type = TYPE_MAIN_VARIANT (type);
5394 return type;
5397 /* Return nonzero if the given type node represents a tagged type. */
5399 static inline int
5400 is_tagged_type (const_tree type)
5402 enum tree_code code = TREE_CODE (type);
5404 return (code == RECORD_TYPE || code == UNION_TYPE
5405 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
5408 /* Convert a DIE tag into its string name. */
5410 static const char *
5411 dwarf_tag_name (unsigned int tag)
5413 switch (tag)
5415 case DW_TAG_padding:
5416 return "DW_TAG_padding";
5417 case DW_TAG_array_type:
5418 return "DW_TAG_array_type";
5419 case DW_TAG_class_type:
5420 return "DW_TAG_class_type";
5421 case DW_TAG_entry_point:
5422 return "DW_TAG_entry_point";
5423 case DW_TAG_enumeration_type:
5424 return "DW_TAG_enumeration_type";
5425 case DW_TAG_formal_parameter:
5426 return "DW_TAG_formal_parameter";
5427 case DW_TAG_imported_declaration:
5428 return "DW_TAG_imported_declaration";
5429 case DW_TAG_label:
5430 return "DW_TAG_label";
5431 case DW_TAG_lexical_block:
5432 return "DW_TAG_lexical_block";
5433 case DW_TAG_member:
5434 return "DW_TAG_member";
5435 case DW_TAG_pointer_type:
5436 return "DW_TAG_pointer_type";
5437 case DW_TAG_reference_type:
5438 return "DW_TAG_reference_type";
5439 case DW_TAG_compile_unit:
5440 return "DW_TAG_compile_unit";
5441 case DW_TAG_string_type:
5442 return "DW_TAG_string_type";
5443 case DW_TAG_structure_type:
5444 return "DW_TAG_structure_type";
5445 case DW_TAG_subroutine_type:
5446 return "DW_TAG_subroutine_type";
5447 case DW_TAG_typedef:
5448 return "DW_TAG_typedef";
5449 case DW_TAG_union_type:
5450 return "DW_TAG_union_type";
5451 case DW_TAG_unspecified_parameters:
5452 return "DW_TAG_unspecified_parameters";
5453 case DW_TAG_variant:
5454 return "DW_TAG_variant";
5455 case DW_TAG_common_block:
5456 return "DW_TAG_common_block";
5457 case DW_TAG_common_inclusion:
5458 return "DW_TAG_common_inclusion";
5459 case DW_TAG_inheritance:
5460 return "DW_TAG_inheritance";
5461 case DW_TAG_inlined_subroutine:
5462 return "DW_TAG_inlined_subroutine";
5463 case DW_TAG_module:
5464 return "DW_TAG_module";
5465 case DW_TAG_ptr_to_member_type:
5466 return "DW_TAG_ptr_to_member_type";
5467 case DW_TAG_set_type:
5468 return "DW_TAG_set_type";
5469 case DW_TAG_subrange_type:
5470 return "DW_TAG_subrange_type";
5471 case DW_TAG_with_stmt:
5472 return "DW_TAG_with_stmt";
5473 case DW_TAG_access_declaration:
5474 return "DW_TAG_access_declaration";
5475 case DW_TAG_base_type:
5476 return "DW_TAG_base_type";
5477 case DW_TAG_catch_block:
5478 return "DW_TAG_catch_block";
5479 case DW_TAG_const_type:
5480 return "DW_TAG_const_type";
5481 case DW_TAG_constant:
5482 return "DW_TAG_constant";
5483 case DW_TAG_enumerator:
5484 return "DW_TAG_enumerator";
5485 case DW_TAG_file_type:
5486 return "DW_TAG_file_type";
5487 case DW_TAG_friend:
5488 return "DW_TAG_friend";
5489 case DW_TAG_namelist:
5490 return "DW_TAG_namelist";
5491 case DW_TAG_namelist_item:
5492 return "DW_TAG_namelist_item";
5493 case DW_TAG_packed_type:
5494 return "DW_TAG_packed_type";
5495 case DW_TAG_subprogram:
5496 return "DW_TAG_subprogram";
5497 case DW_TAG_template_type_param:
5498 return "DW_TAG_template_type_param";
5499 case DW_TAG_template_value_param:
5500 return "DW_TAG_template_value_param";
5501 case DW_TAG_thrown_type:
5502 return "DW_TAG_thrown_type";
5503 case DW_TAG_try_block:
5504 return "DW_TAG_try_block";
5505 case DW_TAG_variant_part:
5506 return "DW_TAG_variant_part";
5507 case DW_TAG_variable:
5508 return "DW_TAG_variable";
5509 case DW_TAG_volatile_type:
5510 return "DW_TAG_volatile_type";
5511 case DW_TAG_dwarf_procedure:
5512 return "DW_TAG_dwarf_procedure";
5513 case DW_TAG_restrict_type:
5514 return "DW_TAG_restrict_type";
5515 case DW_TAG_interface_type:
5516 return "DW_TAG_interface_type";
5517 case DW_TAG_namespace:
5518 return "DW_TAG_namespace";
5519 case DW_TAG_imported_module:
5520 return "DW_TAG_imported_module";
5521 case DW_TAG_unspecified_type:
5522 return "DW_TAG_unspecified_type";
5523 case DW_TAG_partial_unit:
5524 return "DW_TAG_partial_unit";
5525 case DW_TAG_imported_unit:
5526 return "DW_TAG_imported_unit";
5527 case DW_TAG_condition:
5528 return "DW_TAG_condition";
5529 case DW_TAG_shared_type:
5530 return "DW_TAG_shared_type";
5531 case DW_TAG_MIPS_loop:
5532 return "DW_TAG_MIPS_loop";
5533 case DW_TAG_format_label:
5534 return "DW_TAG_format_label";
5535 case DW_TAG_function_template:
5536 return "DW_TAG_function_template";
5537 case DW_TAG_class_template:
5538 return "DW_TAG_class_template";
5539 case DW_TAG_GNU_BINCL:
5540 return "DW_TAG_GNU_BINCL";
5541 case DW_TAG_GNU_EINCL:
5542 return "DW_TAG_GNU_EINCL";
5543 default:
5544 return "DW_TAG_<unknown>";
5548 /* Convert a DWARF attribute code into its string name. */
5550 static const char *
5551 dwarf_attr_name (unsigned int attr)
5553 switch (attr)
5555 case DW_AT_sibling:
5556 return "DW_AT_sibling";
5557 case DW_AT_location:
5558 return "DW_AT_location";
5559 case DW_AT_name:
5560 return "DW_AT_name";
5561 case DW_AT_ordering:
5562 return "DW_AT_ordering";
5563 case DW_AT_subscr_data:
5564 return "DW_AT_subscr_data";
5565 case DW_AT_byte_size:
5566 return "DW_AT_byte_size";
5567 case DW_AT_bit_offset:
5568 return "DW_AT_bit_offset";
5569 case DW_AT_bit_size:
5570 return "DW_AT_bit_size";
5571 case DW_AT_element_list:
5572 return "DW_AT_element_list";
5573 case DW_AT_stmt_list:
5574 return "DW_AT_stmt_list";
5575 case DW_AT_low_pc:
5576 return "DW_AT_low_pc";
5577 case DW_AT_high_pc:
5578 return "DW_AT_high_pc";
5579 case DW_AT_language:
5580 return "DW_AT_language";
5581 case DW_AT_member:
5582 return "DW_AT_member";
5583 case DW_AT_discr:
5584 return "DW_AT_discr";
5585 case DW_AT_discr_value:
5586 return "DW_AT_discr_value";
5587 case DW_AT_visibility:
5588 return "DW_AT_visibility";
5589 case DW_AT_import:
5590 return "DW_AT_import";
5591 case DW_AT_string_length:
5592 return "DW_AT_string_length";
5593 case DW_AT_common_reference:
5594 return "DW_AT_common_reference";
5595 case DW_AT_comp_dir:
5596 return "DW_AT_comp_dir";
5597 case DW_AT_const_value:
5598 return "DW_AT_const_value";
5599 case DW_AT_containing_type:
5600 return "DW_AT_containing_type";
5601 case DW_AT_default_value:
5602 return "DW_AT_default_value";
5603 case DW_AT_inline:
5604 return "DW_AT_inline";
5605 case DW_AT_is_optional:
5606 return "DW_AT_is_optional";
5607 case DW_AT_lower_bound:
5608 return "DW_AT_lower_bound";
5609 case DW_AT_producer:
5610 return "DW_AT_producer";
5611 case DW_AT_prototyped:
5612 return "DW_AT_prototyped";
5613 case DW_AT_return_addr:
5614 return "DW_AT_return_addr";
5615 case DW_AT_start_scope:
5616 return "DW_AT_start_scope";
5617 case DW_AT_bit_stride:
5618 return "DW_AT_bit_stride";
5619 case DW_AT_upper_bound:
5620 return "DW_AT_upper_bound";
5621 case DW_AT_abstract_origin:
5622 return "DW_AT_abstract_origin";
5623 case DW_AT_accessibility:
5624 return "DW_AT_accessibility";
5625 case DW_AT_address_class:
5626 return "DW_AT_address_class";
5627 case DW_AT_artificial:
5628 return "DW_AT_artificial";
5629 case DW_AT_base_types:
5630 return "DW_AT_base_types";
5631 case DW_AT_calling_convention:
5632 return "DW_AT_calling_convention";
5633 case DW_AT_count:
5634 return "DW_AT_count";
5635 case DW_AT_data_member_location:
5636 return "DW_AT_data_member_location";
5637 case DW_AT_decl_column:
5638 return "DW_AT_decl_column";
5639 case DW_AT_decl_file:
5640 return "DW_AT_decl_file";
5641 case DW_AT_decl_line:
5642 return "DW_AT_decl_line";
5643 case DW_AT_declaration:
5644 return "DW_AT_declaration";
5645 case DW_AT_discr_list:
5646 return "DW_AT_discr_list";
5647 case DW_AT_encoding:
5648 return "DW_AT_encoding";
5649 case DW_AT_external:
5650 return "DW_AT_external";
5651 case DW_AT_frame_base:
5652 return "DW_AT_frame_base";
5653 case DW_AT_friend:
5654 return "DW_AT_friend";
5655 case DW_AT_identifier_case:
5656 return "DW_AT_identifier_case";
5657 case DW_AT_macro_info:
5658 return "DW_AT_macro_info";
5659 case DW_AT_namelist_items:
5660 return "DW_AT_namelist_items";
5661 case DW_AT_priority:
5662 return "DW_AT_priority";
5663 case DW_AT_segment:
5664 return "DW_AT_segment";
5665 case DW_AT_specification:
5666 return "DW_AT_specification";
5667 case DW_AT_static_link:
5668 return "DW_AT_static_link";
5669 case DW_AT_type:
5670 return "DW_AT_type";
5671 case DW_AT_use_location:
5672 return "DW_AT_use_location";
5673 case DW_AT_variable_parameter:
5674 return "DW_AT_variable_parameter";
5675 case DW_AT_virtuality:
5676 return "DW_AT_virtuality";
5677 case DW_AT_vtable_elem_location:
5678 return "DW_AT_vtable_elem_location";
5680 case DW_AT_allocated:
5681 return "DW_AT_allocated";
5682 case DW_AT_associated:
5683 return "DW_AT_associated";
5684 case DW_AT_data_location:
5685 return "DW_AT_data_location";
5686 case DW_AT_byte_stride:
5687 return "DW_AT_byte_stride";
5688 case DW_AT_entry_pc:
5689 return "DW_AT_entry_pc";
5690 case DW_AT_use_UTF8:
5691 return "DW_AT_use_UTF8";
5692 case DW_AT_extension:
5693 return "DW_AT_extension";
5694 case DW_AT_ranges:
5695 return "DW_AT_ranges";
5696 case DW_AT_trampoline:
5697 return "DW_AT_trampoline";
5698 case DW_AT_call_column:
5699 return "DW_AT_call_column";
5700 case DW_AT_call_file:
5701 return "DW_AT_call_file";
5702 case DW_AT_call_line:
5703 return "DW_AT_call_line";
5705 case DW_AT_MIPS_fde:
5706 return "DW_AT_MIPS_fde";
5707 case DW_AT_MIPS_loop_begin:
5708 return "DW_AT_MIPS_loop_begin";
5709 case DW_AT_MIPS_tail_loop_begin:
5710 return "DW_AT_MIPS_tail_loop_begin";
5711 case DW_AT_MIPS_epilog_begin:
5712 return "DW_AT_MIPS_epilog_begin";
5713 case DW_AT_MIPS_loop_unroll_factor:
5714 return "DW_AT_MIPS_loop_unroll_factor";
5715 case DW_AT_MIPS_software_pipeline_depth:
5716 return "DW_AT_MIPS_software_pipeline_depth";
5717 case DW_AT_MIPS_linkage_name:
5718 return "DW_AT_MIPS_linkage_name";
5719 case DW_AT_MIPS_stride:
5720 return "DW_AT_MIPS_stride";
5721 case DW_AT_MIPS_abstract_name:
5722 return "DW_AT_MIPS_abstract_name";
5723 case DW_AT_MIPS_clone_origin:
5724 return "DW_AT_MIPS_clone_origin";
5725 case DW_AT_MIPS_has_inlines:
5726 return "DW_AT_MIPS_has_inlines";
5728 case DW_AT_sf_names:
5729 return "DW_AT_sf_names";
5730 case DW_AT_src_info:
5731 return "DW_AT_src_info";
5732 case DW_AT_mac_info:
5733 return "DW_AT_mac_info";
5734 case DW_AT_src_coords:
5735 return "DW_AT_src_coords";
5736 case DW_AT_body_begin:
5737 return "DW_AT_body_begin";
5738 case DW_AT_body_end:
5739 return "DW_AT_body_end";
5740 case DW_AT_GNU_vector:
5741 return "DW_AT_GNU_vector";
5743 case DW_AT_VMS_rtnbeg_pd_address:
5744 return "DW_AT_VMS_rtnbeg_pd_address";
5746 default:
5747 return "DW_AT_<unknown>";
5751 /* Convert a DWARF value form code into its string name. */
5753 static const char *
5754 dwarf_form_name (unsigned int form)
5756 switch (form)
5758 case DW_FORM_addr:
5759 return "DW_FORM_addr";
5760 case DW_FORM_block2:
5761 return "DW_FORM_block2";
5762 case DW_FORM_block4:
5763 return "DW_FORM_block4";
5764 case DW_FORM_data2:
5765 return "DW_FORM_data2";
5766 case DW_FORM_data4:
5767 return "DW_FORM_data4";
5768 case DW_FORM_data8:
5769 return "DW_FORM_data8";
5770 case DW_FORM_string:
5771 return "DW_FORM_string";
5772 case DW_FORM_block:
5773 return "DW_FORM_block";
5774 case DW_FORM_block1:
5775 return "DW_FORM_block1";
5776 case DW_FORM_data1:
5777 return "DW_FORM_data1";
5778 case DW_FORM_flag:
5779 return "DW_FORM_flag";
5780 case DW_FORM_sdata:
5781 return "DW_FORM_sdata";
5782 case DW_FORM_strp:
5783 return "DW_FORM_strp";
5784 case DW_FORM_udata:
5785 return "DW_FORM_udata";
5786 case DW_FORM_ref_addr:
5787 return "DW_FORM_ref_addr";
5788 case DW_FORM_ref1:
5789 return "DW_FORM_ref1";
5790 case DW_FORM_ref2:
5791 return "DW_FORM_ref2";
5792 case DW_FORM_ref4:
5793 return "DW_FORM_ref4";
5794 case DW_FORM_ref8:
5795 return "DW_FORM_ref8";
5796 case DW_FORM_ref_udata:
5797 return "DW_FORM_ref_udata";
5798 case DW_FORM_indirect:
5799 return "DW_FORM_indirect";
5800 default:
5801 return "DW_FORM_<unknown>";
5805 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
5806 instance of an inlined instance of a decl which is local to an inline
5807 function, so we have to trace all of the way back through the origin chain
5808 to find out what sort of node actually served as the original seed for the
5809 given block. */
5811 static tree
5812 decl_ultimate_origin (const_tree decl)
5814 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
5815 return NULL_TREE;
5817 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
5818 nodes in the function to point to themselves; ignore that if
5819 we're trying to output the abstract instance of this function. */
5820 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
5821 return NULL_TREE;
5823 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
5824 most distant ancestor, this should never happen. */
5825 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
5827 return DECL_ABSTRACT_ORIGIN (decl);
5830 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
5831 of a virtual function may refer to a base class, so we check the 'this'
5832 parameter. */
5834 static tree
5835 decl_class_context (tree decl)
5837 tree context = NULL_TREE;
5839 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
5840 context = DECL_CONTEXT (decl);
5841 else
5842 context = TYPE_MAIN_VARIANT
5843 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
5845 if (context && !TYPE_P (context))
5846 context = NULL_TREE;
5848 return context;
5851 /* Add an attribute/value pair to a DIE. */
5853 static inline void
5854 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
5856 /* Maybe this should be an assert? */
5857 if (die == NULL)
5858 return;
5860 if (die->die_attr == NULL)
5861 die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
5862 VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
5865 static inline enum dw_val_class
5866 AT_class (dw_attr_ref a)
5868 return a->dw_attr_val.val_class;
5871 /* Add a flag value attribute to a DIE. */
5873 static inline void
5874 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
5876 dw_attr_node attr;
5878 attr.dw_attr = attr_kind;
5879 attr.dw_attr_val.val_class = dw_val_class_flag;
5880 attr.dw_attr_val.v.val_flag = flag;
5881 add_dwarf_attr (die, &attr);
5884 static inline unsigned
5885 AT_flag (dw_attr_ref a)
5887 gcc_assert (a && AT_class (a) == dw_val_class_flag);
5888 return a->dw_attr_val.v.val_flag;
5891 /* Add a signed integer attribute value to a DIE. */
5893 static inline void
5894 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
5896 dw_attr_node attr;
5898 attr.dw_attr = attr_kind;
5899 attr.dw_attr_val.val_class = dw_val_class_const;
5900 attr.dw_attr_val.v.val_int = int_val;
5901 add_dwarf_attr (die, &attr);
5904 static inline HOST_WIDE_INT
5905 AT_int (dw_attr_ref a)
5907 gcc_assert (a && AT_class (a) == dw_val_class_const);
5908 return a->dw_attr_val.v.val_int;
5911 /* Add an unsigned integer attribute value to a DIE. */
5913 static inline void
5914 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
5915 unsigned HOST_WIDE_INT unsigned_val)
5917 dw_attr_node attr;
5919 attr.dw_attr = attr_kind;
5920 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
5921 attr.dw_attr_val.v.val_unsigned = unsigned_val;
5922 add_dwarf_attr (die, &attr);
5925 static inline unsigned HOST_WIDE_INT
5926 AT_unsigned (dw_attr_ref a)
5928 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
5929 return a->dw_attr_val.v.val_unsigned;
5932 /* Add an unsigned double integer attribute value to a DIE. */
5934 static inline void
5935 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
5936 long unsigned int val_hi, long unsigned int val_low)
5938 dw_attr_node attr;
5940 attr.dw_attr = attr_kind;
5941 attr.dw_attr_val.val_class = dw_val_class_long_long;
5942 attr.dw_attr_val.v.val_long_long.hi = val_hi;
5943 attr.dw_attr_val.v.val_long_long.low = val_low;
5944 add_dwarf_attr (die, &attr);
5947 /* Add a floating point attribute value to a DIE and return it. */
5949 static inline void
5950 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
5951 unsigned int length, unsigned int elt_size, unsigned char *array)
5953 dw_attr_node attr;
5955 attr.dw_attr = attr_kind;
5956 attr.dw_attr_val.val_class = dw_val_class_vec;
5957 attr.dw_attr_val.v.val_vec.length = length;
5958 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
5959 attr.dw_attr_val.v.val_vec.array = array;
5960 add_dwarf_attr (die, &attr);
5963 /* Hash and equality functions for debug_str_hash. */
5965 static hashval_t
5966 debug_str_do_hash (const void *x)
5968 return htab_hash_string (((const struct indirect_string_node *)x)->str);
5971 static int
5972 debug_str_eq (const void *x1, const void *x2)
5974 return strcmp ((((const struct indirect_string_node *)x1)->str),
5975 (const char *)x2) == 0;
5978 /* Add a string attribute value to a DIE. */
5980 static inline void
5981 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
5983 dw_attr_node attr;
5984 struct indirect_string_node *node;
5985 void **slot;
5987 if (! debug_str_hash)
5988 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
5989 debug_str_eq, NULL);
5991 slot = htab_find_slot_with_hash (debug_str_hash, str,
5992 htab_hash_string (str), INSERT);
5993 if (*slot == NULL)
5995 node = (struct indirect_string_node *)
5996 ggc_alloc_cleared (sizeof (struct indirect_string_node));
5997 node->str = ggc_strdup (str);
5998 *slot = node;
6000 else
6001 node = (struct indirect_string_node *) *slot;
6003 node->refcount++;
6005 attr.dw_attr = attr_kind;
6006 attr.dw_attr_val.val_class = dw_val_class_str;
6007 attr.dw_attr_val.v.val_str = node;
6008 add_dwarf_attr (die, &attr);
6011 static inline const char *
6012 AT_string (dw_attr_ref a)
6014 gcc_assert (a && AT_class (a) == dw_val_class_str);
6015 return a->dw_attr_val.v.val_str->str;
6018 /* Find out whether a string should be output inline in DIE
6019 or out-of-line in .debug_str section. */
6021 static int
6022 AT_string_form (dw_attr_ref a)
6024 struct indirect_string_node *node;
6025 unsigned int len;
6026 char label[32];
6028 gcc_assert (a && AT_class (a) == dw_val_class_str);
6030 node = a->dw_attr_val.v.val_str;
6031 if (node->form)
6032 return node->form;
6034 len = strlen (node->str) + 1;
6036 /* If the string is shorter or equal to the size of the reference, it is
6037 always better to put it inline. */
6038 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
6039 return node->form = DW_FORM_string;
6041 /* If we cannot expect the linker to merge strings in .debug_str
6042 section, only put it into .debug_str if it is worth even in this
6043 single module. */
6044 if ((debug_str_section->common.flags & SECTION_MERGE) == 0
6045 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
6046 return node->form = DW_FORM_string;
6048 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
6049 ++dw2_string_counter;
6050 node->label = xstrdup (label);
6052 return node->form = DW_FORM_strp;
6055 /* Add a DIE reference attribute value to a DIE. */
6057 static inline void
6058 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
6060 dw_attr_node attr;
6062 attr.dw_attr = attr_kind;
6063 attr.dw_attr_val.val_class = dw_val_class_die_ref;
6064 attr.dw_attr_val.v.val_die_ref.die = targ_die;
6065 attr.dw_attr_val.v.val_die_ref.external = 0;
6066 add_dwarf_attr (die, &attr);
6069 /* Add an AT_specification attribute to a DIE, and also make the back
6070 pointer from the specification to the definition. */
6072 static inline void
6073 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
6075 add_AT_die_ref (die, DW_AT_specification, targ_die);
6076 gcc_assert (!targ_die->die_definition);
6077 targ_die->die_definition = die;
6080 static inline dw_die_ref
6081 AT_ref (dw_attr_ref a)
6083 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6084 return a->dw_attr_val.v.val_die_ref.die;
6087 static inline int
6088 AT_ref_external (dw_attr_ref a)
6090 if (a && AT_class (a) == dw_val_class_die_ref)
6091 return a->dw_attr_val.v.val_die_ref.external;
6093 return 0;
6096 static inline void
6097 set_AT_ref_external (dw_attr_ref a, int i)
6099 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6100 a->dw_attr_val.v.val_die_ref.external = i;
6103 /* Add an FDE reference attribute value to a DIE. */
6105 static inline void
6106 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
6108 dw_attr_node attr;
6110 attr.dw_attr = attr_kind;
6111 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
6112 attr.dw_attr_val.v.val_fde_index = targ_fde;
6113 add_dwarf_attr (die, &attr);
6116 /* Add a location description attribute value to a DIE. */
6118 static inline void
6119 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
6121 dw_attr_node attr;
6123 attr.dw_attr = attr_kind;
6124 attr.dw_attr_val.val_class = dw_val_class_loc;
6125 attr.dw_attr_val.v.val_loc = loc;
6126 add_dwarf_attr (die, &attr);
6129 static inline dw_loc_descr_ref
6130 AT_loc (dw_attr_ref a)
6132 gcc_assert (a && AT_class (a) == dw_val_class_loc);
6133 return a->dw_attr_val.v.val_loc;
6136 static inline void
6137 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
6139 dw_attr_node attr;
6141 attr.dw_attr = attr_kind;
6142 attr.dw_attr_val.val_class = dw_val_class_loc_list;
6143 attr.dw_attr_val.v.val_loc_list = loc_list;
6144 add_dwarf_attr (die, &attr);
6145 have_location_lists = true;
6148 static inline dw_loc_list_ref
6149 AT_loc_list (dw_attr_ref a)
6151 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
6152 return a->dw_attr_val.v.val_loc_list;
6155 /* Add an address constant attribute value to a DIE. */
6157 static inline void
6158 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
6160 dw_attr_node attr;
6162 attr.dw_attr = attr_kind;
6163 attr.dw_attr_val.val_class = dw_val_class_addr;
6164 attr.dw_attr_val.v.val_addr = addr;
6165 add_dwarf_attr (die, &attr);
6168 /* Get the RTX from to an address DIE attribute. */
6170 static inline rtx
6171 AT_addr (dw_attr_ref a)
6173 gcc_assert (a && AT_class (a) == dw_val_class_addr);
6174 return a->dw_attr_val.v.val_addr;
6177 /* Add a file attribute value to a DIE. */
6179 static inline void
6180 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
6181 struct dwarf_file_data *fd)
6183 dw_attr_node attr;
6185 attr.dw_attr = attr_kind;
6186 attr.dw_attr_val.val_class = dw_val_class_file;
6187 attr.dw_attr_val.v.val_file = fd;
6188 add_dwarf_attr (die, &attr);
6191 /* Get the dwarf_file_data from a file DIE attribute. */
6193 static inline struct dwarf_file_data *
6194 AT_file (dw_attr_ref a)
6196 gcc_assert (a && AT_class (a) == dw_val_class_file);
6197 return a->dw_attr_val.v.val_file;
6200 /* Add a label identifier attribute value to a DIE. */
6202 static inline void
6203 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
6205 dw_attr_node attr;
6207 attr.dw_attr = attr_kind;
6208 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
6209 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
6210 add_dwarf_attr (die, &attr);
6213 /* Add a section offset attribute value to a DIE, an offset into the
6214 debug_line section. */
6216 static inline void
6217 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
6218 const char *label)
6220 dw_attr_node attr;
6222 attr.dw_attr = attr_kind;
6223 attr.dw_attr_val.val_class = dw_val_class_lineptr;
6224 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
6225 add_dwarf_attr (die, &attr);
6228 /* Add a section offset attribute value to a DIE, an offset into the
6229 debug_macinfo section. */
6231 static inline void
6232 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
6233 const char *label)
6235 dw_attr_node attr;
6237 attr.dw_attr = attr_kind;
6238 attr.dw_attr_val.val_class = dw_val_class_macptr;
6239 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
6240 add_dwarf_attr (die, &attr);
6243 /* Add an offset attribute value to a DIE. */
6245 static inline void
6246 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
6247 unsigned HOST_WIDE_INT offset)
6249 dw_attr_node attr;
6251 attr.dw_attr = attr_kind;
6252 attr.dw_attr_val.val_class = dw_val_class_offset;
6253 attr.dw_attr_val.v.val_offset = offset;
6254 add_dwarf_attr (die, &attr);
6257 /* Add an range_list attribute value to a DIE. */
6259 static void
6260 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
6261 long unsigned int offset)
6263 dw_attr_node attr;
6265 attr.dw_attr = attr_kind;
6266 attr.dw_attr_val.val_class = dw_val_class_range_list;
6267 attr.dw_attr_val.v.val_offset = offset;
6268 add_dwarf_attr (die, &attr);
6271 static inline const char *
6272 AT_lbl (dw_attr_ref a)
6274 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
6275 || AT_class (a) == dw_val_class_lineptr
6276 || AT_class (a) == dw_val_class_macptr));
6277 return a->dw_attr_val.v.val_lbl_id;
6280 /* Get the attribute of type attr_kind. */
6282 static dw_attr_ref
6283 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
6285 dw_attr_ref a;
6286 unsigned ix;
6287 dw_die_ref spec = NULL;
6289 if (! die)
6290 return NULL;
6292 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6293 if (a->dw_attr == attr_kind)
6294 return a;
6295 else if (a->dw_attr == DW_AT_specification
6296 || a->dw_attr == DW_AT_abstract_origin)
6297 spec = AT_ref (a);
6299 if (spec)
6300 return get_AT (spec, attr_kind);
6302 return NULL;
6305 /* Return the "low pc" attribute value, typically associated with a subprogram
6306 DIE. Return null if the "low pc" attribute is either not present, or if it
6307 cannot be represented as an assembler label identifier. */
6309 static inline const char *
6310 get_AT_low_pc (dw_die_ref die)
6312 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
6314 return a ? AT_lbl (a) : NULL;
6317 /* Return the "high pc" attribute value, typically associated with a subprogram
6318 DIE. Return null if the "high pc" attribute is either not present, or if it
6319 cannot be represented as an assembler label identifier. */
6321 static inline const char *
6322 get_AT_hi_pc (dw_die_ref die)
6324 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
6326 return a ? AT_lbl (a) : NULL;
6329 /* Return the value of the string attribute designated by ATTR_KIND, or
6330 NULL if it is not present. */
6332 static inline const char *
6333 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
6335 dw_attr_ref a = get_AT (die, attr_kind);
6337 return a ? AT_string (a) : NULL;
6340 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
6341 if it is not present. */
6343 static inline int
6344 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
6346 dw_attr_ref a = get_AT (die, attr_kind);
6348 return a ? AT_flag (a) : 0;
6351 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
6352 if it is not present. */
6354 static inline unsigned
6355 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
6357 dw_attr_ref a = get_AT (die, attr_kind);
6359 return a ? AT_unsigned (a) : 0;
6362 static inline dw_die_ref
6363 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
6365 dw_attr_ref a = get_AT (die, attr_kind);
6367 return a ? AT_ref (a) : NULL;
6370 static inline struct dwarf_file_data *
6371 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
6373 dw_attr_ref a = get_AT (die, attr_kind);
6375 return a ? AT_file (a) : NULL;
6378 /* Return TRUE if the language is C or C++. */
6380 static inline bool
6381 is_c_family (void)
6383 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6385 return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
6386 || lang == DW_LANG_C99
6387 || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
6390 /* Return TRUE if the language is C++. */
6392 static inline bool
6393 is_cxx (void)
6395 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6397 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
6400 /* Return TRUE if the language is Fortran. */
6402 static inline bool
6403 is_fortran (void)
6405 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6407 return (lang == DW_LANG_Fortran77
6408 || lang == DW_LANG_Fortran90
6409 || lang == DW_LANG_Fortran95);
6412 /* Return TRUE if the language is Java. */
6414 static inline bool
6415 is_java (void)
6417 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6419 return lang == DW_LANG_Java;
6422 /* Return TRUE if the language is Ada. */
6424 static inline bool
6425 is_ada (void)
6427 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6429 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
6432 /* Remove the specified attribute if present. */
6434 static void
6435 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
6437 dw_attr_ref a;
6438 unsigned ix;
6440 if (! die)
6441 return;
6443 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6444 if (a->dw_attr == attr_kind)
6446 if (AT_class (a) == dw_val_class_str)
6447 if (a->dw_attr_val.v.val_str->refcount)
6448 a->dw_attr_val.v.val_str->refcount--;
6450 /* VEC_ordered_remove should help reduce the number of abbrevs
6451 that are needed. */
6452 VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
6453 return;
6457 /* Remove CHILD from its parent. PREV must have the property that
6458 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
6460 static void
6461 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
6463 gcc_assert (child->die_parent == prev->die_parent);
6464 gcc_assert (prev->die_sib == child);
6465 if (prev == child)
6467 gcc_assert (child->die_parent->die_child == child);
6468 prev = NULL;
6470 else
6471 prev->die_sib = child->die_sib;
6472 if (child->die_parent->die_child == child)
6473 child->die_parent->die_child = prev;
6476 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
6477 matches TAG. */
6479 static void
6480 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
6482 dw_die_ref c;
6484 c = die->die_child;
6485 if (c) do {
6486 dw_die_ref prev = c;
6487 c = c->die_sib;
6488 while (c->die_tag == tag)
6490 remove_child_with_prev (c, prev);
6491 /* Might have removed every child. */
6492 if (c == c->die_sib)
6493 return;
6494 c = c->die_sib;
6496 } while (c != die->die_child);
6499 /* Add a CHILD_DIE as the last child of DIE. */
6501 static void
6502 add_child_die (dw_die_ref die, dw_die_ref child_die)
6504 /* FIXME this should probably be an assert. */
6505 if (! die || ! child_die)
6506 return;
6507 gcc_assert (die != child_die);
6509 child_die->die_parent = die;
6510 if (die->die_child)
6512 child_die->die_sib = die->die_child->die_sib;
6513 die->die_child->die_sib = child_die;
6515 else
6516 child_die->die_sib = child_die;
6517 die->die_child = child_die;
6520 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
6521 is the specification, to the end of PARENT's list of children.
6522 This is done by removing and re-adding it. */
6524 static void
6525 splice_child_die (dw_die_ref parent, dw_die_ref child)
6527 dw_die_ref p;
6529 /* We want the declaration DIE from inside the class, not the
6530 specification DIE at toplevel. */
6531 if (child->die_parent != parent)
6533 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
6535 if (tmp)
6536 child = tmp;
6539 gcc_assert (child->die_parent == parent
6540 || (child->die_parent
6541 == get_AT_ref (parent, DW_AT_specification)));
6543 for (p = child->die_parent->die_child; ; p = p->die_sib)
6544 if (p->die_sib == child)
6546 remove_child_with_prev (child, p);
6547 break;
6550 add_child_die (parent, child);
6553 /* Return a pointer to a newly created DIE node. */
6555 static inline dw_die_ref
6556 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
6558 dw_die_ref die = GGC_CNEW (die_node);
6560 die->die_tag = tag_value;
6562 if (parent_die != NULL)
6563 add_child_die (parent_die, die);
6564 else
6566 limbo_die_node *limbo_node;
6568 limbo_node = GGC_CNEW (limbo_die_node);
6569 limbo_node->die = die;
6570 limbo_node->created_for = t;
6571 limbo_node->next = limbo_die_list;
6572 limbo_die_list = limbo_node;
6575 return die;
6578 /* Return the DIE associated with the given type specifier. */
6580 static inline dw_die_ref
6581 lookup_type_die (tree type)
6583 return TYPE_SYMTAB_DIE (type);
6586 /* Equate a DIE to a given type specifier. */
6588 static inline void
6589 equate_type_number_to_die (tree type, dw_die_ref type_die)
6591 TYPE_SYMTAB_DIE (type) = type_die;
6594 /* Returns a hash value for X (which really is a die_struct). */
6596 static hashval_t
6597 decl_die_table_hash (const void *x)
6599 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
6602 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
6604 static int
6605 decl_die_table_eq (const void *x, const void *y)
6607 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
6610 /* Return the DIE associated with a given declaration. */
6612 static inline dw_die_ref
6613 lookup_decl_die (tree decl)
6615 return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
6618 /* Returns a hash value for X (which really is a var_loc_list). */
6620 static hashval_t
6621 decl_loc_table_hash (const void *x)
6623 return (hashval_t) ((const var_loc_list *) x)->decl_id;
6626 /* Return nonzero if decl_id of var_loc_list X is the same as
6627 UID of decl *Y. */
6629 static int
6630 decl_loc_table_eq (const void *x, const void *y)
6632 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
6635 /* Return the var_loc list associated with a given declaration. */
6637 static inline var_loc_list *
6638 lookup_decl_loc (const_tree decl)
6640 return (var_loc_list *)
6641 htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
6644 /* Equate a DIE to a particular declaration. */
6646 static void
6647 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
6649 unsigned int decl_id = DECL_UID (decl);
6650 void **slot;
6652 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
6653 *slot = decl_die;
6654 decl_die->decl_id = decl_id;
6657 /* Add a variable location node to the linked list for DECL. */
6659 static void
6660 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
6662 unsigned int decl_id = DECL_UID (decl);
6663 var_loc_list *temp;
6664 void **slot;
6666 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
6667 if (*slot == NULL)
6669 temp = GGC_CNEW (var_loc_list);
6670 temp->decl_id = decl_id;
6671 *slot = temp;
6673 else
6674 temp = (var_loc_list *) *slot;
6676 if (temp->last)
6678 /* If the current location is the same as the end of the list,
6679 and either both or neither of the locations is uninitialized,
6680 we have nothing to do. */
6681 if ((!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
6682 NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
6683 || ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
6684 != NOTE_VAR_LOCATION_STATUS (loc->var_loc_note))
6685 && ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
6686 == VAR_INIT_STATUS_UNINITIALIZED)
6687 || (NOTE_VAR_LOCATION_STATUS (loc->var_loc_note)
6688 == VAR_INIT_STATUS_UNINITIALIZED))))
6690 /* Add LOC to the end of list and update LAST. */
6691 temp->last->next = loc;
6692 temp->last = loc;
6695 /* Do not add empty location to the beginning of the list. */
6696 else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
6698 temp->first = loc;
6699 temp->last = loc;
6703 /* Keep track of the number of spaces used to indent the
6704 output of the debugging routines that print the structure of
6705 the DIE internal representation. */
6706 static int print_indent;
6708 /* Indent the line the number of spaces given by print_indent. */
6710 static inline void
6711 print_spaces (FILE *outfile)
6713 fprintf (outfile, "%*s", print_indent, "");
6716 /* Print the information associated with a given DIE, and its children.
6717 This routine is a debugging aid only. */
6719 static void
6720 print_die (dw_die_ref die, FILE *outfile)
6722 dw_attr_ref a;
6723 dw_die_ref c;
6724 unsigned ix;
6726 print_spaces (outfile);
6727 fprintf (outfile, "DIE %4ld: %s\n",
6728 die->die_offset, dwarf_tag_name (die->die_tag));
6729 print_spaces (outfile);
6730 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
6731 fprintf (outfile, " offset: %ld\n", die->die_offset);
6733 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6735 print_spaces (outfile);
6736 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
6738 switch (AT_class (a))
6740 case dw_val_class_addr:
6741 fprintf (outfile, "address");
6742 break;
6743 case dw_val_class_offset:
6744 fprintf (outfile, "offset");
6745 break;
6746 case dw_val_class_loc:
6747 fprintf (outfile, "location descriptor");
6748 break;
6749 case dw_val_class_loc_list:
6750 fprintf (outfile, "location list -> label:%s",
6751 AT_loc_list (a)->ll_symbol);
6752 break;
6753 case dw_val_class_range_list:
6754 fprintf (outfile, "range list");
6755 break;
6756 case dw_val_class_const:
6757 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
6758 break;
6759 case dw_val_class_unsigned_const:
6760 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
6761 break;
6762 case dw_val_class_long_long:
6763 fprintf (outfile, "constant (%lu,%lu)",
6764 a->dw_attr_val.v.val_long_long.hi,
6765 a->dw_attr_val.v.val_long_long.low);
6766 break;
6767 case dw_val_class_vec:
6768 fprintf (outfile, "floating-point or vector constant");
6769 break;
6770 case dw_val_class_flag:
6771 fprintf (outfile, "%u", AT_flag (a));
6772 break;
6773 case dw_val_class_die_ref:
6774 if (AT_ref (a) != NULL)
6776 if (AT_ref (a)->die_symbol)
6777 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
6778 else
6779 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
6781 else
6782 fprintf (outfile, "die -> <null>");
6783 break;
6784 case dw_val_class_lbl_id:
6785 case dw_val_class_lineptr:
6786 case dw_val_class_macptr:
6787 fprintf (outfile, "label: %s", AT_lbl (a));
6788 break;
6789 case dw_val_class_str:
6790 if (AT_string (a) != NULL)
6791 fprintf (outfile, "\"%s\"", AT_string (a));
6792 else
6793 fprintf (outfile, "<null>");
6794 break;
6795 case dw_val_class_file:
6796 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
6797 AT_file (a)->emitted_number);
6798 break;
6799 default:
6800 break;
6803 fprintf (outfile, "\n");
6806 if (die->die_child != NULL)
6808 print_indent += 4;
6809 FOR_EACH_CHILD (die, c, print_die (c, outfile));
6810 print_indent -= 4;
6812 if (print_indent == 0)
6813 fprintf (outfile, "\n");
6816 /* Print the contents of the source code line number correspondence table.
6817 This routine is a debugging aid only. */
6819 static void
6820 print_dwarf_line_table (FILE *outfile)
6822 unsigned i;
6823 dw_line_info_ref line_info;
6825 fprintf (outfile, "\n\nDWARF source line information\n");
6826 for (i = 1; i < line_info_table_in_use; i++)
6828 line_info = &line_info_table[i];
6829 fprintf (outfile, "%5d: %4ld %6ld\n", i,
6830 line_info->dw_file_num,
6831 line_info->dw_line_num);
6834 fprintf (outfile, "\n\n");
6837 /* Print the information collected for a given DIE. */
6839 void
6840 debug_dwarf_die (dw_die_ref die)
6842 print_die (die, stderr);
6845 /* Print all DWARF information collected for the compilation unit.
6846 This routine is a debugging aid only. */
6848 void
6849 debug_dwarf (void)
6851 print_indent = 0;
6852 print_die (comp_unit_die, stderr);
6853 if (! DWARF2_ASM_LINE_DEBUG_INFO)
6854 print_dwarf_line_table (stderr);
6857 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
6858 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
6859 DIE that marks the start of the DIEs for this include file. */
6861 static dw_die_ref
6862 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
6864 const char *filename = get_AT_string (bincl_die, DW_AT_name);
6865 dw_die_ref new_unit = gen_compile_unit_die (filename);
6867 new_unit->die_sib = old_unit;
6868 return new_unit;
6871 /* Close an include-file CU and reopen the enclosing one. */
6873 static dw_die_ref
6874 pop_compile_unit (dw_die_ref old_unit)
6876 dw_die_ref new_unit = old_unit->die_sib;
6878 old_unit->die_sib = NULL;
6879 return new_unit;
6882 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6883 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6885 /* Calculate the checksum of a location expression. */
6887 static inline void
6888 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6890 CHECKSUM (loc->dw_loc_opc);
6891 CHECKSUM (loc->dw_loc_oprnd1);
6892 CHECKSUM (loc->dw_loc_oprnd2);
6895 /* Calculate the checksum of an attribute. */
6897 static void
6898 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
6900 dw_loc_descr_ref loc;
6901 rtx r;
6903 CHECKSUM (at->dw_attr);
6905 /* We don't care that this was compiled with a different compiler
6906 snapshot; if the output is the same, that's what matters. */
6907 if (at->dw_attr == DW_AT_producer)
6908 return;
6910 switch (AT_class (at))
6912 case dw_val_class_const:
6913 CHECKSUM (at->dw_attr_val.v.val_int);
6914 break;
6915 case dw_val_class_unsigned_const:
6916 CHECKSUM (at->dw_attr_val.v.val_unsigned);
6917 break;
6918 case dw_val_class_long_long:
6919 CHECKSUM (at->dw_attr_val.v.val_long_long);
6920 break;
6921 case dw_val_class_vec:
6922 CHECKSUM (at->dw_attr_val.v.val_vec);
6923 break;
6924 case dw_val_class_flag:
6925 CHECKSUM (at->dw_attr_val.v.val_flag);
6926 break;
6927 case dw_val_class_str:
6928 CHECKSUM_STRING (AT_string (at));
6929 break;
6931 case dw_val_class_addr:
6932 r = AT_addr (at);
6933 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6934 CHECKSUM_STRING (XSTR (r, 0));
6935 break;
6937 case dw_val_class_offset:
6938 CHECKSUM (at->dw_attr_val.v.val_offset);
6939 break;
6941 case dw_val_class_loc:
6942 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6943 loc_checksum (loc, ctx);
6944 break;
6946 case dw_val_class_die_ref:
6947 die_checksum (AT_ref (at), ctx, mark);
6948 break;
6950 case dw_val_class_fde_ref:
6951 case dw_val_class_lbl_id:
6952 case dw_val_class_lineptr:
6953 case dw_val_class_macptr:
6954 break;
6956 case dw_val_class_file:
6957 CHECKSUM_STRING (AT_file (at)->filename);
6958 break;
6960 default:
6961 break;
6965 /* Calculate the checksum of a DIE. */
6967 static void
6968 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6970 dw_die_ref c;
6971 dw_attr_ref a;
6972 unsigned ix;
6974 /* To avoid infinite recursion. */
6975 if (die->die_mark)
6977 CHECKSUM (die->die_mark);
6978 return;
6980 die->die_mark = ++(*mark);
6982 CHECKSUM (die->die_tag);
6984 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6985 attr_checksum (a, ctx, mark);
6987 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6990 #undef CHECKSUM
6991 #undef CHECKSUM_STRING
6993 /* Do the location expressions look same? */
6994 static inline int
6995 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6997 return loc1->dw_loc_opc == loc2->dw_loc_opc
6998 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6999 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7002 /* Do the values look the same? */
7003 static int
7004 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7006 dw_loc_descr_ref loc1, loc2;
7007 rtx r1, r2;
7009 if (v1->val_class != v2->val_class)
7010 return 0;
7012 switch (v1->val_class)
7014 case dw_val_class_const:
7015 return v1->v.val_int == v2->v.val_int;
7016 case dw_val_class_unsigned_const:
7017 return v1->v.val_unsigned == v2->v.val_unsigned;
7018 case dw_val_class_long_long:
7019 return v1->v.val_long_long.hi == v2->v.val_long_long.hi
7020 && v1->v.val_long_long.low == v2->v.val_long_long.low;
7021 case dw_val_class_vec:
7022 if (v1->v.val_vec.length != v2->v.val_vec.length
7023 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7024 return 0;
7025 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7026 v1->v.val_vec.length * v1->v.val_vec.elt_size))
7027 return 0;
7028 return 1;
7029 case dw_val_class_flag:
7030 return v1->v.val_flag == v2->v.val_flag;
7031 case dw_val_class_str:
7032 return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
7034 case dw_val_class_addr:
7035 r1 = v1->v.val_addr;
7036 r2 = v2->v.val_addr;
7037 if (GET_CODE (r1) != GET_CODE (r2))
7038 return 0;
7039 gcc_assert (GET_CODE (r1) == SYMBOL_REF);
7040 return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
7042 case dw_val_class_offset:
7043 return v1->v.val_offset == v2->v.val_offset;
7045 case dw_val_class_loc:
7046 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7047 loc1 && loc2;
7048 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7049 if (!same_loc_p (loc1, loc2, mark))
7050 return 0;
7051 return !loc1 && !loc2;
7053 case dw_val_class_die_ref:
7054 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7056 case dw_val_class_fde_ref:
7057 case dw_val_class_lbl_id:
7058 case dw_val_class_lineptr:
7059 case dw_val_class_macptr:
7060 return 1;
7062 case dw_val_class_file:
7063 return v1->v.val_file == v2->v.val_file;
7065 default:
7066 return 1;
7070 /* Do the attributes look the same? */
7072 static int
7073 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
7075 if (at1->dw_attr != at2->dw_attr)
7076 return 0;
7078 /* We don't care that this was compiled with a different compiler
7079 snapshot; if the output is the same, that's what matters. */
7080 if (at1->dw_attr == DW_AT_producer)
7081 return 1;
7083 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7086 /* Do the dies look the same? */
7088 static int
7089 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7091 dw_die_ref c1, c2;
7092 dw_attr_ref a1;
7093 unsigned ix;
7095 /* To avoid infinite recursion. */
7096 if (die1->die_mark)
7097 return die1->die_mark == die2->die_mark;
7098 die1->die_mark = die2->die_mark = ++(*mark);
7100 if (die1->die_tag != die2->die_tag)
7101 return 0;
7103 if (VEC_length (dw_attr_node, die1->die_attr)
7104 != VEC_length (dw_attr_node, die2->die_attr))
7105 return 0;
7107 for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
7108 if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
7109 return 0;
7111 c1 = die1->die_child;
7112 c2 = die2->die_child;
7113 if (! c1)
7115 if (c2)
7116 return 0;
7118 else
7119 for (;;)
7121 if (!same_die_p (c1, c2, mark))
7122 return 0;
7123 c1 = c1->die_sib;
7124 c2 = c2->die_sib;
7125 if (c1 == die1->die_child)
7127 if (c2 == die2->die_child)
7128 break;
7129 else
7130 return 0;
7134 return 1;
7137 /* Do the dies look the same? Wrapper around same_die_p. */
7139 static int
7140 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
7142 int mark = 0;
7143 int ret = same_die_p (die1, die2, &mark);
7145 unmark_all_dies (die1);
7146 unmark_all_dies (die2);
7148 return ret;
7151 /* The prefix to attach to symbols on DIEs in the current comdat debug
7152 info section. */
7153 static char *comdat_symbol_id;
7155 /* The index of the current symbol within the current comdat CU. */
7156 static unsigned int comdat_symbol_number;
7158 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7159 children, and set comdat_symbol_id accordingly. */
7161 static void
7162 compute_section_prefix (dw_die_ref unit_die)
7164 const char *die_name = get_AT_string (unit_die, DW_AT_name);
7165 const char *base = die_name ? lbasename (die_name) : "anonymous";
7166 char *name = XALLOCAVEC (char, strlen (base) + 64);
7167 char *p;
7168 int i, mark;
7169 unsigned char checksum[16];
7170 struct md5_ctx ctx;
7172 /* Compute the checksum of the DIE, then append part of it as hex digits to
7173 the name filename of the unit. */
7175 md5_init_ctx (&ctx);
7176 mark = 0;
7177 die_checksum (unit_die, &ctx, &mark);
7178 unmark_all_dies (unit_die);
7179 md5_finish_ctx (&ctx, checksum);
7181 sprintf (name, "%s.", base);
7182 clean_symbol_name (name);
7184 p = name + strlen (name);
7185 for (i = 0; i < 4; i++)
7187 sprintf (p, "%.2x", checksum[i]);
7188 p += 2;
7191 comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
7192 comdat_symbol_number = 0;
7195 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
7197 static int
7198 is_type_die (dw_die_ref die)
7200 switch (die->die_tag)
7202 case DW_TAG_array_type:
7203 case DW_TAG_class_type:
7204 case DW_TAG_interface_type:
7205 case DW_TAG_enumeration_type:
7206 case DW_TAG_pointer_type:
7207 case DW_TAG_reference_type:
7208 case DW_TAG_string_type:
7209 case DW_TAG_structure_type:
7210 case DW_TAG_subroutine_type:
7211 case DW_TAG_union_type:
7212 case DW_TAG_ptr_to_member_type:
7213 case DW_TAG_set_type:
7214 case DW_TAG_subrange_type:
7215 case DW_TAG_base_type:
7216 case DW_TAG_const_type:
7217 case DW_TAG_file_type:
7218 case DW_TAG_packed_type:
7219 case DW_TAG_volatile_type:
7220 case DW_TAG_typedef:
7221 return 1;
7222 default:
7223 return 0;
7227 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7228 Basically, we want to choose the bits that are likely to be shared between
7229 compilations (types) and leave out the bits that are specific to individual
7230 compilations (functions). */
7232 static int
7233 is_comdat_die (dw_die_ref c)
7235 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7236 we do for stabs. The advantage is a greater likelihood of sharing between
7237 objects that don't include headers in the same order (and therefore would
7238 put the base types in a different comdat). jason 8/28/00 */
7240 if (c->die_tag == DW_TAG_base_type)
7241 return 0;
7243 if (c->die_tag == DW_TAG_pointer_type
7244 || c->die_tag == DW_TAG_reference_type
7245 || c->die_tag == DW_TAG_const_type
7246 || c->die_tag == DW_TAG_volatile_type)
7248 dw_die_ref t = get_AT_ref (c, DW_AT_type);
7250 return t ? is_comdat_die (t) : 0;
7253 return is_type_die (c);
7256 /* Returns 1 iff C is the sort of DIE that might be referred to from another
7257 compilation unit. */
7259 static int
7260 is_symbol_die (dw_die_ref c)
7262 return (is_type_die (c)
7263 || (get_AT (c, DW_AT_declaration)
7264 && !get_AT (c, DW_AT_specification))
7265 || c->die_tag == DW_TAG_namespace
7266 || c->die_tag == DW_TAG_module);
7269 static char *
7270 gen_internal_sym (const char *prefix)
7272 char buf[256];
7274 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7275 return xstrdup (buf);
7278 /* Assign symbols to all worthy DIEs under DIE. */
7280 static void
7281 assign_symbol_names (dw_die_ref die)
7283 dw_die_ref c;
7285 if (is_symbol_die (die))
7287 if (comdat_symbol_id)
7289 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
7291 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
7292 comdat_symbol_id, comdat_symbol_number++);
7293 die->die_symbol = xstrdup (p);
7295 else
7296 die->die_symbol = gen_internal_sym ("LDIE");
7299 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
7302 struct cu_hash_table_entry
7304 dw_die_ref cu;
7305 unsigned min_comdat_num, max_comdat_num;
7306 struct cu_hash_table_entry *next;
7309 /* Routines to manipulate hash table of CUs. */
7310 static hashval_t
7311 htab_cu_hash (const void *of)
7313 const struct cu_hash_table_entry *const entry =
7314 (const struct cu_hash_table_entry *) of;
7316 return htab_hash_string (entry->cu->die_symbol);
7319 static int
7320 htab_cu_eq (const void *of1, const void *of2)
7322 const struct cu_hash_table_entry *const entry1 =
7323 (const struct cu_hash_table_entry *) of1;
7324 const struct die_struct *const entry2 = (const struct die_struct *) of2;
7326 return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
7329 static void
7330 htab_cu_del (void *what)
7332 struct cu_hash_table_entry *next,
7333 *entry = (struct cu_hash_table_entry *) what;
7335 while (entry)
7337 next = entry->next;
7338 free (entry);
7339 entry = next;
7343 /* Check whether we have already seen this CU and set up SYM_NUM
7344 accordingly. */
7345 static int
7346 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
7348 struct cu_hash_table_entry dummy;
7349 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7351 dummy.max_comdat_num = 0;
7353 slot = (struct cu_hash_table_entry **)
7354 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
7355 INSERT);
7356 entry = *slot;
7358 for (; entry; last = entry, entry = entry->next)
7360 if (same_die_p_wrap (cu, entry->cu))
7361 break;
7364 if (entry)
7366 *sym_num = entry->min_comdat_num;
7367 return 1;
7370 entry = XCNEW (struct cu_hash_table_entry);
7371 entry->cu = cu;
7372 entry->min_comdat_num = *sym_num = last->max_comdat_num;
7373 entry->next = *slot;
7374 *slot = entry;
7376 return 0;
7379 /* Record SYM_NUM to record of CU in HTABLE. */
7380 static void
7381 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
7383 struct cu_hash_table_entry **slot, *entry;
7385 slot = (struct cu_hash_table_entry **)
7386 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
7387 NO_INSERT);
7388 entry = *slot;
7390 entry->max_comdat_num = sym_num;
7393 /* Traverse the DIE (which is always comp_unit_die), and set up
7394 additional compilation units for each of the include files we see
7395 bracketed by BINCL/EINCL. */
7397 static void
7398 break_out_includes (dw_die_ref die)
7400 dw_die_ref c;
7401 dw_die_ref unit = NULL;
7402 limbo_die_node *node, **pnode;
7403 htab_t cu_hash_table;
7405 c = die->die_child;
7406 if (c) do {
7407 dw_die_ref prev = c;
7408 c = c->die_sib;
7409 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7410 || (unit && is_comdat_die (c)))
7412 dw_die_ref next = c->die_sib;
7414 /* This DIE is for a secondary CU; remove it from the main one. */
7415 remove_child_with_prev (c, prev);
7417 if (c->die_tag == DW_TAG_GNU_BINCL)
7418 unit = push_new_compile_unit (unit, c);
7419 else if (c->die_tag == DW_TAG_GNU_EINCL)
7420 unit = pop_compile_unit (unit);
7421 else
7422 add_child_die (unit, c);
7423 c = next;
7424 if (c == die->die_child)
7425 break;
7427 } while (c != die->die_child);
7429 #if 0
7430 /* We can only use this in debugging, since the frontend doesn't check
7431 to make sure that we leave every include file we enter. */
7432 gcc_assert (!unit);
7433 #endif
7435 assign_symbol_names (die);
7436 cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
7437 for (node = limbo_die_list, pnode = &limbo_die_list;
7438 node;
7439 node = node->next)
7441 int is_dupl;
7443 compute_section_prefix (node->die);
7444 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
7445 &comdat_symbol_number);
7446 assign_symbol_names (node->die);
7447 if (is_dupl)
7448 *pnode = node->next;
7449 else
7451 pnode = &node->next;
7452 record_comdat_symbol_number (node->die, cu_hash_table,
7453 comdat_symbol_number);
7456 htab_delete (cu_hash_table);
7459 /* Traverse the DIE and add a sibling attribute if it may have the
7460 effect of speeding up access to siblings. To save some space,
7461 avoid generating sibling attributes for DIE's without children. */
7463 static void
7464 add_sibling_attributes (dw_die_ref die)
7466 dw_die_ref c;
7468 if (! die->die_child)
7469 return;
7471 if (die->die_parent && die != die->die_parent->die_child)
7472 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7474 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7477 /* Output all location lists for the DIE and its children. */
7479 static void
7480 output_location_lists (dw_die_ref die)
7482 dw_die_ref c;
7483 dw_attr_ref a;
7484 unsigned ix;
7486 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7487 if (AT_class (a) == dw_val_class_loc_list)
7488 output_loc_list (AT_loc_list (a));
7490 FOR_EACH_CHILD (die, c, output_location_lists (c));
7493 /* The format of each DIE (and its attribute value pairs) is encoded in an
7494 abbreviation table. This routine builds the abbreviation table and assigns
7495 a unique abbreviation id for each abbreviation entry. The children of each
7496 die are visited recursively. */
7498 static void
7499 build_abbrev_table (dw_die_ref die)
7501 unsigned long abbrev_id;
7502 unsigned int n_alloc;
7503 dw_die_ref c;
7504 dw_attr_ref a;
7505 unsigned ix;
7507 /* Scan the DIE references, and mark as external any that refer to
7508 DIEs from other CUs (i.e. those which are not marked). */
7509 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7510 if (AT_class (a) == dw_val_class_die_ref
7511 && AT_ref (a)->die_mark == 0)
7513 gcc_assert (AT_ref (a)->die_symbol);
7515 set_AT_ref_external (a, 1);
7518 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7520 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7521 dw_attr_ref die_a, abbrev_a;
7522 unsigned ix;
7523 bool ok = true;
7525 if (abbrev->die_tag != die->die_tag)
7526 continue;
7527 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7528 continue;
7530 if (VEC_length (dw_attr_node, abbrev->die_attr)
7531 != VEC_length (dw_attr_node, die->die_attr))
7532 continue;
7534 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
7536 abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
7537 if ((abbrev_a->dw_attr != die_a->dw_attr)
7538 || (value_format (abbrev_a) != value_format (die_a)))
7540 ok = false;
7541 break;
7544 if (ok)
7545 break;
7548 if (abbrev_id >= abbrev_die_table_in_use)
7550 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7552 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7553 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7554 n_alloc);
7556 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7557 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7558 abbrev_die_table_allocated = n_alloc;
7561 ++abbrev_die_table_in_use;
7562 abbrev_die_table[abbrev_id] = die;
7565 die->die_abbrev = abbrev_id;
7566 FOR_EACH_CHILD (die, c, build_abbrev_table (c));
7569 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7571 static int
7572 constant_size (unsigned HOST_WIDE_INT value)
7574 int log;
7576 if (value == 0)
7577 log = 0;
7578 else
7579 log = floor_log2 (value);
7581 log = log / 8;
7582 log = 1 << (floor_log2 (log) + 1);
7584 return log;
7587 /* Return the size of a DIE as it is represented in the
7588 .debug_info section. */
7590 static unsigned long
7591 size_of_die (dw_die_ref die)
7593 unsigned long size = 0;
7594 dw_attr_ref a;
7595 unsigned ix;
7597 size += size_of_uleb128 (die->die_abbrev);
7598 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7600 switch (AT_class (a))
7602 case dw_val_class_addr:
7603 size += DWARF2_ADDR_SIZE;
7604 break;
7605 case dw_val_class_offset:
7606 size += DWARF_OFFSET_SIZE;
7607 break;
7608 case dw_val_class_loc:
7610 unsigned long lsize = size_of_locs (AT_loc (a));
7612 /* Block length. */
7613 size += constant_size (lsize);
7614 size += lsize;
7616 break;
7617 case dw_val_class_loc_list:
7618 size += DWARF_OFFSET_SIZE;
7619 break;
7620 case dw_val_class_range_list:
7621 size += DWARF_OFFSET_SIZE;
7622 break;
7623 case dw_val_class_const:
7624 size += size_of_sleb128 (AT_int (a));
7625 break;
7626 case dw_val_class_unsigned_const:
7627 size += constant_size (AT_unsigned (a));
7628 break;
7629 case dw_val_class_long_long:
7630 size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
7631 break;
7632 case dw_val_class_vec:
7633 size += constant_size (a->dw_attr_val.v.val_vec.length
7634 * a->dw_attr_val.v.val_vec.elt_size)
7635 + a->dw_attr_val.v.val_vec.length
7636 * a->dw_attr_val.v.val_vec.elt_size; /* block */
7637 break;
7638 case dw_val_class_flag:
7639 size += 1;
7640 break;
7641 case dw_val_class_die_ref:
7642 if (AT_ref_external (a))
7643 size += DWARF2_ADDR_SIZE;
7644 else
7645 size += DWARF_OFFSET_SIZE;
7646 break;
7647 case dw_val_class_fde_ref:
7648 size += DWARF_OFFSET_SIZE;
7649 break;
7650 case dw_val_class_lbl_id:
7651 size += DWARF2_ADDR_SIZE;
7652 break;
7653 case dw_val_class_lineptr:
7654 case dw_val_class_macptr:
7655 size += DWARF_OFFSET_SIZE;
7656 break;
7657 case dw_val_class_str:
7658 if (AT_string_form (a) == DW_FORM_strp)
7659 size += DWARF_OFFSET_SIZE;
7660 else
7661 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7662 break;
7663 case dw_val_class_file:
7664 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7665 break;
7666 default:
7667 gcc_unreachable ();
7671 return size;
7674 /* Size the debugging information associated with a given DIE. Visits the
7675 DIE's children recursively. Updates the global variable next_die_offset, on
7676 each time through. Uses the current value of next_die_offset to update the
7677 die_offset field in each DIE. */
7679 static void
7680 calc_die_sizes (dw_die_ref die)
7682 dw_die_ref c;
7684 die->die_offset = next_die_offset;
7685 next_die_offset += size_of_die (die);
7687 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7689 if (die->die_child != NULL)
7690 /* Count the null byte used to terminate sibling lists. */
7691 next_die_offset += 1;
7694 /* Set the marks for a die and its children. We do this so
7695 that we know whether or not a reference needs to use FORM_ref_addr; only
7696 DIEs in the same CU will be marked. We used to clear out the offset
7697 and use that as the flag, but ran into ordering problems. */
7699 static void
7700 mark_dies (dw_die_ref die)
7702 dw_die_ref c;
7704 gcc_assert (!die->die_mark);
7706 die->die_mark = 1;
7707 FOR_EACH_CHILD (die, c, mark_dies (c));
7710 /* Clear the marks for a die and its children. */
7712 static void
7713 unmark_dies (dw_die_ref die)
7715 dw_die_ref c;
7717 gcc_assert (die->die_mark);
7719 die->die_mark = 0;
7720 FOR_EACH_CHILD (die, c, unmark_dies (c));
7723 /* Clear the marks for a die, its children and referred dies. */
7725 static void
7726 unmark_all_dies (dw_die_ref die)
7728 dw_die_ref c;
7729 dw_attr_ref a;
7730 unsigned ix;
7732 if (!die->die_mark)
7733 return;
7734 die->die_mark = 0;
7736 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7738 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7739 if (AT_class (a) == dw_val_class_die_ref)
7740 unmark_all_dies (AT_ref (a));
7743 /* Return the size of the .debug_pubnames or .debug_pubtypes table
7744 generated for the compilation unit. */
7746 static unsigned long
7747 size_of_pubnames (VEC (pubname_entry, gc) * names)
7749 unsigned long size;
7750 unsigned i;
7751 pubname_ref p;
7753 size = DWARF_PUBNAMES_HEADER_SIZE;
7754 for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
7755 if (names != pubtype_table
7756 || p->die->die_offset != 0
7757 || !flag_eliminate_unused_debug_types)
7758 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
7760 size += DWARF_OFFSET_SIZE;
7761 return size;
7764 /* Return the size of the information in the .debug_aranges section. */
7766 static unsigned long
7767 size_of_aranges (void)
7769 unsigned long size;
7771 size = DWARF_ARANGES_HEADER_SIZE;
7773 /* Count the address/length pair for this compilation unit. */
7774 if (text_section_used)
7775 size += 2 * DWARF2_ADDR_SIZE;
7776 if (cold_text_section_used)
7777 size += 2 * DWARF2_ADDR_SIZE;
7778 size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
7780 /* Count the two zero words used to terminated the address range table. */
7781 size += 2 * DWARF2_ADDR_SIZE;
7782 return size;
7785 /* Select the encoding of an attribute value. */
7787 static enum dwarf_form
7788 value_format (dw_attr_ref a)
7790 switch (a->dw_attr_val.val_class)
7792 case dw_val_class_addr:
7793 return DW_FORM_addr;
7794 case dw_val_class_range_list:
7795 case dw_val_class_offset:
7796 case dw_val_class_loc_list:
7797 switch (DWARF_OFFSET_SIZE)
7799 case 4:
7800 return DW_FORM_data4;
7801 case 8:
7802 return DW_FORM_data8;
7803 default:
7804 gcc_unreachable ();
7806 case dw_val_class_loc:
7807 switch (constant_size (size_of_locs (AT_loc (a))))
7809 case 1:
7810 return DW_FORM_block1;
7811 case 2:
7812 return DW_FORM_block2;
7813 default:
7814 gcc_unreachable ();
7816 case dw_val_class_const:
7817 return DW_FORM_sdata;
7818 case dw_val_class_unsigned_const:
7819 switch (constant_size (AT_unsigned (a)))
7821 case 1:
7822 return DW_FORM_data1;
7823 case 2:
7824 return DW_FORM_data2;
7825 case 4:
7826 return DW_FORM_data4;
7827 case 8:
7828 return DW_FORM_data8;
7829 default:
7830 gcc_unreachable ();
7832 case dw_val_class_long_long:
7833 return DW_FORM_block1;
7834 case dw_val_class_vec:
7835 switch (constant_size (a->dw_attr_val.v.val_vec.length
7836 * a->dw_attr_val.v.val_vec.elt_size))
7838 case 1:
7839 return DW_FORM_block1;
7840 case 2:
7841 return DW_FORM_block2;
7842 case 4:
7843 return DW_FORM_block4;
7844 default:
7845 gcc_unreachable ();
7847 case dw_val_class_flag:
7848 return DW_FORM_flag;
7849 case dw_val_class_die_ref:
7850 if (AT_ref_external (a))
7851 return DW_FORM_ref_addr;
7852 else
7853 return DW_FORM_ref;
7854 case dw_val_class_fde_ref:
7855 return DW_FORM_data;
7856 case dw_val_class_lbl_id:
7857 return DW_FORM_addr;
7858 case dw_val_class_lineptr:
7859 case dw_val_class_macptr:
7860 return DW_FORM_data;
7861 case dw_val_class_str:
7862 return AT_string_form (a);
7863 case dw_val_class_file:
7864 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
7866 case 1:
7867 return DW_FORM_data1;
7868 case 2:
7869 return DW_FORM_data2;
7870 case 4:
7871 return DW_FORM_data4;
7872 default:
7873 gcc_unreachable ();
7876 default:
7877 gcc_unreachable ();
7881 /* Output the encoding of an attribute value. */
7883 static void
7884 output_value_format (dw_attr_ref a)
7886 enum dwarf_form form = value_format (a);
7888 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
7891 /* Output the .debug_abbrev section which defines the DIE abbreviation
7892 table. */
7894 static void
7895 output_abbrev_section (void)
7897 unsigned long abbrev_id;
7899 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7901 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7902 unsigned ix;
7903 dw_attr_ref a_attr;
7905 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
7906 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
7907 dwarf_tag_name (abbrev->die_tag));
7909 if (abbrev->die_child != NULL)
7910 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
7911 else
7912 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
7914 for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
7915 ix++)
7917 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
7918 dwarf_attr_name (a_attr->dw_attr));
7919 output_value_format (a_attr);
7922 dw2_asm_output_data (1, 0, NULL);
7923 dw2_asm_output_data (1, 0, NULL);
7926 /* Terminate the table. */
7927 dw2_asm_output_data (1, 0, NULL);
7930 /* Output a symbol we can use to refer to this DIE from another CU. */
7932 static inline void
7933 output_die_symbol (dw_die_ref die)
7935 char *sym = die->die_symbol;
7937 if (sym == 0)
7938 return;
7940 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
7941 /* We make these global, not weak; if the target doesn't support
7942 .linkonce, it doesn't support combining the sections, so debugging
7943 will break. */
7944 targetm.asm_out.globalize_label (asm_out_file, sym);
7946 ASM_OUTPUT_LABEL (asm_out_file, sym);
7949 /* Return a new location list, given the begin and end range, and the
7950 expression. gensym tells us whether to generate a new internal symbol for
7951 this location list node, which is done for the head of the list only. */
7953 static inline dw_loc_list_ref
7954 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
7955 const char *section, unsigned int gensym)
7957 dw_loc_list_ref retlist = GGC_CNEW (dw_loc_list_node);
7959 retlist->begin = begin;
7960 retlist->end = end;
7961 retlist->expr = expr;
7962 retlist->section = section;
7963 if (gensym)
7964 retlist->ll_symbol = gen_internal_sym ("LLST");
7966 return retlist;
7969 /* Add a location description expression to a location list. */
7971 static inline void
7972 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
7973 const char *begin, const char *end,
7974 const char *section)
7976 dw_loc_list_ref *d;
7978 /* Find the end of the chain. */
7979 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
7982 /* Add a new location list node to the list. */
7983 *d = new_loc_list (descr, begin, end, section, 0);
7986 /* Output the location list given to us. */
7988 static void
7989 output_loc_list (dw_loc_list_ref list_head)
7991 dw_loc_list_ref curr = list_head;
7993 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
7995 /* Walk the location list, and output each range + expression. */
7996 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
7998 unsigned long size;
7999 /* Don't output an entry that starts and ends at the same address. */
8000 if (strcmp (curr->begin, curr->end) == 0)
8001 continue;
8002 if (!have_multiple_function_sections)
8004 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8005 "Location list begin address (%s)",
8006 list_head->ll_symbol);
8007 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8008 "Location list end address (%s)",
8009 list_head->ll_symbol);
8011 else
8013 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8014 "Location list begin address (%s)",
8015 list_head->ll_symbol);
8016 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8017 "Location list end address (%s)",
8018 list_head->ll_symbol);
8020 size = size_of_locs (curr->expr);
8022 /* Output the block length for this list of location operations. */
8023 gcc_assert (size <= 0xffff);
8024 dw2_asm_output_data (2, size, "%s", "Location expression size");
8026 output_loc_sequence (curr->expr);
8029 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8030 "Location list terminator begin (%s)",
8031 list_head->ll_symbol);
8032 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8033 "Location list terminator end (%s)",
8034 list_head->ll_symbol);
8037 /* Output the DIE and its attributes. Called recursively to generate
8038 the definitions of each child DIE. */
8040 static void
8041 output_die (dw_die_ref die)
8043 dw_attr_ref a;
8044 dw_die_ref c;
8045 unsigned long size;
8046 unsigned ix;
8048 /* If someone in another CU might refer to us, set up a symbol for
8049 them to point to. */
8050 if (die->die_symbol)
8051 output_die_symbol (die);
8053 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
8054 (unsigned long)die->die_offset,
8055 dwarf_tag_name (die->die_tag));
8057 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8059 const char *name = dwarf_attr_name (a->dw_attr);
8061 switch (AT_class (a))
8063 case dw_val_class_addr:
8064 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8065 break;
8067 case dw_val_class_offset:
8068 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8069 "%s", name);
8070 break;
8072 case dw_val_class_range_list:
8074 char *p = strchr (ranges_section_label, '\0');
8076 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
8077 a->dw_attr_val.v.val_offset);
8078 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8079 debug_ranges_section, "%s", name);
8080 *p = '\0';
8082 break;
8084 case dw_val_class_loc:
8085 size = size_of_locs (AT_loc (a));
8087 /* Output the block length for this list of location operations. */
8088 dw2_asm_output_data (constant_size (size), size, "%s", name);
8090 output_loc_sequence (AT_loc (a));
8091 break;
8093 case dw_val_class_const:
8094 /* ??? It would be slightly more efficient to use a scheme like is
8095 used for unsigned constants below, but gdb 4.x does not sign
8096 extend. Gdb 5.x does sign extend. */
8097 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8098 break;
8100 case dw_val_class_unsigned_const:
8101 dw2_asm_output_data (constant_size (AT_unsigned (a)),
8102 AT_unsigned (a), "%s", name);
8103 break;
8105 case dw_val_class_long_long:
8107 unsigned HOST_WIDE_INT first, second;
8109 dw2_asm_output_data (1,
8110 2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8111 "%s", name);
8113 if (WORDS_BIG_ENDIAN)
8115 first = a->dw_attr_val.v.val_long_long.hi;
8116 second = a->dw_attr_val.v.val_long_long.low;
8118 else
8120 first = a->dw_attr_val.v.val_long_long.low;
8121 second = a->dw_attr_val.v.val_long_long.hi;
8124 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8125 first, "long long constant");
8126 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8127 second, NULL);
8129 break;
8131 case dw_val_class_vec:
8133 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8134 unsigned int len = a->dw_attr_val.v.val_vec.length;
8135 unsigned int i;
8136 unsigned char *p;
8138 dw2_asm_output_data (constant_size (len * elt_size),
8139 len * elt_size, "%s", name);
8140 if (elt_size > sizeof (HOST_WIDE_INT))
8142 elt_size /= 2;
8143 len *= 2;
8145 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8146 i < len;
8147 i++, p += elt_size)
8148 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8149 "fp or vector constant word %u", i);
8150 break;
8153 case dw_val_class_flag:
8154 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8155 break;
8157 case dw_val_class_loc_list:
8159 char *sym = AT_loc_list (a)->ll_symbol;
8161 gcc_assert (sym);
8162 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8163 "%s", name);
8165 break;
8167 case dw_val_class_die_ref:
8168 if (AT_ref_external (a))
8170 char *sym = AT_ref (a)->die_symbol;
8172 gcc_assert (sym);
8173 dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, debug_info_section,
8174 "%s", name);
8176 else
8178 gcc_assert (AT_ref (a)->die_offset);
8179 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8180 "%s", name);
8182 break;
8184 case dw_val_class_fde_ref:
8186 char l1[20];
8188 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8189 a->dw_attr_val.v.val_fde_index * 2);
8190 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8191 "%s", name);
8193 break;
8195 case dw_val_class_lbl_id:
8196 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8197 break;
8199 case dw_val_class_lineptr:
8200 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8201 debug_line_section, "%s", name);
8202 break;
8204 case dw_val_class_macptr:
8205 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8206 debug_macinfo_section, "%s", name);
8207 break;
8209 case dw_val_class_str:
8210 if (AT_string_form (a) == DW_FORM_strp)
8211 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8212 a->dw_attr_val.v.val_str->label,
8213 debug_str_section,
8214 "%s: \"%s\"", name, AT_string (a));
8215 else
8216 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8217 break;
8219 case dw_val_class_file:
8221 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8223 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8224 a->dw_attr_val.v.val_file->filename);
8225 break;
8228 default:
8229 gcc_unreachable ();
8233 FOR_EACH_CHILD (die, c, output_die (c));
8235 /* Add null byte to terminate sibling list. */
8236 if (die->die_child != NULL)
8237 dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
8238 (unsigned long) die->die_offset);
8241 /* Output the compilation unit that appears at the beginning of the
8242 .debug_info section, and precedes the DIE descriptions. */
8244 static void
8245 output_compilation_unit_header (void)
8247 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8248 dw2_asm_output_data (4, 0xffffffff,
8249 "Initial length escape value indicating 64-bit DWARF extension");
8250 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8251 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8252 "Length of Compilation Unit Info");
8253 dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
8254 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8255 debug_abbrev_section,
8256 "Offset Into Abbrev. Section");
8257 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8260 /* Output the compilation unit DIE and its children. */
8262 static void
8263 output_comp_unit (dw_die_ref die, int output_if_empty)
8265 const char *secname;
8266 char *oldsym, *tmp;
8268 /* Unless we are outputting main CU, we may throw away empty ones. */
8269 if (!output_if_empty && die->die_child == NULL)
8270 return;
8272 /* Even if there are no children of this DIE, we must output the information
8273 about the compilation unit. Otherwise, on an empty translation unit, we
8274 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
8275 will then complain when examining the file. First mark all the DIEs in
8276 this CU so we know which get local refs. */
8277 mark_dies (die);
8279 build_abbrev_table (die);
8281 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8282 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8283 calc_die_sizes (die);
8285 oldsym = die->die_symbol;
8286 if (oldsym)
8288 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8290 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8291 secname = tmp;
8292 die->die_symbol = NULL;
8293 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8295 else
8296 switch_to_section (debug_info_section);
8298 /* Output debugging information. */
8299 output_compilation_unit_header ();
8300 output_die (die);
8302 /* Leave the marks on the main CU, so we can check them in
8303 output_pubnames. */
8304 if (oldsym)
8306 unmark_dies (die);
8307 die->die_symbol = oldsym;
8311 /* Return the DWARF2/3 pubname associated with a decl. */
8313 static const char *
8314 dwarf2_name (tree decl, int scope)
8316 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
8319 /* Add a new entry to .debug_pubnames if appropriate. */
8321 static void
8322 add_pubname_string (const char *str, dw_die_ref die)
8324 pubname_entry e;
8326 e.die = die;
8327 e.name = xstrdup (str);
8328 VEC_safe_push (pubname_entry, gc, pubname_table, &e);
8331 static void
8332 add_pubname (tree decl, dw_die_ref die)
8335 if (TREE_PUBLIC (decl))
8336 add_pubname_string (dwarf2_name (decl, 1), die);
8339 /* Add a new entry to .debug_pubtypes if appropriate. */
8341 static void
8342 add_pubtype (tree decl, dw_die_ref die)
8344 pubname_entry e;
8346 e.name = NULL;
8347 if ((TREE_PUBLIC (decl)
8348 || die->die_parent == comp_unit_die)
8349 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
8351 e.die = die;
8352 if (TYPE_P (decl))
8354 if (TYPE_NAME (decl))
8356 if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
8357 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
8358 else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
8359 && DECL_NAME (TYPE_NAME (decl)))
8360 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
8361 else
8362 e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
8365 else
8366 e.name = xstrdup (dwarf2_name (decl, 1));
8368 /* If we don't have a name for the type, there's no point in adding
8369 it to the table. */
8370 if (e.name && e.name[0] != '\0')
8371 VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
8375 /* Output the public names table used to speed up access to externally
8376 visible names; or the public types table used to find type definitions. */
8378 static void
8379 output_pubnames (VEC (pubname_entry, gc) * names)
8381 unsigned i;
8382 unsigned long pubnames_length = size_of_pubnames (names);
8383 pubname_ref pub;
8385 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8386 dw2_asm_output_data (4, 0xffffffff,
8387 "Initial length escape value indicating 64-bit DWARF extension");
8388 if (names == pubname_table)
8389 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8390 "Length of Public Names Info");
8391 else
8392 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8393 "Length of Public Type Names Info");
8394 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8395 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8396 debug_info_section,
8397 "Offset of Compilation Unit Info");
8398 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
8399 "Compilation Unit Length");
8401 for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
8403 /* We shouldn't see pubnames for DIEs outside of the main CU. */
8404 if (names == pubname_table)
8405 gcc_assert (pub->die->die_mark);
8407 if (names != pubtype_table
8408 || pub->die->die_offset != 0
8409 || !flag_eliminate_unused_debug_types)
8411 dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
8412 "DIE offset");
8414 dw2_asm_output_nstring (pub->name, -1, "external name");
8418 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
8421 /* Add a new entry to .debug_aranges if appropriate. */
8423 static void
8424 add_arange (tree decl, dw_die_ref die)
8426 if (! DECL_SECTION_NAME (decl))
8427 return;
8429 if (arange_table_in_use == arange_table_allocated)
8431 arange_table_allocated += ARANGE_TABLE_INCREMENT;
8432 arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
8433 arange_table_allocated);
8434 memset (arange_table + arange_table_in_use, 0,
8435 ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
8438 arange_table[arange_table_in_use++] = die;
8441 /* Output the information that goes into the .debug_aranges table.
8442 Namely, define the beginning and ending address range of the
8443 text section generated for this compilation unit. */
8445 static void
8446 output_aranges (void)
8448 unsigned i;
8449 unsigned long aranges_length = size_of_aranges ();
8451 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8452 dw2_asm_output_data (4, 0xffffffff,
8453 "Initial length escape value indicating 64-bit DWARF extension");
8454 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
8455 "Length of Address Ranges Info");
8456 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8457 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8458 debug_info_section,
8459 "Offset of Compilation Unit Info");
8460 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
8461 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
8463 /* We need to align to twice the pointer size here. */
8464 if (DWARF_ARANGES_PAD_SIZE)
8466 /* Pad using a 2 byte words so that padding is correct for any
8467 pointer size. */
8468 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
8469 2 * DWARF2_ADDR_SIZE);
8470 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
8471 dw2_asm_output_data (2, 0, NULL);
8474 /* It is necessary not to output these entries if the sections were
8475 not used; if the sections were not used, the length will be 0 and
8476 the address may end up as 0 if the section is discarded by ld
8477 --gc-sections, leaving an invalid (0, 0) entry that can be
8478 confused with the terminator. */
8479 if (text_section_used)
8481 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
8482 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
8483 text_section_label, "Length");
8485 if (cold_text_section_used)
8487 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
8488 "Address");
8489 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
8490 cold_text_section_label, "Length");
8493 for (i = 0; i < arange_table_in_use; i++)
8495 dw_die_ref die = arange_table[i];
8497 /* We shouldn't see aranges for DIEs outside of the main CU. */
8498 gcc_assert (die->die_mark);
8500 if (die->die_tag == DW_TAG_subprogram)
8502 dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
8503 "Address");
8504 dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
8505 get_AT_low_pc (die), "Length");
8507 else
8509 /* A static variable; extract the symbol from DW_AT_location.
8510 Note that this code isn't currently hit, as we only emit
8511 aranges for functions (jason 9/23/99). */
8512 dw_attr_ref a = get_AT (die, DW_AT_location);
8513 dw_loc_descr_ref loc;
8515 gcc_assert (a && AT_class (a) == dw_val_class_loc);
8517 loc = AT_loc (a);
8518 gcc_assert (loc->dw_loc_opc == DW_OP_addr);
8520 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
8521 loc->dw_loc_oprnd1.v.val_addr, "Address");
8522 dw2_asm_output_data (DWARF2_ADDR_SIZE,
8523 get_AT_unsigned (die, DW_AT_byte_size),
8524 "Length");
8528 /* Output the terminator words. */
8529 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8530 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8533 /* Add a new entry to .debug_ranges. Return the offset at which it
8534 was placed. */
8536 static unsigned int
8537 add_ranges_num (int num)
8539 unsigned int in_use = ranges_table_in_use;
8541 if (in_use == ranges_table_allocated)
8543 ranges_table_allocated += RANGES_TABLE_INCREMENT;
8544 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
8545 ranges_table_allocated);
8546 memset (ranges_table + ranges_table_in_use, 0,
8547 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
8550 ranges_table[in_use].num = num;
8551 ranges_table_in_use = in_use + 1;
8553 return in_use * 2 * DWARF2_ADDR_SIZE;
8556 /* Add a new entry to .debug_ranges corresponding to a block, or a
8557 range terminator if BLOCK is NULL. */
8559 static unsigned int
8560 add_ranges (const_tree block)
8562 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
8565 /* Add a new entry to .debug_ranges corresponding to a pair of
8566 labels. */
8568 static unsigned int
8569 add_ranges_by_labels (const char *begin, const char *end)
8571 unsigned int in_use = ranges_by_label_in_use;
8573 if (in_use == ranges_by_label_allocated)
8575 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
8576 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
8577 ranges_by_label,
8578 ranges_by_label_allocated);
8579 memset (ranges_by_label + ranges_by_label_in_use, 0,
8580 RANGES_TABLE_INCREMENT
8581 * sizeof (struct dw_ranges_by_label_struct));
8584 ranges_by_label[in_use].begin = begin;
8585 ranges_by_label[in_use].end = end;
8586 ranges_by_label_in_use = in_use + 1;
8588 return add_ranges_num (-(int)in_use - 1);
8591 static void
8592 output_ranges (void)
8594 unsigned i;
8595 static const char *const start_fmt = "Offset 0x%x";
8596 const char *fmt = start_fmt;
8598 for (i = 0; i < ranges_table_in_use; i++)
8600 int block_num = ranges_table[i].num;
8602 if (block_num > 0)
8604 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
8605 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
8607 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
8608 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
8610 /* If all code is in the text section, then the compilation
8611 unit base address defaults to DW_AT_low_pc, which is the
8612 base of the text section. */
8613 if (!have_multiple_function_sections)
8615 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
8616 text_section_label,
8617 fmt, i * 2 * DWARF2_ADDR_SIZE);
8618 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
8619 text_section_label, NULL);
8622 /* Otherwise, the compilation unit base address is zero,
8623 which allows us to use absolute addresses, and not worry
8624 about whether the target supports cross-section
8625 arithmetic. */
8626 else
8628 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
8629 fmt, i * 2 * DWARF2_ADDR_SIZE);
8630 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
8633 fmt = NULL;
8636 /* Negative block_num stands for an index into ranges_by_label. */
8637 else if (block_num < 0)
8639 int lab_idx = - block_num - 1;
8641 if (!have_multiple_function_sections)
8643 gcc_unreachable ();
8644 #if 0
8645 /* If we ever use add_ranges_by_labels () for a single
8646 function section, all we have to do is to take out
8647 the #if 0 above. */
8648 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8649 ranges_by_label[lab_idx].begin,
8650 text_section_label,
8651 fmt, i * 2 * DWARF2_ADDR_SIZE);
8652 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8653 ranges_by_label[lab_idx].end,
8654 text_section_label, NULL);
8655 #endif
8657 else
8659 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8660 ranges_by_label[lab_idx].begin,
8661 fmt, i * 2 * DWARF2_ADDR_SIZE);
8662 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8663 ranges_by_label[lab_idx].end,
8664 NULL);
8667 else
8669 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8670 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8671 fmt = start_fmt;
8676 /* Data structure containing information about input files. */
8677 struct file_info
8679 const char *path; /* Complete file name. */
8680 const char *fname; /* File name part. */
8681 int length; /* Length of entire string. */
8682 struct dwarf_file_data * file_idx; /* Index in input file table. */
8683 int dir_idx; /* Index in directory table. */
8686 /* Data structure containing information about directories with source
8687 files. */
8688 struct dir_info
8690 const char *path; /* Path including directory name. */
8691 int length; /* Path length. */
8692 int prefix; /* Index of directory entry which is a prefix. */
8693 int count; /* Number of files in this directory. */
8694 int dir_idx; /* Index of directory used as base. */
8697 /* Callback function for file_info comparison. We sort by looking at
8698 the directories in the path. */
8700 static int
8701 file_info_cmp (const void *p1, const void *p2)
8703 const struct file_info *const s1 = (const struct file_info *) p1;
8704 const struct file_info *const s2 = (const struct file_info *) p2;
8705 const unsigned char *cp1;
8706 const unsigned char *cp2;
8708 /* Take care of file names without directories. We need to make sure that
8709 we return consistent values to qsort since some will get confused if
8710 we return the same value when identical operands are passed in opposite
8711 orders. So if neither has a directory, return 0 and otherwise return
8712 1 or -1 depending on which one has the directory. */
8713 if ((s1->path == s1->fname || s2->path == s2->fname))
8714 return (s2->path == s2->fname) - (s1->path == s1->fname);
8716 cp1 = (const unsigned char *) s1->path;
8717 cp2 = (const unsigned char *) s2->path;
8719 while (1)
8721 ++cp1;
8722 ++cp2;
8723 /* Reached the end of the first path? If so, handle like above. */
8724 if ((cp1 == (const unsigned char *) s1->fname)
8725 || (cp2 == (const unsigned char *) s2->fname))
8726 return ((cp2 == (const unsigned char *) s2->fname)
8727 - (cp1 == (const unsigned char *) s1->fname));
8729 /* Character of current path component the same? */
8730 else if (*cp1 != *cp2)
8731 return *cp1 - *cp2;
8735 struct file_name_acquire_data
8737 struct file_info *files;
8738 int used_files;
8739 int max_files;
8742 /* Traversal function for the hash table. */
8744 static int
8745 file_name_acquire (void ** slot, void *data)
8747 struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
8748 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
8749 struct file_info *fi;
8750 const char *f;
8752 gcc_assert (fnad->max_files >= d->emitted_number);
8754 if (! d->emitted_number)
8755 return 1;
8757 gcc_assert (fnad->max_files != fnad->used_files);
8759 fi = fnad->files + fnad->used_files++;
8761 /* Skip all leading "./". */
8762 f = d->filename;
8763 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
8764 f += 2;
8766 /* Create a new array entry. */
8767 fi->path = f;
8768 fi->length = strlen (f);
8769 fi->file_idx = d;
8771 /* Search for the file name part. */
8772 f = strrchr (f, DIR_SEPARATOR);
8773 #if defined (DIR_SEPARATOR_2)
8775 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
8777 if (g != NULL)
8779 if (f == NULL || f < g)
8780 f = g;
8783 #endif
8785 fi->fname = f == NULL ? fi->path : f + 1;
8786 return 1;
8789 /* Output the directory table and the file name table. We try to minimize
8790 the total amount of memory needed. A heuristic is used to avoid large
8791 slowdowns with many input files. */
8793 static void
8794 output_file_names (void)
8796 struct file_name_acquire_data fnad;
8797 int numfiles;
8798 struct file_info *files;
8799 struct dir_info *dirs;
8800 int *saved;
8801 int *savehere;
8802 int *backmap;
8803 int ndirs;
8804 int idx_offset;
8805 int i;
8806 int idx;
8808 if (!last_emitted_file)
8810 dw2_asm_output_data (1, 0, "End directory table");
8811 dw2_asm_output_data (1, 0, "End file name table");
8812 return;
8815 numfiles = last_emitted_file->emitted_number;
8817 /* Allocate the various arrays we need. */
8818 files = XALLOCAVEC (struct file_info, numfiles);
8819 dirs = XALLOCAVEC (struct dir_info, numfiles);
8821 fnad.files = files;
8822 fnad.used_files = 0;
8823 fnad.max_files = numfiles;
8824 htab_traverse (file_table, file_name_acquire, &fnad);
8825 gcc_assert (fnad.used_files == fnad.max_files);
8827 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
8829 /* Find all the different directories used. */
8830 dirs[0].path = files[0].path;
8831 dirs[0].length = files[0].fname - files[0].path;
8832 dirs[0].prefix = -1;
8833 dirs[0].count = 1;
8834 dirs[0].dir_idx = 0;
8835 files[0].dir_idx = 0;
8836 ndirs = 1;
8838 for (i = 1; i < numfiles; i++)
8839 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
8840 && memcmp (dirs[ndirs - 1].path, files[i].path,
8841 dirs[ndirs - 1].length) == 0)
8843 /* Same directory as last entry. */
8844 files[i].dir_idx = ndirs - 1;
8845 ++dirs[ndirs - 1].count;
8847 else
8849 int j;
8851 /* This is a new directory. */
8852 dirs[ndirs].path = files[i].path;
8853 dirs[ndirs].length = files[i].fname - files[i].path;
8854 dirs[ndirs].count = 1;
8855 dirs[ndirs].dir_idx = ndirs;
8856 files[i].dir_idx = ndirs;
8858 /* Search for a prefix. */
8859 dirs[ndirs].prefix = -1;
8860 for (j = 0; j < ndirs; j++)
8861 if (dirs[j].length < dirs[ndirs].length
8862 && dirs[j].length > 1
8863 && (dirs[ndirs].prefix == -1
8864 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
8865 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
8866 dirs[ndirs].prefix = j;
8868 ++ndirs;
8871 /* Now to the actual work. We have to find a subset of the directories which
8872 allow expressing the file name using references to the directory table
8873 with the least amount of characters. We do not do an exhaustive search
8874 where we would have to check out every combination of every single
8875 possible prefix. Instead we use a heuristic which provides nearly optimal
8876 results in most cases and never is much off. */
8877 saved = XALLOCAVEC (int, ndirs);
8878 savehere = XALLOCAVEC (int, ndirs);
8880 memset (saved, '\0', ndirs * sizeof (saved[0]));
8881 for (i = 0; i < ndirs; i++)
8883 int j;
8884 int total;
8886 /* We can always save some space for the current directory. But this
8887 does not mean it will be enough to justify adding the directory. */
8888 savehere[i] = dirs[i].length;
8889 total = (savehere[i] - saved[i]) * dirs[i].count;
8891 for (j = i + 1; j < ndirs; j++)
8893 savehere[j] = 0;
8894 if (saved[j] < dirs[i].length)
8896 /* Determine whether the dirs[i] path is a prefix of the
8897 dirs[j] path. */
8898 int k;
8900 k = dirs[j].prefix;
8901 while (k != -1 && k != (int) i)
8902 k = dirs[k].prefix;
8904 if (k == (int) i)
8906 /* Yes it is. We can possibly save some memory by
8907 writing the filenames in dirs[j] relative to
8908 dirs[i]. */
8909 savehere[j] = dirs[i].length;
8910 total += (savehere[j] - saved[j]) * dirs[j].count;
8915 /* Check whether we can save enough to justify adding the dirs[i]
8916 directory. */
8917 if (total > dirs[i].length + 1)
8919 /* It's worthwhile adding. */
8920 for (j = i; j < ndirs; j++)
8921 if (savehere[j] > 0)
8923 /* Remember how much we saved for this directory so far. */
8924 saved[j] = savehere[j];
8926 /* Remember the prefix directory. */
8927 dirs[j].dir_idx = i;
8932 /* Emit the directory name table. */
8933 idx = 1;
8934 idx_offset = dirs[0].length > 0 ? 1 : 0;
8935 for (i = 1 - idx_offset; i < ndirs; i++)
8936 dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
8937 "Directory Entry: 0x%x", i + idx_offset);
8939 dw2_asm_output_data (1, 0, "End directory table");
8941 /* We have to emit them in the order of emitted_number since that's
8942 used in the debug info generation. To do this efficiently we
8943 generate a back-mapping of the indices first. */
8944 backmap = XALLOCAVEC (int, numfiles);
8945 for (i = 0; i < numfiles; i++)
8946 backmap[files[i].file_idx->emitted_number - 1] = i;
8948 /* Now write all the file names. */
8949 for (i = 0; i < numfiles; i++)
8951 int file_idx = backmap[i];
8952 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
8954 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
8955 "File Entry: 0x%x", (unsigned) i + 1);
8957 /* Include directory index. */
8958 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
8960 /* Modification time. */
8961 dw2_asm_output_data_uleb128 (0, NULL);
8963 /* File length in bytes. */
8964 dw2_asm_output_data_uleb128 (0, NULL);
8967 dw2_asm_output_data (1, 0, "End file name table");
8971 /* Output the source line number correspondence information. This
8972 information goes into the .debug_line section. */
8974 static void
8975 output_line_info (void)
8977 char l1[20], l2[20], p1[20], p2[20];
8978 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8979 char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8980 unsigned opc;
8981 unsigned n_op_args;
8982 unsigned long lt_index;
8983 unsigned long current_line;
8984 long line_offset;
8985 long line_delta;
8986 unsigned long current_file;
8987 unsigned long function;
8989 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
8990 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
8991 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
8992 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
8994 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8995 dw2_asm_output_data (4, 0xffffffff,
8996 "Initial length escape value indicating 64-bit DWARF extension");
8997 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
8998 "Length of Source Line Info");
8999 ASM_OUTPUT_LABEL (asm_out_file, l1);
9001 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
9002 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
9003 ASM_OUTPUT_LABEL (asm_out_file, p1);
9005 /* Define the architecture-dependent minimum instruction length (in
9006 bytes). In this implementation of DWARF, this field is used for
9007 information purposes only. Since GCC generates assembly language,
9008 we have no a priori knowledge of how many instruction bytes are
9009 generated for each source line, and therefore can use only the
9010 DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
9011 commands. Accordingly, we fix this as `1', which is "correct
9012 enough" for all architectures, and don't let the target override. */
9013 dw2_asm_output_data (1, 1,
9014 "Minimum Instruction Length");
9016 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
9017 "Default is_stmt_start flag");
9018 dw2_asm_output_data (1, DWARF_LINE_BASE,
9019 "Line Base Value (Special Opcodes)");
9020 dw2_asm_output_data (1, DWARF_LINE_RANGE,
9021 "Line Range Value (Special Opcodes)");
9022 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
9023 "Special Opcode Base");
9025 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
9027 switch (opc)
9029 case DW_LNS_advance_pc:
9030 case DW_LNS_advance_line:
9031 case DW_LNS_set_file:
9032 case DW_LNS_set_column:
9033 case DW_LNS_fixed_advance_pc:
9034 n_op_args = 1;
9035 break;
9036 default:
9037 n_op_args = 0;
9038 break;
9041 dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
9042 opc, n_op_args);
9045 /* Write out the information about the files we use. */
9046 output_file_names ();
9047 ASM_OUTPUT_LABEL (asm_out_file, p2);
9049 /* We used to set the address register to the first location in the text
9050 section here, but that didn't accomplish anything since we already
9051 have a line note for the opening brace of the first function. */
9053 /* Generate the line number to PC correspondence table, encoded as
9054 a series of state machine operations. */
9055 current_file = 1;
9056 current_line = 1;
9058 if (cfun && in_cold_section_p)
9059 strcpy (prev_line_label, crtl->subsections.cold_section_label);
9060 else
9061 strcpy (prev_line_label, text_section_label);
9062 for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
9064 dw_line_info_ref line_info = &line_info_table[lt_index];
9066 #if 0
9067 /* Disable this optimization for now; GDB wants to see two line notes
9068 at the beginning of a function so it can find the end of the
9069 prologue. */
9071 /* Don't emit anything for redundant notes. Just updating the
9072 address doesn't accomplish anything, because we already assume
9073 that anything after the last address is this line. */
9074 if (line_info->dw_line_num == current_line
9075 && line_info->dw_file_num == current_file)
9076 continue;
9077 #endif
9079 /* Emit debug info for the address of the current line.
9081 Unfortunately, we have little choice here currently, and must always
9082 use the most general form. GCC does not know the address delta
9083 itself, so we can't use DW_LNS_advance_pc. Many ports do have length
9084 attributes which will give an upper bound on the address range. We
9085 could perhaps use length attributes to determine when it is safe to
9086 use DW_LNS_fixed_advance_pc. */
9088 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
9089 if (0)
9091 /* This can handle deltas up to 0xffff. This takes 3 bytes. */
9092 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9093 "DW_LNS_fixed_advance_pc");
9094 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9096 else
9098 /* This can handle any delta. This takes
9099 4+DWARF2_ADDR_SIZE bytes. */
9100 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9101 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9102 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9103 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9106 strcpy (prev_line_label, line_label);
9108 /* Emit debug info for the source file of the current line, if
9109 different from the previous line. */
9110 if (line_info->dw_file_num != current_file)
9112 current_file = line_info->dw_file_num;
9113 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
9114 dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
9117 /* Emit debug info for the current line number, choosing the encoding
9118 that uses the least amount of space. */
9119 if (line_info->dw_line_num != current_line)
9121 line_offset = line_info->dw_line_num - current_line;
9122 line_delta = line_offset - DWARF_LINE_BASE;
9123 current_line = line_info->dw_line_num;
9124 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9125 /* This can handle deltas from -10 to 234, using the current
9126 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE. This
9127 takes 1 byte. */
9128 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9129 "line %lu", current_line);
9130 else
9132 /* This can handle any delta. This takes at least 4 bytes,
9133 depending on the value being encoded. */
9134 dw2_asm_output_data (1, DW_LNS_advance_line,
9135 "advance to line %lu", current_line);
9136 dw2_asm_output_data_sleb128 (line_offset, NULL);
9137 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9140 else
9141 /* We still need to start a new row, so output a copy insn. */
9142 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9145 /* Emit debug info for the address of the end of the function. */
9146 if (0)
9148 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9149 "DW_LNS_fixed_advance_pc");
9150 dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
9152 else
9154 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9155 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9156 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9157 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
9160 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
9161 dw2_asm_output_data_uleb128 (1, NULL);
9162 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9164 function = 0;
9165 current_file = 1;
9166 current_line = 1;
9167 for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
9169 dw_separate_line_info_ref line_info
9170 = &separate_line_info_table[lt_index];
9172 #if 0
9173 /* Don't emit anything for redundant notes. */
9174 if (line_info->dw_line_num == current_line
9175 && line_info->dw_file_num == current_file
9176 && line_info->function == function)
9177 goto cont;
9178 #endif
9180 /* Emit debug info for the address of the current line. If this is
9181 a new function, or the first line of a function, then we need
9182 to handle it differently. */
9183 ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
9184 lt_index);
9185 if (function != line_info->function)
9187 function = line_info->function;
9189 /* Set the address register to the first line in the function. */
9190 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9191 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9192 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9193 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9195 else
9197 /* ??? See the DW_LNS_advance_pc comment above. */
9198 if (0)
9200 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9201 "DW_LNS_fixed_advance_pc");
9202 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9204 else
9206 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9207 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9208 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9209 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9213 strcpy (prev_line_label, line_label);
9215 /* Emit debug info for the source file of the current line, if
9216 different from the previous line. */
9217 if (line_info->dw_file_num != current_file)
9219 current_file = line_info->dw_file_num;
9220 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
9221 dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
9224 /* Emit debug info for the current line number, choosing the encoding
9225 that uses the least amount of space. */
9226 if (line_info->dw_line_num != current_line)
9228 line_offset = line_info->dw_line_num - current_line;
9229 line_delta = line_offset - DWARF_LINE_BASE;
9230 current_line = line_info->dw_line_num;
9231 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9232 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9233 "line %lu", current_line);
9234 else
9236 dw2_asm_output_data (1, DW_LNS_advance_line,
9237 "advance to line %lu", current_line);
9238 dw2_asm_output_data_sleb128 (line_offset, NULL);
9239 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9242 else
9243 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9245 #if 0
9246 cont:
9247 #endif
9249 lt_index++;
9251 /* If we're done with a function, end its sequence. */
9252 if (lt_index == separate_line_info_table_in_use
9253 || separate_line_info_table[lt_index].function != function)
9255 current_file = 1;
9256 current_line = 1;
9258 /* Emit debug info for the address of the end of the function. */
9259 ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
9260 if (0)
9262 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9263 "DW_LNS_fixed_advance_pc");
9264 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9266 else
9268 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9269 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9270 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9271 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9274 /* Output the marker for the end of this sequence. */
9275 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
9276 dw2_asm_output_data_uleb128 (1, NULL);
9277 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9281 /* Output the marker for the end of the line number info. */
9282 ASM_OUTPUT_LABEL (asm_out_file, l2);
9285 /* Given a pointer to a tree node for some base type, return a pointer to
9286 a DIE that describes the given type.
9288 This routine must only be called for GCC type nodes that correspond to
9289 Dwarf base (fundamental) types. */
9291 static dw_die_ref
9292 base_type_die (tree type)
9294 dw_die_ref base_type_result;
9295 enum dwarf_type encoding;
9297 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
9298 return 0;
9300 switch (TREE_CODE (type))
9302 case INTEGER_TYPE:
9303 if (TYPE_STRING_FLAG (type))
9305 if (TYPE_UNSIGNED (type))
9306 encoding = DW_ATE_unsigned_char;
9307 else
9308 encoding = DW_ATE_signed_char;
9310 else if (TYPE_UNSIGNED (type))
9311 encoding = DW_ATE_unsigned;
9312 else
9313 encoding = DW_ATE_signed;
9314 break;
9316 case REAL_TYPE:
9317 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
9318 encoding = DW_ATE_decimal_float;
9319 else
9320 encoding = DW_ATE_float;
9321 break;
9323 case FIXED_POINT_TYPE:
9324 if (TYPE_UNSIGNED (type))
9325 encoding = DW_ATE_unsigned_fixed;
9326 else
9327 encoding = DW_ATE_signed_fixed;
9328 break;
9330 /* Dwarf2 doesn't know anything about complex ints, so use
9331 a user defined type for it. */
9332 case COMPLEX_TYPE:
9333 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
9334 encoding = DW_ATE_complex_float;
9335 else
9336 encoding = DW_ATE_lo_user;
9337 break;
9339 case BOOLEAN_TYPE:
9340 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
9341 encoding = DW_ATE_boolean;
9342 break;
9344 default:
9345 /* No other TREE_CODEs are Dwarf fundamental types. */
9346 gcc_unreachable ();
9349 base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
9351 /* This probably indicates a bug. */
9352 if (! TYPE_NAME (type))
9353 add_name_attribute (base_type_result, "__unknown__");
9355 add_AT_unsigned (base_type_result, DW_AT_byte_size,
9356 int_size_in_bytes (type));
9357 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
9359 return base_type_result;
9362 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
9363 given input type is a Dwarf "fundamental" type. Otherwise return null. */
9365 static inline int
9366 is_base_type (tree type)
9368 switch (TREE_CODE (type))
9370 case ERROR_MARK:
9371 case VOID_TYPE:
9372 case INTEGER_TYPE:
9373 case REAL_TYPE:
9374 case FIXED_POINT_TYPE:
9375 case COMPLEX_TYPE:
9376 case BOOLEAN_TYPE:
9377 return 1;
9379 case ARRAY_TYPE:
9380 case RECORD_TYPE:
9381 case UNION_TYPE:
9382 case QUAL_UNION_TYPE:
9383 case ENUMERAL_TYPE:
9384 case FUNCTION_TYPE:
9385 case METHOD_TYPE:
9386 case POINTER_TYPE:
9387 case REFERENCE_TYPE:
9388 case OFFSET_TYPE:
9389 case LANG_TYPE:
9390 case VECTOR_TYPE:
9391 return 0;
9393 default:
9394 gcc_unreachable ();
9397 return 0;
9400 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
9401 node, return the size in bits for the type if it is a constant, or else
9402 return the alignment for the type if the type's size is not constant, or
9403 else return BITS_PER_WORD if the type actually turns out to be an
9404 ERROR_MARK node. */
9406 static inline unsigned HOST_WIDE_INT
9407 simple_type_size_in_bits (const_tree type)
9409 if (TREE_CODE (type) == ERROR_MARK)
9410 return BITS_PER_WORD;
9411 else if (TYPE_SIZE (type) == NULL_TREE)
9412 return 0;
9413 else if (host_integerp (TYPE_SIZE (type), 1))
9414 return tree_low_cst (TYPE_SIZE (type), 1);
9415 else
9416 return TYPE_ALIGN (type);
9419 /* Return true if the debug information for the given type should be
9420 emitted as a subrange type. */
9422 static inline bool
9423 is_subrange_type (const_tree type)
9425 tree subtype = TREE_TYPE (type);
9427 /* Subrange types are identified by the fact that they are integer
9428 types, and that they have a subtype which is either an integer type
9429 or an enumeral type. */
9431 if (TREE_CODE (type) != INTEGER_TYPE
9432 || subtype == NULL_TREE)
9433 return false;
9435 if (TREE_CODE (subtype) != INTEGER_TYPE
9436 && TREE_CODE (subtype) != ENUMERAL_TYPE
9437 && TREE_CODE (subtype) != BOOLEAN_TYPE)
9438 return false;
9440 if (TREE_CODE (type) == TREE_CODE (subtype)
9441 && int_size_in_bytes (type) == int_size_in_bytes (subtype)
9442 && TYPE_MIN_VALUE (type) != NULL
9443 && TYPE_MIN_VALUE (subtype) != NULL
9444 && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
9445 && TYPE_MAX_VALUE (type) != NULL
9446 && TYPE_MAX_VALUE (subtype) != NULL
9447 && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
9449 /* The type and its subtype have the same representation. If in
9450 addition the two types also have the same name, then the given
9451 type is not a subrange type, but rather a plain base type. */
9452 /* FIXME: brobecker/2004-03-22:
9453 Sizetype INTEGER_CSTs nodes are canonicalized. It should
9454 therefore be sufficient to check the TYPE_SIZE node pointers
9455 rather than checking the actual size. Unfortunately, we have
9456 found some cases, such as in the Ada "integer" type, where
9457 this is not the case. Until this problem is solved, we need to
9458 keep checking the actual size. */
9459 tree type_name = TYPE_NAME (type);
9460 tree subtype_name = TYPE_NAME (subtype);
9462 if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
9463 type_name = DECL_NAME (type_name);
9465 if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
9466 subtype_name = DECL_NAME (subtype_name);
9468 if (type_name == subtype_name)
9469 return false;
9472 return true;
9475 /* Given a pointer to a tree node for a subrange type, return a pointer
9476 to a DIE that describes the given type. */
9478 static dw_die_ref
9479 subrange_type_die (tree type, dw_die_ref context_die)
9481 dw_die_ref subrange_die;
9482 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
9484 if (context_die == NULL)
9485 context_die = comp_unit_die;
9487 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
9489 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
9491 /* The size of the subrange type and its base type do not match,
9492 so we need to generate a size attribute for the subrange type. */
9493 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
9496 if (TYPE_MIN_VALUE (type) != NULL)
9497 add_bound_info (subrange_die, DW_AT_lower_bound,
9498 TYPE_MIN_VALUE (type));
9499 if (TYPE_MAX_VALUE (type) != NULL)
9500 add_bound_info (subrange_die, DW_AT_upper_bound,
9501 TYPE_MAX_VALUE (type));
9503 return subrange_die;
9506 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
9507 entry that chains various modifiers in front of the given type. */
9509 static dw_die_ref
9510 modified_type_die (tree type, int is_const_type, int is_volatile_type,
9511 dw_die_ref context_die)
9513 enum tree_code code = TREE_CODE (type);
9514 dw_die_ref mod_type_die;
9515 dw_die_ref sub_die = NULL;
9516 tree item_type = NULL;
9517 tree qualified_type;
9518 tree name;
9520 if (code == ERROR_MARK)
9521 return NULL;
9523 /* See if we already have the appropriately qualified variant of
9524 this type. */
9525 qualified_type
9526 = get_qualified_type (type,
9527 ((is_const_type ? TYPE_QUAL_CONST : 0)
9528 | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
9530 /* If we do, then we can just use its DIE, if it exists. */
9531 if (qualified_type)
9533 mod_type_die = lookup_type_die (qualified_type);
9534 if (mod_type_die)
9535 return mod_type_die;
9538 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
9540 /* Handle C typedef types. */
9541 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
9543 tree dtype = TREE_TYPE (name);
9545 if (qualified_type == dtype)
9547 /* For a named type, use the typedef. */
9548 gen_type_die (qualified_type, context_die);
9549 return lookup_type_die (qualified_type);
9551 else if (is_const_type < TYPE_READONLY (dtype)
9552 || is_volatile_type < TYPE_VOLATILE (dtype)
9553 || (is_const_type <= TYPE_READONLY (dtype)
9554 && is_volatile_type <= TYPE_VOLATILE (dtype)
9555 && DECL_ORIGINAL_TYPE (name) != type))
9556 /* cv-unqualified version of named type. Just use the unnamed
9557 type to which it refers. */
9558 return modified_type_die (DECL_ORIGINAL_TYPE (name),
9559 is_const_type, is_volatile_type,
9560 context_die);
9561 /* Else cv-qualified version of named type; fall through. */
9564 if (is_const_type)
9566 mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
9567 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
9569 else if (is_volatile_type)
9571 mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
9572 sub_die = modified_type_die (type, 0, 0, context_die);
9574 else if (code == POINTER_TYPE)
9576 mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
9577 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9578 simple_type_size_in_bits (type) / BITS_PER_UNIT);
9579 item_type = TREE_TYPE (type);
9581 else if (code == REFERENCE_TYPE)
9583 mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
9584 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9585 simple_type_size_in_bits (type) / BITS_PER_UNIT);
9586 item_type = TREE_TYPE (type);
9588 else if (is_subrange_type (type))
9590 mod_type_die = subrange_type_die (type, context_die);
9591 item_type = TREE_TYPE (type);
9593 else if (is_base_type (type))
9594 mod_type_die = base_type_die (type);
9595 else
9597 gen_type_die (type, context_die);
9599 /* We have to get the type_main_variant here (and pass that to the
9600 `lookup_type_die' routine) because the ..._TYPE node we have
9601 might simply be a *copy* of some original type node (where the
9602 copy was created to help us keep track of typedef names) and
9603 that copy might have a different TYPE_UID from the original
9604 ..._TYPE node. */
9605 if (TREE_CODE (type) != VECTOR_TYPE)
9606 return lookup_type_die (type_main_variant (type));
9607 else
9608 /* Vectors have the debugging information in the type,
9609 not the main variant. */
9610 return lookup_type_die (type);
9613 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
9614 don't output a DW_TAG_typedef, since there isn't one in the
9615 user's program; just attach a DW_AT_name to the type. */
9616 if (name
9617 && (TREE_CODE (name) != TYPE_DECL
9618 || (TREE_TYPE (name) == qualified_type && DECL_NAME (name))))
9620 if (TREE_CODE (name) == TYPE_DECL)
9621 /* Could just call add_name_and_src_coords_attributes here,
9622 but since this is a builtin type it doesn't have any
9623 useful source coordinates anyway. */
9624 name = DECL_NAME (name);
9625 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
9628 if (qualified_type)
9629 equate_type_number_to_die (qualified_type, mod_type_die);
9631 if (item_type)
9632 /* We must do this after the equate_type_number_to_die call, in case
9633 this is a recursive type. This ensures that the modified_type_die
9634 recursion will terminate even if the type is recursive. Recursive
9635 types are possible in Ada. */
9636 sub_die = modified_type_die (item_type,
9637 TYPE_READONLY (item_type),
9638 TYPE_VOLATILE (item_type),
9639 context_die);
9641 if (sub_die != NULL)
9642 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
9644 return mod_type_die;
9647 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
9648 an enumerated type. */
9650 static inline int
9651 type_is_enum (const_tree type)
9653 return TREE_CODE (type) == ENUMERAL_TYPE;
9656 /* Return the DBX register number described by a given RTL node. */
9658 static unsigned int
9659 dbx_reg_number (const_rtx rtl)
9661 unsigned regno = REGNO (rtl);
9663 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
9665 #ifdef LEAF_REG_REMAP
9666 if (current_function_uses_only_leaf_regs)
9668 int leaf_reg = LEAF_REG_REMAP (regno);
9669 if (leaf_reg != -1)
9670 regno = (unsigned) leaf_reg;
9672 #endif
9674 return DBX_REGISTER_NUMBER (regno);
9677 /* Optionally add a DW_OP_piece term to a location description expression.
9678 DW_OP_piece is only added if the location description expression already
9679 doesn't end with DW_OP_piece. */
9681 static void
9682 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
9684 dw_loc_descr_ref loc;
9686 if (*list_head != NULL)
9688 /* Find the end of the chain. */
9689 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
9692 if (loc->dw_loc_opc != DW_OP_piece)
9693 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
9697 /* Return a location descriptor that designates a machine register or
9698 zero if there is none. */
9700 static dw_loc_descr_ref
9701 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
9703 rtx regs;
9705 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
9706 return 0;
9708 regs = targetm.dwarf_register_span (rtl);
9710 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
9711 return multiple_reg_loc_descriptor (rtl, regs, initialized);
9712 else
9713 return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
9716 /* Return a location descriptor that designates a machine register for
9717 a given hard register number. */
9719 static dw_loc_descr_ref
9720 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
9722 dw_loc_descr_ref reg_loc_descr = new_reg_loc_descr (regno, 0);
9724 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9725 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9727 return reg_loc_descr;
9730 /* Given an RTL of a register, return a location descriptor that
9731 designates a value that spans more than one register. */
9733 static dw_loc_descr_ref
9734 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
9735 enum var_init_status initialized)
9737 int nregs, size, i;
9738 unsigned reg;
9739 dw_loc_descr_ref loc_result = NULL;
9741 reg = REGNO (rtl);
9742 #ifdef LEAF_REG_REMAP
9743 if (current_function_uses_only_leaf_regs)
9745 int leaf_reg = LEAF_REG_REMAP (reg);
9746 if (leaf_reg != -1)
9747 reg = (unsigned) leaf_reg;
9749 #endif
9750 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
9751 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
9753 /* Simple, contiguous registers. */
9754 if (regs == NULL_RTX)
9756 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
9758 loc_result = NULL;
9759 while (nregs--)
9761 dw_loc_descr_ref t;
9763 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
9764 VAR_INIT_STATUS_INITIALIZED);
9765 add_loc_descr (&loc_result, t);
9766 add_loc_descr_op_piece (&loc_result, size);
9767 ++reg;
9769 return loc_result;
9772 /* Now onto stupid register sets in non contiguous locations. */
9774 gcc_assert (GET_CODE (regs) == PARALLEL);
9776 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
9777 loc_result = NULL;
9779 for (i = 0; i < XVECLEN (regs, 0); ++i)
9781 dw_loc_descr_ref t;
9783 t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
9784 VAR_INIT_STATUS_INITIALIZED);
9785 add_loc_descr (&loc_result, t);
9786 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
9787 add_loc_descr_op_piece (&loc_result, size);
9790 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9791 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9792 return loc_result;
9795 #endif /* DWARF2_DEBUGGING_INFO */
9797 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
9799 /* Return a location descriptor that designates a constant. */
9801 static dw_loc_descr_ref
9802 int_loc_descriptor (HOST_WIDE_INT i)
9804 enum dwarf_location_atom op;
9806 /* Pick the smallest representation of a constant, rather than just
9807 defaulting to the LEB encoding. */
9808 if (i >= 0)
9810 if (i <= 31)
9811 op = DW_OP_lit0 + i;
9812 else if (i <= 0xff)
9813 op = DW_OP_const1u;
9814 else if (i <= 0xffff)
9815 op = DW_OP_const2u;
9816 else if (HOST_BITS_PER_WIDE_INT == 32
9817 || i <= 0xffffffff)
9818 op = DW_OP_const4u;
9819 else
9820 op = DW_OP_constu;
9822 else
9824 if (i >= -0x80)
9825 op = DW_OP_const1s;
9826 else if (i >= -0x8000)
9827 op = DW_OP_const2s;
9828 else if (HOST_BITS_PER_WIDE_INT == 32
9829 || i >= -0x80000000)
9830 op = DW_OP_const4s;
9831 else
9832 op = DW_OP_consts;
9835 return new_loc_descr (op, i, 0);
9837 #endif
9839 #ifdef DWARF2_DEBUGGING_INFO
9841 /* Return a location descriptor that designates a base+offset location. */
9843 static dw_loc_descr_ref
9844 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
9845 enum var_init_status initialized)
9847 unsigned int regno;
9848 dw_loc_descr_ref result;
9849 dw_fde_ref fde = current_fde ();
9851 /* We only use "frame base" when we're sure we're talking about the
9852 post-prologue local stack frame. We do this by *not* running
9853 register elimination until this point, and recognizing the special
9854 argument pointer and soft frame pointer rtx's. */
9855 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
9857 rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
9859 if (elim != reg)
9861 if (GET_CODE (elim) == PLUS)
9863 offset += INTVAL (XEXP (elim, 1));
9864 elim = XEXP (elim, 0);
9866 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
9867 && (elim == hard_frame_pointer_rtx
9868 || elim == stack_pointer_rtx))
9869 || elim == (frame_pointer_needed
9870 ? hard_frame_pointer_rtx
9871 : stack_pointer_rtx));
9873 /* If drap register is used to align stack, use frame
9874 pointer + offset to access stack variables. If stack
9875 is aligned without drap, use stack pointer + offset to
9876 access stack variables. */
9877 if (crtl->stack_realign_tried
9878 && cfa.reg == HARD_FRAME_POINTER_REGNUM
9879 && reg == frame_pointer_rtx)
9881 int base_reg
9882 = DWARF_FRAME_REGNUM (cfa.indirect
9883 ? HARD_FRAME_POINTER_REGNUM
9884 : STACK_POINTER_REGNUM);
9885 return new_reg_loc_descr (base_reg, offset);
9888 offset += frame_pointer_fb_offset;
9889 return new_loc_descr (DW_OP_fbreg, offset, 0);
9892 else if (fde
9893 && fde->drap_reg != INVALID_REGNUM
9894 && (fde->drap_reg == REGNO (reg)
9895 || fde->vdrap_reg == REGNO (reg)))
9897 /* Use cfa+offset to represent the location of arguments passed
9898 on stack when drap is used to align stack. */
9899 return new_loc_descr (DW_OP_fbreg, offset, 0);
9902 regno = dbx_reg_number (reg);
9903 if (regno <= 31)
9904 result = new_loc_descr (DW_OP_breg0 + regno, offset, 0);
9905 else
9906 result = new_loc_descr (DW_OP_bregx, regno, offset);
9908 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9909 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9911 return result;
9914 /* Return true if this RTL expression describes a base+offset calculation. */
9916 static inline int
9917 is_based_loc (const_rtx rtl)
9919 return (GET_CODE (rtl) == PLUS
9920 && ((REG_P (XEXP (rtl, 0))
9921 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
9922 && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
9925 /* Return a descriptor that describes the concatenation of N locations
9926 used to form the address of a memory location. */
9928 static dw_loc_descr_ref
9929 concatn_mem_loc_descriptor (rtx concatn, enum machine_mode mode,
9930 enum var_init_status initialized)
9932 unsigned int i;
9933 dw_loc_descr_ref cc_loc_result = NULL;
9934 unsigned int n = XVECLEN (concatn, 0);
9936 for (i = 0; i < n; ++i)
9938 dw_loc_descr_ref ref;
9939 rtx x = XVECEXP (concatn, 0, i);
9941 ref = mem_loc_descriptor (x, mode, VAR_INIT_STATUS_INITIALIZED);
9942 if (ref == NULL)
9943 return NULL;
9945 add_loc_descr (&cc_loc_result, ref);
9946 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
9949 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9950 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9952 return cc_loc_result;
9955 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
9956 failed. */
9958 static dw_loc_descr_ref
9959 tls_mem_loc_descriptor (rtx mem)
9961 tree base;
9962 dw_loc_descr_ref loc_result, loc_result2;
9964 if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
9965 return NULL;
9967 base = get_base_address (MEM_EXPR (mem));
9968 if (base == NULL
9969 || TREE_CODE (base) != VAR_DECL
9970 || !DECL_THREAD_LOCAL_P (base))
9971 return NULL;
9973 loc_result = loc_descriptor_from_tree_1 (MEM_EXPR (mem), 2);
9974 if (loc_result == NULL)
9975 return NULL;
9977 if (INTVAL (MEM_OFFSET (mem)))
9979 if (INTVAL (MEM_OFFSET (mem)) >= 0)
9980 add_loc_descr (&loc_result,
9981 new_loc_descr (DW_OP_plus_uconst,
9982 INTVAL (MEM_OFFSET (mem)), 0));
9983 else
9985 loc_result2 = mem_loc_descriptor (MEM_OFFSET (mem), GET_MODE (mem),
9986 VAR_INIT_STATUS_INITIALIZED);
9987 if (loc_result2 == 0)
9988 return NULL;
9989 add_loc_descr (&loc_result, loc_result2);
9990 add_loc_descr (&loc_result, new_loc_descr (DW_OP_plus, 0, 0));
9994 return loc_result;
9997 /* The following routine converts the RTL for a variable or parameter
9998 (resident in memory) into an equivalent Dwarf representation of a
9999 mechanism for getting the address of that same variable onto the top of a
10000 hypothetical "address evaluation" stack.
10002 When creating memory location descriptors, we are effectively transforming
10003 the RTL for a memory-resident object into its Dwarf postfix expression
10004 equivalent. This routine recursively descends an RTL tree, turning
10005 it into Dwarf postfix code as it goes.
10007 MODE is the mode of the memory reference, needed to handle some
10008 autoincrement addressing modes.
10010 CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
10011 location list for RTL.
10013 Return 0 if we can't represent the location. */
10015 static dw_loc_descr_ref
10016 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
10017 enum var_init_status initialized)
10019 dw_loc_descr_ref mem_loc_result = NULL;
10020 enum dwarf_location_atom op;
10022 /* Note that for a dynamically sized array, the location we will generate a
10023 description of here will be the lowest numbered location which is
10024 actually within the array. That's *not* necessarily the same as the
10025 zeroth element of the array. */
10027 rtl = targetm.delegitimize_address (rtl);
10029 switch (GET_CODE (rtl))
10031 case POST_INC:
10032 case POST_DEC:
10033 case POST_MODIFY:
10034 /* POST_INC and POST_DEC can be handled just like a SUBREG. So we
10035 just fall into the SUBREG code. */
10037 /* ... fall through ... */
10039 case SUBREG:
10040 /* The case of a subreg may arise when we have a local (register)
10041 variable or a formal (register) parameter which doesn't quite fill
10042 up an entire register. For now, just assume that it is
10043 legitimate to make the Dwarf info refer to the whole register which
10044 contains the given subreg. */
10045 rtl = XEXP (rtl, 0);
10047 /* ... fall through ... */
10049 case REG:
10050 /* Whenever a register number forms a part of the description of the
10051 method for calculating the (dynamic) address of a memory resident
10052 object, DWARF rules require the register number be referred to as
10053 a "base register". This distinction is not based in any way upon
10054 what category of register the hardware believes the given register
10055 belongs to. This is strictly DWARF terminology we're dealing with
10056 here. Note that in cases where the location of a memory-resident
10057 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
10058 OP_CONST (0)) the actual DWARF location descriptor that we generate
10059 may just be OP_BASEREG (basereg). This may look deceptively like
10060 the object in question was allocated to a register (rather than in
10061 memory) so DWARF consumers need to be aware of the subtle
10062 distinction between OP_REG and OP_BASEREG. */
10063 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
10064 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
10065 else if (stack_realign_drap
10066 && crtl->drap_reg
10067 && crtl->args.internal_arg_pointer == rtl
10068 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
10070 /* If RTL is internal_arg_pointer, which has been optimized
10071 out, use DRAP instead. */
10072 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
10073 VAR_INIT_STATUS_INITIALIZED);
10075 break;
10077 case MEM:
10078 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
10079 VAR_INIT_STATUS_INITIALIZED);
10080 if (mem_loc_result == NULL)
10081 mem_loc_result = tls_mem_loc_descriptor (rtl);
10082 if (mem_loc_result != 0)
10083 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
10084 break;
10086 case LO_SUM:
10087 rtl = XEXP (rtl, 1);
10089 /* ... fall through ... */
10091 case LABEL_REF:
10092 /* Some ports can transform a symbol ref into a label ref, because
10093 the symbol ref is too far away and has to be dumped into a constant
10094 pool. */
10095 case CONST:
10096 case SYMBOL_REF:
10097 /* Alternatively, the symbol in the constant pool might be referenced
10098 by a different symbol. */
10099 if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
10101 bool marked;
10102 rtx tmp = get_pool_constant_mark (rtl, &marked);
10104 if (GET_CODE (tmp) == SYMBOL_REF)
10106 rtl = tmp;
10107 if (CONSTANT_POOL_ADDRESS_P (tmp))
10108 get_pool_constant_mark (tmp, &marked);
10109 else
10110 marked = true;
10113 /* If all references to this pool constant were optimized away,
10114 it was not output and thus we can't represent it.
10115 FIXME: might try to use DW_OP_const_value here, though
10116 DW_OP_piece complicates it. */
10117 if (!marked)
10118 return 0;
10121 mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
10122 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
10123 mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
10124 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
10125 break;
10127 case PRE_MODIFY:
10128 /* Extract the PLUS expression nested inside and fall into
10129 PLUS code below. */
10130 rtl = XEXP (rtl, 1);
10131 goto plus;
10133 case PRE_INC:
10134 case PRE_DEC:
10135 /* Turn these into a PLUS expression and fall into the PLUS code
10136 below. */
10137 rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
10138 GEN_INT (GET_CODE (rtl) == PRE_INC
10139 ? GET_MODE_UNIT_SIZE (mode)
10140 : -GET_MODE_UNIT_SIZE (mode)));
10142 /* ... fall through ... */
10144 case PLUS:
10145 plus:
10146 if (is_based_loc (rtl))
10147 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
10148 INTVAL (XEXP (rtl, 1)),
10149 VAR_INIT_STATUS_INITIALIZED);
10150 else
10152 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
10153 VAR_INIT_STATUS_INITIALIZED);
10154 if (mem_loc_result == 0)
10155 break;
10157 if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
10158 && INTVAL (XEXP (rtl, 1)) >= 0)
10159 add_loc_descr (&mem_loc_result,
10160 new_loc_descr (DW_OP_plus_uconst,
10161 INTVAL (XEXP (rtl, 1)), 0));
10162 else
10164 dw_loc_descr_ref mem_loc_result2
10165 = mem_loc_descriptor (XEXP (rtl, 1), mode,
10166 VAR_INIT_STATUS_INITIALIZED);
10167 if (mem_loc_result2 == 0)
10168 break;
10169 add_loc_descr (&mem_loc_result, mem_loc_result2);
10170 add_loc_descr (&mem_loc_result,
10171 new_loc_descr (DW_OP_plus, 0, 0));
10174 break;
10176 /* If a pseudo-reg is optimized away, it is possible for it to
10177 be replaced with a MEM containing a multiply or shift. */
10178 case MULT:
10179 op = DW_OP_mul;
10180 goto do_binop;
10182 case ASHIFT:
10183 op = DW_OP_shl;
10184 goto do_binop;
10186 case ASHIFTRT:
10187 op = DW_OP_shra;
10188 goto do_binop;
10190 case LSHIFTRT:
10191 op = DW_OP_shr;
10192 goto do_binop;
10194 do_binop:
10196 dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
10197 VAR_INIT_STATUS_INITIALIZED);
10198 dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
10199 VAR_INIT_STATUS_INITIALIZED);
10201 if (op0 == 0 || op1 == 0)
10202 break;
10204 mem_loc_result = op0;
10205 add_loc_descr (&mem_loc_result, op1);
10206 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
10207 break;
10210 case CONST_INT:
10211 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
10212 break;
10214 case CONCATN:
10215 mem_loc_result = concatn_mem_loc_descriptor (rtl, mode,
10216 VAR_INIT_STATUS_INITIALIZED);
10217 break;
10219 case UNSPEC:
10220 /* If delegitimize_address couldn't do anything with the UNSPEC, we
10221 can't express it in the debug info. This can happen e.g. with some
10222 TLS UNSPECs. */
10223 break;
10225 default:
10226 gcc_unreachable ();
10229 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10230 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10232 return mem_loc_result;
10235 /* Return a descriptor that describes the concatenation of two locations.
10236 This is typically a complex variable. */
10238 static dw_loc_descr_ref
10239 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
10241 dw_loc_descr_ref cc_loc_result = NULL;
10242 dw_loc_descr_ref x0_ref = loc_descriptor (x0, VAR_INIT_STATUS_INITIALIZED);
10243 dw_loc_descr_ref x1_ref = loc_descriptor (x1, VAR_INIT_STATUS_INITIALIZED);
10245 if (x0_ref == 0 || x1_ref == 0)
10246 return 0;
10248 cc_loc_result = x0_ref;
10249 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
10251 add_loc_descr (&cc_loc_result, x1_ref);
10252 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
10254 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10255 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10257 return cc_loc_result;
10260 /* Return a descriptor that describes the concatenation of N
10261 locations. */
10263 static dw_loc_descr_ref
10264 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
10266 unsigned int i;
10267 dw_loc_descr_ref cc_loc_result = NULL;
10268 unsigned int n = XVECLEN (concatn, 0);
10270 for (i = 0; i < n; ++i)
10272 dw_loc_descr_ref ref;
10273 rtx x = XVECEXP (concatn, 0, i);
10275 ref = loc_descriptor (x, VAR_INIT_STATUS_INITIALIZED);
10276 if (ref == NULL)
10277 return NULL;
10279 add_loc_descr (&cc_loc_result, ref);
10280 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
10283 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10284 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10286 return cc_loc_result;
10289 /* Output a proper Dwarf location descriptor for a variable or parameter
10290 which is either allocated in a register or in a memory location. For a
10291 register, we just generate an OP_REG and the register number. For a
10292 memory location we provide a Dwarf postfix expression describing how to
10293 generate the (dynamic) address of the object onto the address stack.
10295 If we don't know how to describe it, return 0. */
10297 static dw_loc_descr_ref
10298 loc_descriptor (rtx rtl, enum var_init_status initialized)
10300 dw_loc_descr_ref loc_result = NULL;
10302 switch (GET_CODE (rtl))
10304 case SUBREG:
10305 /* The case of a subreg may arise when we have a local (register)
10306 variable or a formal (register) parameter which doesn't quite fill
10307 up an entire register. For now, just assume that it is
10308 legitimate to make the Dwarf info refer to the whole register which
10309 contains the given subreg. */
10310 rtl = SUBREG_REG (rtl);
10312 /* ... fall through ... */
10314 case REG:
10315 loc_result = reg_loc_descriptor (rtl, initialized);
10316 break;
10318 case MEM:
10319 loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
10320 initialized);
10321 if (loc_result == NULL)
10322 loc_result = tls_mem_loc_descriptor (rtl);
10323 break;
10325 case CONCAT:
10326 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
10327 initialized);
10328 break;
10330 case CONCATN:
10331 loc_result = concatn_loc_descriptor (rtl, initialized);
10332 break;
10334 case VAR_LOCATION:
10335 /* Single part. */
10336 if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
10338 loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), initialized);
10339 break;
10342 rtl = XEXP (rtl, 1);
10343 /* FALLTHRU */
10345 case PARALLEL:
10347 rtvec par_elems = XVEC (rtl, 0);
10348 int num_elem = GET_NUM_ELEM (par_elems);
10349 enum machine_mode mode;
10350 int i;
10352 /* Create the first one, so we have something to add to. */
10353 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
10354 initialized);
10355 if (loc_result == NULL)
10356 return NULL;
10357 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
10358 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
10359 for (i = 1; i < num_elem; i++)
10361 dw_loc_descr_ref temp;
10363 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
10364 initialized);
10365 if (temp == NULL)
10366 return NULL;
10367 add_loc_descr (&loc_result, temp);
10368 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
10369 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
10372 break;
10374 default:
10375 gcc_unreachable ();
10378 return loc_result;
10381 /* Similar, but generate the descriptor from trees instead of rtl. This comes
10382 up particularly with variable length arrays. WANT_ADDRESS is 2 if this is
10383 a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
10384 top-level invocation, and we require the address of LOC; is 0 if we require
10385 the value of LOC. */
10387 static dw_loc_descr_ref
10388 loc_descriptor_from_tree_1 (tree loc, int want_address)
10390 dw_loc_descr_ref ret, ret1;
10391 int have_address = 0;
10392 enum dwarf_location_atom op;
10394 /* ??? Most of the time we do not take proper care for sign/zero
10395 extending the values properly. Hopefully this won't be a real
10396 problem... */
10398 switch (TREE_CODE (loc))
10400 case ERROR_MARK:
10401 return 0;
10403 case PLACEHOLDER_EXPR:
10404 /* This case involves extracting fields from an object to determine the
10405 position of other fields. We don't try to encode this here. The
10406 only user of this is Ada, which encodes the needed information using
10407 the names of types. */
10408 return 0;
10410 case CALL_EXPR:
10411 return 0;
10413 case PREINCREMENT_EXPR:
10414 case PREDECREMENT_EXPR:
10415 case POSTINCREMENT_EXPR:
10416 case POSTDECREMENT_EXPR:
10417 /* There are no opcodes for these operations. */
10418 return 0;
10420 case ADDR_EXPR:
10421 /* If we already want an address, there's nothing we can do. */
10422 if (want_address)
10423 return 0;
10425 /* Otherwise, process the argument and look for the address. */
10426 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
10428 case VAR_DECL:
10429 if (DECL_THREAD_LOCAL_P (loc))
10431 rtx rtl;
10432 unsigned first_op;
10433 unsigned second_op;
10435 if (targetm.have_tls)
10437 /* If this is not defined, we have no way to emit the
10438 data. */
10439 if (!targetm.asm_out.output_dwarf_dtprel)
10440 return 0;
10442 /* The way DW_OP_GNU_push_tls_address is specified, we
10443 can only look up addresses of objects in the current
10444 module. */
10445 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
10446 return 0;
10447 first_op = INTERNAL_DW_OP_tls_addr;
10448 second_op = DW_OP_GNU_push_tls_address;
10450 else
10452 if (!targetm.emutls.debug_form_tls_address)
10453 return 0;
10454 loc = emutls_decl (loc);
10455 first_op = DW_OP_addr;
10456 second_op = DW_OP_form_tls_address;
10459 rtl = rtl_for_decl_location (loc);
10460 if (rtl == NULL_RTX)
10461 return 0;
10463 if (!MEM_P (rtl))
10464 return 0;
10465 rtl = XEXP (rtl, 0);
10466 if (! CONSTANT_P (rtl))
10467 return 0;
10469 ret = new_loc_descr (first_op, 0, 0);
10470 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
10471 ret->dw_loc_oprnd1.v.val_addr = rtl;
10473 ret1 = new_loc_descr (second_op, 0, 0);
10474 add_loc_descr (&ret, ret1);
10476 have_address = 1;
10477 break;
10479 /* FALLTHRU */
10481 case PARM_DECL:
10482 if (DECL_HAS_VALUE_EXPR_P (loc))
10483 return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
10484 want_address);
10485 /* FALLTHRU */
10487 case RESULT_DECL:
10488 case FUNCTION_DECL:
10490 rtx rtl = rtl_for_decl_location (loc);
10492 if (rtl == NULL_RTX)
10493 return 0;
10494 else if (GET_CODE (rtl) == CONST_INT)
10496 HOST_WIDE_INT val = INTVAL (rtl);
10497 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
10498 val &= GET_MODE_MASK (DECL_MODE (loc));
10499 ret = int_loc_descriptor (val);
10501 else if (GET_CODE (rtl) == CONST_STRING)
10502 return 0;
10503 else if (CONSTANT_P (rtl))
10505 ret = new_loc_descr (DW_OP_addr, 0, 0);
10506 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
10507 ret->dw_loc_oprnd1.v.val_addr = rtl;
10509 else
10511 enum machine_mode mode;
10513 /* Certain constructs can only be represented at top-level. */
10514 if (want_address == 2)
10515 return loc_descriptor (rtl, VAR_INIT_STATUS_INITIALIZED);
10517 mode = GET_MODE (rtl);
10518 if (MEM_P (rtl))
10520 rtl = XEXP (rtl, 0);
10521 have_address = 1;
10523 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
10526 break;
10528 case INDIRECT_REF:
10529 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10530 have_address = 1;
10531 break;
10533 case COMPOUND_EXPR:
10534 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
10536 CASE_CONVERT:
10537 case VIEW_CONVERT_EXPR:
10538 case SAVE_EXPR:
10539 case MODIFY_EXPR:
10540 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), want_address);
10542 case COMPONENT_REF:
10543 case BIT_FIELD_REF:
10544 case ARRAY_REF:
10545 case ARRAY_RANGE_REF:
10547 tree obj, offset;
10548 HOST_WIDE_INT bitsize, bitpos, bytepos;
10549 enum machine_mode mode;
10550 int volatilep;
10551 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
10553 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
10554 &unsignedp, &volatilep, false);
10556 if (obj == loc)
10557 return 0;
10559 ret = loc_descriptor_from_tree_1 (obj, 1);
10560 if (ret == 0
10561 || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
10562 return 0;
10564 if (offset != NULL_TREE)
10566 /* Variable offset. */
10567 ret1 = loc_descriptor_from_tree_1 (offset, 0);
10568 if (ret1 == 0)
10569 return 0;
10570 add_loc_descr (&ret, ret1);
10571 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
10574 bytepos = bitpos / BITS_PER_UNIT;
10575 if (bytepos > 0)
10576 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
10577 else if (bytepos < 0)
10579 add_loc_descr (&ret, int_loc_descriptor (bytepos));
10580 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
10583 have_address = 1;
10584 break;
10587 case INTEGER_CST:
10588 if (host_integerp (loc, 0))
10589 ret = int_loc_descriptor (tree_low_cst (loc, 0));
10590 else
10591 return 0;
10592 break;
10594 case CONSTRUCTOR:
10596 /* Get an RTL for this, if something has been emitted. */
10597 rtx rtl = lookup_constant_def (loc);
10598 enum machine_mode mode;
10600 if (!rtl || !MEM_P (rtl))
10601 return 0;
10602 mode = GET_MODE (rtl);
10603 rtl = XEXP (rtl, 0);
10604 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
10605 have_address = 1;
10606 break;
10609 case TRUTH_AND_EXPR:
10610 case TRUTH_ANDIF_EXPR:
10611 case BIT_AND_EXPR:
10612 op = DW_OP_and;
10613 goto do_binop;
10615 case TRUTH_XOR_EXPR:
10616 case BIT_XOR_EXPR:
10617 op = DW_OP_xor;
10618 goto do_binop;
10620 case TRUTH_OR_EXPR:
10621 case TRUTH_ORIF_EXPR:
10622 case BIT_IOR_EXPR:
10623 op = DW_OP_or;
10624 goto do_binop;
10626 case FLOOR_DIV_EXPR:
10627 case CEIL_DIV_EXPR:
10628 case ROUND_DIV_EXPR:
10629 case TRUNC_DIV_EXPR:
10630 op = DW_OP_div;
10631 goto do_binop;
10633 case MINUS_EXPR:
10634 op = DW_OP_minus;
10635 goto do_binop;
10637 case FLOOR_MOD_EXPR:
10638 case CEIL_MOD_EXPR:
10639 case ROUND_MOD_EXPR:
10640 case TRUNC_MOD_EXPR:
10641 op = DW_OP_mod;
10642 goto do_binop;
10644 case MULT_EXPR:
10645 op = DW_OP_mul;
10646 goto do_binop;
10648 case LSHIFT_EXPR:
10649 op = DW_OP_shl;
10650 goto do_binop;
10652 case RSHIFT_EXPR:
10653 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
10654 goto do_binop;
10656 case POINTER_PLUS_EXPR:
10657 case PLUS_EXPR:
10658 if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
10659 && host_integerp (TREE_OPERAND (loc, 1), 0))
10661 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10662 if (ret == 0)
10663 return 0;
10665 add_loc_descr (&ret,
10666 new_loc_descr (DW_OP_plus_uconst,
10667 tree_low_cst (TREE_OPERAND (loc, 1),
10669 0));
10670 break;
10673 op = DW_OP_plus;
10674 goto do_binop;
10676 case LE_EXPR:
10677 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10678 return 0;
10680 op = DW_OP_le;
10681 goto do_binop;
10683 case GE_EXPR:
10684 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10685 return 0;
10687 op = DW_OP_ge;
10688 goto do_binop;
10690 case LT_EXPR:
10691 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10692 return 0;
10694 op = DW_OP_lt;
10695 goto do_binop;
10697 case GT_EXPR:
10698 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10699 return 0;
10701 op = DW_OP_gt;
10702 goto do_binop;
10704 case EQ_EXPR:
10705 op = DW_OP_eq;
10706 goto do_binop;
10708 case NE_EXPR:
10709 op = DW_OP_ne;
10710 goto do_binop;
10712 do_binop:
10713 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10714 ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
10715 if (ret == 0 || ret1 == 0)
10716 return 0;
10718 add_loc_descr (&ret, ret1);
10719 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10720 break;
10722 case TRUTH_NOT_EXPR:
10723 case BIT_NOT_EXPR:
10724 op = DW_OP_not;
10725 goto do_unop;
10727 case ABS_EXPR:
10728 op = DW_OP_abs;
10729 goto do_unop;
10731 case NEGATE_EXPR:
10732 op = DW_OP_neg;
10733 goto do_unop;
10735 do_unop:
10736 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10737 if (ret == 0)
10738 return 0;
10740 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10741 break;
10743 case MIN_EXPR:
10744 case MAX_EXPR:
10746 const enum tree_code code =
10747 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
10749 loc = build3 (COND_EXPR, TREE_TYPE (loc),
10750 build2 (code, integer_type_node,
10751 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
10752 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
10755 /* ... fall through ... */
10757 case COND_EXPR:
10759 dw_loc_descr_ref lhs
10760 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
10761 dw_loc_descr_ref rhs
10762 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
10763 dw_loc_descr_ref bra_node, jump_node, tmp;
10765 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10766 if (ret == 0 || lhs == 0 || rhs == 0)
10767 return 0;
10769 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
10770 add_loc_descr (&ret, bra_node);
10772 add_loc_descr (&ret, rhs);
10773 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
10774 add_loc_descr (&ret, jump_node);
10776 add_loc_descr (&ret, lhs);
10777 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
10778 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
10780 /* ??? Need a node to point the skip at. Use a nop. */
10781 tmp = new_loc_descr (DW_OP_nop, 0, 0);
10782 add_loc_descr (&ret, tmp);
10783 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
10784 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
10786 break;
10788 case FIX_TRUNC_EXPR:
10789 return 0;
10791 default:
10792 /* Leave front-end specific codes as simply unknown. This comes
10793 up, for instance, with the C STMT_EXPR. */
10794 if ((unsigned int) TREE_CODE (loc)
10795 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
10796 return 0;
10798 #ifdef ENABLE_CHECKING
10799 /* Otherwise this is a generic code; we should just lists all of
10800 these explicitly. We forgot one. */
10801 gcc_unreachable ();
10802 #else
10803 /* In a release build, we want to degrade gracefully: better to
10804 generate incomplete debugging information than to crash. */
10805 return NULL;
10806 #endif
10809 /* Show if we can't fill the request for an address. */
10810 if (want_address && !have_address)
10811 return 0;
10813 /* If we've got an address and don't want one, dereference. */
10814 if (!want_address && have_address && ret)
10816 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
10818 if (size > DWARF2_ADDR_SIZE || size == -1)
10819 return 0;
10820 else if (size == DWARF2_ADDR_SIZE)
10821 op = DW_OP_deref;
10822 else
10823 op = DW_OP_deref_size;
10825 add_loc_descr (&ret, new_loc_descr (op, size, 0));
10828 return ret;
10831 static inline dw_loc_descr_ref
10832 loc_descriptor_from_tree (tree loc)
10834 return loc_descriptor_from_tree_1 (loc, 2);
10837 /* Given a value, round it up to the lowest multiple of `boundary'
10838 which is not less than the value itself. */
10840 static inline HOST_WIDE_INT
10841 ceiling (HOST_WIDE_INT value, unsigned int boundary)
10843 return (((value + boundary - 1) / boundary) * boundary);
10846 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
10847 pointer to the declared type for the relevant field variable, or return
10848 `integer_type_node' if the given node turns out to be an
10849 ERROR_MARK node. */
10851 static inline tree
10852 field_type (const_tree decl)
10854 tree type;
10856 if (TREE_CODE (decl) == ERROR_MARK)
10857 return integer_type_node;
10859 type = DECL_BIT_FIELD_TYPE (decl);
10860 if (type == NULL_TREE)
10861 type = TREE_TYPE (decl);
10863 return type;
10866 /* Given a pointer to a tree node, return the alignment in bits for
10867 it, or else return BITS_PER_WORD if the node actually turns out to
10868 be an ERROR_MARK node. */
10870 static inline unsigned
10871 simple_type_align_in_bits (const_tree type)
10873 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
10876 static inline unsigned
10877 simple_decl_align_in_bits (const_tree decl)
10879 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
10882 /* Return the result of rounding T up to ALIGN. */
10884 static inline HOST_WIDE_INT
10885 round_up_to_align (HOST_WIDE_INT t, unsigned int align)
10887 /* We must be careful if T is negative because HOST_WIDE_INT can be
10888 either "above" or "below" unsigned int as per the C promotion
10889 rules, depending on the host, thus making the signedness of the
10890 direct multiplication and division unpredictable. */
10891 unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
10893 u += align - 1;
10894 u /= align;
10895 u *= align;
10897 return (HOST_WIDE_INT) u;
10900 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
10901 lowest addressed byte of the "containing object" for the given FIELD_DECL,
10902 or return 0 if we are unable to determine what that offset is, either
10903 because the argument turns out to be a pointer to an ERROR_MARK node, or
10904 because the offset is actually variable. (We can't handle the latter case
10905 just yet). */
10907 static HOST_WIDE_INT
10908 field_byte_offset (const_tree decl)
10910 HOST_WIDE_INT object_offset_in_bits;
10911 HOST_WIDE_INT bitpos_int;
10913 if (TREE_CODE (decl) == ERROR_MARK)
10914 return 0;
10916 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
10918 /* We cannot yet cope with fields whose positions are variable, so
10919 for now, when we see such things, we simply return 0. Someday, we may
10920 be able to handle such cases, but it will be damn difficult. */
10921 if (! host_integerp (bit_position (decl), 0))
10922 return 0;
10924 bitpos_int = int_bit_position (decl);
10926 #ifdef PCC_BITFIELD_TYPE_MATTERS
10927 if (PCC_BITFIELD_TYPE_MATTERS)
10929 tree type;
10930 tree field_size_tree;
10931 HOST_WIDE_INT deepest_bitpos;
10932 unsigned HOST_WIDE_INT field_size_in_bits;
10933 unsigned int type_align_in_bits;
10934 unsigned int decl_align_in_bits;
10935 unsigned HOST_WIDE_INT type_size_in_bits;
10937 type = field_type (decl);
10938 field_size_tree = DECL_SIZE (decl);
10940 /* The size could be unspecified if there was an error, or for
10941 a flexible array member. */
10942 if (! field_size_tree)
10943 field_size_tree = bitsize_zero_node;
10945 /* If we don't know the size of the field, pretend it's a full word. */
10946 if (host_integerp (field_size_tree, 1))
10947 field_size_in_bits = tree_low_cst (field_size_tree, 1);
10948 else
10949 field_size_in_bits = BITS_PER_WORD;
10951 type_size_in_bits = simple_type_size_in_bits (type);
10952 type_align_in_bits = simple_type_align_in_bits (type);
10953 decl_align_in_bits = simple_decl_align_in_bits (decl);
10955 /* The GCC front-end doesn't make any attempt to keep track of the
10956 starting bit offset (relative to the start of the containing
10957 structure type) of the hypothetical "containing object" for a
10958 bit-field. Thus, when computing the byte offset value for the
10959 start of the "containing object" of a bit-field, we must deduce
10960 this information on our own. This can be rather tricky to do in
10961 some cases. For example, handling the following structure type
10962 definition when compiling for an i386/i486 target (which only
10963 aligns long long's to 32-bit boundaries) can be very tricky:
10965 struct S { int field1; long long field2:31; };
10967 Fortunately, there is a simple rule-of-thumb which can be used
10968 in such cases. When compiling for an i386/i486, GCC will
10969 allocate 8 bytes for the structure shown above. It decides to
10970 do this based upon one simple rule for bit-field allocation.
10971 GCC allocates each "containing object" for each bit-field at
10972 the first (i.e. lowest addressed) legitimate alignment boundary
10973 (based upon the required minimum alignment for the declared
10974 type of the field) which it can possibly use, subject to the
10975 condition that there is still enough available space remaining
10976 in the containing object (when allocated at the selected point)
10977 to fully accommodate all of the bits of the bit-field itself.
10979 This simple rule makes it obvious why GCC allocates 8 bytes for
10980 each object of the structure type shown above. When looking
10981 for a place to allocate the "containing object" for `field2',
10982 the compiler simply tries to allocate a 64-bit "containing
10983 object" at each successive 32-bit boundary (starting at zero)
10984 until it finds a place to allocate that 64- bit field such that
10985 at least 31 contiguous (and previously unallocated) bits remain
10986 within that selected 64 bit field. (As it turns out, for the
10987 example above, the compiler finds it is OK to allocate the
10988 "containing object" 64-bit field at bit-offset zero within the
10989 structure type.)
10991 Here we attempt to work backwards from the limited set of facts
10992 we're given, and we try to deduce from those facts, where GCC
10993 must have believed that the containing object started (within
10994 the structure type). The value we deduce is then used (by the
10995 callers of this routine) to generate DW_AT_location and
10996 DW_AT_bit_offset attributes for fields (both bit-fields and, in
10997 the case of DW_AT_location, regular fields as well). */
10999 /* Figure out the bit-distance from the start of the structure to
11000 the "deepest" bit of the bit-field. */
11001 deepest_bitpos = bitpos_int + field_size_in_bits;
11003 /* This is the tricky part. Use some fancy footwork to deduce
11004 where the lowest addressed bit of the containing object must
11005 be. */
11006 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
11008 /* Round up to type_align by default. This works best for
11009 bitfields. */
11010 object_offset_in_bits
11011 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
11013 if (object_offset_in_bits > bitpos_int)
11015 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
11017 /* Round up to decl_align instead. */
11018 object_offset_in_bits
11019 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
11022 else
11023 #endif
11024 object_offset_in_bits = bitpos_int;
11026 return object_offset_in_bits / BITS_PER_UNIT;
11029 /* The following routines define various Dwarf attributes and any data
11030 associated with them. */
11032 /* Add a location description attribute value to a DIE.
11034 This emits location attributes suitable for whole variables and
11035 whole parameters. Note that the location attributes for struct fields are
11036 generated by the routine `data_member_location_attribute' below. */
11038 static inline void
11039 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
11040 dw_loc_descr_ref descr)
11042 if (descr != 0)
11043 add_AT_loc (die, attr_kind, descr);
11046 /* Attach the specialized form of location attribute used for data members of
11047 struct and union types. In the special case of a FIELD_DECL node which
11048 represents a bit-field, the "offset" part of this special location
11049 descriptor must indicate the distance in bytes from the lowest-addressed
11050 byte of the containing struct or union type to the lowest-addressed byte of
11051 the "containing object" for the bit-field. (See the `field_byte_offset'
11052 function above).
11054 For any given bit-field, the "containing object" is a hypothetical object
11055 (of some integral or enum type) within which the given bit-field lives. The
11056 type of this hypothetical "containing object" is always the same as the
11057 declared type of the individual bit-field itself (for GCC anyway... the
11058 DWARF spec doesn't actually mandate this). Note that it is the size (in
11059 bytes) of the hypothetical "containing object" which will be given in the
11060 DW_AT_byte_size attribute for this bit-field. (See the
11061 `byte_size_attribute' function below.) It is also used when calculating the
11062 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
11063 function below.) */
11065 static void
11066 add_data_member_location_attribute (dw_die_ref die, tree decl)
11068 HOST_WIDE_INT offset;
11069 dw_loc_descr_ref loc_descr = 0;
11071 if (TREE_CODE (decl) == TREE_BINFO)
11073 /* We're working on the TAG_inheritance for a base class. */
11074 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
11076 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
11077 aren't at a fixed offset from all (sub)objects of the same
11078 type. We need to extract the appropriate offset from our
11079 vtable. The following dwarf expression means
11081 BaseAddr = ObAddr + *((*ObAddr) - Offset)
11083 This is specific to the V3 ABI, of course. */
11085 dw_loc_descr_ref tmp;
11087 /* Make a copy of the object address. */
11088 tmp = new_loc_descr (DW_OP_dup, 0, 0);
11089 add_loc_descr (&loc_descr, tmp);
11091 /* Extract the vtable address. */
11092 tmp = new_loc_descr (DW_OP_deref, 0, 0);
11093 add_loc_descr (&loc_descr, tmp);
11095 /* Calculate the address of the offset. */
11096 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
11097 gcc_assert (offset < 0);
11099 tmp = int_loc_descriptor (-offset);
11100 add_loc_descr (&loc_descr, tmp);
11101 tmp = new_loc_descr (DW_OP_minus, 0, 0);
11102 add_loc_descr (&loc_descr, tmp);
11104 /* Extract the offset. */
11105 tmp = new_loc_descr (DW_OP_deref, 0, 0);
11106 add_loc_descr (&loc_descr, tmp);
11108 /* Add it to the object address. */
11109 tmp = new_loc_descr (DW_OP_plus, 0, 0);
11110 add_loc_descr (&loc_descr, tmp);
11112 else
11113 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
11115 else
11116 offset = field_byte_offset (decl);
11118 if (! loc_descr)
11120 enum dwarf_location_atom op;
11122 /* The DWARF2 standard says that we should assume that the structure
11123 address is already on the stack, so we can specify a structure field
11124 address by using DW_OP_plus_uconst. */
11126 #ifdef MIPS_DEBUGGING_INFO
11127 /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
11128 operator correctly. It works only if we leave the offset on the
11129 stack. */
11130 op = DW_OP_constu;
11131 #else
11132 op = DW_OP_plus_uconst;
11133 #endif
11135 loc_descr = new_loc_descr (op, offset, 0);
11138 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
11141 /* Writes integer values to dw_vec_const array. */
11143 static void
11144 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
11146 while (size != 0)
11148 *dest++ = val & 0xff;
11149 val >>= 8;
11150 --size;
11154 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
11156 static HOST_WIDE_INT
11157 extract_int (const unsigned char *src, unsigned int size)
11159 HOST_WIDE_INT val = 0;
11161 src += size;
11162 while (size != 0)
11164 val <<= 8;
11165 val |= *--src & 0xff;
11166 --size;
11168 return val;
11171 /* Writes floating point values to dw_vec_const array. */
11173 static void
11174 insert_float (const_rtx rtl, unsigned char *array)
11176 REAL_VALUE_TYPE rv;
11177 long val[4];
11178 int i;
11180 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
11181 real_to_target (val, &rv, GET_MODE (rtl));
11183 /* real_to_target puts 32-bit pieces in each long. Pack them. */
11184 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
11186 insert_int (val[i], 4, array);
11187 array += 4;
11191 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
11192 does not have a "location" either in memory or in a register. These
11193 things can arise in GNU C when a constant is passed as an actual parameter
11194 to an inlined function. They can also arise in C++ where declared
11195 constants do not necessarily get memory "homes". */
11197 static void
11198 add_const_value_attribute (dw_die_ref die, rtx rtl)
11200 switch (GET_CODE (rtl))
11202 case CONST_INT:
11204 HOST_WIDE_INT val = INTVAL (rtl);
11206 if (val < 0)
11207 add_AT_int (die, DW_AT_const_value, val);
11208 else
11209 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
11211 break;
11213 case CONST_DOUBLE:
11214 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
11215 floating-point constant. A CONST_DOUBLE is used whenever the
11216 constant requires more than one word in order to be adequately
11217 represented. We output CONST_DOUBLEs as blocks. */
11219 enum machine_mode mode = GET_MODE (rtl);
11221 if (SCALAR_FLOAT_MODE_P (mode))
11223 unsigned int length = GET_MODE_SIZE (mode);
11224 unsigned char *array = GGC_NEWVEC (unsigned char, length);
11226 insert_float (rtl, array);
11227 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
11229 else
11231 /* ??? We really should be using HOST_WIDE_INT throughout. */
11232 gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
11234 add_AT_long_long (die, DW_AT_const_value,
11235 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
11238 break;
11240 case CONST_VECTOR:
11242 enum machine_mode mode = GET_MODE (rtl);
11243 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
11244 unsigned int length = CONST_VECTOR_NUNITS (rtl);
11245 unsigned char *array = GGC_NEWVEC (unsigned char, length * elt_size);
11246 unsigned int i;
11247 unsigned char *p;
11249 switch (GET_MODE_CLASS (mode))
11251 case MODE_VECTOR_INT:
11252 for (i = 0, p = array; i < length; i++, p += elt_size)
11254 rtx elt = CONST_VECTOR_ELT (rtl, i);
11255 HOST_WIDE_INT lo, hi;
11257 switch (GET_CODE (elt))
11259 case CONST_INT:
11260 lo = INTVAL (elt);
11261 hi = -(lo < 0);
11262 break;
11264 case CONST_DOUBLE:
11265 lo = CONST_DOUBLE_LOW (elt);
11266 hi = CONST_DOUBLE_HIGH (elt);
11267 break;
11269 default:
11270 gcc_unreachable ();
11273 if (elt_size <= sizeof (HOST_WIDE_INT))
11274 insert_int (lo, elt_size, p);
11275 else
11277 unsigned char *p0 = p;
11278 unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
11280 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
11281 if (WORDS_BIG_ENDIAN)
11283 p0 = p1;
11284 p1 = p;
11286 insert_int (lo, sizeof (HOST_WIDE_INT), p0);
11287 insert_int (hi, sizeof (HOST_WIDE_INT), p1);
11290 break;
11292 case MODE_VECTOR_FLOAT:
11293 for (i = 0, p = array; i < length; i++, p += elt_size)
11295 rtx elt = CONST_VECTOR_ELT (rtl, i);
11296 insert_float (elt, p);
11298 break;
11300 default:
11301 gcc_unreachable ();
11304 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
11306 break;
11308 case CONST_STRING:
11309 add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
11310 break;
11312 case SYMBOL_REF:
11313 case LABEL_REF:
11314 case CONST:
11315 add_AT_addr (die, DW_AT_const_value, rtl);
11316 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
11317 break;
11319 case PLUS:
11320 /* In cases where an inlined instance of an inline function is passed
11321 the address of an `auto' variable (which is local to the caller) we
11322 can get a situation where the DECL_RTL of the artificial local
11323 variable (for the inlining) which acts as a stand-in for the
11324 corresponding formal parameter (of the inline function) will look
11325 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
11326 exactly a compile-time constant expression, but it isn't the address
11327 of the (artificial) local variable either. Rather, it represents the
11328 *value* which the artificial local variable always has during its
11329 lifetime. We currently have no way to represent such quasi-constant
11330 values in Dwarf, so for now we just punt and generate nothing. */
11331 break;
11333 default:
11334 /* No other kinds of rtx should be possible here. */
11335 gcc_unreachable ();
11340 /* Determine whether the evaluation of EXPR references any variables
11341 or functions which aren't otherwise used (and therefore may not be
11342 output). */
11343 static tree
11344 reference_to_unused (tree * tp, int * walk_subtrees,
11345 void * data ATTRIBUTE_UNUSED)
11347 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
11348 *walk_subtrees = 0;
11350 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
11351 && ! TREE_ASM_WRITTEN (*tp))
11352 return *tp;
11353 /* ??? The C++ FE emits debug information for using decls, so
11354 putting gcc_unreachable here falls over. See PR31899. For now
11355 be conservative. */
11356 else if (!cgraph_global_info_ready
11357 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
11358 return *tp;
11359 else if (DECL_P (*tp) && TREE_CODE (*tp) == VAR_DECL)
11361 struct varpool_node *node = varpool_node (*tp);
11362 if (!node->needed)
11363 return *tp;
11365 else if (DECL_P (*tp) && TREE_CODE (*tp) == FUNCTION_DECL
11366 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
11368 struct cgraph_node *node = cgraph_node (*tp);
11369 if (!node->output)
11370 return *tp;
11372 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
11373 return *tp;
11375 return NULL_TREE;
11378 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
11379 for use in a later add_const_value_attribute call. */
11381 static rtx
11382 rtl_for_decl_init (tree init, tree type)
11384 rtx rtl = NULL_RTX;
11386 /* If a variable is initialized with a string constant without embedded
11387 zeros, build CONST_STRING. */
11388 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
11390 tree enttype = TREE_TYPE (type);
11391 tree domain = TYPE_DOMAIN (type);
11392 enum machine_mode mode = TYPE_MODE (enttype);
11394 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
11395 && domain
11396 && integer_zerop (TYPE_MIN_VALUE (domain))
11397 && compare_tree_int (TYPE_MAX_VALUE (domain),
11398 TREE_STRING_LENGTH (init) - 1) == 0
11399 && ((size_t) TREE_STRING_LENGTH (init)
11400 == strlen (TREE_STRING_POINTER (init)) + 1))
11401 rtl = gen_rtx_CONST_STRING (VOIDmode,
11402 ggc_strdup (TREE_STRING_POINTER (init)));
11404 /* Other aggregates, and complex values, could be represented using
11405 CONCAT: FIXME! */
11406 else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
11408 /* Vectors only work if their mode is supported by the target.
11409 FIXME: generic vectors ought to work too. */
11410 else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
11412 /* If the initializer is something that we know will expand into an
11413 immediate RTL constant, expand it now. We must be careful not to
11414 reference variables which won't be output. */
11415 else if (initializer_constant_valid_p (init, type)
11416 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
11418 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
11419 possible. */
11420 if (TREE_CODE (type) == VECTOR_TYPE)
11421 switch (TREE_CODE (init))
11423 case VECTOR_CST:
11424 break;
11425 case CONSTRUCTOR:
11426 if (TREE_CONSTANT (init))
11428 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
11429 bool constant_p = true;
11430 tree value;
11431 unsigned HOST_WIDE_INT ix;
11433 /* Even when ctor is constant, it might contain non-*_CST
11434 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
11435 belong into VECTOR_CST nodes. */
11436 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
11437 if (!CONSTANT_CLASS_P (value))
11439 constant_p = false;
11440 break;
11443 if (constant_p)
11445 init = build_vector_from_ctor (type, elts);
11446 break;
11449 /* FALLTHRU */
11451 default:
11452 return NULL;
11455 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
11457 /* If expand_expr returns a MEM, it wasn't immediate. */
11458 gcc_assert (!rtl || !MEM_P (rtl));
11461 return rtl;
11464 /* Generate RTL for the variable DECL to represent its location. */
11466 static rtx
11467 rtl_for_decl_location (tree decl)
11469 rtx rtl;
11471 /* Here we have to decide where we are going to say the parameter "lives"
11472 (as far as the debugger is concerned). We only have a couple of
11473 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
11475 DECL_RTL normally indicates where the parameter lives during most of the
11476 activation of the function. If optimization is enabled however, this
11477 could be either NULL or else a pseudo-reg. Both of those cases indicate
11478 that the parameter doesn't really live anywhere (as far as the code
11479 generation parts of GCC are concerned) during most of the function's
11480 activation. That will happen (for example) if the parameter is never
11481 referenced within the function.
11483 We could just generate a location descriptor here for all non-NULL
11484 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
11485 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
11486 where DECL_RTL is NULL or is a pseudo-reg.
11488 Note however that we can only get away with using DECL_INCOMING_RTL as
11489 a backup substitute for DECL_RTL in certain limited cases. In cases
11490 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
11491 we can be sure that the parameter was passed using the same type as it is
11492 declared to have within the function, and that its DECL_INCOMING_RTL
11493 points us to a place where a value of that type is passed.
11495 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
11496 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
11497 because in these cases DECL_INCOMING_RTL points us to a value of some
11498 type which is *different* from the type of the parameter itself. Thus,
11499 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
11500 such cases, the debugger would end up (for example) trying to fetch a
11501 `float' from a place which actually contains the first part of a
11502 `double'. That would lead to really incorrect and confusing
11503 output at debug-time.
11505 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
11506 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
11507 are a couple of exceptions however. On little-endian machines we can
11508 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
11509 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
11510 an integral type that is smaller than TREE_TYPE (decl). These cases arise
11511 when (on a little-endian machine) a non-prototyped function has a
11512 parameter declared to be of type `short' or `char'. In such cases,
11513 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
11514 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
11515 passed `int' value. If the debugger then uses that address to fetch
11516 a `short' or a `char' (on a little-endian machine) the result will be
11517 the correct data, so we allow for such exceptional cases below.
11519 Note that our goal here is to describe the place where the given formal
11520 parameter lives during most of the function's activation (i.e. between the
11521 end of the prologue and the start of the epilogue). We'll do that as best
11522 as we can. Note however that if the given formal parameter is modified
11523 sometime during the execution of the function, then a stack backtrace (at
11524 debug-time) will show the function as having been called with the *new*
11525 value rather than the value which was originally passed in. This happens
11526 rarely enough that it is not a major problem, but it *is* a problem, and
11527 I'd like to fix it.
11529 A future version of dwarf2out.c may generate two additional attributes for
11530 any given DW_TAG_formal_parameter DIE which will describe the "passed
11531 type" and the "passed location" for the given formal parameter in addition
11532 to the attributes we now generate to indicate the "declared type" and the
11533 "active location" for each parameter. This additional set of attributes
11534 could be used by debuggers for stack backtraces. Separately, note that
11535 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
11536 This happens (for example) for inlined-instances of inline function formal
11537 parameters which are never referenced. This really shouldn't be
11538 happening. All PARM_DECL nodes should get valid non-NULL
11539 DECL_INCOMING_RTL values. FIXME. */
11541 /* Use DECL_RTL as the "location" unless we find something better. */
11542 rtl = DECL_RTL_IF_SET (decl);
11544 /* When generating abstract instances, ignore everything except
11545 constants, symbols living in memory, and symbols living in
11546 fixed registers. */
11547 if (! reload_completed)
11549 if (rtl
11550 && (CONSTANT_P (rtl)
11551 || (MEM_P (rtl)
11552 && CONSTANT_P (XEXP (rtl, 0)))
11553 || (REG_P (rtl)
11554 && TREE_CODE (decl) == VAR_DECL
11555 && TREE_STATIC (decl))))
11557 rtl = targetm.delegitimize_address (rtl);
11558 return rtl;
11560 rtl = NULL_RTX;
11562 else if (TREE_CODE (decl) == PARM_DECL)
11564 if (rtl == NULL_RTX || is_pseudo_reg (rtl))
11566 tree declared_type = TREE_TYPE (decl);
11567 tree passed_type = DECL_ARG_TYPE (decl);
11568 enum machine_mode dmode = TYPE_MODE (declared_type);
11569 enum machine_mode pmode = TYPE_MODE (passed_type);
11571 /* This decl represents a formal parameter which was optimized out.
11572 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
11573 all cases where (rtl == NULL_RTX) just below. */
11574 if (dmode == pmode)
11575 rtl = DECL_INCOMING_RTL (decl);
11576 else if (SCALAR_INT_MODE_P (dmode)
11577 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
11578 && DECL_INCOMING_RTL (decl))
11580 rtx inc = DECL_INCOMING_RTL (decl);
11581 if (REG_P (inc))
11582 rtl = inc;
11583 else if (MEM_P (inc))
11585 if (BYTES_BIG_ENDIAN)
11586 rtl = adjust_address_nv (inc, dmode,
11587 GET_MODE_SIZE (pmode)
11588 - GET_MODE_SIZE (dmode));
11589 else
11590 rtl = inc;
11595 /* If the parm was passed in registers, but lives on the stack, then
11596 make a big endian correction if the mode of the type of the
11597 parameter is not the same as the mode of the rtl. */
11598 /* ??? This is the same series of checks that are made in dbxout.c before
11599 we reach the big endian correction code there. It isn't clear if all
11600 of these checks are necessary here, but keeping them all is the safe
11601 thing to do. */
11602 else if (MEM_P (rtl)
11603 && XEXP (rtl, 0) != const0_rtx
11604 && ! CONSTANT_P (XEXP (rtl, 0))
11605 /* Not passed in memory. */
11606 && !MEM_P (DECL_INCOMING_RTL (decl))
11607 /* Not passed by invisible reference. */
11608 && (!REG_P (XEXP (rtl, 0))
11609 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
11610 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
11611 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
11612 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
11613 #endif
11615 /* Big endian correction check. */
11616 && BYTES_BIG_ENDIAN
11617 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
11618 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
11619 < UNITS_PER_WORD))
11621 int offset = (UNITS_PER_WORD
11622 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
11624 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
11625 plus_constant (XEXP (rtl, 0), offset));
11628 else if (TREE_CODE (decl) == VAR_DECL
11629 && rtl
11630 && MEM_P (rtl)
11631 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
11632 && BYTES_BIG_ENDIAN)
11634 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
11635 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
11637 /* If a variable is declared "register" yet is smaller than
11638 a register, then if we store the variable to memory, it
11639 looks like we're storing a register-sized value, when in
11640 fact we are not. We need to adjust the offset of the
11641 storage location to reflect the actual value's bytes,
11642 else gdb will not be able to display it. */
11643 if (rsize > dsize)
11644 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
11645 plus_constant (XEXP (rtl, 0), rsize-dsize));
11648 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
11649 and will have been substituted directly into all expressions that use it.
11650 C does not have such a concept, but C++ and other languages do. */
11651 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
11652 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
11654 if (rtl)
11655 rtl = targetm.delegitimize_address (rtl);
11657 /* If we don't look past the constant pool, we risk emitting a
11658 reference to a constant pool entry that isn't referenced from
11659 code, and thus is not emitted. */
11660 if (rtl)
11661 rtl = avoid_constant_pool_reference (rtl);
11663 return rtl;
11666 /* We need to figure out what section we should use as the base for the
11667 address ranges where a given location is valid.
11668 1. If this particular DECL has a section associated with it, use that.
11669 2. If this function has a section associated with it, use that.
11670 3. Otherwise, use the text section.
11671 XXX: If you split a variable across multiple sections, we won't notice. */
11673 static const char *
11674 secname_for_decl (const_tree decl)
11676 const char *secname;
11678 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
11680 tree sectree = DECL_SECTION_NAME (decl);
11681 secname = TREE_STRING_POINTER (sectree);
11683 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
11685 tree sectree = DECL_SECTION_NAME (current_function_decl);
11686 secname = TREE_STRING_POINTER (sectree);
11688 else if (cfun && in_cold_section_p)
11689 secname = crtl->subsections.cold_section_label;
11690 else
11691 secname = text_section_label;
11693 return secname;
11696 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
11697 returned. If so, the decl for the COMMON block is returned, and the
11698 value is the offset into the common block for the symbol. */
11700 static tree
11701 fortran_common (tree decl, HOST_WIDE_INT *value)
11703 tree val_expr, cvar;
11704 enum machine_mode mode;
11705 HOST_WIDE_INT bitsize, bitpos;
11706 tree offset;
11707 int volatilep = 0, unsignedp = 0;
11709 /* If the decl isn't a VAR_DECL, or if it isn't public or static, or if
11710 it does not have a value (the offset into the common area), or if it
11711 is thread local (as opposed to global) then it isn't common, and shouldn't
11712 be handled as such. */
11713 if (TREE_CODE (decl) != VAR_DECL
11714 || !TREE_PUBLIC (decl)
11715 || !TREE_STATIC (decl)
11716 || !DECL_HAS_VALUE_EXPR_P (decl)
11717 || !is_fortran ())
11718 return NULL_TREE;
11720 val_expr = DECL_VALUE_EXPR (decl);
11721 if (TREE_CODE (val_expr) != COMPONENT_REF)
11722 return NULL_TREE;
11724 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
11725 &mode, &unsignedp, &volatilep, true);
11727 if (cvar == NULL_TREE
11728 || TREE_CODE (cvar) != VAR_DECL
11729 || DECL_ARTIFICIAL (cvar)
11730 || !TREE_PUBLIC (cvar))
11731 return NULL_TREE;
11733 *value = 0;
11734 if (offset != NULL)
11736 if (!host_integerp (offset, 0))
11737 return NULL_TREE;
11738 *value = tree_low_cst (offset, 0);
11740 if (bitpos != 0)
11741 *value += bitpos / BITS_PER_UNIT;
11743 return cvar;
11746 /* Dereference a location expression LOC if DECL is passed by invisible
11747 reference. */
11749 static dw_loc_descr_ref
11750 loc_by_reference (dw_loc_descr_ref loc, tree decl)
11752 HOST_WIDE_INT size;
11753 enum dwarf_location_atom op;
11755 if (loc == NULL)
11756 return NULL;
11758 if ((TREE_CODE (decl) != PARM_DECL && TREE_CODE (decl) != RESULT_DECL)
11759 || !DECL_BY_REFERENCE (decl))
11760 return loc;
11762 size = int_size_in_bytes (TREE_TYPE (decl));
11763 if (size > DWARF2_ADDR_SIZE || size == -1)
11764 return 0;
11765 else if (size == DWARF2_ADDR_SIZE)
11766 op = DW_OP_deref;
11767 else
11768 op = DW_OP_deref_size;
11769 add_loc_descr (&loc, new_loc_descr (op, size, 0));
11770 return loc;
11773 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
11774 data attribute for a variable or a parameter. We generate the
11775 DW_AT_const_value attribute only in those cases where the given variable
11776 or parameter does not have a true "location" either in memory or in a
11777 register. This can happen (for example) when a constant is passed as an
11778 actual argument in a call to an inline function. (It's possible that
11779 these things can crop up in other ways also.) Note that one type of
11780 constant value which can be passed into an inlined function is a constant
11781 pointer. This can happen for example if an actual argument in an inlined
11782 function call evaluates to a compile-time constant address. */
11784 static void
11785 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
11786 enum dwarf_attribute attr)
11788 rtx rtl;
11789 dw_loc_descr_ref descr;
11790 var_loc_list *loc_list;
11791 struct var_loc_node *node;
11792 if (TREE_CODE (decl) == ERROR_MARK)
11793 return;
11795 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
11796 || TREE_CODE (decl) == RESULT_DECL);
11798 /* See if we possibly have multiple locations for this variable. */
11799 loc_list = lookup_decl_loc (decl);
11801 /* If it truly has multiple locations, the first and last node will
11802 differ. */
11803 if (loc_list && loc_list->first != loc_list->last)
11805 const char *endname, *secname;
11806 dw_loc_list_ref list;
11807 rtx varloc;
11808 enum var_init_status initialized;
11810 /* Now that we know what section we are using for a base,
11811 actually construct the list of locations.
11812 The first location information is what is passed to the
11813 function that creates the location list, and the remaining
11814 locations just get added on to that list.
11815 Note that we only know the start address for a location
11816 (IE location changes), so to build the range, we use
11817 the range [current location start, next location start].
11818 This means we have to special case the last node, and generate
11819 a range of [last location start, end of function label]. */
11821 node = loc_list->first;
11822 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
11823 secname = secname_for_decl (decl);
11825 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note))
11826 initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11827 else
11828 initialized = VAR_INIT_STATUS_INITIALIZED;
11830 descr = loc_by_reference (loc_descriptor (varloc, initialized), decl);
11831 list = new_loc_list (descr, node->label, node->next->label, secname, 1);
11832 node = node->next;
11834 for (; node->next; node = node->next)
11835 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
11837 /* The variable has a location between NODE->LABEL and
11838 NODE->NEXT->LABEL. */
11839 enum var_init_status initialized =
11840 NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11841 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
11842 descr = loc_by_reference (loc_descriptor (varloc, initialized),
11843 decl);
11844 add_loc_descr_to_loc_list (&list, descr,
11845 node->label, node->next->label, secname);
11848 /* If the variable has a location at the last label
11849 it keeps its location until the end of function. */
11850 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
11852 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
11853 enum var_init_status initialized =
11854 NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11856 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
11857 if (!current_function_decl)
11858 endname = text_end_label;
11859 else
11861 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
11862 current_function_funcdef_no);
11863 endname = ggc_strdup (label_id);
11865 descr = loc_by_reference (loc_descriptor (varloc, initialized),
11866 decl);
11867 add_loc_descr_to_loc_list (&list, descr,
11868 node->label, endname, secname);
11871 /* Finally, add the location list to the DIE, and we are done. */
11872 add_AT_loc_list (die, attr, list);
11873 return;
11876 /* Try to get some constant RTL for this decl, and use that as the value of
11877 the location. */
11879 rtl = rtl_for_decl_location (decl);
11880 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
11882 add_const_value_attribute (die, rtl);
11883 return;
11886 /* If we have tried to generate the location otherwise, and it
11887 didn't work out (we wouldn't be here if we did), and we have a one entry
11888 location list, try generating a location from that. */
11889 if (loc_list && loc_list->first)
11891 enum var_init_status status;
11892 node = loc_list->first;
11893 status = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11894 descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note), status);
11895 if (descr)
11897 descr = loc_by_reference (descr, decl);
11898 add_AT_location_description (die, attr, descr);
11899 return;
11903 /* We couldn't get any rtl, so try directly generating the location
11904 description from the tree. */
11905 descr = loc_descriptor_from_tree (decl);
11906 if (descr)
11908 descr = loc_by_reference (descr, decl);
11909 add_AT_location_description (die, attr, descr);
11910 return;
11912 /* None of that worked, so it must not really have a location;
11913 try adding a constant value attribute from the DECL_INITIAL. */
11914 tree_add_const_value_attribute (die, decl);
11917 /* Add VARIABLE and DIE into deferred locations list. */
11919 static void
11920 defer_location (tree variable, dw_die_ref die)
11922 deferred_locations entry;
11923 entry.variable = variable;
11924 entry.die = die;
11925 VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
11928 /* Helper function for tree_add_const_value_attribute. Natively encode
11929 initializer INIT into an array. Return true if successful. */
11931 static bool
11932 native_encode_initializer (tree init, unsigned char *array, int size)
11934 tree type;
11936 if (init == NULL_TREE)
11937 return false;
11939 STRIP_NOPS (init);
11940 switch (TREE_CODE (init))
11942 case STRING_CST:
11943 type = TREE_TYPE (init);
11944 if (TREE_CODE (type) == ARRAY_TYPE)
11946 tree enttype = TREE_TYPE (type);
11947 enum machine_mode mode = TYPE_MODE (enttype);
11949 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
11950 return false;
11951 if (int_size_in_bytes (type) != size)
11952 return false;
11953 if (size > TREE_STRING_LENGTH (init))
11955 memcpy (array, TREE_STRING_POINTER (init),
11956 TREE_STRING_LENGTH (init));
11957 memset (array + TREE_STRING_LENGTH (init),
11958 '\0', size - TREE_STRING_LENGTH (init));
11960 else
11961 memcpy (array, TREE_STRING_POINTER (init), size);
11962 return true;
11964 return false;
11965 case CONSTRUCTOR:
11966 type = TREE_TYPE (init);
11967 if (int_size_in_bytes (type) != size)
11968 return false;
11969 if (TREE_CODE (type) == ARRAY_TYPE)
11971 HOST_WIDE_INT min_index;
11972 unsigned HOST_WIDE_INT cnt;
11973 int curpos = 0, fieldsize;
11974 constructor_elt *ce;
11976 if (TYPE_DOMAIN (type) == NULL_TREE
11977 || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
11978 return false;
11980 fieldsize = int_size_in_bytes (TREE_TYPE (type));
11981 if (fieldsize <= 0)
11982 return false;
11984 min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
11985 memset (array, '\0', size);
11986 for (cnt = 0;
11987 VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
11988 cnt++)
11990 tree val = ce->value;
11991 tree index = ce->index;
11992 int pos = curpos;
11993 if (index && TREE_CODE (index) == RANGE_EXPR)
11994 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
11995 * fieldsize;
11996 else if (index)
11997 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
11999 if (val)
12001 STRIP_NOPS (val);
12002 if (!native_encode_initializer (val, array + pos, fieldsize))
12003 return false;
12005 curpos = pos + fieldsize;
12006 if (index && TREE_CODE (index) == RANGE_EXPR)
12008 int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
12009 - tree_low_cst (TREE_OPERAND (index, 0), 0);
12010 while (count > 0)
12012 if (val)
12013 memcpy (array + curpos, array + pos, fieldsize);
12014 curpos += fieldsize;
12017 gcc_assert (curpos <= size);
12019 return true;
12021 else if (TREE_CODE (type) == RECORD_TYPE
12022 || TREE_CODE (type) == UNION_TYPE)
12024 tree field = NULL_TREE;
12025 unsigned HOST_WIDE_INT cnt;
12026 constructor_elt *ce;
12028 if (int_size_in_bytes (type) != size)
12029 return false;
12031 if (TREE_CODE (type) == RECORD_TYPE)
12032 field = TYPE_FIELDS (type);
12034 for (cnt = 0;
12035 VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
12036 cnt++, field = field ? TREE_CHAIN (field) : 0)
12038 tree val = ce->value;
12039 int pos, fieldsize;
12041 if (ce->index != 0)
12042 field = ce->index;
12044 if (val)
12045 STRIP_NOPS (val);
12047 if (field == NULL_TREE || DECL_BIT_FIELD (field))
12048 return false;
12050 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
12051 && TYPE_DOMAIN (TREE_TYPE (field))
12052 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
12053 return false;
12054 else if (DECL_SIZE_UNIT (field) == NULL_TREE
12055 || !host_integerp (DECL_SIZE_UNIT (field), 0))
12056 return false;
12057 fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
12058 pos = int_byte_position (field);
12059 gcc_assert (pos + fieldsize <= size);
12060 if (val
12061 && !native_encode_initializer (val, array + pos, fieldsize))
12062 return false;
12064 return true;
12066 return false;
12067 case VIEW_CONVERT_EXPR:
12068 case NON_LVALUE_EXPR:
12069 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
12070 default:
12071 return native_encode_expr (init, array, size) == size;
12075 /* If we don't have a copy of this variable in memory for some reason (such
12076 as a C++ member constant that doesn't have an out-of-line definition),
12077 we should tell the debugger about the constant value. */
12079 static void
12080 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
12082 tree init;
12083 tree type = TREE_TYPE (decl);
12084 rtx rtl;
12086 if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != CONST_DECL)
12087 return;
12089 init = DECL_INITIAL (decl);
12090 if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
12091 /* OK */;
12092 else
12093 return;
12095 rtl = rtl_for_decl_init (init, type);
12096 if (rtl)
12097 add_const_value_attribute (var_die, rtl);
12098 /* If the host and target are sane, try harder. */
12099 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
12100 && initializer_constant_valid_p (init, type))
12102 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
12103 if (size > 0 && (int) size == size)
12105 unsigned char *array = GGC_CNEWVEC (unsigned char, size);
12107 if (native_encode_initializer (init, array, size))
12108 add_AT_vec (var_die, DW_AT_const_value, size, 1, array);
12113 /* Convert the CFI instructions for the current function into a
12114 location list. This is used for DW_AT_frame_base when we targeting
12115 a dwarf2 consumer that does not support the dwarf3
12116 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
12117 expressions. */
12119 static dw_loc_list_ref
12120 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
12122 dw_fde_ref fde;
12123 dw_loc_list_ref list, *list_tail;
12124 dw_cfi_ref cfi;
12125 dw_cfa_location last_cfa, next_cfa;
12126 const char *start_label, *last_label, *section;
12128 fde = current_fde ();
12129 gcc_assert (fde != NULL);
12131 section = secname_for_decl (current_function_decl);
12132 list_tail = &list;
12133 list = NULL;
12135 next_cfa.reg = INVALID_REGNUM;
12136 next_cfa.offset = 0;
12137 next_cfa.indirect = 0;
12138 next_cfa.base_offset = 0;
12140 start_label = fde->dw_fde_begin;
12142 /* ??? Bald assumption that the CIE opcode list does not contain
12143 advance opcodes. */
12144 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
12145 lookup_cfa_1 (cfi, &next_cfa);
12147 last_cfa = next_cfa;
12148 last_label = start_label;
12150 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
12151 switch (cfi->dw_cfi_opc)
12153 case DW_CFA_set_loc:
12154 case DW_CFA_advance_loc1:
12155 case DW_CFA_advance_loc2:
12156 case DW_CFA_advance_loc4:
12157 if (!cfa_equal_p (&last_cfa, &next_cfa))
12159 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
12160 start_label, last_label, section,
12161 list == NULL);
12163 list_tail = &(*list_tail)->dw_loc_next;
12164 last_cfa = next_cfa;
12165 start_label = last_label;
12167 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
12168 break;
12170 case DW_CFA_advance_loc:
12171 /* The encoding is complex enough that we should never emit this. */
12172 case DW_CFA_remember_state:
12173 case DW_CFA_restore_state:
12174 /* We don't handle these two in this function. It would be possible
12175 if it were to be required. */
12176 gcc_unreachable ();
12178 default:
12179 lookup_cfa_1 (cfi, &next_cfa);
12180 break;
12183 if (!cfa_equal_p (&last_cfa, &next_cfa))
12185 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
12186 start_label, last_label, section,
12187 list == NULL);
12188 list_tail = &(*list_tail)->dw_loc_next;
12189 start_label = last_label;
12191 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
12192 start_label, fde->dw_fde_end, section,
12193 list == NULL);
12195 return list;
12198 /* Compute a displacement from the "steady-state frame pointer" to the
12199 frame base (often the same as the CFA), and store it in
12200 frame_pointer_fb_offset. OFFSET is added to the displacement
12201 before the latter is negated. */
12203 static void
12204 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
12206 rtx reg, elim;
12208 #ifdef FRAME_POINTER_CFA_OFFSET
12209 reg = frame_pointer_rtx;
12210 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
12211 #else
12212 reg = arg_pointer_rtx;
12213 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
12214 #endif
12216 elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
12217 if (GET_CODE (elim) == PLUS)
12219 offset += INTVAL (XEXP (elim, 1));
12220 elim = XEXP (elim, 0);
12223 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
12224 && (elim == hard_frame_pointer_rtx
12225 || elim == stack_pointer_rtx))
12226 || elim == (frame_pointer_needed
12227 ? hard_frame_pointer_rtx
12228 : stack_pointer_rtx));
12230 frame_pointer_fb_offset = -offset;
12233 /* Generate a DW_AT_name attribute given some string value to be included as
12234 the value of the attribute. */
12236 static void
12237 add_name_attribute (dw_die_ref die, const char *name_string)
12239 if (name_string != NULL && *name_string != 0)
12241 if (demangle_name_func)
12242 name_string = (*demangle_name_func) (name_string);
12244 add_AT_string (die, DW_AT_name, name_string);
12248 /* Generate a DW_AT_comp_dir attribute for DIE. */
12250 static void
12251 add_comp_dir_attribute (dw_die_ref die)
12253 const char *wd = get_src_pwd ();
12254 if (wd != NULL)
12255 add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
12258 /* Given a tree node describing an array bound (either lower or upper) output
12259 a representation for that bound. */
12261 static void
12262 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
12264 switch (TREE_CODE (bound))
12266 case ERROR_MARK:
12267 return;
12269 /* All fixed-bounds are represented by INTEGER_CST nodes. */
12270 case INTEGER_CST:
12271 if (! host_integerp (bound, 0)
12272 || (bound_attr == DW_AT_lower_bound
12273 && (((is_c_family () || is_java ()) && integer_zerop (bound))
12274 || (is_fortran () && integer_onep (bound)))))
12275 /* Use the default. */
12277 else
12278 add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
12279 break;
12281 CASE_CONVERT:
12282 case VIEW_CONVERT_EXPR:
12283 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
12284 break;
12286 case SAVE_EXPR:
12287 break;
12289 case VAR_DECL:
12290 case PARM_DECL:
12291 case RESULT_DECL:
12293 dw_die_ref decl_die = lookup_decl_die (bound);
12294 dw_loc_descr_ref loc;
12296 /* ??? Can this happen, or should the variable have been bound
12297 first? Probably it can, since I imagine that we try to create
12298 the types of parameters in the order in which they exist in
12299 the list, and won't have created a forward reference to a
12300 later parameter. */
12301 if (decl_die != NULL)
12302 add_AT_die_ref (subrange_die, bound_attr, decl_die);
12303 else
12305 loc = loc_descriptor_from_tree_1 (bound, 0);
12306 add_AT_location_description (subrange_die, bound_attr, loc);
12308 break;
12311 default:
12313 /* Otherwise try to create a stack operation procedure to
12314 evaluate the value of the array bound. */
12316 dw_die_ref ctx, decl_die;
12317 dw_loc_descr_ref loc;
12319 loc = loc_descriptor_from_tree (bound);
12320 if (loc == NULL)
12321 break;
12323 if (current_function_decl == 0)
12324 ctx = comp_unit_die;
12325 else
12326 ctx = lookup_decl_die (current_function_decl);
12328 decl_die = new_die (DW_TAG_variable, ctx, bound);
12329 add_AT_flag (decl_die, DW_AT_artificial, 1);
12330 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
12331 add_AT_loc (decl_die, DW_AT_location, loc);
12333 add_AT_die_ref (subrange_die, bound_attr, decl_die);
12334 break;
12339 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
12340 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
12341 Note that the block of subscript information for an array type also
12342 includes information about the element type of the given array type. */
12344 static void
12345 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
12347 unsigned dimension_number;
12348 tree lower, upper;
12349 dw_die_ref subrange_die;
12351 for (dimension_number = 0;
12352 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
12353 type = TREE_TYPE (type), dimension_number++)
12355 tree domain = TYPE_DOMAIN (type);
12357 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
12358 break;
12360 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
12361 and (in GNU C only) variable bounds. Handle all three forms
12362 here. */
12363 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
12364 if (domain)
12366 /* We have an array type with specified bounds. */
12367 lower = TYPE_MIN_VALUE (domain);
12368 upper = TYPE_MAX_VALUE (domain);
12370 /* Define the index type. */
12371 if (TREE_TYPE (domain))
12373 /* ??? This is probably an Ada unnamed subrange type. Ignore the
12374 TREE_TYPE field. We can't emit debug info for this
12375 because it is an unnamed integral type. */
12376 if (TREE_CODE (domain) == INTEGER_TYPE
12377 && TYPE_NAME (domain) == NULL_TREE
12378 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
12379 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
12381 else
12382 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
12383 type_die);
12386 /* ??? If upper is NULL, the array has unspecified length,
12387 but it does have a lower bound. This happens with Fortran
12388 dimension arr(N:*)
12389 Since the debugger is definitely going to need to know N
12390 to produce useful results, go ahead and output the lower
12391 bound solo, and hope the debugger can cope. */
12393 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
12394 if (upper)
12395 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
12398 /* Otherwise we have an array type with an unspecified length. The
12399 DWARF-2 spec does not say how to handle this; let's just leave out the
12400 bounds. */
12404 static void
12405 add_byte_size_attribute (dw_die_ref die, tree tree_node)
12407 unsigned size;
12409 switch (TREE_CODE (tree_node))
12411 case ERROR_MARK:
12412 size = 0;
12413 break;
12414 case ENUMERAL_TYPE:
12415 case RECORD_TYPE:
12416 case UNION_TYPE:
12417 case QUAL_UNION_TYPE:
12418 size = int_size_in_bytes (tree_node);
12419 break;
12420 case FIELD_DECL:
12421 /* For a data member of a struct or union, the DW_AT_byte_size is
12422 generally given as the number of bytes normally allocated for an
12423 object of the *declared* type of the member itself. This is true
12424 even for bit-fields. */
12425 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
12426 break;
12427 default:
12428 gcc_unreachable ();
12431 /* Note that `size' might be -1 when we get to this point. If it is, that
12432 indicates that the byte size of the entity in question is variable. We
12433 have no good way of expressing this fact in Dwarf at the present time,
12434 so just let the -1 pass on through. */
12435 add_AT_unsigned (die, DW_AT_byte_size, size);
12438 /* For a FIELD_DECL node which represents a bit-field, output an attribute
12439 which specifies the distance in bits from the highest order bit of the
12440 "containing object" for the bit-field to the highest order bit of the
12441 bit-field itself.
12443 For any given bit-field, the "containing object" is a hypothetical object
12444 (of some integral or enum type) within which the given bit-field lives. The
12445 type of this hypothetical "containing object" is always the same as the
12446 declared type of the individual bit-field itself. The determination of the
12447 exact location of the "containing object" for a bit-field is rather
12448 complicated. It's handled by the `field_byte_offset' function (above).
12450 Note that it is the size (in bytes) of the hypothetical "containing object"
12451 which will be given in the DW_AT_byte_size attribute for this bit-field.
12452 (See `byte_size_attribute' above). */
12454 static inline void
12455 add_bit_offset_attribute (dw_die_ref die, tree decl)
12457 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
12458 tree type = DECL_BIT_FIELD_TYPE (decl);
12459 HOST_WIDE_INT bitpos_int;
12460 HOST_WIDE_INT highest_order_object_bit_offset;
12461 HOST_WIDE_INT highest_order_field_bit_offset;
12462 HOST_WIDE_INT unsigned bit_offset;
12464 /* Must be a field and a bit field. */
12465 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
12467 /* We can't yet handle bit-fields whose offsets are variable, so if we
12468 encounter such things, just return without generating any attribute
12469 whatsoever. Likewise for variable or too large size. */
12470 if (! host_integerp (bit_position (decl), 0)
12471 || ! host_integerp (DECL_SIZE (decl), 1))
12472 return;
12474 bitpos_int = int_bit_position (decl);
12476 /* Note that the bit offset is always the distance (in bits) from the
12477 highest-order bit of the "containing object" to the highest-order bit of
12478 the bit-field itself. Since the "high-order end" of any object or field
12479 is different on big-endian and little-endian machines, the computation
12480 below must take account of these differences. */
12481 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
12482 highest_order_field_bit_offset = bitpos_int;
12484 if (! BYTES_BIG_ENDIAN)
12486 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
12487 highest_order_object_bit_offset += simple_type_size_in_bits (type);
12490 bit_offset
12491 = (! BYTES_BIG_ENDIAN
12492 ? highest_order_object_bit_offset - highest_order_field_bit_offset
12493 : highest_order_field_bit_offset - highest_order_object_bit_offset);
12495 add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
12498 /* For a FIELD_DECL node which represents a bit field, output an attribute
12499 which specifies the length in bits of the given field. */
12501 static inline void
12502 add_bit_size_attribute (dw_die_ref die, tree decl)
12504 /* Must be a field and a bit field. */
12505 gcc_assert (TREE_CODE (decl) == FIELD_DECL
12506 && DECL_BIT_FIELD_TYPE (decl));
12508 if (host_integerp (DECL_SIZE (decl), 1))
12509 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
12512 /* If the compiled language is ANSI C, then add a 'prototyped'
12513 attribute, if arg types are given for the parameters of a function. */
12515 static inline void
12516 add_prototyped_attribute (dw_die_ref die, tree func_type)
12518 if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
12519 && TYPE_ARG_TYPES (func_type) != NULL)
12520 add_AT_flag (die, DW_AT_prototyped, 1);
12523 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
12524 by looking in either the type declaration or object declaration
12525 equate table. */
12527 static inline dw_die_ref
12528 add_abstract_origin_attribute (dw_die_ref die, tree origin)
12530 dw_die_ref origin_die = NULL;
12532 if (TREE_CODE (origin) != FUNCTION_DECL)
12534 /* We may have gotten separated from the block for the inlined
12535 function, if we're in an exception handler or some such; make
12536 sure that the abstract function has been written out.
12538 Doing this for nested functions is wrong, however; functions are
12539 distinct units, and our context might not even be inline. */
12540 tree fn = origin;
12542 if (TYPE_P (fn))
12543 fn = TYPE_STUB_DECL (fn);
12545 fn = decl_function_context (fn);
12546 if (fn)
12547 dwarf2out_abstract_function (fn);
12550 if (DECL_P (origin))
12551 origin_die = lookup_decl_die (origin);
12552 else if (TYPE_P (origin))
12553 origin_die = lookup_type_die (origin);
12555 /* XXX: Functions that are never lowered don't always have correct block
12556 trees (in the case of java, they simply have no block tree, in some other
12557 languages). For these functions, there is nothing we can really do to
12558 output correct debug info for inlined functions in all cases. Rather
12559 than die, we'll just produce deficient debug info now, in that we will
12560 have variables without a proper abstract origin. In the future, when all
12561 functions are lowered, we should re-add a gcc_assert (origin_die)
12562 here. */
12564 if (origin_die)
12565 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
12566 return origin_die;
12569 /* We do not currently support the pure_virtual attribute. */
12571 static inline void
12572 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
12574 if (DECL_VINDEX (func_decl))
12576 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
12578 if (host_integerp (DECL_VINDEX (func_decl), 0))
12579 add_AT_loc (die, DW_AT_vtable_elem_location,
12580 new_loc_descr (DW_OP_constu,
12581 tree_low_cst (DECL_VINDEX (func_decl), 0),
12582 0));
12584 /* GNU extension: Record what type this method came from originally. */
12585 if (debug_info_level > DINFO_LEVEL_TERSE)
12586 add_AT_die_ref (die, DW_AT_containing_type,
12587 lookup_type_die (DECL_CONTEXT (func_decl)));
12591 /* Add source coordinate attributes for the given decl. */
12593 static void
12594 add_src_coords_attributes (dw_die_ref die, tree decl)
12596 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12598 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
12599 add_AT_unsigned (die, DW_AT_decl_line, s.line);
12602 /* Add a DW_AT_name attribute and source coordinate attribute for the
12603 given decl, but only if it actually has a name. */
12605 static void
12606 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
12608 tree decl_name;
12610 decl_name = DECL_NAME (decl);
12611 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
12613 add_name_attribute (die, dwarf2_name (decl, 0));
12614 if (! DECL_ARTIFICIAL (decl))
12615 add_src_coords_attributes (die, decl);
12617 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
12618 && TREE_PUBLIC (decl)
12619 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
12620 && !DECL_ABSTRACT (decl)
12621 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
12622 && !is_fortran ())
12623 add_AT_string (die, DW_AT_MIPS_linkage_name,
12624 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
12627 #ifdef VMS_DEBUGGING_INFO
12628 /* Get the function's name, as described by its RTL. This may be different
12629 from the DECL_NAME name used in the source file. */
12630 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
12632 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
12633 XEXP (DECL_RTL (decl), 0));
12634 VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
12636 #endif
12639 /* Push a new declaration scope. */
12641 static void
12642 push_decl_scope (tree scope)
12644 VEC_safe_push (tree, gc, decl_scope_table, scope);
12647 /* Pop a declaration scope. */
12649 static inline void
12650 pop_decl_scope (void)
12652 VEC_pop (tree, decl_scope_table);
12655 /* Return the DIE for the scope that immediately contains this type.
12656 Non-named types get global scope. Named types nested in other
12657 types get their containing scope if it's open, or global scope
12658 otherwise. All other types (i.e. function-local named types) get
12659 the current active scope. */
12661 static dw_die_ref
12662 scope_die_for (tree t, dw_die_ref context_die)
12664 dw_die_ref scope_die = NULL;
12665 tree containing_scope;
12666 int i;
12668 /* Non-types always go in the current scope. */
12669 gcc_assert (TYPE_P (t));
12671 containing_scope = TYPE_CONTEXT (t);
12673 /* Use the containing namespace if it was passed in (for a declaration). */
12674 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
12676 if (context_die == lookup_decl_die (containing_scope))
12677 /* OK */;
12678 else
12679 containing_scope = NULL_TREE;
12682 /* Ignore function type "scopes" from the C frontend. They mean that
12683 a tagged type is local to a parmlist of a function declarator, but
12684 that isn't useful to DWARF. */
12685 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
12686 containing_scope = NULL_TREE;
12688 if (containing_scope == NULL_TREE)
12689 scope_die = comp_unit_die;
12690 else if (TYPE_P (containing_scope))
12692 /* For types, we can just look up the appropriate DIE. But
12693 first we check to see if we're in the middle of emitting it
12694 so we know where the new DIE should go. */
12695 for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
12696 if (VEC_index (tree, decl_scope_table, i) == containing_scope)
12697 break;
12699 if (i < 0)
12701 gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
12702 || TREE_ASM_WRITTEN (containing_scope));
12704 /* If none of the current dies are suitable, we get file scope. */
12705 scope_die = comp_unit_die;
12707 else
12708 scope_die = lookup_type_die (containing_scope);
12710 else
12711 scope_die = context_die;
12713 return scope_die;
12716 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
12718 static inline int
12719 local_scope_p (dw_die_ref context_die)
12721 for (; context_die; context_die = context_die->die_parent)
12722 if (context_die->die_tag == DW_TAG_inlined_subroutine
12723 || context_die->die_tag == DW_TAG_subprogram)
12724 return 1;
12726 return 0;
12729 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
12730 whether or not to treat a DIE in this context as a declaration. */
12732 static inline int
12733 class_or_namespace_scope_p (dw_die_ref context_die)
12735 return (context_die
12736 && (context_die->die_tag == DW_TAG_structure_type
12737 || context_die->die_tag == DW_TAG_class_type
12738 || context_die->die_tag == DW_TAG_interface_type
12739 || context_die->die_tag == DW_TAG_union_type
12740 || context_die->die_tag == DW_TAG_namespace));
12743 /* Many forms of DIEs require a "type description" attribute. This
12744 routine locates the proper "type descriptor" die for the type given
12745 by 'type', and adds a DW_AT_type attribute below the given die. */
12747 static void
12748 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
12749 int decl_volatile, dw_die_ref context_die)
12751 enum tree_code code = TREE_CODE (type);
12752 dw_die_ref type_die = NULL;
12754 /* ??? If this type is an unnamed subrange type of an integral, floating-point
12755 or fixed-point type, use the inner type. This is because we have no
12756 support for unnamed types in base_type_die. This can happen if this is
12757 an Ada subrange type. Correct solution is emit a subrange type die. */
12758 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
12759 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
12760 type = TREE_TYPE (type), code = TREE_CODE (type);
12762 if (code == ERROR_MARK
12763 /* Handle a special case. For functions whose return type is void, we
12764 generate *no* type attribute. (Note that no object may have type
12765 `void', so this only applies to function return types). */
12766 || code == VOID_TYPE)
12767 return;
12769 type_die = modified_type_die (type,
12770 decl_const || TYPE_READONLY (type),
12771 decl_volatile || TYPE_VOLATILE (type),
12772 context_die);
12774 if (type_die != NULL)
12775 add_AT_die_ref (object_die, DW_AT_type, type_die);
12778 /* Given an object die, add the calling convention attribute for the
12779 function call type. */
12780 static void
12781 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
12783 enum dwarf_calling_convention value = DW_CC_normal;
12785 value = targetm.dwarf_calling_convention (TREE_TYPE (decl));
12787 /* DWARF doesn't provide a way to identify a program's source-level
12788 entry point. DW_AT_calling_convention attributes are only meant
12789 to describe functions' calling conventions. However, lacking a
12790 better way to signal the Fortran main program, we use this for the
12791 time being, following existing custom. */
12792 if (is_fortran ()
12793 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
12794 value = DW_CC_program;
12796 /* Only add the attribute if the backend requests it, and
12797 is not DW_CC_normal. */
12798 if (value && (value != DW_CC_normal))
12799 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
12802 /* Given a tree pointer to a struct, class, union, or enum type node, return
12803 a pointer to the (string) tag name for the given type, or zero if the type
12804 was declared without a tag. */
12806 static const char *
12807 type_tag (const_tree type)
12809 const char *name = 0;
12811 if (TYPE_NAME (type) != 0)
12813 tree t = 0;
12815 /* Find the IDENTIFIER_NODE for the type name. */
12816 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
12817 t = TYPE_NAME (type);
12819 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
12820 a TYPE_DECL node, regardless of whether or not a `typedef' was
12821 involved. */
12822 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12823 && ! DECL_IGNORED_P (TYPE_NAME (type)))
12825 /* We want to be extra verbose. Don't call dwarf_name if
12826 DECL_NAME isn't set. The default hook for decl_printable_name
12827 doesn't like that, and in this context it's correct to return
12828 0, instead of "<anonymous>" or the like. */
12829 if (DECL_NAME (TYPE_NAME (type)))
12830 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
12833 /* Now get the name as a string, or invent one. */
12834 if (!name && t != 0)
12835 name = IDENTIFIER_POINTER (t);
12838 return (name == 0 || *name == '\0') ? 0 : name;
12841 /* Return the type associated with a data member, make a special check
12842 for bit field types. */
12844 static inline tree
12845 member_declared_type (const_tree member)
12847 return (DECL_BIT_FIELD_TYPE (member)
12848 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
12851 /* Get the decl's label, as described by its RTL. This may be different
12852 from the DECL_NAME name used in the source file. */
12854 #if 0
12855 static const char *
12856 decl_start_label (tree decl)
12858 rtx x;
12859 const char *fnname;
12861 x = DECL_RTL (decl);
12862 gcc_assert (MEM_P (x));
12864 x = XEXP (x, 0);
12865 gcc_assert (GET_CODE (x) == SYMBOL_REF);
12867 fnname = XSTR (x, 0);
12868 return fnname;
12870 #endif
12872 /* These routines generate the internal representation of the DIE's for
12873 the compilation unit. Debugging information is collected by walking
12874 the declaration trees passed in from dwarf2out_decl(). */
12876 static void
12877 gen_array_type_die (tree type, dw_die_ref context_die)
12879 dw_die_ref scope_die = scope_die_for (type, context_die);
12880 dw_die_ref array_die;
12882 /* GNU compilers represent multidimensional array types as sequences of one
12883 dimensional array types whose element types are themselves array types.
12884 We sometimes squish that down to a single array_type DIE with multiple
12885 subscripts in the Dwarf debugging info. The draft Dwarf specification
12886 say that we are allowed to do this kind of compression in C, because
12887 there is no difference between an array of arrays and a multidimensional
12888 array. We don't do this for Ada to remain as close as possible to the
12889 actual representation, which is especially important against the language
12890 flexibilty wrt arrays of variable size. */
12892 bool collapse_nested_arrays = !is_ada ();
12893 tree element_type;
12895 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
12896 DW_TAG_string_type doesn't have DW_AT_type attribute). */
12897 if (TYPE_STRING_FLAG (type)
12898 && TREE_CODE (type) == ARRAY_TYPE
12899 && is_fortran ()
12900 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
12902 HOST_WIDE_INT size;
12904 array_die = new_die (DW_TAG_string_type, scope_die, type);
12905 add_name_attribute (array_die, type_tag (type));
12906 equate_type_number_to_die (type, array_die);
12907 size = int_size_in_bytes (type);
12908 if (size >= 0)
12909 add_AT_unsigned (array_die, DW_AT_byte_size, size);
12910 else if (TYPE_DOMAIN (type) != NULL_TREE
12911 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
12912 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
12914 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
12915 dw_loc_descr_ref loc = loc_descriptor_from_tree (szdecl);
12917 size = int_size_in_bytes (TREE_TYPE (szdecl));
12918 if (loc && size > 0)
12920 add_AT_loc (array_die, DW_AT_string_length, loc);
12921 if (size != DWARF2_ADDR_SIZE)
12922 add_AT_unsigned (array_die, DW_AT_byte_size, size);
12925 return;
12928 /* ??? The SGI dwarf reader fails for array of array of enum types
12929 (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
12930 array type comes before the outer array type. We thus call gen_type_die
12931 before we new_die and must prevent nested array types collapsing for this
12932 target. */
12934 #ifdef MIPS_DEBUGGING_INFO
12935 gen_type_die (TREE_TYPE (type), context_die);
12936 collapse_nested_arrays = false;
12937 #endif
12939 array_die = new_die (DW_TAG_array_type, scope_die, type);
12940 add_name_attribute (array_die, type_tag (type));
12941 equate_type_number_to_die (type, array_die);
12943 if (TREE_CODE (type) == VECTOR_TYPE)
12945 /* The frontend feeds us a representation for the vector as a struct
12946 containing an array. Pull out the array type. */
12947 type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
12948 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
12951 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
12952 if (is_fortran ()
12953 && TREE_CODE (type) == ARRAY_TYPE
12954 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
12955 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
12956 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
12958 #if 0
12959 /* We default the array ordering. SDB will probably do
12960 the right things even if DW_AT_ordering is not present. It's not even
12961 an issue until we start to get into multidimensional arrays anyway. If
12962 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
12963 then we'll have to put the DW_AT_ordering attribute back in. (But if
12964 and when we find out that we need to put these in, we will only do so
12965 for multidimensional arrays. */
12966 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
12967 #endif
12969 #ifdef MIPS_DEBUGGING_INFO
12970 /* The SGI compilers handle arrays of unknown bound by setting
12971 AT_declaration and not emitting any subrange DIEs. */
12972 if (! TYPE_DOMAIN (type))
12973 add_AT_flag (array_die, DW_AT_declaration, 1);
12974 else
12975 #endif
12976 add_subscript_info (array_die, type, collapse_nested_arrays);
12978 /* Add representation of the type of the elements of this array type and
12979 emit the corresponding DIE if we haven't done it already. */
12980 element_type = TREE_TYPE (type);
12981 if (collapse_nested_arrays)
12982 while (TREE_CODE (element_type) == ARRAY_TYPE)
12984 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
12985 break;
12986 element_type = TREE_TYPE (element_type);
12989 #ifndef MIPS_DEBUGGING_INFO
12990 gen_type_die (element_type, context_die);
12991 #endif
12993 add_type_attribute (array_die, element_type, 0, 0, context_die);
12995 if (get_AT (array_die, DW_AT_name))
12996 add_pubtype (type, array_die);
12999 static dw_loc_descr_ref
13000 descr_info_loc (tree val, tree base_decl)
13002 HOST_WIDE_INT size;
13003 dw_loc_descr_ref loc, loc2;
13004 enum dwarf_location_atom op;
13006 if (val == base_decl)
13007 return new_loc_descr (DW_OP_push_object_address, 0, 0);
13009 switch (TREE_CODE (val))
13011 CASE_CONVERT:
13012 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13013 case VAR_DECL:
13014 return loc_descriptor_from_tree_1 (val, 0);
13015 case INTEGER_CST:
13016 if (host_integerp (val, 0))
13017 return int_loc_descriptor (tree_low_cst (val, 0));
13018 break;
13019 case INDIRECT_REF:
13020 size = int_size_in_bytes (TREE_TYPE (val));
13021 if (size < 0)
13022 break;
13023 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13024 if (!loc)
13025 break;
13026 if (size == DWARF2_ADDR_SIZE)
13027 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
13028 else
13029 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
13030 return loc;
13031 case POINTER_PLUS_EXPR:
13032 case PLUS_EXPR:
13033 if (host_integerp (TREE_OPERAND (val, 1), 1)
13034 && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
13035 < 16384)
13037 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13038 if (!loc)
13039 break;
13040 add_loc_descr (&loc,
13041 new_loc_descr (DW_OP_plus_uconst,
13042 tree_low_cst (TREE_OPERAND (val, 1),
13043 1), 0));
13045 else
13047 op = DW_OP_plus;
13048 do_binop:
13049 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13050 if (!loc)
13051 break;
13052 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
13053 if (!loc2)
13054 break;
13055 add_loc_descr (&loc, loc2);
13056 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
13058 return loc;
13059 case MINUS_EXPR:
13060 op = DW_OP_minus;
13061 goto do_binop;
13062 case MULT_EXPR:
13063 op = DW_OP_mul;
13064 goto do_binop;
13065 case EQ_EXPR:
13066 op = DW_OP_eq;
13067 goto do_binop;
13068 case NE_EXPR:
13069 op = DW_OP_ne;
13070 goto do_binop;
13071 default:
13072 break;
13074 return NULL;
13077 static void
13078 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
13079 tree val, tree base_decl)
13081 dw_loc_descr_ref loc;
13083 if (host_integerp (val, 0))
13085 add_AT_unsigned (die, attr, tree_low_cst (val, 0));
13086 return;
13089 loc = descr_info_loc (val, base_decl);
13090 if (!loc)
13091 return;
13093 add_AT_loc (die, attr, loc);
13096 /* This routine generates DIE for array with hidden descriptor, details
13097 are filled into *info by a langhook. */
13099 static void
13100 gen_descr_array_type_die (tree type, struct array_descr_info *info,
13101 dw_die_ref context_die)
13103 dw_die_ref scope_die = scope_die_for (type, context_die);
13104 dw_die_ref array_die;
13105 int dim;
13107 array_die = new_die (DW_TAG_array_type, scope_die, type);
13108 add_name_attribute (array_die, type_tag (type));
13109 equate_type_number_to_die (type, array_die);
13111 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
13112 if (is_fortran ()
13113 && info->ndimensions >= 2)
13114 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
13116 if (info->data_location)
13117 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
13118 info->base_decl);
13119 if (info->associated)
13120 add_descr_info_field (array_die, DW_AT_associated, info->associated,
13121 info->base_decl);
13122 if (info->allocated)
13123 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
13124 info->base_decl);
13126 for (dim = 0; dim < info->ndimensions; dim++)
13128 dw_die_ref subrange_die
13129 = new_die (DW_TAG_subrange_type, array_die, NULL);
13131 if (info->dimen[dim].lower_bound)
13133 /* If it is the default value, omit it. */
13134 if ((is_c_family () || is_java ())
13135 && integer_zerop (info->dimen[dim].lower_bound))
13137 else if (is_fortran ()
13138 && integer_onep (info->dimen[dim].lower_bound))
13140 else
13141 add_descr_info_field (subrange_die, DW_AT_lower_bound,
13142 info->dimen[dim].lower_bound,
13143 info->base_decl);
13145 if (info->dimen[dim].upper_bound)
13146 add_descr_info_field (subrange_die, DW_AT_upper_bound,
13147 info->dimen[dim].upper_bound,
13148 info->base_decl);
13149 if (info->dimen[dim].stride)
13150 add_descr_info_field (subrange_die, DW_AT_byte_stride,
13151 info->dimen[dim].stride,
13152 info->base_decl);
13155 gen_type_die (info->element_type, context_die);
13156 add_type_attribute (array_die, info->element_type, 0, 0, context_die);
13158 if (get_AT (array_die, DW_AT_name))
13159 add_pubtype (type, array_die);
13162 #if 0
13163 static void
13164 gen_entry_point_die (tree decl, dw_die_ref context_die)
13166 tree origin = decl_ultimate_origin (decl);
13167 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
13169 if (origin != NULL)
13170 add_abstract_origin_attribute (decl_die, origin);
13171 else
13173 add_name_and_src_coords_attributes (decl_die, decl);
13174 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
13175 0, 0, context_die);
13178 if (DECL_ABSTRACT (decl))
13179 equate_decl_number_to_die (decl, decl_die);
13180 else
13181 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
13183 #endif
13185 /* Walk through the list of incomplete types again, trying once more to
13186 emit full debugging info for them. */
13188 static void
13189 retry_incomplete_types (void)
13191 int i;
13193 for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
13194 gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
13197 /* Determine what tag to use for a record type. */
13199 static enum dwarf_tag
13200 record_type_tag (tree type)
13202 if (! lang_hooks.types.classify_record)
13203 return DW_TAG_structure_type;
13205 switch (lang_hooks.types.classify_record (type))
13207 case RECORD_IS_STRUCT:
13208 return DW_TAG_structure_type;
13210 case RECORD_IS_CLASS:
13211 return DW_TAG_class_type;
13213 case RECORD_IS_INTERFACE:
13214 return DW_TAG_interface_type;
13216 default:
13217 gcc_unreachable ();
13221 /* Generate a DIE to represent an enumeration type. Note that these DIEs
13222 include all of the information about the enumeration values also. Each
13223 enumerated type name/value is listed as a child of the enumerated type
13224 DIE. */
13226 static dw_die_ref
13227 gen_enumeration_type_die (tree type, dw_die_ref context_die)
13229 dw_die_ref type_die = lookup_type_die (type);
13231 if (type_die == NULL)
13233 type_die = new_die (DW_TAG_enumeration_type,
13234 scope_die_for (type, context_die), type);
13235 equate_type_number_to_die (type, type_die);
13236 add_name_attribute (type_die, type_tag (type));
13238 else if (! TYPE_SIZE (type))
13239 return type_die;
13240 else
13241 remove_AT (type_die, DW_AT_declaration);
13243 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
13244 given enum type is incomplete, do not generate the DW_AT_byte_size
13245 attribute or the DW_AT_element_list attribute. */
13246 if (TYPE_SIZE (type))
13248 tree link;
13250 TREE_ASM_WRITTEN (type) = 1;
13251 add_byte_size_attribute (type_die, type);
13252 if (TYPE_STUB_DECL (type) != NULL_TREE)
13253 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
13255 /* If the first reference to this type was as the return type of an
13256 inline function, then it may not have a parent. Fix this now. */
13257 if (type_die->die_parent == NULL)
13258 add_child_die (scope_die_for (type, context_die), type_die);
13260 for (link = TYPE_VALUES (type);
13261 link != NULL; link = TREE_CHAIN (link))
13263 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
13264 tree value = TREE_VALUE (link);
13266 add_name_attribute (enum_die,
13267 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
13269 if (TREE_CODE (value) == CONST_DECL)
13270 value = DECL_INITIAL (value);
13272 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
13273 /* DWARF2 does not provide a way of indicating whether or
13274 not enumeration constants are signed or unsigned. GDB
13275 always assumes the values are signed, so we output all
13276 values as if they were signed. That means that
13277 enumeration constants with very large unsigned values
13278 will appear to have negative values in the debugger. */
13279 add_AT_int (enum_die, DW_AT_const_value,
13280 tree_low_cst (value, tree_int_cst_sgn (value) > 0));
13283 else
13284 add_AT_flag (type_die, DW_AT_declaration, 1);
13286 if (get_AT (type_die, DW_AT_name))
13287 add_pubtype (type, type_die);
13289 return type_die;
13292 /* Generate a DIE to represent either a real live formal parameter decl or to
13293 represent just the type of some formal parameter position in some function
13294 type.
13296 Note that this routine is a bit unusual because its argument may be a
13297 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
13298 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
13299 node. If it's the former then this function is being called to output a
13300 DIE to represent a formal parameter object (or some inlining thereof). If
13301 it's the latter, then this function is only being called to output a
13302 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
13303 argument type of some subprogram type. */
13305 static dw_die_ref
13306 gen_formal_parameter_die (tree node, tree origin, dw_die_ref context_die)
13308 tree node_or_origin = node ? node : origin;
13309 dw_die_ref parm_die
13310 = new_die (DW_TAG_formal_parameter, context_die, node);
13312 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
13314 case tcc_declaration:
13315 if (!origin)
13316 origin = decl_ultimate_origin (node);
13317 if (origin != NULL)
13318 add_abstract_origin_attribute (parm_die, origin);
13319 else
13321 tree type = TREE_TYPE (node);
13322 add_name_and_src_coords_attributes (parm_die, node);
13323 if (DECL_BY_REFERENCE (node))
13324 add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
13325 context_die);
13326 else
13327 add_type_attribute (parm_die, type,
13328 TREE_READONLY (node),
13329 TREE_THIS_VOLATILE (node),
13330 context_die);
13331 if (DECL_ARTIFICIAL (node))
13332 add_AT_flag (parm_die, DW_AT_artificial, 1);
13335 if (node)
13336 equate_decl_number_to_die (node, parm_die);
13337 if (! DECL_ABSTRACT (node_or_origin))
13338 add_location_or_const_value_attribute (parm_die, node_or_origin,
13339 DW_AT_location);
13341 break;
13343 case tcc_type:
13344 /* We were called with some kind of a ..._TYPE node. */
13345 add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
13346 break;
13348 default:
13349 gcc_unreachable ();
13352 return parm_die;
13355 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
13356 at the end of an (ANSI prototyped) formal parameters list. */
13358 static void
13359 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
13361 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
13364 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
13365 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
13366 parameters as specified in some function type specification (except for
13367 those which appear as part of a function *definition*). */
13369 static void
13370 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
13372 tree link;
13373 tree formal_type = NULL;
13374 tree first_parm_type;
13375 tree arg;
13377 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
13379 arg = DECL_ARGUMENTS (function_or_method_type);
13380 function_or_method_type = TREE_TYPE (function_or_method_type);
13382 else
13383 arg = NULL_TREE;
13385 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
13387 /* Make our first pass over the list of formal parameter types and output a
13388 DW_TAG_formal_parameter DIE for each one. */
13389 for (link = first_parm_type; link; )
13391 dw_die_ref parm_die;
13393 formal_type = TREE_VALUE (link);
13394 if (formal_type == void_type_node)
13395 break;
13397 /* Output a (nameless) DIE to represent the formal parameter itself. */
13398 parm_die = gen_formal_parameter_die (formal_type, NULL, context_die);
13399 if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
13400 && link == first_parm_type)
13401 || (arg && DECL_ARTIFICIAL (arg)))
13402 add_AT_flag (parm_die, DW_AT_artificial, 1);
13404 link = TREE_CHAIN (link);
13405 if (arg)
13406 arg = TREE_CHAIN (arg);
13409 /* If this function type has an ellipsis, add a
13410 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
13411 if (formal_type != void_type_node)
13412 gen_unspecified_parameters_die (function_or_method_type, context_die);
13414 /* Make our second (and final) pass over the list of formal parameter types
13415 and output DIEs to represent those types (as necessary). */
13416 for (link = TYPE_ARG_TYPES (function_or_method_type);
13417 link && TREE_VALUE (link);
13418 link = TREE_CHAIN (link))
13419 gen_type_die (TREE_VALUE (link), context_die);
13422 /* We want to generate the DIE for TYPE so that we can generate the
13423 die for MEMBER, which has been defined; we will need to refer back
13424 to the member declaration nested within TYPE. If we're trying to
13425 generate minimal debug info for TYPE, processing TYPE won't do the
13426 trick; we need to attach the member declaration by hand. */
13428 static void
13429 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
13431 gen_type_die (type, context_die);
13433 /* If we're trying to avoid duplicate debug info, we may not have
13434 emitted the member decl for this function. Emit it now. */
13435 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
13436 && ! lookup_decl_die (member))
13438 dw_die_ref type_die;
13439 gcc_assert (!decl_ultimate_origin (member));
13441 push_decl_scope (type);
13442 type_die = lookup_type_die (type);
13443 if (TREE_CODE (member) == FUNCTION_DECL)
13444 gen_subprogram_die (member, type_die);
13445 else if (TREE_CODE (member) == FIELD_DECL)
13447 /* Ignore the nameless fields that are used to skip bits but handle
13448 C++ anonymous unions and structs. */
13449 if (DECL_NAME (member) != NULL_TREE
13450 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
13451 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
13453 gen_type_die (member_declared_type (member), type_die);
13454 gen_field_die (member, type_die);
13457 else
13458 gen_variable_die (member, NULL_TREE, type_die);
13460 pop_decl_scope ();
13464 /* Generate the DWARF2 info for the "abstract" instance of a function which we
13465 may later generate inlined and/or out-of-line instances of. */
13467 static void
13468 dwarf2out_abstract_function (tree decl)
13470 dw_die_ref old_die;
13471 tree save_fn;
13472 tree context;
13473 int was_abstract = DECL_ABSTRACT (decl);
13475 /* Make sure we have the actual abstract inline, not a clone. */
13476 decl = DECL_ORIGIN (decl);
13478 old_die = lookup_decl_die (decl);
13479 if (old_die && get_AT (old_die, DW_AT_inline))
13480 /* We've already generated the abstract instance. */
13481 return;
13483 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
13484 we don't get confused by DECL_ABSTRACT. */
13485 if (debug_info_level > DINFO_LEVEL_TERSE)
13487 context = decl_class_context (decl);
13488 if (context)
13489 gen_type_die_for_member
13490 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
13493 /* Pretend we've just finished compiling this function. */
13494 save_fn = current_function_decl;
13495 current_function_decl = decl;
13496 push_cfun (DECL_STRUCT_FUNCTION (decl));
13498 set_decl_abstract_flags (decl, 1);
13499 dwarf2out_decl (decl);
13500 if (! was_abstract)
13501 set_decl_abstract_flags (decl, 0);
13503 current_function_decl = save_fn;
13504 pop_cfun ();
13507 /* Helper function of premark_used_types() which gets called through
13508 htab_traverse_resize().
13510 Marks the DIE of a given type in *SLOT as perennial, so it never gets
13511 marked as unused by prune_unused_types. */
13512 static int
13513 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
13515 tree type;
13516 dw_die_ref die;
13518 type = (tree) *slot;
13519 die = lookup_type_die (type);
13520 if (die != NULL)
13521 die->die_perennial_p = 1;
13522 return 1;
13525 /* Mark all members of used_types_hash as perennial. */
13526 static void
13527 premark_used_types (void)
13529 if (cfun && cfun->used_types_hash)
13530 htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
13533 /* Generate a DIE to represent a declared function (either file-scope or
13534 block-local). */
13536 static void
13537 gen_subprogram_die (tree decl, dw_die_ref context_die)
13539 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13540 tree origin = decl_ultimate_origin (decl);
13541 dw_die_ref subr_die;
13542 tree fn_arg_types;
13543 tree outer_scope;
13544 dw_die_ref old_die = lookup_decl_die (decl);
13545 int declaration = (current_function_decl != decl
13546 || class_or_namespace_scope_p (context_die));
13548 premark_used_types ();
13550 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
13551 started to generate the abstract instance of an inline, decided to output
13552 its containing class, and proceeded to emit the declaration of the inline
13553 from the member list for the class. If so, DECLARATION takes priority;
13554 we'll get back to the abstract instance when done with the class. */
13556 /* The class-scope declaration DIE must be the primary DIE. */
13557 if (origin && declaration && class_or_namespace_scope_p (context_die))
13559 origin = NULL;
13560 gcc_assert (!old_die);
13563 /* Now that the C++ front end lazily declares artificial member fns, we
13564 might need to retrofit the declaration into its class. */
13565 if (!declaration && !origin && !old_die
13566 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
13567 && !class_or_namespace_scope_p (context_die)
13568 && debug_info_level > DINFO_LEVEL_TERSE)
13569 old_die = force_decl_die (decl);
13571 if (origin != NULL)
13573 gcc_assert (!declaration || local_scope_p (context_die));
13575 /* Fixup die_parent for the abstract instance of a nested
13576 inline function. */
13577 if (old_die && old_die->die_parent == NULL)
13578 add_child_die (context_die, old_die);
13580 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13581 add_abstract_origin_attribute (subr_die, origin);
13583 else if (old_die)
13585 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
13586 struct dwarf_file_data * file_index = lookup_filename (s.file);
13588 if (!get_AT_flag (old_die, DW_AT_declaration)
13589 /* We can have a normal definition following an inline one in the
13590 case of redefinition of GNU C extern inlines.
13591 It seems reasonable to use AT_specification in this case. */
13592 && !get_AT (old_die, DW_AT_inline))
13594 /* Detect and ignore this case, where we are trying to output
13595 something we have already output. */
13596 return;
13599 /* If the definition comes from the same place as the declaration,
13600 maybe use the old DIE. We always want the DIE for this function
13601 that has the *_pc attributes to be under comp_unit_die so the
13602 debugger can find it. We also need to do this for abstract
13603 instances of inlines, since the spec requires the out-of-line copy
13604 to have the same parent. For local class methods, this doesn't
13605 apply; we just use the old DIE. */
13606 if ((old_die->die_parent == comp_unit_die || context_die == NULL)
13607 && (DECL_ARTIFICIAL (decl)
13608 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
13609 && (get_AT_unsigned (old_die, DW_AT_decl_line)
13610 == (unsigned) s.line))))
13612 subr_die = old_die;
13614 /* Clear out the declaration attribute and the formal parameters.
13615 Do not remove all children, because it is possible that this
13616 declaration die was forced using force_decl_die(). In such
13617 cases die that forced declaration die (e.g. TAG_imported_module)
13618 is one of the children that we do not want to remove. */
13619 remove_AT (subr_die, DW_AT_declaration);
13620 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
13622 else
13624 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13625 add_AT_specification (subr_die, old_die);
13626 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
13627 add_AT_file (subr_die, DW_AT_decl_file, file_index);
13628 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
13629 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
13632 else
13634 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13636 if (TREE_PUBLIC (decl))
13637 add_AT_flag (subr_die, DW_AT_external, 1);
13639 add_name_and_src_coords_attributes (subr_die, decl);
13640 if (debug_info_level > DINFO_LEVEL_TERSE)
13642 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
13643 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
13644 0, 0, context_die);
13647 add_pure_or_virtual_attribute (subr_die, decl);
13648 if (DECL_ARTIFICIAL (decl))
13649 add_AT_flag (subr_die, DW_AT_artificial, 1);
13651 if (TREE_PROTECTED (decl))
13652 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
13653 else if (TREE_PRIVATE (decl))
13654 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
13657 if (declaration)
13659 if (!old_die || !get_AT (old_die, DW_AT_inline))
13661 add_AT_flag (subr_die, DW_AT_declaration, 1);
13663 /* The first time we see a member function, it is in the context of
13664 the class to which it belongs. We make sure of this by emitting
13665 the class first. The next time is the definition, which is
13666 handled above. The two may come from the same source text.
13668 Note that force_decl_die() forces function declaration die. It is
13669 later reused to represent definition. */
13670 equate_decl_number_to_die (decl, subr_die);
13673 else if (DECL_ABSTRACT (decl))
13675 if (DECL_DECLARED_INLINE_P (decl))
13677 if (cgraph_function_possibly_inlined_p (decl))
13678 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
13679 else
13680 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
13682 else
13684 if (cgraph_function_possibly_inlined_p (decl))
13685 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
13686 else
13687 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
13690 if (DECL_DECLARED_INLINE_P (decl)
13691 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
13692 add_AT_flag (subr_die, DW_AT_artificial, 1);
13694 equate_decl_number_to_die (decl, subr_die);
13696 else if (!DECL_EXTERNAL (decl))
13698 HOST_WIDE_INT cfa_fb_offset;
13700 if (!old_die || !get_AT (old_die, DW_AT_inline))
13701 equate_decl_number_to_die (decl, subr_die);
13703 if (!flag_reorder_blocks_and_partition)
13705 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
13706 current_function_funcdef_no);
13707 add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
13708 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13709 current_function_funcdef_no);
13710 add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
13712 add_pubname (decl, subr_die);
13713 add_arange (decl, subr_die);
13715 else
13716 { /* Do nothing for now; maybe need to duplicate die, one for
13717 hot section and one for cold section, then use the hot/cold
13718 section begin/end labels to generate the aranges... */
13720 add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
13721 add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
13722 add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
13723 add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
13725 add_pubname (decl, subr_die);
13726 add_arange (decl, subr_die);
13727 add_arange (decl, subr_die);
13731 #ifdef MIPS_DEBUGGING_INFO
13732 /* Add a reference to the FDE for this routine. */
13733 add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
13734 #endif
13736 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
13738 /* We define the "frame base" as the function's CFA. This is more
13739 convenient for several reasons: (1) It's stable across the prologue
13740 and epilogue, which makes it better than just a frame pointer,
13741 (2) With dwarf3, there exists a one-byte encoding that allows us
13742 to reference the .debug_frame data by proxy, but failing that,
13743 (3) We can at least reuse the code inspection and interpretation
13744 code that determines the CFA position at various points in the
13745 function. */
13746 /* ??? Use some command-line or configury switch to enable the use
13747 of dwarf3 DW_OP_call_frame_cfa. At present there are no dwarf
13748 consumers that understand it; fall back to "pure" dwarf2 and
13749 convert the CFA data into a location list. */
13751 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
13752 if (list->dw_loc_next)
13753 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
13754 else
13755 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
13758 /* Compute a displacement from the "steady-state frame pointer" to
13759 the CFA. The former is what all stack slots and argument slots
13760 will reference in the rtl; the later is what we've told the
13761 debugger about. We'll need to adjust all frame_base references
13762 by this displacement. */
13763 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
13765 if (cfun->static_chain_decl)
13766 add_AT_location_description (subr_die, DW_AT_static_link,
13767 loc_descriptor_from_tree (cfun->static_chain_decl));
13770 /* Now output descriptions of the arguments for this function. This gets
13771 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
13772 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
13773 `...' at the end of the formal parameter list. In order to find out if
13774 there was a trailing ellipsis or not, we must instead look at the type
13775 associated with the FUNCTION_DECL. This will be a node of type
13776 FUNCTION_TYPE. If the chain of type nodes hanging off of this
13777 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
13778 an ellipsis at the end. */
13780 /* In the case where we are describing a mere function declaration, all we
13781 need to do here (and all we *can* do here) is to describe the *types* of
13782 its formal parameters. */
13783 if (debug_info_level <= DINFO_LEVEL_TERSE)
13785 else if (declaration)
13786 gen_formal_types_die (decl, subr_die);
13787 else
13789 /* Generate DIEs to represent all known formal parameters. */
13790 tree arg_decls = DECL_ARGUMENTS (decl);
13791 tree parm;
13793 /* When generating DIEs, generate the unspecified_parameters DIE
13794 instead if we come across the arg "__builtin_va_alist" */
13795 for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
13796 if (TREE_CODE (parm) == PARM_DECL)
13798 if (DECL_NAME (parm)
13799 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
13800 "__builtin_va_alist"))
13801 gen_unspecified_parameters_die (parm, subr_die);
13802 else
13803 gen_decl_die (parm, NULL, subr_die);
13806 /* Decide whether we need an unspecified_parameters DIE at the end.
13807 There are 2 more cases to do this for: 1) the ansi ... declaration -
13808 this is detectable when the end of the arg list is not a
13809 void_type_node 2) an unprototyped function declaration (not a
13810 definition). This just means that we have no info about the
13811 parameters at all. */
13812 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
13813 if (fn_arg_types != NULL)
13815 /* This is the prototyped case, check for.... */
13816 if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
13817 gen_unspecified_parameters_die (decl, subr_die);
13819 else if (DECL_INITIAL (decl) == NULL_TREE)
13820 gen_unspecified_parameters_die (decl, subr_die);
13823 /* Output Dwarf info for all of the stuff within the body of the function
13824 (if it has one - it may be just a declaration). */
13825 outer_scope = DECL_INITIAL (decl);
13827 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
13828 a function. This BLOCK actually represents the outermost binding contour
13829 for the function, i.e. the contour in which the function's formal
13830 parameters and labels get declared. Curiously, it appears that the front
13831 end doesn't actually put the PARM_DECL nodes for the current function onto
13832 the BLOCK_VARS list for this outer scope, but are strung off of the
13833 DECL_ARGUMENTS list for the function instead.
13835 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
13836 the LABEL_DECL nodes for the function however, and we output DWARF info
13837 for those in decls_for_scope. Just within the `outer_scope' there will be
13838 a BLOCK node representing the function's outermost pair of curly braces,
13839 and any blocks used for the base and member initializers of a C++
13840 constructor function. */
13841 if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
13843 /* Emit a DW_TAG_variable DIE for a named return value. */
13844 if (DECL_NAME (DECL_RESULT (decl)))
13845 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
13847 current_function_has_inlines = 0;
13848 decls_for_scope (outer_scope, subr_die, 0);
13850 #if 0 && defined (MIPS_DEBUGGING_INFO)
13851 if (current_function_has_inlines)
13853 add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
13854 if (! comp_unit_has_inlines)
13856 add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
13857 comp_unit_has_inlines = 1;
13860 #endif
13862 /* Add the calling convention attribute if requested. */
13863 add_calling_convention_attribute (subr_die, decl);
13867 /* Returns a hash value for X (which really is a die_struct). */
13869 static hashval_t
13870 common_block_die_table_hash (const void *x)
13872 const_dw_die_ref d = (const_dw_die_ref) x;
13873 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
13876 /* Return nonzero if decl_id and die_parent of die_struct X is the same
13877 as decl_id and die_parent of die_struct Y. */
13879 static int
13880 common_block_die_table_eq (const void *x, const void *y)
13882 const_dw_die_ref d = (const_dw_die_ref) x;
13883 const_dw_die_ref e = (const_dw_die_ref) y;
13884 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
13887 /* Generate a DIE to represent a declared data object.
13888 Either DECL or ORIGIN must be non-null. */
13890 static void
13891 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
13893 HOST_WIDE_INT off;
13894 tree com_decl;
13895 tree decl_or_origin = decl ? decl : origin;
13896 dw_die_ref var_die;
13897 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
13898 dw_die_ref origin_die;
13899 int declaration = (DECL_EXTERNAL (decl_or_origin)
13900 /* If DECL is COMDAT and has not actually been
13901 emitted, we cannot take its address; there
13902 might end up being no definition anywhere in
13903 the program. For example, consider the C++
13904 test case:
13906 template <class T>
13907 struct S { static const int i = 7; };
13909 template <class T>
13910 const int S<T>::i;
13912 int f() { return S<int>::i; }
13914 Here, S<int>::i is not DECL_EXTERNAL, but no
13915 definition is required, so the compiler will
13916 not emit a definition. */
13917 || (TREE_CODE (decl_or_origin) == VAR_DECL
13918 && DECL_COMDAT (decl_or_origin)
13919 && !TREE_ASM_WRITTEN (decl_or_origin))
13920 || class_or_namespace_scope_p (context_die));
13922 if (!origin)
13923 origin = decl_ultimate_origin (decl);
13925 com_decl = fortran_common (decl_or_origin, &off);
13927 /* Symbol in common gets emitted as a child of the common block, in the form
13928 of a data member. */
13929 if (com_decl)
13931 tree field;
13932 dw_die_ref com_die;
13933 dw_loc_descr_ref loc;
13934 die_node com_die_arg;
13936 var_die = lookup_decl_die (decl_or_origin);
13937 if (var_die)
13939 if (get_AT (var_die, DW_AT_location) == NULL)
13941 loc = loc_descriptor_from_tree (com_decl);
13942 if (loc)
13944 if (off)
13946 /* Optimize the common case. */
13947 if (loc->dw_loc_opc == DW_OP_addr
13948 && loc->dw_loc_next == NULL
13949 && GET_CODE (loc->dw_loc_oprnd1.v.val_addr)
13950 == SYMBOL_REF)
13951 loc->dw_loc_oprnd1.v.val_addr
13952 = plus_constant (loc->dw_loc_oprnd1.v.val_addr, off);
13953 else
13954 add_loc_descr (&loc,
13955 new_loc_descr (DW_OP_plus_uconst,
13956 off, 0));
13958 add_AT_loc (var_die, DW_AT_location, loc);
13959 remove_AT (var_die, DW_AT_declaration);
13962 return;
13965 if (common_block_die_table == NULL)
13966 common_block_die_table
13967 = htab_create_ggc (10, common_block_die_table_hash,
13968 common_block_die_table_eq, NULL);
13970 field = TREE_OPERAND (DECL_VALUE_EXPR (decl), 0);
13971 com_die_arg.decl_id = DECL_UID (com_decl);
13972 com_die_arg.die_parent = context_die;
13973 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
13974 loc = loc_descriptor_from_tree (com_decl);
13975 if (com_die == NULL)
13977 const char *cnam
13978 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
13979 void **slot;
13981 com_die = new_die (DW_TAG_common_block, context_die, decl);
13982 add_name_and_src_coords_attributes (com_die, com_decl);
13983 if (loc)
13985 add_AT_loc (com_die, DW_AT_location, loc);
13986 /* Avoid sharing the same loc descriptor between
13987 DW_TAG_common_block and DW_TAG_variable. */
13988 loc = loc_descriptor_from_tree (com_decl);
13990 else if (DECL_EXTERNAL (decl))
13991 add_AT_flag (com_die, DW_AT_declaration, 1);
13992 add_pubname_string (cnam, com_die); /* ??? needed? */
13993 com_die->decl_id = DECL_UID (com_decl);
13994 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
13995 *slot = (void *) com_die;
13997 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
13999 add_AT_loc (com_die, DW_AT_location, loc);
14000 loc = loc_descriptor_from_tree (com_decl);
14001 remove_AT (com_die, DW_AT_declaration);
14003 var_die = new_die (DW_TAG_variable, com_die, decl);
14004 add_name_and_src_coords_attributes (var_die, decl);
14005 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
14006 TREE_THIS_VOLATILE (decl), context_die);
14007 add_AT_flag (var_die, DW_AT_external, 1);
14008 if (loc)
14010 if (off)
14012 /* Optimize the common case. */
14013 if (loc->dw_loc_opc == DW_OP_addr
14014 && loc->dw_loc_next == NULL
14015 && GET_CODE (loc->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
14016 loc->dw_loc_oprnd1.v.val_addr
14017 = plus_constant (loc->dw_loc_oprnd1.v.val_addr, off);
14018 else
14019 add_loc_descr (&loc, new_loc_descr (DW_OP_plus_uconst,
14020 off, 0));
14022 add_AT_loc (var_die, DW_AT_location, loc);
14024 else if (DECL_EXTERNAL (decl))
14025 add_AT_flag (var_die, DW_AT_declaration, 1);
14026 equate_decl_number_to_die (decl, var_die);
14027 return;
14030 /* If the compiler emitted a definition for the DECL declaration
14031 and if we already emitted a DIE for it, don't emit a second
14032 DIE for it again. */
14033 if (old_die
14034 && declaration
14035 && old_die->die_parent == context_die)
14036 return;
14038 var_die = new_die (DW_TAG_variable, context_die, decl);
14040 origin_die = NULL;
14041 if (origin != NULL)
14042 origin_die = add_abstract_origin_attribute (var_die, origin);
14044 /* Loop unrolling can create multiple blocks that refer to the same
14045 static variable, so we must test for the DW_AT_declaration flag.
14047 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
14048 copy decls and set the DECL_ABSTRACT flag on them instead of
14049 sharing them.
14051 ??? Duplicated blocks have been rewritten to use .debug_ranges.
14053 ??? The declare_in_namespace support causes us to get two DIEs for one
14054 variable, both of which are declarations. We want to avoid considering
14055 one to be a specification, so we must test that this DIE is not a
14056 declaration. */
14057 else if (old_die && TREE_STATIC (decl) && ! declaration
14058 && get_AT_flag (old_die, DW_AT_declaration) == 1)
14060 /* This is a definition of a C++ class level static. */
14061 add_AT_specification (var_die, old_die);
14062 if (DECL_NAME (decl))
14064 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
14065 struct dwarf_file_data * file_index = lookup_filename (s.file);
14067 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
14068 add_AT_file (var_die, DW_AT_decl_file, file_index);
14070 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
14071 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
14074 else
14076 tree type = TREE_TYPE (decl);
14078 add_name_and_src_coords_attributes (var_die, decl);
14079 if ((TREE_CODE (decl) == PARM_DECL
14080 || TREE_CODE (decl) == RESULT_DECL)
14081 && DECL_BY_REFERENCE (decl))
14082 add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
14083 else
14084 add_type_attribute (var_die, type, TREE_READONLY (decl),
14085 TREE_THIS_VOLATILE (decl), context_die);
14087 if (TREE_PUBLIC (decl))
14088 add_AT_flag (var_die, DW_AT_external, 1);
14090 if (DECL_ARTIFICIAL (decl))
14091 add_AT_flag (var_die, DW_AT_artificial, 1);
14093 if (TREE_PROTECTED (decl))
14094 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
14095 else if (TREE_PRIVATE (decl))
14096 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
14099 if (declaration)
14100 add_AT_flag (var_die, DW_AT_declaration, 1);
14102 if (decl && (DECL_ABSTRACT (decl) || declaration))
14103 equate_decl_number_to_die (decl, var_die);
14105 if (! declaration
14106 && (! DECL_ABSTRACT (decl_or_origin)
14107 /* Local static vars are shared between all clones/inlines,
14108 so emit DW_AT_location on the abstract DIE if DECL_RTL is
14109 already set. */
14110 || (TREE_CODE (decl_or_origin) == VAR_DECL
14111 && TREE_STATIC (decl_or_origin)
14112 && DECL_RTL_SET_P (decl_or_origin)))
14113 /* When abstract origin already has DW_AT_location attribute, no need
14114 to add it again. */
14115 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
14117 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
14118 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
14119 defer_location (decl_or_origin, var_die);
14120 else
14121 add_location_or_const_value_attribute (var_die,
14122 decl_or_origin,
14123 DW_AT_location);
14124 add_pubname (decl_or_origin, var_die);
14126 else
14127 tree_add_const_value_attribute (var_die, decl_or_origin);
14130 /* Generate a DIE to represent a named constant. */
14132 static void
14133 gen_const_die (tree decl, dw_die_ref context_die)
14135 dw_die_ref const_die;
14136 tree type = TREE_TYPE (decl);
14138 const_die = new_die (DW_TAG_constant, context_die, decl);
14139 add_name_and_src_coords_attributes (const_die, decl);
14140 add_type_attribute (const_die, type, 1, 0, context_die);
14141 if (TREE_PUBLIC (decl))
14142 add_AT_flag (const_die, DW_AT_external, 1);
14143 if (DECL_ARTIFICIAL (decl))
14144 add_AT_flag (const_die, DW_AT_artificial, 1);
14145 tree_add_const_value_attribute (const_die, decl);
14148 /* Generate a DIE to represent a label identifier. */
14150 static void
14151 gen_label_die (tree decl, dw_die_ref context_die)
14153 tree origin = decl_ultimate_origin (decl);
14154 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
14155 rtx insn;
14156 char label[MAX_ARTIFICIAL_LABEL_BYTES];
14158 if (origin != NULL)
14159 add_abstract_origin_attribute (lbl_die, origin);
14160 else
14161 add_name_and_src_coords_attributes (lbl_die, decl);
14163 if (DECL_ABSTRACT (decl))
14164 equate_decl_number_to_die (decl, lbl_die);
14165 else
14167 insn = DECL_RTL_IF_SET (decl);
14169 /* Deleted labels are programmer specified labels which have been
14170 eliminated because of various optimizations. We still emit them
14171 here so that it is possible to put breakpoints on them. */
14172 if (insn
14173 && (LABEL_P (insn)
14174 || ((NOTE_P (insn)
14175 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
14177 /* When optimization is enabled (via -O) some parts of the compiler
14178 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
14179 represent source-level labels which were explicitly declared by
14180 the user. This really shouldn't be happening though, so catch
14181 it if it ever does happen. */
14182 gcc_assert (!INSN_DELETED_P (insn));
14184 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
14185 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
14190 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
14191 attributes to the DIE for a block STMT, to describe where the inlined
14192 function was called from. This is similar to add_src_coords_attributes. */
14194 static inline void
14195 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
14197 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
14199 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
14200 add_AT_unsigned (die, DW_AT_call_line, s.line);
14204 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
14205 Add low_pc and high_pc attributes to the DIE for a block STMT. */
14207 static inline void
14208 add_high_low_attributes (tree stmt, dw_die_ref die)
14210 char label[MAX_ARTIFICIAL_LABEL_BYTES];
14212 if (BLOCK_FRAGMENT_CHAIN (stmt))
14214 tree chain;
14216 if (inlined_function_outer_scope_p (stmt))
14218 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
14219 BLOCK_NUMBER (stmt));
14220 add_AT_lbl_id (die, DW_AT_entry_pc, label);
14223 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
14225 chain = BLOCK_FRAGMENT_CHAIN (stmt);
14228 add_ranges (chain);
14229 chain = BLOCK_FRAGMENT_CHAIN (chain);
14231 while (chain);
14232 add_ranges (NULL);
14234 else
14236 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
14237 BLOCK_NUMBER (stmt));
14238 add_AT_lbl_id (die, DW_AT_low_pc, label);
14239 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
14240 BLOCK_NUMBER (stmt));
14241 add_AT_lbl_id (die, DW_AT_high_pc, label);
14245 /* Generate a DIE for a lexical block. */
14247 static void
14248 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
14250 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
14252 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
14253 add_high_low_attributes (stmt, stmt_die);
14255 decls_for_scope (stmt, stmt_die, depth);
14258 /* Generate a DIE for an inlined subprogram. */
14260 static void
14261 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
14263 tree decl = block_ultimate_origin (stmt);
14265 /* Emit info for the abstract instance first, if we haven't yet. We
14266 must emit this even if the block is abstract, otherwise when we
14267 emit the block below (or elsewhere), we may end up trying to emit
14268 a die whose origin die hasn't been emitted, and crashing. */
14269 dwarf2out_abstract_function (decl);
14271 if (! BLOCK_ABSTRACT (stmt))
14273 dw_die_ref subr_die
14274 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
14276 add_abstract_origin_attribute (subr_die, decl);
14277 if (TREE_ASM_WRITTEN (stmt))
14278 add_high_low_attributes (stmt, subr_die);
14279 add_call_src_coords_attributes (stmt, subr_die);
14281 decls_for_scope (stmt, subr_die, depth);
14282 current_function_has_inlines = 1;
14284 else
14285 /* We may get here if we're the outer block of function A that was
14286 inlined into function B that was inlined into function C. When
14287 generating debugging info for C, dwarf2out_abstract_function(B)
14288 would mark all inlined blocks as abstract, including this one.
14289 So, we wouldn't (and shouldn't) expect labels to be generated
14290 for this one. Instead, just emit debugging info for
14291 declarations within the block. This is particularly important
14292 in the case of initializers of arguments passed from B to us:
14293 if they're statement expressions containing declarations, we
14294 wouldn't generate dies for their abstract variables, and then,
14295 when generating dies for the real variables, we'd die (pun
14296 intended :-) */
14297 gen_lexical_block_die (stmt, context_die, depth);
14300 /* Generate a DIE for a field in a record, or structure. */
14302 static void
14303 gen_field_die (tree decl, dw_die_ref context_die)
14305 dw_die_ref decl_die;
14307 if (TREE_TYPE (decl) == error_mark_node)
14308 return;
14310 decl_die = new_die (DW_TAG_member, context_die, decl);
14311 add_name_and_src_coords_attributes (decl_die, decl);
14312 add_type_attribute (decl_die, member_declared_type (decl),
14313 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
14314 context_die);
14316 if (DECL_BIT_FIELD_TYPE (decl))
14318 add_byte_size_attribute (decl_die, decl);
14319 add_bit_size_attribute (decl_die, decl);
14320 add_bit_offset_attribute (decl_die, decl);
14323 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
14324 add_data_member_location_attribute (decl_die, decl);
14326 if (DECL_ARTIFICIAL (decl))
14327 add_AT_flag (decl_die, DW_AT_artificial, 1);
14329 if (TREE_PROTECTED (decl))
14330 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
14331 else if (TREE_PRIVATE (decl))
14332 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
14334 /* Equate decl number to die, so that we can look up this decl later on. */
14335 equate_decl_number_to_die (decl, decl_die);
14338 #if 0
14339 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
14340 Use modified_type_die instead.
14341 We keep this code here just in case these types of DIEs may be needed to
14342 represent certain things in other languages (e.g. Pascal) someday. */
14344 static void
14345 gen_pointer_type_die (tree type, dw_die_ref context_die)
14347 dw_die_ref ptr_die
14348 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
14350 equate_type_number_to_die (type, ptr_die);
14351 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
14352 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
14355 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
14356 Use modified_type_die instead.
14357 We keep this code here just in case these types of DIEs may be needed to
14358 represent certain things in other languages (e.g. Pascal) someday. */
14360 static void
14361 gen_reference_type_die (tree type, dw_die_ref context_die)
14363 dw_die_ref ref_die
14364 = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
14366 equate_type_number_to_die (type, ref_die);
14367 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
14368 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
14370 #endif
14372 /* Generate a DIE for a pointer to a member type. */
14374 static void
14375 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
14377 dw_die_ref ptr_die
14378 = new_die (DW_TAG_ptr_to_member_type,
14379 scope_die_for (type, context_die), type);
14381 equate_type_number_to_die (type, ptr_die);
14382 add_AT_die_ref (ptr_die, DW_AT_containing_type,
14383 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
14384 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
14387 /* Generate the DIE for the compilation unit. */
14389 static dw_die_ref
14390 gen_compile_unit_die (const char *filename)
14392 dw_die_ref die;
14393 char producer[250];
14394 const char *language_string = lang_hooks.name;
14395 int language;
14397 die = new_die (DW_TAG_compile_unit, NULL, NULL);
14399 if (filename)
14401 add_name_attribute (die, filename);
14402 /* Don't add cwd for <built-in>. */
14403 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
14404 add_comp_dir_attribute (die);
14407 sprintf (producer, "%s %s", language_string, version_string);
14409 #ifdef MIPS_DEBUGGING_INFO
14410 /* The MIPS/SGI compilers place the 'cc' command line options in the producer
14411 string. The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
14412 not appear in the producer string, the debugger reaches the conclusion
14413 that the object file is stripped and has no debugging information.
14414 To get the MIPS/SGI debugger to believe that there is debugging
14415 information in the object file, we add a -g to the producer string. */
14416 if (debug_info_level > DINFO_LEVEL_TERSE)
14417 strcat (producer, " -g");
14418 #endif
14420 add_AT_string (die, DW_AT_producer, producer);
14422 if (strcmp (language_string, "GNU C++") == 0)
14423 language = DW_LANG_C_plus_plus;
14424 else if (strcmp (language_string, "GNU Ada") == 0)
14425 language = DW_LANG_Ada95;
14426 else if (strcmp (language_string, "GNU F77") == 0)
14427 language = DW_LANG_Fortran77;
14428 else if (strcmp (language_string, "GNU Fortran") == 0)
14429 language = DW_LANG_Fortran95;
14430 else if (strcmp (language_string, "GNU Pascal") == 0)
14431 language = DW_LANG_Pascal83;
14432 else if (strcmp (language_string, "GNU Java") == 0)
14433 language = DW_LANG_Java;
14434 else if (strcmp (language_string, "GNU Objective-C") == 0)
14435 language = DW_LANG_ObjC;
14436 else if (strcmp (language_string, "GNU Objective-C++") == 0)
14437 language = DW_LANG_ObjC_plus_plus;
14438 else
14439 language = DW_LANG_C89;
14441 add_AT_unsigned (die, DW_AT_language, language);
14442 return die;
14445 /* Generate the DIE for a base class. */
14447 static void
14448 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
14450 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
14452 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
14453 add_data_member_location_attribute (die, binfo);
14455 if (BINFO_VIRTUAL_P (binfo))
14456 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
14458 if (access == access_public_node)
14459 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14460 else if (access == access_protected_node)
14461 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14464 /* Generate a DIE for a class member. */
14466 static void
14467 gen_member_die (tree type, dw_die_ref context_die)
14469 tree member;
14470 tree binfo = TYPE_BINFO (type);
14471 dw_die_ref child;
14473 /* If this is not an incomplete type, output descriptions of each of its
14474 members. Note that as we output the DIEs necessary to represent the
14475 members of this record or union type, we will also be trying to output
14476 DIEs to represent the *types* of those members. However the `type'
14477 function (above) will specifically avoid generating type DIEs for member
14478 types *within* the list of member DIEs for this (containing) type except
14479 for those types (of members) which are explicitly marked as also being
14480 members of this (containing) type themselves. The g++ front- end can
14481 force any given type to be treated as a member of some other (containing)
14482 type by setting the TYPE_CONTEXT of the given (member) type to point to
14483 the TREE node representing the appropriate (containing) type. */
14485 /* First output info about the base classes. */
14486 if (binfo)
14488 VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
14489 int i;
14490 tree base;
14492 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
14493 gen_inheritance_die (base,
14494 (accesses ? VEC_index (tree, accesses, i)
14495 : access_public_node), context_die);
14498 /* Now output info about the data members and type members. */
14499 for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
14501 /* If we thought we were generating minimal debug info for TYPE
14502 and then changed our minds, some of the member declarations
14503 may have already been defined. Don't define them again, but
14504 do put them in the right order. */
14506 child = lookup_decl_die (member);
14507 if (child)
14508 splice_child_die (context_die, child);
14509 else
14510 gen_decl_die (member, NULL, context_die);
14513 /* Now output info about the function members (if any). */
14514 for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
14516 /* Don't include clones in the member list. */
14517 if (DECL_ABSTRACT_ORIGIN (member))
14518 continue;
14520 child = lookup_decl_die (member);
14521 if (child)
14522 splice_child_die (context_die, child);
14523 else
14524 gen_decl_die (member, NULL, context_die);
14528 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
14529 is set, we pretend that the type was never defined, so we only get the
14530 member DIEs needed by later specification DIEs. */
14532 static void
14533 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
14534 enum debug_info_usage usage)
14536 dw_die_ref type_die = lookup_type_die (type);
14537 dw_die_ref scope_die = 0;
14538 int nested = 0;
14539 int complete = (TYPE_SIZE (type)
14540 && (! TYPE_STUB_DECL (type)
14541 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
14542 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
14543 complete = complete && should_emit_struct_debug (type, usage);
14545 if (type_die && ! complete)
14546 return;
14548 if (TYPE_CONTEXT (type) != NULL_TREE
14549 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
14550 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
14551 nested = 1;
14553 scope_die = scope_die_for (type, context_die);
14555 if (! type_die || (nested && scope_die == comp_unit_die))
14556 /* First occurrence of type or toplevel definition of nested class. */
14558 dw_die_ref old_die = type_die;
14560 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
14561 ? record_type_tag (type) : DW_TAG_union_type,
14562 scope_die, type);
14563 equate_type_number_to_die (type, type_die);
14564 if (old_die)
14565 add_AT_specification (type_die, old_die);
14566 else
14567 add_name_attribute (type_die, type_tag (type));
14569 else
14570 remove_AT (type_die, DW_AT_declaration);
14572 /* If this type has been completed, then give it a byte_size attribute and
14573 then give a list of members. */
14574 if (complete && !ns_decl)
14576 /* Prevent infinite recursion in cases where the type of some member of
14577 this type is expressed in terms of this type itself. */
14578 TREE_ASM_WRITTEN (type) = 1;
14579 add_byte_size_attribute (type_die, type);
14580 if (TYPE_STUB_DECL (type) != NULL_TREE)
14581 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
14583 /* If the first reference to this type was as the return type of an
14584 inline function, then it may not have a parent. Fix this now. */
14585 if (type_die->die_parent == NULL)
14586 add_child_die (scope_die, type_die);
14588 push_decl_scope (type);
14589 gen_member_die (type, type_die);
14590 pop_decl_scope ();
14592 /* GNU extension: Record what type our vtable lives in. */
14593 if (TYPE_VFIELD (type))
14595 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
14597 gen_type_die (vtype, context_die);
14598 add_AT_die_ref (type_die, DW_AT_containing_type,
14599 lookup_type_die (vtype));
14602 else
14604 add_AT_flag (type_die, DW_AT_declaration, 1);
14606 /* We don't need to do this for function-local types. */
14607 if (TYPE_STUB_DECL (type)
14608 && ! decl_function_context (TYPE_STUB_DECL (type)))
14609 VEC_safe_push (tree, gc, incomplete_types, type);
14612 if (get_AT (type_die, DW_AT_name))
14613 add_pubtype (type, type_die);
14616 /* Generate a DIE for a subroutine _type_. */
14618 static void
14619 gen_subroutine_type_die (tree type, dw_die_ref context_die)
14621 tree return_type = TREE_TYPE (type);
14622 dw_die_ref subr_die
14623 = new_die (DW_TAG_subroutine_type,
14624 scope_die_for (type, context_die), type);
14626 equate_type_number_to_die (type, subr_die);
14627 add_prototyped_attribute (subr_die, type);
14628 add_type_attribute (subr_die, return_type, 0, 0, context_die);
14629 gen_formal_types_die (type, subr_die);
14631 if (get_AT (subr_die, DW_AT_name))
14632 add_pubtype (type, subr_die);
14635 /* Generate a DIE for a type definition. */
14637 static void
14638 gen_typedef_die (tree decl, dw_die_ref context_die)
14640 dw_die_ref type_die;
14641 tree origin;
14643 if (TREE_ASM_WRITTEN (decl))
14644 return;
14646 TREE_ASM_WRITTEN (decl) = 1;
14647 type_die = new_die (DW_TAG_typedef, context_die, decl);
14648 origin = decl_ultimate_origin (decl);
14649 if (origin != NULL)
14650 add_abstract_origin_attribute (type_die, origin);
14651 else
14653 tree type;
14655 add_name_and_src_coords_attributes (type_die, decl);
14656 if (DECL_ORIGINAL_TYPE (decl))
14658 type = DECL_ORIGINAL_TYPE (decl);
14660 gcc_assert (type != TREE_TYPE (decl));
14661 equate_type_number_to_die (TREE_TYPE (decl), type_die);
14663 else
14664 type = TREE_TYPE (decl);
14666 add_type_attribute (type_die, type, TREE_READONLY (decl),
14667 TREE_THIS_VOLATILE (decl), context_die);
14670 if (DECL_ABSTRACT (decl))
14671 equate_decl_number_to_die (decl, type_die);
14673 if (get_AT (type_die, DW_AT_name))
14674 add_pubtype (decl, type_die);
14677 /* Generate a type description DIE. */
14679 static void
14680 gen_type_die_with_usage (tree type, dw_die_ref context_die,
14681 enum debug_info_usage usage)
14683 int need_pop;
14684 struct array_descr_info info;
14686 if (type == NULL_TREE || type == error_mark_node)
14687 return;
14689 if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
14690 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
14692 if (TREE_ASM_WRITTEN (type))
14693 return;
14695 /* Prevent broken recursion; we can't hand off to the same type. */
14696 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
14698 /* Use the DIE of the containing namespace as the parent DIE of
14699 the type description DIE we want to generate. */
14700 if (DECL_CONTEXT (TYPE_NAME (type))
14701 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
14702 context_die = lookup_decl_die (DECL_CONTEXT (TYPE_NAME (type)));
14704 TREE_ASM_WRITTEN (type) = 1;
14705 gen_decl_die (TYPE_NAME (type), NULL, context_die);
14706 return;
14709 /* If this is an array type with hidden descriptor, handle it first. */
14710 if (!TREE_ASM_WRITTEN (type)
14711 && lang_hooks.types.get_array_descr_info
14712 && lang_hooks.types.get_array_descr_info (type, &info))
14714 gen_descr_array_type_die (type, &info, context_die);
14715 TREE_ASM_WRITTEN (type) = 1;
14716 return;
14719 /* We are going to output a DIE to represent the unqualified version
14720 of this type (i.e. without any const or volatile qualifiers) so
14721 get the main variant (i.e. the unqualified version) of this type
14722 now. (Vectors are special because the debugging info is in the
14723 cloned type itself). */
14724 if (TREE_CODE (type) != VECTOR_TYPE)
14725 type = type_main_variant (type);
14727 if (TREE_ASM_WRITTEN (type))
14728 return;
14730 switch (TREE_CODE (type))
14732 case ERROR_MARK:
14733 break;
14735 case POINTER_TYPE:
14736 case REFERENCE_TYPE:
14737 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
14738 ensures that the gen_type_die recursion will terminate even if the
14739 type is recursive. Recursive types are possible in Ada. */
14740 /* ??? We could perhaps do this for all types before the switch
14741 statement. */
14742 TREE_ASM_WRITTEN (type) = 1;
14744 /* For these types, all that is required is that we output a DIE (or a
14745 set of DIEs) to represent the "basis" type. */
14746 gen_type_die_with_usage (TREE_TYPE (type), context_die,
14747 DINFO_USAGE_IND_USE);
14748 break;
14750 case OFFSET_TYPE:
14751 /* This code is used for C++ pointer-to-data-member types.
14752 Output a description of the relevant class type. */
14753 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
14754 DINFO_USAGE_IND_USE);
14756 /* Output a description of the type of the object pointed to. */
14757 gen_type_die_with_usage (TREE_TYPE (type), context_die,
14758 DINFO_USAGE_IND_USE);
14760 /* Now output a DIE to represent this pointer-to-data-member type
14761 itself. */
14762 gen_ptr_to_mbr_type_die (type, context_die);
14763 break;
14765 case FUNCTION_TYPE:
14766 /* Force out return type (in case it wasn't forced out already). */
14767 gen_type_die_with_usage (TREE_TYPE (type), context_die,
14768 DINFO_USAGE_DIR_USE);
14769 gen_subroutine_type_die (type, context_die);
14770 break;
14772 case METHOD_TYPE:
14773 /* Force out return type (in case it wasn't forced out already). */
14774 gen_type_die_with_usage (TREE_TYPE (type), context_die,
14775 DINFO_USAGE_DIR_USE);
14776 gen_subroutine_type_die (type, context_die);
14777 break;
14779 case ARRAY_TYPE:
14780 gen_array_type_die (type, context_die);
14781 break;
14783 case VECTOR_TYPE:
14784 gen_array_type_die (type, context_die);
14785 break;
14787 case ENUMERAL_TYPE:
14788 case RECORD_TYPE:
14789 case UNION_TYPE:
14790 case QUAL_UNION_TYPE:
14791 /* If this is a nested type whose containing class hasn't been written
14792 out yet, writing it out will cover this one, too. This does not apply
14793 to instantiations of member class templates; they need to be added to
14794 the containing class as they are generated. FIXME: This hurts the
14795 idea of combining type decls from multiple TUs, since we can't predict
14796 what set of template instantiations we'll get. */
14797 if (TYPE_CONTEXT (type)
14798 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
14799 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
14801 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
14803 if (TREE_ASM_WRITTEN (type))
14804 return;
14806 /* If that failed, attach ourselves to the stub. */
14807 push_decl_scope (TYPE_CONTEXT (type));
14808 context_die = lookup_type_die (TYPE_CONTEXT (type));
14809 need_pop = 1;
14811 else
14813 context_die = declare_in_namespace (type, context_die);
14814 need_pop = 0;
14817 if (TREE_CODE (type) == ENUMERAL_TYPE)
14819 /* This might have been written out by the call to
14820 declare_in_namespace. */
14821 if (!TREE_ASM_WRITTEN (type))
14822 gen_enumeration_type_die (type, context_die);
14824 else
14825 gen_struct_or_union_type_die (type, context_die, usage);
14827 if (need_pop)
14828 pop_decl_scope ();
14830 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
14831 it up if it is ever completed. gen_*_type_die will set it for us
14832 when appropriate. */
14833 return;
14835 case VOID_TYPE:
14836 case INTEGER_TYPE:
14837 case REAL_TYPE:
14838 case FIXED_POINT_TYPE:
14839 case COMPLEX_TYPE:
14840 case BOOLEAN_TYPE:
14841 /* No DIEs needed for fundamental types. */
14842 break;
14844 case LANG_TYPE:
14845 /* No Dwarf representation currently defined. */
14846 break;
14848 default:
14849 gcc_unreachable ();
14852 TREE_ASM_WRITTEN (type) = 1;
14855 static void
14856 gen_type_die (tree type, dw_die_ref context_die)
14858 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
14861 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
14862 things which are local to the given block. */
14864 static void
14865 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
14867 int must_output_die = 0;
14868 bool inlined_func;
14870 /* Ignore blocks that are NULL. */
14871 if (stmt == NULL_TREE)
14872 return;
14874 inlined_func = inlined_function_outer_scope_p (stmt);
14876 /* If the block is one fragment of a non-contiguous block, do not
14877 process the variables, since they will have been done by the
14878 origin block. Do process subblocks. */
14879 if (BLOCK_FRAGMENT_ORIGIN (stmt))
14881 tree sub;
14883 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
14884 gen_block_die (sub, context_die, depth + 1);
14886 return;
14889 /* Determine if we need to output any Dwarf DIEs at all to represent this
14890 block. */
14891 if (inlined_func)
14892 /* The outer scopes for inlinings *must* always be represented. We
14893 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
14894 must_output_die = 1;
14895 else
14897 /* Determine if this block directly contains any "significant"
14898 local declarations which we will need to output DIEs for. */
14899 if (debug_info_level > DINFO_LEVEL_TERSE)
14900 /* We are not in terse mode so *any* local declaration counts
14901 as being a "significant" one. */
14902 must_output_die = ((BLOCK_VARS (stmt) != NULL
14903 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
14904 && (TREE_USED (stmt)
14905 || TREE_ASM_WRITTEN (stmt)
14906 || BLOCK_ABSTRACT (stmt)));
14907 else if ((TREE_USED (stmt)
14908 || TREE_ASM_WRITTEN (stmt)
14909 || BLOCK_ABSTRACT (stmt))
14910 && !dwarf2out_ignore_block (stmt))
14911 must_output_die = 1;
14914 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
14915 DIE for any block which contains no significant local declarations at
14916 all. Rather, in such cases we just call `decls_for_scope' so that any
14917 needed Dwarf info for any sub-blocks will get properly generated. Note
14918 that in terse mode, our definition of what constitutes a "significant"
14919 local declaration gets restricted to include only inlined function
14920 instances and local (nested) function definitions. */
14921 if (must_output_die)
14923 if (inlined_func)
14924 gen_inlined_subroutine_die (stmt, context_die, depth);
14925 else
14926 gen_lexical_block_die (stmt, context_die, depth);
14928 else
14929 decls_for_scope (stmt, context_die, depth);
14932 /* Process variable DECL (or variable with origin ORIGIN) within
14933 block STMT and add it to CONTEXT_DIE. */
14934 static void
14935 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
14937 dw_die_ref die;
14938 tree decl_or_origin = decl ? decl : origin;
14939 tree ultimate_origin = origin ? decl_ultimate_origin (origin) : NULL;
14941 if (ultimate_origin)
14942 origin = ultimate_origin;
14944 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
14945 die = lookup_decl_die (decl_or_origin);
14946 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
14947 && TYPE_DECL_IS_STUB (decl_or_origin))
14948 die = lookup_type_die (TREE_TYPE (decl_or_origin));
14949 else
14950 die = NULL;
14952 if (die != NULL && die->die_parent == NULL)
14953 add_child_die (context_die, die);
14954 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
14955 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
14956 stmt, context_die);
14957 else
14958 gen_decl_die (decl, origin, context_die);
14961 /* Generate all of the decls declared within a given scope and (recursively)
14962 all of its sub-blocks. */
14964 static void
14965 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
14967 tree decl;
14968 unsigned int i;
14969 tree subblocks;
14971 /* Ignore NULL blocks. */
14972 if (stmt == NULL_TREE)
14973 return;
14975 /* Output the DIEs to represent all of the data objects and typedefs
14976 declared directly within this block but not within any nested
14977 sub-blocks. Also, nested function and tag DIEs have been
14978 generated with a parent of NULL; fix that up now. */
14979 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
14980 process_scope_var (stmt, decl, NULL_TREE, context_die);
14981 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
14982 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
14983 context_die);
14985 /* If we're at -g1, we're not interested in subblocks. */
14986 if (debug_info_level <= DINFO_LEVEL_TERSE)
14987 return;
14989 /* Output the DIEs to represent all sub-blocks (and the items declared
14990 therein) of this block. */
14991 for (subblocks = BLOCK_SUBBLOCKS (stmt);
14992 subblocks != NULL;
14993 subblocks = BLOCK_CHAIN (subblocks))
14994 gen_block_die (subblocks, context_die, depth + 1);
14997 /* Is this a typedef we can avoid emitting? */
14999 static inline int
15000 is_redundant_typedef (const_tree decl)
15002 if (TYPE_DECL_IS_STUB (decl))
15003 return 1;
15005 if (DECL_ARTIFICIAL (decl)
15006 && DECL_CONTEXT (decl)
15007 && is_tagged_type (DECL_CONTEXT (decl))
15008 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
15009 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
15010 /* Also ignore the artificial member typedef for the class name. */
15011 return 1;
15013 return 0;
15016 /* Returns the DIE for a context. */
15018 static inline dw_die_ref
15019 get_context_die (tree context)
15021 if (context)
15023 /* Find die that represents this context. */
15024 if (TYPE_P (context))
15025 return force_type_die (context);
15026 else
15027 return force_decl_die (context);
15029 return comp_unit_die;
15032 /* Returns the DIE for decl. A DIE will always be returned. */
15034 static dw_die_ref
15035 force_decl_die (tree decl)
15037 dw_die_ref decl_die;
15038 unsigned saved_external_flag;
15039 tree save_fn = NULL_TREE;
15040 decl_die = lookup_decl_die (decl);
15041 if (!decl_die)
15043 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
15045 decl_die = lookup_decl_die (decl);
15046 if (decl_die)
15047 return decl_die;
15049 switch (TREE_CODE (decl))
15051 case FUNCTION_DECL:
15052 /* Clear current_function_decl, so that gen_subprogram_die thinks
15053 that this is a declaration. At this point, we just want to force
15054 declaration die. */
15055 save_fn = current_function_decl;
15056 current_function_decl = NULL_TREE;
15057 gen_subprogram_die (decl, context_die);
15058 current_function_decl = save_fn;
15059 break;
15061 case VAR_DECL:
15062 /* Set external flag to force declaration die. Restore it after
15063 gen_decl_die() call. */
15064 saved_external_flag = DECL_EXTERNAL (decl);
15065 DECL_EXTERNAL (decl) = 1;
15066 gen_decl_die (decl, NULL, context_die);
15067 DECL_EXTERNAL (decl) = saved_external_flag;
15068 break;
15070 case NAMESPACE_DECL:
15071 dwarf2out_decl (decl);
15072 break;
15074 default:
15075 gcc_unreachable ();
15078 /* We should be able to find the DIE now. */
15079 if (!decl_die)
15080 decl_die = lookup_decl_die (decl);
15081 gcc_assert (decl_die);
15084 return decl_die;
15087 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
15088 always returned. */
15090 static dw_die_ref
15091 force_type_die (tree type)
15093 dw_die_ref type_die;
15095 type_die = lookup_type_die (type);
15096 if (!type_die)
15098 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
15100 type_die = modified_type_die (type, TYPE_READONLY (type),
15101 TYPE_VOLATILE (type), context_die);
15102 gcc_assert (type_die);
15104 return type_die;
15107 /* Force out any required namespaces to be able to output DECL,
15108 and return the new context_die for it, if it's changed. */
15110 static dw_die_ref
15111 setup_namespace_context (tree thing, dw_die_ref context_die)
15113 tree context = (DECL_P (thing)
15114 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
15115 if (context && TREE_CODE (context) == NAMESPACE_DECL)
15116 /* Force out the namespace. */
15117 context_die = force_decl_die (context);
15119 return context_die;
15122 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
15123 type) within its namespace, if appropriate.
15125 For compatibility with older debuggers, namespace DIEs only contain
15126 declarations; all definitions are emitted at CU scope. */
15128 static dw_die_ref
15129 declare_in_namespace (tree thing, dw_die_ref context_die)
15131 dw_die_ref ns_context;
15133 if (debug_info_level <= DINFO_LEVEL_TERSE)
15134 return context_die;
15136 /* If this decl is from an inlined function, then don't try to emit it in its
15137 namespace, as we will get confused. It would have already been emitted
15138 when the abstract instance of the inline function was emitted anyways. */
15139 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
15140 return context_die;
15142 ns_context = setup_namespace_context (thing, context_die);
15144 if (ns_context != context_die)
15146 if (is_fortran ())
15147 return ns_context;
15148 if (DECL_P (thing))
15149 gen_decl_die (thing, NULL, ns_context);
15150 else
15151 gen_type_die (thing, ns_context);
15153 return context_die;
15156 /* Generate a DIE for a namespace or namespace alias. */
15158 static void
15159 gen_namespace_die (tree decl, dw_die_ref context_die)
15161 dw_die_ref namespace_die;
15163 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
15164 they are an alias of. */
15165 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
15167 /* Output a real namespace or module. */
15168 context_die = setup_namespace_context (decl, comp_unit_die);
15169 namespace_die = new_die (is_fortran ()
15170 ? DW_TAG_module : DW_TAG_namespace,
15171 context_die, decl);
15172 /* For Fortran modules defined in different CU don't add src coords. */
15173 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
15174 add_name_attribute (namespace_die, dwarf2_name (decl, 0));
15175 else
15176 add_name_and_src_coords_attributes (namespace_die, decl);
15177 if (DECL_EXTERNAL (decl))
15178 add_AT_flag (namespace_die, DW_AT_declaration, 1);
15179 equate_decl_number_to_die (decl, namespace_die);
15181 else
15183 /* Output a namespace alias. */
15185 /* Force out the namespace we are an alias of, if necessary. */
15186 dw_die_ref origin_die
15187 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
15189 if (DECL_CONTEXT (decl) == NULL_TREE
15190 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
15191 context_die = setup_namespace_context (decl, comp_unit_die);
15192 /* Now create the namespace alias DIE. */
15193 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
15194 add_name_and_src_coords_attributes (namespace_die, decl);
15195 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
15196 equate_decl_number_to_die (decl, namespace_die);
15200 /* Generate Dwarf debug information for a decl described by DECL. */
15202 static void
15203 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
15205 tree decl_or_origin = decl ? decl : origin;
15206 tree class_origin = NULL;
15208 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
15209 return;
15211 switch (TREE_CODE (decl_or_origin))
15213 case ERROR_MARK:
15214 break;
15216 case CONST_DECL:
15217 if (!is_fortran ())
15219 /* The individual enumerators of an enum type get output when we output
15220 the Dwarf representation of the relevant enum type itself. */
15221 break;
15224 /* Emit its type. */
15225 gen_type_die (TREE_TYPE (decl), context_die);
15227 /* And its containing namespace. */
15228 context_die = declare_in_namespace (decl, context_die);
15230 gen_const_die (decl, context_die);
15231 break;
15233 case FUNCTION_DECL:
15234 /* Don't output any DIEs to represent mere function declarations,
15235 unless they are class members or explicit block externs. */
15236 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
15237 && DECL_CONTEXT (decl_or_origin) == NULL_TREE
15238 && (current_function_decl == NULL_TREE
15239 || DECL_ARTIFICIAL (decl_or_origin)))
15240 break;
15242 #if 0
15243 /* FIXME */
15244 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
15245 on local redeclarations of global functions. That seems broken. */
15246 if (current_function_decl != decl)
15247 /* This is only a declaration. */;
15248 #endif
15250 /* If we're emitting a clone, emit info for the abstract instance. */
15251 if (origin || DECL_ORIGIN (decl) != decl)
15252 dwarf2out_abstract_function (origin ? origin : DECL_ABSTRACT_ORIGIN (decl));
15254 /* If we're emitting an out-of-line copy of an inline function,
15255 emit info for the abstract instance and set up to refer to it. */
15256 else if (cgraph_function_possibly_inlined_p (decl)
15257 && ! DECL_ABSTRACT (decl)
15258 && ! class_or_namespace_scope_p (context_die)
15259 /* dwarf2out_abstract_function won't emit a die if this is just
15260 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
15261 that case, because that works only if we have a die. */
15262 && DECL_INITIAL (decl) != NULL_TREE)
15264 dwarf2out_abstract_function (decl);
15265 set_decl_origin_self (decl);
15268 /* Otherwise we're emitting the primary DIE for this decl. */
15269 else if (debug_info_level > DINFO_LEVEL_TERSE)
15271 /* Before we describe the FUNCTION_DECL itself, make sure that we
15272 have described its return type. */
15273 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
15275 /* And its virtual context. */
15276 if (DECL_VINDEX (decl) != NULL_TREE)
15277 gen_type_die (DECL_CONTEXT (decl), context_die);
15279 /* And its containing type. */
15280 if (!origin)
15281 origin = decl_class_context (decl);
15282 if (origin != NULL_TREE)
15283 gen_type_die_for_member (origin, decl, context_die);
15285 /* And its containing namespace. */
15286 context_die = declare_in_namespace (decl, context_die);
15289 /* Now output a DIE to represent the function itself. */
15290 if (decl)
15291 gen_subprogram_die (decl, context_die);
15292 break;
15294 case TYPE_DECL:
15295 /* If we are in terse mode, don't generate any DIEs to represent any
15296 actual typedefs. */
15297 if (debug_info_level <= DINFO_LEVEL_TERSE)
15298 break;
15300 /* In the special case of a TYPE_DECL node representing the declaration
15301 of some type tag, if the given TYPE_DECL is marked as having been
15302 instantiated from some other (original) TYPE_DECL node (e.g. one which
15303 was generated within the original definition of an inline function) we
15304 used to generate a special (abbreviated) DW_TAG_structure_type,
15305 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
15306 should be actually referencing those DIEs, as variable DIEs with that
15307 type would be emitted already in the abstract origin, so it was always
15308 removed during unused type prunning. Don't add anything in this
15309 case. */
15310 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
15311 break;
15313 if (is_redundant_typedef (decl))
15314 gen_type_die (TREE_TYPE (decl), context_die);
15315 else
15316 /* Output a DIE to represent the typedef itself. */
15317 gen_typedef_die (decl, context_die);
15318 break;
15320 case LABEL_DECL:
15321 if (debug_info_level >= DINFO_LEVEL_NORMAL)
15322 gen_label_die (decl, context_die);
15323 break;
15325 case VAR_DECL:
15326 case RESULT_DECL:
15327 /* If we are in terse mode, don't generate any DIEs to represent any
15328 variable declarations or definitions. */
15329 if (debug_info_level <= DINFO_LEVEL_TERSE)
15330 break;
15332 /* Output any DIEs that are needed to specify the type of this data
15333 object. */
15334 if (TREE_CODE (decl_or_origin) == RESULT_DECL
15335 && DECL_BY_REFERENCE (decl_or_origin))
15336 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
15337 else
15338 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
15340 /* And its containing type. */
15341 class_origin = decl_class_context (decl_or_origin);
15342 if (class_origin != NULL_TREE)
15343 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
15345 /* And its containing namespace. */
15346 context_die = declare_in_namespace (decl_or_origin, context_die);
15348 /* Now output the DIE to represent the data object itself. This gets
15349 complicated because of the possibility that the VAR_DECL really
15350 represents an inlined instance of a formal parameter for an inline
15351 function. */
15352 if (!origin)
15353 origin = decl_ultimate_origin (decl);
15354 if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
15355 gen_formal_parameter_die (decl, origin, context_die);
15356 else
15357 gen_variable_die (decl, origin, context_die);
15358 break;
15360 case FIELD_DECL:
15361 /* Ignore the nameless fields that are used to skip bits but handle C++
15362 anonymous unions and structs. */
15363 if (DECL_NAME (decl) != NULL_TREE
15364 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
15365 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
15367 gen_type_die (member_declared_type (decl), context_die);
15368 gen_field_die (decl, context_die);
15370 break;
15372 case PARM_DECL:
15373 if (DECL_BY_REFERENCE (decl_or_origin))
15374 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
15375 else
15376 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
15377 gen_formal_parameter_die (decl, origin, context_die);
15378 break;
15380 case NAMESPACE_DECL:
15381 case IMPORTED_DECL:
15382 gen_namespace_die (decl, context_die);
15383 break;
15385 default:
15386 /* Probably some frontend-internal decl. Assume we don't care. */
15387 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
15388 break;
15392 /* Output debug information for global decl DECL. Called from toplev.c after
15393 compilation proper has finished. */
15395 static void
15396 dwarf2out_global_decl (tree decl)
15398 /* Output DWARF2 information for file-scope tentative data object
15399 declarations, file-scope (extern) function declarations (which
15400 had no corresponding body) and file-scope tagged type declarations
15401 and definitions which have not yet been forced out. */
15402 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
15403 dwarf2out_decl (decl);
15406 /* Output debug information for type decl DECL. Called from toplev.c
15407 and from language front ends (to record built-in types). */
15408 static void
15409 dwarf2out_type_decl (tree decl, int local)
15411 if (!local)
15412 dwarf2out_decl (decl);
15415 /* Output debug information for imported module or decl DECL.
15416 NAME is non-NULL name in the lexical block if the decl has been renamed.
15417 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
15418 that DECL belongs to.
15419 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
15420 static void
15421 dwarf2out_imported_module_or_decl_1 (tree decl,
15422 tree name,
15423 tree lexical_block,
15424 dw_die_ref lexical_block_die)
15426 expanded_location xloc;
15427 dw_die_ref imported_die = NULL;
15428 dw_die_ref at_import_die;
15430 if (TREE_CODE (decl) == IMPORTED_DECL)
15432 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
15433 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
15434 gcc_assert (decl);
15436 else
15437 xloc = expand_location (input_location);
15439 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
15441 if (is_base_type (TREE_TYPE (decl)))
15442 at_import_die = base_type_die (TREE_TYPE (decl));
15443 else
15444 at_import_die = force_type_die (TREE_TYPE (decl));
15445 /* For namespace N { typedef void T; } using N::T; base_type_die
15446 returns NULL, but DW_TAG_imported_declaration requires
15447 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
15448 if (!at_import_die)
15450 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
15451 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
15452 at_import_die = lookup_type_die (TREE_TYPE (decl));
15453 gcc_assert (at_import_die);
15456 else
15458 at_import_die = lookup_decl_die (decl);
15459 if (!at_import_die)
15461 /* If we're trying to avoid duplicate debug info, we may not have
15462 emitted the member decl for this field. Emit it now. */
15463 if (TREE_CODE (decl) == FIELD_DECL)
15465 tree type = DECL_CONTEXT (decl);
15467 if (TYPE_CONTEXT (type)
15468 && TYPE_P (TYPE_CONTEXT (type))
15469 && !should_emit_struct_debug (TYPE_CONTEXT (type),
15470 DINFO_USAGE_DIR_USE))
15471 return;
15472 gen_type_die_for_member (type, decl,
15473 get_context_die (TYPE_CONTEXT (type)));
15475 at_import_die = force_decl_die (decl);
15479 if (TREE_CODE (decl) == NAMESPACE_DECL)
15480 imported_die = new_die (DW_TAG_imported_module,
15481 lexical_block_die,
15482 lexical_block);
15483 else
15484 imported_die = new_die (DW_TAG_imported_declaration,
15485 lexical_block_die,
15486 lexical_block);
15488 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
15489 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
15490 if (name)
15491 add_AT_string (imported_die, DW_AT_name,
15492 IDENTIFIER_POINTER (name));
15493 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
15496 /* Output debug information for imported module or decl DECL.
15497 NAME is non-NULL name in context if the decl has been renamed.
15498 CHILD is true if decl is one of the renamed decls as part of
15499 importing whole module. */
15501 static void
15502 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
15503 bool child)
15505 /* dw_die_ref at_import_die; */
15506 dw_die_ref scope_die;
15508 if (debug_info_level <= DINFO_LEVEL_TERSE)
15509 return;
15511 gcc_assert (decl);
15513 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
15514 We need decl DIE for reference and scope die. First, get DIE for the decl
15515 itself. */
15517 /* Get the scope die for decl context. Use comp_unit_die for global module
15518 or decl. If die is not found for non globals, force new die. */
15519 if (context
15520 && TYPE_P (context)
15521 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
15522 return;
15523 scope_die = get_context_die (context);
15525 if (child)
15527 gcc_assert (scope_die->die_child);
15528 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
15529 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
15530 scope_die = scope_die->die_child;
15533 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
15534 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
15538 /* Write the debugging output for DECL. */
15540 void
15541 dwarf2out_decl (tree decl)
15543 dw_die_ref context_die = comp_unit_die;
15545 switch (TREE_CODE (decl))
15547 case ERROR_MARK:
15548 return;
15550 case FUNCTION_DECL:
15551 /* What we would really like to do here is to filter out all mere
15552 file-scope declarations of file-scope functions which are never
15553 referenced later within this translation unit (and keep all of ones
15554 that *are* referenced later on) but we aren't clairvoyant, so we have
15555 no idea which functions will be referenced in the future (i.e. later
15556 on within the current translation unit). So here we just ignore all
15557 file-scope function declarations which are not also definitions. If
15558 and when the debugger needs to know something about these functions,
15559 it will have to hunt around and find the DWARF information associated
15560 with the definition of the function.
15562 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
15563 nodes represent definitions and which ones represent mere
15564 declarations. We have to check DECL_INITIAL instead. That's because
15565 the C front-end supports some weird semantics for "extern inline"
15566 function definitions. These can get inlined within the current
15567 translation unit (and thus, we need to generate Dwarf info for their
15568 abstract instances so that the Dwarf info for the concrete inlined
15569 instances can have something to refer to) but the compiler never
15570 generates any out-of-lines instances of such things (despite the fact
15571 that they *are* definitions).
15573 The important point is that the C front-end marks these "extern
15574 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
15575 them anyway. Note that the C++ front-end also plays some similar games
15576 for inline function definitions appearing within include files which
15577 also contain `#pragma interface' pragmas. */
15578 if (DECL_INITIAL (decl) == NULL_TREE)
15579 return;
15581 /* If we're a nested function, initially use a parent of NULL; if we're
15582 a plain function, this will be fixed up in decls_for_scope. If
15583 we're a method, it will be ignored, since we already have a DIE. */
15584 if (decl_function_context (decl)
15585 /* But if we're in terse mode, we don't care about scope. */
15586 && debug_info_level > DINFO_LEVEL_TERSE)
15587 context_die = NULL;
15588 break;
15590 case VAR_DECL:
15591 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
15592 declaration and if the declaration was never even referenced from
15593 within this entire compilation unit. We suppress these DIEs in
15594 order to save space in the .debug section (by eliminating entries
15595 which are probably useless). Note that we must not suppress
15596 block-local extern declarations (whether used or not) because that
15597 would screw-up the debugger's name lookup mechanism and cause it to
15598 miss things which really ought to be in scope at a given point. */
15599 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
15600 return;
15602 /* For local statics lookup proper context die. */
15603 if (TREE_STATIC (decl) && decl_function_context (decl))
15604 context_die = lookup_decl_die (DECL_CONTEXT (decl));
15606 /* If we are in terse mode, don't generate any DIEs to represent any
15607 variable declarations or definitions. */
15608 if (debug_info_level <= DINFO_LEVEL_TERSE)
15609 return;
15610 break;
15612 case CONST_DECL:
15613 if (debug_info_level <= DINFO_LEVEL_TERSE)
15614 return;
15615 if (!is_fortran ())
15616 return;
15617 if (TREE_STATIC (decl) && decl_function_context (decl))
15618 context_die = lookup_decl_die (DECL_CONTEXT (decl));
15619 break;
15621 case NAMESPACE_DECL:
15622 case IMPORTED_DECL:
15623 if (debug_info_level <= DINFO_LEVEL_TERSE)
15624 return;
15625 if (lookup_decl_die (decl) != NULL)
15626 return;
15627 break;
15629 case TYPE_DECL:
15630 /* Don't emit stubs for types unless they are needed by other DIEs. */
15631 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
15632 return;
15634 /* Don't bother trying to generate any DIEs to represent any of the
15635 normal built-in types for the language we are compiling. */
15636 if (DECL_IS_BUILTIN (decl))
15638 /* OK, we need to generate one for `bool' so GDB knows what type
15639 comparisons have. */
15640 if (is_cxx ()
15641 && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
15642 && ! DECL_IGNORED_P (decl))
15643 modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
15645 return;
15648 /* If we are in terse mode, don't generate any DIEs for types. */
15649 if (debug_info_level <= DINFO_LEVEL_TERSE)
15650 return;
15652 /* If we're a function-scope tag, initially use a parent of NULL;
15653 this will be fixed up in decls_for_scope. */
15654 if (decl_function_context (decl))
15655 context_die = NULL;
15657 break;
15659 default:
15660 return;
15663 gen_decl_die (decl, NULL, context_die);
15666 /* Output a marker (i.e. a label) for the beginning of the generated code for
15667 a lexical block. */
15669 static void
15670 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
15671 unsigned int blocknum)
15673 switch_to_section (current_function_section ());
15674 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
15677 /* Output a marker (i.e. a label) for the end of the generated code for a
15678 lexical block. */
15680 static void
15681 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
15683 switch_to_section (current_function_section ());
15684 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
15687 /* Returns nonzero if it is appropriate not to emit any debugging
15688 information for BLOCK, because it doesn't contain any instructions.
15690 Don't allow this for blocks with nested functions or local classes
15691 as we would end up with orphans, and in the presence of scheduling
15692 we may end up calling them anyway. */
15694 static bool
15695 dwarf2out_ignore_block (const_tree block)
15697 tree decl;
15698 unsigned int i;
15700 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
15701 if (TREE_CODE (decl) == FUNCTION_DECL
15702 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
15703 return 0;
15704 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
15706 decl = BLOCK_NONLOCALIZED_VAR (block, i);
15707 if (TREE_CODE (decl) == FUNCTION_DECL
15708 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
15709 return 0;
15712 return 1;
15715 /* Hash table routines for file_hash. */
15717 static int
15718 file_table_eq (const void *p1_p, const void *p2_p)
15720 const struct dwarf_file_data *const p1 =
15721 (const struct dwarf_file_data *) p1_p;
15722 const char *const p2 = (const char *) p2_p;
15723 return strcmp (p1->filename, p2) == 0;
15726 static hashval_t
15727 file_table_hash (const void *p_p)
15729 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
15730 return htab_hash_string (p->filename);
15733 /* Lookup FILE_NAME (in the list of filenames that we know about here in
15734 dwarf2out.c) and return its "index". The index of each (known) filename is
15735 just a unique number which is associated with only that one filename. We
15736 need such numbers for the sake of generating labels (in the .debug_sfnames
15737 section) and references to those files numbers (in the .debug_srcinfo
15738 and.debug_macinfo sections). If the filename given as an argument is not
15739 found in our current list, add it to the list and assign it the next
15740 available unique index number. In order to speed up searches, we remember
15741 the index of the filename was looked up last. This handles the majority of
15742 all searches. */
15744 static struct dwarf_file_data *
15745 lookup_filename (const char *file_name)
15747 void ** slot;
15748 struct dwarf_file_data * created;
15750 /* Check to see if the file name that was searched on the previous
15751 call matches this file name. If so, return the index. */
15752 if (file_table_last_lookup
15753 && (file_name == file_table_last_lookup->filename
15754 || strcmp (file_table_last_lookup->filename, file_name) == 0))
15755 return file_table_last_lookup;
15757 /* Didn't match the previous lookup, search the table. */
15758 slot = htab_find_slot_with_hash (file_table, file_name,
15759 htab_hash_string (file_name), INSERT);
15760 if (*slot)
15761 return (struct dwarf_file_data *) *slot;
15763 created = GGC_NEW (struct dwarf_file_data);
15764 created->filename = file_name;
15765 created->emitted_number = 0;
15766 *slot = created;
15767 return created;
15770 /* If the assembler will construct the file table, then translate the compiler
15771 internal file table number into the assembler file table number, and emit
15772 a .file directive if we haven't already emitted one yet. The file table
15773 numbers are different because we prune debug info for unused variables and
15774 types, which may include filenames. */
15776 static int
15777 maybe_emit_file (struct dwarf_file_data * fd)
15779 if (! fd->emitted_number)
15781 if (last_emitted_file)
15782 fd->emitted_number = last_emitted_file->emitted_number + 1;
15783 else
15784 fd->emitted_number = 1;
15785 last_emitted_file = fd;
15787 if (DWARF2_ASM_LINE_DEBUG_INFO)
15789 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
15790 output_quoted_string (asm_out_file,
15791 remap_debug_filename (fd->filename));
15792 fputc ('\n', asm_out_file);
15796 return fd->emitted_number;
15799 /* Called by the final INSN scan whenever we see a var location. We
15800 use it to drop labels in the right places, and throw the location in
15801 our lookup table. */
15803 static void
15804 dwarf2out_var_location (rtx loc_note)
15806 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
15807 struct var_loc_node *newloc;
15808 rtx prev_insn;
15809 static rtx last_insn;
15810 static const char *last_label;
15811 tree decl;
15813 if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
15814 return;
15815 prev_insn = PREV_INSN (loc_note);
15817 newloc = GGC_CNEW (struct var_loc_node);
15818 /* If the insn we processed last time is the previous insn
15819 and it is also a var location note, use the label we emitted
15820 last time. */
15821 if (last_insn != NULL_RTX
15822 && last_insn == prev_insn
15823 && NOTE_P (prev_insn)
15824 && NOTE_KIND (prev_insn) == NOTE_INSN_VAR_LOCATION)
15826 newloc->label = last_label;
15828 else
15830 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
15831 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
15832 loclabel_num++;
15833 newloc->label = ggc_strdup (loclabel);
15835 newloc->var_loc_note = loc_note;
15836 newloc->next = NULL;
15838 if (cfun && in_cold_section_p)
15839 newloc->section_label = crtl->subsections.cold_section_label;
15840 else
15841 newloc->section_label = text_section_label;
15843 last_insn = loc_note;
15844 last_label = newloc->label;
15845 decl = NOTE_VAR_LOCATION_DECL (loc_note);
15846 add_var_loc_to_decl (decl, newloc);
15849 /* We need to reset the locations at the beginning of each
15850 function. We can't do this in the end_function hook, because the
15851 declarations that use the locations won't have been output when
15852 that hook is called. Also compute have_multiple_function_sections here. */
15854 static void
15855 dwarf2out_begin_function (tree fun)
15857 htab_empty (decl_loc_table);
15859 if (function_section (fun) != text_section)
15860 have_multiple_function_sections = true;
15862 dwarf2out_note_section_used ();
15865 /* Output a label to mark the beginning of a source code line entry
15866 and record information relating to this source line, in
15867 'line_info_table' for later output of the .debug_line section. */
15869 static void
15870 dwarf2out_source_line (unsigned int line, const char *filename)
15872 if (debug_info_level >= DINFO_LEVEL_NORMAL
15873 && line != 0)
15875 int file_num = maybe_emit_file (lookup_filename (filename));
15877 switch_to_section (current_function_section ());
15879 /* If requested, emit something human-readable. */
15880 if (flag_debug_asm)
15881 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
15882 filename, line);
15884 if (DWARF2_ASM_LINE_DEBUG_INFO)
15886 /* Emit the .loc directive understood by GNU as. */
15887 fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
15889 /* Indicate that line number info exists. */
15890 line_info_table_in_use++;
15892 else if (function_section (current_function_decl) != text_section)
15894 dw_separate_line_info_ref line_info;
15895 targetm.asm_out.internal_label (asm_out_file,
15896 SEPARATE_LINE_CODE_LABEL,
15897 separate_line_info_table_in_use);
15899 /* Expand the line info table if necessary. */
15900 if (separate_line_info_table_in_use
15901 == separate_line_info_table_allocated)
15903 separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
15904 separate_line_info_table
15905 = GGC_RESIZEVEC (dw_separate_line_info_entry,
15906 separate_line_info_table,
15907 separate_line_info_table_allocated);
15908 memset (separate_line_info_table
15909 + separate_line_info_table_in_use,
15911 (LINE_INFO_TABLE_INCREMENT
15912 * sizeof (dw_separate_line_info_entry)));
15915 /* Add the new entry at the end of the line_info_table. */
15916 line_info
15917 = &separate_line_info_table[separate_line_info_table_in_use++];
15918 line_info->dw_file_num = file_num;
15919 line_info->dw_line_num = line;
15920 line_info->function = current_function_funcdef_no;
15922 else
15924 dw_line_info_ref line_info;
15926 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
15927 line_info_table_in_use);
15929 /* Expand the line info table if necessary. */
15930 if (line_info_table_in_use == line_info_table_allocated)
15932 line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
15933 line_info_table
15934 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
15935 line_info_table_allocated);
15936 memset (line_info_table + line_info_table_in_use, 0,
15937 LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
15940 /* Add the new entry at the end of the line_info_table. */
15941 line_info = &line_info_table[line_info_table_in_use++];
15942 line_info->dw_file_num = file_num;
15943 line_info->dw_line_num = line;
15948 /* Record the beginning of a new source file. */
15950 static void
15951 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
15953 if (flag_eliminate_dwarf2_dups)
15955 /* Record the beginning of the file for break_out_includes. */
15956 dw_die_ref bincl_die;
15958 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
15959 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
15962 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15964 int file_num = maybe_emit_file (lookup_filename (filename));
15966 switch_to_section (debug_macinfo_section);
15967 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
15968 dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
15969 lineno);
15971 dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
15975 /* Record the end of a source file. */
15977 static void
15978 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
15980 if (flag_eliminate_dwarf2_dups)
15981 /* Record the end of the file for break_out_includes. */
15982 new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
15984 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15986 switch_to_section (debug_macinfo_section);
15987 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
15991 /* Called from debug_define in toplev.c. The `buffer' parameter contains
15992 the tail part of the directive line, i.e. the part which is past the
15993 initial whitespace, #, whitespace, directive-name, whitespace part. */
15995 static void
15996 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
15997 const char *buffer ATTRIBUTE_UNUSED)
15999 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16001 switch_to_section (debug_macinfo_section);
16002 dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
16003 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
16004 dw2_asm_output_nstring (buffer, -1, "The macro");
16008 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
16009 the tail part of the directive line, i.e. the part which is past the
16010 initial whitespace, #, whitespace, directive-name, whitespace part. */
16012 static void
16013 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
16014 const char *buffer ATTRIBUTE_UNUSED)
16016 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16018 switch_to_section (debug_macinfo_section);
16019 dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
16020 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
16021 dw2_asm_output_nstring (buffer, -1, "The macro");
16025 /* Set up for Dwarf output at the start of compilation. */
16027 static void
16028 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
16030 /* Allocate the file_table. */
16031 file_table = htab_create_ggc (50, file_table_hash,
16032 file_table_eq, NULL);
16034 /* Allocate the decl_die_table. */
16035 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
16036 decl_die_table_eq, NULL);
16038 /* Allocate the decl_loc_table. */
16039 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
16040 decl_loc_table_eq, NULL);
16042 /* Allocate the initial hunk of the decl_scope_table. */
16043 decl_scope_table = VEC_alloc (tree, gc, 256);
16045 /* Allocate the initial hunk of the abbrev_die_table. */
16046 abbrev_die_table = GGC_CNEWVEC (dw_die_ref, ABBREV_DIE_TABLE_INCREMENT);
16047 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
16048 /* Zero-th entry is allocated, but unused. */
16049 abbrev_die_table_in_use = 1;
16051 /* Allocate the initial hunk of the line_info_table. */
16052 line_info_table = GGC_CNEWVEC (dw_line_info_entry, LINE_INFO_TABLE_INCREMENT);
16053 line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
16055 /* Zero-th entry is allocated, but unused. */
16056 line_info_table_in_use = 1;
16058 /* Allocate the pubtypes and pubnames vectors. */
16059 pubname_table = VEC_alloc (pubname_entry, gc, 32);
16060 pubtype_table = VEC_alloc (pubname_entry, gc, 32);
16062 /* Generate the initial DIE for the .debug section. Note that the (string)
16063 value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
16064 will (typically) be a relative pathname and that this pathname should be
16065 taken as being relative to the directory from which the compiler was
16066 invoked when the given (base) source file was compiled. We will fill
16067 in this value in dwarf2out_finish. */
16068 comp_unit_die = gen_compile_unit_die (NULL);
16070 incomplete_types = VEC_alloc (tree, gc, 64);
16072 used_rtx_array = VEC_alloc (rtx, gc, 32);
16074 debug_info_section = get_section (DEBUG_INFO_SECTION,
16075 SECTION_DEBUG, NULL);
16076 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
16077 SECTION_DEBUG, NULL);
16078 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
16079 SECTION_DEBUG, NULL);
16080 debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
16081 SECTION_DEBUG, NULL);
16082 debug_line_section = get_section (DEBUG_LINE_SECTION,
16083 SECTION_DEBUG, NULL);
16084 debug_loc_section = get_section (DEBUG_LOC_SECTION,
16085 SECTION_DEBUG, NULL);
16086 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
16087 SECTION_DEBUG, NULL);
16088 #ifdef DEBUG_PUBTYPES_SECTION
16089 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
16090 SECTION_DEBUG, NULL);
16091 #endif
16092 debug_str_section = get_section (DEBUG_STR_SECTION,
16093 DEBUG_STR_SECTION_FLAGS, NULL);
16094 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
16095 SECTION_DEBUG, NULL);
16096 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
16097 SECTION_DEBUG, NULL);
16099 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
16100 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
16101 DEBUG_ABBREV_SECTION_LABEL, 0);
16102 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
16103 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
16104 COLD_TEXT_SECTION_LABEL, 0);
16105 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
16107 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
16108 DEBUG_INFO_SECTION_LABEL, 0);
16109 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
16110 DEBUG_LINE_SECTION_LABEL, 0);
16111 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
16112 DEBUG_RANGES_SECTION_LABEL, 0);
16113 switch_to_section (debug_abbrev_section);
16114 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
16115 switch_to_section (debug_info_section);
16116 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
16117 switch_to_section (debug_line_section);
16118 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
16120 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16122 switch_to_section (debug_macinfo_section);
16123 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
16124 DEBUG_MACINFO_SECTION_LABEL, 0);
16125 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
16128 switch_to_section (text_section);
16129 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
16130 if (flag_reorder_blocks_and_partition)
16132 cold_text_section = unlikely_text_section ();
16133 switch_to_section (cold_text_section);
16134 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
16138 /* A helper function for dwarf2out_finish called through
16139 ht_forall. Emit one queued .debug_str string. */
16141 static int
16142 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
16144 struct indirect_string_node *node = (struct indirect_string_node *) *h;
16146 if (node->form == DW_FORM_strp)
16148 switch_to_section (debug_str_section);
16149 ASM_OUTPUT_LABEL (asm_out_file, node->label);
16150 assemble_string (node->str, strlen (node->str) + 1);
16153 return 1;
16156 #if ENABLE_ASSERT_CHECKING
16157 /* Verify that all marks are clear. */
16159 static void
16160 verify_marks_clear (dw_die_ref die)
16162 dw_die_ref c;
16164 gcc_assert (! die->die_mark);
16165 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
16167 #endif /* ENABLE_ASSERT_CHECKING */
16169 /* Clear the marks for a die and its children.
16170 Be cool if the mark isn't set. */
16172 static void
16173 prune_unmark_dies (dw_die_ref die)
16175 dw_die_ref c;
16177 if (die->die_mark)
16178 die->die_mark = 0;
16179 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
16182 /* Given DIE that we're marking as used, find any other dies
16183 it references as attributes and mark them as used. */
16185 static void
16186 prune_unused_types_walk_attribs (dw_die_ref die)
16188 dw_attr_ref a;
16189 unsigned ix;
16191 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
16193 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
16195 /* A reference to another DIE.
16196 Make sure that it will get emitted. */
16197 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
16199 /* Set the string's refcount to 0 so that prune_unused_types_mark
16200 accounts properly for it. */
16201 if (AT_class (a) == dw_val_class_str)
16202 a->dw_attr_val.v.val_str->refcount = 0;
16207 /* Mark DIE as being used. If DOKIDS is true, then walk down
16208 to DIE's children. */
16210 static void
16211 prune_unused_types_mark (dw_die_ref die, int dokids)
16213 dw_die_ref c;
16215 if (die->die_mark == 0)
16217 /* We haven't done this node yet. Mark it as used. */
16218 die->die_mark = 1;
16220 /* We also have to mark its parents as used.
16221 (But we don't want to mark our parents' kids due to this.) */
16222 if (die->die_parent)
16223 prune_unused_types_mark (die->die_parent, 0);
16225 /* Mark any referenced nodes. */
16226 prune_unused_types_walk_attribs (die);
16228 /* If this node is a specification,
16229 also mark the definition, if it exists. */
16230 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
16231 prune_unused_types_mark (die->die_definition, 1);
16234 if (dokids && die->die_mark != 2)
16236 /* We need to walk the children, but haven't done so yet.
16237 Remember that we've walked the kids. */
16238 die->die_mark = 2;
16240 /* If this is an array type, we need to make sure our
16241 kids get marked, even if they're types. */
16242 if (die->die_tag == DW_TAG_array_type)
16243 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
16244 else
16245 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
16249 /* For local classes, look if any static member functions were emitted
16250 and if so, mark them. */
16252 static void
16253 prune_unused_types_walk_local_classes (dw_die_ref die)
16255 dw_die_ref c;
16257 if (die->die_mark == 2)
16258 return;
16260 switch (die->die_tag)
16262 case DW_TAG_structure_type:
16263 case DW_TAG_union_type:
16264 case DW_TAG_class_type:
16265 break;
16267 case DW_TAG_subprogram:
16268 if (!get_AT_flag (die, DW_AT_declaration)
16269 || die->die_definition != NULL)
16270 prune_unused_types_mark (die, 1);
16271 return;
16273 default:
16274 return;
16277 /* Mark children. */
16278 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
16281 /* Walk the tree DIE and mark types that we actually use. */
16283 static void
16284 prune_unused_types_walk (dw_die_ref die)
16286 dw_die_ref c;
16288 /* Don't do anything if this node is already marked and
16289 children have been marked as well. */
16290 if (die->die_mark == 2)
16291 return;
16293 switch (die->die_tag)
16295 case DW_TAG_structure_type:
16296 case DW_TAG_union_type:
16297 case DW_TAG_class_type:
16298 if (die->die_perennial_p)
16299 break;
16301 for (c = die->die_parent; c; c = c->die_parent)
16302 if (c->die_tag == DW_TAG_subprogram)
16303 break;
16305 /* Finding used static member functions inside of classes
16306 is needed just for local classes, because for other classes
16307 static member function DIEs with DW_AT_specification
16308 are emitted outside of the DW_TAG_*_type. If we ever change
16309 it, we'd need to call this even for non-local classes. */
16310 if (c)
16311 prune_unused_types_walk_local_classes (die);
16313 /* It's a type node --- don't mark it. */
16314 return;
16316 case DW_TAG_const_type:
16317 case DW_TAG_packed_type:
16318 case DW_TAG_pointer_type:
16319 case DW_TAG_reference_type:
16320 case DW_TAG_volatile_type:
16321 case DW_TAG_typedef:
16322 case DW_TAG_array_type:
16323 case DW_TAG_interface_type:
16324 case DW_TAG_friend:
16325 case DW_TAG_variant_part:
16326 case DW_TAG_enumeration_type:
16327 case DW_TAG_subroutine_type:
16328 case DW_TAG_string_type:
16329 case DW_TAG_set_type:
16330 case DW_TAG_subrange_type:
16331 case DW_TAG_ptr_to_member_type:
16332 case DW_TAG_file_type:
16333 if (die->die_perennial_p)
16334 break;
16336 /* It's a type node --- don't mark it. */
16337 return;
16339 default:
16340 /* Mark everything else. */
16341 break;
16344 if (die->die_mark == 0)
16346 die->die_mark = 1;
16348 /* Now, mark any dies referenced from here. */
16349 prune_unused_types_walk_attribs (die);
16352 die->die_mark = 2;
16354 /* Mark children. */
16355 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
16358 /* Increment the string counts on strings referred to from DIE's
16359 attributes. */
16361 static void
16362 prune_unused_types_update_strings (dw_die_ref die)
16364 dw_attr_ref a;
16365 unsigned ix;
16367 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
16368 if (AT_class (a) == dw_val_class_str)
16370 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
16371 s->refcount++;
16372 /* Avoid unnecessarily putting strings that are used less than
16373 twice in the hash table. */
16374 if (s->refcount
16375 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
16377 void ** slot;
16378 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
16379 htab_hash_string (s->str),
16380 INSERT);
16381 gcc_assert (*slot == NULL);
16382 *slot = s;
16387 /* Remove from the tree DIE any dies that aren't marked. */
16389 static void
16390 prune_unused_types_prune (dw_die_ref die)
16392 dw_die_ref c;
16394 gcc_assert (die->die_mark);
16395 prune_unused_types_update_strings (die);
16397 if (! die->die_child)
16398 return;
16400 c = die->die_child;
16401 do {
16402 dw_die_ref prev = c;
16403 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
16404 if (c == die->die_child)
16406 /* No marked children between 'prev' and the end of the list. */
16407 if (prev == c)
16408 /* No marked children at all. */
16409 die->die_child = NULL;
16410 else
16412 prev->die_sib = c->die_sib;
16413 die->die_child = prev;
16415 return;
16418 if (c != prev->die_sib)
16419 prev->die_sib = c;
16420 prune_unused_types_prune (c);
16421 } while (c != die->die_child);
16425 /* Remove dies representing declarations that we never use. */
16427 static void
16428 prune_unused_types (void)
16430 unsigned int i;
16431 limbo_die_node *node;
16432 pubname_ref pub;
16434 #if ENABLE_ASSERT_CHECKING
16435 /* All the marks should already be clear. */
16436 verify_marks_clear (comp_unit_die);
16437 for (node = limbo_die_list; node; node = node->next)
16438 verify_marks_clear (node->die);
16439 #endif /* ENABLE_ASSERT_CHECKING */
16441 /* Set the mark on nodes that are actually used. */
16442 prune_unused_types_walk (comp_unit_die);
16443 for (node = limbo_die_list; node; node = node->next)
16444 prune_unused_types_walk (node->die);
16446 /* Also set the mark on nodes referenced from the
16447 pubname_table or arange_table. */
16448 for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
16449 prune_unused_types_mark (pub->die, 1);
16450 for (i = 0; i < arange_table_in_use; i++)
16451 prune_unused_types_mark (arange_table[i], 1);
16453 /* Get rid of nodes that aren't marked; and update the string counts. */
16454 if (debug_str_hash)
16455 htab_empty (debug_str_hash);
16456 prune_unused_types_prune (comp_unit_die);
16457 for (node = limbo_die_list; node; node = node->next)
16458 prune_unused_types_prune (node->die);
16460 /* Leave the marks clear. */
16461 prune_unmark_dies (comp_unit_die);
16462 for (node = limbo_die_list; node; node = node->next)
16463 prune_unmark_dies (node->die);
16466 /* Set the parameter to true if there are any relative pathnames in
16467 the file table. */
16468 static int
16469 file_table_relative_p (void ** slot, void *param)
16471 bool *p = (bool *) param;
16472 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
16473 if (!IS_ABSOLUTE_PATH (d->filename))
16475 *p = true;
16476 return 0;
16478 return 1;
16481 /* Output stuff that dwarf requires at the end of every file,
16482 and generate the DWARF-2 debugging info. */
16484 static void
16485 dwarf2out_finish (const char *filename)
16487 limbo_die_node *node, *next_node;
16488 dw_die_ref die = 0;
16489 unsigned int i;
16491 /* Add the name for the main input file now. We delayed this from
16492 dwarf2out_init to avoid complications with PCH. */
16493 add_name_attribute (comp_unit_die, remap_debug_filename (filename));
16494 if (!IS_ABSOLUTE_PATH (filename))
16495 add_comp_dir_attribute (comp_unit_die);
16496 else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
16498 bool p = false;
16499 htab_traverse (file_table, file_table_relative_p, &p);
16500 if (p)
16501 add_comp_dir_attribute (comp_unit_die);
16504 for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
16506 add_location_or_const_value_attribute (
16507 VEC_index (deferred_locations, deferred_locations_list, i)->die,
16508 VEC_index (deferred_locations, deferred_locations_list, i)->variable,
16509 DW_AT_location);
16512 /* Traverse the limbo die list, and add parent/child links. The only
16513 dies without parents that should be here are concrete instances of
16514 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
16515 For concrete instances, we can get the parent die from the abstract
16516 instance. */
16517 for (node = limbo_die_list; node; node = next_node)
16519 next_node = node->next;
16520 die = node->die;
16522 if (die->die_parent == NULL)
16524 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
16526 if (origin)
16527 add_child_die (origin->die_parent, die);
16528 else if (die == comp_unit_die)
16530 else if (errorcount > 0 || sorrycount > 0)
16531 /* It's OK to be confused by errors in the input. */
16532 add_child_die (comp_unit_die, die);
16533 else
16535 /* In certain situations, the lexical block containing a
16536 nested function can be optimized away, which results
16537 in the nested function die being orphaned. Likewise
16538 with the return type of that nested function. Force
16539 this to be a child of the containing function.
16541 It may happen that even the containing function got fully
16542 inlined and optimized out. In that case we are lost and
16543 assign the empty child. This should not be big issue as
16544 the function is likely unreachable too. */
16545 tree context = NULL_TREE;
16547 gcc_assert (node->created_for);
16549 if (DECL_P (node->created_for))
16550 context = DECL_CONTEXT (node->created_for);
16551 else if (TYPE_P (node->created_for))
16552 context = TYPE_CONTEXT (node->created_for);
16554 gcc_assert (context
16555 && (TREE_CODE (context) == FUNCTION_DECL
16556 || TREE_CODE (context) == NAMESPACE_DECL));
16558 origin = lookup_decl_die (context);
16559 if (origin)
16560 add_child_die (origin, die);
16561 else
16562 add_child_die (comp_unit_die, die);
16567 limbo_die_list = NULL;
16569 /* Walk through the list of incomplete types again, trying once more to
16570 emit full debugging info for them. */
16571 retry_incomplete_types ();
16573 if (flag_eliminate_unused_debug_types)
16574 prune_unused_types ();
16576 /* Generate separate CUs for each of the include files we've seen.
16577 They will go into limbo_die_list. */
16578 if (flag_eliminate_dwarf2_dups)
16579 break_out_includes (comp_unit_die);
16581 /* Traverse the DIE's and add add sibling attributes to those DIE's
16582 that have children. */
16583 add_sibling_attributes (comp_unit_die);
16584 for (node = limbo_die_list; node; node = node->next)
16585 add_sibling_attributes (node->die);
16587 /* Output a terminator label for the .text section. */
16588 switch_to_section (text_section);
16589 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
16590 if (flag_reorder_blocks_and_partition)
16592 switch_to_section (unlikely_text_section ());
16593 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
16596 /* We can only use the low/high_pc attributes if all of the code was
16597 in .text. */
16598 if (!have_multiple_function_sections)
16600 add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
16601 add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
16604 else
16606 unsigned fde_idx = 0;
16608 /* We need to give .debug_loc and .debug_ranges an appropriate
16609 "base address". Use zero so that these addresses become
16610 absolute. Historically, we've emitted the unexpected
16611 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
16612 Emit both to give time for other tools to adapt. */
16613 add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
16614 add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
16616 add_AT_range_list (comp_unit_die, DW_AT_ranges,
16617 add_ranges_by_labels (text_section_label,
16618 text_end_label));
16619 if (flag_reorder_blocks_and_partition)
16620 add_ranges_by_labels (cold_text_section_label,
16621 cold_end_label);
16623 for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
16625 dw_fde_ref fde = &fde_table[fde_idx];
16627 if (fde->dw_fde_switched_sections)
16629 add_ranges_by_labels (fde->dw_fde_hot_section_label,
16630 fde->dw_fde_hot_section_end_label);
16631 add_ranges_by_labels (fde->dw_fde_unlikely_section_label,
16632 fde->dw_fde_unlikely_section_end_label);
16634 else
16635 add_ranges_by_labels (fde->dw_fde_begin,
16636 fde->dw_fde_end);
16639 add_ranges (NULL);
16642 /* Output location list section if necessary. */
16643 if (have_location_lists)
16645 /* Output the location lists info. */
16646 switch_to_section (debug_loc_section);
16647 ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
16648 DEBUG_LOC_SECTION_LABEL, 0);
16649 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
16650 output_location_lists (die);
16653 if (debug_info_level >= DINFO_LEVEL_NORMAL)
16654 add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
16655 debug_line_section_label);
16657 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16658 add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
16660 /* Output all of the compilation units. We put the main one last so that
16661 the offsets are available to output_pubnames. */
16662 for (node = limbo_die_list; node; node = node->next)
16663 output_comp_unit (node->die, 0);
16665 /* Output the main compilation unit if non-empty or if .debug_macinfo
16666 has been emitted. */
16667 output_comp_unit (comp_unit_die, debug_info_level >= DINFO_LEVEL_VERBOSE);
16669 /* Output the abbreviation table. */
16670 switch_to_section (debug_abbrev_section);
16671 output_abbrev_section ();
16673 /* Output public names table if necessary. */
16674 if (!VEC_empty (pubname_entry, pubname_table))
16676 switch_to_section (debug_pubnames_section);
16677 output_pubnames (pubname_table);
16680 #ifdef DEBUG_PUBTYPES_SECTION
16681 /* Output public types table if necessary. */
16682 if (!VEC_empty (pubname_entry, pubtype_table))
16684 switch_to_section (debug_pubtypes_section);
16685 output_pubnames (pubtype_table);
16687 #endif
16689 /* Output the address range information. We only put functions in the arange
16690 table, so don't write it out if we don't have any. */
16691 if (fde_table_in_use)
16693 switch_to_section (debug_aranges_section);
16694 output_aranges ();
16697 /* Output ranges section if necessary. */
16698 if (ranges_table_in_use)
16700 switch_to_section (debug_ranges_section);
16701 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
16702 output_ranges ();
16705 /* Output the source line correspondence table. We must do this
16706 even if there is no line information. Otherwise, on an empty
16707 translation unit, we will generate a present, but empty,
16708 .debug_info section. IRIX 6.5 `nm' will then complain when
16709 examining the file. This is done late so that any filenames
16710 used by the debug_info section are marked as 'used'. */
16711 if (! DWARF2_ASM_LINE_DEBUG_INFO)
16713 switch_to_section (debug_line_section);
16714 output_line_info ();
16717 /* Have to end the macro section. */
16718 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16720 switch_to_section (debug_macinfo_section);
16721 dw2_asm_output_data (1, 0, "End compilation unit");
16724 /* If we emitted any DW_FORM_strp form attribute, output the string
16725 table too. */
16726 if (debug_str_hash)
16727 htab_traverse (debug_str_hash, output_indirect_string, NULL);
16729 #else
16731 /* This should never be used, but its address is needed for comparisons. */
16732 const struct gcc_debug_hooks dwarf2_debug_hooks;
16734 #endif /* DWARF2_DEBUGGING_INFO */
16736 #include "gt-dwarf2out.h"