include cstring as well
[official-gcc.git] / gcc / dwarf2out.c
blob8c9d028d2f64681880a81e094bd2458675fa1c55
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
4 Contributed by Gary Funck (gary@intrepid.com).
5 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
6 Extensively modified by Jason Merrill (jason@cygnus.com).
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 2, or (at your option) any later
13 version.
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING. If not, write to the Free
22 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
23 02110-1301, USA. */
25 /* TODO: Emit .debug_line header even when there are no functions, since
26 the file numbers are used by .debug_info. Alternately, leave
27 out locations for types and decls.
28 Avoid talking about ctors and op= for PODs.
29 Factor out common prologue sequences into multiple CIEs. */
31 /* The first part of this file deals with the DWARF 2 frame unwind
32 information, which is also used by the GCC efficient exception handling
33 mechanism. The second part, controlled only by an #ifdef
34 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
35 information. */
37 #include "config.h"
38 #include "system.h"
39 #include "coretypes.h"
40 #include "tm.h"
41 #include "tree.h"
42 #include "version.h"
43 #include "flags.h"
44 #include "real.h"
45 #include "rtl.h"
46 #include "hard-reg-set.h"
47 #include "regs.h"
48 #include "insn-config.h"
49 #include "reload.h"
50 #include "function.h"
51 #include "output.h"
52 #include "expr.h"
53 #include "libfuncs.h"
54 #include "except.h"
55 #include "dwarf2.h"
56 #include "dwarf2out.h"
57 #include "dwarf2asm.h"
58 #include "toplev.h"
59 #include "varray.h"
60 #include "ggc.h"
61 #include "md5.h"
62 #include "tm_p.h"
63 #include "diagnostic.h"
64 #include "debug.h"
65 #include "target.h"
66 #include "langhooks.h"
67 #include "hashtab.h"
68 #include "cgraph.h"
69 #include "input.h"
71 #ifdef DWARF2_DEBUGGING_INFO
72 static void dwarf2out_source_line (unsigned int, const char *);
73 #endif
75 /* DWARF2 Abbreviation Glossary:
76 CFA = Canonical Frame Address
77 a fixed address on the stack which identifies a call frame.
78 We define it to be the value of SP just before the call insn.
79 The CFA register and offset, which may change during the course
80 of the function, are used to calculate its value at runtime.
81 CFI = Call Frame Instruction
82 an instruction for the DWARF2 abstract machine
83 CIE = Common Information Entry
84 information describing information common to one or more FDEs
85 DIE = Debugging Information Entry
86 FDE = Frame Description Entry
87 information describing the stack call frame, in particular,
88 how to restore registers
90 DW_CFA_... = DWARF2 CFA call frame instruction
91 DW_TAG_... = DWARF2 DIE tag */
93 #ifndef DWARF2_FRAME_INFO
94 # ifdef DWARF2_DEBUGGING_INFO
95 # define DWARF2_FRAME_INFO \
96 (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
97 # else
98 # define DWARF2_FRAME_INFO 0
99 # endif
100 #endif
102 /* Map register numbers held in the call frame info that gcc has
103 collected using DWARF_FRAME_REGNUM to those that should be output in
104 .debug_frame and .eh_frame. */
105 #ifndef DWARF2_FRAME_REG_OUT
106 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
107 #endif
109 /* Decide whether we want to emit frame unwind information for the current
110 translation unit. */
113 dwarf2out_do_frame (void)
115 /* We want to emit correct CFA location expressions or lists, so we
116 have to return true if we're going to output debug info, even if
117 we're not going to output frame or unwind info. */
118 return (write_symbols == DWARF2_DEBUG
119 || write_symbols == VMS_AND_DWARF2_DEBUG
120 || DWARF2_FRAME_INFO
121 #ifdef DWARF2_UNWIND_INFO
122 || (DWARF2_UNWIND_INFO
123 && (flag_unwind_tables
124 || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)))
125 #endif
129 /* The size of the target's pointer type. */
130 #ifndef PTR_SIZE
131 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
132 #endif
134 /* Array of RTXes referenced by the debugging information, which therefore
135 must be kept around forever. */
136 static GTY(()) VEC(rtx,gc) *used_rtx_array;
138 /* A pointer to the base of a list of incomplete types which might be
139 completed at some later time. incomplete_types_list needs to be a
140 VEC(tree,gc) because we want to tell the garbage collector about
141 it. */
142 static GTY(()) VEC(tree,gc) *incomplete_types;
144 /* A pointer to the base of a table of references to declaration
145 scopes. This table is a display which tracks the nesting
146 of declaration scopes at the current scope and containing
147 scopes. This table is used to find the proper place to
148 define type declaration DIE's. */
149 static GTY(()) VEC(tree,gc) *decl_scope_table;
151 /* Pointers to various DWARF2 sections. */
152 static GTY(()) section *debug_info_section;
153 static GTY(()) section *debug_abbrev_section;
154 static GTY(()) section *debug_aranges_section;
155 static GTY(()) section *debug_macinfo_section;
156 static GTY(()) section *debug_line_section;
157 static GTY(()) section *debug_loc_section;
158 static GTY(()) section *debug_pubnames_section;
159 static GTY(()) section *debug_pubtypes_section;
160 static GTY(()) section *debug_str_section;
161 static GTY(()) section *debug_ranges_section;
162 static GTY(()) section *debug_frame_section;
164 /* How to start an assembler comment. */
165 #ifndef ASM_COMMENT_START
166 #define ASM_COMMENT_START ";#"
167 #endif
169 typedef struct dw_cfi_struct *dw_cfi_ref;
170 typedef struct dw_fde_struct *dw_fde_ref;
171 typedef union dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
173 /* Call frames are described using a sequence of Call Frame
174 Information instructions. The register number, offset
175 and address fields are provided as possible operands;
176 their use is selected by the opcode field. */
178 enum dw_cfi_oprnd_type {
179 dw_cfi_oprnd_unused,
180 dw_cfi_oprnd_reg_num,
181 dw_cfi_oprnd_offset,
182 dw_cfi_oprnd_addr,
183 dw_cfi_oprnd_loc
186 typedef union dw_cfi_oprnd_struct GTY(())
188 unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
189 HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
190 const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
191 struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
193 dw_cfi_oprnd;
195 typedef struct dw_cfi_struct GTY(())
197 dw_cfi_ref dw_cfi_next;
198 enum dwarf_call_frame_info dw_cfi_opc;
199 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
200 dw_cfi_oprnd1;
201 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
202 dw_cfi_oprnd2;
204 dw_cfi_node;
206 /* This is how we define the location of the CFA. We use to handle it
207 as REG + OFFSET all the time, but now it can be more complex.
208 It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
209 Instead of passing around REG and OFFSET, we pass a copy
210 of this structure. */
211 typedef struct cfa_loc GTY(())
213 HOST_WIDE_INT offset;
214 HOST_WIDE_INT base_offset;
215 unsigned int reg;
216 int indirect; /* 1 if CFA is accessed via a dereference. */
217 } dw_cfa_location;
219 /* All call frame descriptions (FDE's) in the GCC generated DWARF
220 refer to a single Common Information Entry (CIE), defined at
221 the beginning of the .debug_frame section. This use of a single
222 CIE obviates the need to keep track of multiple CIE's
223 in the DWARF generation routines below. */
225 typedef struct dw_fde_struct GTY(())
227 tree decl;
228 const char *dw_fde_begin;
229 const char *dw_fde_current_label;
230 const char *dw_fde_end;
231 const char *dw_fde_hot_section_label;
232 const char *dw_fde_hot_section_end_label;
233 const char *dw_fde_unlikely_section_label;
234 const char *dw_fde_unlikely_section_end_label;
235 bool dw_fde_switched_sections;
236 dw_cfi_ref dw_fde_cfi;
237 unsigned funcdef_number;
238 unsigned all_throwers_are_sibcalls : 1;
239 unsigned nothrow : 1;
240 unsigned uses_eh_lsda : 1;
242 dw_fde_node;
244 /* Maximum size (in bytes) of an artificially generated label. */
245 #define MAX_ARTIFICIAL_LABEL_BYTES 30
247 /* The size of addresses as they appear in the Dwarf 2 data.
248 Some architectures use word addresses to refer to code locations,
249 but Dwarf 2 info always uses byte addresses. On such machines,
250 Dwarf 2 addresses need to be larger than the architecture's
251 pointers. */
252 #ifndef DWARF2_ADDR_SIZE
253 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
254 #endif
256 /* The size in bytes of a DWARF field indicating an offset or length
257 relative to a debug info section, specified to be 4 bytes in the
258 DWARF-2 specification. The SGI/MIPS ABI defines it to be the same
259 as PTR_SIZE. */
261 #ifndef DWARF_OFFSET_SIZE
262 #define DWARF_OFFSET_SIZE 4
263 #endif
265 /* According to the (draft) DWARF 3 specification, the initial length
266 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
267 bytes are 0xffffffff, followed by the length stored in the next 8
268 bytes.
270 However, the SGI/MIPS ABI uses an initial length which is equal to
271 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
273 #ifndef DWARF_INITIAL_LENGTH_SIZE
274 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
275 #endif
277 #define DWARF_VERSION 2
279 /* Round SIZE up to the nearest BOUNDARY. */
280 #define DWARF_ROUND(SIZE,BOUNDARY) \
281 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
283 /* Offsets recorded in opcodes are a multiple of this alignment factor. */
284 #ifndef DWARF_CIE_DATA_ALIGNMENT
285 #ifdef STACK_GROWS_DOWNWARD
286 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
287 #else
288 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
289 #endif
290 #endif
292 /* CIE identifier. */
293 #if HOST_BITS_PER_WIDE_INT >= 64
294 #define DWARF_CIE_ID \
295 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
296 #else
297 #define DWARF_CIE_ID DW_CIE_ID
298 #endif
300 /* A pointer to the base of a table that contains frame description
301 information for each routine. */
302 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
304 /* Number of elements currently allocated for fde_table. */
305 static GTY(()) unsigned fde_table_allocated;
307 /* Number of elements in fde_table currently in use. */
308 static GTY(()) unsigned fde_table_in_use;
310 /* Size (in elements) of increments by which we may expand the
311 fde_table. */
312 #define FDE_TABLE_INCREMENT 256
314 /* A list of call frame insns for the CIE. */
315 static GTY(()) dw_cfi_ref cie_cfi_head;
317 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
318 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
319 attribute that accelerates the lookup of the FDE associated
320 with the subprogram. This variable holds the table index of the FDE
321 associated with the current function (body) definition. */
322 static unsigned current_funcdef_fde;
323 #endif
325 struct indirect_string_node GTY(())
327 const char *str;
328 unsigned int refcount;
329 unsigned int form;
330 char *label;
333 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
335 static GTY(()) int dw2_string_counter;
336 static GTY(()) unsigned long dwarf2out_cfi_label_num;
338 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
340 /* Forward declarations for functions defined in this file. */
342 static char *stripattributes (const char *);
343 static const char *dwarf_cfi_name (unsigned);
344 static dw_cfi_ref new_cfi (void);
345 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
346 static void add_fde_cfi (const char *, dw_cfi_ref);
347 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *);
348 static void lookup_cfa (dw_cfa_location *);
349 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
350 static void initial_return_save (rtx);
351 static HOST_WIDE_INT stack_adjust_offset (rtx);
352 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
353 static void output_call_frame_info (int);
354 static void dwarf2out_stack_adjust (rtx, bool);
355 static void flush_queued_reg_saves (void);
356 static bool clobbers_queued_reg_save (rtx);
357 static void dwarf2out_frame_debug_expr (rtx, const char *);
359 /* Support for complex CFA locations. */
360 static void output_cfa_loc (dw_cfi_ref);
361 static void get_cfa_from_loc_descr (dw_cfa_location *,
362 struct dw_loc_descr_struct *);
363 static struct dw_loc_descr_struct *build_cfa_loc
364 (dw_cfa_location *, HOST_WIDE_INT);
365 static void def_cfa_1 (const char *, dw_cfa_location *);
367 /* How to start an assembler comment. */
368 #ifndef ASM_COMMENT_START
369 #define ASM_COMMENT_START ";#"
370 #endif
372 /* Data and reference forms for relocatable data. */
373 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
374 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
376 #ifndef DEBUG_FRAME_SECTION
377 #define DEBUG_FRAME_SECTION ".debug_frame"
378 #endif
380 #ifndef FUNC_BEGIN_LABEL
381 #define FUNC_BEGIN_LABEL "LFB"
382 #endif
384 #ifndef FUNC_END_LABEL
385 #define FUNC_END_LABEL "LFE"
386 #endif
388 #ifndef FRAME_BEGIN_LABEL
389 #define FRAME_BEGIN_LABEL "Lframe"
390 #endif
391 #define CIE_AFTER_SIZE_LABEL "LSCIE"
392 #define CIE_END_LABEL "LECIE"
393 #define FDE_LABEL "LSFDE"
394 #define FDE_AFTER_SIZE_LABEL "LASFDE"
395 #define FDE_END_LABEL "LEFDE"
396 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
397 #define LINE_NUMBER_END_LABEL "LELT"
398 #define LN_PROLOG_AS_LABEL "LASLTP"
399 #define LN_PROLOG_END_LABEL "LELTP"
400 #define DIE_LABEL_PREFIX "DW"
402 /* The DWARF 2 CFA column which tracks the return address. Normally this
403 is the column for PC, or the first column after all of the hard
404 registers. */
405 #ifndef DWARF_FRAME_RETURN_COLUMN
406 #ifdef PC_REGNUM
407 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGNUM (PC_REGNUM)
408 #else
409 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGISTERS
410 #endif
411 #endif
413 /* The mapping from gcc register number to DWARF 2 CFA column number. By
414 default, we just provide columns for all registers. */
415 #ifndef DWARF_FRAME_REGNUM
416 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
417 #endif
419 /* Hook used by __throw. */
422 expand_builtin_dwarf_sp_column (void)
424 unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
425 return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
428 /* Return a pointer to a copy of the section string name S with all
429 attributes stripped off, and an asterisk prepended (for assemble_name). */
431 static inline char *
432 stripattributes (const char *s)
434 char *stripped = XNEWVEC (char, strlen (s) + 2);
435 char *p = stripped;
437 *p++ = '*';
439 while (*s && *s != ',')
440 *p++ = *s++;
442 *p = '\0';
443 return stripped;
446 /* MEM is a memory reference for the register size table, each element of
447 which has mode MODE. Initialize column C as a return address column. */
449 static void
450 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
452 HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
453 HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
454 emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
457 /* Generate code to initialize the register size table. */
459 void
460 expand_builtin_init_dwarf_reg_sizes (tree address)
462 unsigned int i;
463 enum machine_mode mode = TYPE_MODE (char_type_node);
464 rtx addr = expand_normal (address);
465 rtx mem = gen_rtx_MEM (BLKmode, addr);
466 bool wrote_return_column = false;
468 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
470 int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
472 if (rnum < DWARF_FRAME_REGISTERS)
474 HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
475 enum machine_mode save_mode = reg_raw_mode[i];
476 HOST_WIDE_INT size;
478 if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
479 save_mode = choose_hard_reg_mode (i, 1, true);
480 if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
482 if (save_mode == VOIDmode)
483 continue;
484 wrote_return_column = true;
486 size = GET_MODE_SIZE (save_mode);
487 if (offset < 0)
488 continue;
490 emit_move_insn (adjust_address (mem, mode, offset),
491 gen_int_mode (size, mode));
495 if (!wrote_return_column)
496 init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
498 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
499 init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
500 #endif
502 targetm.init_dwarf_reg_sizes_extra (address);
505 /* Convert a DWARF call frame info. operation to its string name */
507 static const char *
508 dwarf_cfi_name (unsigned int cfi_opc)
510 switch (cfi_opc)
512 case DW_CFA_advance_loc:
513 return "DW_CFA_advance_loc";
514 case DW_CFA_offset:
515 return "DW_CFA_offset";
516 case DW_CFA_restore:
517 return "DW_CFA_restore";
518 case DW_CFA_nop:
519 return "DW_CFA_nop";
520 case DW_CFA_set_loc:
521 return "DW_CFA_set_loc";
522 case DW_CFA_advance_loc1:
523 return "DW_CFA_advance_loc1";
524 case DW_CFA_advance_loc2:
525 return "DW_CFA_advance_loc2";
526 case DW_CFA_advance_loc4:
527 return "DW_CFA_advance_loc4";
528 case DW_CFA_offset_extended:
529 return "DW_CFA_offset_extended";
530 case DW_CFA_restore_extended:
531 return "DW_CFA_restore_extended";
532 case DW_CFA_undefined:
533 return "DW_CFA_undefined";
534 case DW_CFA_same_value:
535 return "DW_CFA_same_value";
536 case DW_CFA_register:
537 return "DW_CFA_register";
538 case DW_CFA_remember_state:
539 return "DW_CFA_remember_state";
540 case DW_CFA_restore_state:
541 return "DW_CFA_restore_state";
542 case DW_CFA_def_cfa:
543 return "DW_CFA_def_cfa";
544 case DW_CFA_def_cfa_register:
545 return "DW_CFA_def_cfa_register";
546 case DW_CFA_def_cfa_offset:
547 return "DW_CFA_def_cfa_offset";
549 /* DWARF 3 */
550 case DW_CFA_def_cfa_expression:
551 return "DW_CFA_def_cfa_expression";
552 case DW_CFA_expression:
553 return "DW_CFA_expression";
554 case DW_CFA_offset_extended_sf:
555 return "DW_CFA_offset_extended_sf";
556 case DW_CFA_def_cfa_sf:
557 return "DW_CFA_def_cfa_sf";
558 case DW_CFA_def_cfa_offset_sf:
559 return "DW_CFA_def_cfa_offset_sf";
561 /* SGI/MIPS specific */
562 case DW_CFA_MIPS_advance_loc8:
563 return "DW_CFA_MIPS_advance_loc8";
565 /* GNU extensions */
566 case DW_CFA_GNU_window_save:
567 return "DW_CFA_GNU_window_save";
568 case DW_CFA_GNU_args_size:
569 return "DW_CFA_GNU_args_size";
570 case DW_CFA_GNU_negative_offset_extended:
571 return "DW_CFA_GNU_negative_offset_extended";
573 default:
574 return "DW_CFA_<unknown>";
578 /* Return a pointer to a newly allocated Call Frame Instruction. */
580 static inline dw_cfi_ref
581 new_cfi (void)
583 dw_cfi_ref cfi = ggc_alloc (sizeof (dw_cfi_node));
585 cfi->dw_cfi_next = NULL;
586 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
587 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
589 return cfi;
592 /* Add a Call Frame Instruction to list of instructions. */
594 static inline void
595 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
597 dw_cfi_ref *p;
599 /* Find the end of the chain. */
600 for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
603 *p = cfi;
606 /* Generate a new label for the CFI info to refer to. */
608 char *
609 dwarf2out_cfi_label (void)
611 static char label[20];
613 ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
614 ASM_OUTPUT_LABEL (asm_out_file, label);
615 return label;
618 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
619 or to the CIE if LABEL is NULL. */
621 static void
622 add_fde_cfi (const char *label, dw_cfi_ref cfi)
624 if (label)
626 dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
628 if (*label == 0)
629 label = dwarf2out_cfi_label ();
631 if (fde->dw_fde_current_label == NULL
632 || strcmp (label, fde->dw_fde_current_label) != 0)
634 dw_cfi_ref xcfi;
636 label = xstrdup (label);
638 /* Set the location counter to the new label. */
639 xcfi = new_cfi ();
640 /* If we have a current label, advance from there, otherwise
641 set the location directly using set_loc. */
642 xcfi->dw_cfi_opc = fde->dw_fde_current_label
643 ? DW_CFA_advance_loc4
644 : DW_CFA_set_loc;
645 xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
646 add_cfi (&fde->dw_fde_cfi, xcfi);
648 fde->dw_fde_current_label = label;
651 add_cfi (&fde->dw_fde_cfi, cfi);
654 else
655 add_cfi (&cie_cfi_head, cfi);
658 /* Subroutine of lookup_cfa. */
660 static void
661 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc)
663 switch (cfi->dw_cfi_opc)
665 case DW_CFA_def_cfa_offset:
666 loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
667 break;
668 case DW_CFA_def_cfa_offset_sf:
669 loc->offset
670 = cfi->dw_cfi_oprnd1.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
671 break;
672 case DW_CFA_def_cfa_register:
673 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
674 break;
675 case DW_CFA_def_cfa:
676 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
677 loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
678 break;
679 case DW_CFA_def_cfa_sf:
680 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
681 loc->offset
682 = cfi->dw_cfi_oprnd2.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
683 break;
684 case DW_CFA_def_cfa_expression:
685 get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
686 break;
687 default:
688 break;
692 /* Find the previous value for the CFA. */
694 static void
695 lookup_cfa (dw_cfa_location *loc)
697 dw_cfi_ref cfi;
699 loc->reg = INVALID_REGNUM;
700 loc->offset = 0;
701 loc->indirect = 0;
702 loc->base_offset = 0;
704 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
705 lookup_cfa_1 (cfi, loc);
707 if (fde_table_in_use)
709 dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
710 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
711 lookup_cfa_1 (cfi, loc);
715 /* The current rule for calculating the DWARF2 canonical frame address. */
716 static dw_cfa_location cfa;
718 /* The register used for saving registers to the stack, and its offset
719 from the CFA. */
720 static dw_cfa_location cfa_store;
722 /* The running total of the size of arguments pushed onto the stack. */
723 static HOST_WIDE_INT args_size;
725 /* The last args_size we actually output. */
726 static HOST_WIDE_INT old_args_size;
728 /* Entry point to update the canonical frame address (CFA).
729 LABEL is passed to add_fde_cfi. The value of CFA is now to be
730 calculated from REG+OFFSET. */
732 void
733 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
735 dw_cfa_location loc;
736 loc.indirect = 0;
737 loc.base_offset = 0;
738 loc.reg = reg;
739 loc.offset = offset;
740 def_cfa_1 (label, &loc);
743 /* Determine if two dw_cfa_location structures define the same data. */
745 static bool
746 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
748 return (loc1->reg == loc2->reg
749 && loc1->offset == loc2->offset
750 && loc1->indirect == loc2->indirect
751 && (loc1->indirect == 0
752 || loc1->base_offset == loc2->base_offset));
755 /* This routine does the actual work. The CFA is now calculated from
756 the dw_cfa_location structure. */
758 static void
759 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
761 dw_cfi_ref cfi;
762 dw_cfa_location old_cfa, loc;
764 cfa = *loc_p;
765 loc = *loc_p;
767 if (cfa_store.reg == loc.reg && loc.indirect == 0)
768 cfa_store.offset = loc.offset;
770 loc.reg = DWARF_FRAME_REGNUM (loc.reg);
771 lookup_cfa (&old_cfa);
773 /* If nothing changed, no need to issue any call frame instructions. */
774 if (cfa_equal_p (&loc, &old_cfa))
775 return;
777 cfi = new_cfi ();
779 if (loc.reg == old_cfa.reg && !loc.indirect)
781 /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
782 the CFA register did not change but the offset did. */
783 if (loc.offset < 0)
785 HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
786 gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
788 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
789 cfi->dw_cfi_oprnd1.dw_cfi_offset = f_offset;
791 else
793 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
794 cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
798 #ifndef MIPS_DEBUGGING_INFO /* SGI dbx thinks this means no offset. */
799 else if (loc.offset == old_cfa.offset
800 && old_cfa.reg != INVALID_REGNUM
801 && !loc.indirect)
803 /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
804 indicating the CFA register has changed to <register> but the
805 offset has not changed. */
806 cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
807 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
809 #endif
811 else if (loc.indirect == 0)
813 /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
814 indicating the CFA register has changed to <register> with
815 the specified offset. */
816 if (loc.offset < 0)
818 HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
819 gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
821 cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
822 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
823 cfi->dw_cfi_oprnd2.dw_cfi_offset = f_offset;
825 else
827 cfi->dw_cfi_opc = DW_CFA_def_cfa;
828 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
829 cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
832 else
834 /* Construct a DW_CFA_def_cfa_expression instruction to
835 calculate the CFA using a full location expression since no
836 register-offset pair is available. */
837 struct dw_loc_descr_struct *loc_list;
839 cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
840 loc_list = build_cfa_loc (&loc, 0);
841 cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
844 add_fde_cfi (label, cfi);
847 /* Add the CFI for saving a register. REG is the CFA column number.
848 LABEL is passed to add_fde_cfi.
849 If SREG is -1, the register is saved at OFFSET from the CFA;
850 otherwise it is saved in SREG. */
852 static void
853 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
855 dw_cfi_ref cfi = new_cfi ();
857 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
859 if (sreg == INVALID_REGNUM)
861 if (reg & ~0x3f)
862 /* The register number won't fit in 6 bits, so we have to use
863 the long form. */
864 cfi->dw_cfi_opc = DW_CFA_offset_extended;
865 else
866 cfi->dw_cfi_opc = DW_CFA_offset;
868 #ifdef ENABLE_CHECKING
870 /* If we get an offset that is not a multiple of
871 DWARF_CIE_DATA_ALIGNMENT, there is either a bug in the
872 definition of DWARF_CIE_DATA_ALIGNMENT, or a bug in the machine
873 description. */
874 HOST_WIDE_INT check_offset = offset / DWARF_CIE_DATA_ALIGNMENT;
876 gcc_assert (check_offset * DWARF_CIE_DATA_ALIGNMENT == offset);
878 #endif
879 offset /= DWARF_CIE_DATA_ALIGNMENT;
880 if (offset < 0)
881 cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
883 cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
885 else if (sreg == reg)
886 cfi->dw_cfi_opc = DW_CFA_same_value;
887 else
889 cfi->dw_cfi_opc = DW_CFA_register;
890 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
893 add_fde_cfi (label, cfi);
896 /* Add the CFI for saving a register window. LABEL is passed to reg_save.
897 This CFI tells the unwinder that it needs to restore the window registers
898 from the previous frame's window save area.
900 ??? Perhaps we should note in the CIE where windows are saved (instead of
901 assuming 0(cfa)) and what registers are in the window. */
903 void
904 dwarf2out_window_save (const char *label)
906 dw_cfi_ref cfi = new_cfi ();
908 cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
909 add_fde_cfi (label, cfi);
912 /* Add a CFI to update the running total of the size of arguments
913 pushed onto the stack. */
915 void
916 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
918 dw_cfi_ref cfi;
920 if (size == old_args_size)
921 return;
923 old_args_size = size;
925 cfi = new_cfi ();
926 cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
927 cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
928 add_fde_cfi (label, cfi);
931 /* Entry point for saving a register to the stack. REG is the GCC register
932 number. LABEL and OFFSET are passed to reg_save. */
934 void
935 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
937 reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
940 /* Entry point for saving the return address in the stack.
941 LABEL and OFFSET are passed to reg_save. */
943 void
944 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
946 reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
949 /* Entry point for saving the return address in a register.
950 LABEL and SREG are passed to reg_save. */
952 void
953 dwarf2out_return_reg (const char *label, unsigned int sreg)
955 reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
958 /* Record the initial position of the return address. RTL is
959 INCOMING_RETURN_ADDR_RTX. */
961 static void
962 initial_return_save (rtx rtl)
964 unsigned int reg = INVALID_REGNUM;
965 HOST_WIDE_INT offset = 0;
967 switch (GET_CODE (rtl))
969 case REG:
970 /* RA is in a register. */
971 reg = DWARF_FRAME_REGNUM (REGNO (rtl));
972 break;
974 case MEM:
975 /* RA is on the stack. */
976 rtl = XEXP (rtl, 0);
977 switch (GET_CODE (rtl))
979 case REG:
980 gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
981 offset = 0;
982 break;
984 case PLUS:
985 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
986 offset = INTVAL (XEXP (rtl, 1));
987 break;
989 case MINUS:
990 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
991 offset = -INTVAL (XEXP (rtl, 1));
992 break;
994 default:
995 gcc_unreachable ();
998 break;
1000 case PLUS:
1001 /* The return address is at some offset from any value we can
1002 actually load. For instance, on the SPARC it is in %i7+8. Just
1003 ignore the offset for now; it doesn't matter for unwinding frames. */
1004 gcc_assert (GET_CODE (XEXP (rtl, 1)) == CONST_INT);
1005 initial_return_save (XEXP (rtl, 0));
1006 return;
1008 default:
1009 gcc_unreachable ();
1012 if (reg != DWARF_FRAME_RETURN_COLUMN)
1013 reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1016 /* Given a SET, calculate the amount of stack adjustment it
1017 contains. */
1019 static HOST_WIDE_INT
1020 stack_adjust_offset (rtx pattern)
1022 rtx src = SET_SRC (pattern);
1023 rtx dest = SET_DEST (pattern);
1024 HOST_WIDE_INT offset = 0;
1025 enum rtx_code code;
1027 if (dest == stack_pointer_rtx)
1029 /* (set (reg sp) (plus (reg sp) (const_int))) */
1030 code = GET_CODE (src);
1031 if (! (code == PLUS || code == MINUS)
1032 || XEXP (src, 0) != stack_pointer_rtx
1033 || GET_CODE (XEXP (src, 1)) != CONST_INT)
1034 return 0;
1036 offset = INTVAL (XEXP (src, 1));
1037 if (code == PLUS)
1038 offset = -offset;
1040 else if (MEM_P (dest))
1042 /* (set (mem (pre_dec (reg sp))) (foo)) */
1043 src = XEXP (dest, 0);
1044 code = GET_CODE (src);
1046 switch (code)
1048 case PRE_MODIFY:
1049 case POST_MODIFY:
1050 if (XEXP (src, 0) == stack_pointer_rtx)
1052 rtx val = XEXP (XEXP (src, 1), 1);
1053 /* We handle only adjustments by constant amount. */
1054 gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1055 && GET_CODE (val) == CONST_INT);
1056 offset = -INTVAL (val);
1057 break;
1059 return 0;
1061 case PRE_DEC:
1062 case POST_DEC:
1063 if (XEXP (src, 0) == stack_pointer_rtx)
1065 offset = GET_MODE_SIZE (GET_MODE (dest));
1066 break;
1068 return 0;
1070 case PRE_INC:
1071 case POST_INC:
1072 if (XEXP (src, 0) == stack_pointer_rtx)
1074 offset = -GET_MODE_SIZE (GET_MODE (dest));
1075 break;
1077 return 0;
1079 default:
1080 return 0;
1083 else
1084 return 0;
1086 return offset;
1089 /* Check INSN to see if it looks like a push or a stack adjustment, and
1090 make a note of it if it does. EH uses this information to find out how
1091 much extra space it needs to pop off the stack. */
1093 static void
1094 dwarf2out_stack_adjust (rtx insn, bool after_p)
1096 HOST_WIDE_INT offset;
1097 const char *label;
1098 int i;
1100 /* Don't handle epilogues at all. Certainly it would be wrong to do so
1101 with this function. Proper support would require all frame-related
1102 insns to be marked, and to be able to handle saving state around
1103 epilogues textually in the middle of the function. */
1104 if (prologue_epilogue_contains (insn) || sibcall_epilogue_contains (insn))
1105 return;
1107 /* If only calls can throw, and we have a frame pointer,
1108 save up adjustments until we see the CALL_INSN. */
1109 if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1111 if (CALL_P (insn) && !after_p)
1113 /* Extract the size of the args from the CALL rtx itself. */
1114 insn = PATTERN (insn);
1115 if (GET_CODE (insn) == PARALLEL)
1116 insn = XVECEXP (insn, 0, 0);
1117 if (GET_CODE (insn) == SET)
1118 insn = SET_SRC (insn);
1119 gcc_assert (GET_CODE (insn) == CALL);
1120 dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1122 return;
1125 if (CALL_P (insn) && !after_p)
1127 if (!flag_asynchronous_unwind_tables)
1128 dwarf2out_args_size ("", args_size);
1129 return;
1131 else if (BARRIER_P (insn))
1133 /* When we see a BARRIER, we know to reset args_size to 0. Usually
1134 the compiler will have already emitted a stack adjustment, but
1135 doesn't bother for calls to noreturn functions. */
1136 #ifdef STACK_GROWS_DOWNWARD
1137 offset = -args_size;
1138 #else
1139 offset = args_size;
1140 #endif
1142 else if (GET_CODE (PATTERN (insn)) == SET)
1143 offset = stack_adjust_offset (PATTERN (insn));
1144 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1145 || GET_CODE (PATTERN (insn)) == SEQUENCE)
1147 /* There may be stack adjustments inside compound insns. Search
1148 for them. */
1149 for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1150 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1151 offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i));
1153 else
1154 return;
1156 if (offset == 0)
1157 return;
1159 if (cfa.reg == STACK_POINTER_REGNUM)
1160 cfa.offset += offset;
1162 #ifndef STACK_GROWS_DOWNWARD
1163 offset = -offset;
1164 #endif
1166 args_size += offset;
1167 if (args_size < 0)
1168 args_size = 0;
1170 label = dwarf2out_cfi_label ();
1171 def_cfa_1 (label, &cfa);
1172 if (flag_asynchronous_unwind_tables)
1173 dwarf2out_args_size (label, args_size);
1176 #endif
1178 /* We delay emitting a register save until either (a) we reach the end
1179 of the prologue or (b) the register is clobbered. This clusters
1180 register saves so that there are fewer pc advances. */
1182 struct queued_reg_save GTY(())
1184 struct queued_reg_save *next;
1185 rtx reg;
1186 HOST_WIDE_INT cfa_offset;
1187 rtx saved_reg;
1190 static GTY(()) struct queued_reg_save *queued_reg_saves;
1192 /* The caller's ORIG_REG is saved in SAVED_IN_REG. */
1193 struct reg_saved_in_data GTY(()) {
1194 rtx orig_reg;
1195 rtx saved_in_reg;
1198 /* A list of registers saved in other registers.
1199 The list intentionally has a small maximum capacity of 4; if your
1200 port needs more than that, you might consider implementing a
1201 more efficient data structure. */
1202 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1203 static GTY(()) size_t num_regs_saved_in_regs;
1205 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1206 static const char *last_reg_save_label;
1208 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1209 SREG, or if SREG is NULL then it is saved at OFFSET to the CFA. */
1211 static void
1212 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1214 struct queued_reg_save *q;
1216 /* Duplicates waste space, but it's also necessary to remove them
1217 for correctness, since the queue gets output in reverse
1218 order. */
1219 for (q = queued_reg_saves; q != NULL; q = q->next)
1220 if (REGNO (q->reg) == REGNO (reg))
1221 break;
1223 if (q == NULL)
1225 q = ggc_alloc (sizeof (*q));
1226 q->next = queued_reg_saves;
1227 queued_reg_saves = q;
1230 q->reg = reg;
1231 q->cfa_offset = offset;
1232 q->saved_reg = sreg;
1234 last_reg_save_label = label;
1237 /* Output all the entries in QUEUED_REG_SAVES. */
1239 static void
1240 flush_queued_reg_saves (void)
1242 struct queued_reg_save *q;
1244 for (q = queued_reg_saves; q; q = q->next)
1246 size_t i;
1247 unsigned int reg, sreg;
1249 for (i = 0; i < num_regs_saved_in_regs; i++)
1250 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1251 break;
1252 if (q->saved_reg && i == num_regs_saved_in_regs)
1254 gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1255 num_regs_saved_in_regs++;
1257 if (i != num_regs_saved_in_regs)
1259 regs_saved_in_regs[i].orig_reg = q->reg;
1260 regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1263 reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1264 if (q->saved_reg)
1265 sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1266 else
1267 sreg = INVALID_REGNUM;
1268 reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1271 queued_reg_saves = NULL;
1272 last_reg_save_label = NULL;
1275 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1276 location for? Or, does it clobber a register which we've previously
1277 said that some other register is saved in, and for which we now
1278 have a new location for? */
1280 static bool
1281 clobbers_queued_reg_save (rtx insn)
1283 struct queued_reg_save *q;
1285 for (q = queued_reg_saves; q; q = q->next)
1287 size_t i;
1288 if (modified_in_p (q->reg, insn))
1289 return true;
1290 for (i = 0; i < num_regs_saved_in_regs; i++)
1291 if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1292 && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1293 return true;
1296 return false;
1299 /* Entry point for saving the first register into the second. */
1301 void
1302 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1304 size_t i;
1305 unsigned int regno, sregno;
1307 for (i = 0; i < num_regs_saved_in_regs; i++)
1308 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1309 break;
1310 if (i == num_regs_saved_in_regs)
1312 gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1313 num_regs_saved_in_regs++;
1315 regs_saved_in_regs[i].orig_reg = reg;
1316 regs_saved_in_regs[i].saved_in_reg = sreg;
1318 regno = DWARF_FRAME_REGNUM (REGNO (reg));
1319 sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1320 reg_save (label, regno, sregno, 0);
1323 /* What register, if any, is currently saved in REG? */
1325 static rtx
1326 reg_saved_in (rtx reg)
1328 unsigned int regn = REGNO (reg);
1329 size_t i;
1330 struct queued_reg_save *q;
1332 for (q = queued_reg_saves; q; q = q->next)
1333 if (q->saved_reg && regn == REGNO (q->saved_reg))
1334 return q->reg;
1336 for (i = 0; i < num_regs_saved_in_regs; i++)
1337 if (regs_saved_in_regs[i].saved_in_reg
1338 && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1339 return regs_saved_in_regs[i].orig_reg;
1341 return NULL_RTX;
1345 /* A temporary register holding an integral value used in adjusting SP
1346 or setting up the store_reg. The "offset" field holds the integer
1347 value, not an offset. */
1348 static dw_cfa_location cfa_temp;
1350 /* Record call frame debugging information for an expression EXPR,
1351 which either sets SP or FP (adjusting how we calculate the frame
1352 address) or saves a register to the stack or another register.
1353 LABEL indicates the address of EXPR.
1355 This function encodes a state machine mapping rtxes to actions on
1356 cfa, cfa_store, and cfa_temp.reg. We describe these rules so
1357 users need not read the source code.
1359 The High-Level Picture
1361 Changes in the register we use to calculate the CFA: Currently we
1362 assume that if you copy the CFA register into another register, we
1363 should take the other one as the new CFA register; this seems to
1364 work pretty well. If it's wrong for some target, it's simple
1365 enough not to set RTX_FRAME_RELATED_P on the insn in question.
1367 Changes in the register we use for saving registers to the stack:
1368 This is usually SP, but not always. Again, we deduce that if you
1369 copy SP into another register (and SP is not the CFA register),
1370 then the new register is the one we will be using for register
1371 saves. This also seems to work.
1373 Register saves: There's not much guesswork about this one; if
1374 RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1375 register save, and the register used to calculate the destination
1376 had better be the one we think we're using for this purpose.
1377 It's also assumed that a copy from a call-saved register to another
1378 register is saving that register if RTX_FRAME_RELATED_P is set on
1379 that instruction. If the copy is from a call-saved register to
1380 the *same* register, that means that the register is now the same
1381 value as in the caller.
1383 Except: If the register being saved is the CFA register, and the
1384 offset is nonzero, we are saving the CFA, so we assume we have to
1385 use DW_CFA_def_cfa_expression. If the offset is 0, we assume that
1386 the intent is to save the value of SP from the previous frame.
1388 In addition, if a register has previously been saved to a different
1389 register,
1391 Invariants / Summaries of Rules
1393 cfa current rule for calculating the CFA. It usually
1394 consists of a register and an offset.
1395 cfa_store register used by prologue code to save things to the stack
1396 cfa_store.offset is the offset from the value of
1397 cfa_store.reg to the actual CFA
1398 cfa_temp register holding an integral value. cfa_temp.offset
1399 stores the value, which will be used to adjust the
1400 stack pointer. cfa_temp is also used like cfa_store,
1401 to track stores to the stack via fp or a temp reg.
1403 Rules 1- 4: Setting a register's value to cfa.reg or an expression
1404 with cfa.reg as the first operand changes the cfa.reg and its
1405 cfa.offset. Rule 1 and 4 also set cfa_temp.reg and
1406 cfa_temp.offset.
1408 Rules 6- 9: Set a non-cfa.reg register value to a constant or an
1409 expression yielding a constant. This sets cfa_temp.reg
1410 and cfa_temp.offset.
1412 Rule 5: Create a new register cfa_store used to save items to the
1413 stack.
1415 Rules 10-14: Save a register to the stack. Define offset as the
1416 difference of the original location and cfa_store's
1417 location (or cfa_temp's location if cfa_temp is used).
1419 The Rules
1421 "{a,b}" indicates a choice of a xor b.
1422 "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1424 Rule 1:
1425 (set <reg1> <reg2>:cfa.reg)
1426 effects: cfa.reg = <reg1>
1427 cfa.offset unchanged
1428 cfa_temp.reg = <reg1>
1429 cfa_temp.offset = cfa.offset
1431 Rule 2:
1432 (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1433 {<const_int>,<reg>:cfa_temp.reg}))
1434 effects: cfa.reg = sp if fp used
1435 cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
1436 cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1437 if cfa_store.reg==sp
1439 Rule 3:
1440 (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
1441 effects: cfa.reg = fp
1442 cfa_offset += +/- <const_int>
1444 Rule 4:
1445 (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
1446 constraints: <reg1> != fp
1447 <reg1> != sp
1448 effects: cfa.reg = <reg1>
1449 cfa_temp.reg = <reg1>
1450 cfa_temp.offset = cfa.offset
1452 Rule 5:
1453 (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1454 constraints: <reg1> != fp
1455 <reg1> != sp
1456 effects: cfa_store.reg = <reg1>
1457 cfa_store.offset = cfa.offset - cfa_temp.offset
1459 Rule 6:
1460 (set <reg> <const_int>)
1461 effects: cfa_temp.reg = <reg>
1462 cfa_temp.offset = <const_int>
1464 Rule 7:
1465 (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1466 effects: cfa_temp.reg = <reg1>
1467 cfa_temp.offset |= <const_int>
1469 Rule 8:
1470 (set <reg> (high <exp>))
1471 effects: none
1473 Rule 9:
1474 (set <reg> (lo_sum <exp> <const_int>))
1475 effects: cfa_temp.reg = <reg>
1476 cfa_temp.offset = <const_int>
1478 Rule 10:
1479 (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1480 effects: cfa_store.offset -= <const_int>
1481 cfa.offset = cfa_store.offset if cfa.reg == sp
1482 cfa.reg = sp
1483 cfa.base_offset = -cfa_store.offset
1485 Rule 11:
1486 (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1487 effects: cfa_store.offset += -/+ mode_size(mem)
1488 cfa.offset = cfa_store.offset if cfa.reg == sp
1489 cfa.reg = sp
1490 cfa.base_offset = -cfa_store.offset
1492 Rule 12:
1493 (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1495 <reg2>)
1496 effects: cfa.reg = <reg1>
1497 cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
1499 Rule 13:
1500 (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1501 effects: cfa.reg = <reg1>
1502 cfa.base_offset = -{cfa_store,cfa_temp}.offset
1504 Rule 14:
1505 (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
1506 effects: cfa.reg = <reg1>
1507 cfa.base_offset = -cfa_temp.offset
1508 cfa_temp.offset -= mode_size(mem)
1510 Rule 15:
1511 (set <reg> {unspec, unspec_volatile})
1512 effects: target-dependent */
1514 static void
1515 dwarf2out_frame_debug_expr (rtx expr, const char *label)
1517 rtx src, dest;
1518 HOST_WIDE_INT offset;
1520 /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1521 the PARALLEL independently. The first element is always processed if
1522 it is a SET. This is for backward compatibility. Other elements
1523 are processed only if they are SETs and the RTX_FRAME_RELATED_P
1524 flag is set in them. */
1525 if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
1527 int par_index;
1528 int limit = XVECLEN (expr, 0);
1529 rtx elem;
1531 /* PARALLELs have strict read-modify-write semantics, so we
1532 ought to evaluate every rvalue before changing any lvalue.
1533 It's cumbersome to do that in general, but there's an
1534 easy approximation that is enough for all current users:
1535 handle register saves before register assignments. */
1536 if (GET_CODE (expr) == PARALLEL)
1537 for (par_index = 0; par_index < limit; par_index++)
1539 elem = XVECEXP (expr, 0, par_index);
1540 if (GET_CODE (elem) == SET
1541 && MEM_P (SET_DEST (elem))
1542 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1543 dwarf2out_frame_debug_expr (elem, label);
1546 for (par_index = 0; par_index < limit; par_index++)
1548 elem = XVECEXP (expr, 0, par_index);
1549 if (GET_CODE (elem) == SET
1550 && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
1551 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1552 dwarf2out_frame_debug_expr (elem, label);
1554 return;
1557 gcc_assert (GET_CODE (expr) == SET);
1559 src = SET_SRC (expr);
1560 dest = SET_DEST (expr);
1562 if (REG_P (src))
1564 rtx rsi = reg_saved_in (src);
1565 if (rsi)
1566 src = rsi;
1569 switch (GET_CODE (dest))
1571 case REG:
1572 switch (GET_CODE (src))
1574 /* Setting FP from SP. */
1575 case REG:
1576 if (cfa.reg == (unsigned) REGNO (src))
1578 /* Rule 1 */
1579 /* Update the CFA rule wrt SP or FP. Make sure src is
1580 relative to the current CFA register.
1582 We used to require that dest be either SP or FP, but the
1583 ARM copies SP to a temporary register, and from there to
1584 FP. So we just rely on the backends to only set
1585 RTX_FRAME_RELATED_P on appropriate insns. */
1586 cfa.reg = REGNO (dest);
1587 cfa_temp.reg = cfa.reg;
1588 cfa_temp.offset = cfa.offset;
1590 else
1592 /* Saving a register in a register. */
1593 gcc_assert (!fixed_regs [REGNO (dest)]
1594 /* For the SPARC and its register window. */
1595 || (DWARF_FRAME_REGNUM (REGNO (src))
1596 == DWARF_FRAME_RETURN_COLUMN));
1597 queue_reg_save (label, src, dest, 0);
1599 break;
1601 case PLUS:
1602 case MINUS:
1603 case LO_SUM:
1604 if (dest == stack_pointer_rtx)
1606 /* Rule 2 */
1607 /* Adjusting SP. */
1608 switch (GET_CODE (XEXP (src, 1)))
1610 case CONST_INT:
1611 offset = INTVAL (XEXP (src, 1));
1612 break;
1613 case REG:
1614 gcc_assert ((unsigned) REGNO (XEXP (src, 1))
1615 == cfa_temp.reg);
1616 offset = cfa_temp.offset;
1617 break;
1618 default:
1619 gcc_unreachable ();
1622 if (XEXP (src, 0) == hard_frame_pointer_rtx)
1624 /* Restoring SP from FP in the epilogue. */
1625 gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
1626 cfa.reg = STACK_POINTER_REGNUM;
1628 else if (GET_CODE (src) == LO_SUM)
1629 /* Assume we've set the source reg of the LO_SUM from sp. */
1631 else
1632 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
1634 if (GET_CODE (src) != MINUS)
1635 offset = -offset;
1636 if (cfa.reg == STACK_POINTER_REGNUM)
1637 cfa.offset += offset;
1638 if (cfa_store.reg == STACK_POINTER_REGNUM)
1639 cfa_store.offset += offset;
1641 else if (dest == hard_frame_pointer_rtx)
1643 /* Rule 3 */
1644 /* Either setting the FP from an offset of the SP,
1645 or adjusting the FP */
1646 gcc_assert (frame_pointer_needed);
1648 gcc_assert (REG_P (XEXP (src, 0))
1649 && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
1650 && GET_CODE (XEXP (src, 1)) == CONST_INT);
1651 offset = INTVAL (XEXP (src, 1));
1652 if (GET_CODE (src) != MINUS)
1653 offset = -offset;
1654 cfa.offset += offset;
1655 cfa.reg = HARD_FRAME_POINTER_REGNUM;
1657 else
1659 gcc_assert (GET_CODE (src) != MINUS);
1661 /* Rule 4 */
1662 if (REG_P (XEXP (src, 0))
1663 && REGNO (XEXP (src, 0)) == cfa.reg
1664 && GET_CODE (XEXP (src, 1)) == CONST_INT)
1666 /* Setting a temporary CFA register that will be copied
1667 into the FP later on. */
1668 offset = - INTVAL (XEXP (src, 1));
1669 cfa.offset += offset;
1670 cfa.reg = REGNO (dest);
1671 /* Or used to save regs to the stack. */
1672 cfa_temp.reg = cfa.reg;
1673 cfa_temp.offset = cfa.offset;
1676 /* Rule 5 */
1677 else if (REG_P (XEXP (src, 0))
1678 && REGNO (XEXP (src, 0)) == cfa_temp.reg
1679 && XEXP (src, 1) == stack_pointer_rtx)
1681 /* Setting a scratch register that we will use instead
1682 of SP for saving registers to the stack. */
1683 gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
1684 cfa_store.reg = REGNO (dest);
1685 cfa_store.offset = cfa.offset - cfa_temp.offset;
1688 /* Rule 9 */
1689 else if (GET_CODE (src) == LO_SUM
1690 && GET_CODE (XEXP (src, 1)) == CONST_INT)
1692 cfa_temp.reg = REGNO (dest);
1693 cfa_temp.offset = INTVAL (XEXP (src, 1));
1695 else
1696 gcc_unreachable ();
1698 break;
1700 /* Rule 6 */
1701 case CONST_INT:
1702 cfa_temp.reg = REGNO (dest);
1703 cfa_temp.offset = INTVAL (src);
1704 break;
1706 /* Rule 7 */
1707 case IOR:
1708 gcc_assert (REG_P (XEXP (src, 0))
1709 && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
1710 && GET_CODE (XEXP (src, 1)) == CONST_INT);
1712 if ((unsigned) REGNO (dest) != cfa_temp.reg)
1713 cfa_temp.reg = REGNO (dest);
1714 cfa_temp.offset |= INTVAL (XEXP (src, 1));
1715 break;
1717 /* Skip over HIGH, assuming it will be followed by a LO_SUM,
1718 which will fill in all of the bits. */
1719 /* Rule 8 */
1720 case HIGH:
1721 break;
1723 /* Rule 15 */
1724 case UNSPEC:
1725 case UNSPEC_VOLATILE:
1726 gcc_assert (targetm.dwarf_handle_frame_unspec);
1727 targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
1728 return;
1730 default:
1731 gcc_unreachable ();
1734 def_cfa_1 (label, &cfa);
1735 break;
1737 case MEM:
1738 gcc_assert (REG_P (src));
1740 /* Saving a register to the stack. Make sure dest is relative to the
1741 CFA register. */
1742 switch (GET_CODE (XEXP (dest, 0)))
1744 /* Rule 10 */
1745 /* With a push. */
1746 case PRE_MODIFY:
1747 /* We can't handle variable size modifications. */
1748 gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
1749 == CONST_INT);
1750 offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
1752 gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1753 && cfa_store.reg == STACK_POINTER_REGNUM);
1755 cfa_store.offset += offset;
1756 if (cfa.reg == STACK_POINTER_REGNUM)
1757 cfa.offset = cfa_store.offset;
1759 offset = -cfa_store.offset;
1760 break;
1762 /* Rule 11 */
1763 case PRE_INC:
1764 case PRE_DEC:
1765 offset = GET_MODE_SIZE (GET_MODE (dest));
1766 if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
1767 offset = -offset;
1769 gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1770 && cfa_store.reg == STACK_POINTER_REGNUM);
1772 cfa_store.offset += offset;
1773 if (cfa.reg == STACK_POINTER_REGNUM)
1774 cfa.offset = cfa_store.offset;
1776 offset = -cfa_store.offset;
1777 break;
1779 /* Rule 12 */
1780 /* With an offset. */
1781 case PLUS:
1782 case MINUS:
1783 case LO_SUM:
1785 int regno;
1787 gcc_assert (GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT
1788 && REG_P (XEXP (XEXP (dest, 0), 0)));
1789 offset = INTVAL (XEXP (XEXP (dest, 0), 1));
1790 if (GET_CODE (XEXP (dest, 0)) == MINUS)
1791 offset = -offset;
1793 regno = REGNO (XEXP (XEXP (dest, 0), 0));
1795 if (cfa_store.reg == (unsigned) regno)
1796 offset -= cfa_store.offset;
1797 else
1799 gcc_assert (cfa_temp.reg == (unsigned) regno);
1800 offset -= cfa_temp.offset;
1803 break;
1805 /* Rule 13 */
1806 /* Without an offset. */
1807 case REG:
1809 int regno = REGNO (XEXP (dest, 0));
1811 if (cfa_store.reg == (unsigned) regno)
1812 offset = -cfa_store.offset;
1813 else
1815 gcc_assert (cfa_temp.reg == (unsigned) regno);
1816 offset = -cfa_temp.offset;
1819 break;
1821 /* Rule 14 */
1822 case POST_INC:
1823 gcc_assert (cfa_temp.reg
1824 == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
1825 offset = -cfa_temp.offset;
1826 cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
1827 break;
1829 default:
1830 gcc_unreachable ();
1833 if (REGNO (src) != STACK_POINTER_REGNUM
1834 && REGNO (src) != HARD_FRAME_POINTER_REGNUM
1835 && (unsigned) REGNO (src) == cfa.reg)
1837 /* We're storing the current CFA reg into the stack. */
1839 if (cfa.offset == 0)
1841 /* If the source register is exactly the CFA, assume
1842 we're saving SP like any other register; this happens
1843 on the ARM. */
1844 def_cfa_1 (label, &cfa);
1845 queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
1846 break;
1848 else
1850 /* Otherwise, we'll need to look in the stack to
1851 calculate the CFA. */
1852 rtx x = XEXP (dest, 0);
1854 if (!REG_P (x))
1855 x = XEXP (x, 0);
1856 gcc_assert (REG_P (x));
1858 cfa.reg = REGNO (x);
1859 cfa.base_offset = offset;
1860 cfa.indirect = 1;
1861 def_cfa_1 (label, &cfa);
1862 break;
1866 def_cfa_1 (label, &cfa);
1867 queue_reg_save (label, src, NULL_RTX, offset);
1868 break;
1870 default:
1871 gcc_unreachable ();
1875 /* Record call frame debugging information for INSN, which either
1876 sets SP or FP (adjusting how we calculate the frame address) or saves a
1877 register to the stack. If INSN is NULL_RTX, initialize our state.
1879 If AFTER_P is false, we're being called before the insn is emitted,
1880 otherwise after. Call instructions get invoked twice. */
1882 void
1883 dwarf2out_frame_debug (rtx insn, bool after_p)
1885 const char *label;
1886 rtx src;
1888 if (insn == NULL_RTX)
1890 size_t i;
1892 /* Flush any queued register saves. */
1893 flush_queued_reg_saves ();
1895 /* Set up state for generating call frame debug info. */
1896 lookup_cfa (&cfa);
1897 gcc_assert (cfa.reg
1898 == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
1900 cfa.reg = STACK_POINTER_REGNUM;
1901 cfa_store = cfa;
1902 cfa_temp.reg = -1;
1903 cfa_temp.offset = 0;
1905 for (i = 0; i < num_regs_saved_in_regs; i++)
1907 regs_saved_in_regs[i].orig_reg = NULL_RTX;
1908 regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
1910 num_regs_saved_in_regs = 0;
1911 return;
1914 if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
1915 flush_queued_reg_saves ();
1917 if (! RTX_FRAME_RELATED_P (insn))
1919 if (!ACCUMULATE_OUTGOING_ARGS)
1920 dwarf2out_stack_adjust (insn, after_p);
1921 return;
1924 label = dwarf2out_cfi_label ();
1925 src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1926 if (src)
1927 insn = XEXP (src, 0);
1928 else
1929 insn = PATTERN (insn);
1931 dwarf2out_frame_debug_expr (insn, label);
1934 #endif
1936 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
1937 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
1938 (enum dwarf_call_frame_info cfi);
1940 static enum dw_cfi_oprnd_type
1941 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
1943 switch (cfi)
1945 case DW_CFA_nop:
1946 case DW_CFA_GNU_window_save:
1947 return dw_cfi_oprnd_unused;
1949 case DW_CFA_set_loc:
1950 case DW_CFA_advance_loc1:
1951 case DW_CFA_advance_loc2:
1952 case DW_CFA_advance_loc4:
1953 case DW_CFA_MIPS_advance_loc8:
1954 return dw_cfi_oprnd_addr;
1956 case DW_CFA_offset:
1957 case DW_CFA_offset_extended:
1958 case DW_CFA_def_cfa:
1959 case DW_CFA_offset_extended_sf:
1960 case DW_CFA_def_cfa_sf:
1961 case DW_CFA_restore_extended:
1962 case DW_CFA_undefined:
1963 case DW_CFA_same_value:
1964 case DW_CFA_def_cfa_register:
1965 case DW_CFA_register:
1966 return dw_cfi_oprnd_reg_num;
1968 case DW_CFA_def_cfa_offset:
1969 case DW_CFA_GNU_args_size:
1970 case DW_CFA_def_cfa_offset_sf:
1971 return dw_cfi_oprnd_offset;
1973 case DW_CFA_def_cfa_expression:
1974 case DW_CFA_expression:
1975 return dw_cfi_oprnd_loc;
1977 default:
1978 gcc_unreachable ();
1982 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
1983 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
1984 (enum dwarf_call_frame_info cfi);
1986 static enum dw_cfi_oprnd_type
1987 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
1989 switch (cfi)
1991 case DW_CFA_def_cfa:
1992 case DW_CFA_def_cfa_sf:
1993 case DW_CFA_offset:
1994 case DW_CFA_offset_extended_sf:
1995 case DW_CFA_offset_extended:
1996 return dw_cfi_oprnd_offset;
1998 case DW_CFA_register:
1999 return dw_cfi_oprnd_reg_num;
2001 default:
2002 return dw_cfi_oprnd_unused;
2006 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2008 /* Switch to eh_frame_section. If we don't have an eh_frame_section,
2009 switch to the data section instead, and write out a synthetic label
2010 for collect2. */
2012 static void
2013 switch_to_eh_frame_section (void)
2015 tree label;
2017 #ifdef EH_FRAME_SECTION_NAME
2018 if (eh_frame_section == 0)
2020 int flags;
2022 if (EH_TABLES_CAN_BE_READ_ONLY)
2024 int fde_encoding;
2025 int per_encoding;
2026 int lsda_encoding;
2028 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2029 /*global=*/0);
2030 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2031 /*global=*/1);
2032 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2033 /*global=*/0);
2034 flags = ((! flag_pic
2035 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2036 && (fde_encoding & 0x70) != DW_EH_PE_aligned
2037 && (per_encoding & 0x70) != DW_EH_PE_absptr
2038 && (per_encoding & 0x70) != DW_EH_PE_aligned
2039 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2040 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2041 ? 0 : SECTION_WRITE);
2043 else
2044 flags = SECTION_WRITE;
2045 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2047 #endif
2049 if (eh_frame_section)
2050 switch_to_section (eh_frame_section);
2051 else
2053 /* We have no special eh_frame section. Put the information in
2054 the data section and emit special labels to guide collect2. */
2055 switch_to_section (data_section);
2056 label = get_file_function_name ("F");
2057 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2058 targetm.asm_out.globalize_label (asm_out_file,
2059 IDENTIFIER_POINTER (label));
2060 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
2064 /* Output a Call Frame Information opcode and its operand(s). */
2066 static void
2067 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
2069 unsigned long r;
2070 if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
2071 dw2_asm_output_data (1, (cfi->dw_cfi_opc
2072 | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
2073 "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
2074 cfi->dw_cfi_oprnd1.dw_cfi_offset);
2075 else if (cfi->dw_cfi_opc == DW_CFA_offset)
2077 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2078 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2079 "DW_CFA_offset, column 0x%lx", r);
2080 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2082 else if (cfi->dw_cfi_opc == DW_CFA_restore)
2084 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2085 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2086 "DW_CFA_restore, column 0x%lx", r);
2088 else
2090 dw2_asm_output_data (1, cfi->dw_cfi_opc,
2091 "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
2093 switch (cfi->dw_cfi_opc)
2095 case DW_CFA_set_loc:
2096 if (for_eh)
2097 dw2_asm_output_encoded_addr_rtx (
2098 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
2099 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
2100 false, NULL);
2101 else
2102 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2103 cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
2104 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2105 break;
2107 case DW_CFA_advance_loc1:
2108 dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2109 fde->dw_fde_current_label, NULL);
2110 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2111 break;
2113 case DW_CFA_advance_loc2:
2114 dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2115 fde->dw_fde_current_label, NULL);
2116 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2117 break;
2119 case DW_CFA_advance_loc4:
2120 dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2121 fde->dw_fde_current_label, NULL);
2122 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2123 break;
2125 case DW_CFA_MIPS_advance_loc8:
2126 dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2127 fde->dw_fde_current_label, NULL);
2128 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2129 break;
2131 case DW_CFA_offset_extended:
2132 case DW_CFA_def_cfa:
2133 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2134 dw2_asm_output_data_uleb128 (r, NULL);
2135 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2136 break;
2138 case DW_CFA_offset_extended_sf:
2139 case DW_CFA_def_cfa_sf:
2140 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2141 dw2_asm_output_data_uleb128 (r, NULL);
2142 dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2143 break;
2145 case DW_CFA_restore_extended:
2146 case DW_CFA_undefined:
2147 case DW_CFA_same_value:
2148 case DW_CFA_def_cfa_register:
2149 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2150 dw2_asm_output_data_uleb128 (r, NULL);
2151 break;
2153 case DW_CFA_register:
2154 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2155 dw2_asm_output_data_uleb128 (r, NULL);
2156 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
2157 dw2_asm_output_data_uleb128 (r, NULL);
2158 break;
2160 case DW_CFA_def_cfa_offset:
2161 case DW_CFA_GNU_args_size:
2162 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2163 break;
2165 case DW_CFA_def_cfa_offset_sf:
2166 dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2167 break;
2169 case DW_CFA_GNU_window_save:
2170 break;
2172 case DW_CFA_def_cfa_expression:
2173 case DW_CFA_expression:
2174 output_cfa_loc (cfi);
2175 break;
2177 case DW_CFA_GNU_negative_offset_extended:
2178 /* Obsoleted by DW_CFA_offset_extended_sf. */
2179 gcc_unreachable ();
2181 default:
2182 break;
2187 /* Output the call frame information used to record information
2188 that relates to calculating the frame pointer, and records the
2189 location of saved registers. */
2191 static void
2192 output_call_frame_info (int for_eh)
2194 unsigned int i;
2195 dw_fde_ref fde;
2196 dw_cfi_ref cfi;
2197 char l1[20], l2[20], section_start_label[20];
2198 bool any_lsda_needed = false;
2199 char augmentation[6];
2200 int augmentation_size;
2201 int fde_encoding = DW_EH_PE_absptr;
2202 int per_encoding = DW_EH_PE_absptr;
2203 int lsda_encoding = DW_EH_PE_absptr;
2204 int return_reg;
2206 /* Don't emit a CIE if there won't be any FDEs. */
2207 if (fde_table_in_use == 0)
2208 return;
2210 /* If we make FDEs linkonce, we may have to emit an empty label for
2211 an FDE that wouldn't otherwise be emitted. We want to avoid
2212 having an FDE kept around when the function it refers to is
2213 discarded. Example where this matters: a primary function
2214 template in C++ requires EH information, but an explicit
2215 specialization doesn't. */
2216 if (TARGET_USES_WEAK_UNWIND_INFO
2217 && ! flag_asynchronous_unwind_tables
2218 && for_eh)
2219 for (i = 0; i < fde_table_in_use; i++)
2220 if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
2221 && !fde_table[i].uses_eh_lsda
2222 && ! DECL_WEAK (fde_table[i].decl))
2223 targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
2224 for_eh, /* empty */ 1);
2226 /* If we don't have any functions we'll want to unwind out of, don't
2227 emit any EH unwind information. Note that if exceptions aren't
2228 enabled, we won't have collected nothrow information, and if we
2229 asked for asynchronous tables, we always want this info. */
2230 if (for_eh)
2232 bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
2234 for (i = 0; i < fde_table_in_use; i++)
2235 if (fde_table[i].uses_eh_lsda)
2236 any_eh_needed = any_lsda_needed = true;
2237 else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2238 any_eh_needed = true;
2239 else if (! fde_table[i].nothrow
2240 && ! fde_table[i].all_throwers_are_sibcalls)
2241 any_eh_needed = true;
2243 if (! any_eh_needed)
2244 return;
2247 /* We're going to be generating comments, so turn on app. */
2248 if (flag_debug_asm)
2249 app_enable ();
2251 if (for_eh)
2252 switch_to_eh_frame_section ();
2253 else
2255 if (!debug_frame_section)
2256 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
2257 SECTION_DEBUG, NULL);
2258 switch_to_section (debug_frame_section);
2261 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
2262 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
2264 /* Output the CIE. */
2265 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
2266 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
2267 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2268 dw2_asm_output_data (4, 0xffffffff,
2269 "Initial length escape value indicating 64-bit DWARF extension");
2270 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2271 "Length of Common Information Entry");
2272 ASM_OUTPUT_LABEL (asm_out_file, l1);
2274 /* Now that the CIE pointer is PC-relative for EH,
2275 use 0 to identify the CIE. */
2276 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
2277 (for_eh ? 0 : DWARF_CIE_ID),
2278 "CIE Identifier Tag");
2280 dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
2282 augmentation[0] = 0;
2283 augmentation_size = 0;
2284 if (for_eh)
2286 char *p;
2288 /* Augmentation:
2289 z Indicates that a uleb128 is present to size the
2290 augmentation section.
2291 L Indicates the encoding (and thus presence) of
2292 an LSDA pointer in the FDE augmentation.
2293 R Indicates a non-default pointer encoding for
2294 FDE code pointers.
2295 P Indicates the presence of an encoding + language
2296 personality routine in the CIE augmentation. */
2298 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
2299 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
2300 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
2302 p = augmentation + 1;
2303 if (eh_personality_libfunc)
2305 *p++ = 'P';
2306 augmentation_size += 1 + size_of_encoded_value (per_encoding);
2307 assemble_external_libcall (eh_personality_libfunc);
2309 if (any_lsda_needed)
2311 *p++ = 'L';
2312 augmentation_size += 1;
2314 if (fde_encoding != DW_EH_PE_absptr)
2316 *p++ = 'R';
2317 augmentation_size += 1;
2319 if (p > augmentation + 1)
2321 augmentation[0] = 'z';
2322 *p = '\0';
2325 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
2326 if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
2328 int offset = ( 4 /* Length */
2329 + 4 /* CIE Id */
2330 + 1 /* CIE version */
2331 + strlen (augmentation) + 1 /* Augmentation */
2332 + size_of_uleb128 (1) /* Code alignment */
2333 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
2334 + 1 /* RA column */
2335 + 1 /* Augmentation size */
2336 + 1 /* Personality encoding */ );
2337 int pad = -offset & (PTR_SIZE - 1);
2339 augmentation_size += pad;
2341 /* Augmentations should be small, so there's scarce need to
2342 iterate for a solution. Die if we exceed one uleb128 byte. */
2343 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
2347 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
2348 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
2349 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
2350 "CIE Data Alignment Factor");
2352 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
2353 if (DW_CIE_VERSION == 1)
2354 dw2_asm_output_data (1, return_reg, "CIE RA Column");
2355 else
2356 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
2358 if (augmentation[0])
2360 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
2361 if (eh_personality_libfunc)
2363 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
2364 eh_data_format_name (per_encoding));
2365 dw2_asm_output_encoded_addr_rtx (per_encoding,
2366 eh_personality_libfunc,
2367 true, NULL);
2370 if (any_lsda_needed)
2371 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
2372 eh_data_format_name (lsda_encoding));
2374 if (fde_encoding != DW_EH_PE_absptr)
2375 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
2376 eh_data_format_name (fde_encoding));
2379 for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
2380 output_cfi (cfi, NULL, for_eh);
2382 /* Pad the CIE out to an address sized boundary. */
2383 ASM_OUTPUT_ALIGN (asm_out_file,
2384 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
2385 ASM_OUTPUT_LABEL (asm_out_file, l2);
2387 /* Loop through all of the FDE's. */
2388 for (i = 0; i < fde_table_in_use; i++)
2390 fde = &fde_table[i];
2392 /* Don't emit EH unwind info for leaf functions that don't need it. */
2393 if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
2394 && (fde->nothrow || fde->all_throwers_are_sibcalls)
2395 && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2396 && !fde->uses_eh_lsda)
2397 continue;
2399 targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
2400 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
2401 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
2402 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
2403 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2404 dw2_asm_output_data (4, 0xffffffff,
2405 "Initial length escape value indicating 64-bit DWARF extension");
2406 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2407 "FDE Length");
2408 ASM_OUTPUT_LABEL (asm_out_file, l1);
2410 if (for_eh)
2411 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
2412 else
2413 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
2414 debug_frame_section, "FDE CIE offset");
2416 if (for_eh)
2418 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
2419 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
2420 dw2_asm_output_encoded_addr_rtx (fde_encoding,
2421 sym_ref,
2422 false,
2423 "FDE initial location");
2424 if (fde->dw_fde_switched_sections)
2426 rtx sym_ref2 = gen_rtx_SYMBOL_REF (Pmode,
2427 fde->dw_fde_unlikely_section_label);
2428 rtx sym_ref3= gen_rtx_SYMBOL_REF (Pmode,
2429 fde->dw_fde_hot_section_label);
2430 SYMBOL_REF_FLAGS (sym_ref2) |= SYMBOL_FLAG_LOCAL;
2431 SYMBOL_REF_FLAGS (sym_ref3) |= SYMBOL_FLAG_LOCAL;
2432 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref3, false,
2433 "FDE initial location");
2434 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2435 fde->dw_fde_hot_section_end_label,
2436 fde->dw_fde_hot_section_label,
2437 "FDE address range");
2438 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref2, false,
2439 "FDE initial location");
2440 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2441 fde->dw_fde_unlikely_section_end_label,
2442 fde->dw_fde_unlikely_section_label,
2443 "FDE address range");
2445 else
2446 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2447 fde->dw_fde_end, fde->dw_fde_begin,
2448 "FDE address range");
2450 else
2452 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
2453 "FDE initial location");
2454 if (fde->dw_fde_switched_sections)
2456 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2457 fde->dw_fde_hot_section_label,
2458 "FDE initial location");
2459 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2460 fde->dw_fde_hot_section_end_label,
2461 fde->dw_fde_hot_section_label,
2462 "FDE address range");
2463 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2464 fde->dw_fde_unlikely_section_label,
2465 "FDE initial location");
2466 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2467 fde->dw_fde_unlikely_section_end_label,
2468 fde->dw_fde_unlikely_section_label,
2469 "FDE address range");
2471 else
2472 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2473 fde->dw_fde_end, fde->dw_fde_begin,
2474 "FDE address range");
2477 if (augmentation[0])
2479 if (any_lsda_needed)
2481 int size = size_of_encoded_value (lsda_encoding);
2483 if (lsda_encoding == DW_EH_PE_aligned)
2485 int offset = ( 4 /* Length */
2486 + 4 /* CIE offset */
2487 + 2 * size_of_encoded_value (fde_encoding)
2488 + 1 /* Augmentation size */ );
2489 int pad = -offset & (PTR_SIZE - 1);
2491 size += pad;
2492 gcc_assert (size_of_uleb128 (size) == 1);
2495 dw2_asm_output_data_uleb128 (size, "Augmentation size");
2497 if (fde->uses_eh_lsda)
2499 ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
2500 fde->funcdef_number);
2501 dw2_asm_output_encoded_addr_rtx (
2502 lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
2503 false, "Language Specific Data Area");
2505 else
2507 if (lsda_encoding == DW_EH_PE_aligned)
2508 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2509 dw2_asm_output_data
2510 (size_of_encoded_value (lsda_encoding), 0,
2511 "Language Specific Data Area (none)");
2514 else
2515 dw2_asm_output_data_uleb128 (0, "Augmentation size");
2518 /* Loop through the Call Frame Instructions associated with
2519 this FDE. */
2520 fde->dw_fde_current_label = fde->dw_fde_begin;
2521 for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
2522 output_cfi (cfi, fde, for_eh);
2524 /* Pad the FDE out to an address sized boundary. */
2525 ASM_OUTPUT_ALIGN (asm_out_file,
2526 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
2527 ASM_OUTPUT_LABEL (asm_out_file, l2);
2530 if (for_eh && targetm.terminate_dw2_eh_frame_info)
2531 dw2_asm_output_data (4, 0, "End of Table");
2532 #ifdef MIPS_DEBUGGING_INFO
2533 /* Work around Irix 6 assembler bug whereby labels at the end of a section
2534 get a value of 0. Putting .align 0 after the label fixes it. */
2535 ASM_OUTPUT_ALIGN (asm_out_file, 0);
2536 #endif
2538 /* Turn off app to make assembly quicker. */
2539 if (flag_debug_asm)
2540 app_disable ();
2543 /* Output a marker (i.e. a label) for the beginning of a function, before
2544 the prologue. */
2546 void
2547 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
2548 const char *file ATTRIBUTE_UNUSED)
2550 char label[MAX_ARTIFICIAL_LABEL_BYTES];
2551 char * dup_label;
2552 dw_fde_ref fde;
2554 current_function_func_begin_label = NULL;
2556 #ifdef TARGET_UNWIND_INFO
2557 /* ??? current_function_func_begin_label is also used by except.c
2558 for call-site information. We must emit this label if it might
2559 be used. */
2560 if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
2561 && ! dwarf2out_do_frame ())
2562 return;
2563 #else
2564 if (! dwarf2out_do_frame ())
2565 return;
2566 #endif
2568 switch_to_section (function_section (current_function_decl));
2569 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
2570 current_function_funcdef_no);
2571 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
2572 current_function_funcdef_no);
2573 dup_label = xstrdup (label);
2574 current_function_func_begin_label = dup_label;
2576 #ifdef TARGET_UNWIND_INFO
2577 /* We can elide the fde allocation if we're not emitting debug info. */
2578 if (! dwarf2out_do_frame ())
2579 return;
2580 #endif
2582 /* Expand the fde table if necessary. */
2583 if (fde_table_in_use == fde_table_allocated)
2585 fde_table_allocated += FDE_TABLE_INCREMENT;
2586 fde_table = ggc_realloc (fde_table,
2587 fde_table_allocated * sizeof (dw_fde_node));
2588 memset (fde_table + fde_table_in_use, 0,
2589 FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2592 /* Record the FDE associated with this function. */
2593 current_funcdef_fde = fde_table_in_use;
2595 /* Add the new FDE at the end of the fde_table. */
2596 fde = &fde_table[fde_table_in_use++];
2597 fde->decl = current_function_decl;
2598 fde->dw_fde_begin = dup_label;
2599 fde->dw_fde_current_label = dup_label;
2600 fde->dw_fde_hot_section_label = NULL;
2601 fde->dw_fde_hot_section_end_label = NULL;
2602 fde->dw_fde_unlikely_section_label = NULL;
2603 fde->dw_fde_unlikely_section_end_label = NULL;
2604 fde->dw_fde_switched_sections = false;
2605 fde->dw_fde_end = NULL;
2606 fde->dw_fde_cfi = NULL;
2607 fde->funcdef_number = current_function_funcdef_no;
2608 fde->nothrow = TREE_NOTHROW (current_function_decl);
2609 fde->uses_eh_lsda = cfun->uses_eh_lsda;
2610 fde->all_throwers_are_sibcalls = cfun->all_throwers_are_sibcalls;
2612 args_size = old_args_size = 0;
2614 /* We only want to output line number information for the genuine dwarf2
2615 prologue case, not the eh frame case. */
2616 #ifdef DWARF2_DEBUGGING_INFO
2617 if (file)
2618 dwarf2out_source_line (line, file);
2619 #endif
2622 /* Output a marker (i.e. a label) for the absolute end of the generated code
2623 for a function definition. This gets called *after* the epilogue code has
2624 been generated. */
2626 void
2627 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
2628 const char *file ATTRIBUTE_UNUSED)
2630 dw_fde_ref fde;
2631 char label[MAX_ARTIFICIAL_LABEL_BYTES];
2633 /* Output a label to mark the endpoint of the code generated for this
2634 function. */
2635 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
2636 current_function_funcdef_no);
2637 ASM_OUTPUT_LABEL (asm_out_file, label);
2638 fde = &fde_table[fde_table_in_use - 1];
2639 fde->dw_fde_end = xstrdup (label);
2642 void
2643 dwarf2out_frame_init (void)
2645 /* Allocate the initial hunk of the fde_table. */
2646 fde_table = ggc_alloc_cleared (FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2647 fde_table_allocated = FDE_TABLE_INCREMENT;
2648 fde_table_in_use = 0;
2650 /* Generate the CFA instructions common to all FDE's. Do it now for the
2651 sake of lookup_cfa. */
2653 /* On entry, the Canonical Frame Address is at SP. */
2654 dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
2656 #ifdef DWARF2_UNWIND_INFO
2657 if (DWARF2_UNWIND_INFO)
2658 initial_return_save (INCOMING_RETURN_ADDR_RTX);
2659 #endif
2662 void
2663 dwarf2out_frame_finish (void)
2665 /* Output call frame information. */
2666 if (DWARF2_FRAME_INFO)
2667 output_call_frame_info (0);
2669 #ifndef TARGET_UNWIND_INFO
2670 /* Output another copy for the unwinder. */
2671 if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
2672 output_call_frame_info (1);
2673 #endif
2675 #endif
2677 /* And now, the subset of the debugging information support code necessary
2678 for emitting location expressions. */
2680 /* Data about a single source file. */
2681 struct dwarf_file_data GTY(())
2683 const char * filename;
2684 int emitted_number;
2687 /* We need some way to distinguish DW_OP_addr with a direct symbol
2688 relocation from DW_OP_addr with a dtp-relative symbol relocation. */
2689 #define INTERNAL_DW_OP_tls_addr (0x100 + DW_OP_addr)
2692 typedef struct dw_val_struct *dw_val_ref;
2693 typedef struct die_struct *dw_die_ref;
2694 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
2695 typedef struct dw_loc_list_struct *dw_loc_list_ref;
2697 /* Each DIE may have a series of attribute/value pairs. Values
2698 can take on several forms. The forms that are used in this
2699 implementation are listed below. */
2701 enum dw_val_class
2703 dw_val_class_addr,
2704 dw_val_class_offset,
2705 dw_val_class_loc,
2706 dw_val_class_loc_list,
2707 dw_val_class_range_list,
2708 dw_val_class_const,
2709 dw_val_class_unsigned_const,
2710 dw_val_class_long_long,
2711 dw_val_class_vec,
2712 dw_val_class_flag,
2713 dw_val_class_die_ref,
2714 dw_val_class_fde_ref,
2715 dw_val_class_lbl_id,
2716 dw_val_class_lineptr,
2717 dw_val_class_str,
2718 dw_val_class_macptr,
2719 dw_val_class_file
2722 /* Describe a double word constant value. */
2723 /* ??? Every instance of long_long in the code really means CONST_DOUBLE. */
2725 typedef struct dw_long_long_struct GTY(())
2727 unsigned long hi;
2728 unsigned long low;
2730 dw_long_long_const;
2732 /* Describe a floating point constant value, or a vector constant value. */
2734 typedef struct dw_vec_struct GTY(())
2736 unsigned char * GTY((length ("%h.length"))) array;
2737 unsigned length;
2738 unsigned elt_size;
2740 dw_vec_const;
2742 /* The dw_val_node describes an attribute's value, as it is
2743 represented internally. */
2745 typedef struct dw_val_struct GTY(())
2747 enum dw_val_class val_class;
2748 union dw_val_struct_union
2750 rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
2751 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
2752 dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
2753 dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
2754 HOST_WIDE_INT GTY ((default)) val_int;
2755 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
2756 dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
2757 dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
2758 struct dw_val_die_union
2760 dw_die_ref die;
2761 int external;
2762 } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
2763 unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
2764 struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
2765 char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
2766 unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
2767 struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
2769 GTY ((desc ("%1.val_class"))) v;
2771 dw_val_node;
2773 /* Locations in memory are described using a sequence of stack machine
2774 operations. */
2776 typedef struct dw_loc_descr_struct GTY(())
2778 dw_loc_descr_ref dw_loc_next;
2779 enum dwarf_location_atom dw_loc_opc;
2780 dw_val_node dw_loc_oprnd1;
2781 dw_val_node dw_loc_oprnd2;
2782 int dw_loc_addr;
2784 dw_loc_descr_node;
2786 /* Location lists are ranges + location descriptions for that range,
2787 so you can track variables that are in different places over
2788 their entire life. */
2789 typedef struct dw_loc_list_struct GTY(())
2791 dw_loc_list_ref dw_loc_next;
2792 const char *begin; /* Label for begin address of range */
2793 const char *end; /* Label for end address of range */
2794 char *ll_symbol; /* Label for beginning of location list.
2795 Only on head of list */
2796 const char *section; /* Section this loclist is relative to */
2797 dw_loc_descr_ref expr;
2798 } dw_loc_list_node;
2800 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2802 static const char *dwarf_stack_op_name (unsigned);
2803 static dw_loc_descr_ref new_loc_descr (enum dwarf_location_atom,
2804 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2805 static void add_loc_descr (dw_loc_descr_ref *, dw_loc_descr_ref);
2806 static unsigned long size_of_loc_descr (dw_loc_descr_ref);
2807 static unsigned long size_of_locs (dw_loc_descr_ref);
2808 static void output_loc_operands (dw_loc_descr_ref);
2809 static void output_loc_sequence (dw_loc_descr_ref);
2811 /* Convert a DWARF stack opcode into its string name. */
2813 static const char *
2814 dwarf_stack_op_name (unsigned int op)
2816 switch (op)
2818 case DW_OP_addr:
2819 case INTERNAL_DW_OP_tls_addr:
2820 return "DW_OP_addr";
2821 case DW_OP_deref:
2822 return "DW_OP_deref";
2823 case DW_OP_const1u:
2824 return "DW_OP_const1u";
2825 case DW_OP_const1s:
2826 return "DW_OP_const1s";
2827 case DW_OP_const2u:
2828 return "DW_OP_const2u";
2829 case DW_OP_const2s:
2830 return "DW_OP_const2s";
2831 case DW_OP_const4u:
2832 return "DW_OP_const4u";
2833 case DW_OP_const4s:
2834 return "DW_OP_const4s";
2835 case DW_OP_const8u:
2836 return "DW_OP_const8u";
2837 case DW_OP_const8s:
2838 return "DW_OP_const8s";
2839 case DW_OP_constu:
2840 return "DW_OP_constu";
2841 case DW_OP_consts:
2842 return "DW_OP_consts";
2843 case DW_OP_dup:
2844 return "DW_OP_dup";
2845 case DW_OP_drop:
2846 return "DW_OP_drop";
2847 case DW_OP_over:
2848 return "DW_OP_over";
2849 case DW_OP_pick:
2850 return "DW_OP_pick";
2851 case DW_OP_swap:
2852 return "DW_OP_swap";
2853 case DW_OP_rot:
2854 return "DW_OP_rot";
2855 case DW_OP_xderef:
2856 return "DW_OP_xderef";
2857 case DW_OP_abs:
2858 return "DW_OP_abs";
2859 case DW_OP_and:
2860 return "DW_OP_and";
2861 case DW_OP_div:
2862 return "DW_OP_div";
2863 case DW_OP_minus:
2864 return "DW_OP_minus";
2865 case DW_OP_mod:
2866 return "DW_OP_mod";
2867 case DW_OP_mul:
2868 return "DW_OP_mul";
2869 case DW_OP_neg:
2870 return "DW_OP_neg";
2871 case DW_OP_not:
2872 return "DW_OP_not";
2873 case DW_OP_or:
2874 return "DW_OP_or";
2875 case DW_OP_plus:
2876 return "DW_OP_plus";
2877 case DW_OP_plus_uconst:
2878 return "DW_OP_plus_uconst";
2879 case DW_OP_shl:
2880 return "DW_OP_shl";
2881 case DW_OP_shr:
2882 return "DW_OP_shr";
2883 case DW_OP_shra:
2884 return "DW_OP_shra";
2885 case DW_OP_xor:
2886 return "DW_OP_xor";
2887 case DW_OP_bra:
2888 return "DW_OP_bra";
2889 case DW_OP_eq:
2890 return "DW_OP_eq";
2891 case DW_OP_ge:
2892 return "DW_OP_ge";
2893 case DW_OP_gt:
2894 return "DW_OP_gt";
2895 case DW_OP_le:
2896 return "DW_OP_le";
2897 case DW_OP_lt:
2898 return "DW_OP_lt";
2899 case DW_OP_ne:
2900 return "DW_OP_ne";
2901 case DW_OP_skip:
2902 return "DW_OP_skip";
2903 case DW_OP_lit0:
2904 return "DW_OP_lit0";
2905 case DW_OP_lit1:
2906 return "DW_OP_lit1";
2907 case DW_OP_lit2:
2908 return "DW_OP_lit2";
2909 case DW_OP_lit3:
2910 return "DW_OP_lit3";
2911 case DW_OP_lit4:
2912 return "DW_OP_lit4";
2913 case DW_OP_lit5:
2914 return "DW_OP_lit5";
2915 case DW_OP_lit6:
2916 return "DW_OP_lit6";
2917 case DW_OP_lit7:
2918 return "DW_OP_lit7";
2919 case DW_OP_lit8:
2920 return "DW_OP_lit8";
2921 case DW_OP_lit9:
2922 return "DW_OP_lit9";
2923 case DW_OP_lit10:
2924 return "DW_OP_lit10";
2925 case DW_OP_lit11:
2926 return "DW_OP_lit11";
2927 case DW_OP_lit12:
2928 return "DW_OP_lit12";
2929 case DW_OP_lit13:
2930 return "DW_OP_lit13";
2931 case DW_OP_lit14:
2932 return "DW_OP_lit14";
2933 case DW_OP_lit15:
2934 return "DW_OP_lit15";
2935 case DW_OP_lit16:
2936 return "DW_OP_lit16";
2937 case DW_OP_lit17:
2938 return "DW_OP_lit17";
2939 case DW_OP_lit18:
2940 return "DW_OP_lit18";
2941 case DW_OP_lit19:
2942 return "DW_OP_lit19";
2943 case DW_OP_lit20:
2944 return "DW_OP_lit20";
2945 case DW_OP_lit21:
2946 return "DW_OP_lit21";
2947 case DW_OP_lit22:
2948 return "DW_OP_lit22";
2949 case DW_OP_lit23:
2950 return "DW_OP_lit23";
2951 case DW_OP_lit24:
2952 return "DW_OP_lit24";
2953 case DW_OP_lit25:
2954 return "DW_OP_lit25";
2955 case DW_OP_lit26:
2956 return "DW_OP_lit26";
2957 case DW_OP_lit27:
2958 return "DW_OP_lit27";
2959 case DW_OP_lit28:
2960 return "DW_OP_lit28";
2961 case DW_OP_lit29:
2962 return "DW_OP_lit29";
2963 case DW_OP_lit30:
2964 return "DW_OP_lit30";
2965 case DW_OP_lit31:
2966 return "DW_OP_lit31";
2967 case DW_OP_reg0:
2968 return "DW_OP_reg0";
2969 case DW_OP_reg1:
2970 return "DW_OP_reg1";
2971 case DW_OP_reg2:
2972 return "DW_OP_reg2";
2973 case DW_OP_reg3:
2974 return "DW_OP_reg3";
2975 case DW_OP_reg4:
2976 return "DW_OP_reg4";
2977 case DW_OP_reg5:
2978 return "DW_OP_reg5";
2979 case DW_OP_reg6:
2980 return "DW_OP_reg6";
2981 case DW_OP_reg7:
2982 return "DW_OP_reg7";
2983 case DW_OP_reg8:
2984 return "DW_OP_reg8";
2985 case DW_OP_reg9:
2986 return "DW_OP_reg9";
2987 case DW_OP_reg10:
2988 return "DW_OP_reg10";
2989 case DW_OP_reg11:
2990 return "DW_OP_reg11";
2991 case DW_OP_reg12:
2992 return "DW_OP_reg12";
2993 case DW_OP_reg13:
2994 return "DW_OP_reg13";
2995 case DW_OP_reg14:
2996 return "DW_OP_reg14";
2997 case DW_OP_reg15:
2998 return "DW_OP_reg15";
2999 case DW_OP_reg16:
3000 return "DW_OP_reg16";
3001 case DW_OP_reg17:
3002 return "DW_OP_reg17";
3003 case DW_OP_reg18:
3004 return "DW_OP_reg18";
3005 case DW_OP_reg19:
3006 return "DW_OP_reg19";
3007 case DW_OP_reg20:
3008 return "DW_OP_reg20";
3009 case DW_OP_reg21:
3010 return "DW_OP_reg21";
3011 case DW_OP_reg22:
3012 return "DW_OP_reg22";
3013 case DW_OP_reg23:
3014 return "DW_OP_reg23";
3015 case DW_OP_reg24:
3016 return "DW_OP_reg24";
3017 case DW_OP_reg25:
3018 return "DW_OP_reg25";
3019 case DW_OP_reg26:
3020 return "DW_OP_reg26";
3021 case DW_OP_reg27:
3022 return "DW_OP_reg27";
3023 case DW_OP_reg28:
3024 return "DW_OP_reg28";
3025 case DW_OP_reg29:
3026 return "DW_OP_reg29";
3027 case DW_OP_reg30:
3028 return "DW_OP_reg30";
3029 case DW_OP_reg31:
3030 return "DW_OP_reg31";
3031 case DW_OP_breg0:
3032 return "DW_OP_breg0";
3033 case DW_OP_breg1:
3034 return "DW_OP_breg1";
3035 case DW_OP_breg2:
3036 return "DW_OP_breg2";
3037 case DW_OP_breg3:
3038 return "DW_OP_breg3";
3039 case DW_OP_breg4:
3040 return "DW_OP_breg4";
3041 case DW_OP_breg5:
3042 return "DW_OP_breg5";
3043 case DW_OP_breg6:
3044 return "DW_OP_breg6";
3045 case DW_OP_breg7:
3046 return "DW_OP_breg7";
3047 case DW_OP_breg8:
3048 return "DW_OP_breg8";
3049 case DW_OP_breg9:
3050 return "DW_OP_breg9";
3051 case DW_OP_breg10:
3052 return "DW_OP_breg10";
3053 case DW_OP_breg11:
3054 return "DW_OP_breg11";
3055 case DW_OP_breg12:
3056 return "DW_OP_breg12";
3057 case DW_OP_breg13:
3058 return "DW_OP_breg13";
3059 case DW_OP_breg14:
3060 return "DW_OP_breg14";
3061 case DW_OP_breg15:
3062 return "DW_OP_breg15";
3063 case DW_OP_breg16:
3064 return "DW_OP_breg16";
3065 case DW_OP_breg17:
3066 return "DW_OP_breg17";
3067 case DW_OP_breg18:
3068 return "DW_OP_breg18";
3069 case DW_OP_breg19:
3070 return "DW_OP_breg19";
3071 case DW_OP_breg20:
3072 return "DW_OP_breg20";
3073 case DW_OP_breg21:
3074 return "DW_OP_breg21";
3075 case DW_OP_breg22:
3076 return "DW_OP_breg22";
3077 case DW_OP_breg23:
3078 return "DW_OP_breg23";
3079 case DW_OP_breg24:
3080 return "DW_OP_breg24";
3081 case DW_OP_breg25:
3082 return "DW_OP_breg25";
3083 case DW_OP_breg26:
3084 return "DW_OP_breg26";
3085 case DW_OP_breg27:
3086 return "DW_OP_breg27";
3087 case DW_OP_breg28:
3088 return "DW_OP_breg28";
3089 case DW_OP_breg29:
3090 return "DW_OP_breg29";
3091 case DW_OP_breg30:
3092 return "DW_OP_breg30";
3093 case DW_OP_breg31:
3094 return "DW_OP_breg31";
3095 case DW_OP_regx:
3096 return "DW_OP_regx";
3097 case DW_OP_fbreg:
3098 return "DW_OP_fbreg";
3099 case DW_OP_bregx:
3100 return "DW_OP_bregx";
3101 case DW_OP_piece:
3102 return "DW_OP_piece";
3103 case DW_OP_deref_size:
3104 return "DW_OP_deref_size";
3105 case DW_OP_xderef_size:
3106 return "DW_OP_xderef_size";
3107 case DW_OP_nop:
3108 return "DW_OP_nop";
3109 case DW_OP_push_object_address:
3110 return "DW_OP_push_object_address";
3111 case DW_OP_call2:
3112 return "DW_OP_call2";
3113 case DW_OP_call4:
3114 return "DW_OP_call4";
3115 case DW_OP_call_ref:
3116 return "DW_OP_call_ref";
3117 case DW_OP_GNU_push_tls_address:
3118 return "DW_OP_GNU_push_tls_address";
3119 default:
3120 return "OP_<unknown>";
3124 /* Return a pointer to a newly allocated location description. Location
3125 descriptions are simple expression terms that can be strung
3126 together to form more complicated location (address) descriptions. */
3128 static inline dw_loc_descr_ref
3129 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
3130 unsigned HOST_WIDE_INT oprnd2)
3132 dw_loc_descr_ref descr = ggc_alloc_cleared (sizeof (dw_loc_descr_node));
3134 descr->dw_loc_opc = op;
3135 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
3136 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
3137 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
3138 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
3140 return descr;
3143 /* Add a location description term to a location description expression. */
3145 static inline void
3146 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
3148 dw_loc_descr_ref *d;
3150 /* Find the end of the chain. */
3151 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
3154 *d = descr;
3157 /* Return the size of a location descriptor. */
3159 static unsigned long
3160 size_of_loc_descr (dw_loc_descr_ref loc)
3162 unsigned long size = 1;
3164 switch (loc->dw_loc_opc)
3166 case DW_OP_addr:
3167 case INTERNAL_DW_OP_tls_addr:
3168 size += DWARF2_ADDR_SIZE;
3169 break;
3170 case DW_OP_const1u:
3171 case DW_OP_const1s:
3172 size += 1;
3173 break;
3174 case DW_OP_const2u:
3175 case DW_OP_const2s:
3176 size += 2;
3177 break;
3178 case DW_OP_const4u:
3179 case DW_OP_const4s:
3180 size += 4;
3181 break;
3182 case DW_OP_const8u:
3183 case DW_OP_const8s:
3184 size += 8;
3185 break;
3186 case DW_OP_constu:
3187 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3188 break;
3189 case DW_OP_consts:
3190 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3191 break;
3192 case DW_OP_pick:
3193 size += 1;
3194 break;
3195 case DW_OP_plus_uconst:
3196 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3197 break;
3198 case DW_OP_skip:
3199 case DW_OP_bra:
3200 size += 2;
3201 break;
3202 case DW_OP_breg0:
3203 case DW_OP_breg1:
3204 case DW_OP_breg2:
3205 case DW_OP_breg3:
3206 case DW_OP_breg4:
3207 case DW_OP_breg5:
3208 case DW_OP_breg6:
3209 case DW_OP_breg7:
3210 case DW_OP_breg8:
3211 case DW_OP_breg9:
3212 case DW_OP_breg10:
3213 case DW_OP_breg11:
3214 case DW_OP_breg12:
3215 case DW_OP_breg13:
3216 case DW_OP_breg14:
3217 case DW_OP_breg15:
3218 case DW_OP_breg16:
3219 case DW_OP_breg17:
3220 case DW_OP_breg18:
3221 case DW_OP_breg19:
3222 case DW_OP_breg20:
3223 case DW_OP_breg21:
3224 case DW_OP_breg22:
3225 case DW_OP_breg23:
3226 case DW_OP_breg24:
3227 case DW_OP_breg25:
3228 case DW_OP_breg26:
3229 case DW_OP_breg27:
3230 case DW_OP_breg28:
3231 case DW_OP_breg29:
3232 case DW_OP_breg30:
3233 case DW_OP_breg31:
3234 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3235 break;
3236 case DW_OP_regx:
3237 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3238 break;
3239 case DW_OP_fbreg:
3240 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3241 break;
3242 case DW_OP_bregx:
3243 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3244 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
3245 break;
3246 case DW_OP_piece:
3247 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3248 break;
3249 case DW_OP_deref_size:
3250 case DW_OP_xderef_size:
3251 size += 1;
3252 break;
3253 case DW_OP_call2:
3254 size += 2;
3255 break;
3256 case DW_OP_call4:
3257 size += 4;
3258 break;
3259 case DW_OP_call_ref:
3260 size += DWARF2_ADDR_SIZE;
3261 break;
3262 default:
3263 break;
3266 return size;
3269 /* Return the size of a series of location descriptors. */
3271 static unsigned long
3272 size_of_locs (dw_loc_descr_ref loc)
3274 dw_loc_descr_ref l;
3275 unsigned long size;
3277 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
3278 field, to avoid writing to a PCH file. */
3279 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3281 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
3282 break;
3283 size += size_of_loc_descr (l);
3285 if (! l)
3286 return size;
3288 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3290 l->dw_loc_addr = size;
3291 size += size_of_loc_descr (l);
3294 return size;
3297 /* Output location description stack opcode's operands (if any). */
3299 static void
3300 output_loc_operands (dw_loc_descr_ref loc)
3302 dw_val_ref val1 = &loc->dw_loc_oprnd1;
3303 dw_val_ref val2 = &loc->dw_loc_oprnd2;
3305 switch (loc->dw_loc_opc)
3307 #ifdef DWARF2_DEBUGGING_INFO
3308 case DW_OP_addr:
3309 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
3310 break;
3311 case DW_OP_const2u:
3312 case DW_OP_const2s:
3313 dw2_asm_output_data (2, val1->v.val_int, NULL);
3314 break;
3315 case DW_OP_const4u:
3316 case DW_OP_const4s:
3317 dw2_asm_output_data (4, val1->v.val_int, NULL);
3318 break;
3319 case DW_OP_const8u:
3320 case DW_OP_const8s:
3321 gcc_assert (HOST_BITS_PER_LONG >= 64);
3322 dw2_asm_output_data (8, val1->v.val_int, NULL);
3323 break;
3324 case DW_OP_skip:
3325 case DW_OP_bra:
3327 int offset;
3329 gcc_assert (val1->val_class == dw_val_class_loc);
3330 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
3332 dw2_asm_output_data (2, offset, NULL);
3334 break;
3335 #else
3336 case DW_OP_addr:
3337 case DW_OP_const2u:
3338 case DW_OP_const2s:
3339 case DW_OP_const4u:
3340 case DW_OP_const4s:
3341 case DW_OP_const8u:
3342 case DW_OP_const8s:
3343 case DW_OP_skip:
3344 case DW_OP_bra:
3345 /* We currently don't make any attempt to make sure these are
3346 aligned properly like we do for the main unwind info, so
3347 don't support emitting things larger than a byte if we're
3348 only doing unwinding. */
3349 gcc_unreachable ();
3350 #endif
3351 case DW_OP_const1u:
3352 case DW_OP_const1s:
3353 dw2_asm_output_data (1, val1->v.val_int, NULL);
3354 break;
3355 case DW_OP_constu:
3356 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3357 break;
3358 case DW_OP_consts:
3359 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3360 break;
3361 case DW_OP_pick:
3362 dw2_asm_output_data (1, val1->v.val_int, NULL);
3363 break;
3364 case DW_OP_plus_uconst:
3365 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3366 break;
3367 case DW_OP_breg0:
3368 case DW_OP_breg1:
3369 case DW_OP_breg2:
3370 case DW_OP_breg3:
3371 case DW_OP_breg4:
3372 case DW_OP_breg5:
3373 case DW_OP_breg6:
3374 case DW_OP_breg7:
3375 case DW_OP_breg8:
3376 case DW_OP_breg9:
3377 case DW_OP_breg10:
3378 case DW_OP_breg11:
3379 case DW_OP_breg12:
3380 case DW_OP_breg13:
3381 case DW_OP_breg14:
3382 case DW_OP_breg15:
3383 case DW_OP_breg16:
3384 case DW_OP_breg17:
3385 case DW_OP_breg18:
3386 case DW_OP_breg19:
3387 case DW_OP_breg20:
3388 case DW_OP_breg21:
3389 case DW_OP_breg22:
3390 case DW_OP_breg23:
3391 case DW_OP_breg24:
3392 case DW_OP_breg25:
3393 case DW_OP_breg26:
3394 case DW_OP_breg27:
3395 case DW_OP_breg28:
3396 case DW_OP_breg29:
3397 case DW_OP_breg30:
3398 case DW_OP_breg31:
3399 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3400 break;
3401 case DW_OP_regx:
3402 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3403 break;
3404 case DW_OP_fbreg:
3405 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3406 break;
3407 case DW_OP_bregx:
3408 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3409 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
3410 break;
3411 case DW_OP_piece:
3412 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3413 break;
3414 case DW_OP_deref_size:
3415 case DW_OP_xderef_size:
3416 dw2_asm_output_data (1, val1->v.val_int, NULL);
3417 break;
3419 case INTERNAL_DW_OP_tls_addr:
3420 if (targetm.asm_out.output_dwarf_dtprel)
3422 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
3423 DWARF2_ADDR_SIZE,
3424 val1->v.val_addr);
3425 fputc ('\n', asm_out_file);
3427 else
3428 gcc_unreachable ();
3429 break;
3431 default:
3432 /* Other codes have no operands. */
3433 break;
3437 /* Output a sequence of location operations. */
3439 static void
3440 output_loc_sequence (dw_loc_descr_ref loc)
3442 for (; loc != NULL; loc = loc->dw_loc_next)
3444 /* Output the opcode. */
3445 dw2_asm_output_data (1, loc->dw_loc_opc,
3446 "%s", dwarf_stack_op_name (loc->dw_loc_opc));
3448 /* Output the operand(s) (if any). */
3449 output_loc_operands (loc);
3453 /* This routine will generate the correct assembly data for a location
3454 description based on a cfi entry with a complex address. */
3456 static void
3457 output_cfa_loc (dw_cfi_ref cfi)
3459 dw_loc_descr_ref loc;
3460 unsigned long size;
3462 /* Output the size of the block. */
3463 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
3464 size = size_of_locs (loc);
3465 dw2_asm_output_data_uleb128 (size, NULL);
3467 /* Now output the operations themselves. */
3468 output_loc_sequence (loc);
3471 /* This function builds a dwarf location descriptor sequence from a
3472 dw_cfa_location, adding the given OFFSET to the result of the
3473 expression. */
3475 static struct dw_loc_descr_struct *
3476 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
3478 struct dw_loc_descr_struct *head, *tmp;
3480 offset += cfa->offset;
3482 if (cfa->indirect)
3484 if (cfa->base_offset)
3486 if (cfa->reg <= 31)
3487 head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->base_offset, 0);
3488 else
3489 head = new_loc_descr (DW_OP_bregx, cfa->reg, cfa->base_offset);
3491 else if (cfa->reg <= 31)
3492 head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3493 else
3494 head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3496 head->dw_loc_oprnd1.val_class = dw_val_class_const;
3497 tmp = new_loc_descr (DW_OP_deref, 0, 0);
3498 add_loc_descr (&head, tmp);
3499 if (offset != 0)
3501 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
3502 add_loc_descr (&head, tmp);
3505 else
3507 if (offset == 0)
3508 if (cfa->reg <= 31)
3509 head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3510 else
3511 head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3512 else if (cfa->reg <= 31)
3513 head = new_loc_descr (DW_OP_breg0 + cfa->reg, offset, 0);
3514 else
3515 head = new_loc_descr (DW_OP_bregx, cfa->reg, offset);
3518 return head;
3521 /* This function fills in aa dw_cfa_location structure from a dwarf location
3522 descriptor sequence. */
3524 static void
3525 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
3527 struct dw_loc_descr_struct *ptr;
3528 cfa->offset = 0;
3529 cfa->base_offset = 0;
3530 cfa->indirect = 0;
3531 cfa->reg = -1;
3533 for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
3535 enum dwarf_location_atom op = ptr->dw_loc_opc;
3537 switch (op)
3539 case DW_OP_reg0:
3540 case DW_OP_reg1:
3541 case DW_OP_reg2:
3542 case DW_OP_reg3:
3543 case DW_OP_reg4:
3544 case DW_OP_reg5:
3545 case DW_OP_reg6:
3546 case DW_OP_reg7:
3547 case DW_OP_reg8:
3548 case DW_OP_reg9:
3549 case DW_OP_reg10:
3550 case DW_OP_reg11:
3551 case DW_OP_reg12:
3552 case DW_OP_reg13:
3553 case DW_OP_reg14:
3554 case DW_OP_reg15:
3555 case DW_OP_reg16:
3556 case DW_OP_reg17:
3557 case DW_OP_reg18:
3558 case DW_OP_reg19:
3559 case DW_OP_reg20:
3560 case DW_OP_reg21:
3561 case DW_OP_reg22:
3562 case DW_OP_reg23:
3563 case DW_OP_reg24:
3564 case DW_OP_reg25:
3565 case DW_OP_reg26:
3566 case DW_OP_reg27:
3567 case DW_OP_reg28:
3568 case DW_OP_reg29:
3569 case DW_OP_reg30:
3570 case DW_OP_reg31:
3571 cfa->reg = op - DW_OP_reg0;
3572 break;
3573 case DW_OP_regx:
3574 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3575 break;
3576 case DW_OP_breg0:
3577 case DW_OP_breg1:
3578 case DW_OP_breg2:
3579 case DW_OP_breg3:
3580 case DW_OP_breg4:
3581 case DW_OP_breg5:
3582 case DW_OP_breg6:
3583 case DW_OP_breg7:
3584 case DW_OP_breg8:
3585 case DW_OP_breg9:
3586 case DW_OP_breg10:
3587 case DW_OP_breg11:
3588 case DW_OP_breg12:
3589 case DW_OP_breg13:
3590 case DW_OP_breg14:
3591 case DW_OP_breg15:
3592 case DW_OP_breg16:
3593 case DW_OP_breg17:
3594 case DW_OP_breg18:
3595 case DW_OP_breg19:
3596 case DW_OP_breg20:
3597 case DW_OP_breg21:
3598 case DW_OP_breg22:
3599 case DW_OP_breg23:
3600 case DW_OP_breg24:
3601 case DW_OP_breg25:
3602 case DW_OP_breg26:
3603 case DW_OP_breg27:
3604 case DW_OP_breg28:
3605 case DW_OP_breg29:
3606 case DW_OP_breg30:
3607 case DW_OP_breg31:
3608 cfa->reg = op - DW_OP_breg0;
3609 cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
3610 break;
3611 case DW_OP_bregx:
3612 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3613 cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
3614 break;
3615 case DW_OP_deref:
3616 cfa->indirect = 1;
3617 break;
3618 case DW_OP_plus_uconst:
3619 cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
3620 break;
3621 default:
3622 internal_error ("DW_LOC_OP %s not implemented",
3623 dwarf_stack_op_name (ptr->dw_loc_opc));
3627 #endif /* .debug_frame support */
3629 /* And now, the support for symbolic debugging information. */
3630 #ifdef DWARF2_DEBUGGING_INFO
3632 /* .debug_str support. */
3633 static int output_indirect_string (void **, void *);
3635 static void dwarf2out_init (const char *);
3636 static void dwarf2out_finish (const char *);
3637 static void dwarf2out_define (unsigned int, const char *);
3638 static void dwarf2out_undef (unsigned int, const char *);
3639 static void dwarf2out_start_source_file (unsigned, const char *);
3640 static void dwarf2out_end_source_file (unsigned);
3641 static void dwarf2out_begin_block (unsigned, unsigned);
3642 static void dwarf2out_end_block (unsigned, unsigned);
3643 static bool dwarf2out_ignore_block (tree);
3644 static void dwarf2out_global_decl (tree);
3645 static void dwarf2out_type_decl (tree, int);
3646 static void dwarf2out_imported_module_or_decl (tree, tree);
3647 static void dwarf2out_abstract_function (tree);
3648 static void dwarf2out_var_location (rtx);
3649 static void dwarf2out_begin_function (tree);
3650 static void dwarf2out_switch_text_section (void);
3652 /* The debug hooks structure. */
3654 const struct gcc_debug_hooks dwarf2_debug_hooks =
3656 dwarf2out_init,
3657 dwarf2out_finish,
3658 dwarf2out_define,
3659 dwarf2out_undef,
3660 dwarf2out_start_source_file,
3661 dwarf2out_end_source_file,
3662 dwarf2out_begin_block,
3663 dwarf2out_end_block,
3664 dwarf2out_ignore_block,
3665 dwarf2out_source_line,
3666 dwarf2out_begin_prologue,
3667 debug_nothing_int_charstar, /* end_prologue */
3668 dwarf2out_end_epilogue,
3669 dwarf2out_begin_function,
3670 debug_nothing_int, /* end_function */
3671 dwarf2out_decl, /* function_decl */
3672 dwarf2out_global_decl,
3673 dwarf2out_type_decl, /* type_decl */
3674 dwarf2out_imported_module_or_decl,
3675 debug_nothing_tree, /* deferred_inline_function */
3676 /* The DWARF 2 backend tries to reduce debugging bloat by not
3677 emitting the abstract description of inline functions until
3678 something tries to reference them. */
3679 dwarf2out_abstract_function, /* outlining_inline_function */
3680 debug_nothing_rtx, /* label */
3681 debug_nothing_int, /* handle_pch */
3682 dwarf2out_var_location,
3683 dwarf2out_switch_text_section,
3684 1 /* start_end_main_source_file */
3686 #endif
3688 /* NOTE: In the comments in this file, many references are made to
3689 "Debugging Information Entries". This term is abbreviated as `DIE'
3690 throughout the remainder of this file. */
3692 /* An internal representation of the DWARF output is built, and then
3693 walked to generate the DWARF debugging info. The walk of the internal
3694 representation is done after the entire program has been compiled.
3695 The types below are used to describe the internal representation. */
3697 /* Various DIE's use offsets relative to the beginning of the
3698 .debug_info section to refer to each other. */
3700 typedef long int dw_offset;
3702 /* Define typedefs here to avoid circular dependencies. */
3704 typedef struct dw_attr_struct *dw_attr_ref;
3705 typedef struct dw_line_info_struct *dw_line_info_ref;
3706 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
3707 typedef struct pubname_struct *pubname_ref;
3708 typedef struct dw_ranges_struct *dw_ranges_ref;
3710 /* Each entry in the line_info_table maintains the file and
3711 line number associated with the label generated for that
3712 entry. The label gives the PC value associated with
3713 the line number entry. */
3715 typedef struct dw_line_info_struct GTY(())
3717 unsigned long dw_file_num;
3718 unsigned long dw_line_num;
3720 dw_line_info_entry;
3722 /* Line information for functions in separate sections; each one gets its
3723 own sequence. */
3724 typedef struct dw_separate_line_info_struct GTY(())
3726 unsigned long dw_file_num;
3727 unsigned long dw_line_num;
3728 unsigned long function;
3730 dw_separate_line_info_entry;
3732 /* Each DIE attribute has a field specifying the attribute kind,
3733 a link to the next attribute in the chain, and an attribute value.
3734 Attributes are typically linked below the DIE they modify. */
3736 typedef struct dw_attr_struct GTY(())
3738 enum dwarf_attribute dw_attr;
3739 dw_val_node dw_attr_val;
3741 dw_attr_node;
3743 DEF_VEC_O(dw_attr_node);
3744 DEF_VEC_ALLOC_O(dw_attr_node,gc);
3746 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
3747 The children of each node form a circular list linked by
3748 die_sib. die_child points to the node *before* the "first" child node. */
3750 typedef struct die_struct GTY(())
3752 enum dwarf_tag die_tag;
3753 char *die_symbol;
3754 VEC(dw_attr_node,gc) * die_attr;
3755 dw_die_ref die_parent;
3756 dw_die_ref die_child;
3757 dw_die_ref die_sib;
3758 dw_die_ref die_definition; /* ref from a specification to its definition */
3759 dw_offset die_offset;
3760 unsigned long die_abbrev;
3761 int die_mark;
3762 /* Die is used and must not be pruned as unused. */
3763 int die_perennial_p;
3764 unsigned int decl_id;
3766 die_node;
3768 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
3769 #define FOR_EACH_CHILD(die, c, expr) do { \
3770 c = die->die_child; \
3771 if (c) do { \
3772 c = c->die_sib; \
3773 expr; \
3774 } while (c != die->die_child); \
3775 } while (0)
3777 /* The pubname structure */
3779 typedef struct pubname_struct GTY(())
3781 dw_die_ref die;
3782 const char *name;
3784 pubname_entry;
3786 DEF_VEC_O(pubname_entry);
3787 DEF_VEC_ALLOC_O(pubname_entry, gc);
3789 struct dw_ranges_struct GTY(())
3791 int block_num;
3794 /* The limbo die list structure. */
3795 typedef struct limbo_die_struct GTY(())
3797 dw_die_ref die;
3798 tree created_for;
3799 struct limbo_die_struct *next;
3801 limbo_die_node;
3803 /* How to start an assembler comment. */
3804 #ifndef ASM_COMMENT_START
3805 #define ASM_COMMENT_START ";#"
3806 #endif
3808 /* Define a macro which returns nonzero for a TYPE_DECL which was
3809 implicitly generated for a tagged type.
3811 Note that unlike the gcc front end (which generates a NULL named
3812 TYPE_DECL node for each complete tagged type, each array type, and
3813 each function type node created) the g++ front end generates a
3814 _named_ TYPE_DECL node for each tagged type node created.
3815 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3816 generate a DW_TAG_typedef DIE for them. */
3818 #define TYPE_DECL_IS_STUB(decl) \
3819 (DECL_NAME (decl) == NULL_TREE \
3820 || (DECL_ARTIFICIAL (decl) \
3821 && is_tagged_type (TREE_TYPE (decl)) \
3822 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3823 /* This is necessary for stub decls that \
3824 appear in nested inline functions. */ \
3825 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3826 && (decl_ultimate_origin (decl) \
3827 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3829 /* Information concerning the compilation unit's programming
3830 language, and compiler version. */
3832 /* Fixed size portion of the DWARF compilation unit header. */
3833 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3834 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
3836 /* Fixed size portion of public names info. */
3837 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3839 /* Fixed size portion of the address range info. */
3840 #define DWARF_ARANGES_HEADER_SIZE \
3841 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3842 DWARF2_ADDR_SIZE * 2) \
3843 - DWARF_INITIAL_LENGTH_SIZE)
3845 /* Size of padding portion in the address range info. It must be
3846 aligned to twice the pointer size. */
3847 #define DWARF_ARANGES_PAD_SIZE \
3848 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3849 DWARF2_ADDR_SIZE * 2) \
3850 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3852 /* Use assembler line directives if available. */
3853 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3854 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3855 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3856 #else
3857 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3858 #endif
3859 #endif
3861 /* Minimum line offset in a special line info. opcode.
3862 This value was chosen to give a reasonable range of values. */
3863 #define DWARF_LINE_BASE -10
3865 /* First special line opcode - leave room for the standard opcodes. */
3866 #define DWARF_LINE_OPCODE_BASE 10
3868 /* Range of line offsets in a special line info. opcode. */
3869 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3871 /* Flag that indicates the initial value of the is_stmt_start flag.
3872 In the present implementation, we do not mark any lines as
3873 the beginning of a source statement, because that information
3874 is not made available by the GCC front-end. */
3875 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3877 #ifdef DWARF2_DEBUGGING_INFO
3878 /* This location is used by calc_die_sizes() to keep track
3879 the offset of each DIE within the .debug_info section. */
3880 static unsigned long next_die_offset;
3881 #endif
3883 /* Record the root of the DIE's built for the current compilation unit. */
3884 static GTY(()) dw_die_ref comp_unit_die;
3886 /* A list of DIEs with a NULL parent waiting to be relocated. */
3887 static GTY(()) limbo_die_node *limbo_die_list;
3889 /* Filenames referenced by this compilation unit. */
3890 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
3892 /* A hash table of references to DIE's that describe declarations.
3893 The key is a DECL_UID() which is a unique number identifying each decl. */
3894 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
3896 /* Node of the variable location list. */
3897 struct var_loc_node GTY ((chain_next ("%h.next")))
3899 rtx GTY (()) var_loc_note;
3900 const char * GTY (()) label;
3901 const char * GTY (()) section_label;
3902 struct var_loc_node * GTY (()) next;
3905 /* Variable location list. */
3906 struct var_loc_list_def GTY (())
3908 struct var_loc_node * GTY (()) first;
3910 /* Do not mark the last element of the chained list because
3911 it is marked through the chain. */
3912 struct var_loc_node * GTY ((skip ("%h"))) last;
3914 /* DECL_UID of the variable decl. */
3915 unsigned int decl_id;
3917 typedef struct var_loc_list_def var_loc_list;
3920 /* Table of decl location linked lists. */
3921 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
3923 /* A pointer to the base of a list of references to DIE's that
3924 are uniquely identified by their tag, presence/absence of
3925 children DIE's, and list of attribute/value pairs. */
3926 static GTY((length ("abbrev_die_table_allocated")))
3927 dw_die_ref *abbrev_die_table;
3929 /* Number of elements currently allocated for abbrev_die_table. */
3930 static GTY(()) unsigned abbrev_die_table_allocated;
3932 /* Number of elements in type_die_table currently in use. */
3933 static GTY(()) unsigned abbrev_die_table_in_use;
3935 /* Size (in elements) of increments by which we may expand the
3936 abbrev_die_table. */
3937 #define ABBREV_DIE_TABLE_INCREMENT 256
3939 /* A pointer to the base of a table that contains line information
3940 for each source code line in .text in the compilation unit. */
3941 static GTY((length ("line_info_table_allocated")))
3942 dw_line_info_ref line_info_table;
3944 /* Number of elements currently allocated for line_info_table. */
3945 static GTY(()) unsigned line_info_table_allocated;
3947 /* Number of elements in line_info_table currently in use. */
3948 static GTY(()) unsigned line_info_table_in_use;
3950 /* True if the compilation unit places functions in more than one section. */
3951 static GTY(()) bool have_multiple_function_sections = false;
3953 /* A pointer to the base of a table that contains line information
3954 for each source code line outside of .text in the compilation unit. */
3955 static GTY ((length ("separate_line_info_table_allocated")))
3956 dw_separate_line_info_ref separate_line_info_table;
3958 /* Number of elements currently allocated for separate_line_info_table. */
3959 static GTY(()) unsigned separate_line_info_table_allocated;
3961 /* Number of elements in separate_line_info_table currently in use. */
3962 static GTY(()) unsigned separate_line_info_table_in_use;
3964 /* Size (in elements) of increments by which we may expand the
3965 line_info_table. */
3966 #define LINE_INFO_TABLE_INCREMENT 1024
3968 /* A pointer to the base of a table that contains a list of publicly
3969 accessible names. */
3970 static GTY (()) VEC (pubname_entry, gc) * pubname_table;
3972 /* A pointer to the base of a table that contains a list of publicly
3973 accessible types. */
3974 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
3976 /* Array of dies for which we should generate .debug_arange info. */
3977 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
3979 /* Number of elements currently allocated for arange_table. */
3980 static GTY(()) unsigned arange_table_allocated;
3982 /* Number of elements in arange_table currently in use. */
3983 static GTY(()) unsigned arange_table_in_use;
3985 /* Size (in elements) of increments by which we may expand the
3986 arange_table. */
3987 #define ARANGE_TABLE_INCREMENT 64
3989 /* Array of dies for which we should generate .debug_ranges info. */
3990 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
3992 /* Number of elements currently allocated for ranges_table. */
3993 static GTY(()) unsigned ranges_table_allocated;
3995 /* Number of elements in ranges_table currently in use. */
3996 static GTY(()) unsigned ranges_table_in_use;
3998 /* Size (in elements) of increments by which we may expand the
3999 ranges_table. */
4000 #define RANGES_TABLE_INCREMENT 64
4002 /* Whether we have location lists that need outputting */
4003 static GTY(()) bool have_location_lists;
4005 /* Unique label counter. */
4006 static GTY(()) unsigned int loclabel_num;
4008 #ifdef DWARF2_DEBUGGING_INFO
4009 /* Record whether the function being analyzed contains inlined functions. */
4010 static int current_function_has_inlines;
4011 #endif
4012 #if 0 && defined (MIPS_DEBUGGING_INFO)
4013 static int comp_unit_has_inlines;
4014 #endif
4016 /* The last file entry emitted by maybe_emit_file(). */
4017 static GTY(()) struct dwarf_file_data * last_emitted_file;
4019 /* Number of internal labels generated by gen_internal_sym(). */
4020 static GTY(()) int label_num;
4022 /* Cached result of previous call to lookup_filename. */
4023 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
4025 #ifdef DWARF2_DEBUGGING_INFO
4027 /* Offset from the "steady-state frame pointer" to the frame base,
4028 within the current function. */
4029 static HOST_WIDE_INT frame_pointer_fb_offset;
4031 /* Forward declarations for functions defined in this file. */
4033 static int is_pseudo_reg (rtx);
4034 static tree type_main_variant (tree);
4035 static int is_tagged_type (tree);
4036 static const char *dwarf_tag_name (unsigned);
4037 static const char *dwarf_attr_name (unsigned);
4038 static const char *dwarf_form_name (unsigned);
4039 static tree decl_ultimate_origin (tree);
4040 static tree block_ultimate_origin (tree);
4041 static tree decl_class_context (tree);
4042 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
4043 static inline enum dw_val_class AT_class (dw_attr_ref);
4044 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
4045 static inline unsigned AT_flag (dw_attr_ref);
4046 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
4047 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
4048 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
4049 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
4050 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
4051 unsigned long);
4052 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
4053 unsigned int, unsigned char *);
4054 static hashval_t debug_str_do_hash (const void *);
4055 static int debug_str_eq (const void *, const void *);
4056 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
4057 static inline const char *AT_string (dw_attr_ref);
4058 static int AT_string_form (dw_attr_ref);
4059 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
4060 static void add_AT_specification (dw_die_ref, dw_die_ref);
4061 static inline dw_die_ref AT_ref (dw_attr_ref);
4062 static inline int AT_ref_external (dw_attr_ref);
4063 static inline void set_AT_ref_external (dw_attr_ref, int);
4064 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
4065 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
4066 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
4067 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
4068 dw_loc_list_ref);
4069 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
4070 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
4071 static inline rtx AT_addr (dw_attr_ref);
4072 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
4073 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
4074 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
4075 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
4076 unsigned HOST_WIDE_INT);
4077 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
4078 unsigned long);
4079 static inline const char *AT_lbl (dw_attr_ref);
4080 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
4081 static const char *get_AT_low_pc (dw_die_ref);
4082 static const char *get_AT_hi_pc (dw_die_ref);
4083 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
4084 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
4085 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
4086 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
4087 static bool is_c_family (void);
4088 static bool is_cxx (void);
4089 static bool is_java (void);
4090 static bool is_fortran (void);
4091 static bool is_ada (void);
4092 static void remove_AT (dw_die_ref, enum dwarf_attribute);
4093 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
4094 static void add_child_die (dw_die_ref, dw_die_ref);
4095 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
4096 static dw_die_ref lookup_type_die (tree);
4097 static void equate_type_number_to_die (tree, dw_die_ref);
4098 static hashval_t decl_die_table_hash (const void *);
4099 static int decl_die_table_eq (const void *, const void *);
4100 static dw_die_ref lookup_decl_die (tree);
4101 static hashval_t decl_loc_table_hash (const void *);
4102 static int decl_loc_table_eq (const void *, const void *);
4103 static var_loc_list *lookup_decl_loc (tree);
4104 static void equate_decl_number_to_die (tree, dw_die_ref);
4105 static void add_var_loc_to_decl (tree, struct var_loc_node *);
4106 static void print_spaces (FILE *);
4107 static void print_die (dw_die_ref, FILE *);
4108 static void print_dwarf_line_table (FILE *);
4109 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
4110 static dw_die_ref pop_compile_unit (dw_die_ref);
4111 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
4112 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
4113 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
4114 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
4115 static int same_dw_val_p (dw_val_node *, dw_val_node *, int *);
4116 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
4117 static int same_die_p (dw_die_ref, dw_die_ref, int *);
4118 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
4119 static void compute_section_prefix (dw_die_ref);
4120 static int is_type_die (dw_die_ref);
4121 static int is_comdat_die (dw_die_ref);
4122 static int is_symbol_die (dw_die_ref);
4123 static void assign_symbol_names (dw_die_ref);
4124 static void break_out_includes (dw_die_ref);
4125 static hashval_t htab_cu_hash (const void *);
4126 static int htab_cu_eq (const void *, const void *);
4127 static void htab_cu_del (void *);
4128 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
4129 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
4130 static void add_sibling_attributes (dw_die_ref);
4131 static void build_abbrev_table (dw_die_ref);
4132 static void output_location_lists (dw_die_ref);
4133 static int constant_size (long unsigned);
4134 static unsigned long size_of_die (dw_die_ref);
4135 static void calc_die_sizes (dw_die_ref);
4136 static void mark_dies (dw_die_ref);
4137 static void unmark_dies (dw_die_ref);
4138 static void unmark_all_dies (dw_die_ref);
4139 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
4140 static unsigned long size_of_aranges (void);
4141 static enum dwarf_form value_format (dw_attr_ref);
4142 static void output_value_format (dw_attr_ref);
4143 static void output_abbrev_section (void);
4144 static void output_die_symbol (dw_die_ref);
4145 static void output_die (dw_die_ref);
4146 static void output_compilation_unit_header (void);
4147 static void output_comp_unit (dw_die_ref, int);
4148 static const char *dwarf2_name (tree, int);
4149 static void add_pubname (tree, dw_die_ref);
4150 static void add_pubtype (tree, dw_die_ref);
4151 static void output_pubnames (VEC (pubname_entry,gc) *);
4152 static void add_arange (tree, dw_die_ref);
4153 static void output_aranges (void);
4154 static unsigned int add_ranges (tree);
4155 static void output_ranges (void);
4156 static void output_line_info (void);
4157 static void output_file_names (void);
4158 static dw_die_ref base_type_die (tree);
4159 static int is_base_type (tree);
4160 static bool is_subrange_type (tree);
4161 static dw_die_ref subrange_type_die (tree, dw_die_ref);
4162 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
4163 static int type_is_enum (tree);
4164 static unsigned int dbx_reg_number (rtx);
4165 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
4166 static dw_loc_descr_ref reg_loc_descriptor (rtx);
4167 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int);
4168 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx);
4169 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4170 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT);
4171 static int is_based_loc (rtx);
4172 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode);
4173 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx);
4174 static dw_loc_descr_ref loc_descriptor (rtx);
4175 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
4176 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
4177 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
4178 static tree field_type (tree);
4179 static unsigned int simple_type_align_in_bits (tree);
4180 static unsigned int simple_decl_align_in_bits (tree);
4181 static unsigned HOST_WIDE_INT simple_type_size_in_bits (tree);
4182 static HOST_WIDE_INT field_byte_offset (tree);
4183 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
4184 dw_loc_descr_ref);
4185 static void add_data_member_location_attribute (dw_die_ref, tree);
4186 static void add_const_value_attribute (dw_die_ref, rtx);
4187 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
4188 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4189 static void insert_float (rtx, unsigned char *);
4190 static rtx rtl_for_decl_location (tree);
4191 static void add_location_or_const_value_attribute (dw_die_ref, tree,
4192 enum dwarf_attribute);
4193 static void tree_add_const_value_attribute (dw_die_ref, tree);
4194 static void add_name_attribute (dw_die_ref, const char *);
4195 static void add_comp_dir_attribute (dw_die_ref);
4196 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
4197 static void add_subscript_info (dw_die_ref, tree);
4198 static void add_byte_size_attribute (dw_die_ref, tree);
4199 static void add_bit_offset_attribute (dw_die_ref, tree);
4200 static void add_bit_size_attribute (dw_die_ref, tree);
4201 static void add_prototyped_attribute (dw_die_ref, tree);
4202 static void add_abstract_origin_attribute (dw_die_ref, tree);
4203 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
4204 static void add_src_coords_attributes (dw_die_ref, tree);
4205 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
4206 static void push_decl_scope (tree);
4207 static void pop_decl_scope (void);
4208 static dw_die_ref scope_die_for (tree, dw_die_ref);
4209 static inline int local_scope_p (dw_die_ref);
4210 static inline int class_or_namespace_scope_p (dw_die_ref);
4211 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
4212 static void add_calling_convention_attribute (dw_die_ref, tree);
4213 static const char *type_tag (tree);
4214 static tree member_declared_type (tree);
4215 #if 0
4216 static const char *decl_start_label (tree);
4217 #endif
4218 static void gen_array_type_die (tree, dw_die_ref);
4219 #if 0
4220 static void gen_entry_point_die (tree, dw_die_ref);
4221 #endif
4222 static void gen_inlined_enumeration_type_die (tree, dw_die_ref);
4223 static void gen_inlined_structure_type_die (tree, dw_die_ref);
4224 static void gen_inlined_union_type_die (tree, dw_die_ref);
4225 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
4226 static dw_die_ref gen_formal_parameter_die (tree, dw_die_ref);
4227 static void gen_unspecified_parameters_die (tree, dw_die_ref);
4228 static void gen_formal_types_die (tree, dw_die_ref);
4229 static void gen_subprogram_die (tree, dw_die_ref);
4230 static void gen_variable_die (tree, dw_die_ref);
4231 static void gen_label_die (tree, dw_die_ref);
4232 static void gen_lexical_block_die (tree, dw_die_ref, int);
4233 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
4234 static void gen_field_die (tree, dw_die_ref);
4235 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
4236 static dw_die_ref gen_compile_unit_die (const char *);
4237 static void gen_inheritance_die (tree, tree, dw_die_ref);
4238 static void gen_member_die (tree, dw_die_ref);
4239 static void gen_struct_or_union_type_die (tree, dw_die_ref,
4240 enum debug_info_usage);
4241 static void gen_subroutine_type_die (tree, dw_die_ref);
4242 static void gen_typedef_die (tree, dw_die_ref);
4243 static void gen_type_die (tree, dw_die_ref);
4244 static void gen_tagged_type_instantiation_die (tree, dw_die_ref);
4245 static void gen_block_die (tree, dw_die_ref, int);
4246 static void decls_for_scope (tree, dw_die_ref, int);
4247 static int is_redundant_typedef (tree);
4248 static void gen_namespace_die (tree);
4249 static void gen_decl_die (tree, dw_die_ref);
4250 static dw_die_ref force_decl_die (tree);
4251 static dw_die_ref force_type_die (tree);
4252 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
4253 static void declare_in_namespace (tree, dw_die_ref);
4254 static struct dwarf_file_data * lookup_filename (const char *);
4255 static void retry_incomplete_types (void);
4256 static void gen_type_die_for_member (tree, tree, dw_die_ref);
4257 static void splice_child_die (dw_die_ref, dw_die_ref);
4258 static int file_info_cmp (const void *, const void *);
4259 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
4260 const char *, const char *, unsigned);
4261 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
4262 const char *, const char *,
4263 const char *);
4264 static void output_loc_list (dw_loc_list_ref);
4265 static char *gen_internal_sym (const char *);
4267 static void prune_unmark_dies (dw_die_ref);
4268 static void prune_unused_types_mark (dw_die_ref, int);
4269 static void prune_unused_types_walk (dw_die_ref);
4270 static void prune_unused_types_walk_attribs (dw_die_ref);
4271 static void prune_unused_types_prune (dw_die_ref);
4272 static void prune_unused_types (void);
4273 static int maybe_emit_file (struct dwarf_file_data *fd);
4275 /* Section names used to hold DWARF debugging information. */
4276 #ifndef DEBUG_INFO_SECTION
4277 #define DEBUG_INFO_SECTION ".debug_info"
4278 #endif
4279 #ifndef DEBUG_ABBREV_SECTION
4280 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
4281 #endif
4282 #ifndef DEBUG_ARANGES_SECTION
4283 #define DEBUG_ARANGES_SECTION ".debug_aranges"
4284 #endif
4285 #ifndef DEBUG_MACINFO_SECTION
4286 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
4287 #endif
4288 #ifndef DEBUG_LINE_SECTION
4289 #define DEBUG_LINE_SECTION ".debug_line"
4290 #endif
4291 #ifndef DEBUG_LOC_SECTION
4292 #define DEBUG_LOC_SECTION ".debug_loc"
4293 #endif
4294 #ifndef DEBUG_PUBNAMES_SECTION
4295 #define DEBUG_PUBNAMES_SECTION ".debug_pubnames"
4296 #endif
4297 #ifndef DEBUG_STR_SECTION
4298 #define DEBUG_STR_SECTION ".debug_str"
4299 #endif
4300 #ifndef DEBUG_RANGES_SECTION
4301 #define DEBUG_RANGES_SECTION ".debug_ranges"
4302 #endif
4304 /* Standard ELF section names for compiled code and data. */
4305 #ifndef TEXT_SECTION_NAME
4306 #define TEXT_SECTION_NAME ".text"
4307 #endif
4309 /* Section flags for .debug_str section. */
4310 #define DEBUG_STR_SECTION_FLAGS \
4311 (HAVE_GAS_SHF_MERGE && flag_merge_constants \
4312 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
4313 : SECTION_DEBUG)
4315 /* Labels we insert at beginning sections we can reference instead of
4316 the section names themselves. */
4318 #ifndef TEXT_SECTION_LABEL
4319 #define TEXT_SECTION_LABEL "Ltext"
4320 #endif
4321 #ifndef COLD_TEXT_SECTION_LABEL
4322 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
4323 #endif
4324 #ifndef DEBUG_LINE_SECTION_LABEL
4325 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
4326 #endif
4327 #ifndef DEBUG_INFO_SECTION_LABEL
4328 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
4329 #endif
4330 #ifndef DEBUG_ABBREV_SECTION_LABEL
4331 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
4332 #endif
4333 #ifndef DEBUG_LOC_SECTION_LABEL
4334 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
4335 #endif
4336 #ifndef DEBUG_RANGES_SECTION_LABEL
4337 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
4338 #endif
4339 #ifndef DEBUG_MACINFO_SECTION_LABEL
4340 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
4341 #endif
4343 /* Definitions of defaults for formats and names of various special
4344 (artificial) labels which may be generated within this file (when the -g
4345 options is used and DWARF2_DEBUGGING_INFO is in effect.
4346 If necessary, these may be overridden from within the tm.h file, but
4347 typically, overriding these defaults is unnecessary. */
4349 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4350 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4351 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4352 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4353 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4354 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4355 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4356 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4357 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4358 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4360 #ifndef TEXT_END_LABEL
4361 #define TEXT_END_LABEL "Letext"
4362 #endif
4363 #ifndef COLD_END_LABEL
4364 #define COLD_END_LABEL "Letext_cold"
4365 #endif
4366 #ifndef BLOCK_BEGIN_LABEL
4367 #define BLOCK_BEGIN_LABEL "LBB"
4368 #endif
4369 #ifndef BLOCK_END_LABEL
4370 #define BLOCK_END_LABEL "LBE"
4371 #endif
4372 #ifndef LINE_CODE_LABEL
4373 #define LINE_CODE_LABEL "LM"
4374 #endif
4375 #ifndef SEPARATE_LINE_CODE_LABEL
4376 #define SEPARATE_LINE_CODE_LABEL "LSM"
4377 #endif
4379 /* We allow a language front-end to designate a function that is to be
4380 called to "demangle" any name before it is put into a DIE. */
4382 static const char *(*demangle_name_func) (const char *);
4384 void
4385 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4387 demangle_name_func = func;
4390 /* Test if rtl node points to a pseudo register. */
4392 static inline int
4393 is_pseudo_reg (rtx rtl)
4395 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4396 || (GET_CODE (rtl) == SUBREG
4397 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4400 /* Return a reference to a type, with its const and volatile qualifiers
4401 removed. */
4403 static inline tree
4404 type_main_variant (tree type)
4406 type = TYPE_MAIN_VARIANT (type);
4408 /* ??? There really should be only one main variant among any group of
4409 variants of a given type (and all of the MAIN_VARIANT values for all
4410 members of the group should point to that one type) but sometimes the C
4411 front-end messes this up for array types, so we work around that bug
4412 here. */
4413 if (TREE_CODE (type) == ARRAY_TYPE)
4414 while (type != TYPE_MAIN_VARIANT (type))
4415 type = TYPE_MAIN_VARIANT (type);
4417 return type;
4420 /* Return nonzero if the given type node represents a tagged type. */
4422 static inline int
4423 is_tagged_type (tree type)
4425 enum tree_code code = TREE_CODE (type);
4427 return (code == RECORD_TYPE || code == UNION_TYPE
4428 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4431 /* Convert a DIE tag into its string name. */
4433 static const char *
4434 dwarf_tag_name (unsigned int tag)
4436 switch (tag)
4438 case DW_TAG_padding:
4439 return "DW_TAG_padding";
4440 case DW_TAG_array_type:
4441 return "DW_TAG_array_type";
4442 case DW_TAG_class_type:
4443 return "DW_TAG_class_type";
4444 case DW_TAG_entry_point:
4445 return "DW_TAG_entry_point";
4446 case DW_TAG_enumeration_type:
4447 return "DW_TAG_enumeration_type";
4448 case DW_TAG_formal_parameter:
4449 return "DW_TAG_formal_parameter";
4450 case DW_TAG_imported_declaration:
4451 return "DW_TAG_imported_declaration";
4452 case DW_TAG_label:
4453 return "DW_TAG_label";
4454 case DW_TAG_lexical_block:
4455 return "DW_TAG_lexical_block";
4456 case DW_TAG_member:
4457 return "DW_TAG_member";
4458 case DW_TAG_pointer_type:
4459 return "DW_TAG_pointer_type";
4460 case DW_TAG_reference_type:
4461 return "DW_TAG_reference_type";
4462 case DW_TAG_compile_unit:
4463 return "DW_TAG_compile_unit";
4464 case DW_TAG_string_type:
4465 return "DW_TAG_string_type";
4466 case DW_TAG_structure_type:
4467 return "DW_TAG_structure_type";
4468 case DW_TAG_subroutine_type:
4469 return "DW_TAG_subroutine_type";
4470 case DW_TAG_typedef:
4471 return "DW_TAG_typedef";
4472 case DW_TAG_union_type:
4473 return "DW_TAG_union_type";
4474 case DW_TAG_unspecified_parameters:
4475 return "DW_TAG_unspecified_parameters";
4476 case DW_TAG_variant:
4477 return "DW_TAG_variant";
4478 case DW_TAG_common_block:
4479 return "DW_TAG_common_block";
4480 case DW_TAG_common_inclusion:
4481 return "DW_TAG_common_inclusion";
4482 case DW_TAG_inheritance:
4483 return "DW_TAG_inheritance";
4484 case DW_TAG_inlined_subroutine:
4485 return "DW_TAG_inlined_subroutine";
4486 case DW_TAG_module:
4487 return "DW_TAG_module";
4488 case DW_TAG_ptr_to_member_type:
4489 return "DW_TAG_ptr_to_member_type";
4490 case DW_TAG_set_type:
4491 return "DW_TAG_set_type";
4492 case DW_TAG_subrange_type:
4493 return "DW_TAG_subrange_type";
4494 case DW_TAG_with_stmt:
4495 return "DW_TAG_with_stmt";
4496 case DW_TAG_access_declaration:
4497 return "DW_TAG_access_declaration";
4498 case DW_TAG_base_type:
4499 return "DW_TAG_base_type";
4500 case DW_TAG_catch_block:
4501 return "DW_TAG_catch_block";
4502 case DW_TAG_const_type:
4503 return "DW_TAG_const_type";
4504 case DW_TAG_constant:
4505 return "DW_TAG_constant";
4506 case DW_TAG_enumerator:
4507 return "DW_TAG_enumerator";
4508 case DW_TAG_file_type:
4509 return "DW_TAG_file_type";
4510 case DW_TAG_friend:
4511 return "DW_TAG_friend";
4512 case DW_TAG_namelist:
4513 return "DW_TAG_namelist";
4514 case DW_TAG_namelist_item:
4515 return "DW_TAG_namelist_item";
4516 case DW_TAG_namespace:
4517 return "DW_TAG_namespace";
4518 case DW_TAG_packed_type:
4519 return "DW_TAG_packed_type";
4520 case DW_TAG_subprogram:
4521 return "DW_TAG_subprogram";
4522 case DW_TAG_template_type_param:
4523 return "DW_TAG_template_type_param";
4524 case DW_TAG_template_value_param:
4525 return "DW_TAG_template_value_param";
4526 case DW_TAG_thrown_type:
4527 return "DW_TAG_thrown_type";
4528 case DW_TAG_try_block:
4529 return "DW_TAG_try_block";
4530 case DW_TAG_variant_part:
4531 return "DW_TAG_variant_part";
4532 case DW_TAG_variable:
4533 return "DW_TAG_variable";
4534 case DW_TAG_volatile_type:
4535 return "DW_TAG_volatile_type";
4536 case DW_TAG_imported_module:
4537 return "DW_TAG_imported_module";
4538 case DW_TAG_MIPS_loop:
4539 return "DW_TAG_MIPS_loop";
4540 case DW_TAG_format_label:
4541 return "DW_TAG_format_label";
4542 case DW_TAG_function_template:
4543 return "DW_TAG_function_template";
4544 case DW_TAG_class_template:
4545 return "DW_TAG_class_template";
4546 case DW_TAG_GNU_BINCL:
4547 return "DW_TAG_GNU_BINCL";
4548 case DW_TAG_GNU_EINCL:
4549 return "DW_TAG_GNU_EINCL";
4550 default:
4551 return "DW_TAG_<unknown>";
4555 /* Convert a DWARF attribute code into its string name. */
4557 static const char *
4558 dwarf_attr_name (unsigned int attr)
4560 switch (attr)
4562 case DW_AT_sibling:
4563 return "DW_AT_sibling";
4564 case DW_AT_location:
4565 return "DW_AT_location";
4566 case DW_AT_name:
4567 return "DW_AT_name";
4568 case DW_AT_ordering:
4569 return "DW_AT_ordering";
4570 case DW_AT_subscr_data:
4571 return "DW_AT_subscr_data";
4572 case DW_AT_byte_size:
4573 return "DW_AT_byte_size";
4574 case DW_AT_bit_offset:
4575 return "DW_AT_bit_offset";
4576 case DW_AT_bit_size:
4577 return "DW_AT_bit_size";
4578 case DW_AT_element_list:
4579 return "DW_AT_element_list";
4580 case DW_AT_stmt_list:
4581 return "DW_AT_stmt_list";
4582 case DW_AT_low_pc:
4583 return "DW_AT_low_pc";
4584 case DW_AT_high_pc:
4585 return "DW_AT_high_pc";
4586 case DW_AT_language:
4587 return "DW_AT_language";
4588 case DW_AT_member:
4589 return "DW_AT_member";
4590 case DW_AT_discr:
4591 return "DW_AT_discr";
4592 case DW_AT_discr_value:
4593 return "DW_AT_discr_value";
4594 case DW_AT_visibility:
4595 return "DW_AT_visibility";
4596 case DW_AT_import:
4597 return "DW_AT_import";
4598 case DW_AT_string_length:
4599 return "DW_AT_string_length";
4600 case DW_AT_common_reference:
4601 return "DW_AT_common_reference";
4602 case DW_AT_comp_dir:
4603 return "DW_AT_comp_dir";
4604 case DW_AT_const_value:
4605 return "DW_AT_const_value";
4606 case DW_AT_containing_type:
4607 return "DW_AT_containing_type";
4608 case DW_AT_default_value:
4609 return "DW_AT_default_value";
4610 case DW_AT_inline:
4611 return "DW_AT_inline";
4612 case DW_AT_is_optional:
4613 return "DW_AT_is_optional";
4614 case DW_AT_lower_bound:
4615 return "DW_AT_lower_bound";
4616 case DW_AT_producer:
4617 return "DW_AT_producer";
4618 case DW_AT_prototyped:
4619 return "DW_AT_prototyped";
4620 case DW_AT_return_addr:
4621 return "DW_AT_return_addr";
4622 case DW_AT_start_scope:
4623 return "DW_AT_start_scope";
4624 case DW_AT_stride_size:
4625 return "DW_AT_stride_size";
4626 case DW_AT_upper_bound:
4627 return "DW_AT_upper_bound";
4628 case DW_AT_abstract_origin:
4629 return "DW_AT_abstract_origin";
4630 case DW_AT_accessibility:
4631 return "DW_AT_accessibility";
4632 case DW_AT_address_class:
4633 return "DW_AT_address_class";
4634 case DW_AT_artificial:
4635 return "DW_AT_artificial";
4636 case DW_AT_base_types:
4637 return "DW_AT_base_types";
4638 case DW_AT_calling_convention:
4639 return "DW_AT_calling_convention";
4640 case DW_AT_count:
4641 return "DW_AT_count";
4642 case DW_AT_data_member_location:
4643 return "DW_AT_data_member_location";
4644 case DW_AT_decl_column:
4645 return "DW_AT_decl_column";
4646 case DW_AT_decl_file:
4647 return "DW_AT_decl_file";
4648 case DW_AT_decl_line:
4649 return "DW_AT_decl_line";
4650 case DW_AT_declaration:
4651 return "DW_AT_declaration";
4652 case DW_AT_discr_list:
4653 return "DW_AT_discr_list";
4654 case DW_AT_encoding:
4655 return "DW_AT_encoding";
4656 case DW_AT_external:
4657 return "DW_AT_external";
4658 case DW_AT_frame_base:
4659 return "DW_AT_frame_base";
4660 case DW_AT_friend:
4661 return "DW_AT_friend";
4662 case DW_AT_identifier_case:
4663 return "DW_AT_identifier_case";
4664 case DW_AT_macro_info:
4665 return "DW_AT_macro_info";
4666 case DW_AT_namelist_items:
4667 return "DW_AT_namelist_items";
4668 case DW_AT_priority:
4669 return "DW_AT_priority";
4670 case DW_AT_segment:
4671 return "DW_AT_segment";
4672 case DW_AT_specification:
4673 return "DW_AT_specification";
4674 case DW_AT_static_link:
4675 return "DW_AT_static_link";
4676 case DW_AT_type:
4677 return "DW_AT_type";
4678 case DW_AT_use_location:
4679 return "DW_AT_use_location";
4680 case DW_AT_variable_parameter:
4681 return "DW_AT_variable_parameter";
4682 case DW_AT_virtuality:
4683 return "DW_AT_virtuality";
4684 case DW_AT_vtable_elem_location:
4685 return "DW_AT_vtable_elem_location";
4687 case DW_AT_allocated:
4688 return "DW_AT_allocated";
4689 case DW_AT_associated:
4690 return "DW_AT_associated";
4691 case DW_AT_data_location:
4692 return "DW_AT_data_location";
4693 case DW_AT_stride:
4694 return "DW_AT_stride";
4695 case DW_AT_entry_pc:
4696 return "DW_AT_entry_pc";
4697 case DW_AT_use_UTF8:
4698 return "DW_AT_use_UTF8";
4699 case DW_AT_extension:
4700 return "DW_AT_extension";
4701 case DW_AT_ranges:
4702 return "DW_AT_ranges";
4703 case DW_AT_trampoline:
4704 return "DW_AT_trampoline";
4705 case DW_AT_call_column:
4706 return "DW_AT_call_column";
4707 case DW_AT_call_file:
4708 return "DW_AT_call_file";
4709 case DW_AT_call_line:
4710 return "DW_AT_call_line";
4712 case DW_AT_MIPS_fde:
4713 return "DW_AT_MIPS_fde";
4714 case DW_AT_MIPS_loop_begin:
4715 return "DW_AT_MIPS_loop_begin";
4716 case DW_AT_MIPS_tail_loop_begin:
4717 return "DW_AT_MIPS_tail_loop_begin";
4718 case DW_AT_MIPS_epilog_begin:
4719 return "DW_AT_MIPS_epilog_begin";
4720 case DW_AT_MIPS_loop_unroll_factor:
4721 return "DW_AT_MIPS_loop_unroll_factor";
4722 case DW_AT_MIPS_software_pipeline_depth:
4723 return "DW_AT_MIPS_software_pipeline_depth";
4724 case DW_AT_MIPS_linkage_name:
4725 return "DW_AT_MIPS_linkage_name";
4726 case DW_AT_MIPS_stride:
4727 return "DW_AT_MIPS_stride";
4728 case DW_AT_MIPS_abstract_name:
4729 return "DW_AT_MIPS_abstract_name";
4730 case DW_AT_MIPS_clone_origin:
4731 return "DW_AT_MIPS_clone_origin";
4732 case DW_AT_MIPS_has_inlines:
4733 return "DW_AT_MIPS_has_inlines";
4735 case DW_AT_sf_names:
4736 return "DW_AT_sf_names";
4737 case DW_AT_src_info:
4738 return "DW_AT_src_info";
4739 case DW_AT_mac_info:
4740 return "DW_AT_mac_info";
4741 case DW_AT_src_coords:
4742 return "DW_AT_src_coords";
4743 case DW_AT_body_begin:
4744 return "DW_AT_body_begin";
4745 case DW_AT_body_end:
4746 return "DW_AT_body_end";
4747 case DW_AT_GNU_vector:
4748 return "DW_AT_GNU_vector";
4750 case DW_AT_VMS_rtnbeg_pd_address:
4751 return "DW_AT_VMS_rtnbeg_pd_address";
4753 default:
4754 return "DW_AT_<unknown>";
4758 /* Convert a DWARF value form code into its string name. */
4760 static const char *
4761 dwarf_form_name (unsigned int form)
4763 switch (form)
4765 case DW_FORM_addr:
4766 return "DW_FORM_addr";
4767 case DW_FORM_block2:
4768 return "DW_FORM_block2";
4769 case DW_FORM_block4:
4770 return "DW_FORM_block4";
4771 case DW_FORM_data2:
4772 return "DW_FORM_data2";
4773 case DW_FORM_data4:
4774 return "DW_FORM_data4";
4775 case DW_FORM_data8:
4776 return "DW_FORM_data8";
4777 case DW_FORM_string:
4778 return "DW_FORM_string";
4779 case DW_FORM_block:
4780 return "DW_FORM_block";
4781 case DW_FORM_block1:
4782 return "DW_FORM_block1";
4783 case DW_FORM_data1:
4784 return "DW_FORM_data1";
4785 case DW_FORM_flag:
4786 return "DW_FORM_flag";
4787 case DW_FORM_sdata:
4788 return "DW_FORM_sdata";
4789 case DW_FORM_strp:
4790 return "DW_FORM_strp";
4791 case DW_FORM_udata:
4792 return "DW_FORM_udata";
4793 case DW_FORM_ref_addr:
4794 return "DW_FORM_ref_addr";
4795 case DW_FORM_ref1:
4796 return "DW_FORM_ref1";
4797 case DW_FORM_ref2:
4798 return "DW_FORM_ref2";
4799 case DW_FORM_ref4:
4800 return "DW_FORM_ref4";
4801 case DW_FORM_ref8:
4802 return "DW_FORM_ref8";
4803 case DW_FORM_ref_udata:
4804 return "DW_FORM_ref_udata";
4805 case DW_FORM_indirect:
4806 return "DW_FORM_indirect";
4807 default:
4808 return "DW_FORM_<unknown>";
4812 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4813 instance of an inlined instance of a decl which is local to an inline
4814 function, so we have to trace all of the way back through the origin chain
4815 to find out what sort of node actually served as the original seed for the
4816 given block. */
4818 static tree
4819 decl_ultimate_origin (tree decl)
4821 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4822 return NULL_TREE;
4824 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4825 nodes in the function to point to themselves; ignore that if
4826 we're trying to output the abstract instance of this function. */
4827 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4828 return NULL_TREE;
4830 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4831 most distant ancestor, this should never happen. */
4832 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4834 return DECL_ABSTRACT_ORIGIN (decl);
4837 /* Determine the "ultimate origin" of a block. The block may be an inlined
4838 instance of an inlined instance of a block which is local to an inline
4839 function, so we have to trace all of the way back through the origin chain
4840 to find out what sort of node actually served as the original seed for the
4841 given block. */
4843 static tree
4844 block_ultimate_origin (tree block)
4846 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
4848 /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
4849 nodes in the function to point to themselves; ignore that if
4850 we're trying to output the abstract instance of this function. */
4851 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
4852 return NULL_TREE;
4854 if (immediate_origin == NULL_TREE)
4855 return NULL_TREE;
4856 else
4858 tree ret_val;
4859 tree lookahead = immediate_origin;
4863 ret_val = lookahead;
4864 lookahead = (TREE_CODE (ret_val) == BLOCK
4865 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
4867 while (lookahead != NULL && lookahead != ret_val);
4869 /* The block's abstract origin chain may not be the *ultimate* origin of
4870 the block. It could lead to a DECL that has an abstract origin set.
4871 If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
4872 will give us if it has one). Note that DECL's abstract origins are
4873 supposed to be the most distant ancestor (or so decl_ultimate_origin
4874 claims), so we don't need to loop following the DECL origins. */
4875 if (DECL_P (ret_val))
4876 return DECL_ORIGIN (ret_val);
4878 return ret_val;
4882 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4883 of a virtual function may refer to a base class, so we check the 'this'
4884 parameter. */
4886 static tree
4887 decl_class_context (tree decl)
4889 tree context = NULL_TREE;
4891 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4892 context = DECL_CONTEXT (decl);
4893 else
4894 context = TYPE_MAIN_VARIANT
4895 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4897 if (context && !TYPE_P (context))
4898 context = NULL_TREE;
4900 return context;
4903 /* Add an attribute/value pair to a DIE. */
4905 static inline void
4906 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
4908 /* Maybe this should be an assert? */
4909 if (die == NULL)
4910 return;
4912 if (die->die_attr == NULL)
4913 die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
4914 VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
4917 static inline enum dw_val_class
4918 AT_class (dw_attr_ref a)
4920 return a->dw_attr_val.val_class;
4923 /* Add a flag value attribute to a DIE. */
4925 static inline void
4926 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4928 dw_attr_node attr;
4930 attr.dw_attr = attr_kind;
4931 attr.dw_attr_val.val_class = dw_val_class_flag;
4932 attr.dw_attr_val.v.val_flag = flag;
4933 add_dwarf_attr (die, &attr);
4936 static inline unsigned
4937 AT_flag (dw_attr_ref a)
4939 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4940 return a->dw_attr_val.v.val_flag;
4943 /* Add a signed integer attribute value to a DIE. */
4945 static inline void
4946 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4948 dw_attr_node attr;
4950 attr.dw_attr = attr_kind;
4951 attr.dw_attr_val.val_class = dw_val_class_const;
4952 attr.dw_attr_val.v.val_int = int_val;
4953 add_dwarf_attr (die, &attr);
4956 static inline HOST_WIDE_INT
4957 AT_int (dw_attr_ref a)
4959 gcc_assert (a && AT_class (a) == dw_val_class_const);
4960 return a->dw_attr_val.v.val_int;
4963 /* Add an unsigned integer attribute value to a DIE. */
4965 static inline void
4966 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4967 unsigned HOST_WIDE_INT unsigned_val)
4969 dw_attr_node attr;
4971 attr.dw_attr = attr_kind;
4972 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4973 attr.dw_attr_val.v.val_unsigned = unsigned_val;
4974 add_dwarf_attr (die, &attr);
4977 static inline unsigned HOST_WIDE_INT
4978 AT_unsigned (dw_attr_ref a)
4980 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
4981 return a->dw_attr_val.v.val_unsigned;
4984 /* Add an unsigned double integer attribute value to a DIE. */
4986 static inline void
4987 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
4988 long unsigned int val_hi, long unsigned int val_low)
4990 dw_attr_node attr;
4992 attr.dw_attr = attr_kind;
4993 attr.dw_attr_val.val_class = dw_val_class_long_long;
4994 attr.dw_attr_val.v.val_long_long.hi = val_hi;
4995 attr.dw_attr_val.v.val_long_long.low = val_low;
4996 add_dwarf_attr (die, &attr);
4999 /* Add a floating point attribute value to a DIE and return it. */
5001 static inline void
5002 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
5003 unsigned int length, unsigned int elt_size, unsigned char *array)
5005 dw_attr_node attr;
5007 attr.dw_attr = attr_kind;
5008 attr.dw_attr_val.val_class = dw_val_class_vec;
5009 attr.dw_attr_val.v.val_vec.length = length;
5010 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
5011 attr.dw_attr_val.v.val_vec.array = array;
5012 add_dwarf_attr (die, &attr);
5015 /* Hash and equality functions for debug_str_hash. */
5017 static hashval_t
5018 debug_str_do_hash (const void *x)
5020 return htab_hash_string (((const struct indirect_string_node *)x)->str);
5023 static int
5024 debug_str_eq (const void *x1, const void *x2)
5026 return strcmp ((((const struct indirect_string_node *)x1)->str),
5027 (const char *)x2) == 0;
5030 /* Add a string attribute value to a DIE. */
5032 static inline void
5033 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
5035 dw_attr_node attr;
5036 struct indirect_string_node *node;
5037 void **slot;
5039 if (! debug_str_hash)
5040 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
5041 debug_str_eq, NULL);
5043 slot = htab_find_slot_with_hash (debug_str_hash, str,
5044 htab_hash_string (str), INSERT);
5045 if (*slot == NULL)
5047 node = (struct indirect_string_node *)
5048 ggc_alloc_cleared (sizeof (struct indirect_string_node));
5049 node->str = ggc_strdup (str);
5050 *slot = node;
5052 else
5053 node = (struct indirect_string_node *) *slot;
5055 node->refcount++;
5057 attr.dw_attr = attr_kind;
5058 attr.dw_attr_val.val_class = dw_val_class_str;
5059 attr.dw_attr_val.v.val_str = node;
5060 add_dwarf_attr (die, &attr);
5063 static inline const char *
5064 AT_string (dw_attr_ref a)
5066 gcc_assert (a && AT_class (a) == dw_val_class_str);
5067 return a->dw_attr_val.v.val_str->str;
5070 /* Find out whether a string should be output inline in DIE
5071 or out-of-line in .debug_str section. */
5073 static int
5074 AT_string_form (dw_attr_ref a)
5076 struct indirect_string_node *node;
5077 unsigned int len;
5078 char label[32];
5080 gcc_assert (a && AT_class (a) == dw_val_class_str);
5082 node = a->dw_attr_val.v.val_str;
5083 if (node->form)
5084 return node->form;
5086 len = strlen (node->str) + 1;
5088 /* If the string is shorter or equal to the size of the reference, it is
5089 always better to put it inline. */
5090 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
5091 return node->form = DW_FORM_string;
5093 /* If we cannot expect the linker to merge strings in .debug_str
5094 section, only put it into .debug_str if it is worth even in this
5095 single module. */
5096 if ((debug_str_section->common.flags & SECTION_MERGE) == 0
5097 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
5098 return node->form = DW_FORM_string;
5100 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
5101 ++dw2_string_counter;
5102 node->label = xstrdup (label);
5104 return node->form = DW_FORM_strp;
5107 /* Add a DIE reference attribute value to a DIE. */
5109 static inline void
5110 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
5112 dw_attr_node attr;
5114 attr.dw_attr = attr_kind;
5115 attr.dw_attr_val.val_class = dw_val_class_die_ref;
5116 attr.dw_attr_val.v.val_die_ref.die = targ_die;
5117 attr.dw_attr_val.v.val_die_ref.external = 0;
5118 add_dwarf_attr (die, &attr);
5121 /* Add an AT_specification attribute to a DIE, and also make the back
5122 pointer from the specification to the definition. */
5124 static inline void
5125 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
5127 add_AT_die_ref (die, DW_AT_specification, targ_die);
5128 gcc_assert (!targ_die->die_definition);
5129 targ_die->die_definition = die;
5132 static inline dw_die_ref
5133 AT_ref (dw_attr_ref a)
5135 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5136 return a->dw_attr_val.v.val_die_ref.die;
5139 static inline int
5140 AT_ref_external (dw_attr_ref a)
5142 if (a && AT_class (a) == dw_val_class_die_ref)
5143 return a->dw_attr_val.v.val_die_ref.external;
5145 return 0;
5148 static inline void
5149 set_AT_ref_external (dw_attr_ref a, int i)
5151 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5152 a->dw_attr_val.v.val_die_ref.external = i;
5155 /* Add an FDE reference attribute value to a DIE. */
5157 static inline void
5158 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
5160 dw_attr_node attr;
5162 attr.dw_attr = attr_kind;
5163 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
5164 attr.dw_attr_val.v.val_fde_index = targ_fde;
5165 add_dwarf_attr (die, &attr);
5168 /* Add a location description attribute value to a DIE. */
5170 static inline void
5171 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
5173 dw_attr_node attr;
5175 attr.dw_attr = attr_kind;
5176 attr.dw_attr_val.val_class = dw_val_class_loc;
5177 attr.dw_attr_val.v.val_loc = loc;
5178 add_dwarf_attr (die, &attr);
5181 static inline dw_loc_descr_ref
5182 AT_loc (dw_attr_ref a)
5184 gcc_assert (a && AT_class (a) == dw_val_class_loc);
5185 return a->dw_attr_val.v.val_loc;
5188 static inline void
5189 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
5191 dw_attr_node attr;
5193 attr.dw_attr = attr_kind;
5194 attr.dw_attr_val.val_class = dw_val_class_loc_list;
5195 attr.dw_attr_val.v.val_loc_list = loc_list;
5196 add_dwarf_attr (die, &attr);
5197 have_location_lists = true;
5200 static inline dw_loc_list_ref
5201 AT_loc_list (dw_attr_ref a)
5203 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
5204 return a->dw_attr_val.v.val_loc_list;
5207 /* Add an address constant attribute value to a DIE. */
5209 static inline void
5210 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
5212 dw_attr_node attr;
5214 attr.dw_attr = attr_kind;
5215 attr.dw_attr_val.val_class = dw_val_class_addr;
5216 attr.dw_attr_val.v.val_addr = addr;
5217 add_dwarf_attr (die, &attr);
5220 /* Get the RTX from to an address DIE attribute. */
5222 static inline rtx
5223 AT_addr (dw_attr_ref a)
5225 gcc_assert (a && AT_class (a) == dw_val_class_addr);
5226 return a->dw_attr_val.v.val_addr;
5229 /* Add a file attribute value to a DIE. */
5231 static inline void
5232 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
5233 struct dwarf_file_data *fd)
5235 dw_attr_node attr;
5237 attr.dw_attr = attr_kind;
5238 attr.dw_attr_val.val_class = dw_val_class_file;
5239 attr.dw_attr_val.v.val_file = fd;
5240 add_dwarf_attr (die, &attr);
5243 /* Get the dwarf_file_data from a file DIE attribute. */
5245 static inline struct dwarf_file_data *
5246 AT_file (dw_attr_ref a)
5248 gcc_assert (a && AT_class (a) == dw_val_class_file);
5249 return a->dw_attr_val.v.val_file;
5252 /* Add a label identifier attribute value to a DIE. */
5254 static inline void
5255 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
5257 dw_attr_node attr;
5259 attr.dw_attr = attr_kind;
5260 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
5261 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5262 add_dwarf_attr (die, &attr);
5265 /* Add a section offset attribute value to a DIE, an offset into the
5266 debug_line section. */
5268 static inline void
5269 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5270 const char *label)
5272 dw_attr_node attr;
5274 attr.dw_attr = attr_kind;
5275 attr.dw_attr_val.val_class = dw_val_class_lineptr;
5276 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5277 add_dwarf_attr (die, &attr);
5280 /* Add a section offset attribute value to a DIE, an offset into the
5281 debug_macinfo section. */
5283 static inline void
5284 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5285 const char *label)
5287 dw_attr_node attr;
5289 attr.dw_attr = attr_kind;
5290 attr.dw_attr_val.val_class = dw_val_class_macptr;
5291 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5292 add_dwarf_attr (die, &attr);
5295 /* Add an offset attribute value to a DIE. */
5297 static inline void
5298 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
5299 unsigned HOST_WIDE_INT offset)
5301 dw_attr_node attr;
5303 attr.dw_attr = attr_kind;
5304 attr.dw_attr_val.val_class = dw_val_class_offset;
5305 attr.dw_attr_val.v.val_offset = offset;
5306 add_dwarf_attr (die, &attr);
5309 /* Add an range_list attribute value to a DIE. */
5311 static void
5312 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5313 long unsigned int offset)
5315 dw_attr_node attr;
5317 attr.dw_attr = attr_kind;
5318 attr.dw_attr_val.val_class = dw_val_class_range_list;
5319 attr.dw_attr_val.v.val_offset = offset;
5320 add_dwarf_attr (die, &attr);
5323 static inline const char *
5324 AT_lbl (dw_attr_ref a)
5326 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5327 || AT_class (a) == dw_val_class_lineptr
5328 || AT_class (a) == dw_val_class_macptr));
5329 return a->dw_attr_val.v.val_lbl_id;
5332 /* Get the attribute of type attr_kind. */
5334 static dw_attr_ref
5335 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5337 dw_attr_ref a;
5338 unsigned ix;
5339 dw_die_ref spec = NULL;
5341 if (! die)
5342 return NULL;
5344 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5345 if (a->dw_attr == attr_kind)
5346 return a;
5347 else if (a->dw_attr == DW_AT_specification
5348 || a->dw_attr == DW_AT_abstract_origin)
5349 spec = AT_ref (a);
5351 if (spec)
5352 return get_AT (spec, attr_kind);
5354 return NULL;
5357 /* Return the "low pc" attribute value, typically associated with a subprogram
5358 DIE. Return null if the "low pc" attribute is either not present, or if it
5359 cannot be represented as an assembler label identifier. */
5361 static inline const char *
5362 get_AT_low_pc (dw_die_ref die)
5364 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
5366 return a ? AT_lbl (a) : NULL;
5369 /* Return the "high pc" attribute value, typically associated with a subprogram
5370 DIE. Return null if the "high pc" attribute is either not present, or if it
5371 cannot be represented as an assembler label identifier. */
5373 static inline const char *
5374 get_AT_hi_pc (dw_die_ref die)
5376 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
5378 return a ? AT_lbl (a) : NULL;
5381 /* Return the value of the string attribute designated by ATTR_KIND, or
5382 NULL if it is not present. */
5384 static inline const char *
5385 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5387 dw_attr_ref a = get_AT (die, attr_kind);
5389 return a ? AT_string (a) : NULL;
5392 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5393 if it is not present. */
5395 static inline int
5396 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5398 dw_attr_ref a = get_AT (die, attr_kind);
5400 return a ? AT_flag (a) : 0;
5403 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5404 if it is not present. */
5406 static inline unsigned
5407 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5409 dw_attr_ref a = get_AT (die, attr_kind);
5411 return a ? AT_unsigned (a) : 0;
5414 static inline dw_die_ref
5415 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5417 dw_attr_ref a = get_AT (die, attr_kind);
5419 return a ? AT_ref (a) : NULL;
5422 static inline struct dwarf_file_data *
5423 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5425 dw_attr_ref a = get_AT (die, attr_kind);
5427 return a ? AT_file (a) : NULL;
5430 /* Return TRUE if the language is C or C++. */
5432 static inline bool
5433 is_c_family (void)
5435 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5437 return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
5438 || lang == DW_LANG_C99
5439 || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
5442 /* Return TRUE if the language is C++. */
5444 static inline bool
5445 is_cxx (void)
5447 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5449 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
5452 /* Return TRUE if the language is Fortran. */
5454 static inline bool
5455 is_fortran (void)
5457 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5459 return (lang == DW_LANG_Fortran77
5460 || lang == DW_LANG_Fortran90
5461 || lang == DW_LANG_Fortran95);
5464 /* Return TRUE if the language is Java. */
5466 static inline bool
5467 is_java (void)
5469 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5471 return lang == DW_LANG_Java;
5474 /* Return TRUE if the language is Ada. */
5476 static inline bool
5477 is_ada (void)
5479 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5481 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5484 /* Remove the specified attribute if present. */
5486 static void
5487 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5489 dw_attr_ref a;
5490 unsigned ix;
5492 if (! die)
5493 return;
5495 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5496 if (a->dw_attr == attr_kind)
5498 if (AT_class (a) == dw_val_class_str)
5499 if (a->dw_attr_val.v.val_str->refcount)
5500 a->dw_attr_val.v.val_str->refcount--;
5502 /* VEC_ordered_remove should help reduce the number of abbrevs
5503 that are needed. */
5504 VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
5505 return;
5509 /* Remove CHILD from its parent. PREV must have the property that
5510 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5512 static void
5513 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5515 gcc_assert (child->die_parent == prev->die_parent);
5516 gcc_assert (prev->die_sib == child);
5517 if (prev == child)
5519 gcc_assert (child->die_parent->die_child == child);
5520 prev = NULL;
5522 else
5523 prev->die_sib = child->die_sib;
5524 if (child->die_parent->die_child == child)
5525 child->die_parent->die_child = prev;
5528 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
5529 matches TAG. */
5531 static void
5532 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5534 dw_die_ref c;
5536 c = die->die_child;
5537 if (c) do {
5538 dw_die_ref prev = c;
5539 c = c->die_sib;
5540 while (c->die_tag == tag)
5542 remove_child_with_prev (c, prev);
5543 /* Might have removed every child. */
5544 if (c == c->die_sib)
5545 return;
5546 c = c->die_sib;
5548 } while (c != die->die_child);
5551 /* Add a CHILD_DIE as the last child of DIE. */
5553 static void
5554 add_child_die (dw_die_ref die, dw_die_ref child_die)
5556 /* FIXME this should probably be an assert. */
5557 if (! die || ! child_die)
5558 return;
5559 gcc_assert (die != child_die);
5561 child_die->die_parent = die;
5562 if (die->die_child)
5564 child_die->die_sib = die->die_child->die_sib;
5565 die->die_child->die_sib = child_die;
5567 else
5568 child_die->die_sib = child_die;
5569 die->die_child = child_die;
5572 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5573 is the specification, to the end of PARENT's list of children.
5574 This is done by removing and re-adding it. */
5576 static void
5577 splice_child_die (dw_die_ref parent, dw_die_ref child)
5579 dw_die_ref p;
5581 /* We want the declaration DIE from inside the class, not the
5582 specification DIE at toplevel. */
5583 if (child->die_parent != parent)
5585 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5587 if (tmp)
5588 child = tmp;
5591 gcc_assert (child->die_parent == parent
5592 || (child->die_parent
5593 == get_AT_ref (parent, DW_AT_specification)));
5595 for (p = child->die_parent->die_child; ; p = p->die_sib)
5596 if (p->die_sib == child)
5598 remove_child_with_prev (child, p);
5599 break;
5602 add_child_die (parent, child);
5605 /* Return a pointer to a newly created DIE node. */
5607 static inline dw_die_ref
5608 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5610 dw_die_ref die = ggc_alloc_cleared (sizeof (die_node));
5612 die->die_tag = tag_value;
5614 if (parent_die != NULL)
5615 add_child_die (parent_die, die);
5616 else
5618 limbo_die_node *limbo_node;
5620 limbo_node = ggc_alloc_cleared (sizeof (limbo_die_node));
5621 limbo_node->die = die;
5622 limbo_node->created_for = t;
5623 limbo_node->next = limbo_die_list;
5624 limbo_die_list = limbo_node;
5627 return die;
5630 /* Return the DIE associated with the given type specifier. */
5632 static inline dw_die_ref
5633 lookup_type_die (tree type)
5635 return TYPE_SYMTAB_DIE (type);
5638 /* Equate a DIE to a given type specifier. */
5640 static inline void
5641 equate_type_number_to_die (tree type, dw_die_ref type_die)
5643 TYPE_SYMTAB_DIE (type) = type_die;
5646 /* Returns a hash value for X (which really is a die_struct). */
5648 static hashval_t
5649 decl_die_table_hash (const void *x)
5651 return (hashval_t) ((const dw_die_ref) x)->decl_id;
5654 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5656 static int
5657 decl_die_table_eq (const void *x, const void *y)
5659 return (((const dw_die_ref) x)->decl_id == DECL_UID ((const tree) y));
5662 /* Return the DIE associated with a given declaration. */
5664 static inline dw_die_ref
5665 lookup_decl_die (tree decl)
5667 return htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
5670 /* Returns a hash value for X (which really is a var_loc_list). */
5672 static hashval_t
5673 decl_loc_table_hash (const void *x)
5675 return (hashval_t) ((const var_loc_list *) x)->decl_id;
5678 /* Return nonzero if decl_id of var_loc_list X is the same as
5679 UID of decl *Y. */
5681 static int
5682 decl_loc_table_eq (const void *x, const void *y)
5684 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const tree) y));
5687 /* Return the var_loc list associated with a given declaration. */
5689 static inline var_loc_list *
5690 lookup_decl_loc (tree decl)
5692 return htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5695 /* Equate a DIE to a particular declaration. */
5697 static void
5698 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5700 unsigned int decl_id = DECL_UID (decl);
5701 void **slot;
5703 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5704 *slot = decl_die;
5705 decl_die->decl_id = decl_id;
5708 /* Add a variable location node to the linked list for DECL. */
5710 static void
5711 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
5713 unsigned int decl_id = DECL_UID (decl);
5714 var_loc_list *temp;
5715 void **slot;
5717 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5718 if (*slot == NULL)
5720 temp = ggc_alloc_cleared (sizeof (var_loc_list));
5721 temp->decl_id = decl_id;
5722 *slot = temp;
5724 else
5725 temp = *slot;
5727 if (temp->last)
5729 /* If the current location is the same as the end of the list,
5730 we have nothing to do. */
5731 if (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
5732 NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
5734 /* Add LOC to the end of list and update LAST. */
5735 temp->last->next = loc;
5736 temp->last = loc;
5739 /* Do not add empty location to the beginning of the list. */
5740 else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
5742 temp->first = loc;
5743 temp->last = loc;
5747 /* Keep track of the number of spaces used to indent the
5748 output of the debugging routines that print the structure of
5749 the DIE internal representation. */
5750 static int print_indent;
5752 /* Indent the line the number of spaces given by print_indent. */
5754 static inline void
5755 print_spaces (FILE *outfile)
5757 fprintf (outfile, "%*s", print_indent, "");
5760 /* Print the information associated with a given DIE, and its children.
5761 This routine is a debugging aid only. */
5763 static void
5764 print_die (dw_die_ref die, FILE *outfile)
5766 dw_attr_ref a;
5767 dw_die_ref c;
5768 unsigned ix;
5770 print_spaces (outfile);
5771 fprintf (outfile, "DIE %4lu: %s\n",
5772 die->die_offset, dwarf_tag_name (die->die_tag));
5773 print_spaces (outfile);
5774 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5775 fprintf (outfile, " offset: %lu\n", die->die_offset);
5777 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5779 print_spaces (outfile);
5780 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5782 switch (AT_class (a))
5784 case dw_val_class_addr:
5785 fprintf (outfile, "address");
5786 break;
5787 case dw_val_class_offset:
5788 fprintf (outfile, "offset");
5789 break;
5790 case dw_val_class_loc:
5791 fprintf (outfile, "location descriptor");
5792 break;
5793 case dw_val_class_loc_list:
5794 fprintf (outfile, "location list -> label:%s",
5795 AT_loc_list (a)->ll_symbol);
5796 break;
5797 case dw_val_class_range_list:
5798 fprintf (outfile, "range list");
5799 break;
5800 case dw_val_class_const:
5801 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5802 break;
5803 case dw_val_class_unsigned_const:
5804 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5805 break;
5806 case dw_val_class_long_long:
5807 fprintf (outfile, "constant (%lu,%lu)",
5808 a->dw_attr_val.v.val_long_long.hi,
5809 a->dw_attr_val.v.val_long_long.low);
5810 break;
5811 case dw_val_class_vec:
5812 fprintf (outfile, "floating-point or vector constant");
5813 break;
5814 case dw_val_class_flag:
5815 fprintf (outfile, "%u", AT_flag (a));
5816 break;
5817 case dw_val_class_die_ref:
5818 if (AT_ref (a) != NULL)
5820 if (AT_ref (a)->die_symbol)
5821 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
5822 else
5823 fprintf (outfile, "die -> %lu", AT_ref (a)->die_offset);
5825 else
5826 fprintf (outfile, "die -> <null>");
5827 break;
5828 case dw_val_class_lbl_id:
5829 case dw_val_class_lineptr:
5830 case dw_val_class_macptr:
5831 fprintf (outfile, "label: %s", AT_lbl (a));
5832 break;
5833 case dw_val_class_str:
5834 if (AT_string (a) != NULL)
5835 fprintf (outfile, "\"%s\"", AT_string (a));
5836 else
5837 fprintf (outfile, "<null>");
5838 break;
5839 case dw_val_class_file:
5840 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5841 AT_file (a)->emitted_number);
5842 break;
5843 default:
5844 break;
5847 fprintf (outfile, "\n");
5850 if (die->die_child != NULL)
5852 print_indent += 4;
5853 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5854 print_indent -= 4;
5856 if (print_indent == 0)
5857 fprintf (outfile, "\n");
5860 /* Print the contents of the source code line number correspondence table.
5861 This routine is a debugging aid only. */
5863 static void
5864 print_dwarf_line_table (FILE *outfile)
5866 unsigned i;
5867 dw_line_info_ref line_info;
5869 fprintf (outfile, "\n\nDWARF source line information\n");
5870 for (i = 1; i < line_info_table_in_use; i++)
5872 line_info = &line_info_table[i];
5873 fprintf (outfile, "%5d: %4ld %6ld\n", i,
5874 line_info->dw_file_num,
5875 line_info->dw_line_num);
5878 fprintf (outfile, "\n\n");
5881 /* Print the information collected for a given DIE. */
5883 void
5884 debug_dwarf_die (dw_die_ref die)
5886 print_die (die, stderr);
5889 /* Print all DWARF information collected for the compilation unit.
5890 This routine is a debugging aid only. */
5892 void
5893 debug_dwarf (void)
5895 print_indent = 0;
5896 print_die (comp_unit_die, stderr);
5897 if (! DWARF2_ASM_LINE_DEBUG_INFO)
5898 print_dwarf_line_table (stderr);
5901 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5902 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5903 DIE that marks the start of the DIEs for this include file. */
5905 static dw_die_ref
5906 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5908 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5909 dw_die_ref new_unit = gen_compile_unit_die (filename);
5911 new_unit->die_sib = old_unit;
5912 return new_unit;
5915 /* Close an include-file CU and reopen the enclosing one. */
5917 static dw_die_ref
5918 pop_compile_unit (dw_die_ref old_unit)
5920 dw_die_ref new_unit = old_unit->die_sib;
5922 old_unit->die_sib = NULL;
5923 return new_unit;
5926 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5927 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5929 /* Calculate the checksum of a location expression. */
5931 static inline void
5932 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5934 CHECKSUM (loc->dw_loc_opc);
5935 CHECKSUM (loc->dw_loc_oprnd1);
5936 CHECKSUM (loc->dw_loc_oprnd2);
5939 /* Calculate the checksum of an attribute. */
5941 static void
5942 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5944 dw_loc_descr_ref loc;
5945 rtx r;
5947 CHECKSUM (at->dw_attr);
5949 /* We don't care that this was compiled with a different compiler
5950 snapshot; if the output is the same, that's what matters. */
5951 if (at->dw_attr == DW_AT_producer)
5952 return;
5954 switch (AT_class (at))
5956 case dw_val_class_const:
5957 CHECKSUM (at->dw_attr_val.v.val_int);
5958 break;
5959 case dw_val_class_unsigned_const:
5960 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5961 break;
5962 case dw_val_class_long_long:
5963 CHECKSUM (at->dw_attr_val.v.val_long_long);
5964 break;
5965 case dw_val_class_vec:
5966 CHECKSUM (at->dw_attr_val.v.val_vec);
5967 break;
5968 case dw_val_class_flag:
5969 CHECKSUM (at->dw_attr_val.v.val_flag);
5970 break;
5971 case dw_val_class_str:
5972 CHECKSUM_STRING (AT_string (at));
5973 break;
5975 case dw_val_class_addr:
5976 r = AT_addr (at);
5977 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5978 CHECKSUM_STRING (XSTR (r, 0));
5979 break;
5981 case dw_val_class_offset:
5982 CHECKSUM (at->dw_attr_val.v.val_offset);
5983 break;
5985 case dw_val_class_loc:
5986 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5987 loc_checksum (loc, ctx);
5988 break;
5990 case dw_val_class_die_ref:
5991 die_checksum (AT_ref (at), ctx, mark);
5992 break;
5994 case dw_val_class_fde_ref:
5995 case dw_val_class_lbl_id:
5996 case dw_val_class_lineptr:
5997 case dw_val_class_macptr:
5998 break;
6000 case dw_val_class_file:
6001 CHECKSUM_STRING (AT_file (at)->filename);
6002 break;
6004 default:
6005 break;
6009 /* Calculate the checksum of a DIE. */
6011 static void
6012 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6014 dw_die_ref c;
6015 dw_attr_ref a;
6016 unsigned ix;
6018 /* To avoid infinite recursion. */
6019 if (die->die_mark)
6021 CHECKSUM (die->die_mark);
6022 return;
6024 die->die_mark = ++(*mark);
6026 CHECKSUM (die->die_tag);
6028 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6029 attr_checksum (a, ctx, mark);
6031 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6034 #undef CHECKSUM
6035 #undef CHECKSUM_STRING
6037 /* Do the location expressions look same? */
6038 static inline int
6039 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6041 return loc1->dw_loc_opc == loc2->dw_loc_opc
6042 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6043 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6046 /* Do the values look the same? */
6047 static int
6048 same_dw_val_p (dw_val_node *v1, dw_val_node *v2, int *mark)
6050 dw_loc_descr_ref loc1, loc2;
6051 rtx r1, r2;
6053 if (v1->val_class != v2->val_class)
6054 return 0;
6056 switch (v1->val_class)
6058 case dw_val_class_const:
6059 return v1->v.val_int == v2->v.val_int;
6060 case dw_val_class_unsigned_const:
6061 return v1->v.val_unsigned == v2->v.val_unsigned;
6062 case dw_val_class_long_long:
6063 return v1->v.val_long_long.hi == v2->v.val_long_long.hi
6064 && v1->v.val_long_long.low == v2->v.val_long_long.low;
6065 case dw_val_class_vec:
6066 if (v1->v.val_vec.length != v2->v.val_vec.length
6067 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6068 return 0;
6069 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6070 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6071 return 0;
6072 return 1;
6073 case dw_val_class_flag:
6074 return v1->v.val_flag == v2->v.val_flag;
6075 case dw_val_class_str:
6076 return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6078 case dw_val_class_addr:
6079 r1 = v1->v.val_addr;
6080 r2 = v2->v.val_addr;
6081 if (GET_CODE (r1) != GET_CODE (r2))
6082 return 0;
6083 gcc_assert (GET_CODE (r1) == SYMBOL_REF);
6084 return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
6086 case dw_val_class_offset:
6087 return v1->v.val_offset == v2->v.val_offset;
6089 case dw_val_class_loc:
6090 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6091 loc1 && loc2;
6092 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6093 if (!same_loc_p (loc1, loc2, mark))
6094 return 0;
6095 return !loc1 && !loc2;
6097 case dw_val_class_die_ref:
6098 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6100 case dw_val_class_fde_ref:
6101 case dw_val_class_lbl_id:
6102 case dw_val_class_lineptr:
6103 case dw_val_class_macptr:
6104 return 1;
6106 case dw_val_class_file:
6107 return v1->v.val_file == v2->v.val_file;
6109 default:
6110 return 1;
6114 /* Do the attributes look the same? */
6116 static int
6117 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6119 if (at1->dw_attr != at2->dw_attr)
6120 return 0;
6122 /* We don't care that this was compiled with a different compiler
6123 snapshot; if the output is the same, that's what matters. */
6124 if (at1->dw_attr == DW_AT_producer)
6125 return 1;
6127 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6130 /* Do the dies look the same? */
6132 static int
6133 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6135 dw_die_ref c1, c2;
6136 dw_attr_ref a1;
6137 unsigned ix;
6139 /* To avoid infinite recursion. */
6140 if (die1->die_mark)
6141 return die1->die_mark == die2->die_mark;
6142 die1->die_mark = die2->die_mark = ++(*mark);
6144 if (die1->die_tag != die2->die_tag)
6145 return 0;
6147 if (VEC_length (dw_attr_node, die1->die_attr)
6148 != VEC_length (dw_attr_node, die2->die_attr))
6149 return 0;
6151 for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
6152 if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
6153 return 0;
6155 c1 = die1->die_child;
6156 c2 = die2->die_child;
6157 if (! c1)
6159 if (c2)
6160 return 0;
6162 else
6163 for (;;)
6165 if (!same_die_p (c1, c2, mark))
6166 return 0;
6167 c1 = c1->die_sib;
6168 c2 = c2->die_sib;
6169 if (c1 == die1->die_child)
6171 if (c2 == die2->die_child)
6172 break;
6173 else
6174 return 0;
6178 return 1;
6181 /* Do the dies look the same? Wrapper around same_die_p. */
6183 static int
6184 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6186 int mark = 0;
6187 int ret = same_die_p (die1, die2, &mark);
6189 unmark_all_dies (die1);
6190 unmark_all_dies (die2);
6192 return ret;
6195 /* The prefix to attach to symbols on DIEs in the current comdat debug
6196 info section. */
6197 static char *comdat_symbol_id;
6199 /* The index of the current symbol within the current comdat CU. */
6200 static unsigned int comdat_symbol_number;
6202 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6203 children, and set comdat_symbol_id accordingly. */
6205 static void
6206 compute_section_prefix (dw_die_ref unit_die)
6208 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6209 const char *base = die_name ? lbasename (die_name) : "anonymous";
6210 char *name = alloca (strlen (base) + 64);
6211 char *p;
6212 int i, mark;
6213 unsigned char checksum[16];
6214 struct md5_ctx ctx;
6216 /* Compute the checksum of the DIE, then append part of it as hex digits to
6217 the name filename of the unit. */
6219 md5_init_ctx (&ctx);
6220 mark = 0;
6221 die_checksum (unit_die, &ctx, &mark);
6222 unmark_all_dies (unit_die);
6223 md5_finish_ctx (&ctx, checksum);
6225 sprintf (name, "%s.", base);
6226 clean_symbol_name (name);
6228 p = name + strlen (name);
6229 for (i = 0; i < 4; i++)
6231 sprintf (p, "%.2x", checksum[i]);
6232 p += 2;
6235 comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
6236 comdat_symbol_number = 0;
6239 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6241 static int
6242 is_type_die (dw_die_ref die)
6244 switch (die->die_tag)
6246 case DW_TAG_array_type:
6247 case DW_TAG_class_type:
6248 case DW_TAG_enumeration_type:
6249 case DW_TAG_pointer_type:
6250 case DW_TAG_reference_type:
6251 case DW_TAG_string_type:
6252 case DW_TAG_structure_type:
6253 case DW_TAG_subroutine_type:
6254 case DW_TAG_union_type:
6255 case DW_TAG_ptr_to_member_type:
6256 case DW_TAG_set_type:
6257 case DW_TAG_subrange_type:
6258 case DW_TAG_base_type:
6259 case DW_TAG_const_type:
6260 case DW_TAG_file_type:
6261 case DW_TAG_packed_type:
6262 case DW_TAG_volatile_type:
6263 case DW_TAG_typedef:
6264 return 1;
6265 default:
6266 return 0;
6270 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6271 Basically, we want to choose the bits that are likely to be shared between
6272 compilations (types) and leave out the bits that are specific to individual
6273 compilations (functions). */
6275 static int
6276 is_comdat_die (dw_die_ref c)
6278 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6279 we do for stabs. The advantage is a greater likelihood of sharing between
6280 objects that don't include headers in the same order (and therefore would
6281 put the base types in a different comdat). jason 8/28/00 */
6283 if (c->die_tag == DW_TAG_base_type)
6284 return 0;
6286 if (c->die_tag == DW_TAG_pointer_type
6287 || c->die_tag == DW_TAG_reference_type
6288 || c->die_tag == DW_TAG_const_type
6289 || c->die_tag == DW_TAG_volatile_type)
6291 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6293 return t ? is_comdat_die (t) : 0;
6296 return is_type_die (c);
6299 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6300 compilation unit. */
6302 static int
6303 is_symbol_die (dw_die_ref c)
6305 return (is_type_die (c)
6306 || (get_AT (c, DW_AT_declaration)
6307 && !get_AT (c, DW_AT_specification))
6308 || c->die_tag == DW_TAG_namespace);
6311 static char *
6312 gen_internal_sym (const char *prefix)
6314 char buf[256];
6316 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6317 return xstrdup (buf);
6320 /* Assign symbols to all worthy DIEs under DIE. */
6322 static void
6323 assign_symbol_names (dw_die_ref die)
6325 dw_die_ref c;
6327 if (is_symbol_die (die))
6329 if (comdat_symbol_id)
6331 char *p = alloca (strlen (comdat_symbol_id) + 64);
6333 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6334 comdat_symbol_id, comdat_symbol_number++);
6335 die->die_symbol = xstrdup (p);
6337 else
6338 die->die_symbol = gen_internal_sym ("LDIE");
6341 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6344 struct cu_hash_table_entry
6346 dw_die_ref cu;
6347 unsigned min_comdat_num, max_comdat_num;
6348 struct cu_hash_table_entry *next;
6351 /* Routines to manipulate hash table of CUs. */
6352 static hashval_t
6353 htab_cu_hash (const void *of)
6355 const struct cu_hash_table_entry *entry = of;
6357 return htab_hash_string (entry->cu->die_symbol);
6360 static int
6361 htab_cu_eq (const void *of1, const void *of2)
6363 const struct cu_hash_table_entry *entry1 = of1;
6364 const struct die_struct *entry2 = of2;
6366 return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
6369 static void
6370 htab_cu_del (void *what)
6372 struct cu_hash_table_entry *next, *entry = what;
6374 while (entry)
6376 next = entry->next;
6377 free (entry);
6378 entry = next;
6382 /* Check whether we have already seen this CU and set up SYM_NUM
6383 accordingly. */
6384 static int
6385 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6387 struct cu_hash_table_entry dummy;
6388 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6390 dummy.max_comdat_num = 0;
6392 slot = (struct cu_hash_table_entry **)
6393 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6394 INSERT);
6395 entry = *slot;
6397 for (; entry; last = entry, entry = entry->next)
6399 if (same_die_p_wrap (cu, entry->cu))
6400 break;
6403 if (entry)
6405 *sym_num = entry->min_comdat_num;
6406 return 1;
6409 entry = XCNEW (struct cu_hash_table_entry);
6410 entry->cu = cu;
6411 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6412 entry->next = *slot;
6413 *slot = entry;
6415 return 0;
6418 /* Record SYM_NUM to record of CU in HTABLE. */
6419 static void
6420 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6422 struct cu_hash_table_entry **slot, *entry;
6424 slot = (struct cu_hash_table_entry **)
6425 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6426 NO_INSERT);
6427 entry = *slot;
6429 entry->max_comdat_num = sym_num;
6432 /* Traverse the DIE (which is always comp_unit_die), and set up
6433 additional compilation units for each of the include files we see
6434 bracketed by BINCL/EINCL. */
6436 static void
6437 break_out_includes (dw_die_ref die)
6439 dw_die_ref c;
6440 dw_die_ref unit = NULL;
6441 limbo_die_node *node, **pnode;
6442 htab_t cu_hash_table;
6444 c = die->die_child;
6445 if (c) do {
6446 dw_die_ref prev = c;
6447 c = c->die_sib;
6448 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6449 || (unit && is_comdat_die (c)))
6451 dw_die_ref next = c->die_sib;
6453 /* This DIE is for a secondary CU; remove it from the main one. */
6454 remove_child_with_prev (c, prev);
6456 if (c->die_tag == DW_TAG_GNU_BINCL)
6457 unit = push_new_compile_unit (unit, c);
6458 else if (c->die_tag == DW_TAG_GNU_EINCL)
6459 unit = pop_compile_unit (unit);
6460 else
6461 add_child_die (unit, c);
6462 c = next;
6463 if (c == die->die_child)
6464 break;
6466 } while (c != die->die_child);
6468 #if 0
6469 /* We can only use this in debugging, since the frontend doesn't check
6470 to make sure that we leave every include file we enter. */
6471 gcc_assert (!unit);
6472 #endif
6474 assign_symbol_names (die);
6475 cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6476 for (node = limbo_die_list, pnode = &limbo_die_list;
6477 node;
6478 node = node->next)
6480 int is_dupl;
6482 compute_section_prefix (node->die);
6483 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6484 &comdat_symbol_number);
6485 assign_symbol_names (node->die);
6486 if (is_dupl)
6487 *pnode = node->next;
6488 else
6490 pnode = &node->next;
6491 record_comdat_symbol_number (node->die, cu_hash_table,
6492 comdat_symbol_number);
6495 htab_delete (cu_hash_table);
6498 /* Traverse the DIE and add a sibling attribute if it may have the
6499 effect of speeding up access to siblings. To save some space,
6500 avoid generating sibling attributes for DIE's without children. */
6502 static void
6503 add_sibling_attributes (dw_die_ref die)
6505 dw_die_ref c;
6507 if (! die->die_child)
6508 return;
6510 if (die->die_parent && die != die->die_parent->die_child)
6511 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
6513 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
6516 /* Output all location lists for the DIE and its children. */
6518 static void
6519 output_location_lists (dw_die_ref die)
6521 dw_die_ref c;
6522 dw_attr_ref a;
6523 unsigned ix;
6525 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6526 if (AT_class (a) == dw_val_class_loc_list)
6527 output_loc_list (AT_loc_list (a));
6529 FOR_EACH_CHILD (die, c, output_location_lists (c));
6532 /* The format of each DIE (and its attribute value pairs) is encoded in an
6533 abbreviation table. This routine builds the abbreviation table and assigns
6534 a unique abbreviation id for each abbreviation entry. The children of each
6535 die are visited recursively. */
6537 static void
6538 build_abbrev_table (dw_die_ref die)
6540 unsigned long abbrev_id;
6541 unsigned int n_alloc;
6542 dw_die_ref c;
6543 dw_attr_ref a;
6544 unsigned ix;
6546 /* Scan the DIE references, and mark as external any that refer to
6547 DIEs from other CUs (i.e. those which are not marked). */
6548 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6549 if (AT_class (a) == dw_val_class_die_ref
6550 && AT_ref (a)->die_mark == 0)
6552 gcc_assert (AT_ref (a)->die_symbol);
6554 set_AT_ref_external (a, 1);
6557 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6559 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6560 dw_attr_ref die_a, abbrev_a;
6561 unsigned ix;
6562 bool ok = true;
6564 if (abbrev->die_tag != die->die_tag)
6565 continue;
6566 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
6567 continue;
6569 if (VEC_length (dw_attr_node, abbrev->die_attr)
6570 != VEC_length (dw_attr_node, die->die_attr))
6571 continue;
6573 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
6575 abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
6576 if ((abbrev_a->dw_attr != die_a->dw_attr)
6577 || (value_format (abbrev_a) != value_format (die_a)))
6579 ok = false;
6580 break;
6583 if (ok)
6584 break;
6587 if (abbrev_id >= abbrev_die_table_in_use)
6589 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
6591 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
6592 abbrev_die_table = ggc_realloc (abbrev_die_table,
6593 sizeof (dw_die_ref) * n_alloc);
6595 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
6596 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
6597 abbrev_die_table_allocated = n_alloc;
6600 ++abbrev_die_table_in_use;
6601 abbrev_die_table[abbrev_id] = die;
6604 die->die_abbrev = abbrev_id;
6605 FOR_EACH_CHILD (die, c, build_abbrev_table (c));
6608 /* Return the power-of-two number of bytes necessary to represent VALUE. */
6610 static int
6611 constant_size (long unsigned int value)
6613 int log;
6615 if (value == 0)
6616 log = 0;
6617 else
6618 log = floor_log2 (value);
6620 log = log / 8;
6621 log = 1 << (floor_log2 (log) + 1);
6623 return log;
6626 /* Return the size of a DIE as it is represented in the
6627 .debug_info section. */
6629 static unsigned long
6630 size_of_die (dw_die_ref die)
6632 unsigned long size = 0;
6633 dw_attr_ref a;
6634 unsigned ix;
6636 size += size_of_uleb128 (die->die_abbrev);
6637 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6639 switch (AT_class (a))
6641 case dw_val_class_addr:
6642 size += DWARF2_ADDR_SIZE;
6643 break;
6644 case dw_val_class_offset:
6645 size += DWARF_OFFSET_SIZE;
6646 break;
6647 case dw_val_class_loc:
6649 unsigned long lsize = size_of_locs (AT_loc (a));
6651 /* Block length. */
6652 size += constant_size (lsize);
6653 size += lsize;
6655 break;
6656 case dw_val_class_loc_list:
6657 size += DWARF_OFFSET_SIZE;
6658 break;
6659 case dw_val_class_range_list:
6660 size += DWARF_OFFSET_SIZE;
6661 break;
6662 case dw_val_class_const:
6663 size += size_of_sleb128 (AT_int (a));
6664 break;
6665 case dw_val_class_unsigned_const:
6666 size += constant_size (AT_unsigned (a));
6667 break;
6668 case dw_val_class_long_long:
6669 size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
6670 break;
6671 case dw_val_class_vec:
6672 size += 1 + (a->dw_attr_val.v.val_vec.length
6673 * a->dw_attr_val.v.val_vec.elt_size); /* block */
6674 break;
6675 case dw_val_class_flag:
6676 size += 1;
6677 break;
6678 case dw_val_class_die_ref:
6679 if (AT_ref_external (a))
6680 size += DWARF2_ADDR_SIZE;
6681 else
6682 size += DWARF_OFFSET_SIZE;
6683 break;
6684 case dw_val_class_fde_ref:
6685 size += DWARF_OFFSET_SIZE;
6686 break;
6687 case dw_val_class_lbl_id:
6688 size += DWARF2_ADDR_SIZE;
6689 break;
6690 case dw_val_class_lineptr:
6691 case dw_val_class_macptr:
6692 size += DWARF_OFFSET_SIZE;
6693 break;
6694 case dw_val_class_str:
6695 if (AT_string_form (a) == DW_FORM_strp)
6696 size += DWARF_OFFSET_SIZE;
6697 else
6698 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
6699 break;
6700 case dw_val_class_file:
6701 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
6702 break;
6703 default:
6704 gcc_unreachable ();
6708 return size;
6711 /* Size the debugging information associated with a given DIE. Visits the
6712 DIE's children recursively. Updates the global variable next_die_offset, on
6713 each time through. Uses the current value of next_die_offset to update the
6714 die_offset field in each DIE. */
6716 static void
6717 calc_die_sizes (dw_die_ref die)
6719 dw_die_ref c;
6721 die->die_offset = next_die_offset;
6722 next_die_offset += size_of_die (die);
6724 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
6726 if (die->die_child != NULL)
6727 /* Count the null byte used to terminate sibling lists. */
6728 next_die_offset += 1;
6731 /* Set the marks for a die and its children. We do this so
6732 that we know whether or not a reference needs to use FORM_ref_addr; only
6733 DIEs in the same CU will be marked. We used to clear out the offset
6734 and use that as the flag, but ran into ordering problems. */
6736 static void
6737 mark_dies (dw_die_ref die)
6739 dw_die_ref c;
6741 gcc_assert (!die->die_mark);
6743 die->die_mark = 1;
6744 FOR_EACH_CHILD (die, c, mark_dies (c));
6747 /* Clear the marks for a die and its children. */
6749 static void
6750 unmark_dies (dw_die_ref die)
6752 dw_die_ref c;
6754 gcc_assert (die->die_mark);
6756 die->die_mark = 0;
6757 FOR_EACH_CHILD (die, c, unmark_dies (c));
6760 /* Clear the marks for a die, its children and referred dies. */
6762 static void
6763 unmark_all_dies (dw_die_ref die)
6765 dw_die_ref c;
6766 dw_attr_ref a;
6767 unsigned ix;
6769 if (!die->die_mark)
6770 return;
6771 die->die_mark = 0;
6773 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
6775 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6776 if (AT_class (a) == dw_val_class_die_ref)
6777 unmark_all_dies (AT_ref (a));
6780 /* Return the size of the .debug_pubnames or .debug_pubtypes table
6781 generated for the compilation unit. */
6783 static unsigned long
6784 size_of_pubnames (VEC (pubname_entry, gc) * names)
6786 unsigned long size;
6787 unsigned i;
6788 pubname_ref p;
6790 size = DWARF_PUBNAMES_HEADER_SIZE;
6791 for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
6792 if (names != pubtype_table
6793 || p->die->die_offset != 0
6794 || !flag_eliminate_unused_debug_types)
6795 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
6797 size += DWARF_OFFSET_SIZE;
6798 return size;
6801 /* Return the size of the information in the .debug_aranges section. */
6803 static unsigned long
6804 size_of_aranges (void)
6806 unsigned long size;
6808 size = DWARF_ARANGES_HEADER_SIZE;
6810 /* Count the address/length pair for this compilation unit. */
6811 size += 2 * DWARF2_ADDR_SIZE;
6812 size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
6814 /* Count the two zero words used to terminated the address range table. */
6815 size += 2 * DWARF2_ADDR_SIZE;
6816 return size;
6819 /* Select the encoding of an attribute value. */
6821 static enum dwarf_form
6822 value_format (dw_attr_ref a)
6824 switch (a->dw_attr_val.val_class)
6826 case dw_val_class_addr:
6827 return DW_FORM_addr;
6828 case dw_val_class_range_list:
6829 case dw_val_class_offset:
6830 case dw_val_class_loc_list:
6831 switch (DWARF_OFFSET_SIZE)
6833 case 4:
6834 return DW_FORM_data4;
6835 case 8:
6836 return DW_FORM_data8;
6837 default:
6838 gcc_unreachable ();
6840 case dw_val_class_loc:
6841 switch (constant_size (size_of_locs (AT_loc (a))))
6843 case 1:
6844 return DW_FORM_block1;
6845 case 2:
6846 return DW_FORM_block2;
6847 default:
6848 gcc_unreachable ();
6850 case dw_val_class_const:
6851 return DW_FORM_sdata;
6852 case dw_val_class_unsigned_const:
6853 switch (constant_size (AT_unsigned (a)))
6855 case 1:
6856 return DW_FORM_data1;
6857 case 2:
6858 return DW_FORM_data2;
6859 case 4:
6860 return DW_FORM_data4;
6861 case 8:
6862 return DW_FORM_data8;
6863 default:
6864 gcc_unreachable ();
6866 case dw_val_class_long_long:
6867 return DW_FORM_block1;
6868 case dw_val_class_vec:
6869 return DW_FORM_block1;
6870 case dw_val_class_flag:
6871 return DW_FORM_flag;
6872 case dw_val_class_die_ref:
6873 if (AT_ref_external (a))
6874 return DW_FORM_ref_addr;
6875 else
6876 return DW_FORM_ref;
6877 case dw_val_class_fde_ref:
6878 return DW_FORM_data;
6879 case dw_val_class_lbl_id:
6880 return DW_FORM_addr;
6881 case dw_val_class_lineptr:
6882 case dw_val_class_macptr:
6883 return DW_FORM_data;
6884 case dw_val_class_str:
6885 return AT_string_form (a);
6886 case dw_val_class_file:
6887 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
6889 case 1:
6890 return DW_FORM_data1;
6891 case 2:
6892 return DW_FORM_data2;
6893 case 4:
6894 return DW_FORM_data4;
6895 default:
6896 gcc_unreachable ();
6899 default:
6900 gcc_unreachable ();
6904 /* Output the encoding of an attribute value. */
6906 static void
6907 output_value_format (dw_attr_ref a)
6909 enum dwarf_form form = value_format (a);
6911 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
6914 /* Output the .debug_abbrev section which defines the DIE abbreviation
6915 table. */
6917 static void
6918 output_abbrev_section (void)
6920 unsigned long abbrev_id;
6922 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6924 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6925 unsigned ix;
6926 dw_attr_ref a_attr;
6928 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
6929 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
6930 dwarf_tag_name (abbrev->die_tag));
6932 if (abbrev->die_child != NULL)
6933 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
6934 else
6935 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
6937 for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
6938 ix++)
6940 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
6941 dwarf_attr_name (a_attr->dw_attr));
6942 output_value_format (a_attr);
6945 dw2_asm_output_data (1, 0, NULL);
6946 dw2_asm_output_data (1, 0, NULL);
6949 /* Terminate the table. */
6950 dw2_asm_output_data (1, 0, NULL);
6953 /* Output a symbol we can use to refer to this DIE from another CU. */
6955 static inline void
6956 output_die_symbol (dw_die_ref die)
6958 char *sym = die->die_symbol;
6960 if (sym == 0)
6961 return;
6963 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
6964 /* We make these global, not weak; if the target doesn't support
6965 .linkonce, it doesn't support combining the sections, so debugging
6966 will break. */
6967 targetm.asm_out.globalize_label (asm_out_file, sym);
6969 ASM_OUTPUT_LABEL (asm_out_file, sym);
6972 /* Return a new location list, given the begin and end range, and the
6973 expression. gensym tells us whether to generate a new internal symbol for
6974 this location list node, which is done for the head of the list only. */
6976 static inline dw_loc_list_ref
6977 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
6978 const char *section, unsigned int gensym)
6980 dw_loc_list_ref retlist = ggc_alloc_cleared (sizeof (dw_loc_list_node));
6982 retlist->begin = begin;
6983 retlist->end = end;
6984 retlist->expr = expr;
6985 retlist->section = section;
6986 if (gensym)
6987 retlist->ll_symbol = gen_internal_sym ("LLST");
6989 return retlist;
6992 /* Add a location description expression to a location list. */
6994 static inline void
6995 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
6996 const char *begin, const char *end,
6997 const char *section)
6999 dw_loc_list_ref *d;
7001 /* Find the end of the chain. */
7002 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
7005 /* Add a new location list node to the list. */
7006 *d = new_loc_list (descr, begin, end, section, 0);
7009 static void
7010 dwarf2out_switch_text_section (void)
7012 dw_fde_ref fde;
7014 gcc_assert (cfun);
7016 fde = &fde_table[fde_table_in_use - 1];
7017 fde->dw_fde_switched_sections = true;
7018 fde->dw_fde_hot_section_label = cfun->hot_section_label;
7019 fde->dw_fde_hot_section_end_label = cfun->hot_section_end_label;
7020 fde->dw_fde_unlikely_section_label = cfun->cold_section_label;
7021 fde->dw_fde_unlikely_section_end_label = cfun->cold_section_end_label;
7022 have_multiple_function_sections = true;
7024 /* Reset the current label on switching text sections, so that we
7025 don't attempt to advance_loc4 between labels in different sections. */
7026 fde->dw_fde_current_label = NULL;
7029 /* Output the location list given to us. */
7031 static void
7032 output_loc_list (dw_loc_list_ref list_head)
7034 dw_loc_list_ref curr = list_head;
7036 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
7038 /* Walk the location list, and output each range + expression. */
7039 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
7041 unsigned long size;
7042 if (!have_multiple_function_sections)
7044 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
7045 "Location list begin address (%s)",
7046 list_head->ll_symbol);
7047 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
7048 "Location list end address (%s)",
7049 list_head->ll_symbol);
7051 else
7053 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
7054 "Location list begin address (%s)",
7055 list_head->ll_symbol);
7056 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
7057 "Location list end address (%s)",
7058 list_head->ll_symbol);
7060 size = size_of_locs (curr->expr);
7062 /* Output the block length for this list of location operations. */
7063 gcc_assert (size <= 0xffff);
7064 dw2_asm_output_data (2, size, "%s", "Location expression size");
7066 output_loc_sequence (curr->expr);
7069 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7070 "Location list terminator begin (%s)",
7071 list_head->ll_symbol);
7072 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7073 "Location list terminator end (%s)",
7074 list_head->ll_symbol);
7077 /* Output the DIE and its attributes. Called recursively to generate
7078 the definitions of each child DIE. */
7080 static void
7081 output_die (dw_die_ref die)
7083 dw_attr_ref a;
7084 dw_die_ref c;
7085 unsigned long size;
7086 unsigned ix;
7088 /* If someone in another CU might refer to us, set up a symbol for
7089 them to point to. */
7090 if (die->die_symbol)
7091 output_die_symbol (die);
7093 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
7094 die->die_offset, dwarf_tag_name (die->die_tag));
7096 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7098 const char *name = dwarf_attr_name (a->dw_attr);
7100 switch (AT_class (a))
7102 case dw_val_class_addr:
7103 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
7104 break;
7106 case dw_val_class_offset:
7107 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
7108 "%s", name);
7109 break;
7111 case dw_val_class_range_list:
7113 char *p = strchr (ranges_section_label, '\0');
7115 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
7116 a->dw_attr_val.v.val_offset);
7117 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
7118 debug_ranges_section, "%s", name);
7119 *p = '\0';
7121 break;
7123 case dw_val_class_loc:
7124 size = size_of_locs (AT_loc (a));
7126 /* Output the block length for this list of location operations. */
7127 dw2_asm_output_data (constant_size (size), size, "%s", name);
7129 output_loc_sequence (AT_loc (a));
7130 break;
7132 case dw_val_class_const:
7133 /* ??? It would be slightly more efficient to use a scheme like is
7134 used for unsigned constants below, but gdb 4.x does not sign
7135 extend. Gdb 5.x does sign extend. */
7136 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
7137 break;
7139 case dw_val_class_unsigned_const:
7140 dw2_asm_output_data (constant_size (AT_unsigned (a)),
7141 AT_unsigned (a), "%s", name);
7142 break;
7144 case dw_val_class_long_long:
7146 unsigned HOST_WIDE_INT first, second;
7148 dw2_asm_output_data (1,
7149 2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7150 "%s", name);
7152 if (WORDS_BIG_ENDIAN)
7154 first = a->dw_attr_val.v.val_long_long.hi;
7155 second = a->dw_attr_val.v.val_long_long.low;
7157 else
7159 first = a->dw_attr_val.v.val_long_long.low;
7160 second = a->dw_attr_val.v.val_long_long.hi;
7163 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7164 first, "long long constant");
7165 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7166 second, NULL);
7168 break;
7170 case dw_val_class_vec:
7172 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
7173 unsigned int len = a->dw_attr_val.v.val_vec.length;
7174 unsigned int i;
7175 unsigned char *p;
7177 dw2_asm_output_data (1, len * elt_size, "%s", name);
7178 if (elt_size > sizeof (HOST_WIDE_INT))
7180 elt_size /= 2;
7181 len *= 2;
7183 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
7184 i < len;
7185 i++, p += elt_size)
7186 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
7187 "fp or vector constant word %u", i);
7188 break;
7191 case dw_val_class_flag:
7192 dw2_asm_output_data (1, AT_flag (a), "%s", name);
7193 break;
7195 case dw_val_class_loc_list:
7197 char *sym = AT_loc_list (a)->ll_symbol;
7199 gcc_assert (sym);
7200 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
7201 "%s", name);
7203 break;
7205 case dw_val_class_die_ref:
7206 if (AT_ref_external (a))
7208 char *sym = AT_ref (a)->die_symbol;
7210 gcc_assert (sym);
7211 dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, debug_info_section,
7212 "%s", name);
7214 else
7216 gcc_assert (AT_ref (a)->die_offset);
7217 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
7218 "%s", name);
7220 break;
7222 case dw_val_class_fde_ref:
7224 char l1[20];
7226 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
7227 a->dw_attr_val.v.val_fde_index * 2);
7228 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
7229 "%s", name);
7231 break;
7233 case dw_val_class_lbl_id:
7234 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
7235 break;
7237 case dw_val_class_lineptr:
7238 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7239 debug_line_section, "%s", name);
7240 break;
7242 case dw_val_class_macptr:
7243 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7244 debug_macinfo_section, "%s", name);
7245 break;
7247 case dw_val_class_str:
7248 if (AT_string_form (a) == DW_FORM_strp)
7249 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
7250 a->dw_attr_val.v.val_str->label,
7251 debug_str_section,
7252 "%s: \"%s\"", name, AT_string (a));
7253 else
7254 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
7255 break;
7257 case dw_val_class_file:
7259 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
7261 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
7262 a->dw_attr_val.v.val_file->filename);
7263 break;
7266 default:
7267 gcc_unreachable ();
7271 FOR_EACH_CHILD (die, c, output_die (c));
7273 /* Add null byte to terminate sibling list. */
7274 if (die->die_child != NULL)
7275 dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
7276 die->die_offset);
7279 /* Output the compilation unit that appears at the beginning of the
7280 .debug_info section, and precedes the DIE descriptions. */
7282 static void
7283 output_compilation_unit_header (void)
7285 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7286 dw2_asm_output_data (4, 0xffffffff,
7287 "Initial length escape value indicating 64-bit DWARF extension");
7288 dw2_asm_output_data (DWARF_OFFSET_SIZE,
7289 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
7290 "Length of Compilation Unit Info");
7291 dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
7292 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
7293 debug_abbrev_section,
7294 "Offset Into Abbrev. Section");
7295 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
7298 /* Output the compilation unit DIE and its children. */
7300 static void
7301 output_comp_unit (dw_die_ref die, int output_if_empty)
7303 const char *secname;
7304 char *oldsym, *tmp;
7306 /* Unless we are outputting main CU, we may throw away empty ones. */
7307 if (!output_if_empty && die->die_child == NULL)
7308 return;
7310 /* Even if there are no children of this DIE, we must output the information
7311 about the compilation unit. Otherwise, on an empty translation unit, we
7312 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
7313 will then complain when examining the file. First mark all the DIEs in
7314 this CU so we know which get local refs. */
7315 mark_dies (die);
7317 build_abbrev_table (die);
7319 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
7320 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7321 calc_die_sizes (die);
7323 oldsym = die->die_symbol;
7324 if (oldsym)
7326 tmp = alloca (strlen (oldsym) + 24);
7328 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
7329 secname = tmp;
7330 die->die_symbol = NULL;
7331 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
7333 else
7334 switch_to_section (debug_info_section);
7336 /* Output debugging information. */
7337 output_compilation_unit_header ();
7338 output_die (die);
7340 /* Leave the marks on the main CU, so we can check them in
7341 output_pubnames. */
7342 if (oldsym)
7344 unmark_dies (die);
7345 die->die_symbol = oldsym;
7349 /* Return the DWARF2/3 pubname associated with a decl. */
7351 static const char *
7352 dwarf2_name (tree decl, int scope)
7354 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
7357 /* Add a new entry to .debug_pubnames if appropriate. */
7359 static void
7360 add_pubname (tree decl, dw_die_ref die)
7362 pubname_entry e;
7364 if (! TREE_PUBLIC (decl))
7365 return;
7367 e.die = die;
7368 e.name = xstrdup (dwarf2_name (decl, 1));
7369 VEC_safe_push (pubname_entry, gc, pubname_table, &e);
7372 /* Add a new entry to .debug_pubtypes if appropriate. */
7374 static void
7375 add_pubtype (tree decl, dw_die_ref die)
7377 pubname_entry e;
7379 e.name = NULL;
7380 if ((TREE_PUBLIC (decl)
7381 || die->die_parent == comp_unit_die)
7382 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
7384 e.die = die;
7385 if (TYPE_P (decl))
7387 if (TYPE_NAME (decl))
7389 if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
7390 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
7391 else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
7392 && DECL_NAME (TYPE_NAME (decl)))
7393 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
7394 else
7395 e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
7398 else
7399 e.name = xstrdup (dwarf2_name (decl, 1));
7401 /* If we don't have a name for the type, there's no point in adding
7402 it to the table. */
7403 if (e.name && e.name[0] != '\0')
7404 VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
7408 /* Output the public names table used to speed up access to externally
7409 visible names; or the public types table used to find type definitions. */
7411 static void
7412 output_pubnames (VEC (pubname_entry, gc) * names)
7414 unsigned i;
7415 unsigned long pubnames_length = size_of_pubnames (names);
7416 pubname_ref pub;
7418 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7419 dw2_asm_output_data (4, 0xffffffff,
7420 "Initial length escape value indicating 64-bit DWARF extension");
7421 if (names == pubname_table)
7422 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7423 "Length of Public Names Info");
7424 else
7425 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7426 "Length of Public Type Names Info");
7427 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7428 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7429 debug_info_section,
7430 "Offset of Compilation Unit Info");
7431 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
7432 "Compilation Unit Length");
7434 for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
7436 /* We shouldn't see pubnames for DIEs outside of the main CU. */
7437 if (names == pubname_table)
7438 gcc_assert (pub->die->die_mark);
7440 if (names != pubtype_table
7441 || pub->die->die_offset != 0
7442 || !flag_eliminate_unused_debug_types)
7444 dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
7445 "DIE offset");
7447 dw2_asm_output_nstring (pub->name, -1, "external name");
7451 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
7454 /* Add a new entry to .debug_aranges if appropriate. */
7456 static void
7457 add_arange (tree decl, dw_die_ref die)
7459 if (! DECL_SECTION_NAME (decl))
7460 return;
7462 if (arange_table_in_use == arange_table_allocated)
7464 arange_table_allocated += ARANGE_TABLE_INCREMENT;
7465 arange_table = ggc_realloc (arange_table,
7466 (arange_table_allocated
7467 * sizeof (dw_die_ref)));
7468 memset (arange_table + arange_table_in_use, 0,
7469 ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
7472 arange_table[arange_table_in_use++] = die;
7475 /* Output the information that goes into the .debug_aranges table.
7476 Namely, define the beginning and ending address range of the
7477 text section generated for this compilation unit. */
7479 static void
7480 output_aranges (void)
7482 unsigned i;
7483 unsigned long aranges_length = size_of_aranges ();
7485 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7486 dw2_asm_output_data (4, 0xffffffff,
7487 "Initial length escape value indicating 64-bit DWARF extension");
7488 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
7489 "Length of Address Ranges Info");
7490 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7491 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7492 debug_info_section,
7493 "Offset of Compilation Unit Info");
7494 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
7495 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
7497 /* We need to align to twice the pointer size here. */
7498 if (DWARF_ARANGES_PAD_SIZE)
7500 /* Pad using a 2 byte words so that padding is correct for any
7501 pointer size. */
7502 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
7503 2 * DWARF2_ADDR_SIZE);
7504 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
7505 dw2_asm_output_data (2, 0, NULL);
7508 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
7509 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
7510 text_section_label, "Length");
7511 if (flag_reorder_blocks_and_partition)
7513 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
7514 "Address");
7515 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
7516 cold_text_section_label, "Length");
7519 for (i = 0; i < arange_table_in_use; i++)
7521 dw_die_ref die = arange_table[i];
7523 /* We shouldn't see aranges for DIEs outside of the main CU. */
7524 gcc_assert (die->die_mark);
7526 if (die->die_tag == DW_TAG_subprogram)
7528 dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
7529 "Address");
7530 dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
7531 get_AT_low_pc (die), "Length");
7533 else
7535 /* A static variable; extract the symbol from DW_AT_location.
7536 Note that this code isn't currently hit, as we only emit
7537 aranges for functions (jason 9/23/99). */
7538 dw_attr_ref a = get_AT (die, DW_AT_location);
7539 dw_loc_descr_ref loc;
7541 gcc_assert (a && AT_class (a) == dw_val_class_loc);
7543 loc = AT_loc (a);
7544 gcc_assert (loc->dw_loc_opc == DW_OP_addr);
7546 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
7547 loc->dw_loc_oprnd1.v.val_addr, "Address");
7548 dw2_asm_output_data (DWARF2_ADDR_SIZE,
7549 get_AT_unsigned (die, DW_AT_byte_size),
7550 "Length");
7554 /* Output the terminator words. */
7555 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7556 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7559 /* Add a new entry to .debug_ranges. Return the offset at which it
7560 was placed. */
7562 static unsigned int
7563 add_ranges (tree block)
7565 unsigned int in_use = ranges_table_in_use;
7567 if (in_use == ranges_table_allocated)
7569 ranges_table_allocated += RANGES_TABLE_INCREMENT;
7570 ranges_table
7571 = ggc_realloc (ranges_table, (ranges_table_allocated
7572 * sizeof (struct dw_ranges_struct)));
7573 memset (ranges_table + ranges_table_in_use, 0,
7574 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
7577 ranges_table[in_use].block_num = (block ? BLOCK_NUMBER (block) : 0);
7578 ranges_table_in_use = in_use + 1;
7580 return in_use * 2 * DWARF2_ADDR_SIZE;
7583 static void
7584 output_ranges (void)
7586 unsigned i;
7587 static const char *const start_fmt = "Offset 0x%x";
7588 const char *fmt = start_fmt;
7590 for (i = 0; i < ranges_table_in_use; i++)
7592 int block_num = ranges_table[i].block_num;
7594 if (block_num)
7596 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
7597 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
7599 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
7600 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
7602 /* If all code is in the text section, then the compilation
7603 unit base address defaults to DW_AT_low_pc, which is the
7604 base of the text section. */
7605 if (!have_multiple_function_sections)
7607 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
7608 text_section_label,
7609 fmt, i * 2 * DWARF2_ADDR_SIZE);
7610 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
7611 text_section_label, NULL);
7614 /* Otherwise, we add a DW_AT_entry_pc attribute to force the
7615 compilation unit base address to zero, which allows us to
7616 use absolute addresses, and not worry about whether the
7617 target supports cross-section arithmetic. */
7618 else
7620 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
7621 fmt, i * 2 * DWARF2_ADDR_SIZE);
7622 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
7625 fmt = NULL;
7627 else
7629 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7630 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7631 fmt = start_fmt;
7636 /* Data structure containing information about input files. */
7637 struct file_info
7639 const char *path; /* Complete file name. */
7640 const char *fname; /* File name part. */
7641 int length; /* Length of entire string. */
7642 struct dwarf_file_data * file_idx; /* Index in input file table. */
7643 int dir_idx; /* Index in directory table. */
7646 /* Data structure containing information about directories with source
7647 files. */
7648 struct dir_info
7650 const char *path; /* Path including directory name. */
7651 int length; /* Path length. */
7652 int prefix; /* Index of directory entry which is a prefix. */
7653 int count; /* Number of files in this directory. */
7654 int dir_idx; /* Index of directory used as base. */
7657 /* Callback function for file_info comparison. We sort by looking at
7658 the directories in the path. */
7660 static int
7661 file_info_cmp (const void *p1, const void *p2)
7663 const struct file_info *s1 = p1;
7664 const struct file_info *s2 = p2;
7665 unsigned char *cp1;
7666 unsigned char *cp2;
7668 /* Take care of file names without directories. We need to make sure that
7669 we return consistent values to qsort since some will get confused if
7670 we return the same value when identical operands are passed in opposite
7671 orders. So if neither has a directory, return 0 and otherwise return
7672 1 or -1 depending on which one has the directory. */
7673 if ((s1->path == s1->fname || s2->path == s2->fname))
7674 return (s2->path == s2->fname) - (s1->path == s1->fname);
7676 cp1 = (unsigned char *) s1->path;
7677 cp2 = (unsigned char *) s2->path;
7679 while (1)
7681 ++cp1;
7682 ++cp2;
7683 /* Reached the end of the first path? If so, handle like above. */
7684 if ((cp1 == (unsigned char *) s1->fname)
7685 || (cp2 == (unsigned char *) s2->fname))
7686 return ((cp2 == (unsigned char *) s2->fname)
7687 - (cp1 == (unsigned char *) s1->fname));
7689 /* Character of current path component the same? */
7690 else if (*cp1 != *cp2)
7691 return *cp1 - *cp2;
7695 struct file_name_acquire_data
7697 struct file_info *files;
7698 int used_files;
7699 int max_files;
7702 /* Traversal function for the hash table. */
7704 static int
7705 file_name_acquire (void ** slot, void *data)
7707 struct file_name_acquire_data *fnad = data;
7708 struct dwarf_file_data *d = *slot;
7709 struct file_info *fi;
7710 const char *f;
7712 gcc_assert (fnad->max_files >= d->emitted_number);
7714 if (! d->emitted_number)
7715 return 1;
7717 gcc_assert (fnad->max_files != fnad->used_files);
7719 fi = fnad->files + fnad->used_files++;
7721 /* Skip all leading "./". */
7722 f = d->filename;
7723 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
7724 f += 2;
7726 /* Create a new array entry. */
7727 fi->path = f;
7728 fi->length = strlen (f);
7729 fi->file_idx = d;
7731 /* Search for the file name part. */
7732 f = strrchr (f, DIR_SEPARATOR);
7733 #if defined (DIR_SEPARATOR_2)
7735 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
7737 if (g != NULL)
7739 if (f == NULL || f < g)
7740 f = g;
7743 #endif
7745 fi->fname = f == NULL ? fi->path : f + 1;
7746 return 1;
7749 /* Output the directory table and the file name table. We try to minimize
7750 the total amount of memory needed. A heuristic is used to avoid large
7751 slowdowns with many input files. */
7753 static void
7754 output_file_names (void)
7756 struct file_name_acquire_data fnad;
7757 int numfiles;
7758 struct file_info *files;
7759 struct dir_info *dirs;
7760 int *saved;
7761 int *savehere;
7762 int *backmap;
7763 int ndirs;
7764 int idx_offset;
7765 int i;
7766 int idx;
7768 if (!last_emitted_file)
7770 dw2_asm_output_data (1, 0, "End directory table");
7771 dw2_asm_output_data (1, 0, "End file name table");
7772 return;
7775 numfiles = last_emitted_file->emitted_number;
7777 /* Allocate the various arrays we need. */
7778 files = alloca (numfiles * sizeof (struct file_info));
7779 dirs = alloca (numfiles * sizeof (struct dir_info));
7781 fnad.files = files;
7782 fnad.used_files = 0;
7783 fnad.max_files = numfiles;
7784 htab_traverse (file_table, file_name_acquire, &fnad);
7785 gcc_assert (fnad.used_files == fnad.max_files);
7787 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
7789 /* Find all the different directories used. */
7790 dirs[0].path = files[0].path;
7791 dirs[0].length = files[0].fname - files[0].path;
7792 dirs[0].prefix = -1;
7793 dirs[0].count = 1;
7794 dirs[0].dir_idx = 0;
7795 files[0].dir_idx = 0;
7796 ndirs = 1;
7798 for (i = 1; i < numfiles; i++)
7799 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
7800 && memcmp (dirs[ndirs - 1].path, files[i].path,
7801 dirs[ndirs - 1].length) == 0)
7803 /* Same directory as last entry. */
7804 files[i].dir_idx = ndirs - 1;
7805 ++dirs[ndirs - 1].count;
7807 else
7809 int j;
7811 /* This is a new directory. */
7812 dirs[ndirs].path = files[i].path;
7813 dirs[ndirs].length = files[i].fname - files[i].path;
7814 dirs[ndirs].count = 1;
7815 dirs[ndirs].dir_idx = ndirs;
7816 files[i].dir_idx = ndirs;
7818 /* Search for a prefix. */
7819 dirs[ndirs].prefix = -1;
7820 for (j = 0; j < ndirs; j++)
7821 if (dirs[j].length < dirs[ndirs].length
7822 && dirs[j].length > 1
7823 && (dirs[ndirs].prefix == -1
7824 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
7825 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
7826 dirs[ndirs].prefix = j;
7828 ++ndirs;
7831 /* Now to the actual work. We have to find a subset of the directories which
7832 allow expressing the file name using references to the directory table
7833 with the least amount of characters. We do not do an exhaustive search
7834 where we would have to check out every combination of every single
7835 possible prefix. Instead we use a heuristic which provides nearly optimal
7836 results in most cases and never is much off. */
7837 saved = alloca (ndirs * sizeof (int));
7838 savehere = alloca (ndirs * sizeof (int));
7840 memset (saved, '\0', ndirs * sizeof (saved[0]));
7841 for (i = 0; i < ndirs; i++)
7843 int j;
7844 int total;
7846 /* We can always save some space for the current directory. But this
7847 does not mean it will be enough to justify adding the directory. */
7848 savehere[i] = dirs[i].length;
7849 total = (savehere[i] - saved[i]) * dirs[i].count;
7851 for (j = i + 1; j < ndirs; j++)
7853 savehere[j] = 0;
7854 if (saved[j] < dirs[i].length)
7856 /* Determine whether the dirs[i] path is a prefix of the
7857 dirs[j] path. */
7858 int k;
7860 k = dirs[j].prefix;
7861 while (k != -1 && k != (int) i)
7862 k = dirs[k].prefix;
7864 if (k == (int) i)
7866 /* Yes it is. We can possibly save some memory by
7867 writing the filenames in dirs[j] relative to
7868 dirs[i]. */
7869 savehere[j] = dirs[i].length;
7870 total += (savehere[j] - saved[j]) * dirs[j].count;
7875 /* Check whether we can save enough to justify adding the dirs[i]
7876 directory. */
7877 if (total > dirs[i].length + 1)
7879 /* It's worthwhile adding. */
7880 for (j = i; j < ndirs; j++)
7881 if (savehere[j] > 0)
7883 /* Remember how much we saved for this directory so far. */
7884 saved[j] = savehere[j];
7886 /* Remember the prefix directory. */
7887 dirs[j].dir_idx = i;
7892 /* Emit the directory name table. */
7893 idx = 1;
7894 idx_offset = dirs[0].length > 0 ? 1 : 0;
7895 for (i = 1 - idx_offset; i < ndirs; i++)
7896 dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
7897 "Directory Entry: 0x%x", i + idx_offset);
7899 dw2_asm_output_data (1, 0, "End directory table");
7901 /* We have to emit them in the order of emitted_number since that's
7902 used in the debug info generation. To do this efficiently we
7903 generate a back-mapping of the indices first. */
7904 backmap = alloca (numfiles * sizeof (int));
7905 for (i = 0; i < numfiles; i++)
7906 backmap[files[i].file_idx->emitted_number - 1] = i;
7908 /* Now write all the file names. */
7909 for (i = 0; i < numfiles; i++)
7911 int file_idx = backmap[i];
7912 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
7914 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
7915 "File Entry: 0x%x", (unsigned) i + 1);
7917 /* Include directory index. */
7918 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
7920 /* Modification time. */
7921 dw2_asm_output_data_uleb128 (0, NULL);
7923 /* File length in bytes. */
7924 dw2_asm_output_data_uleb128 (0, NULL);
7927 dw2_asm_output_data (1, 0, "End file name table");
7931 /* Output the source line number correspondence information. This
7932 information goes into the .debug_line section. */
7934 static void
7935 output_line_info (void)
7937 char l1[20], l2[20], p1[20], p2[20];
7938 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
7939 char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
7940 unsigned opc;
7941 unsigned n_op_args;
7942 unsigned long lt_index;
7943 unsigned long current_line;
7944 long line_offset;
7945 long line_delta;
7946 unsigned long current_file;
7947 unsigned long function;
7949 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
7950 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
7951 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
7952 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
7954 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7955 dw2_asm_output_data (4, 0xffffffff,
7956 "Initial length escape value indicating 64-bit DWARF extension");
7957 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
7958 "Length of Source Line Info");
7959 ASM_OUTPUT_LABEL (asm_out_file, l1);
7961 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7962 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
7963 ASM_OUTPUT_LABEL (asm_out_file, p1);
7965 /* Define the architecture-dependent minimum instruction length (in
7966 bytes). In this implementation of DWARF, this field is used for
7967 information purposes only. Since GCC generates assembly language,
7968 we have no a priori knowledge of how many instruction bytes are
7969 generated for each source line, and therefore can use only the
7970 DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
7971 commands. Accordingly, we fix this as `1', which is "correct
7972 enough" for all architectures, and don't let the target override. */
7973 dw2_asm_output_data (1, 1,
7974 "Minimum Instruction Length");
7976 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
7977 "Default is_stmt_start flag");
7978 dw2_asm_output_data (1, DWARF_LINE_BASE,
7979 "Line Base Value (Special Opcodes)");
7980 dw2_asm_output_data (1, DWARF_LINE_RANGE,
7981 "Line Range Value (Special Opcodes)");
7982 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
7983 "Special Opcode Base");
7985 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
7987 switch (opc)
7989 case DW_LNS_advance_pc:
7990 case DW_LNS_advance_line:
7991 case DW_LNS_set_file:
7992 case DW_LNS_set_column:
7993 case DW_LNS_fixed_advance_pc:
7994 n_op_args = 1;
7995 break;
7996 default:
7997 n_op_args = 0;
7998 break;
8001 dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
8002 opc, n_op_args);
8005 /* Write out the information about the files we use. */
8006 output_file_names ();
8007 ASM_OUTPUT_LABEL (asm_out_file, p2);
8009 /* We used to set the address register to the first location in the text
8010 section here, but that didn't accomplish anything since we already
8011 have a line note for the opening brace of the first function. */
8013 /* Generate the line number to PC correspondence table, encoded as
8014 a series of state machine operations. */
8015 current_file = 1;
8016 current_line = 1;
8018 if (cfun && in_cold_section_p)
8019 strcpy (prev_line_label, cfun->cold_section_label);
8020 else
8021 strcpy (prev_line_label, text_section_label);
8022 for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
8024 dw_line_info_ref line_info = &line_info_table[lt_index];
8026 #if 0
8027 /* Disable this optimization for now; GDB wants to see two line notes
8028 at the beginning of a function so it can find the end of the
8029 prologue. */
8031 /* Don't emit anything for redundant notes. Just updating the
8032 address doesn't accomplish anything, because we already assume
8033 that anything after the last address is this line. */
8034 if (line_info->dw_line_num == current_line
8035 && line_info->dw_file_num == current_file)
8036 continue;
8037 #endif
8039 /* Emit debug info for the address of the current line.
8041 Unfortunately, we have little choice here currently, and must always
8042 use the most general form. GCC does not know the address delta
8043 itself, so we can't use DW_LNS_advance_pc. Many ports do have length
8044 attributes which will give an upper bound on the address range. We
8045 could perhaps use length attributes to determine when it is safe to
8046 use DW_LNS_fixed_advance_pc. */
8048 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
8049 if (0)
8051 /* This can handle deltas up to 0xffff. This takes 3 bytes. */
8052 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8053 "DW_LNS_fixed_advance_pc");
8054 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8056 else
8058 /* This can handle any delta. This takes
8059 4+DWARF2_ADDR_SIZE bytes. */
8060 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8061 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8062 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8063 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8066 strcpy (prev_line_label, line_label);
8068 /* Emit debug info for the source file of the current line, if
8069 different from the previous line. */
8070 if (line_info->dw_file_num != current_file)
8072 current_file = line_info->dw_file_num;
8073 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8074 dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
8077 /* Emit debug info for the current line number, choosing the encoding
8078 that uses the least amount of space. */
8079 if (line_info->dw_line_num != current_line)
8081 line_offset = line_info->dw_line_num - current_line;
8082 line_delta = line_offset - DWARF_LINE_BASE;
8083 current_line = line_info->dw_line_num;
8084 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
8085 /* This can handle deltas from -10 to 234, using the current
8086 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE. This
8087 takes 1 byte. */
8088 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
8089 "line %lu", current_line);
8090 else
8092 /* This can handle any delta. This takes at least 4 bytes,
8093 depending on the value being encoded. */
8094 dw2_asm_output_data (1, DW_LNS_advance_line,
8095 "advance to line %lu", current_line);
8096 dw2_asm_output_data_sleb128 (line_offset, NULL);
8097 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8100 else
8101 /* We still need to start a new row, so output a copy insn. */
8102 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8105 /* Emit debug info for the address of the end of the function. */
8106 if (0)
8108 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8109 "DW_LNS_fixed_advance_pc");
8110 dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
8112 else
8114 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8115 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8116 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8117 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
8120 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
8121 dw2_asm_output_data_uleb128 (1, NULL);
8122 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
8124 function = 0;
8125 current_file = 1;
8126 current_line = 1;
8127 for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
8129 dw_separate_line_info_ref line_info
8130 = &separate_line_info_table[lt_index];
8132 #if 0
8133 /* Don't emit anything for redundant notes. */
8134 if (line_info->dw_line_num == current_line
8135 && line_info->dw_file_num == current_file
8136 && line_info->function == function)
8137 goto cont;
8138 #endif
8140 /* Emit debug info for the address of the current line. If this is
8141 a new function, or the first line of a function, then we need
8142 to handle it differently. */
8143 ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
8144 lt_index);
8145 if (function != line_info->function)
8147 function = line_info->function;
8149 /* Set the address register to the first line in the function. */
8150 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8151 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8152 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8153 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8155 else
8157 /* ??? See the DW_LNS_advance_pc comment above. */
8158 if (0)
8160 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8161 "DW_LNS_fixed_advance_pc");
8162 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8164 else
8166 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8167 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8168 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8169 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8173 strcpy (prev_line_label, line_label);
8175 /* Emit debug info for the source file of the current line, if
8176 different from the previous line. */
8177 if (line_info->dw_file_num != current_file)
8179 current_file = line_info->dw_file_num;
8180 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8181 dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
8184 /* Emit debug info for the current line number, choosing the encoding
8185 that uses the least amount of space. */
8186 if (line_info->dw_line_num != current_line)
8188 line_offset = line_info->dw_line_num - current_line;
8189 line_delta = line_offset - DWARF_LINE_BASE;
8190 current_line = line_info->dw_line_num;
8191 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
8192 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
8193 "line %lu", current_line);
8194 else
8196 dw2_asm_output_data (1, DW_LNS_advance_line,
8197 "advance to line %lu", current_line);
8198 dw2_asm_output_data_sleb128 (line_offset, NULL);
8199 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8202 else
8203 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8205 #if 0
8206 cont:
8207 #endif
8209 lt_index++;
8211 /* If we're done with a function, end its sequence. */
8212 if (lt_index == separate_line_info_table_in_use
8213 || separate_line_info_table[lt_index].function != function)
8215 current_file = 1;
8216 current_line = 1;
8218 /* Emit debug info for the address of the end of the function. */
8219 ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
8220 if (0)
8222 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8223 "DW_LNS_fixed_advance_pc");
8224 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8226 else
8228 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8229 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8230 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8231 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8234 /* Output the marker for the end of this sequence. */
8235 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
8236 dw2_asm_output_data_uleb128 (1, NULL);
8237 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
8241 /* Output the marker for the end of the line number info. */
8242 ASM_OUTPUT_LABEL (asm_out_file, l2);
8245 /* Given a pointer to a tree node for some base type, return a pointer to
8246 a DIE that describes the given type.
8248 This routine must only be called for GCC type nodes that correspond to
8249 Dwarf base (fundamental) types. */
8251 static dw_die_ref
8252 base_type_die (tree type)
8254 dw_die_ref base_type_result;
8255 enum dwarf_type encoding;
8257 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
8258 return 0;
8260 switch (TREE_CODE (type))
8262 case INTEGER_TYPE:
8263 if (TYPE_STRING_FLAG (type))
8265 if (TYPE_UNSIGNED (type))
8266 encoding = DW_ATE_unsigned_char;
8267 else
8268 encoding = DW_ATE_signed_char;
8270 else if (TYPE_UNSIGNED (type))
8271 encoding = DW_ATE_unsigned;
8272 else
8273 encoding = DW_ATE_signed;
8274 break;
8276 case REAL_TYPE:
8277 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
8278 encoding = DW_ATE_decimal_float;
8279 else
8280 encoding = DW_ATE_float;
8281 break;
8283 /* Dwarf2 doesn't know anything about complex ints, so use
8284 a user defined type for it. */
8285 case COMPLEX_TYPE:
8286 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
8287 encoding = DW_ATE_complex_float;
8288 else
8289 encoding = DW_ATE_lo_user;
8290 break;
8292 case BOOLEAN_TYPE:
8293 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
8294 encoding = DW_ATE_boolean;
8295 break;
8297 default:
8298 /* No other TREE_CODEs are Dwarf fundamental types. */
8299 gcc_unreachable ();
8302 base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
8304 /* This probably indicates a bug. */
8305 if (! TYPE_NAME (type))
8306 add_name_attribute (base_type_result, "__unknown__");
8308 add_AT_unsigned (base_type_result, DW_AT_byte_size,
8309 int_size_in_bytes (type));
8310 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
8312 return base_type_result;
8315 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
8316 given input type is a Dwarf "fundamental" type. Otherwise return null. */
8318 static inline int
8319 is_base_type (tree type)
8321 switch (TREE_CODE (type))
8323 case ERROR_MARK:
8324 case VOID_TYPE:
8325 case INTEGER_TYPE:
8326 case REAL_TYPE:
8327 case COMPLEX_TYPE:
8328 case BOOLEAN_TYPE:
8329 return 1;
8331 case ARRAY_TYPE:
8332 case RECORD_TYPE:
8333 case UNION_TYPE:
8334 case QUAL_UNION_TYPE:
8335 case ENUMERAL_TYPE:
8336 case FUNCTION_TYPE:
8337 case METHOD_TYPE:
8338 case POINTER_TYPE:
8339 case REFERENCE_TYPE:
8340 case OFFSET_TYPE:
8341 case LANG_TYPE:
8342 case VECTOR_TYPE:
8343 return 0;
8345 default:
8346 gcc_unreachable ();
8349 return 0;
8352 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
8353 node, return the size in bits for the type if it is a constant, or else
8354 return the alignment for the type if the type's size is not constant, or
8355 else return BITS_PER_WORD if the type actually turns out to be an
8356 ERROR_MARK node. */
8358 static inline unsigned HOST_WIDE_INT
8359 simple_type_size_in_bits (tree type)
8361 if (TREE_CODE (type) == ERROR_MARK)
8362 return BITS_PER_WORD;
8363 else if (TYPE_SIZE (type) == NULL_TREE)
8364 return 0;
8365 else if (host_integerp (TYPE_SIZE (type), 1))
8366 return tree_low_cst (TYPE_SIZE (type), 1);
8367 else
8368 return TYPE_ALIGN (type);
8371 /* Return true if the debug information for the given type should be
8372 emitted as a subrange type. */
8374 static inline bool
8375 is_subrange_type (tree type)
8377 tree subtype = TREE_TYPE (type);
8379 /* Subrange types are identified by the fact that they are integer
8380 types, and that they have a subtype which is either an integer type
8381 or an enumeral type. */
8383 if (TREE_CODE (type) != INTEGER_TYPE
8384 || subtype == NULL_TREE)
8385 return false;
8387 if (TREE_CODE (subtype) != INTEGER_TYPE
8388 && TREE_CODE (subtype) != ENUMERAL_TYPE)
8389 return false;
8391 if (TREE_CODE (type) == TREE_CODE (subtype)
8392 && int_size_in_bytes (type) == int_size_in_bytes (subtype)
8393 && TYPE_MIN_VALUE (type) != NULL
8394 && TYPE_MIN_VALUE (subtype) != NULL
8395 && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
8396 && TYPE_MAX_VALUE (type) != NULL
8397 && TYPE_MAX_VALUE (subtype) != NULL
8398 && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
8400 /* The type and its subtype have the same representation. If in
8401 addition the two types also have the same name, then the given
8402 type is not a subrange type, but rather a plain base type. */
8403 /* FIXME: brobecker/2004-03-22:
8404 Sizetype INTEGER_CSTs nodes are canonicalized. It should
8405 therefore be sufficient to check the TYPE_SIZE node pointers
8406 rather than checking the actual size. Unfortunately, we have
8407 found some cases, such as in the Ada "integer" type, where
8408 this is not the case. Until this problem is solved, we need to
8409 keep checking the actual size. */
8410 tree type_name = TYPE_NAME (type);
8411 tree subtype_name = TYPE_NAME (subtype);
8413 if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
8414 type_name = DECL_NAME (type_name);
8416 if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
8417 subtype_name = DECL_NAME (subtype_name);
8419 if (type_name == subtype_name)
8420 return false;
8423 return true;
8426 /* Given a pointer to a tree node for a subrange type, return a pointer
8427 to a DIE that describes the given type. */
8429 static dw_die_ref
8430 subrange_type_die (tree type, dw_die_ref context_die)
8432 dw_die_ref subrange_die;
8433 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
8435 if (context_die == NULL)
8436 context_die = comp_unit_die;
8438 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
8440 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
8442 /* The size of the subrange type and its base type do not match,
8443 so we need to generate a size attribute for the subrange type. */
8444 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
8447 if (TYPE_MIN_VALUE (type) != NULL)
8448 add_bound_info (subrange_die, DW_AT_lower_bound,
8449 TYPE_MIN_VALUE (type));
8450 if (TYPE_MAX_VALUE (type) != NULL)
8451 add_bound_info (subrange_die, DW_AT_upper_bound,
8452 TYPE_MAX_VALUE (type));
8454 return subrange_die;
8457 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
8458 entry that chains various modifiers in front of the given type. */
8460 static dw_die_ref
8461 modified_type_die (tree type, int is_const_type, int is_volatile_type,
8462 dw_die_ref context_die)
8464 enum tree_code code = TREE_CODE (type);
8465 dw_die_ref mod_type_die;
8466 dw_die_ref sub_die = NULL;
8467 tree item_type = NULL;
8468 tree qualified_type;
8469 tree name;
8471 if (code == ERROR_MARK)
8472 return NULL;
8474 /* See if we already have the appropriately qualified variant of
8475 this type. */
8476 qualified_type
8477 = get_qualified_type (type,
8478 ((is_const_type ? TYPE_QUAL_CONST : 0)
8479 | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
8481 /* If we do, then we can just use its DIE, if it exists. */
8482 if (qualified_type)
8484 mod_type_die = lookup_type_die (qualified_type);
8485 if (mod_type_die)
8486 return mod_type_die;
8489 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
8491 /* Handle C typedef types. */
8492 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
8494 tree dtype = TREE_TYPE (name);
8496 if (qualified_type == dtype)
8498 /* For a named type, use the typedef. */
8499 gen_type_die (qualified_type, context_die);
8500 return lookup_type_die (qualified_type);
8502 else if (is_const_type < TYPE_READONLY (dtype)
8503 || is_volatile_type < TYPE_VOLATILE (dtype)
8504 || (is_const_type <= TYPE_READONLY (dtype)
8505 && is_volatile_type <= TYPE_VOLATILE (dtype)
8506 && DECL_ORIGINAL_TYPE (name) != type))
8507 /* cv-unqualified version of named type. Just use the unnamed
8508 type to which it refers. */
8509 return modified_type_die (DECL_ORIGINAL_TYPE (name),
8510 is_const_type, is_volatile_type,
8511 context_die);
8512 /* Else cv-qualified version of named type; fall through. */
8515 if (is_const_type)
8517 mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
8518 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
8520 else if (is_volatile_type)
8522 mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
8523 sub_die = modified_type_die (type, 0, 0, context_die);
8525 else if (code == POINTER_TYPE)
8527 mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
8528 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8529 simple_type_size_in_bits (type) / BITS_PER_UNIT);
8530 item_type = TREE_TYPE (type);
8532 else if (code == REFERENCE_TYPE)
8534 mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
8535 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8536 simple_type_size_in_bits (type) / BITS_PER_UNIT);
8537 item_type = TREE_TYPE (type);
8539 else if (is_subrange_type (type))
8541 mod_type_die = subrange_type_die (type, context_die);
8542 item_type = TREE_TYPE (type);
8544 else if (is_base_type (type))
8545 mod_type_die = base_type_die (type);
8546 else
8548 gen_type_die (type, context_die);
8550 /* We have to get the type_main_variant here (and pass that to the
8551 `lookup_type_die' routine) because the ..._TYPE node we have
8552 might simply be a *copy* of some original type node (where the
8553 copy was created to help us keep track of typedef names) and
8554 that copy might have a different TYPE_UID from the original
8555 ..._TYPE node. */
8556 if (TREE_CODE (type) != VECTOR_TYPE)
8557 return lookup_type_die (type_main_variant (type));
8558 else
8559 /* Vectors have the debugging information in the type,
8560 not the main variant. */
8561 return lookup_type_die (type);
8564 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
8565 don't output a DW_TAG_typedef, since there isn't one in the
8566 user's program; just attach a DW_AT_name to the type. */
8567 if (name
8568 && (TREE_CODE (name) != TYPE_DECL || TREE_TYPE (name) == qualified_type))
8570 if (TREE_CODE (name) == TYPE_DECL)
8571 /* Could just call add_name_and_src_coords_attributes here,
8572 but since this is a builtin type it doesn't have any
8573 useful source coordinates anyway. */
8574 name = DECL_NAME (name);
8575 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
8578 if (qualified_type)
8579 equate_type_number_to_die (qualified_type, mod_type_die);
8581 if (item_type)
8582 /* We must do this after the equate_type_number_to_die call, in case
8583 this is a recursive type. This ensures that the modified_type_die
8584 recursion will terminate even if the type is recursive. Recursive
8585 types are possible in Ada. */
8586 sub_die = modified_type_die (item_type,
8587 TYPE_READONLY (item_type),
8588 TYPE_VOLATILE (item_type),
8589 context_die);
8591 if (sub_die != NULL)
8592 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
8594 return mod_type_die;
8597 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
8598 an enumerated type. */
8600 static inline int
8601 type_is_enum (tree type)
8603 return TREE_CODE (type) == ENUMERAL_TYPE;
8606 /* Return the DBX register number described by a given RTL node. */
8608 static unsigned int
8609 dbx_reg_number (rtx rtl)
8611 unsigned regno = REGNO (rtl);
8613 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
8615 #ifdef LEAF_REG_REMAP
8616 if (current_function_uses_only_leaf_regs)
8618 int leaf_reg = LEAF_REG_REMAP (regno);
8619 if (leaf_reg != -1)
8620 regno = (unsigned) leaf_reg;
8622 #endif
8624 return DBX_REGISTER_NUMBER (regno);
8627 /* Optionally add a DW_OP_piece term to a location description expression.
8628 DW_OP_piece is only added if the location description expression already
8629 doesn't end with DW_OP_piece. */
8631 static void
8632 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
8634 dw_loc_descr_ref loc;
8636 if (*list_head != NULL)
8638 /* Find the end of the chain. */
8639 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
8642 if (loc->dw_loc_opc != DW_OP_piece)
8643 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
8647 /* Return a location descriptor that designates a machine register or
8648 zero if there is none. */
8650 static dw_loc_descr_ref
8651 reg_loc_descriptor (rtx rtl)
8653 rtx regs;
8655 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
8656 return 0;
8658 regs = targetm.dwarf_register_span (rtl);
8660 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
8661 return multiple_reg_loc_descriptor (rtl, regs);
8662 else
8663 return one_reg_loc_descriptor (dbx_reg_number (rtl));
8666 /* Return a location descriptor that designates a machine register for
8667 a given hard register number. */
8669 static dw_loc_descr_ref
8670 one_reg_loc_descriptor (unsigned int regno)
8672 if (regno <= 31)
8673 return new_loc_descr (DW_OP_reg0 + regno, 0, 0);
8674 else
8675 return new_loc_descr (DW_OP_regx, regno, 0);
8678 /* Given an RTL of a register, return a location descriptor that
8679 designates a value that spans more than one register. */
8681 static dw_loc_descr_ref
8682 multiple_reg_loc_descriptor (rtx rtl, rtx regs)
8684 int nregs, size, i;
8685 unsigned reg;
8686 dw_loc_descr_ref loc_result = NULL;
8688 reg = REGNO (rtl);
8689 #ifdef LEAF_REG_REMAP
8690 if (current_function_uses_only_leaf_regs)
8692 int leaf_reg = LEAF_REG_REMAP (reg);
8693 if (leaf_reg != -1)
8694 reg = (unsigned) leaf_reg;
8696 #endif
8697 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
8698 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
8700 /* Simple, contiguous registers. */
8701 if (regs == NULL_RTX)
8703 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
8705 loc_result = NULL;
8706 while (nregs--)
8708 dw_loc_descr_ref t;
8710 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg));
8711 add_loc_descr (&loc_result, t);
8712 add_loc_descr_op_piece (&loc_result, size);
8713 ++reg;
8715 return loc_result;
8718 /* Now onto stupid register sets in non contiguous locations. */
8720 gcc_assert (GET_CODE (regs) == PARALLEL);
8722 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8723 loc_result = NULL;
8725 for (i = 0; i < XVECLEN (regs, 0); ++i)
8727 dw_loc_descr_ref t;
8729 t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)));
8730 add_loc_descr (&loc_result, t);
8731 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8732 add_loc_descr_op_piece (&loc_result, size);
8734 return loc_result;
8737 /* Return a location descriptor that designates a constant. */
8739 static dw_loc_descr_ref
8740 int_loc_descriptor (HOST_WIDE_INT i)
8742 enum dwarf_location_atom op;
8744 /* Pick the smallest representation of a constant, rather than just
8745 defaulting to the LEB encoding. */
8746 if (i >= 0)
8748 if (i <= 31)
8749 op = DW_OP_lit0 + i;
8750 else if (i <= 0xff)
8751 op = DW_OP_const1u;
8752 else if (i <= 0xffff)
8753 op = DW_OP_const2u;
8754 else if (HOST_BITS_PER_WIDE_INT == 32
8755 || i <= 0xffffffff)
8756 op = DW_OP_const4u;
8757 else
8758 op = DW_OP_constu;
8760 else
8762 if (i >= -0x80)
8763 op = DW_OP_const1s;
8764 else if (i >= -0x8000)
8765 op = DW_OP_const2s;
8766 else if (HOST_BITS_PER_WIDE_INT == 32
8767 || i >= -0x80000000)
8768 op = DW_OP_const4s;
8769 else
8770 op = DW_OP_consts;
8773 return new_loc_descr (op, i, 0);
8776 /* Return a location descriptor that designates a base+offset location. */
8778 static dw_loc_descr_ref
8779 based_loc_descr (rtx reg, HOST_WIDE_INT offset)
8781 unsigned int regno;
8783 /* We only use "frame base" when we're sure we're talking about the
8784 post-prologue local stack frame. We do this by *not* running
8785 register elimination until this point, and recognizing the special
8786 argument pointer and soft frame pointer rtx's. */
8787 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
8789 rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
8791 if (elim != reg)
8793 if (GET_CODE (elim) == PLUS)
8795 offset += INTVAL (XEXP (elim, 1));
8796 elim = XEXP (elim, 0);
8798 gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
8799 : stack_pointer_rtx));
8800 offset += frame_pointer_fb_offset;
8802 return new_loc_descr (DW_OP_fbreg, offset, 0);
8806 regno = dbx_reg_number (reg);
8807 if (regno <= 31)
8808 return new_loc_descr (DW_OP_breg0 + regno, offset, 0);
8809 else
8810 return new_loc_descr (DW_OP_bregx, regno, offset);
8813 /* Return true if this RTL expression describes a base+offset calculation. */
8815 static inline int
8816 is_based_loc (rtx rtl)
8818 return (GET_CODE (rtl) == PLUS
8819 && ((REG_P (XEXP (rtl, 0))
8820 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
8821 && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
8824 /* Return a descriptor that describes the concatenation of N locations
8825 used to form the address of a memory location. */
8827 static dw_loc_descr_ref
8828 concatn_mem_loc_descriptor (rtx concatn, enum machine_mode mode)
8830 unsigned int i;
8831 dw_loc_descr_ref cc_loc_result = NULL;
8832 unsigned int n = XVECLEN (concatn, 0);
8834 for (i = 0; i < n; ++i)
8836 dw_loc_descr_ref ref;
8837 rtx x = XVECEXP (concatn, 0, i);
8839 ref = mem_loc_descriptor (x, mode);
8840 if (ref == NULL)
8841 return NULL;
8843 add_loc_descr (&cc_loc_result, ref);
8844 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
8847 return cc_loc_result;
8850 /* The following routine converts the RTL for a variable or parameter
8851 (resident in memory) into an equivalent Dwarf representation of a
8852 mechanism for getting the address of that same variable onto the top of a
8853 hypothetical "address evaluation" stack.
8855 When creating memory location descriptors, we are effectively transforming
8856 the RTL for a memory-resident object into its Dwarf postfix expression
8857 equivalent. This routine recursively descends an RTL tree, turning
8858 it into Dwarf postfix code as it goes.
8860 MODE is the mode of the memory reference, needed to handle some
8861 autoincrement addressing modes.
8863 CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
8864 location list for RTL.
8866 Return 0 if we can't represent the location. */
8868 static dw_loc_descr_ref
8869 mem_loc_descriptor (rtx rtl, enum machine_mode mode)
8871 dw_loc_descr_ref mem_loc_result = NULL;
8872 enum dwarf_location_atom op;
8874 /* Note that for a dynamically sized array, the location we will generate a
8875 description of here will be the lowest numbered location which is
8876 actually within the array. That's *not* necessarily the same as the
8877 zeroth element of the array. */
8879 rtl = targetm.delegitimize_address (rtl);
8881 switch (GET_CODE (rtl))
8883 case POST_INC:
8884 case POST_DEC:
8885 case POST_MODIFY:
8886 /* POST_INC and POST_DEC can be handled just like a SUBREG. So we
8887 just fall into the SUBREG code. */
8889 /* ... fall through ... */
8891 case SUBREG:
8892 /* The case of a subreg may arise when we have a local (register)
8893 variable or a formal (register) parameter which doesn't quite fill
8894 up an entire register. For now, just assume that it is
8895 legitimate to make the Dwarf info refer to the whole register which
8896 contains the given subreg. */
8897 rtl = XEXP (rtl, 0);
8899 /* ... fall through ... */
8901 case REG:
8902 /* Whenever a register number forms a part of the description of the
8903 method for calculating the (dynamic) address of a memory resident
8904 object, DWARF rules require the register number be referred to as
8905 a "base register". This distinction is not based in any way upon
8906 what category of register the hardware believes the given register
8907 belongs to. This is strictly DWARF terminology we're dealing with
8908 here. Note that in cases where the location of a memory-resident
8909 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
8910 OP_CONST (0)) the actual DWARF location descriptor that we generate
8911 may just be OP_BASEREG (basereg). This may look deceptively like
8912 the object in question was allocated to a register (rather than in
8913 memory) so DWARF consumers need to be aware of the subtle
8914 distinction between OP_REG and OP_BASEREG. */
8915 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
8916 mem_loc_result = based_loc_descr (rtl, 0);
8917 break;
8919 case MEM:
8920 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl));
8921 if (mem_loc_result != 0)
8922 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
8923 break;
8925 case LO_SUM:
8926 rtl = XEXP (rtl, 1);
8928 /* ... fall through ... */
8930 case LABEL_REF:
8931 /* Some ports can transform a symbol ref into a label ref, because
8932 the symbol ref is too far away and has to be dumped into a constant
8933 pool. */
8934 case CONST:
8935 case SYMBOL_REF:
8936 /* Alternatively, the symbol in the constant pool might be referenced
8937 by a different symbol. */
8938 if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
8940 bool marked;
8941 rtx tmp = get_pool_constant_mark (rtl, &marked);
8943 if (GET_CODE (tmp) == SYMBOL_REF)
8945 rtl = tmp;
8946 if (CONSTANT_POOL_ADDRESS_P (tmp))
8947 get_pool_constant_mark (tmp, &marked);
8948 else
8949 marked = true;
8952 /* If all references to this pool constant were optimized away,
8953 it was not output and thus we can't represent it.
8954 FIXME: might try to use DW_OP_const_value here, though
8955 DW_OP_piece complicates it. */
8956 if (!marked)
8957 return 0;
8960 mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
8961 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
8962 mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
8963 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
8964 break;
8966 case PRE_MODIFY:
8967 /* Extract the PLUS expression nested inside and fall into
8968 PLUS code below. */
8969 rtl = XEXP (rtl, 1);
8970 goto plus;
8972 case PRE_INC:
8973 case PRE_DEC:
8974 /* Turn these into a PLUS expression and fall into the PLUS code
8975 below. */
8976 rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
8977 GEN_INT (GET_CODE (rtl) == PRE_INC
8978 ? GET_MODE_UNIT_SIZE (mode)
8979 : -GET_MODE_UNIT_SIZE (mode)));
8981 /* ... fall through ... */
8983 case PLUS:
8984 plus:
8985 if (is_based_loc (rtl))
8986 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
8987 INTVAL (XEXP (rtl, 1)));
8988 else
8990 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode);
8991 if (mem_loc_result == 0)
8992 break;
8994 if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
8995 && INTVAL (XEXP (rtl, 1)) >= 0)
8996 add_loc_descr (&mem_loc_result,
8997 new_loc_descr (DW_OP_plus_uconst,
8998 INTVAL (XEXP (rtl, 1)), 0));
8999 else
9001 add_loc_descr (&mem_loc_result,
9002 mem_loc_descriptor (XEXP (rtl, 1), mode));
9003 add_loc_descr (&mem_loc_result,
9004 new_loc_descr (DW_OP_plus, 0, 0));
9007 break;
9009 /* If a pseudo-reg is optimized away, it is possible for it to
9010 be replaced with a MEM containing a multiply or shift. */
9011 case MULT:
9012 op = DW_OP_mul;
9013 goto do_binop;
9015 case ASHIFT:
9016 op = DW_OP_shl;
9017 goto do_binop;
9019 case ASHIFTRT:
9020 op = DW_OP_shra;
9021 goto do_binop;
9023 case LSHIFTRT:
9024 op = DW_OP_shr;
9025 goto do_binop;
9027 do_binop:
9029 dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode);
9030 dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode);
9032 if (op0 == 0 || op1 == 0)
9033 break;
9035 mem_loc_result = op0;
9036 add_loc_descr (&mem_loc_result, op1);
9037 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
9038 break;
9041 case CONST_INT:
9042 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
9043 break;
9045 case CONCATN:
9046 mem_loc_result = concatn_mem_loc_descriptor (rtl, mode);
9047 break;
9049 default:
9050 gcc_unreachable ();
9053 return mem_loc_result;
9056 /* Return a descriptor that describes the concatenation of two locations.
9057 This is typically a complex variable. */
9059 static dw_loc_descr_ref
9060 concat_loc_descriptor (rtx x0, rtx x1)
9062 dw_loc_descr_ref cc_loc_result = NULL;
9063 dw_loc_descr_ref x0_ref = loc_descriptor (x0);
9064 dw_loc_descr_ref x1_ref = loc_descriptor (x1);
9066 if (x0_ref == 0 || x1_ref == 0)
9067 return 0;
9069 cc_loc_result = x0_ref;
9070 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
9072 add_loc_descr (&cc_loc_result, x1_ref);
9073 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
9075 return cc_loc_result;
9078 /* Return a descriptor that describes the concatenation of N
9079 locations. */
9081 static dw_loc_descr_ref
9082 concatn_loc_descriptor (rtx concatn)
9084 unsigned int i;
9085 dw_loc_descr_ref cc_loc_result = NULL;
9086 unsigned int n = XVECLEN (concatn, 0);
9088 for (i = 0; i < n; ++i)
9090 dw_loc_descr_ref ref;
9091 rtx x = XVECEXP (concatn, 0, i);
9093 ref = loc_descriptor (x);
9094 if (ref == NULL)
9095 return NULL;
9097 add_loc_descr (&cc_loc_result, ref);
9098 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
9101 return cc_loc_result;
9104 /* Output a proper Dwarf location descriptor for a variable or parameter
9105 which is either allocated in a register or in a memory location. For a
9106 register, we just generate an OP_REG and the register number. For a
9107 memory location we provide a Dwarf postfix expression describing how to
9108 generate the (dynamic) address of the object onto the address stack.
9110 If we don't know how to describe it, return 0. */
9112 static dw_loc_descr_ref
9113 loc_descriptor (rtx rtl)
9115 dw_loc_descr_ref loc_result = NULL;
9117 switch (GET_CODE (rtl))
9119 case SUBREG:
9120 /* The case of a subreg may arise when we have a local (register)
9121 variable or a formal (register) parameter which doesn't quite fill
9122 up an entire register. For now, just assume that it is
9123 legitimate to make the Dwarf info refer to the whole register which
9124 contains the given subreg. */
9125 rtl = SUBREG_REG (rtl);
9127 /* ... fall through ... */
9129 case REG:
9130 loc_result = reg_loc_descriptor (rtl);
9131 break;
9133 case MEM:
9134 loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl));
9135 break;
9137 case CONCAT:
9138 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1));
9139 break;
9141 case CONCATN:
9142 loc_result = concatn_loc_descriptor (rtl);
9143 break;
9145 case VAR_LOCATION:
9146 /* Single part. */
9147 if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
9149 loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0));
9150 break;
9153 rtl = XEXP (rtl, 1);
9154 /* FALLTHRU */
9156 case PARALLEL:
9158 rtvec par_elems = XVEC (rtl, 0);
9159 int num_elem = GET_NUM_ELEM (par_elems);
9160 enum machine_mode mode;
9161 int i;
9163 /* Create the first one, so we have something to add to. */
9164 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0));
9165 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
9166 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9167 for (i = 1; i < num_elem; i++)
9169 dw_loc_descr_ref temp;
9171 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0));
9172 add_loc_descr (&loc_result, temp);
9173 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
9174 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9177 break;
9179 default:
9180 gcc_unreachable ();
9183 return loc_result;
9186 /* Similar, but generate the descriptor from trees instead of rtl. This comes
9187 up particularly with variable length arrays. WANT_ADDRESS is 2 if this is
9188 a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
9189 top-level invocation, and we require the address of LOC; is 0 if we require
9190 the value of LOC. */
9192 static dw_loc_descr_ref
9193 loc_descriptor_from_tree_1 (tree loc, int want_address)
9195 dw_loc_descr_ref ret, ret1;
9196 int have_address = 0;
9197 enum dwarf_location_atom op;
9199 /* ??? Most of the time we do not take proper care for sign/zero
9200 extending the values properly. Hopefully this won't be a real
9201 problem... */
9203 switch (TREE_CODE (loc))
9205 case ERROR_MARK:
9206 return 0;
9208 case PLACEHOLDER_EXPR:
9209 /* This case involves extracting fields from an object to determine the
9210 position of other fields. We don't try to encode this here. The
9211 only user of this is Ada, which encodes the needed information using
9212 the names of types. */
9213 return 0;
9215 case CALL_EXPR:
9216 return 0;
9218 case PREINCREMENT_EXPR:
9219 case PREDECREMENT_EXPR:
9220 case POSTINCREMENT_EXPR:
9221 case POSTDECREMENT_EXPR:
9222 /* There are no opcodes for these operations. */
9223 return 0;
9225 case ADDR_EXPR:
9226 /* If we already want an address, there's nothing we can do. */
9227 if (want_address)
9228 return 0;
9230 /* Otherwise, process the argument and look for the address. */
9231 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
9233 case VAR_DECL:
9234 if (DECL_THREAD_LOCAL_P (loc))
9236 rtx rtl;
9238 /* If this is not defined, we have no way to emit the data. */
9239 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
9240 return 0;
9242 /* The way DW_OP_GNU_push_tls_address is specified, we can only
9243 look up addresses of objects in the current module. */
9244 if (DECL_EXTERNAL (loc))
9245 return 0;
9247 rtl = rtl_for_decl_location (loc);
9248 if (rtl == NULL_RTX)
9249 return 0;
9251 if (!MEM_P (rtl))
9252 return 0;
9253 rtl = XEXP (rtl, 0);
9254 if (! CONSTANT_P (rtl))
9255 return 0;
9257 ret = new_loc_descr (INTERNAL_DW_OP_tls_addr, 0, 0);
9258 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9259 ret->dw_loc_oprnd1.v.val_addr = rtl;
9261 ret1 = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
9262 add_loc_descr (&ret, ret1);
9264 have_address = 1;
9265 break;
9267 /* FALLTHRU */
9269 case PARM_DECL:
9270 if (DECL_HAS_VALUE_EXPR_P (loc))
9271 return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
9272 want_address);
9273 /* FALLTHRU */
9275 case RESULT_DECL:
9276 case FUNCTION_DECL:
9278 rtx rtl = rtl_for_decl_location (loc);
9280 if (rtl == NULL_RTX)
9281 return 0;
9282 else if (GET_CODE (rtl) == CONST_INT)
9284 HOST_WIDE_INT val = INTVAL (rtl);
9285 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
9286 val &= GET_MODE_MASK (DECL_MODE (loc));
9287 ret = int_loc_descriptor (val);
9289 else if (GET_CODE (rtl) == CONST_STRING)
9290 return 0;
9291 else if (CONSTANT_P (rtl))
9293 ret = new_loc_descr (DW_OP_addr, 0, 0);
9294 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9295 ret->dw_loc_oprnd1.v.val_addr = rtl;
9297 else
9299 enum machine_mode mode;
9301 /* Certain constructs can only be represented at top-level. */
9302 if (want_address == 2)
9303 return loc_descriptor (rtl);
9305 mode = GET_MODE (rtl);
9306 if (MEM_P (rtl))
9308 rtl = XEXP (rtl, 0);
9309 have_address = 1;
9311 ret = mem_loc_descriptor (rtl, mode);
9314 break;
9316 case INDIRECT_REF:
9317 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9318 have_address = 1;
9319 break;
9321 case COMPOUND_EXPR:
9322 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
9324 case NOP_EXPR:
9325 case CONVERT_EXPR:
9326 case NON_LVALUE_EXPR:
9327 case VIEW_CONVERT_EXPR:
9328 case SAVE_EXPR:
9329 case GIMPLE_MODIFY_STMT:
9330 return loc_descriptor_from_tree_1 (GENERIC_TREE_OPERAND (loc, 0),
9331 want_address);
9333 case COMPONENT_REF:
9334 case BIT_FIELD_REF:
9335 case ARRAY_REF:
9336 case ARRAY_RANGE_REF:
9338 tree obj, offset;
9339 HOST_WIDE_INT bitsize, bitpos, bytepos;
9340 enum machine_mode mode;
9341 int volatilep;
9342 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
9344 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
9345 &unsignedp, &volatilep, false);
9347 if (obj == loc)
9348 return 0;
9350 ret = loc_descriptor_from_tree_1 (obj, 1);
9351 if (ret == 0
9352 || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
9353 return 0;
9355 if (offset != NULL_TREE)
9357 /* Variable offset. */
9358 add_loc_descr (&ret, loc_descriptor_from_tree_1 (offset, 0));
9359 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9362 bytepos = bitpos / BITS_PER_UNIT;
9363 if (bytepos > 0)
9364 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
9365 else if (bytepos < 0)
9367 add_loc_descr (&ret, int_loc_descriptor (bytepos));
9368 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9371 have_address = 1;
9372 break;
9375 case INTEGER_CST:
9376 if (host_integerp (loc, 0))
9377 ret = int_loc_descriptor (tree_low_cst (loc, 0));
9378 else
9379 return 0;
9380 break;
9382 case CONSTRUCTOR:
9384 /* Get an RTL for this, if something has been emitted. */
9385 rtx rtl = lookup_constant_def (loc);
9386 enum machine_mode mode;
9388 if (!rtl || !MEM_P (rtl))
9389 return 0;
9390 mode = GET_MODE (rtl);
9391 rtl = XEXP (rtl, 0);
9392 ret = mem_loc_descriptor (rtl, mode);
9393 have_address = 1;
9394 break;
9397 case TRUTH_AND_EXPR:
9398 case TRUTH_ANDIF_EXPR:
9399 case BIT_AND_EXPR:
9400 op = DW_OP_and;
9401 goto do_binop;
9403 case TRUTH_XOR_EXPR:
9404 case BIT_XOR_EXPR:
9405 op = DW_OP_xor;
9406 goto do_binop;
9408 case TRUTH_OR_EXPR:
9409 case TRUTH_ORIF_EXPR:
9410 case BIT_IOR_EXPR:
9411 op = DW_OP_or;
9412 goto do_binop;
9414 case FLOOR_DIV_EXPR:
9415 case CEIL_DIV_EXPR:
9416 case ROUND_DIV_EXPR:
9417 case TRUNC_DIV_EXPR:
9418 op = DW_OP_div;
9419 goto do_binop;
9421 case MINUS_EXPR:
9422 op = DW_OP_minus;
9423 goto do_binop;
9425 case FLOOR_MOD_EXPR:
9426 case CEIL_MOD_EXPR:
9427 case ROUND_MOD_EXPR:
9428 case TRUNC_MOD_EXPR:
9429 op = DW_OP_mod;
9430 goto do_binop;
9432 case MULT_EXPR:
9433 op = DW_OP_mul;
9434 goto do_binop;
9436 case LSHIFT_EXPR:
9437 op = DW_OP_shl;
9438 goto do_binop;
9440 case RSHIFT_EXPR:
9441 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
9442 goto do_binop;
9444 case PLUS_EXPR:
9445 if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
9446 && host_integerp (TREE_OPERAND (loc, 1), 0))
9448 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9449 if (ret == 0)
9450 return 0;
9452 add_loc_descr (&ret,
9453 new_loc_descr (DW_OP_plus_uconst,
9454 tree_low_cst (TREE_OPERAND (loc, 1),
9456 0));
9457 break;
9460 op = DW_OP_plus;
9461 goto do_binop;
9463 case LE_EXPR:
9464 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9465 return 0;
9467 op = DW_OP_le;
9468 goto do_binop;
9470 case GE_EXPR:
9471 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9472 return 0;
9474 op = DW_OP_ge;
9475 goto do_binop;
9477 case LT_EXPR:
9478 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9479 return 0;
9481 op = DW_OP_lt;
9482 goto do_binop;
9484 case GT_EXPR:
9485 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9486 return 0;
9488 op = DW_OP_gt;
9489 goto do_binop;
9491 case EQ_EXPR:
9492 op = DW_OP_eq;
9493 goto do_binop;
9495 case NE_EXPR:
9496 op = DW_OP_ne;
9497 goto do_binop;
9499 do_binop:
9500 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9501 ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9502 if (ret == 0 || ret1 == 0)
9503 return 0;
9505 add_loc_descr (&ret, ret1);
9506 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9507 break;
9509 case TRUTH_NOT_EXPR:
9510 case BIT_NOT_EXPR:
9511 op = DW_OP_not;
9512 goto do_unop;
9514 case ABS_EXPR:
9515 op = DW_OP_abs;
9516 goto do_unop;
9518 case NEGATE_EXPR:
9519 op = DW_OP_neg;
9520 goto do_unop;
9522 do_unop:
9523 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9524 if (ret == 0)
9525 return 0;
9527 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9528 break;
9530 case MIN_EXPR:
9531 case MAX_EXPR:
9533 const enum tree_code code =
9534 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
9536 loc = build3 (COND_EXPR, TREE_TYPE (loc),
9537 build2 (code, integer_type_node,
9538 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
9539 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
9542 /* ... fall through ... */
9544 case COND_EXPR:
9546 dw_loc_descr_ref lhs
9547 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9548 dw_loc_descr_ref rhs
9549 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
9550 dw_loc_descr_ref bra_node, jump_node, tmp;
9552 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9553 if (ret == 0 || lhs == 0 || rhs == 0)
9554 return 0;
9556 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
9557 add_loc_descr (&ret, bra_node);
9559 add_loc_descr (&ret, rhs);
9560 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
9561 add_loc_descr (&ret, jump_node);
9563 add_loc_descr (&ret, lhs);
9564 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9565 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
9567 /* ??? Need a node to point the skip at. Use a nop. */
9568 tmp = new_loc_descr (DW_OP_nop, 0, 0);
9569 add_loc_descr (&ret, tmp);
9570 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9571 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
9573 break;
9575 case FIX_TRUNC_EXPR:
9576 return 0;
9578 default:
9579 /* Leave front-end specific codes as simply unknown. This comes
9580 up, for instance, with the C STMT_EXPR. */
9581 if ((unsigned int) TREE_CODE (loc)
9582 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
9583 return 0;
9585 #ifdef ENABLE_CHECKING
9586 /* Otherwise this is a generic code; we should just lists all of
9587 these explicitly. We forgot one. */
9588 gcc_unreachable ();
9589 #else
9590 /* In a release build, we want to degrade gracefully: better to
9591 generate incomplete debugging information than to crash. */
9592 return NULL;
9593 #endif
9596 /* Show if we can't fill the request for an address. */
9597 if (want_address && !have_address)
9598 return 0;
9600 /* If we've got an address and don't want one, dereference. */
9601 if (!want_address && have_address && ret)
9603 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
9605 if (size > DWARF2_ADDR_SIZE || size == -1)
9606 return 0;
9607 else if (size == DWARF2_ADDR_SIZE)
9608 op = DW_OP_deref;
9609 else
9610 op = DW_OP_deref_size;
9612 add_loc_descr (&ret, new_loc_descr (op, size, 0));
9615 return ret;
9618 static inline dw_loc_descr_ref
9619 loc_descriptor_from_tree (tree loc)
9621 return loc_descriptor_from_tree_1 (loc, 2);
9624 /* Given a value, round it up to the lowest multiple of `boundary'
9625 which is not less than the value itself. */
9627 static inline HOST_WIDE_INT
9628 ceiling (HOST_WIDE_INT value, unsigned int boundary)
9630 return (((value + boundary - 1) / boundary) * boundary);
9633 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
9634 pointer to the declared type for the relevant field variable, or return
9635 `integer_type_node' if the given node turns out to be an
9636 ERROR_MARK node. */
9638 static inline tree
9639 field_type (tree decl)
9641 tree type;
9643 if (TREE_CODE (decl) == ERROR_MARK)
9644 return integer_type_node;
9646 type = DECL_BIT_FIELD_TYPE (decl);
9647 if (type == NULL_TREE)
9648 type = TREE_TYPE (decl);
9650 return type;
9653 /* Given a pointer to a tree node, return the alignment in bits for
9654 it, or else return BITS_PER_WORD if the node actually turns out to
9655 be an ERROR_MARK node. */
9657 static inline unsigned
9658 simple_type_align_in_bits (tree type)
9660 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
9663 static inline unsigned
9664 simple_decl_align_in_bits (tree decl)
9666 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
9669 /* Return the result of rounding T up to ALIGN. */
9671 static inline HOST_WIDE_INT
9672 round_up_to_align (HOST_WIDE_INT t, unsigned int align)
9674 /* We must be careful if T is negative because HOST_WIDE_INT can be
9675 either "above" or "below" unsigned int as per the C promotion
9676 rules, depending on the host, thus making the signedness of the
9677 direct multiplication and division unpredictable. */
9678 unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
9680 u += align - 1;
9681 u /= align;
9682 u *= align;
9684 return (HOST_WIDE_INT) u;
9687 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
9688 lowest addressed byte of the "containing object" for the given FIELD_DECL,
9689 or return 0 if we are unable to determine what that offset is, either
9690 because the argument turns out to be a pointer to an ERROR_MARK node, or
9691 because the offset is actually variable. (We can't handle the latter case
9692 just yet). */
9694 static HOST_WIDE_INT
9695 field_byte_offset (tree decl)
9697 HOST_WIDE_INT object_offset_in_bits;
9698 HOST_WIDE_INT bitpos_int;
9700 if (TREE_CODE (decl) == ERROR_MARK)
9701 return 0;
9703 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
9705 /* We cannot yet cope with fields whose positions are variable, so
9706 for now, when we see such things, we simply return 0. Someday, we may
9707 be able to handle such cases, but it will be damn difficult. */
9708 if (! host_integerp (bit_position (decl), 0))
9709 return 0;
9711 bitpos_int = int_bit_position (decl);
9713 #ifdef PCC_BITFIELD_TYPE_MATTERS
9714 if (PCC_BITFIELD_TYPE_MATTERS)
9716 tree type;
9717 tree field_size_tree;
9718 HOST_WIDE_INT deepest_bitpos;
9719 unsigned HOST_WIDE_INT field_size_in_bits;
9720 unsigned int type_align_in_bits;
9721 unsigned int decl_align_in_bits;
9722 unsigned HOST_WIDE_INT type_size_in_bits;
9724 type = field_type (decl);
9725 field_size_tree = DECL_SIZE (decl);
9727 /* The size could be unspecified if there was an error, or for
9728 a flexible array member. */
9729 if (! field_size_tree)
9730 field_size_tree = bitsize_zero_node;
9732 /* If we don't know the size of the field, pretend it's a full word. */
9733 if (host_integerp (field_size_tree, 1))
9734 field_size_in_bits = tree_low_cst (field_size_tree, 1);
9735 else
9736 field_size_in_bits = BITS_PER_WORD;
9738 type_size_in_bits = simple_type_size_in_bits (type);
9739 type_align_in_bits = simple_type_align_in_bits (type);
9740 decl_align_in_bits = simple_decl_align_in_bits (decl);
9742 /* The GCC front-end doesn't make any attempt to keep track of the
9743 starting bit offset (relative to the start of the containing
9744 structure type) of the hypothetical "containing object" for a
9745 bit-field. Thus, when computing the byte offset value for the
9746 start of the "containing object" of a bit-field, we must deduce
9747 this information on our own. This can be rather tricky to do in
9748 some cases. For example, handling the following structure type
9749 definition when compiling for an i386/i486 target (which only
9750 aligns long long's to 32-bit boundaries) can be very tricky:
9752 struct S { int field1; long long field2:31; };
9754 Fortunately, there is a simple rule-of-thumb which can be used
9755 in such cases. When compiling for an i386/i486, GCC will
9756 allocate 8 bytes for the structure shown above. It decides to
9757 do this based upon one simple rule for bit-field allocation.
9758 GCC allocates each "containing object" for each bit-field at
9759 the first (i.e. lowest addressed) legitimate alignment boundary
9760 (based upon the required minimum alignment for the declared
9761 type of the field) which it can possibly use, subject to the
9762 condition that there is still enough available space remaining
9763 in the containing object (when allocated at the selected point)
9764 to fully accommodate all of the bits of the bit-field itself.
9766 This simple rule makes it obvious why GCC allocates 8 bytes for
9767 each object of the structure type shown above. When looking
9768 for a place to allocate the "containing object" for `field2',
9769 the compiler simply tries to allocate a 64-bit "containing
9770 object" at each successive 32-bit boundary (starting at zero)
9771 until it finds a place to allocate that 64- bit field such that
9772 at least 31 contiguous (and previously unallocated) bits remain
9773 within that selected 64 bit field. (As it turns out, for the
9774 example above, the compiler finds it is OK to allocate the
9775 "containing object" 64-bit field at bit-offset zero within the
9776 structure type.)
9778 Here we attempt to work backwards from the limited set of facts
9779 we're given, and we try to deduce from those facts, where GCC
9780 must have believed that the containing object started (within
9781 the structure type). The value we deduce is then used (by the
9782 callers of this routine) to generate DW_AT_location and
9783 DW_AT_bit_offset attributes for fields (both bit-fields and, in
9784 the case of DW_AT_location, regular fields as well). */
9786 /* Figure out the bit-distance from the start of the structure to
9787 the "deepest" bit of the bit-field. */
9788 deepest_bitpos = bitpos_int + field_size_in_bits;
9790 /* This is the tricky part. Use some fancy footwork to deduce
9791 where the lowest addressed bit of the containing object must
9792 be. */
9793 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9795 /* Round up to type_align by default. This works best for
9796 bitfields. */
9797 object_offset_in_bits
9798 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
9800 if (object_offset_in_bits > bitpos_int)
9802 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9804 /* Round up to decl_align instead. */
9805 object_offset_in_bits
9806 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
9809 else
9810 #endif
9811 object_offset_in_bits = bitpos_int;
9813 return object_offset_in_bits / BITS_PER_UNIT;
9816 /* The following routines define various Dwarf attributes and any data
9817 associated with them. */
9819 /* Add a location description attribute value to a DIE.
9821 This emits location attributes suitable for whole variables and
9822 whole parameters. Note that the location attributes for struct fields are
9823 generated by the routine `data_member_location_attribute' below. */
9825 static inline void
9826 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
9827 dw_loc_descr_ref descr)
9829 if (descr != 0)
9830 add_AT_loc (die, attr_kind, descr);
9833 /* Attach the specialized form of location attribute used for data members of
9834 struct and union types. In the special case of a FIELD_DECL node which
9835 represents a bit-field, the "offset" part of this special location
9836 descriptor must indicate the distance in bytes from the lowest-addressed
9837 byte of the containing struct or union type to the lowest-addressed byte of
9838 the "containing object" for the bit-field. (See the `field_byte_offset'
9839 function above).
9841 For any given bit-field, the "containing object" is a hypothetical object
9842 (of some integral or enum type) within which the given bit-field lives. The
9843 type of this hypothetical "containing object" is always the same as the
9844 declared type of the individual bit-field itself (for GCC anyway... the
9845 DWARF spec doesn't actually mandate this). Note that it is the size (in
9846 bytes) of the hypothetical "containing object" which will be given in the
9847 DW_AT_byte_size attribute for this bit-field. (See the
9848 `byte_size_attribute' function below.) It is also used when calculating the
9849 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
9850 function below.) */
9852 static void
9853 add_data_member_location_attribute (dw_die_ref die, tree decl)
9855 HOST_WIDE_INT offset;
9856 dw_loc_descr_ref loc_descr = 0;
9858 if (TREE_CODE (decl) == TREE_BINFO)
9860 /* We're working on the TAG_inheritance for a base class. */
9861 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
9863 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
9864 aren't at a fixed offset from all (sub)objects of the same
9865 type. We need to extract the appropriate offset from our
9866 vtable. The following dwarf expression means
9868 BaseAddr = ObAddr + *((*ObAddr) - Offset)
9870 This is specific to the V3 ABI, of course. */
9872 dw_loc_descr_ref tmp;
9874 /* Make a copy of the object address. */
9875 tmp = new_loc_descr (DW_OP_dup, 0, 0);
9876 add_loc_descr (&loc_descr, tmp);
9878 /* Extract the vtable address. */
9879 tmp = new_loc_descr (DW_OP_deref, 0, 0);
9880 add_loc_descr (&loc_descr, tmp);
9882 /* Calculate the address of the offset. */
9883 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
9884 gcc_assert (offset < 0);
9886 tmp = int_loc_descriptor (-offset);
9887 add_loc_descr (&loc_descr, tmp);
9888 tmp = new_loc_descr (DW_OP_minus, 0, 0);
9889 add_loc_descr (&loc_descr, tmp);
9891 /* Extract the offset. */
9892 tmp = new_loc_descr (DW_OP_deref, 0, 0);
9893 add_loc_descr (&loc_descr, tmp);
9895 /* Add it to the object address. */
9896 tmp = new_loc_descr (DW_OP_plus, 0, 0);
9897 add_loc_descr (&loc_descr, tmp);
9899 else
9900 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
9902 else
9903 offset = field_byte_offset (decl);
9905 if (! loc_descr)
9907 enum dwarf_location_atom op;
9909 /* The DWARF2 standard says that we should assume that the structure
9910 address is already on the stack, so we can specify a structure field
9911 address by using DW_OP_plus_uconst. */
9913 #ifdef MIPS_DEBUGGING_INFO
9914 /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
9915 operator correctly. It works only if we leave the offset on the
9916 stack. */
9917 op = DW_OP_constu;
9918 #else
9919 op = DW_OP_plus_uconst;
9920 #endif
9922 loc_descr = new_loc_descr (op, offset, 0);
9925 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
9928 /* Writes integer values to dw_vec_const array. */
9930 static void
9931 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
9933 while (size != 0)
9935 *dest++ = val & 0xff;
9936 val >>= 8;
9937 --size;
9941 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
9943 static HOST_WIDE_INT
9944 extract_int (const unsigned char *src, unsigned int size)
9946 HOST_WIDE_INT val = 0;
9948 src += size;
9949 while (size != 0)
9951 val <<= 8;
9952 val |= *--src & 0xff;
9953 --size;
9955 return val;
9958 /* Writes floating point values to dw_vec_const array. */
9960 static void
9961 insert_float (rtx rtl, unsigned char *array)
9963 REAL_VALUE_TYPE rv;
9964 long val[4];
9965 int i;
9967 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
9968 real_to_target (val, &rv, GET_MODE (rtl));
9970 /* real_to_target puts 32-bit pieces in each long. Pack them. */
9971 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
9973 insert_int (val[i], 4, array);
9974 array += 4;
9978 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
9979 does not have a "location" either in memory or in a register. These
9980 things can arise in GNU C when a constant is passed as an actual parameter
9981 to an inlined function. They can also arise in C++ where declared
9982 constants do not necessarily get memory "homes". */
9984 static void
9985 add_const_value_attribute (dw_die_ref die, rtx rtl)
9987 switch (GET_CODE (rtl))
9989 case CONST_INT:
9991 HOST_WIDE_INT val = INTVAL (rtl);
9993 if (val < 0)
9994 add_AT_int (die, DW_AT_const_value, val);
9995 else
9996 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
9998 break;
10000 case CONST_DOUBLE:
10001 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
10002 floating-point constant. A CONST_DOUBLE is used whenever the
10003 constant requires more than one word in order to be adequately
10004 represented. We output CONST_DOUBLEs as blocks. */
10006 enum machine_mode mode = GET_MODE (rtl);
10008 if (SCALAR_FLOAT_MODE_P (mode))
10010 unsigned int length = GET_MODE_SIZE (mode);
10011 unsigned char *array = ggc_alloc (length);
10013 insert_float (rtl, array);
10014 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
10016 else
10018 /* ??? We really should be using HOST_WIDE_INT throughout. */
10019 gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
10021 add_AT_long_long (die, DW_AT_const_value,
10022 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
10025 break;
10027 case CONST_VECTOR:
10029 enum machine_mode mode = GET_MODE (rtl);
10030 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
10031 unsigned int length = CONST_VECTOR_NUNITS (rtl);
10032 unsigned char *array = ggc_alloc (length * elt_size);
10033 unsigned int i;
10034 unsigned char *p;
10036 switch (GET_MODE_CLASS (mode))
10038 case MODE_VECTOR_INT:
10039 for (i = 0, p = array; i < length; i++, p += elt_size)
10041 rtx elt = CONST_VECTOR_ELT (rtl, i);
10042 HOST_WIDE_INT lo, hi;
10044 switch (GET_CODE (elt))
10046 case CONST_INT:
10047 lo = INTVAL (elt);
10048 hi = -(lo < 0);
10049 break;
10051 case CONST_DOUBLE:
10052 lo = CONST_DOUBLE_LOW (elt);
10053 hi = CONST_DOUBLE_HIGH (elt);
10054 break;
10056 default:
10057 gcc_unreachable ();
10060 if (elt_size <= sizeof (HOST_WIDE_INT))
10061 insert_int (lo, elt_size, p);
10062 else
10064 unsigned char *p0 = p;
10065 unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
10067 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
10068 if (WORDS_BIG_ENDIAN)
10070 p0 = p1;
10071 p1 = p;
10073 insert_int (lo, sizeof (HOST_WIDE_INT), p0);
10074 insert_int (hi, sizeof (HOST_WIDE_INT), p1);
10077 break;
10079 case MODE_VECTOR_FLOAT:
10080 for (i = 0, p = array; i < length; i++, p += elt_size)
10082 rtx elt = CONST_VECTOR_ELT (rtl, i);
10083 insert_float (elt, p);
10085 break;
10087 default:
10088 gcc_unreachable ();
10091 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
10093 break;
10095 case CONST_STRING:
10096 add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
10097 break;
10099 case SYMBOL_REF:
10100 case LABEL_REF:
10101 case CONST:
10102 add_AT_addr (die, DW_AT_const_value, rtl);
10103 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
10104 break;
10106 case PLUS:
10107 /* In cases where an inlined instance of an inline function is passed
10108 the address of an `auto' variable (which is local to the caller) we
10109 can get a situation where the DECL_RTL of the artificial local
10110 variable (for the inlining) which acts as a stand-in for the
10111 corresponding formal parameter (of the inline function) will look
10112 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
10113 exactly a compile-time constant expression, but it isn't the address
10114 of the (artificial) local variable either. Rather, it represents the
10115 *value* which the artificial local variable always has during its
10116 lifetime. We currently have no way to represent such quasi-constant
10117 values in Dwarf, so for now we just punt and generate nothing. */
10118 break;
10120 default:
10121 /* No other kinds of rtx should be possible here. */
10122 gcc_unreachable ();
10127 /* Determine whether the evaluation of EXPR references any variables
10128 or functions which aren't otherwise used (and therefore may not be
10129 output). */
10130 static tree
10131 reference_to_unused (tree * tp, int * walk_subtrees,
10132 void * data ATTRIBUTE_UNUSED)
10134 if (! EXPR_P (*tp) && ! GIMPLE_STMT_P (*tp) && ! CONSTANT_CLASS_P (*tp))
10135 *walk_subtrees = 0;
10137 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
10138 && ! TREE_ASM_WRITTEN (*tp))
10139 return *tp;
10140 else if (!flag_unit_at_a_time)
10141 return NULL_TREE;
10142 else if (!cgraph_global_info_ready
10143 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
10144 gcc_unreachable ();
10145 else if (DECL_P (*tp) && TREE_CODE (*tp) == VAR_DECL)
10147 struct varpool_node *node = varpool_node (*tp);
10148 if (!node->needed)
10149 return *tp;
10151 else if (DECL_P (*tp) && TREE_CODE (*tp) == FUNCTION_DECL
10152 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
10154 struct cgraph_node *node = cgraph_node (*tp);
10155 if (!node->output)
10156 return *tp;
10159 return NULL_TREE;
10162 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
10163 for use in a later add_const_value_attribute call. */
10165 static rtx
10166 rtl_for_decl_init (tree init, tree type)
10168 rtx rtl = NULL_RTX;
10170 /* If a variable is initialized with a string constant without embedded
10171 zeros, build CONST_STRING. */
10172 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
10174 tree enttype = TREE_TYPE (type);
10175 tree domain = TYPE_DOMAIN (type);
10176 enum machine_mode mode = TYPE_MODE (enttype);
10178 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
10179 && domain
10180 && integer_zerop (TYPE_MIN_VALUE (domain))
10181 && compare_tree_int (TYPE_MAX_VALUE (domain),
10182 TREE_STRING_LENGTH (init) - 1) == 0
10183 && ((size_t) TREE_STRING_LENGTH (init)
10184 == strlen (TREE_STRING_POINTER (init)) + 1))
10185 rtl = gen_rtx_CONST_STRING (VOIDmode,
10186 ggc_strdup (TREE_STRING_POINTER (init)));
10188 /* Other aggregates, and complex values, could be represented using
10189 CONCAT: FIXME! */
10190 else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
10192 /* Vectors only work if their mode is supported by the target.
10193 FIXME: generic vectors ought to work too. */
10194 else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
10196 /* If the initializer is something that we know will expand into an
10197 immediate RTL constant, expand it now. We must be careful not to
10198 reference variables which won't be output. */
10199 else if (initializer_constant_valid_p (init, type)
10200 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
10202 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
10204 /* If expand_expr returns a MEM, it wasn't immediate. */
10205 gcc_assert (!rtl || !MEM_P (rtl));
10208 return rtl;
10211 /* Generate RTL for the variable DECL to represent its location. */
10213 static rtx
10214 rtl_for_decl_location (tree decl)
10216 rtx rtl;
10218 /* Here we have to decide where we are going to say the parameter "lives"
10219 (as far as the debugger is concerned). We only have a couple of
10220 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
10222 DECL_RTL normally indicates where the parameter lives during most of the
10223 activation of the function. If optimization is enabled however, this
10224 could be either NULL or else a pseudo-reg. Both of those cases indicate
10225 that the parameter doesn't really live anywhere (as far as the code
10226 generation parts of GCC are concerned) during most of the function's
10227 activation. That will happen (for example) if the parameter is never
10228 referenced within the function.
10230 We could just generate a location descriptor here for all non-NULL
10231 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
10232 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
10233 where DECL_RTL is NULL or is a pseudo-reg.
10235 Note however that we can only get away with using DECL_INCOMING_RTL as
10236 a backup substitute for DECL_RTL in certain limited cases. In cases
10237 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
10238 we can be sure that the parameter was passed using the same type as it is
10239 declared to have within the function, and that its DECL_INCOMING_RTL
10240 points us to a place where a value of that type is passed.
10242 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
10243 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
10244 because in these cases DECL_INCOMING_RTL points us to a value of some
10245 type which is *different* from the type of the parameter itself. Thus,
10246 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
10247 such cases, the debugger would end up (for example) trying to fetch a
10248 `float' from a place which actually contains the first part of a
10249 `double'. That would lead to really incorrect and confusing
10250 output at debug-time.
10252 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
10253 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
10254 are a couple of exceptions however. On little-endian machines we can
10255 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
10256 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
10257 an integral type that is smaller than TREE_TYPE (decl). These cases arise
10258 when (on a little-endian machine) a non-prototyped function has a
10259 parameter declared to be of type `short' or `char'. In such cases,
10260 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
10261 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
10262 passed `int' value. If the debugger then uses that address to fetch
10263 a `short' or a `char' (on a little-endian machine) the result will be
10264 the correct data, so we allow for such exceptional cases below.
10266 Note that our goal here is to describe the place where the given formal
10267 parameter lives during most of the function's activation (i.e. between the
10268 end of the prologue and the start of the epilogue). We'll do that as best
10269 as we can. Note however that if the given formal parameter is modified
10270 sometime during the execution of the function, then a stack backtrace (at
10271 debug-time) will show the function as having been called with the *new*
10272 value rather than the value which was originally passed in. This happens
10273 rarely enough that it is not a major problem, but it *is* a problem, and
10274 I'd like to fix it.
10276 A future version of dwarf2out.c may generate two additional attributes for
10277 any given DW_TAG_formal_parameter DIE which will describe the "passed
10278 type" and the "passed location" for the given formal parameter in addition
10279 to the attributes we now generate to indicate the "declared type" and the
10280 "active location" for each parameter. This additional set of attributes
10281 could be used by debuggers for stack backtraces. Separately, note that
10282 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
10283 This happens (for example) for inlined-instances of inline function formal
10284 parameters which are never referenced. This really shouldn't be
10285 happening. All PARM_DECL nodes should get valid non-NULL
10286 DECL_INCOMING_RTL values. FIXME. */
10288 /* Use DECL_RTL as the "location" unless we find something better. */
10289 rtl = DECL_RTL_IF_SET (decl);
10291 /* When generating abstract instances, ignore everything except
10292 constants, symbols living in memory, and symbols living in
10293 fixed registers. */
10294 if (! reload_completed)
10296 if (rtl
10297 && (CONSTANT_P (rtl)
10298 || (MEM_P (rtl)
10299 && CONSTANT_P (XEXP (rtl, 0)))
10300 || (REG_P (rtl)
10301 && TREE_CODE (decl) == VAR_DECL
10302 && TREE_STATIC (decl))))
10304 rtl = targetm.delegitimize_address (rtl);
10305 return rtl;
10307 rtl = NULL_RTX;
10309 else if (TREE_CODE (decl) == PARM_DECL)
10311 if (rtl == NULL_RTX || is_pseudo_reg (rtl))
10313 tree declared_type = TREE_TYPE (decl);
10314 tree passed_type = DECL_ARG_TYPE (decl);
10315 enum machine_mode dmode = TYPE_MODE (declared_type);
10316 enum machine_mode pmode = TYPE_MODE (passed_type);
10318 /* This decl represents a formal parameter which was optimized out.
10319 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
10320 all cases where (rtl == NULL_RTX) just below. */
10321 if (dmode == pmode)
10322 rtl = DECL_INCOMING_RTL (decl);
10323 else if (SCALAR_INT_MODE_P (dmode)
10324 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
10325 && DECL_INCOMING_RTL (decl))
10327 rtx inc = DECL_INCOMING_RTL (decl);
10328 if (REG_P (inc))
10329 rtl = inc;
10330 else if (MEM_P (inc))
10332 if (BYTES_BIG_ENDIAN)
10333 rtl = adjust_address_nv (inc, dmode,
10334 GET_MODE_SIZE (pmode)
10335 - GET_MODE_SIZE (dmode));
10336 else
10337 rtl = inc;
10342 /* If the parm was passed in registers, but lives on the stack, then
10343 make a big endian correction if the mode of the type of the
10344 parameter is not the same as the mode of the rtl. */
10345 /* ??? This is the same series of checks that are made in dbxout.c before
10346 we reach the big endian correction code there. It isn't clear if all
10347 of these checks are necessary here, but keeping them all is the safe
10348 thing to do. */
10349 else if (MEM_P (rtl)
10350 && XEXP (rtl, 0) != const0_rtx
10351 && ! CONSTANT_P (XEXP (rtl, 0))
10352 /* Not passed in memory. */
10353 && !MEM_P (DECL_INCOMING_RTL (decl))
10354 /* Not passed by invisible reference. */
10355 && (!REG_P (XEXP (rtl, 0))
10356 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
10357 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
10358 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
10359 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
10360 #endif
10362 /* Big endian correction check. */
10363 && BYTES_BIG_ENDIAN
10364 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
10365 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
10366 < UNITS_PER_WORD))
10368 int offset = (UNITS_PER_WORD
10369 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
10371 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10372 plus_constant (XEXP (rtl, 0), offset));
10375 else if (TREE_CODE (decl) == VAR_DECL
10376 && rtl
10377 && MEM_P (rtl)
10378 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
10379 && BYTES_BIG_ENDIAN)
10381 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
10382 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
10384 /* If a variable is declared "register" yet is smaller than
10385 a register, then if we store the variable to memory, it
10386 looks like we're storing a register-sized value, when in
10387 fact we are not. We need to adjust the offset of the
10388 storage location to reflect the actual value's bytes,
10389 else gdb will not be able to display it. */
10390 if (rsize > dsize)
10391 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10392 plus_constant (XEXP (rtl, 0), rsize-dsize));
10395 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
10396 and will have been substituted directly into all expressions that use it.
10397 C does not have such a concept, but C++ and other languages do. */
10398 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
10399 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
10401 if (rtl)
10402 rtl = targetm.delegitimize_address (rtl);
10404 /* If we don't look past the constant pool, we risk emitting a
10405 reference to a constant pool entry that isn't referenced from
10406 code, and thus is not emitted. */
10407 if (rtl)
10408 rtl = avoid_constant_pool_reference (rtl);
10410 return rtl;
10413 /* We need to figure out what section we should use as the base for the
10414 address ranges where a given location is valid.
10415 1. If this particular DECL has a section associated with it, use that.
10416 2. If this function has a section associated with it, use that.
10417 3. Otherwise, use the text section.
10418 XXX: If you split a variable across multiple sections, we won't notice. */
10420 static const char *
10421 secname_for_decl (tree decl)
10423 const char *secname;
10425 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
10427 tree sectree = DECL_SECTION_NAME (decl);
10428 secname = TREE_STRING_POINTER (sectree);
10430 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
10432 tree sectree = DECL_SECTION_NAME (current_function_decl);
10433 secname = TREE_STRING_POINTER (sectree);
10435 else if (cfun && in_cold_section_p)
10436 secname = cfun->cold_section_label;
10437 else
10438 secname = text_section_label;
10440 return secname;
10443 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
10444 data attribute for a variable or a parameter. We generate the
10445 DW_AT_const_value attribute only in those cases where the given variable
10446 or parameter does not have a true "location" either in memory or in a
10447 register. This can happen (for example) when a constant is passed as an
10448 actual argument in a call to an inline function. (It's possible that
10449 these things can crop up in other ways also.) Note that one type of
10450 constant value which can be passed into an inlined function is a constant
10451 pointer. This can happen for example if an actual argument in an inlined
10452 function call evaluates to a compile-time constant address. */
10454 static void
10455 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
10456 enum dwarf_attribute attr)
10458 rtx rtl;
10459 dw_loc_descr_ref descr;
10460 var_loc_list *loc_list;
10461 struct var_loc_node *node;
10462 if (TREE_CODE (decl) == ERROR_MARK)
10463 return;
10465 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
10466 || TREE_CODE (decl) == RESULT_DECL);
10468 /* See if we possibly have multiple locations for this variable. */
10469 loc_list = lookup_decl_loc (decl);
10471 /* If it truly has multiple locations, the first and last node will
10472 differ. */
10473 if (loc_list && loc_list->first != loc_list->last)
10475 const char *endname, *secname;
10476 dw_loc_list_ref list;
10477 rtx varloc;
10479 /* Now that we know what section we are using for a base,
10480 actually construct the list of locations.
10481 The first location information is what is passed to the
10482 function that creates the location list, and the remaining
10483 locations just get added on to that list.
10484 Note that we only know the start address for a location
10485 (IE location changes), so to build the range, we use
10486 the range [current location start, next location start].
10487 This means we have to special case the last node, and generate
10488 a range of [last location start, end of function label]. */
10490 node = loc_list->first;
10491 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10492 secname = secname_for_decl (decl);
10494 list = new_loc_list (loc_descriptor (varloc),
10495 node->label, node->next->label, secname, 1);
10496 node = node->next;
10498 for (; node->next; node = node->next)
10499 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10501 /* The variable has a location between NODE->LABEL and
10502 NODE->NEXT->LABEL. */
10503 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10504 add_loc_descr_to_loc_list (&list, loc_descriptor (varloc),
10505 node->label, node->next->label, secname);
10508 /* If the variable has a location at the last label
10509 it keeps its location until the end of function. */
10510 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10512 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
10514 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10515 if (!current_function_decl)
10516 endname = text_end_label;
10517 else
10519 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
10520 current_function_funcdef_no);
10521 endname = ggc_strdup (label_id);
10523 add_loc_descr_to_loc_list (&list, loc_descriptor (varloc),
10524 node->label, endname, secname);
10527 /* Finally, add the location list to the DIE, and we are done. */
10528 add_AT_loc_list (die, attr, list);
10529 return;
10532 /* Try to get some constant RTL for this decl, and use that as the value of
10533 the location. */
10535 rtl = rtl_for_decl_location (decl);
10536 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
10538 add_const_value_attribute (die, rtl);
10539 return;
10542 /* If we have tried to generate the location otherwise, and it
10543 didn't work out (we wouldn't be here if we did), and we have a one entry
10544 location list, try generating a location from that. */
10545 if (loc_list && loc_list->first)
10547 node = loc_list->first;
10548 descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note));
10549 if (descr)
10551 add_AT_location_description (die, attr, descr);
10552 return;
10556 /* We couldn't get any rtl, so try directly generating the location
10557 description from the tree. */
10558 descr = loc_descriptor_from_tree (decl);
10559 if (descr)
10561 add_AT_location_description (die, attr, descr);
10562 return;
10564 /* None of that worked, so it must not really have a location;
10565 try adding a constant value attribute from the DECL_INITIAL. */
10566 tree_add_const_value_attribute (die, decl);
10569 /* If we don't have a copy of this variable in memory for some reason (such
10570 as a C++ member constant that doesn't have an out-of-line definition),
10571 we should tell the debugger about the constant value. */
10573 static void
10574 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
10576 tree init = DECL_INITIAL (decl);
10577 tree type = TREE_TYPE (decl);
10578 rtx rtl;
10580 if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
10581 /* OK */;
10582 else
10583 return;
10585 rtl = rtl_for_decl_init (init, type);
10586 if (rtl)
10587 add_const_value_attribute (var_die, rtl);
10590 /* Convert the CFI instructions for the current function into a
10591 location list. This is used for DW_AT_frame_base when we targeting
10592 a dwarf2 consumer that does not support the dwarf3
10593 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
10594 expressions. */
10596 static dw_loc_list_ref
10597 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
10599 dw_fde_ref fde;
10600 dw_loc_list_ref list, *list_tail;
10601 dw_cfi_ref cfi;
10602 dw_cfa_location last_cfa, next_cfa;
10603 const char *start_label, *last_label, *section;
10605 fde = &fde_table[fde_table_in_use - 1];
10607 section = secname_for_decl (current_function_decl);
10608 list_tail = &list;
10609 list = NULL;
10611 next_cfa.reg = INVALID_REGNUM;
10612 next_cfa.offset = 0;
10613 next_cfa.indirect = 0;
10614 next_cfa.base_offset = 0;
10616 start_label = fde->dw_fde_begin;
10618 /* ??? Bald assumption that the CIE opcode list does not contain
10619 advance opcodes. */
10620 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
10621 lookup_cfa_1 (cfi, &next_cfa);
10623 last_cfa = next_cfa;
10624 last_label = start_label;
10626 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
10627 switch (cfi->dw_cfi_opc)
10629 case DW_CFA_set_loc:
10630 case DW_CFA_advance_loc1:
10631 case DW_CFA_advance_loc2:
10632 case DW_CFA_advance_loc4:
10633 if (!cfa_equal_p (&last_cfa, &next_cfa))
10635 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
10636 start_label, last_label, section,
10637 list == NULL);
10639 list_tail = &(*list_tail)->dw_loc_next;
10640 last_cfa = next_cfa;
10641 start_label = last_label;
10643 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
10644 break;
10646 case DW_CFA_advance_loc:
10647 /* The encoding is complex enough that we should never emit this. */
10648 case DW_CFA_remember_state:
10649 case DW_CFA_restore_state:
10650 /* We don't handle these two in this function. It would be possible
10651 if it were to be required. */
10652 gcc_unreachable ();
10654 default:
10655 lookup_cfa_1 (cfi, &next_cfa);
10656 break;
10659 if (!cfa_equal_p (&last_cfa, &next_cfa))
10661 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
10662 start_label, last_label, section,
10663 list == NULL);
10664 list_tail = &(*list_tail)->dw_loc_next;
10665 start_label = last_label;
10667 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
10668 start_label, fde->dw_fde_end, section,
10669 list == NULL);
10671 return list;
10674 /* Compute a displacement from the "steady-state frame pointer" to the
10675 frame base (often the same as the CFA), and store it in
10676 frame_pointer_fb_offset. OFFSET is added to the displacement
10677 before the latter is negated. */
10679 static void
10680 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
10682 rtx reg, elim;
10684 #ifdef FRAME_POINTER_CFA_OFFSET
10685 reg = frame_pointer_rtx;
10686 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
10687 #else
10688 reg = arg_pointer_rtx;
10689 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
10690 #endif
10692 elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10693 if (GET_CODE (elim) == PLUS)
10695 offset += INTVAL (XEXP (elim, 1));
10696 elim = XEXP (elim, 0);
10698 gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
10699 : stack_pointer_rtx));
10701 frame_pointer_fb_offset = -offset;
10704 /* Generate a DW_AT_name attribute given some string value to be included as
10705 the value of the attribute. */
10707 static void
10708 add_name_attribute (dw_die_ref die, const char *name_string)
10710 if (name_string != NULL && *name_string != 0)
10712 if (demangle_name_func)
10713 name_string = (*demangle_name_func) (name_string);
10715 add_AT_string (die, DW_AT_name, name_string);
10719 /* Generate a DW_AT_comp_dir attribute for DIE. */
10721 static void
10722 add_comp_dir_attribute (dw_die_ref die)
10724 const char *wd = get_src_pwd ();
10725 if (wd != NULL)
10726 add_AT_string (die, DW_AT_comp_dir, wd);
10729 /* Given a tree node describing an array bound (either lower or upper) output
10730 a representation for that bound. */
10732 static void
10733 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
10735 switch (TREE_CODE (bound))
10737 case ERROR_MARK:
10738 return;
10740 /* All fixed-bounds are represented by INTEGER_CST nodes. */
10741 case INTEGER_CST:
10742 if (! host_integerp (bound, 0)
10743 || (bound_attr == DW_AT_lower_bound
10744 && (((is_c_family () || is_java ()) && integer_zerop (bound))
10745 || (is_fortran () && integer_onep (bound)))))
10746 /* Use the default. */
10748 else
10749 add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
10750 break;
10752 case CONVERT_EXPR:
10753 case NOP_EXPR:
10754 case NON_LVALUE_EXPR:
10755 case VIEW_CONVERT_EXPR:
10756 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
10757 break;
10759 case SAVE_EXPR:
10760 break;
10762 case VAR_DECL:
10763 case PARM_DECL:
10764 case RESULT_DECL:
10766 dw_die_ref decl_die = lookup_decl_die (bound);
10768 /* ??? Can this happen, or should the variable have been bound
10769 first? Probably it can, since I imagine that we try to create
10770 the types of parameters in the order in which they exist in
10771 the list, and won't have created a forward reference to a
10772 later parameter. */
10773 if (decl_die != NULL)
10774 add_AT_die_ref (subrange_die, bound_attr, decl_die);
10775 break;
10778 default:
10780 /* Otherwise try to create a stack operation procedure to
10781 evaluate the value of the array bound. */
10783 dw_die_ref ctx, decl_die;
10784 dw_loc_descr_ref loc;
10786 loc = loc_descriptor_from_tree (bound);
10787 if (loc == NULL)
10788 break;
10790 if (current_function_decl == 0)
10791 ctx = comp_unit_die;
10792 else
10793 ctx = lookup_decl_die (current_function_decl);
10795 decl_die = new_die (DW_TAG_variable, ctx, bound);
10796 add_AT_flag (decl_die, DW_AT_artificial, 1);
10797 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
10798 add_AT_loc (decl_die, DW_AT_location, loc);
10800 add_AT_die_ref (subrange_die, bound_attr, decl_die);
10801 break;
10806 /* Note that the block of subscript information for an array type also
10807 includes information about the element type of type given array type. */
10809 static void
10810 add_subscript_info (dw_die_ref type_die, tree type)
10812 #ifndef MIPS_DEBUGGING_INFO
10813 unsigned dimension_number;
10814 #endif
10815 tree lower, upper;
10816 dw_die_ref subrange_die;
10818 /* The GNU compilers represent multidimensional array types as sequences of
10819 one dimensional array types whose element types are themselves array
10820 types. Here we squish that down, so that each multidimensional array
10821 type gets only one array_type DIE in the Dwarf debugging info. The draft
10822 Dwarf specification say that we are allowed to do this kind of
10823 compression in C (because there is no difference between an array or
10824 arrays and a multidimensional array in C) but for other source languages
10825 (e.g. Ada) we probably shouldn't do this. */
10827 /* ??? The SGI dwarf reader fails for multidimensional arrays with a
10828 const enum type. E.g. const enum machine_mode insn_operand_mode[2][10].
10829 We work around this by disabling this feature. See also
10830 gen_array_type_die. */
10831 #ifndef MIPS_DEBUGGING_INFO
10832 for (dimension_number = 0;
10833 TREE_CODE (type) == ARRAY_TYPE;
10834 type = TREE_TYPE (type), dimension_number++)
10835 #endif
10837 tree domain = TYPE_DOMAIN (type);
10839 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
10840 and (in GNU C only) variable bounds. Handle all three forms
10841 here. */
10842 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
10843 if (domain)
10845 /* We have an array type with specified bounds. */
10846 lower = TYPE_MIN_VALUE (domain);
10847 upper = TYPE_MAX_VALUE (domain);
10849 /* Define the index type. */
10850 if (TREE_TYPE (domain))
10852 /* ??? This is probably an Ada unnamed subrange type. Ignore the
10853 TREE_TYPE field. We can't emit debug info for this
10854 because it is an unnamed integral type. */
10855 if (TREE_CODE (domain) == INTEGER_TYPE
10856 && TYPE_NAME (domain) == NULL_TREE
10857 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
10858 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
10860 else
10861 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
10862 type_die);
10865 /* ??? If upper is NULL, the array has unspecified length,
10866 but it does have a lower bound. This happens with Fortran
10867 dimension arr(N:*)
10868 Since the debugger is definitely going to need to know N
10869 to produce useful results, go ahead and output the lower
10870 bound solo, and hope the debugger can cope. */
10872 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
10873 if (upper)
10874 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
10877 /* Otherwise we have an array type with an unspecified length. The
10878 DWARF-2 spec does not say how to handle this; let's just leave out the
10879 bounds. */
10883 static void
10884 add_byte_size_attribute (dw_die_ref die, tree tree_node)
10886 unsigned size;
10888 switch (TREE_CODE (tree_node))
10890 case ERROR_MARK:
10891 size = 0;
10892 break;
10893 case ENUMERAL_TYPE:
10894 case RECORD_TYPE:
10895 case UNION_TYPE:
10896 case QUAL_UNION_TYPE:
10897 size = int_size_in_bytes (tree_node);
10898 break;
10899 case FIELD_DECL:
10900 /* For a data member of a struct or union, the DW_AT_byte_size is
10901 generally given as the number of bytes normally allocated for an
10902 object of the *declared* type of the member itself. This is true
10903 even for bit-fields. */
10904 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
10905 break;
10906 default:
10907 gcc_unreachable ();
10910 /* Note that `size' might be -1 when we get to this point. If it is, that
10911 indicates that the byte size of the entity in question is variable. We
10912 have no good way of expressing this fact in Dwarf at the present time,
10913 so just let the -1 pass on through. */
10914 add_AT_unsigned (die, DW_AT_byte_size, size);
10917 /* For a FIELD_DECL node which represents a bit-field, output an attribute
10918 which specifies the distance in bits from the highest order bit of the
10919 "containing object" for the bit-field to the highest order bit of the
10920 bit-field itself.
10922 For any given bit-field, the "containing object" is a hypothetical object
10923 (of some integral or enum type) within which the given bit-field lives. The
10924 type of this hypothetical "containing object" is always the same as the
10925 declared type of the individual bit-field itself. The determination of the
10926 exact location of the "containing object" for a bit-field is rather
10927 complicated. It's handled by the `field_byte_offset' function (above).
10929 Note that it is the size (in bytes) of the hypothetical "containing object"
10930 which will be given in the DW_AT_byte_size attribute for this bit-field.
10931 (See `byte_size_attribute' above). */
10933 static inline void
10934 add_bit_offset_attribute (dw_die_ref die, tree decl)
10936 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
10937 tree type = DECL_BIT_FIELD_TYPE (decl);
10938 HOST_WIDE_INT bitpos_int;
10939 HOST_WIDE_INT highest_order_object_bit_offset;
10940 HOST_WIDE_INT highest_order_field_bit_offset;
10941 HOST_WIDE_INT unsigned bit_offset;
10943 /* Must be a field and a bit field. */
10944 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
10946 /* We can't yet handle bit-fields whose offsets are variable, so if we
10947 encounter such things, just return without generating any attribute
10948 whatsoever. Likewise for variable or too large size. */
10949 if (! host_integerp (bit_position (decl), 0)
10950 || ! host_integerp (DECL_SIZE (decl), 1))
10951 return;
10953 bitpos_int = int_bit_position (decl);
10955 /* Note that the bit offset is always the distance (in bits) from the
10956 highest-order bit of the "containing object" to the highest-order bit of
10957 the bit-field itself. Since the "high-order end" of any object or field
10958 is different on big-endian and little-endian machines, the computation
10959 below must take account of these differences. */
10960 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
10961 highest_order_field_bit_offset = bitpos_int;
10963 if (! BYTES_BIG_ENDIAN)
10965 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
10966 highest_order_object_bit_offset += simple_type_size_in_bits (type);
10969 bit_offset
10970 = (! BYTES_BIG_ENDIAN
10971 ? highest_order_object_bit_offset - highest_order_field_bit_offset
10972 : highest_order_field_bit_offset - highest_order_object_bit_offset);
10974 add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
10977 /* For a FIELD_DECL node which represents a bit field, output an attribute
10978 which specifies the length in bits of the given field. */
10980 static inline void
10981 add_bit_size_attribute (dw_die_ref die, tree decl)
10983 /* Must be a field and a bit field. */
10984 gcc_assert (TREE_CODE (decl) == FIELD_DECL
10985 && DECL_BIT_FIELD_TYPE (decl));
10987 if (host_integerp (DECL_SIZE (decl), 1))
10988 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
10991 /* If the compiled language is ANSI C, then add a 'prototyped'
10992 attribute, if arg types are given for the parameters of a function. */
10994 static inline void
10995 add_prototyped_attribute (dw_die_ref die, tree func_type)
10997 if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
10998 && TYPE_ARG_TYPES (func_type) != NULL)
10999 add_AT_flag (die, DW_AT_prototyped, 1);
11002 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
11003 by looking in either the type declaration or object declaration
11004 equate table. */
11006 static inline void
11007 add_abstract_origin_attribute (dw_die_ref die, tree origin)
11009 dw_die_ref origin_die = NULL;
11011 if (TREE_CODE (origin) != FUNCTION_DECL)
11013 /* We may have gotten separated from the block for the inlined
11014 function, if we're in an exception handler or some such; make
11015 sure that the abstract function has been written out.
11017 Doing this for nested functions is wrong, however; functions are
11018 distinct units, and our context might not even be inline. */
11019 tree fn = origin;
11021 if (TYPE_P (fn))
11022 fn = TYPE_STUB_DECL (fn);
11024 fn = decl_function_context (fn);
11025 if (fn)
11026 dwarf2out_abstract_function (fn);
11029 if (DECL_P (origin))
11030 origin_die = lookup_decl_die (origin);
11031 else if (TYPE_P (origin))
11032 origin_die = lookup_type_die (origin);
11034 /* XXX: Functions that are never lowered don't always have correct block
11035 trees (in the case of java, they simply have no block tree, in some other
11036 languages). For these functions, there is nothing we can really do to
11037 output correct debug info for inlined functions in all cases. Rather
11038 than die, we'll just produce deficient debug info now, in that we will
11039 have variables without a proper abstract origin. In the future, when all
11040 functions are lowered, we should re-add a gcc_assert (origin_die)
11041 here. */
11043 if (origin_die)
11044 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
11047 /* We do not currently support the pure_virtual attribute. */
11049 static inline void
11050 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
11052 if (DECL_VINDEX (func_decl))
11054 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
11056 if (host_integerp (DECL_VINDEX (func_decl), 0))
11057 add_AT_loc (die, DW_AT_vtable_elem_location,
11058 new_loc_descr (DW_OP_constu,
11059 tree_low_cst (DECL_VINDEX (func_decl), 0),
11060 0));
11062 /* GNU extension: Record what type this method came from originally. */
11063 if (debug_info_level > DINFO_LEVEL_TERSE)
11064 add_AT_die_ref (die, DW_AT_containing_type,
11065 lookup_type_die (DECL_CONTEXT (func_decl)));
11069 /* Add source coordinate attributes for the given decl. */
11071 static void
11072 add_src_coords_attributes (dw_die_ref die, tree decl)
11074 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11076 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
11077 add_AT_unsigned (die, DW_AT_decl_line, s.line);
11080 /* Add a DW_AT_name attribute and source coordinate attribute for the
11081 given decl, but only if it actually has a name. */
11083 static void
11084 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
11086 tree decl_name;
11088 decl_name = DECL_NAME (decl);
11089 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
11091 add_name_attribute (die, dwarf2_name (decl, 0));
11092 if (! DECL_ARTIFICIAL (decl))
11093 add_src_coords_attributes (die, decl);
11095 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
11096 && TREE_PUBLIC (decl)
11097 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
11098 && !DECL_ABSTRACT (decl)
11099 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl)))
11100 add_AT_string (die, DW_AT_MIPS_linkage_name,
11101 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
11104 #ifdef VMS_DEBUGGING_INFO
11105 /* Get the function's name, as described by its RTL. This may be different
11106 from the DECL_NAME name used in the source file. */
11107 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
11109 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
11110 XEXP (DECL_RTL (decl), 0));
11111 VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
11113 #endif
11116 /* Push a new declaration scope. */
11118 static void
11119 push_decl_scope (tree scope)
11121 VEC_safe_push (tree, gc, decl_scope_table, scope);
11124 /* Pop a declaration scope. */
11126 static inline void
11127 pop_decl_scope (void)
11129 VEC_pop (tree, decl_scope_table);
11132 /* Return the DIE for the scope that immediately contains this type.
11133 Non-named types get global scope. Named types nested in other
11134 types get their containing scope if it's open, or global scope
11135 otherwise. All other types (i.e. function-local named types) get
11136 the current active scope. */
11138 static dw_die_ref
11139 scope_die_for (tree t, dw_die_ref context_die)
11141 dw_die_ref scope_die = NULL;
11142 tree containing_scope;
11143 int i;
11145 /* Non-types always go in the current scope. */
11146 gcc_assert (TYPE_P (t));
11148 containing_scope = TYPE_CONTEXT (t);
11150 /* Use the containing namespace if it was passed in (for a declaration). */
11151 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
11153 if (context_die == lookup_decl_die (containing_scope))
11154 /* OK */;
11155 else
11156 containing_scope = NULL_TREE;
11159 /* Ignore function type "scopes" from the C frontend. They mean that
11160 a tagged type is local to a parmlist of a function declarator, but
11161 that isn't useful to DWARF. */
11162 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
11163 containing_scope = NULL_TREE;
11165 if (containing_scope == NULL_TREE)
11166 scope_die = comp_unit_die;
11167 else if (TYPE_P (containing_scope))
11169 /* For types, we can just look up the appropriate DIE. But
11170 first we check to see if we're in the middle of emitting it
11171 so we know where the new DIE should go. */
11172 for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
11173 if (VEC_index (tree, decl_scope_table, i) == containing_scope)
11174 break;
11176 if (i < 0)
11178 gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
11179 || TREE_ASM_WRITTEN (containing_scope));
11181 /* If none of the current dies are suitable, we get file scope. */
11182 scope_die = comp_unit_die;
11184 else
11185 scope_die = lookup_type_die (containing_scope);
11187 else
11188 scope_die = context_die;
11190 return scope_die;
11193 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
11195 static inline int
11196 local_scope_p (dw_die_ref context_die)
11198 for (; context_die; context_die = context_die->die_parent)
11199 if (context_die->die_tag == DW_TAG_inlined_subroutine
11200 || context_die->die_tag == DW_TAG_subprogram)
11201 return 1;
11203 return 0;
11206 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
11207 whether or not to treat a DIE in this context as a declaration. */
11209 static inline int
11210 class_or_namespace_scope_p (dw_die_ref context_die)
11212 return (context_die
11213 && (context_die->die_tag == DW_TAG_structure_type
11214 || context_die->die_tag == DW_TAG_union_type
11215 || context_die->die_tag == DW_TAG_namespace));
11218 /* Many forms of DIEs require a "type description" attribute. This
11219 routine locates the proper "type descriptor" die for the type given
11220 by 'type', and adds a DW_AT_type attribute below the given die. */
11222 static void
11223 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
11224 int decl_volatile, dw_die_ref context_die)
11226 enum tree_code code = TREE_CODE (type);
11227 dw_die_ref type_die = NULL;
11229 /* ??? If this type is an unnamed subrange type of an integral or
11230 floating-point type, use the inner type. This is because we have no
11231 support for unnamed types in base_type_die. This can happen if this is
11232 an Ada subrange type. Correct solution is emit a subrange type die. */
11233 if ((code == INTEGER_TYPE || code == REAL_TYPE)
11234 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
11235 type = TREE_TYPE (type), code = TREE_CODE (type);
11237 if (code == ERROR_MARK
11238 /* Handle a special case. For functions whose return type is void, we
11239 generate *no* type attribute. (Note that no object may have type
11240 `void', so this only applies to function return types). */
11241 || code == VOID_TYPE)
11242 return;
11244 type_die = modified_type_die (type,
11245 decl_const || TYPE_READONLY (type),
11246 decl_volatile || TYPE_VOLATILE (type),
11247 context_die);
11249 if (type_die != NULL)
11250 add_AT_die_ref (object_die, DW_AT_type, type_die);
11253 /* Given an object die, add the calling convention attribute for the
11254 function call type. */
11255 static void
11256 add_calling_convention_attribute (dw_die_ref subr_die, tree type)
11258 enum dwarf_calling_convention value = DW_CC_normal;
11260 value = targetm.dwarf_calling_convention (type);
11262 /* Only add the attribute if the backend requests it, and
11263 is not DW_CC_normal. */
11264 if (value && (value != DW_CC_normal))
11265 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
11268 /* Given a tree pointer to a struct, class, union, or enum type node, return
11269 a pointer to the (string) tag name for the given type, or zero if the type
11270 was declared without a tag. */
11272 static const char *
11273 type_tag (tree type)
11275 const char *name = 0;
11277 if (TYPE_NAME (type) != 0)
11279 tree t = 0;
11281 /* Find the IDENTIFIER_NODE for the type name. */
11282 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
11283 t = TYPE_NAME (type);
11285 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
11286 a TYPE_DECL node, regardless of whether or not a `typedef' was
11287 involved. */
11288 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11289 && ! DECL_IGNORED_P (TYPE_NAME (type)))
11291 /* We want to be extra verbose. Don't call dwarf_name if
11292 DECL_NAME isn't set. The default hook for decl_printable_name
11293 doesn't like that, and in this context it's correct to return
11294 0, instead of "<anonymous>" or the like. */
11295 if (DECL_NAME (TYPE_NAME (type)))
11296 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
11299 /* Now get the name as a string, or invent one. */
11300 if (!name && t != 0)
11301 name = IDENTIFIER_POINTER (t);
11304 return (name == 0 || *name == '\0') ? 0 : name;
11307 /* Return the type associated with a data member, make a special check
11308 for bit field types. */
11310 static inline tree
11311 member_declared_type (tree member)
11313 return (DECL_BIT_FIELD_TYPE (member)
11314 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
11317 /* Get the decl's label, as described by its RTL. This may be different
11318 from the DECL_NAME name used in the source file. */
11320 #if 0
11321 static const char *
11322 decl_start_label (tree decl)
11324 rtx x;
11325 const char *fnname;
11327 x = DECL_RTL (decl);
11328 gcc_assert (MEM_P (x));
11330 x = XEXP (x, 0);
11331 gcc_assert (GET_CODE (x) == SYMBOL_REF);
11333 fnname = XSTR (x, 0);
11334 return fnname;
11336 #endif
11338 /* These routines generate the internal representation of the DIE's for
11339 the compilation unit. Debugging information is collected by walking
11340 the declaration trees passed in from dwarf2out_decl(). */
11342 static void
11343 gen_array_type_die (tree type, dw_die_ref context_die)
11345 dw_die_ref scope_die = scope_die_for (type, context_die);
11346 dw_die_ref array_die;
11347 tree element_type;
11349 /* ??? The SGI dwarf reader fails for array of array of enum types unless
11350 the inner array type comes before the outer array type. Thus we must
11351 call gen_type_die before we call new_die. See below also. */
11352 #ifdef MIPS_DEBUGGING_INFO
11353 gen_type_die (TREE_TYPE (type), context_die);
11354 #endif
11356 array_die = new_die (DW_TAG_array_type, scope_die, type);
11357 add_name_attribute (array_die, type_tag (type));
11358 equate_type_number_to_die (type, array_die);
11360 if (TREE_CODE (type) == VECTOR_TYPE)
11362 /* The frontend feeds us a representation for the vector as a struct
11363 containing an array. Pull out the array type. */
11364 type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
11365 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
11368 #if 0
11369 /* We default the array ordering. SDB will probably do
11370 the right things even if DW_AT_ordering is not present. It's not even
11371 an issue until we start to get into multidimensional arrays anyway. If
11372 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
11373 then we'll have to put the DW_AT_ordering attribute back in. (But if
11374 and when we find out that we need to put these in, we will only do so
11375 for multidimensional arrays. */
11376 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
11377 #endif
11379 #ifdef MIPS_DEBUGGING_INFO
11380 /* The SGI compilers handle arrays of unknown bound by setting
11381 AT_declaration and not emitting any subrange DIEs. */
11382 if (! TYPE_DOMAIN (type))
11383 add_AT_flag (array_die, DW_AT_declaration, 1);
11384 else
11385 #endif
11386 add_subscript_info (array_die, type);
11388 /* Add representation of the type of the elements of this array type. */
11389 element_type = TREE_TYPE (type);
11391 /* ??? The SGI dwarf reader fails for multidimensional arrays with a
11392 const enum type. E.g. const enum machine_mode insn_operand_mode[2][10].
11393 We work around this by disabling this feature. See also
11394 add_subscript_info. */
11395 #ifndef MIPS_DEBUGGING_INFO
11396 while (TREE_CODE (element_type) == ARRAY_TYPE)
11397 element_type = TREE_TYPE (element_type);
11399 gen_type_die (element_type, context_die);
11400 #endif
11402 add_type_attribute (array_die, element_type, 0, 0, context_die);
11404 if (get_AT (array_die, DW_AT_name))
11405 add_pubtype (type, array_die);
11408 #if 0
11409 static void
11410 gen_entry_point_die (tree decl, dw_die_ref context_die)
11412 tree origin = decl_ultimate_origin (decl);
11413 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
11415 if (origin != NULL)
11416 add_abstract_origin_attribute (decl_die, origin);
11417 else
11419 add_name_and_src_coords_attributes (decl_die, decl);
11420 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
11421 0, 0, context_die);
11424 if (DECL_ABSTRACT (decl))
11425 equate_decl_number_to_die (decl, decl_die);
11426 else
11427 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
11429 #endif
11431 /* Walk through the list of incomplete types again, trying once more to
11432 emit full debugging info for them. */
11434 static void
11435 retry_incomplete_types (void)
11437 int i;
11439 for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
11440 gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
11443 /* Generate a DIE to represent an inlined instance of an enumeration type. */
11445 static void
11446 gen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
11448 dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
11450 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11451 be incomplete and such types are not marked. */
11452 add_abstract_origin_attribute (type_die, type);
11455 /* Generate a DIE to represent an inlined instance of a structure type. */
11457 static void
11458 gen_inlined_structure_type_die (tree type, dw_die_ref context_die)
11460 dw_die_ref type_die = new_die (DW_TAG_structure_type, context_die, type);
11462 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11463 be incomplete and such types are not marked. */
11464 add_abstract_origin_attribute (type_die, type);
11467 /* Generate a DIE to represent an inlined instance of a union type. */
11469 static void
11470 gen_inlined_union_type_die (tree type, dw_die_ref context_die)
11472 dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
11474 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11475 be incomplete and such types are not marked. */
11476 add_abstract_origin_attribute (type_die, type);
11479 /* Generate a DIE to represent an enumeration type. Note that these DIEs
11480 include all of the information about the enumeration values also. Each
11481 enumerated type name/value is listed as a child of the enumerated type
11482 DIE. */
11484 static dw_die_ref
11485 gen_enumeration_type_die (tree type, dw_die_ref context_die)
11487 dw_die_ref type_die = lookup_type_die (type);
11489 if (type_die == NULL)
11491 type_die = new_die (DW_TAG_enumeration_type,
11492 scope_die_for (type, context_die), type);
11493 equate_type_number_to_die (type, type_die);
11494 add_name_attribute (type_die, type_tag (type));
11496 else if (! TYPE_SIZE (type))
11497 return type_die;
11498 else
11499 remove_AT (type_die, DW_AT_declaration);
11501 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
11502 given enum type is incomplete, do not generate the DW_AT_byte_size
11503 attribute or the DW_AT_element_list attribute. */
11504 if (TYPE_SIZE (type))
11506 tree link;
11508 TREE_ASM_WRITTEN (type) = 1;
11509 add_byte_size_attribute (type_die, type);
11510 if (TYPE_STUB_DECL (type) != NULL_TREE)
11511 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
11513 /* If the first reference to this type was as the return type of an
11514 inline function, then it may not have a parent. Fix this now. */
11515 if (type_die->die_parent == NULL)
11516 add_child_die (scope_die_for (type, context_die), type_die);
11518 for (link = TYPE_VALUES (type);
11519 link != NULL; link = TREE_CHAIN (link))
11521 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
11522 tree value = TREE_VALUE (link);
11524 add_name_attribute (enum_die,
11525 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
11527 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
11528 /* DWARF2 does not provide a way of indicating whether or
11529 not enumeration constants are signed or unsigned. GDB
11530 always assumes the values are signed, so we output all
11531 values as if they were signed. That means that
11532 enumeration constants with very large unsigned values
11533 will appear to have negative values in the debugger. */
11534 add_AT_int (enum_die, DW_AT_const_value,
11535 tree_low_cst (value, tree_int_cst_sgn (value) > 0));
11538 else
11539 add_AT_flag (type_die, DW_AT_declaration, 1);
11541 if (get_AT (type_die, DW_AT_name))
11542 add_pubtype (type, type_die);
11544 return type_die;
11547 /* Generate a DIE to represent either a real live formal parameter decl or to
11548 represent just the type of some formal parameter position in some function
11549 type.
11551 Note that this routine is a bit unusual because its argument may be a
11552 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
11553 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
11554 node. If it's the former then this function is being called to output a
11555 DIE to represent a formal parameter object (or some inlining thereof). If
11556 it's the latter, then this function is only being called to output a
11557 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
11558 argument type of some subprogram type. */
11560 static dw_die_ref
11561 gen_formal_parameter_die (tree node, dw_die_ref context_die)
11563 dw_die_ref parm_die
11564 = new_die (DW_TAG_formal_parameter, context_die, node);
11565 tree origin;
11567 switch (TREE_CODE_CLASS (TREE_CODE (node)))
11569 case tcc_declaration:
11570 origin = decl_ultimate_origin (node);
11571 if (origin != NULL)
11572 add_abstract_origin_attribute (parm_die, origin);
11573 else
11575 add_name_and_src_coords_attributes (parm_die, node);
11576 add_type_attribute (parm_die, TREE_TYPE (node),
11577 TREE_READONLY (node),
11578 TREE_THIS_VOLATILE (node),
11579 context_die);
11580 if (DECL_ARTIFICIAL (node))
11581 add_AT_flag (parm_die, DW_AT_artificial, 1);
11584 equate_decl_number_to_die (node, parm_die);
11585 if (! DECL_ABSTRACT (node))
11586 add_location_or_const_value_attribute (parm_die, node, DW_AT_location);
11588 break;
11590 case tcc_type:
11591 /* We were called with some kind of a ..._TYPE node. */
11592 add_type_attribute (parm_die, node, 0, 0, context_die);
11593 break;
11595 default:
11596 gcc_unreachable ();
11599 return parm_die;
11602 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
11603 at the end of an (ANSI prototyped) formal parameters list. */
11605 static void
11606 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
11608 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
11611 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
11612 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
11613 parameters as specified in some function type specification (except for
11614 those which appear as part of a function *definition*). */
11616 static void
11617 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
11619 tree link;
11620 tree formal_type = NULL;
11621 tree first_parm_type;
11622 tree arg;
11624 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
11626 arg = DECL_ARGUMENTS (function_or_method_type);
11627 function_or_method_type = TREE_TYPE (function_or_method_type);
11629 else
11630 arg = NULL_TREE;
11632 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
11634 /* Make our first pass over the list of formal parameter types and output a
11635 DW_TAG_formal_parameter DIE for each one. */
11636 for (link = first_parm_type; link; )
11638 dw_die_ref parm_die;
11640 formal_type = TREE_VALUE (link);
11641 if (formal_type == void_type_node)
11642 break;
11644 /* Output a (nameless) DIE to represent the formal parameter itself. */
11645 parm_die = gen_formal_parameter_die (formal_type, context_die);
11646 if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
11647 && link == first_parm_type)
11648 || (arg && DECL_ARTIFICIAL (arg)))
11649 add_AT_flag (parm_die, DW_AT_artificial, 1);
11651 link = TREE_CHAIN (link);
11652 if (arg)
11653 arg = TREE_CHAIN (arg);
11656 /* If this function type has an ellipsis, add a
11657 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
11658 if (formal_type != void_type_node)
11659 gen_unspecified_parameters_die (function_or_method_type, context_die);
11661 /* Make our second (and final) pass over the list of formal parameter types
11662 and output DIEs to represent those types (as necessary). */
11663 for (link = TYPE_ARG_TYPES (function_or_method_type);
11664 link && TREE_VALUE (link);
11665 link = TREE_CHAIN (link))
11666 gen_type_die (TREE_VALUE (link), context_die);
11669 /* We want to generate the DIE for TYPE so that we can generate the
11670 die for MEMBER, which has been defined; we will need to refer back
11671 to the member declaration nested within TYPE. If we're trying to
11672 generate minimal debug info for TYPE, processing TYPE won't do the
11673 trick; we need to attach the member declaration by hand. */
11675 static void
11676 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
11678 gen_type_die (type, context_die);
11680 /* If we're trying to avoid duplicate debug info, we may not have
11681 emitted the member decl for this function. Emit it now. */
11682 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
11683 && ! lookup_decl_die (member))
11685 dw_die_ref type_die;
11686 gcc_assert (!decl_ultimate_origin (member));
11688 push_decl_scope (type);
11689 type_die = lookup_type_die (type);
11690 if (TREE_CODE (member) == FUNCTION_DECL)
11691 gen_subprogram_die (member, type_die);
11692 else if (TREE_CODE (member) == FIELD_DECL)
11694 /* Ignore the nameless fields that are used to skip bits but handle
11695 C++ anonymous unions and structs. */
11696 if (DECL_NAME (member) != NULL_TREE
11697 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
11698 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
11700 gen_type_die (member_declared_type (member), type_die);
11701 gen_field_die (member, type_die);
11704 else
11705 gen_variable_die (member, type_die);
11707 pop_decl_scope ();
11711 /* Generate the DWARF2 info for the "abstract" instance of a function which we
11712 may later generate inlined and/or out-of-line instances of. */
11714 static void
11715 dwarf2out_abstract_function (tree decl)
11717 dw_die_ref old_die;
11718 tree save_fn;
11719 struct function *save_cfun;
11720 tree context;
11721 int was_abstract = DECL_ABSTRACT (decl);
11723 /* Make sure we have the actual abstract inline, not a clone. */
11724 decl = DECL_ORIGIN (decl);
11726 old_die = lookup_decl_die (decl);
11727 if (old_die && get_AT (old_die, DW_AT_inline))
11728 /* We've already generated the abstract instance. */
11729 return;
11731 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
11732 we don't get confused by DECL_ABSTRACT. */
11733 if (debug_info_level > DINFO_LEVEL_TERSE)
11735 context = decl_class_context (decl);
11736 if (context)
11737 gen_type_die_for_member
11738 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
11741 /* Pretend we've just finished compiling this function. */
11742 save_fn = current_function_decl;
11743 save_cfun = cfun;
11744 current_function_decl = decl;
11745 cfun = DECL_STRUCT_FUNCTION (decl);
11747 set_decl_abstract_flags (decl, 1);
11748 dwarf2out_decl (decl);
11749 if (! was_abstract)
11750 set_decl_abstract_flags (decl, 0);
11752 current_function_decl = save_fn;
11753 cfun = save_cfun;
11756 /* Helper function of premark_used_types() which gets called through
11757 htab_traverse_resize().
11759 Marks the DIE of a given type in *SLOT as perennial, so it never gets
11760 marked as unused by prune_unused_types. */
11761 static int
11762 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
11764 tree type;
11765 dw_die_ref die;
11767 type = *slot;
11768 die = lookup_type_die (type);
11769 if (die != NULL)
11770 die->die_perennial_p = 1;
11771 return 1;
11774 /* Mark all members of used_types_hash as perennial. */
11775 static void
11776 premark_used_types (void)
11778 if (cfun && cfun->used_types_hash)
11779 htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
11782 /* Generate a DIE to represent a declared function (either file-scope or
11783 block-local). */
11785 static void
11786 gen_subprogram_die (tree decl, dw_die_ref context_die)
11788 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
11789 tree origin = decl_ultimate_origin (decl);
11790 dw_die_ref subr_die;
11791 tree fn_arg_types;
11792 tree outer_scope;
11793 dw_die_ref old_die = lookup_decl_die (decl);
11794 int declaration = (current_function_decl != decl
11795 || class_or_namespace_scope_p (context_die));
11797 premark_used_types ();
11799 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
11800 started to generate the abstract instance of an inline, decided to output
11801 its containing class, and proceeded to emit the declaration of the inline
11802 from the member list for the class. If so, DECLARATION takes priority;
11803 we'll get back to the abstract instance when done with the class. */
11805 /* The class-scope declaration DIE must be the primary DIE. */
11806 if (origin && declaration && class_or_namespace_scope_p (context_die))
11808 origin = NULL;
11809 gcc_assert (!old_die);
11812 /* Now that the C++ front end lazily declares artificial member fns, we
11813 might need to retrofit the declaration into its class. */
11814 if (!declaration && !origin && !old_die
11815 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
11816 && !class_or_namespace_scope_p (context_die)
11817 && debug_info_level > DINFO_LEVEL_TERSE)
11818 old_die = force_decl_die (decl);
11820 if (origin != NULL)
11822 gcc_assert (!declaration || local_scope_p (context_die));
11824 /* Fixup die_parent for the abstract instance of a nested
11825 inline function. */
11826 if (old_die && old_die->die_parent == NULL)
11827 add_child_die (context_die, old_die);
11829 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11830 add_abstract_origin_attribute (subr_die, origin);
11832 else if (old_die)
11834 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11835 struct dwarf_file_data * file_index = lookup_filename (s.file);
11837 if (!get_AT_flag (old_die, DW_AT_declaration)
11838 /* We can have a normal definition following an inline one in the
11839 case of redefinition of GNU C extern inlines.
11840 It seems reasonable to use AT_specification in this case. */
11841 && !get_AT (old_die, DW_AT_inline))
11843 /* Detect and ignore this case, where we are trying to output
11844 something we have already output. */
11845 return;
11848 /* If the definition comes from the same place as the declaration,
11849 maybe use the old DIE. We always want the DIE for this function
11850 that has the *_pc attributes to be under comp_unit_die so the
11851 debugger can find it. We also need to do this for abstract
11852 instances of inlines, since the spec requires the out-of-line copy
11853 to have the same parent. For local class methods, this doesn't
11854 apply; we just use the old DIE. */
11855 if ((old_die->die_parent == comp_unit_die || context_die == NULL)
11856 && (DECL_ARTIFICIAL (decl)
11857 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
11858 && (get_AT_unsigned (old_die, DW_AT_decl_line)
11859 == (unsigned) s.line))))
11861 subr_die = old_die;
11863 /* Clear out the declaration attribute and the formal parameters.
11864 Do not remove all children, because it is possible that this
11865 declaration die was forced using force_decl_die(). In such
11866 cases die that forced declaration die (e.g. TAG_imported_module)
11867 is one of the children that we do not want to remove. */
11868 remove_AT (subr_die, DW_AT_declaration);
11869 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
11871 else
11873 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11874 add_AT_specification (subr_die, old_die);
11875 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
11876 add_AT_file (subr_die, DW_AT_decl_file, file_index);
11877 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
11878 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
11881 else
11883 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11885 if (TREE_PUBLIC (decl))
11886 add_AT_flag (subr_die, DW_AT_external, 1);
11888 add_name_and_src_coords_attributes (subr_die, decl);
11889 if (debug_info_level > DINFO_LEVEL_TERSE)
11891 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
11892 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
11893 0, 0, context_die);
11896 add_pure_or_virtual_attribute (subr_die, decl);
11897 if (DECL_ARTIFICIAL (decl))
11898 add_AT_flag (subr_die, DW_AT_artificial, 1);
11900 if (TREE_PROTECTED (decl))
11901 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
11902 else if (TREE_PRIVATE (decl))
11903 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
11906 if (declaration)
11908 if (!old_die || !get_AT (old_die, DW_AT_inline))
11910 add_AT_flag (subr_die, DW_AT_declaration, 1);
11912 /* The first time we see a member function, it is in the context of
11913 the class to which it belongs. We make sure of this by emitting
11914 the class first. The next time is the definition, which is
11915 handled above. The two may come from the same source text.
11917 Note that force_decl_die() forces function declaration die. It is
11918 later reused to represent definition. */
11919 equate_decl_number_to_die (decl, subr_die);
11922 else if (DECL_ABSTRACT (decl))
11924 if (DECL_DECLARED_INLINE_P (decl))
11926 if (cgraph_function_possibly_inlined_p (decl))
11927 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
11928 else
11929 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
11931 else
11933 if (cgraph_function_possibly_inlined_p (decl))
11934 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
11935 else
11936 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
11939 equate_decl_number_to_die (decl, subr_die);
11941 else if (!DECL_EXTERNAL (decl))
11943 HOST_WIDE_INT cfa_fb_offset;
11945 if (!old_die || !get_AT (old_die, DW_AT_inline))
11946 equate_decl_number_to_die (decl, subr_die);
11948 if (!flag_reorder_blocks_and_partition)
11950 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
11951 current_function_funcdef_no);
11952 add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
11953 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
11954 current_function_funcdef_no);
11955 add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
11957 add_pubname (decl, subr_die);
11958 add_arange (decl, subr_die);
11960 else
11961 { /* Do nothing for now; maybe need to duplicate die, one for
11962 hot section and ond for cold section, then use the hot/cold
11963 section begin/end labels to generate the aranges... */
11965 add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
11966 add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
11967 add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
11968 add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
11970 add_pubname (decl, subr_die);
11971 add_arange (decl, subr_die);
11972 add_arange (decl, subr_die);
11976 #ifdef MIPS_DEBUGGING_INFO
11977 /* Add a reference to the FDE for this routine. */
11978 add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
11979 #endif
11981 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
11983 /* We define the "frame base" as the function's CFA. This is more
11984 convenient for several reasons: (1) It's stable across the prologue
11985 and epilogue, which makes it better than just a frame pointer,
11986 (2) With dwarf3, there exists a one-byte encoding that allows us
11987 to reference the .debug_frame data by proxy, but failing that,
11988 (3) We can at least reuse the code inspection and interpretation
11989 code that determines the CFA position at various points in the
11990 function. */
11991 /* ??? Use some command-line or configury switch to enable the use
11992 of dwarf3 DW_OP_call_frame_cfa. At present there are no dwarf
11993 consumers that understand it; fall back to "pure" dwarf2 and
11994 convert the CFA data into a location list. */
11996 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
11997 if (list->dw_loc_next)
11998 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
11999 else
12000 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
12003 /* Compute a displacement from the "steady-state frame pointer" to
12004 the CFA. The former is what all stack slots and argument slots
12005 will reference in the rtl; the later is what we've told the
12006 debugger about. We'll need to adjust all frame_base references
12007 by this displacement. */
12008 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
12010 if (cfun->static_chain_decl)
12011 add_AT_location_description (subr_die, DW_AT_static_link,
12012 loc_descriptor_from_tree (cfun->static_chain_decl));
12015 /* Now output descriptions of the arguments for this function. This gets
12016 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
12017 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
12018 `...' at the end of the formal parameter list. In order to find out if
12019 there was a trailing ellipsis or not, we must instead look at the type
12020 associated with the FUNCTION_DECL. This will be a node of type
12021 FUNCTION_TYPE. If the chain of type nodes hanging off of this
12022 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
12023 an ellipsis at the end. */
12025 /* In the case where we are describing a mere function declaration, all we
12026 need to do here (and all we *can* do here) is to describe the *types* of
12027 its formal parameters. */
12028 if (debug_info_level <= DINFO_LEVEL_TERSE)
12030 else if (declaration)
12031 gen_formal_types_die (decl, subr_die);
12032 else
12034 /* Generate DIEs to represent all known formal parameters. */
12035 tree arg_decls = DECL_ARGUMENTS (decl);
12036 tree parm;
12038 /* When generating DIEs, generate the unspecified_parameters DIE
12039 instead if we come across the arg "__builtin_va_alist" */
12040 for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
12041 if (TREE_CODE (parm) == PARM_DECL)
12043 if (DECL_NAME (parm)
12044 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
12045 "__builtin_va_alist"))
12046 gen_unspecified_parameters_die (parm, subr_die);
12047 else
12048 gen_decl_die (parm, subr_die);
12051 /* Decide whether we need an unspecified_parameters DIE at the end.
12052 There are 2 more cases to do this for: 1) the ansi ... declaration -
12053 this is detectable when the end of the arg list is not a
12054 void_type_node 2) an unprototyped function declaration (not a
12055 definition). This just means that we have no info about the
12056 parameters at all. */
12057 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
12058 if (fn_arg_types != NULL)
12060 /* This is the prototyped case, check for.... */
12061 if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
12062 gen_unspecified_parameters_die (decl, subr_die);
12064 else if (DECL_INITIAL (decl) == NULL_TREE)
12065 gen_unspecified_parameters_die (decl, subr_die);
12068 /* Output Dwarf info for all of the stuff within the body of the function
12069 (if it has one - it may be just a declaration). */
12070 outer_scope = DECL_INITIAL (decl);
12072 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
12073 a function. This BLOCK actually represents the outermost binding contour
12074 for the function, i.e. the contour in which the function's formal
12075 parameters and labels get declared. Curiously, it appears that the front
12076 end doesn't actually put the PARM_DECL nodes for the current function onto
12077 the BLOCK_VARS list for this outer scope, but are strung off of the
12078 DECL_ARGUMENTS list for the function instead.
12080 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
12081 the LABEL_DECL nodes for the function however, and we output DWARF info
12082 for those in decls_for_scope. Just within the `outer_scope' there will be
12083 a BLOCK node representing the function's outermost pair of curly braces,
12084 and any blocks used for the base and member initializers of a C++
12085 constructor function. */
12086 if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
12088 /* Emit a DW_TAG_variable DIE for a named return value. */
12089 if (DECL_NAME (DECL_RESULT (decl)))
12090 gen_decl_die (DECL_RESULT (decl), subr_die);
12092 current_function_has_inlines = 0;
12093 decls_for_scope (outer_scope, subr_die, 0);
12095 #if 0 && defined (MIPS_DEBUGGING_INFO)
12096 if (current_function_has_inlines)
12098 add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
12099 if (! comp_unit_has_inlines)
12101 add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
12102 comp_unit_has_inlines = 1;
12105 #endif
12107 /* Add the calling convention attribute if requested. */
12108 add_calling_convention_attribute (subr_die, TREE_TYPE (decl));
12112 /* Generate a DIE to represent a declared data object. */
12114 static void
12115 gen_variable_die (tree decl, dw_die_ref context_die)
12117 tree origin = decl_ultimate_origin (decl);
12118 dw_die_ref var_die = new_die (DW_TAG_variable, context_die, decl);
12120 dw_die_ref old_die = lookup_decl_die (decl);
12121 int declaration = (DECL_EXTERNAL (decl)
12122 /* If DECL is COMDAT and has not actually been
12123 emitted, we cannot take its address; there
12124 might end up being no definition anywhere in
12125 the program. For example, consider the C++
12126 test case:
12128 template <class T>
12129 struct S { static const int i = 7; };
12131 template <class T>
12132 const int S<T>::i;
12134 int f() { return S<int>::i; }
12136 Here, S<int>::i is not DECL_EXTERNAL, but no
12137 definition is required, so the compiler will
12138 not emit a definition. */
12139 || (TREE_CODE (decl) == VAR_DECL
12140 && DECL_COMDAT (decl) && !TREE_ASM_WRITTEN (decl))
12141 || class_or_namespace_scope_p (context_die));
12143 if (origin != NULL)
12144 add_abstract_origin_attribute (var_die, origin);
12146 /* Loop unrolling can create multiple blocks that refer to the same
12147 static variable, so we must test for the DW_AT_declaration flag.
12149 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
12150 copy decls and set the DECL_ABSTRACT flag on them instead of
12151 sharing them.
12153 ??? Duplicated blocks have been rewritten to use .debug_ranges.
12155 ??? The declare_in_namespace support causes us to get two DIEs for one
12156 variable, both of which are declarations. We want to avoid considering
12157 one to be a specification, so we must test that this DIE is not a
12158 declaration. */
12159 else if (old_die && TREE_STATIC (decl) && ! declaration
12160 && get_AT_flag (old_die, DW_AT_declaration) == 1)
12162 /* This is a definition of a C++ class level static. */
12163 add_AT_specification (var_die, old_die);
12164 if (DECL_NAME (decl))
12166 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12167 struct dwarf_file_data * file_index = lookup_filename (s.file);
12169 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
12170 add_AT_file (var_die, DW_AT_decl_file, file_index);
12172 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
12173 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
12176 else
12178 add_name_and_src_coords_attributes (var_die, decl);
12179 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
12180 TREE_THIS_VOLATILE (decl), context_die);
12182 if (TREE_PUBLIC (decl))
12183 add_AT_flag (var_die, DW_AT_external, 1);
12185 if (DECL_ARTIFICIAL (decl))
12186 add_AT_flag (var_die, DW_AT_artificial, 1);
12188 if (TREE_PROTECTED (decl))
12189 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
12190 else if (TREE_PRIVATE (decl))
12191 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
12194 if (declaration)
12195 add_AT_flag (var_die, DW_AT_declaration, 1);
12197 if (DECL_ABSTRACT (decl) || declaration)
12198 equate_decl_number_to_die (decl, var_die);
12200 if (! declaration && ! DECL_ABSTRACT (decl))
12202 add_location_or_const_value_attribute (var_die, decl, DW_AT_location);
12203 add_pubname (decl, var_die);
12205 else
12206 tree_add_const_value_attribute (var_die, decl);
12209 /* Generate a DIE to represent a label identifier. */
12211 static void
12212 gen_label_die (tree decl, dw_die_ref context_die)
12214 tree origin = decl_ultimate_origin (decl);
12215 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
12216 rtx insn;
12217 char label[MAX_ARTIFICIAL_LABEL_BYTES];
12219 if (origin != NULL)
12220 add_abstract_origin_attribute (lbl_die, origin);
12221 else
12222 add_name_and_src_coords_attributes (lbl_die, decl);
12224 if (DECL_ABSTRACT (decl))
12225 equate_decl_number_to_die (decl, lbl_die);
12226 else
12228 insn = DECL_RTL_IF_SET (decl);
12230 /* Deleted labels are programmer specified labels which have been
12231 eliminated because of various optimizations. We still emit them
12232 here so that it is possible to put breakpoints on them. */
12233 if (insn
12234 && (LABEL_P (insn)
12235 || ((NOTE_P (insn)
12236 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED_LABEL))))
12238 /* When optimization is enabled (via -O) some parts of the compiler
12239 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
12240 represent source-level labels which were explicitly declared by
12241 the user. This really shouldn't be happening though, so catch
12242 it if it ever does happen. */
12243 gcc_assert (!INSN_DELETED_P (insn));
12245 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
12246 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
12251 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
12252 attributes to the DIE for a block STMT, to describe where the inlined
12253 function was called from. This is similar to add_src_coords_attributes. */
12255 static inline void
12256 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
12258 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
12260 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
12261 add_AT_unsigned (die, DW_AT_call_line, s.line);
12265 /* If STMT's abstract origin is a function declaration and STMT's
12266 first subblock's abstract origin is the function's outermost block,
12267 then we're looking at the main entry point. */
12268 static bool
12269 is_inlined_entry_point (tree stmt)
12271 tree decl, block;
12273 if (!stmt || TREE_CODE (stmt) != BLOCK)
12274 return false;
12276 decl = block_ultimate_origin (stmt);
12278 if (!decl || TREE_CODE (decl) != FUNCTION_DECL)
12279 return false;
12281 block = BLOCK_SUBBLOCKS (stmt);
12283 if (block)
12285 if (TREE_CODE (block) != BLOCK)
12286 return false;
12288 block = block_ultimate_origin (block);
12291 return block == DECL_INITIAL (decl);
12294 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
12295 Add low_pc and high_pc attributes to the DIE for a block STMT. */
12297 static inline void
12298 add_high_low_attributes (tree stmt, dw_die_ref die)
12300 char label[MAX_ARTIFICIAL_LABEL_BYTES];
12302 if (BLOCK_FRAGMENT_CHAIN (stmt))
12304 tree chain;
12306 if (is_inlined_entry_point (stmt))
12308 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
12309 BLOCK_NUMBER (stmt));
12310 add_AT_lbl_id (die, DW_AT_entry_pc, label);
12313 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
12315 chain = BLOCK_FRAGMENT_CHAIN (stmt);
12318 add_ranges (chain);
12319 chain = BLOCK_FRAGMENT_CHAIN (chain);
12321 while (chain);
12322 add_ranges (NULL);
12324 else
12326 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
12327 BLOCK_NUMBER (stmt));
12328 add_AT_lbl_id (die, DW_AT_low_pc, label);
12329 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
12330 BLOCK_NUMBER (stmt));
12331 add_AT_lbl_id (die, DW_AT_high_pc, label);
12335 /* Generate a DIE for a lexical block. */
12337 static void
12338 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
12340 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
12342 if (! BLOCK_ABSTRACT (stmt))
12343 add_high_low_attributes (stmt, stmt_die);
12345 decls_for_scope (stmt, stmt_die, depth);
12348 /* Generate a DIE for an inlined subprogram. */
12350 static void
12351 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
12353 tree decl = block_ultimate_origin (stmt);
12355 /* Emit info for the abstract instance first, if we haven't yet. We
12356 must emit this even if the block is abstract, otherwise when we
12357 emit the block below (or elsewhere), we may end up trying to emit
12358 a die whose origin die hasn't been emitted, and crashing. */
12359 dwarf2out_abstract_function (decl);
12361 if (! BLOCK_ABSTRACT (stmt))
12363 dw_die_ref subr_die
12364 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
12366 add_abstract_origin_attribute (subr_die, decl);
12367 add_high_low_attributes (stmt, subr_die);
12368 add_call_src_coords_attributes (stmt, subr_die);
12370 decls_for_scope (stmt, subr_die, depth);
12371 current_function_has_inlines = 1;
12373 else
12374 /* We may get here if we're the outer block of function A that was
12375 inlined into function B that was inlined into function C. When
12376 generating debugging info for C, dwarf2out_abstract_function(B)
12377 would mark all inlined blocks as abstract, including this one.
12378 So, we wouldn't (and shouldn't) expect labels to be generated
12379 for this one. Instead, just emit debugging info for
12380 declarations within the block. This is particularly important
12381 in the case of initializers of arguments passed from B to us:
12382 if they're statement expressions containing declarations, we
12383 wouldn't generate dies for their abstract variables, and then,
12384 when generating dies for the real variables, we'd die (pun
12385 intended :-) */
12386 gen_lexical_block_die (stmt, context_die, depth);
12389 /* Generate a DIE for a field in a record, or structure. */
12391 static void
12392 gen_field_die (tree decl, dw_die_ref context_die)
12394 dw_die_ref decl_die;
12396 if (TREE_TYPE (decl) == error_mark_node)
12397 return;
12399 decl_die = new_die (DW_TAG_member, context_die, decl);
12400 add_name_and_src_coords_attributes (decl_die, decl);
12401 add_type_attribute (decl_die, member_declared_type (decl),
12402 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
12403 context_die);
12405 if (DECL_BIT_FIELD_TYPE (decl))
12407 add_byte_size_attribute (decl_die, decl);
12408 add_bit_size_attribute (decl_die, decl);
12409 add_bit_offset_attribute (decl_die, decl);
12412 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
12413 add_data_member_location_attribute (decl_die, decl);
12415 if (DECL_ARTIFICIAL (decl))
12416 add_AT_flag (decl_die, DW_AT_artificial, 1);
12418 if (TREE_PROTECTED (decl))
12419 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
12420 else if (TREE_PRIVATE (decl))
12421 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
12423 /* Equate decl number to die, so that we can look up this decl later on. */
12424 equate_decl_number_to_die (decl, decl_die);
12427 #if 0
12428 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
12429 Use modified_type_die instead.
12430 We keep this code here just in case these types of DIEs may be needed to
12431 represent certain things in other languages (e.g. Pascal) someday. */
12433 static void
12434 gen_pointer_type_die (tree type, dw_die_ref context_die)
12436 dw_die_ref ptr_die
12437 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
12439 equate_type_number_to_die (type, ptr_die);
12440 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
12441 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
12444 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
12445 Use modified_type_die instead.
12446 We keep this code here just in case these types of DIEs may be needed to
12447 represent certain things in other languages (e.g. Pascal) someday. */
12449 static void
12450 gen_reference_type_die (tree type, dw_die_ref context_die)
12452 dw_die_ref ref_die
12453 = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
12455 equate_type_number_to_die (type, ref_die);
12456 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
12457 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
12459 #endif
12461 /* Generate a DIE for a pointer to a member type. */
12463 static void
12464 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
12466 dw_die_ref ptr_die
12467 = new_die (DW_TAG_ptr_to_member_type,
12468 scope_die_for (type, context_die), type);
12470 equate_type_number_to_die (type, ptr_die);
12471 add_AT_die_ref (ptr_die, DW_AT_containing_type,
12472 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
12473 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
12476 /* Generate the DIE for the compilation unit. */
12478 static dw_die_ref
12479 gen_compile_unit_die (const char *filename)
12481 dw_die_ref die;
12482 char producer[250];
12483 const char *language_string = lang_hooks.name;
12484 int language;
12486 die = new_die (DW_TAG_compile_unit, NULL, NULL);
12488 if (filename)
12490 add_name_attribute (die, filename);
12491 /* Don't add cwd for <built-in>. */
12492 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
12493 add_comp_dir_attribute (die);
12496 sprintf (producer, "%s %s", language_string, version_string);
12498 #ifdef MIPS_DEBUGGING_INFO
12499 /* The MIPS/SGI compilers place the 'cc' command line options in the producer
12500 string. The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
12501 not appear in the producer string, the debugger reaches the conclusion
12502 that the object file is stripped and has no debugging information.
12503 To get the MIPS/SGI debugger to believe that there is debugging
12504 information in the object file, we add a -g to the producer string. */
12505 if (debug_info_level > DINFO_LEVEL_TERSE)
12506 strcat (producer, " -g");
12507 #endif
12509 add_AT_string (die, DW_AT_producer, producer);
12511 if (strcmp (language_string, "GNU C++") == 0)
12512 language = DW_LANG_C_plus_plus;
12513 else if (strcmp (language_string, "GNU Ada") == 0)
12514 language = DW_LANG_Ada95;
12515 else if (strcmp (language_string, "GNU F77") == 0)
12516 language = DW_LANG_Fortran77;
12517 else if (strcmp (language_string, "GNU F95") == 0)
12518 language = DW_LANG_Fortran95;
12519 else if (strcmp (language_string, "GNU Pascal") == 0)
12520 language = DW_LANG_Pascal83;
12521 else if (strcmp (language_string, "GNU Java") == 0)
12522 language = DW_LANG_Java;
12523 else if (strcmp (language_string, "GNU Objective-C") == 0)
12524 language = DW_LANG_ObjC;
12525 else if (strcmp (language_string, "GNU Objective-C++") == 0)
12526 language = DW_LANG_ObjC_plus_plus;
12527 else
12528 language = DW_LANG_C89;
12530 add_AT_unsigned (die, DW_AT_language, language);
12531 return die;
12534 /* Generate the DIE for a base class. */
12536 static void
12537 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
12539 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
12541 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
12542 add_data_member_location_attribute (die, binfo);
12544 if (BINFO_VIRTUAL_P (binfo))
12545 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
12547 if (access == access_public_node)
12548 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
12549 else if (access == access_protected_node)
12550 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
12553 /* Generate a DIE for a class member. */
12555 static void
12556 gen_member_die (tree type, dw_die_ref context_die)
12558 tree member;
12559 tree binfo = TYPE_BINFO (type);
12560 dw_die_ref child;
12562 /* If this is not an incomplete type, output descriptions of each of its
12563 members. Note that as we output the DIEs necessary to represent the
12564 members of this record or union type, we will also be trying to output
12565 DIEs to represent the *types* of those members. However the `type'
12566 function (above) will specifically avoid generating type DIEs for member
12567 types *within* the list of member DIEs for this (containing) type except
12568 for those types (of members) which are explicitly marked as also being
12569 members of this (containing) type themselves. The g++ front- end can
12570 force any given type to be treated as a member of some other (containing)
12571 type by setting the TYPE_CONTEXT of the given (member) type to point to
12572 the TREE node representing the appropriate (containing) type. */
12574 /* First output info about the base classes. */
12575 if (binfo)
12577 VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
12578 int i;
12579 tree base;
12581 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
12582 gen_inheritance_die (base,
12583 (accesses ? VEC_index (tree, accesses, i)
12584 : access_public_node), context_die);
12587 /* Now output info about the data members and type members. */
12588 for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
12590 /* If we thought we were generating minimal debug info for TYPE
12591 and then changed our minds, some of the member declarations
12592 may have already been defined. Don't define them again, but
12593 do put them in the right order. */
12595 child = lookup_decl_die (member);
12596 if (child)
12597 splice_child_die (context_die, child);
12598 else
12599 gen_decl_die (member, context_die);
12602 /* Now output info about the function members (if any). */
12603 for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
12605 /* Don't include clones in the member list. */
12606 if (DECL_ABSTRACT_ORIGIN (member))
12607 continue;
12609 child = lookup_decl_die (member);
12610 if (child)
12611 splice_child_die (context_die, child);
12612 else
12613 gen_decl_die (member, context_die);
12617 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
12618 is set, we pretend that the type was never defined, so we only get the
12619 member DIEs needed by later specification DIEs. */
12621 static void
12622 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
12623 enum debug_info_usage usage)
12625 dw_die_ref type_die = lookup_type_die (type);
12626 dw_die_ref scope_die = 0;
12627 int nested = 0;
12628 int complete = (TYPE_SIZE (type)
12629 && (! TYPE_STUB_DECL (type)
12630 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
12631 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
12632 complete = complete && should_emit_struct_debug (type, usage);
12634 if (type_die && ! complete)
12635 return;
12637 if (TYPE_CONTEXT (type) != NULL_TREE
12638 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
12639 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
12640 nested = 1;
12642 scope_die = scope_die_for (type, context_die);
12644 if (! type_die || (nested && scope_die == comp_unit_die))
12645 /* First occurrence of type or toplevel definition of nested class. */
12647 dw_die_ref old_die = type_die;
12649 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
12650 ? DW_TAG_structure_type : DW_TAG_union_type,
12651 scope_die, type);
12652 equate_type_number_to_die (type, type_die);
12653 if (old_die)
12654 add_AT_specification (type_die, old_die);
12655 else
12656 add_name_attribute (type_die, type_tag (type));
12658 else
12659 remove_AT (type_die, DW_AT_declaration);
12661 /* If this type has been completed, then give it a byte_size attribute and
12662 then give a list of members. */
12663 if (complete && !ns_decl)
12665 /* Prevent infinite recursion in cases where the type of some member of
12666 this type is expressed in terms of this type itself. */
12667 TREE_ASM_WRITTEN (type) = 1;
12668 add_byte_size_attribute (type_die, type);
12669 if (TYPE_STUB_DECL (type) != NULL_TREE)
12670 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
12672 /* If the first reference to this type was as the return type of an
12673 inline function, then it may not have a parent. Fix this now. */
12674 if (type_die->die_parent == NULL)
12675 add_child_die (scope_die, type_die);
12677 push_decl_scope (type);
12678 gen_member_die (type, type_die);
12679 pop_decl_scope ();
12681 /* GNU extension: Record what type our vtable lives in. */
12682 if (TYPE_VFIELD (type))
12684 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
12686 gen_type_die (vtype, context_die);
12687 add_AT_die_ref (type_die, DW_AT_containing_type,
12688 lookup_type_die (vtype));
12691 else
12693 add_AT_flag (type_die, DW_AT_declaration, 1);
12695 /* We don't need to do this for function-local types. */
12696 if (TYPE_STUB_DECL (type)
12697 && ! decl_function_context (TYPE_STUB_DECL (type)))
12698 VEC_safe_push (tree, gc, incomplete_types, type);
12701 if (get_AT (type_die, DW_AT_name))
12702 add_pubtype (type, type_die);
12705 /* Generate a DIE for a subroutine _type_. */
12707 static void
12708 gen_subroutine_type_die (tree type, dw_die_ref context_die)
12710 tree return_type = TREE_TYPE (type);
12711 dw_die_ref subr_die
12712 = new_die (DW_TAG_subroutine_type,
12713 scope_die_for (type, context_die), type);
12715 equate_type_number_to_die (type, subr_die);
12716 add_prototyped_attribute (subr_die, type);
12717 add_type_attribute (subr_die, return_type, 0, 0, context_die);
12718 gen_formal_types_die (type, subr_die);
12720 if (get_AT (subr_die, DW_AT_name))
12721 add_pubtype (type, subr_die);
12724 /* Generate a DIE for a type definition. */
12726 static void
12727 gen_typedef_die (tree decl, dw_die_ref context_die)
12729 dw_die_ref type_die;
12730 tree origin;
12732 if (TREE_ASM_WRITTEN (decl))
12733 return;
12735 TREE_ASM_WRITTEN (decl) = 1;
12736 type_die = new_die (DW_TAG_typedef, context_die, decl);
12737 origin = decl_ultimate_origin (decl);
12738 if (origin != NULL)
12739 add_abstract_origin_attribute (type_die, origin);
12740 else
12742 tree type;
12744 add_name_and_src_coords_attributes (type_die, decl);
12745 if (DECL_ORIGINAL_TYPE (decl))
12747 type = DECL_ORIGINAL_TYPE (decl);
12749 gcc_assert (type != TREE_TYPE (decl));
12750 equate_type_number_to_die (TREE_TYPE (decl), type_die);
12752 else
12753 type = TREE_TYPE (decl);
12755 add_type_attribute (type_die, type, TREE_READONLY (decl),
12756 TREE_THIS_VOLATILE (decl), context_die);
12759 if (DECL_ABSTRACT (decl))
12760 equate_decl_number_to_die (decl, type_die);
12762 if (get_AT (type_die, DW_AT_name))
12763 add_pubtype (decl, type_die);
12766 /* Generate a type description DIE. */
12768 static void
12769 gen_type_die_with_usage (tree type, dw_die_ref context_die,
12770 enum debug_info_usage usage)
12772 int need_pop;
12774 if (type == NULL_TREE || type == error_mark_node)
12775 return;
12777 if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12778 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
12780 if (TREE_ASM_WRITTEN (type))
12781 return;
12783 /* Prevent broken recursion; we can't hand off to the same type. */
12784 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
12786 TREE_ASM_WRITTEN (type) = 1;
12787 gen_decl_die (TYPE_NAME (type), context_die);
12788 return;
12791 /* We are going to output a DIE to represent the unqualified version
12792 of this type (i.e. without any const or volatile qualifiers) so
12793 get the main variant (i.e. the unqualified version) of this type
12794 now. (Vectors are special because the debugging info is in the
12795 cloned type itself). */
12796 if (TREE_CODE (type) != VECTOR_TYPE)
12797 type = type_main_variant (type);
12799 if (TREE_ASM_WRITTEN (type))
12800 return;
12802 switch (TREE_CODE (type))
12804 case ERROR_MARK:
12805 break;
12807 case POINTER_TYPE:
12808 case REFERENCE_TYPE:
12809 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
12810 ensures that the gen_type_die recursion will terminate even if the
12811 type is recursive. Recursive types are possible in Ada. */
12812 /* ??? We could perhaps do this for all types before the switch
12813 statement. */
12814 TREE_ASM_WRITTEN (type) = 1;
12816 /* For these types, all that is required is that we output a DIE (or a
12817 set of DIEs) to represent the "basis" type. */
12818 gen_type_die_with_usage (TREE_TYPE (type), context_die,
12819 DINFO_USAGE_IND_USE);
12820 break;
12822 case OFFSET_TYPE:
12823 /* This code is used for C++ pointer-to-data-member types.
12824 Output a description of the relevant class type. */
12825 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
12826 DINFO_USAGE_IND_USE);
12828 /* Output a description of the type of the object pointed to. */
12829 gen_type_die_with_usage (TREE_TYPE (type), context_die,
12830 DINFO_USAGE_IND_USE);
12832 /* Now output a DIE to represent this pointer-to-data-member type
12833 itself. */
12834 gen_ptr_to_mbr_type_die (type, context_die);
12835 break;
12837 case FUNCTION_TYPE:
12838 /* Force out return type (in case it wasn't forced out already). */
12839 gen_type_die_with_usage (TREE_TYPE (type), context_die,
12840 DINFO_USAGE_DIR_USE);
12841 gen_subroutine_type_die (type, context_die);
12842 break;
12844 case METHOD_TYPE:
12845 /* Force out return type (in case it wasn't forced out already). */
12846 gen_type_die_with_usage (TREE_TYPE (type), context_die,
12847 DINFO_USAGE_DIR_USE);
12848 gen_subroutine_type_die (type, context_die);
12849 break;
12851 case ARRAY_TYPE:
12852 gen_array_type_die (type, context_die);
12853 break;
12855 case VECTOR_TYPE:
12856 gen_array_type_die (type, context_die);
12857 break;
12859 case ENUMERAL_TYPE:
12860 case RECORD_TYPE:
12861 case UNION_TYPE:
12862 case QUAL_UNION_TYPE:
12863 /* If this is a nested type whose containing class hasn't been written
12864 out yet, writing it out will cover this one, too. This does not apply
12865 to instantiations of member class templates; they need to be added to
12866 the containing class as they are generated. FIXME: This hurts the
12867 idea of combining type decls from multiple TUs, since we can't predict
12868 what set of template instantiations we'll get. */
12869 if (TYPE_CONTEXT (type)
12870 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
12871 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
12873 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
12875 if (TREE_ASM_WRITTEN (type))
12876 return;
12878 /* If that failed, attach ourselves to the stub. */
12879 push_decl_scope (TYPE_CONTEXT (type));
12880 context_die = lookup_type_die (TYPE_CONTEXT (type));
12881 need_pop = 1;
12883 else
12885 declare_in_namespace (type, context_die);
12886 need_pop = 0;
12889 if (TREE_CODE (type) == ENUMERAL_TYPE)
12891 /* This might have been written out by the call to
12892 declare_in_namespace. */
12893 if (!TREE_ASM_WRITTEN (type))
12894 gen_enumeration_type_die (type, context_die);
12896 else
12897 gen_struct_or_union_type_die (type, context_die, usage);
12899 if (need_pop)
12900 pop_decl_scope ();
12902 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
12903 it up if it is ever completed. gen_*_type_die will set it for us
12904 when appropriate. */
12905 return;
12907 case VOID_TYPE:
12908 case INTEGER_TYPE:
12909 case REAL_TYPE:
12910 case COMPLEX_TYPE:
12911 case BOOLEAN_TYPE:
12912 /* No DIEs needed for fundamental types. */
12913 break;
12915 case LANG_TYPE:
12916 /* No Dwarf representation currently defined. */
12917 break;
12919 default:
12920 gcc_unreachable ();
12923 TREE_ASM_WRITTEN (type) = 1;
12926 static void
12927 gen_type_die (tree type, dw_die_ref context_die)
12929 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
12932 /* Generate a DIE for a tagged type instantiation. */
12934 static void
12935 gen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
12937 if (type == NULL_TREE || type == error_mark_node)
12938 return;
12940 /* We are going to output a DIE to represent the unqualified version of
12941 this type (i.e. without any const or volatile qualifiers) so make sure
12942 that we have the main variant (i.e. the unqualified version) of this
12943 type now. */
12944 gcc_assert (type == type_main_variant (type));
12946 /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
12947 an instance of an unresolved type. */
12949 switch (TREE_CODE (type))
12951 case ERROR_MARK:
12952 break;
12954 case ENUMERAL_TYPE:
12955 gen_inlined_enumeration_type_die (type, context_die);
12956 break;
12958 case RECORD_TYPE:
12959 gen_inlined_structure_type_die (type, context_die);
12960 break;
12962 case UNION_TYPE:
12963 case QUAL_UNION_TYPE:
12964 gen_inlined_union_type_die (type, context_die);
12965 break;
12967 default:
12968 gcc_unreachable ();
12972 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
12973 things which are local to the given block. */
12975 static void
12976 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
12978 int must_output_die = 0;
12979 tree origin;
12980 tree decl;
12981 enum tree_code origin_code;
12983 /* Ignore blocks that are NULL. */
12984 if (stmt == NULL_TREE)
12985 return;
12987 /* If the block is one fragment of a non-contiguous block, do not
12988 process the variables, since they will have been done by the
12989 origin block. Do process subblocks. */
12990 if (BLOCK_FRAGMENT_ORIGIN (stmt))
12992 tree sub;
12994 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
12995 gen_block_die (sub, context_die, depth + 1);
12997 return;
13000 /* Determine the "ultimate origin" of this block. This block may be an
13001 inlined instance of an inlined instance of inline function, so we have
13002 to trace all of the way back through the origin chain to find out what
13003 sort of node actually served as the original seed for the creation of
13004 the current block. */
13005 origin = block_ultimate_origin (stmt);
13006 origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
13008 /* Determine if we need to output any Dwarf DIEs at all to represent this
13009 block. */
13010 if (origin_code == FUNCTION_DECL)
13011 /* The outer scopes for inlinings *must* always be represented. We
13012 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
13013 must_output_die = 1;
13014 else
13016 /* In the case where the current block represents an inlining of the
13017 "body block" of an inline function, we must *NOT* output any DIE for
13018 this block because we have already output a DIE to represent the whole
13019 inlined function scope and the "body block" of any function doesn't
13020 really represent a different scope according to ANSI C rules. So we
13021 check here to make sure that this block does not represent a "body
13022 block inlining" before trying to set the MUST_OUTPUT_DIE flag. */
13023 if (! is_body_block (origin ? origin : stmt))
13025 /* Determine if this block directly contains any "significant"
13026 local declarations which we will need to output DIEs for. */
13027 if (debug_info_level > DINFO_LEVEL_TERSE)
13028 /* We are not in terse mode so *any* local declaration counts
13029 as being a "significant" one. */
13030 must_output_die = (BLOCK_VARS (stmt) != NULL
13031 && (TREE_USED (stmt)
13032 || TREE_ASM_WRITTEN (stmt)
13033 || BLOCK_ABSTRACT (stmt)));
13034 else
13035 /* We are in terse mode, so only local (nested) function
13036 definitions count as "significant" local declarations. */
13037 for (decl = BLOCK_VARS (stmt);
13038 decl != NULL; decl = TREE_CHAIN (decl))
13039 if (TREE_CODE (decl) == FUNCTION_DECL
13040 && DECL_INITIAL (decl))
13042 must_output_die = 1;
13043 break;
13048 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
13049 DIE for any block which contains no significant local declarations at
13050 all. Rather, in such cases we just call `decls_for_scope' so that any
13051 needed Dwarf info for any sub-blocks will get properly generated. Note
13052 that in terse mode, our definition of what constitutes a "significant"
13053 local declaration gets restricted to include only inlined function
13054 instances and local (nested) function definitions. */
13055 if (must_output_die)
13057 if (origin_code == FUNCTION_DECL)
13058 gen_inlined_subroutine_die (stmt, context_die, depth);
13059 else
13060 gen_lexical_block_die (stmt, context_die, depth);
13062 else
13063 decls_for_scope (stmt, context_die, depth);
13066 /* Generate all of the decls declared within a given scope and (recursively)
13067 all of its sub-blocks. */
13069 static void
13070 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
13072 tree decl;
13073 tree subblocks;
13075 /* Ignore NULL blocks. */
13076 if (stmt == NULL_TREE)
13077 return;
13079 if (TREE_USED (stmt))
13081 /* Output the DIEs to represent all of the data objects and typedefs
13082 declared directly within this block but not within any nested
13083 sub-blocks. Also, nested function and tag DIEs have been
13084 generated with a parent of NULL; fix that up now. */
13085 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
13087 dw_die_ref die;
13089 if (TREE_CODE (decl) == FUNCTION_DECL)
13090 die = lookup_decl_die (decl);
13091 else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
13092 die = lookup_type_die (TREE_TYPE (decl));
13093 else
13094 die = NULL;
13096 if (die != NULL && die->die_parent == NULL)
13097 add_child_die (context_die, die);
13098 /* Do not produce debug information for static variables since
13099 these might be optimized out. We are called for these later
13100 in varpool_analyze_pending_decls. */
13101 if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
13103 else
13104 gen_decl_die (decl, context_die);
13108 /* If we're at -g1, we're not interested in subblocks. */
13109 if (debug_info_level <= DINFO_LEVEL_TERSE)
13110 return;
13112 /* Output the DIEs to represent all sub-blocks (and the items declared
13113 therein) of this block. */
13114 for (subblocks = BLOCK_SUBBLOCKS (stmt);
13115 subblocks != NULL;
13116 subblocks = BLOCK_CHAIN (subblocks))
13117 gen_block_die (subblocks, context_die, depth + 1);
13120 /* Is this a typedef we can avoid emitting? */
13122 static inline int
13123 is_redundant_typedef (tree decl)
13125 if (TYPE_DECL_IS_STUB (decl))
13126 return 1;
13128 if (DECL_ARTIFICIAL (decl)
13129 && DECL_CONTEXT (decl)
13130 && is_tagged_type (DECL_CONTEXT (decl))
13131 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
13132 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
13133 /* Also ignore the artificial member typedef for the class name. */
13134 return 1;
13136 return 0;
13139 /* Returns the DIE for decl. A DIE will always be returned. */
13141 static dw_die_ref
13142 force_decl_die (tree decl)
13144 dw_die_ref decl_die;
13145 unsigned saved_external_flag;
13146 tree save_fn = NULL_TREE;
13147 decl_die = lookup_decl_die (decl);
13148 if (!decl_die)
13150 dw_die_ref context_die;
13151 tree decl_context = DECL_CONTEXT (decl);
13152 if (decl_context)
13154 /* Find die that represents this context. */
13155 if (TYPE_P (decl_context))
13156 context_die = force_type_die (decl_context);
13157 else
13158 context_die = force_decl_die (decl_context);
13160 else
13161 context_die = comp_unit_die;
13163 decl_die = lookup_decl_die (decl);
13164 if (decl_die)
13165 return decl_die;
13167 switch (TREE_CODE (decl))
13169 case FUNCTION_DECL:
13170 /* Clear current_function_decl, so that gen_subprogram_die thinks
13171 that this is a declaration. At this point, we just want to force
13172 declaration die. */
13173 save_fn = current_function_decl;
13174 current_function_decl = NULL_TREE;
13175 gen_subprogram_die (decl, context_die);
13176 current_function_decl = save_fn;
13177 break;
13179 case VAR_DECL:
13180 /* Set external flag to force declaration die. Restore it after
13181 gen_decl_die() call. */
13182 saved_external_flag = DECL_EXTERNAL (decl);
13183 DECL_EXTERNAL (decl) = 1;
13184 gen_decl_die (decl, context_die);
13185 DECL_EXTERNAL (decl) = saved_external_flag;
13186 break;
13188 case NAMESPACE_DECL:
13189 dwarf2out_decl (decl);
13190 break;
13192 default:
13193 gcc_unreachable ();
13196 /* We should be able to find the DIE now. */
13197 if (!decl_die)
13198 decl_die = lookup_decl_die (decl);
13199 gcc_assert (decl_die);
13202 return decl_die;
13205 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
13206 always returned. */
13208 static dw_die_ref
13209 force_type_die (tree type)
13211 dw_die_ref type_die;
13213 type_die = lookup_type_die (type);
13214 if (!type_die)
13216 dw_die_ref context_die;
13217 if (TYPE_CONTEXT (type))
13219 if (TYPE_P (TYPE_CONTEXT (type)))
13220 context_die = force_type_die (TYPE_CONTEXT (type));
13221 else
13222 context_die = force_decl_die (TYPE_CONTEXT (type));
13224 else
13225 context_die = comp_unit_die;
13227 type_die = lookup_type_die (type);
13228 if (type_die)
13229 return type_die;
13230 gen_type_die (type, context_die);
13231 type_die = lookup_type_die (type);
13232 gcc_assert (type_die);
13234 return type_die;
13237 /* Force out any required namespaces to be able to output DECL,
13238 and return the new context_die for it, if it's changed. */
13240 static dw_die_ref
13241 setup_namespace_context (tree thing, dw_die_ref context_die)
13243 tree context = (DECL_P (thing)
13244 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
13245 if (context && TREE_CODE (context) == NAMESPACE_DECL)
13246 /* Force out the namespace. */
13247 context_die = force_decl_die (context);
13249 return context_die;
13252 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
13253 type) within its namespace, if appropriate.
13255 For compatibility with older debuggers, namespace DIEs only contain
13256 declarations; all definitions are emitted at CU scope. */
13258 static void
13259 declare_in_namespace (tree thing, dw_die_ref context_die)
13261 dw_die_ref ns_context;
13263 if (debug_info_level <= DINFO_LEVEL_TERSE)
13264 return;
13266 /* If this decl is from an inlined function, then don't try to emit it in its
13267 namespace, as we will get confused. It would have already been emitted
13268 when the abstract instance of the inline function was emitted anyways. */
13269 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
13270 return;
13272 ns_context = setup_namespace_context (thing, context_die);
13274 if (ns_context != context_die)
13276 if (DECL_P (thing))
13277 gen_decl_die (thing, ns_context);
13278 else
13279 gen_type_die (thing, ns_context);
13283 /* Generate a DIE for a namespace or namespace alias. */
13285 static void
13286 gen_namespace_die (tree decl)
13288 dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
13290 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
13291 they are an alias of. */
13292 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
13294 /* Output a real namespace. */
13295 dw_die_ref namespace_die
13296 = new_die (DW_TAG_namespace, context_die, decl);
13297 add_name_and_src_coords_attributes (namespace_die, decl);
13298 equate_decl_number_to_die (decl, namespace_die);
13300 else
13302 /* Output a namespace alias. */
13304 /* Force out the namespace we are an alias of, if necessary. */
13305 dw_die_ref origin_die
13306 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
13308 /* Now create the namespace alias DIE. */
13309 dw_die_ref namespace_die
13310 = new_die (DW_TAG_imported_declaration, context_die, decl);
13311 add_name_and_src_coords_attributes (namespace_die, decl);
13312 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
13313 equate_decl_number_to_die (decl, namespace_die);
13317 /* Generate Dwarf debug information for a decl described by DECL. */
13319 static void
13320 gen_decl_die (tree decl, dw_die_ref context_die)
13322 tree origin;
13324 if (DECL_P (decl) && DECL_IGNORED_P (decl))
13325 return;
13327 switch (TREE_CODE (decl))
13329 case ERROR_MARK:
13330 break;
13332 case CONST_DECL:
13333 /* The individual enumerators of an enum type get output when we output
13334 the Dwarf representation of the relevant enum type itself. */
13335 break;
13337 case FUNCTION_DECL:
13338 /* Don't output any DIEs to represent mere function declarations,
13339 unless they are class members or explicit block externs. */
13340 if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
13341 && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
13342 break;
13344 #if 0
13345 /* FIXME */
13346 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
13347 on local redeclarations of global functions. That seems broken. */
13348 if (current_function_decl != decl)
13349 /* This is only a declaration. */;
13350 #endif
13352 /* If we're emitting a clone, emit info for the abstract instance. */
13353 if (DECL_ORIGIN (decl) != decl)
13354 dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
13356 /* If we're emitting an out-of-line copy of an inline function,
13357 emit info for the abstract instance and set up to refer to it. */
13358 else if (cgraph_function_possibly_inlined_p (decl)
13359 && ! DECL_ABSTRACT (decl)
13360 && ! class_or_namespace_scope_p (context_die)
13361 /* dwarf2out_abstract_function won't emit a die if this is just
13362 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
13363 that case, because that works only if we have a die. */
13364 && DECL_INITIAL (decl) != NULL_TREE)
13366 dwarf2out_abstract_function (decl);
13367 set_decl_origin_self (decl);
13370 /* Otherwise we're emitting the primary DIE for this decl. */
13371 else if (debug_info_level > DINFO_LEVEL_TERSE)
13373 /* Before we describe the FUNCTION_DECL itself, make sure that we
13374 have described its return type. */
13375 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
13377 /* And its virtual context. */
13378 if (DECL_VINDEX (decl) != NULL_TREE)
13379 gen_type_die (DECL_CONTEXT (decl), context_die);
13381 /* And its containing type. */
13382 origin = decl_class_context (decl);
13383 if (origin != NULL_TREE)
13384 gen_type_die_for_member (origin, decl, context_die);
13386 /* And its containing namespace. */
13387 declare_in_namespace (decl, context_die);
13390 /* Now output a DIE to represent the function itself. */
13391 gen_subprogram_die (decl, context_die);
13392 break;
13394 case TYPE_DECL:
13395 /* If we are in terse mode, don't generate any DIEs to represent any
13396 actual typedefs. */
13397 if (debug_info_level <= DINFO_LEVEL_TERSE)
13398 break;
13400 /* In the special case of a TYPE_DECL node representing the declaration
13401 of some type tag, if the given TYPE_DECL is marked as having been
13402 instantiated from some other (original) TYPE_DECL node (e.g. one which
13403 was generated within the original definition of an inline function) we
13404 have to generate a special (abbreviated) DW_TAG_structure_type,
13405 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. */
13406 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
13408 gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
13409 break;
13412 if (is_redundant_typedef (decl))
13413 gen_type_die (TREE_TYPE (decl), context_die);
13414 else
13415 /* Output a DIE to represent the typedef itself. */
13416 gen_typedef_die (decl, context_die);
13417 break;
13419 case LABEL_DECL:
13420 if (debug_info_level >= DINFO_LEVEL_NORMAL)
13421 gen_label_die (decl, context_die);
13422 break;
13424 case VAR_DECL:
13425 case RESULT_DECL:
13426 /* If we are in terse mode, don't generate any DIEs to represent any
13427 variable declarations or definitions. */
13428 if (debug_info_level <= DINFO_LEVEL_TERSE)
13429 break;
13431 /* Output any DIEs that are needed to specify the type of this data
13432 object. */
13433 gen_type_die (TREE_TYPE (decl), context_die);
13435 /* And its containing type. */
13436 origin = decl_class_context (decl);
13437 if (origin != NULL_TREE)
13438 gen_type_die_for_member (origin, decl, context_die);
13440 /* And its containing namespace. */
13441 declare_in_namespace (decl, context_die);
13443 /* Now output the DIE to represent the data object itself. This gets
13444 complicated because of the possibility that the VAR_DECL really
13445 represents an inlined instance of a formal parameter for an inline
13446 function. */
13447 origin = decl_ultimate_origin (decl);
13448 if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
13449 gen_formal_parameter_die (decl, context_die);
13450 else
13451 gen_variable_die (decl, context_die);
13452 break;
13454 case FIELD_DECL:
13455 /* Ignore the nameless fields that are used to skip bits but handle C++
13456 anonymous unions and structs. */
13457 if (DECL_NAME (decl) != NULL_TREE
13458 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
13459 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
13461 gen_type_die (member_declared_type (decl), context_die);
13462 gen_field_die (decl, context_die);
13464 break;
13466 case PARM_DECL:
13467 gen_type_die (TREE_TYPE (decl), context_die);
13468 gen_formal_parameter_die (decl, context_die);
13469 break;
13471 case NAMESPACE_DECL:
13472 gen_namespace_die (decl);
13473 break;
13475 default:
13476 /* Probably some frontend-internal decl. Assume we don't care. */
13477 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
13478 break;
13482 /* Output debug information for global decl DECL. Called from toplev.c after
13483 compilation proper has finished. */
13485 static void
13486 dwarf2out_global_decl (tree decl)
13488 /* Output DWARF2 information for file-scope tentative data object
13489 declarations, file-scope (extern) function declarations (which had no
13490 corresponding body) and file-scope tagged type declarations and
13491 definitions which have not yet been forced out. */
13492 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
13493 dwarf2out_decl (decl);
13496 /* Output debug information for type decl DECL. Called from toplev.c
13497 and from language front ends (to record built-in types). */
13498 static void
13499 dwarf2out_type_decl (tree decl, int local)
13501 if (!local)
13502 dwarf2out_decl (decl);
13505 /* Output debug information for imported module or decl. */
13507 static void
13508 dwarf2out_imported_module_or_decl (tree decl, tree context)
13510 dw_die_ref imported_die, at_import_die;
13511 dw_die_ref scope_die;
13512 expanded_location xloc;
13514 if (debug_info_level <= DINFO_LEVEL_TERSE)
13515 return;
13517 gcc_assert (decl);
13519 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
13520 We need decl DIE for reference and scope die. First, get DIE for the decl
13521 itself. */
13523 /* Get the scope die for decl context. Use comp_unit_die for global module
13524 or decl. If die is not found for non globals, force new die. */
13525 if (!context)
13526 scope_die = comp_unit_die;
13527 else if (TYPE_P (context))
13529 if (!should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
13530 return;
13531 scope_die = force_type_die (context);
13533 else
13534 scope_die = force_decl_die (context);
13536 /* For TYPE_DECL or CONST_DECL, lookup TREE_TYPE. */
13537 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
13539 if (is_base_type (TREE_TYPE (decl)))
13540 at_import_die = base_type_die (TREE_TYPE (decl));
13541 else
13542 at_import_die = force_type_die (TREE_TYPE (decl));
13544 else
13546 at_import_die = lookup_decl_die (decl);
13547 if (!at_import_die)
13549 /* If we're trying to avoid duplicate debug info, we may not have
13550 emitted the member decl for this field. Emit it now. */
13551 if (TREE_CODE (decl) == FIELD_DECL)
13553 tree type = DECL_CONTEXT (decl);
13554 dw_die_ref type_context_die;
13556 if (TYPE_CONTEXT (type))
13557 if (TYPE_P (TYPE_CONTEXT (type)))
13559 if (!should_emit_struct_debug (TYPE_CONTEXT (type),
13560 DINFO_USAGE_DIR_USE))
13561 return;
13562 type_context_die = force_type_die (TYPE_CONTEXT (type));
13564 else
13565 type_context_die = force_decl_die (TYPE_CONTEXT (type));
13566 else
13567 type_context_die = comp_unit_die;
13568 gen_type_die_for_member (type, decl, type_context_die);
13570 at_import_die = force_decl_die (decl);
13574 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
13575 if (TREE_CODE (decl) == NAMESPACE_DECL)
13576 imported_die = new_die (DW_TAG_imported_module, scope_die, context);
13577 else
13578 imported_die = new_die (DW_TAG_imported_declaration, scope_die, context);
13580 xloc = expand_location (input_location);
13581 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
13582 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
13583 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
13586 /* Write the debugging output for DECL. */
13588 void
13589 dwarf2out_decl (tree decl)
13591 dw_die_ref context_die = comp_unit_die;
13593 switch (TREE_CODE (decl))
13595 case ERROR_MARK:
13596 return;
13598 case FUNCTION_DECL:
13599 /* What we would really like to do here is to filter out all mere
13600 file-scope declarations of file-scope functions which are never
13601 referenced later within this translation unit (and keep all of ones
13602 that *are* referenced later on) but we aren't clairvoyant, so we have
13603 no idea which functions will be referenced in the future (i.e. later
13604 on within the current translation unit). So here we just ignore all
13605 file-scope function declarations which are not also definitions. If
13606 and when the debugger needs to know something about these functions,
13607 it will have to hunt around and find the DWARF information associated
13608 with the definition of the function.
13610 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
13611 nodes represent definitions and which ones represent mere
13612 declarations. We have to check DECL_INITIAL instead. That's because
13613 the C front-end supports some weird semantics for "extern inline"
13614 function definitions. These can get inlined within the current
13615 translation unit (and thus, we need to generate Dwarf info for their
13616 abstract instances so that the Dwarf info for the concrete inlined
13617 instances can have something to refer to) but the compiler never
13618 generates any out-of-lines instances of such things (despite the fact
13619 that they *are* definitions).
13621 The important point is that the C front-end marks these "extern
13622 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
13623 them anyway. Note that the C++ front-end also plays some similar games
13624 for inline function definitions appearing within include files which
13625 also contain `#pragma interface' pragmas. */
13626 if (DECL_INITIAL (decl) == NULL_TREE)
13627 return;
13629 /* If we're a nested function, initially use a parent of NULL; if we're
13630 a plain function, this will be fixed up in decls_for_scope. If
13631 we're a method, it will be ignored, since we already have a DIE. */
13632 if (decl_function_context (decl)
13633 /* But if we're in terse mode, we don't care about scope. */
13634 && debug_info_level > DINFO_LEVEL_TERSE)
13635 context_die = NULL;
13636 break;
13638 case VAR_DECL:
13639 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
13640 declaration and if the declaration was never even referenced from
13641 within this entire compilation unit. We suppress these DIEs in
13642 order to save space in the .debug section (by eliminating entries
13643 which are probably useless). Note that we must not suppress
13644 block-local extern declarations (whether used or not) because that
13645 would screw-up the debugger's name lookup mechanism and cause it to
13646 miss things which really ought to be in scope at a given point. */
13647 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
13648 return;
13650 /* For local statics lookup proper context die. */
13651 if (TREE_STATIC (decl) && decl_function_context (decl))
13652 context_die = lookup_decl_die (DECL_CONTEXT (decl));
13654 /* If we are in terse mode, don't generate any DIEs to represent any
13655 variable declarations or definitions. */
13656 if (debug_info_level <= DINFO_LEVEL_TERSE)
13657 return;
13658 break;
13660 case NAMESPACE_DECL:
13661 if (debug_info_level <= DINFO_LEVEL_TERSE)
13662 return;
13663 if (lookup_decl_die (decl) != NULL)
13664 return;
13665 break;
13667 case TYPE_DECL:
13668 /* Don't emit stubs for types unless they are needed by other DIEs. */
13669 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
13670 return;
13672 /* Don't bother trying to generate any DIEs to represent any of the
13673 normal built-in types for the language we are compiling. */
13674 if (DECL_IS_BUILTIN (decl))
13676 /* OK, we need to generate one for `bool' so GDB knows what type
13677 comparisons have. */
13678 if (is_cxx ()
13679 && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
13680 && ! DECL_IGNORED_P (decl))
13681 modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
13683 return;
13686 /* If we are in terse mode, don't generate any DIEs for types. */
13687 if (debug_info_level <= DINFO_LEVEL_TERSE)
13688 return;
13690 /* If we're a function-scope tag, initially use a parent of NULL;
13691 this will be fixed up in decls_for_scope. */
13692 if (decl_function_context (decl))
13693 context_die = NULL;
13695 break;
13697 default:
13698 return;
13701 gen_decl_die (decl, context_die);
13704 /* Output a marker (i.e. a label) for the beginning of the generated code for
13705 a lexical block. */
13707 static void
13708 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
13709 unsigned int blocknum)
13711 switch_to_section (current_function_section ());
13712 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
13715 /* Output a marker (i.e. a label) for the end of the generated code for a
13716 lexical block. */
13718 static void
13719 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
13721 switch_to_section (current_function_section ());
13722 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
13725 /* Returns nonzero if it is appropriate not to emit any debugging
13726 information for BLOCK, because it doesn't contain any instructions.
13728 Don't allow this for blocks with nested functions or local classes
13729 as we would end up with orphans, and in the presence of scheduling
13730 we may end up calling them anyway. */
13732 static bool
13733 dwarf2out_ignore_block (tree block)
13735 tree decl;
13737 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
13738 if (TREE_CODE (decl) == FUNCTION_DECL
13739 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
13740 return 0;
13742 return 1;
13745 /* Hash table routines for file_hash. */
13747 static int
13748 file_table_eq (const void *p1_p, const void *p2_p)
13750 const struct dwarf_file_data * p1 = p1_p;
13751 const char * p2 = p2_p;
13752 return strcmp (p1->filename, p2) == 0;
13755 static hashval_t
13756 file_table_hash (const void *p_p)
13758 const struct dwarf_file_data * p = p_p;
13759 return htab_hash_string (p->filename);
13762 /* Lookup FILE_NAME (in the list of filenames that we know about here in
13763 dwarf2out.c) and return its "index". The index of each (known) filename is
13764 just a unique number which is associated with only that one filename. We
13765 need such numbers for the sake of generating labels (in the .debug_sfnames
13766 section) and references to those files numbers (in the .debug_srcinfo
13767 and.debug_macinfo sections). If the filename given as an argument is not
13768 found in our current list, add it to the list and assign it the next
13769 available unique index number. In order to speed up searches, we remember
13770 the index of the filename was looked up last. This handles the majority of
13771 all searches. */
13773 static struct dwarf_file_data *
13774 lookup_filename (const char *file_name)
13776 void ** slot;
13777 struct dwarf_file_data * created;
13779 /* Check to see if the file name that was searched on the previous
13780 call matches this file name. If so, return the index. */
13781 if (file_table_last_lookup
13782 && (file_name == file_table_last_lookup->filename
13783 || strcmp (file_table_last_lookup->filename, file_name) == 0))
13784 return file_table_last_lookup;
13786 /* Didn't match the previous lookup, search the table. */
13787 slot = htab_find_slot_with_hash (file_table, file_name,
13788 htab_hash_string (file_name), INSERT);
13789 if (*slot)
13790 return *slot;
13792 created = ggc_alloc (sizeof (struct dwarf_file_data));
13793 created->filename = file_name;
13794 created->emitted_number = 0;
13795 *slot = created;
13796 return created;
13799 /* If the assembler will construct the file table, then translate the compiler
13800 internal file table number into the assembler file table number, and emit
13801 a .file directive if we haven't already emitted one yet. The file table
13802 numbers are different because we prune debug info for unused variables and
13803 types, which may include filenames. */
13805 static int
13806 maybe_emit_file (struct dwarf_file_data * fd)
13808 if (! fd->emitted_number)
13810 if (last_emitted_file)
13811 fd->emitted_number = last_emitted_file->emitted_number + 1;
13812 else
13813 fd->emitted_number = 1;
13814 last_emitted_file = fd;
13816 if (DWARF2_ASM_LINE_DEBUG_INFO)
13818 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
13819 output_quoted_string (asm_out_file, fd->filename);
13820 fputc ('\n', asm_out_file);
13824 return fd->emitted_number;
13827 /* Called by the final INSN scan whenever we see a var location. We
13828 use it to drop labels in the right places, and throw the location in
13829 our lookup table. */
13831 static void
13832 dwarf2out_var_location (rtx loc_note)
13834 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
13835 struct var_loc_node *newloc;
13836 rtx prev_insn;
13837 static rtx last_insn;
13838 static const char *last_label;
13839 tree decl;
13841 if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
13842 return;
13843 prev_insn = PREV_INSN (loc_note);
13845 newloc = ggc_alloc_cleared (sizeof (struct var_loc_node));
13846 /* If the insn we processed last time is the previous insn
13847 and it is also a var location note, use the label we emitted
13848 last time. */
13849 if (last_insn != NULL_RTX
13850 && last_insn == prev_insn
13851 && NOTE_P (prev_insn)
13852 && NOTE_LINE_NUMBER (prev_insn) == NOTE_INSN_VAR_LOCATION)
13854 newloc->label = last_label;
13856 else
13858 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
13859 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
13860 loclabel_num++;
13861 newloc->label = ggc_strdup (loclabel);
13863 newloc->var_loc_note = loc_note;
13864 newloc->next = NULL;
13866 if (cfun && in_cold_section_p)
13867 newloc->section_label = cfun->cold_section_label;
13868 else
13869 newloc->section_label = text_section_label;
13871 last_insn = loc_note;
13872 last_label = newloc->label;
13873 decl = NOTE_VAR_LOCATION_DECL (loc_note);
13874 add_var_loc_to_decl (decl, newloc);
13877 /* We need to reset the locations at the beginning of each
13878 function. We can't do this in the end_function hook, because the
13879 declarations that use the locations won't have been output when
13880 that hook is called. Also compute have_multiple_function_sections here. */
13882 static void
13883 dwarf2out_begin_function (tree fun)
13885 htab_empty (decl_loc_table);
13887 if (function_section (fun) != text_section)
13888 have_multiple_function_sections = true;
13891 /* Output a label to mark the beginning of a source code line entry
13892 and record information relating to this source line, in
13893 'line_info_table' for later output of the .debug_line section. */
13895 static void
13896 dwarf2out_source_line (unsigned int line, const char *filename)
13898 if (debug_info_level >= DINFO_LEVEL_NORMAL
13899 && line != 0)
13901 int file_num = maybe_emit_file (lookup_filename (filename));
13903 switch_to_section (current_function_section ());
13905 /* If requested, emit something human-readable. */
13906 if (flag_debug_asm)
13907 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
13908 filename, line);
13910 if (DWARF2_ASM_LINE_DEBUG_INFO)
13912 /* Emit the .loc directive understood by GNU as. */
13913 fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
13915 /* Indicate that line number info exists. */
13916 line_info_table_in_use++;
13918 else if (function_section (current_function_decl) != text_section)
13920 dw_separate_line_info_ref line_info;
13921 targetm.asm_out.internal_label (asm_out_file,
13922 SEPARATE_LINE_CODE_LABEL,
13923 separate_line_info_table_in_use);
13925 /* Expand the line info table if necessary. */
13926 if (separate_line_info_table_in_use
13927 == separate_line_info_table_allocated)
13929 separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
13930 separate_line_info_table
13931 = ggc_realloc (separate_line_info_table,
13932 separate_line_info_table_allocated
13933 * sizeof (dw_separate_line_info_entry));
13934 memset (separate_line_info_table
13935 + separate_line_info_table_in_use,
13937 (LINE_INFO_TABLE_INCREMENT
13938 * sizeof (dw_separate_line_info_entry)));
13941 /* Add the new entry at the end of the line_info_table. */
13942 line_info
13943 = &separate_line_info_table[separate_line_info_table_in_use++];
13944 line_info->dw_file_num = file_num;
13945 line_info->dw_line_num = line;
13946 line_info->function = current_function_funcdef_no;
13948 else
13950 dw_line_info_ref line_info;
13952 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
13953 line_info_table_in_use);
13955 /* Expand the line info table if necessary. */
13956 if (line_info_table_in_use == line_info_table_allocated)
13958 line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
13959 line_info_table
13960 = ggc_realloc (line_info_table,
13961 (line_info_table_allocated
13962 * sizeof (dw_line_info_entry)));
13963 memset (line_info_table + line_info_table_in_use, 0,
13964 LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
13967 /* Add the new entry at the end of the line_info_table. */
13968 line_info = &line_info_table[line_info_table_in_use++];
13969 line_info->dw_file_num = file_num;
13970 line_info->dw_line_num = line;
13975 /* Record the beginning of a new source file. */
13977 static void
13978 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
13980 if (flag_eliminate_dwarf2_dups)
13982 /* Record the beginning of the file for break_out_includes. */
13983 dw_die_ref bincl_die;
13985 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
13986 add_AT_string (bincl_die, DW_AT_name, filename);
13989 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13991 int file_num = maybe_emit_file (lookup_filename (filename));
13993 switch_to_section (debug_macinfo_section);
13994 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
13995 dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
13996 lineno);
13998 dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
14002 /* Record the end of a source file. */
14004 static void
14005 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
14007 if (flag_eliminate_dwarf2_dups)
14008 /* Record the end of the file for break_out_includes. */
14009 new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
14011 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14013 switch_to_section (debug_macinfo_section);
14014 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
14018 /* Called from debug_define in toplev.c. The `buffer' parameter contains
14019 the tail part of the directive line, i.e. the part which is past the
14020 initial whitespace, #, whitespace, directive-name, whitespace part. */
14022 static void
14023 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
14024 const char *buffer ATTRIBUTE_UNUSED)
14026 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14028 switch_to_section (debug_macinfo_section);
14029 dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
14030 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
14031 dw2_asm_output_nstring (buffer, -1, "The macro");
14035 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
14036 the tail part of the directive line, i.e. the part which is past the
14037 initial whitespace, #, whitespace, directive-name, whitespace part. */
14039 static void
14040 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
14041 const char *buffer ATTRIBUTE_UNUSED)
14043 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14045 switch_to_section (debug_macinfo_section);
14046 dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
14047 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
14048 dw2_asm_output_nstring (buffer, -1, "The macro");
14052 /* Set up for Dwarf output at the start of compilation. */
14054 static void
14055 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
14057 /* Allocate the file_table. */
14058 file_table = htab_create_ggc (50, file_table_hash,
14059 file_table_eq, NULL);
14061 /* Allocate the decl_die_table. */
14062 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
14063 decl_die_table_eq, NULL);
14065 /* Allocate the decl_loc_table. */
14066 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
14067 decl_loc_table_eq, NULL);
14069 /* Allocate the initial hunk of the decl_scope_table. */
14070 decl_scope_table = VEC_alloc (tree, gc, 256);
14072 /* Allocate the initial hunk of the abbrev_die_table. */
14073 abbrev_die_table = ggc_alloc_cleared (ABBREV_DIE_TABLE_INCREMENT
14074 * sizeof (dw_die_ref));
14075 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
14076 /* Zero-th entry is allocated, but unused. */
14077 abbrev_die_table_in_use = 1;
14079 /* Allocate the initial hunk of the line_info_table. */
14080 line_info_table = ggc_alloc_cleared (LINE_INFO_TABLE_INCREMENT
14081 * sizeof (dw_line_info_entry));
14082 line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
14084 /* Zero-th entry is allocated, but unused. */
14085 line_info_table_in_use = 1;
14087 /* Allocate the pubtypes and pubnames vectors. */
14088 pubname_table = VEC_alloc (pubname_entry, gc, 32);
14089 pubtype_table = VEC_alloc (pubname_entry, gc, 32);
14091 /* Generate the initial DIE for the .debug section. Note that the (string)
14092 value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
14093 will (typically) be a relative pathname and that this pathname should be
14094 taken as being relative to the directory from which the compiler was
14095 invoked when the given (base) source file was compiled. We will fill
14096 in this value in dwarf2out_finish. */
14097 comp_unit_die = gen_compile_unit_die (NULL);
14099 incomplete_types = VEC_alloc (tree, gc, 64);
14101 used_rtx_array = VEC_alloc (rtx, gc, 32);
14103 debug_info_section = get_section (DEBUG_INFO_SECTION,
14104 SECTION_DEBUG, NULL);
14105 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
14106 SECTION_DEBUG, NULL);
14107 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
14108 SECTION_DEBUG, NULL);
14109 debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
14110 SECTION_DEBUG, NULL);
14111 debug_line_section = get_section (DEBUG_LINE_SECTION,
14112 SECTION_DEBUG, NULL);
14113 debug_loc_section = get_section (DEBUG_LOC_SECTION,
14114 SECTION_DEBUG, NULL);
14115 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
14116 SECTION_DEBUG, NULL);
14117 #ifdef DEBUG_PUBTYPES_SECTION
14118 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
14119 SECTION_DEBUG, NULL);
14120 #endif
14121 debug_str_section = get_section (DEBUG_STR_SECTION,
14122 DEBUG_STR_SECTION_FLAGS, NULL);
14123 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
14124 SECTION_DEBUG, NULL);
14125 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
14126 SECTION_DEBUG, NULL);
14128 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
14129 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
14130 DEBUG_ABBREV_SECTION_LABEL, 0);
14131 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
14132 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
14133 COLD_TEXT_SECTION_LABEL, 0);
14134 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
14136 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
14137 DEBUG_INFO_SECTION_LABEL, 0);
14138 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
14139 DEBUG_LINE_SECTION_LABEL, 0);
14140 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
14141 DEBUG_RANGES_SECTION_LABEL, 0);
14142 switch_to_section (debug_abbrev_section);
14143 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
14144 switch_to_section (debug_info_section);
14145 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
14146 switch_to_section (debug_line_section);
14147 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
14149 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14151 switch_to_section (debug_macinfo_section);
14152 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
14153 DEBUG_MACINFO_SECTION_LABEL, 0);
14154 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
14157 switch_to_section (text_section);
14158 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
14159 if (flag_reorder_blocks_and_partition)
14161 switch_to_section (unlikely_text_section ());
14162 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
14166 /* A helper function for dwarf2out_finish called through
14167 ht_forall. Emit one queued .debug_str string. */
14169 static int
14170 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
14172 struct indirect_string_node *node = (struct indirect_string_node *) *h;
14174 if (node->form == DW_FORM_strp)
14176 switch_to_section (debug_str_section);
14177 ASM_OUTPUT_LABEL (asm_out_file, node->label);
14178 assemble_string (node->str, strlen (node->str) + 1);
14181 return 1;
14184 #if ENABLE_ASSERT_CHECKING
14185 /* Verify that all marks are clear. */
14187 static void
14188 verify_marks_clear (dw_die_ref die)
14190 dw_die_ref c;
14192 gcc_assert (! die->die_mark);
14193 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
14195 #endif /* ENABLE_ASSERT_CHECKING */
14197 /* Clear the marks for a die and its children.
14198 Be cool if the mark isn't set. */
14200 static void
14201 prune_unmark_dies (dw_die_ref die)
14203 dw_die_ref c;
14205 if (die->die_mark)
14206 die->die_mark = 0;
14207 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
14210 /* Given DIE that we're marking as used, find any other dies
14211 it references as attributes and mark them as used. */
14213 static void
14214 prune_unused_types_walk_attribs (dw_die_ref die)
14216 dw_attr_ref a;
14217 unsigned ix;
14219 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
14221 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
14223 /* A reference to another DIE.
14224 Make sure that it will get emitted. */
14225 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
14227 /* Set the string's refcount to 0 so that prune_unused_types_mark
14228 accounts properly for it. */
14229 if (AT_class (a) == dw_val_class_str)
14230 a->dw_attr_val.v.val_str->refcount = 0;
14235 /* Mark DIE as being used. If DOKIDS is true, then walk down
14236 to DIE's children. */
14238 static void
14239 prune_unused_types_mark (dw_die_ref die, int dokids)
14241 dw_die_ref c;
14243 if (die->die_mark == 0)
14245 /* We haven't done this node yet. Mark it as used. */
14246 die->die_mark = 1;
14248 /* We also have to mark its parents as used.
14249 (But we don't want to mark our parents' kids due to this.) */
14250 if (die->die_parent)
14251 prune_unused_types_mark (die->die_parent, 0);
14253 /* Mark any referenced nodes. */
14254 prune_unused_types_walk_attribs (die);
14256 /* If this node is a specification,
14257 also mark the definition, if it exists. */
14258 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
14259 prune_unused_types_mark (die->die_definition, 1);
14262 if (dokids && die->die_mark != 2)
14264 /* We need to walk the children, but haven't done so yet.
14265 Remember that we've walked the kids. */
14266 die->die_mark = 2;
14268 /* If this is an array type, we need to make sure our
14269 kids get marked, even if they're types. */
14270 if (die->die_tag == DW_TAG_array_type)
14271 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
14272 else
14273 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
14278 /* Walk the tree DIE and mark types that we actually use. */
14280 static void
14281 prune_unused_types_walk (dw_die_ref die)
14283 dw_die_ref c;
14285 /* Don't do anything if this node is already marked. */
14286 if (die->die_mark)
14287 return;
14289 switch (die->die_tag)
14291 case DW_TAG_const_type:
14292 case DW_TAG_packed_type:
14293 case DW_TAG_pointer_type:
14294 case DW_TAG_reference_type:
14295 case DW_TAG_volatile_type:
14296 case DW_TAG_typedef:
14297 case DW_TAG_array_type:
14298 case DW_TAG_structure_type:
14299 case DW_TAG_union_type:
14300 case DW_TAG_class_type:
14301 case DW_TAG_friend:
14302 case DW_TAG_variant_part:
14303 case DW_TAG_enumeration_type:
14304 case DW_TAG_subroutine_type:
14305 case DW_TAG_string_type:
14306 case DW_TAG_set_type:
14307 case DW_TAG_subrange_type:
14308 case DW_TAG_ptr_to_member_type:
14309 case DW_TAG_file_type:
14310 if (die->die_perennial_p)
14311 break;
14313 /* It's a type node --- don't mark it. */
14314 return;
14316 default:
14317 /* Mark everything else. */
14318 break;
14321 die->die_mark = 1;
14323 /* Now, mark any dies referenced from here. */
14324 prune_unused_types_walk_attribs (die);
14326 /* Mark children. */
14327 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
14330 /* Increment the string counts on strings referred to from DIE's
14331 attributes. */
14333 static void
14334 prune_unused_types_update_strings (dw_die_ref die)
14336 dw_attr_ref a;
14337 unsigned ix;
14339 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
14340 if (AT_class (a) == dw_val_class_str)
14342 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
14343 s->refcount++;
14344 /* Avoid unnecessarily putting strings that are used less than
14345 twice in the hash table. */
14346 if (s->refcount
14347 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
14349 void ** slot;
14350 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
14351 htab_hash_string (s->str),
14352 INSERT);
14353 gcc_assert (*slot == NULL);
14354 *slot = s;
14359 /* Remove from the tree DIE any dies that aren't marked. */
14361 static void
14362 prune_unused_types_prune (dw_die_ref die)
14364 dw_die_ref c;
14366 gcc_assert (die->die_mark);
14367 prune_unused_types_update_strings (die);
14369 if (! die->die_child)
14370 return;
14372 c = die->die_child;
14373 do {
14374 dw_die_ref prev = c;
14375 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
14376 if (c == die->die_child)
14378 /* No marked children between 'prev' and the end of the list. */
14379 if (prev == c)
14380 /* No marked children at all. */
14381 die->die_child = NULL;
14382 else
14384 prev->die_sib = c->die_sib;
14385 die->die_child = prev;
14387 return;
14390 if (c != prev->die_sib)
14391 prev->die_sib = c;
14392 prune_unused_types_prune (c);
14393 } while (c != die->die_child);
14397 /* Remove dies representing declarations that we never use. */
14399 static void
14400 prune_unused_types (void)
14402 unsigned int i;
14403 limbo_die_node *node;
14404 pubname_ref pub;
14406 #if ENABLE_ASSERT_CHECKING
14407 /* All the marks should already be clear. */
14408 verify_marks_clear (comp_unit_die);
14409 for (node = limbo_die_list; node; node = node->next)
14410 verify_marks_clear (node->die);
14411 #endif /* ENABLE_ASSERT_CHECKING */
14413 /* Set the mark on nodes that are actually used. */
14414 prune_unused_types_walk (comp_unit_die);
14415 for (node = limbo_die_list; node; node = node->next)
14416 prune_unused_types_walk (node->die);
14418 /* Also set the mark on nodes referenced from the
14419 pubname_table or arange_table. */
14420 for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
14421 prune_unused_types_mark (pub->die, 1);
14422 for (i = 0; i < arange_table_in_use; i++)
14423 prune_unused_types_mark (arange_table[i], 1);
14425 /* Get rid of nodes that aren't marked; and update the string counts. */
14426 if (debug_str_hash)
14427 htab_empty (debug_str_hash);
14428 prune_unused_types_prune (comp_unit_die);
14429 for (node = limbo_die_list; node; node = node->next)
14430 prune_unused_types_prune (node->die);
14432 /* Leave the marks clear. */
14433 prune_unmark_dies (comp_unit_die);
14434 for (node = limbo_die_list; node; node = node->next)
14435 prune_unmark_dies (node->die);
14438 /* Set the parameter to true if there are any relative pathnames in
14439 the file table. */
14440 static int
14441 file_table_relative_p (void ** slot, void *param)
14443 bool *p = param;
14444 struct dwarf_file_data *d = *slot;
14445 if (d->emitted_number && !IS_ABSOLUTE_PATH (d->filename))
14447 *p = true;
14448 return 0;
14450 return 1;
14453 /* Output stuff that dwarf requires at the end of every file,
14454 and generate the DWARF-2 debugging info. */
14456 static void
14457 dwarf2out_finish (const char *filename)
14459 limbo_die_node *node, *next_node;
14460 dw_die_ref die = 0;
14462 /* Add the name for the main input file now. We delayed this from
14463 dwarf2out_init to avoid complications with PCH. */
14464 add_name_attribute (comp_unit_die, filename);
14465 if (!IS_ABSOLUTE_PATH (filename))
14466 add_comp_dir_attribute (comp_unit_die);
14467 else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
14469 bool p = false;
14470 htab_traverse (file_table, file_table_relative_p, &p);
14471 if (p)
14472 add_comp_dir_attribute (comp_unit_die);
14475 /* Traverse the limbo die list, and add parent/child links. The only
14476 dies without parents that should be here are concrete instances of
14477 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
14478 For concrete instances, we can get the parent die from the abstract
14479 instance. */
14480 for (node = limbo_die_list; node; node = next_node)
14482 next_node = node->next;
14483 die = node->die;
14485 if (die->die_parent == NULL)
14487 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
14489 if (origin)
14490 add_child_die (origin->die_parent, die);
14491 else if (die == comp_unit_die)
14493 else if (errorcount > 0 || sorrycount > 0)
14494 /* It's OK to be confused by errors in the input. */
14495 add_child_die (comp_unit_die, die);
14496 else
14498 /* In certain situations, the lexical block containing a
14499 nested function can be optimized away, which results
14500 in the nested function die being orphaned. Likewise
14501 with the return type of that nested function. Force
14502 this to be a child of the containing function.
14504 It may happen that even the containing function got fully
14505 inlined and optimized out. In that case we are lost and
14506 assign the empty child. This should not be big issue as
14507 the function is likely unreachable too. */
14508 tree context = NULL_TREE;
14510 gcc_assert (node->created_for);
14512 if (DECL_P (node->created_for))
14513 context = DECL_CONTEXT (node->created_for);
14514 else if (TYPE_P (node->created_for))
14515 context = TYPE_CONTEXT (node->created_for);
14517 gcc_assert (context
14518 && (TREE_CODE (context) == FUNCTION_DECL
14519 || TREE_CODE (context) == NAMESPACE_DECL));
14521 origin = lookup_decl_die (context);
14522 if (origin)
14523 add_child_die (origin, die);
14524 else
14525 add_child_die (comp_unit_die, die);
14530 limbo_die_list = NULL;
14532 /* Walk through the list of incomplete types again, trying once more to
14533 emit full debugging info for them. */
14534 retry_incomplete_types ();
14536 if (flag_eliminate_unused_debug_types)
14537 prune_unused_types ();
14539 /* Generate separate CUs for each of the include files we've seen.
14540 They will go into limbo_die_list. */
14541 if (flag_eliminate_dwarf2_dups)
14542 break_out_includes (comp_unit_die);
14544 /* Traverse the DIE's and add add sibling attributes to those DIE's
14545 that have children. */
14546 add_sibling_attributes (comp_unit_die);
14547 for (node = limbo_die_list; node; node = node->next)
14548 add_sibling_attributes (node->die);
14550 /* Output a terminator label for the .text section. */
14551 switch_to_section (text_section);
14552 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
14553 if (flag_reorder_blocks_and_partition)
14555 switch_to_section (unlikely_text_section ());
14556 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
14559 /* We can only use the low/high_pc attributes if all of the code was
14560 in .text. */
14561 if (!have_multiple_function_sections)
14563 add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
14564 add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
14567 /* If it wasn't, we need to give .debug_loc and .debug_ranges an appropriate
14568 "base address". Use zero so that these addresses become absolute. */
14569 else if (have_location_lists || ranges_table_in_use)
14570 add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
14572 /* Output location list section if necessary. */
14573 if (have_location_lists)
14575 /* Output the location lists info. */
14576 switch_to_section (debug_loc_section);
14577 ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
14578 DEBUG_LOC_SECTION_LABEL, 0);
14579 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
14580 output_location_lists (die);
14583 if (debug_info_level >= DINFO_LEVEL_NORMAL)
14584 add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
14585 debug_line_section_label);
14587 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14588 add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
14590 /* Output all of the compilation units. We put the main one last so that
14591 the offsets are available to output_pubnames. */
14592 for (node = limbo_die_list; node; node = node->next)
14593 output_comp_unit (node->die, 0);
14595 output_comp_unit (comp_unit_die, 0);
14597 /* Output the abbreviation table. */
14598 switch_to_section (debug_abbrev_section);
14599 output_abbrev_section ();
14601 /* Output public names table if necessary. */
14602 if (!VEC_empty (pubname_entry, pubname_table))
14604 switch_to_section (debug_pubnames_section);
14605 output_pubnames (pubname_table);
14608 #ifdef DEBUG_PUBTYPES_SECTION
14609 /* Output public types table if necessary. */
14610 if (!VEC_empty (pubname_entry, pubtype_table))
14612 switch_to_section (debug_pubtypes_section);
14613 output_pubnames (pubtype_table);
14615 #endif
14617 /* Output the address range information. We only put functions in the arange
14618 table, so don't write it out if we don't have any. */
14619 if (fde_table_in_use)
14621 switch_to_section (debug_aranges_section);
14622 output_aranges ();
14625 /* Output ranges section if necessary. */
14626 if (ranges_table_in_use)
14628 switch_to_section (debug_ranges_section);
14629 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
14630 output_ranges ();
14633 /* Output the source line correspondence table. We must do this
14634 even if there is no line information. Otherwise, on an empty
14635 translation unit, we will generate a present, but empty,
14636 .debug_info section. IRIX 6.5 `nm' will then complain when
14637 examining the file. This is done late so that any filenames
14638 used by the debug_info section are marked as 'used'. */
14639 if (! DWARF2_ASM_LINE_DEBUG_INFO)
14641 switch_to_section (debug_line_section);
14642 output_line_info ();
14645 /* Have to end the macro section. */
14646 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14648 switch_to_section (debug_macinfo_section);
14649 dw2_asm_output_data (1, 0, "End compilation unit");
14652 /* If we emitted any DW_FORM_strp form attribute, output the string
14653 table too. */
14654 if (debug_str_hash)
14655 htab_traverse (debug_str_hash, output_indirect_string, NULL);
14657 #else
14659 /* This should never be used, but its address is needed for comparisons. */
14660 const struct gcc_debug_hooks dwarf2_debug_hooks;
14662 #endif /* DWARF2_DEBUGGING_INFO */
14664 #include "gt-dwarf2out.h"