dwarf2out.c (dw_cfi_oprnd_struct): Reduce dw_cfi_reg_num to int.
[official-gcc.git] / gcc / dwarf2out.c
blob60a7d558084fb3f75cb73ee7cdb61b2581012bc4
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 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 /* Decide whether we want to emit frame unwind information for the current
94 translation unit. */
96 int
97 dwarf2out_do_frame (void)
99 return (write_symbols == DWARF2_DEBUG
100 || write_symbols == VMS_AND_DWARF2_DEBUG
101 #ifdef DWARF2_FRAME_INFO
102 || DWARF2_FRAME_INFO
103 #endif
104 #ifdef DWARF2_UNWIND_INFO
105 || flag_unwind_tables
106 || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)
107 #endif
111 /* The size of the target's pointer type. */
112 #ifndef PTR_SIZE
113 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
114 #endif
116 /* Various versions of targetm.eh_frame_section. Note these must appear
117 outside the DWARF2_DEBUGGING_INFO || DWARF2_UNWIND_INFO macro guards. */
119 /* Version of targetm.eh_frame_section for systems with named sections. */
120 void
121 named_section_eh_frame_section (void)
123 #ifdef EH_FRAME_SECTION_NAME
124 int flags;
126 if (EH_TABLES_CAN_BE_READ_ONLY)
128 int fde_encoding;
129 int per_encoding;
130 int lsda_encoding;
132 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
133 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
134 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
135 flags = (! flag_pic
136 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
137 && (fde_encoding & 0x70) != DW_EH_PE_aligned
138 && (per_encoding & 0x70) != DW_EH_PE_absptr
139 && (per_encoding & 0x70) != DW_EH_PE_aligned
140 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
141 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
142 ? 0 : SECTION_WRITE;
144 else
145 flags = SECTION_WRITE;
146 named_section_flags (EH_FRAME_SECTION_NAME, flags);
147 #endif
150 /* Version of targetm.eh_frame_section for systems using collect2. */
151 void
152 collect2_eh_frame_section (void)
154 tree label = get_file_function_name ('F');
156 data_section ();
157 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
158 targetm.asm_out.globalize_label (asm_out_file, IDENTIFIER_POINTER (label));
159 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
162 /* Default version of targetm.eh_frame_section. */
163 void
164 default_eh_frame_section (void)
166 #ifdef EH_FRAME_SECTION_NAME
167 named_section_eh_frame_section ();
168 #else
169 collect2_eh_frame_section ();
170 #endif
173 DEF_VEC_P(rtx);
174 DEF_VEC_ALLOC_P(rtx,gc);
176 /* Array of RTXes referenced by the debugging information, which therefore
177 must be kept around forever. */
178 static GTY(()) VEC(rtx,gc) *used_rtx_array;
180 /* A pointer to the base of a list of incomplete types which might be
181 completed at some later time. incomplete_types_list needs to be a
182 VEC(tree,gc) because we want to tell the garbage collector about
183 it. */
184 static GTY(()) VEC(tree,gc) *incomplete_types;
186 /* A pointer to the base of a table of references to declaration
187 scopes. This table is a display which tracks the nesting
188 of declaration scopes at the current scope and containing
189 scopes. This table is used to find the proper place to
190 define type declaration DIE's. */
191 static GTY(()) VEC(tree,gc) *decl_scope_table;
193 /* How to start an assembler comment. */
194 #ifndef ASM_COMMENT_START
195 #define ASM_COMMENT_START ";#"
196 #endif
198 typedef struct dw_cfi_struct *dw_cfi_ref;
199 typedef struct dw_fde_struct *dw_fde_ref;
200 typedef union dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
202 /* Call frames are described using a sequence of Call Frame
203 Information instructions. The register number, offset
204 and address fields are provided as possible operands;
205 their use is selected by the opcode field. */
207 enum dw_cfi_oprnd_type {
208 dw_cfi_oprnd_unused,
209 dw_cfi_oprnd_reg_num,
210 dw_cfi_oprnd_offset,
211 dw_cfi_oprnd_addr,
212 dw_cfi_oprnd_loc
215 typedef union dw_cfi_oprnd_struct GTY(())
217 unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
218 HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
219 const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
220 struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
222 dw_cfi_oprnd;
224 typedef struct dw_cfi_struct GTY(())
226 dw_cfi_ref dw_cfi_next;
227 enum dwarf_call_frame_info dw_cfi_opc;
228 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
229 dw_cfi_oprnd1;
230 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
231 dw_cfi_oprnd2;
233 dw_cfi_node;
235 /* This is how we define the location of the CFA. We use to handle it
236 as REG + OFFSET all the time, but now it can be more complex.
237 It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
238 Instead of passing around REG and OFFSET, we pass a copy
239 of this structure. */
240 typedef struct cfa_loc GTY(())
242 HOST_WIDE_INT offset;
243 HOST_WIDE_INT base_offset;
244 unsigned int reg;
245 int indirect; /* 1 if CFA is accessed via a dereference. */
246 } dw_cfa_location;
248 /* All call frame descriptions (FDE's) in the GCC generated DWARF
249 refer to a single Common Information Entry (CIE), defined at
250 the beginning of the .debug_frame section. This use of a single
251 CIE obviates the need to keep track of multiple CIE's
252 in the DWARF generation routines below. */
254 typedef struct dw_fde_struct GTY(())
256 tree decl;
257 const char *dw_fde_begin;
258 const char *dw_fde_current_label;
259 const char *dw_fde_end;
260 const char *dw_fde_hot_section_label;
261 const char *dw_fde_hot_section_end_label;
262 const char *dw_fde_unlikely_section_label;
263 const char *dw_fde_unlikely_section_end_label;
264 bool dw_fde_switched_sections;
265 dw_cfi_ref dw_fde_cfi;
266 unsigned funcdef_number;
267 unsigned all_throwers_are_sibcalls : 1;
268 unsigned nothrow : 1;
269 unsigned uses_eh_lsda : 1;
271 dw_fde_node;
273 /* Maximum size (in bytes) of an artificially generated label. */
274 #define MAX_ARTIFICIAL_LABEL_BYTES 30
276 /* The size of addresses as they appear in the Dwarf 2 data.
277 Some architectures use word addresses to refer to code locations,
278 but Dwarf 2 info always uses byte addresses. On such machines,
279 Dwarf 2 addresses need to be larger than the architecture's
280 pointers. */
281 #ifndef DWARF2_ADDR_SIZE
282 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
283 #endif
285 /* The size in bytes of a DWARF field indicating an offset or length
286 relative to a debug info section, specified to be 4 bytes in the
287 DWARF-2 specification. The SGI/MIPS ABI defines it to be the same
288 as PTR_SIZE. */
290 #ifndef DWARF_OFFSET_SIZE
291 #define DWARF_OFFSET_SIZE 4
292 #endif
294 /* According to the (draft) DWARF 3 specification, the initial length
295 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
296 bytes are 0xffffffff, followed by the length stored in the next 8
297 bytes.
299 However, the SGI/MIPS ABI uses an initial length which is equal to
300 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
302 #ifndef DWARF_INITIAL_LENGTH_SIZE
303 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
304 #endif
306 #define DWARF_VERSION 2
308 /* Round SIZE up to the nearest BOUNDARY. */
309 #define DWARF_ROUND(SIZE,BOUNDARY) \
310 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
312 /* Offsets recorded in opcodes are a multiple of this alignment factor. */
313 #ifndef DWARF_CIE_DATA_ALIGNMENT
314 #ifdef STACK_GROWS_DOWNWARD
315 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
316 #else
317 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
318 #endif
319 #endif
321 /* A pointer to the base of a table that contains frame description
322 information for each routine. */
323 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
325 /* Number of elements currently allocated for fde_table. */
326 static GTY(()) unsigned fde_table_allocated;
328 /* Number of elements in fde_table currently in use. */
329 static GTY(()) unsigned fde_table_in_use;
331 /* Size (in elements) of increments by which we may expand the
332 fde_table. */
333 #define FDE_TABLE_INCREMENT 256
335 /* A list of call frame insns for the CIE. */
336 static GTY(()) dw_cfi_ref cie_cfi_head;
338 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
339 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
340 attribute that accelerates the lookup of the FDE associated
341 with the subprogram. This variable holds the table index of the FDE
342 associated with the current function (body) definition. */
343 static unsigned current_funcdef_fde;
344 #endif
346 struct indirect_string_node GTY(())
348 const char *str;
349 unsigned int refcount;
350 unsigned int form;
351 char *label;
354 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
356 static GTY(()) int dw2_string_counter;
357 static GTY(()) unsigned long dwarf2out_cfi_label_num;
359 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
361 /* Forward declarations for functions defined in this file. */
363 static char *stripattributes (const char *);
364 static const char *dwarf_cfi_name (unsigned);
365 static dw_cfi_ref new_cfi (void);
366 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
367 static void add_fde_cfi (const char *, dw_cfi_ref);
368 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *);
369 static void lookup_cfa (dw_cfa_location *);
370 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
371 static void initial_return_save (rtx);
372 static HOST_WIDE_INT stack_adjust_offset (rtx);
373 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
374 static void output_call_frame_info (int);
375 static void dwarf2out_stack_adjust (rtx, bool);
376 static void flush_queued_reg_saves (void);
377 static bool clobbers_queued_reg_save (rtx);
378 static void dwarf2out_frame_debug_expr (rtx, const char *);
380 /* Support for complex CFA locations. */
381 static void output_cfa_loc (dw_cfi_ref);
382 static void get_cfa_from_loc_descr (dw_cfa_location *,
383 struct dw_loc_descr_struct *);
384 static struct dw_loc_descr_struct *build_cfa_loc
385 (dw_cfa_location *);
386 static void def_cfa_1 (const char *, dw_cfa_location *);
388 /* How to start an assembler comment. */
389 #ifndef ASM_COMMENT_START
390 #define ASM_COMMENT_START ";#"
391 #endif
393 /* Data and reference forms for relocatable data. */
394 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
395 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
397 #ifndef DEBUG_FRAME_SECTION
398 #define DEBUG_FRAME_SECTION ".debug_frame"
399 #endif
401 #ifndef FUNC_BEGIN_LABEL
402 #define FUNC_BEGIN_LABEL "LFB"
403 #endif
405 #ifndef FUNC_END_LABEL
406 #define FUNC_END_LABEL "LFE"
407 #endif
409 #ifndef FRAME_BEGIN_LABEL
410 #define FRAME_BEGIN_LABEL "Lframe"
411 #endif
412 #define CIE_AFTER_SIZE_LABEL "LSCIE"
413 #define CIE_END_LABEL "LECIE"
414 #define FDE_LABEL "LSFDE"
415 #define FDE_AFTER_SIZE_LABEL "LASFDE"
416 #define FDE_END_LABEL "LEFDE"
417 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
418 #define LINE_NUMBER_END_LABEL "LELT"
419 #define LN_PROLOG_AS_LABEL "LASLTP"
420 #define LN_PROLOG_END_LABEL "LELTP"
421 #define DIE_LABEL_PREFIX "DW"
423 /* The DWARF 2 CFA column which tracks the return address. Normally this
424 is the column for PC, or the first column after all of the hard
425 registers. */
426 #ifndef DWARF_FRAME_RETURN_COLUMN
427 #ifdef PC_REGNUM
428 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGNUM (PC_REGNUM)
429 #else
430 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGISTERS
431 #endif
432 #endif
434 /* The mapping from gcc register number to DWARF 2 CFA column number. By
435 default, we just provide columns for all registers. */
436 #ifndef DWARF_FRAME_REGNUM
437 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
438 #endif
440 /* Hook used by __throw. */
443 expand_builtin_dwarf_sp_column (void)
445 return GEN_INT (DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
448 /* Return a pointer to a copy of the section string name S with all
449 attributes stripped off, and an asterisk prepended (for assemble_name). */
451 static inline char *
452 stripattributes (const char *s)
454 char *stripped = xmalloc (strlen (s) + 2);
455 char *p = stripped;
457 *p++ = '*';
459 while (*s && *s != ',')
460 *p++ = *s++;
462 *p = '\0';
463 return stripped;
466 /* Generate code to initialize the register size table. */
468 void
469 expand_builtin_init_dwarf_reg_sizes (tree address)
471 int i;
472 enum machine_mode mode = TYPE_MODE (char_type_node);
473 rtx addr = expand_expr (address, NULL_RTX, VOIDmode, 0);
474 rtx mem = gen_rtx_MEM (BLKmode, addr);
475 bool wrote_return_column = false;
477 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
478 if (DWARF_FRAME_REGNUM (i) < DWARF_FRAME_REGISTERS)
480 HOST_WIDE_INT offset = DWARF_FRAME_REGNUM (i) * GET_MODE_SIZE (mode);
481 enum machine_mode save_mode = reg_raw_mode[i];
482 HOST_WIDE_INT size;
484 if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
485 save_mode = choose_hard_reg_mode (i, 1, true);
486 if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
488 if (save_mode == VOIDmode)
489 continue;
490 wrote_return_column = true;
492 size = GET_MODE_SIZE (save_mode);
493 if (offset < 0)
494 continue;
496 emit_move_insn (adjust_address (mem, mode, offset),
497 gen_int_mode (size, mode));
500 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
501 gcc_assert (wrote_return_column);
502 i = DWARF_ALT_FRAME_RETURN_COLUMN;
503 wrote_return_column = false;
504 #else
505 i = DWARF_FRAME_RETURN_COLUMN;
506 #endif
508 if (! wrote_return_column)
510 enum machine_mode save_mode = Pmode;
511 HOST_WIDE_INT offset = i * GET_MODE_SIZE (mode);
512 HOST_WIDE_INT size = GET_MODE_SIZE (save_mode);
513 emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
517 /* Convert a DWARF call frame info. operation to its string name */
519 static const char *
520 dwarf_cfi_name (unsigned int cfi_opc)
522 switch (cfi_opc)
524 case DW_CFA_advance_loc:
525 return "DW_CFA_advance_loc";
526 case DW_CFA_offset:
527 return "DW_CFA_offset";
528 case DW_CFA_restore:
529 return "DW_CFA_restore";
530 case DW_CFA_nop:
531 return "DW_CFA_nop";
532 case DW_CFA_set_loc:
533 return "DW_CFA_set_loc";
534 case DW_CFA_advance_loc1:
535 return "DW_CFA_advance_loc1";
536 case DW_CFA_advance_loc2:
537 return "DW_CFA_advance_loc2";
538 case DW_CFA_advance_loc4:
539 return "DW_CFA_advance_loc4";
540 case DW_CFA_offset_extended:
541 return "DW_CFA_offset_extended";
542 case DW_CFA_restore_extended:
543 return "DW_CFA_restore_extended";
544 case DW_CFA_undefined:
545 return "DW_CFA_undefined";
546 case DW_CFA_same_value:
547 return "DW_CFA_same_value";
548 case DW_CFA_register:
549 return "DW_CFA_register";
550 case DW_CFA_remember_state:
551 return "DW_CFA_remember_state";
552 case DW_CFA_restore_state:
553 return "DW_CFA_restore_state";
554 case DW_CFA_def_cfa:
555 return "DW_CFA_def_cfa";
556 case DW_CFA_def_cfa_register:
557 return "DW_CFA_def_cfa_register";
558 case DW_CFA_def_cfa_offset:
559 return "DW_CFA_def_cfa_offset";
561 /* DWARF 3 */
562 case DW_CFA_def_cfa_expression:
563 return "DW_CFA_def_cfa_expression";
564 case DW_CFA_expression:
565 return "DW_CFA_expression";
566 case DW_CFA_offset_extended_sf:
567 return "DW_CFA_offset_extended_sf";
568 case DW_CFA_def_cfa_sf:
569 return "DW_CFA_def_cfa_sf";
570 case DW_CFA_def_cfa_offset_sf:
571 return "DW_CFA_def_cfa_offset_sf";
573 /* SGI/MIPS specific */
574 case DW_CFA_MIPS_advance_loc8:
575 return "DW_CFA_MIPS_advance_loc8";
577 /* GNU extensions */
578 case DW_CFA_GNU_window_save:
579 return "DW_CFA_GNU_window_save";
580 case DW_CFA_GNU_args_size:
581 return "DW_CFA_GNU_args_size";
582 case DW_CFA_GNU_negative_offset_extended:
583 return "DW_CFA_GNU_negative_offset_extended";
585 default:
586 return "DW_CFA_<unknown>";
590 /* Return a pointer to a newly allocated Call Frame Instruction. */
592 static inline dw_cfi_ref
593 new_cfi (void)
595 dw_cfi_ref cfi = ggc_alloc (sizeof (dw_cfi_node));
597 cfi->dw_cfi_next = NULL;
598 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
599 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
601 return cfi;
604 /* Add a Call Frame Instruction to list of instructions. */
606 static inline void
607 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
609 dw_cfi_ref *p;
611 /* Find the end of the chain. */
612 for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
615 *p = cfi;
618 /* Generate a new label for the CFI info to refer to. */
620 char *
621 dwarf2out_cfi_label (void)
623 static char label[20];
625 ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
626 ASM_OUTPUT_LABEL (asm_out_file, label);
627 return label;
630 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
631 or to the CIE if LABEL is NULL. */
633 static void
634 add_fde_cfi (const char *label, dw_cfi_ref cfi)
636 if (label)
638 dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
640 if (*label == 0)
641 label = dwarf2out_cfi_label ();
643 if (fde->dw_fde_current_label == NULL
644 || strcmp (label, fde->dw_fde_current_label) != 0)
646 dw_cfi_ref xcfi;
648 fde->dw_fde_current_label = label = xstrdup (label);
650 /* Set the location counter to the new label. */
651 xcfi = new_cfi ();
652 xcfi->dw_cfi_opc = DW_CFA_advance_loc4;
653 xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
654 add_cfi (&fde->dw_fde_cfi, xcfi);
657 add_cfi (&fde->dw_fde_cfi, cfi);
660 else
661 add_cfi (&cie_cfi_head, cfi);
664 /* Subroutine of lookup_cfa. */
666 static void
667 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc)
669 switch (cfi->dw_cfi_opc)
671 case DW_CFA_def_cfa_offset:
672 loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
673 break;
674 case DW_CFA_def_cfa_offset_sf:
675 loc->offset
676 = cfi->dw_cfi_oprnd1.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
677 break;
678 case DW_CFA_def_cfa_register:
679 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
680 break;
681 case DW_CFA_def_cfa:
682 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
683 loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
684 break;
685 case DW_CFA_def_cfa_sf:
686 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
687 loc->offset
688 = cfi->dw_cfi_oprnd2.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
689 break;
690 case DW_CFA_def_cfa_expression:
691 get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
692 break;
693 default:
694 break;
698 /* Find the previous value for the CFA. */
700 static void
701 lookup_cfa (dw_cfa_location *loc)
703 dw_cfi_ref cfi;
705 loc->reg = INVALID_REGNUM;
706 loc->offset = 0;
707 loc->indirect = 0;
708 loc->base_offset = 0;
710 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
711 lookup_cfa_1 (cfi, loc);
713 if (fde_table_in_use)
715 dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
716 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
717 lookup_cfa_1 (cfi, loc);
721 /* The current rule for calculating the DWARF2 canonical frame address. */
722 static dw_cfa_location cfa;
724 /* The register used for saving registers to the stack, and its offset
725 from the CFA. */
726 static dw_cfa_location cfa_store;
728 /* The running total of the size of arguments pushed onto the stack. */
729 static HOST_WIDE_INT args_size;
731 /* The last args_size we actually output. */
732 static HOST_WIDE_INT old_args_size;
734 /* Entry point to update the canonical frame address (CFA).
735 LABEL is passed to add_fde_cfi. The value of CFA is now to be
736 calculated from REG+OFFSET. */
738 void
739 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
741 dw_cfa_location loc;
742 loc.indirect = 0;
743 loc.base_offset = 0;
744 loc.reg = reg;
745 loc.offset = offset;
746 def_cfa_1 (label, &loc);
749 /* Determine if two dw_cfa_location structures define the same data. */
751 static bool
752 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
754 return (loc1->reg == loc2->reg
755 && loc1->offset == loc2->offset
756 && loc1->indirect == loc2->indirect
757 && (loc1->indirect == 0
758 || loc1->base_offset == loc2->base_offset));
761 /* This routine does the actual work. The CFA is now calculated from
762 the dw_cfa_location structure. */
764 static void
765 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
767 dw_cfi_ref cfi;
768 dw_cfa_location old_cfa, loc;
770 cfa = *loc_p;
771 loc = *loc_p;
773 if (cfa_store.reg == loc.reg && loc.indirect == 0)
774 cfa_store.offset = loc.offset;
776 loc.reg = DWARF_FRAME_REGNUM (loc.reg);
777 lookup_cfa (&old_cfa);
779 /* If nothing changed, no need to issue any call frame instructions. */
780 if (cfa_equal_p (&loc, &old_cfa))
781 return;
783 cfi = new_cfi ();
785 if (loc.reg == old_cfa.reg && !loc.indirect)
787 /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
788 the CFA register did not change but the offset did. */
789 if (loc.offset < 0)
791 HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
792 gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
794 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
795 cfi->dw_cfi_oprnd1.dw_cfi_offset = f_offset;
797 else
799 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
800 cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
804 #ifndef MIPS_DEBUGGING_INFO /* SGI dbx thinks this means no offset. */
805 else if (loc.offset == old_cfa.offset
806 && old_cfa.reg != INVALID_REGNUM
807 && !loc.indirect)
809 /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
810 indicating the CFA register has changed to <register> but the
811 offset has not changed. */
812 cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
813 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
815 #endif
817 else if (loc.indirect == 0)
819 /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
820 indicating the CFA register has changed to <register> with
821 the specified offset. */
822 if (loc.offset < 0)
824 HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
825 gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
827 cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
828 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
829 cfi->dw_cfi_oprnd2.dw_cfi_offset = f_offset;
831 else
833 cfi->dw_cfi_opc = DW_CFA_def_cfa;
834 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
835 cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
838 else
840 /* Construct a DW_CFA_def_cfa_expression instruction to
841 calculate the CFA using a full location expression since no
842 register-offset pair is available. */
843 struct dw_loc_descr_struct *loc_list;
845 cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
846 loc_list = build_cfa_loc (&loc);
847 cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
850 add_fde_cfi (label, cfi);
853 /* Add the CFI for saving a register. REG is the CFA column number.
854 LABEL is passed to add_fde_cfi.
855 If SREG is -1, the register is saved at OFFSET from the CFA;
856 otherwise it is saved in SREG. */
858 static void
859 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
861 dw_cfi_ref cfi = new_cfi ();
863 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
865 if (sreg == INVALID_REGNUM)
867 if (reg & ~0x3f)
868 /* The register number won't fit in 6 bits, so we have to use
869 the long form. */
870 cfi->dw_cfi_opc = DW_CFA_offset_extended;
871 else
872 cfi->dw_cfi_opc = DW_CFA_offset;
874 #ifdef ENABLE_CHECKING
876 /* If we get an offset that is not a multiple of
877 DWARF_CIE_DATA_ALIGNMENT, there is either a bug in the
878 definition of DWARF_CIE_DATA_ALIGNMENT, or a bug in the machine
879 description. */
880 HOST_WIDE_INT check_offset = offset / DWARF_CIE_DATA_ALIGNMENT;
882 gcc_assert (check_offset * DWARF_CIE_DATA_ALIGNMENT == offset);
884 #endif
885 offset /= DWARF_CIE_DATA_ALIGNMENT;
886 if (offset < 0)
887 cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
889 cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
891 else if (sreg == reg)
892 cfi->dw_cfi_opc = DW_CFA_same_value;
893 else
895 cfi->dw_cfi_opc = DW_CFA_register;
896 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
899 add_fde_cfi (label, cfi);
902 /* Add the CFI for saving a register window. LABEL is passed to reg_save.
903 This CFI tells the unwinder that it needs to restore the window registers
904 from the previous frame's window save area.
906 ??? Perhaps we should note in the CIE where windows are saved (instead of
907 assuming 0(cfa)) and what registers are in the window. */
909 void
910 dwarf2out_window_save (const char *label)
912 dw_cfi_ref cfi = new_cfi ();
914 cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
915 add_fde_cfi (label, cfi);
918 /* Add a CFI to update the running total of the size of arguments
919 pushed onto the stack. */
921 void
922 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
924 dw_cfi_ref cfi;
926 if (size == old_args_size)
927 return;
929 old_args_size = size;
931 cfi = new_cfi ();
932 cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
933 cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
934 add_fde_cfi (label, cfi);
937 /* Entry point for saving a register to the stack. REG is the GCC register
938 number. LABEL and OFFSET are passed to reg_save. */
940 void
941 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
943 reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
946 /* Entry point for saving the return address in the stack.
947 LABEL and OFFSET are passed to reg_save. */
949 void
950 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
952 reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
955 /* Entry point for saving the return address in a register.
956 LABEL and SREG are passed to reg_save. */
958 void
959 dwarf2out_return_reg (const char *label, unsigned int sreg)
961 reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
964 /* Record the initial position of the return address. RTL is
965 INCOMING_RETURN_ADDR_RTX. */
967 static void
968 initial_return_save (rtx rtl)
970 unsigned int reg = INVALID_REGNUM;
971 HOST_WIDE_INT offset = 0;
973 switch (GET_CODE (rtl))
975 case REG:
976 /* RA is in a register. */
977 reg = DWARF_FRAME_REGNUM (REGNO (rtl));
978 break;
980 case MEM:
981 /* RA is on the stack. */
982 rtl = XEXP (rtl, 0);
983 switch (GET_CODE (rtl))
985 case REG:
986 gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
987 offset = 0;
988 break;
990 case PLUS:
991 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
992 offset = INTVAL (XEXP (rtl, 1));
993 break;
995 case MINUS:
996 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
997 offset = -INTVAL (XEXP (rtl, 1));
998 break;
1000 default:
1001 gcc_unreachable ();
1004 break;
1006 case PLUS:
1007 /* The return address is at some offset from any value we can
1008 actually load. For instance, on the SPARC it is in %i7+8. Just
1009 ignore the offset for now; it doesn't matter for unwinding frames. */
1010 gcc_assert (GET_CODE (XEXP (rtl, 1)) == CONST_INT);
1011 initial_return_save (XEXP (rtl, 0));
1012 return;
1014 default:
1015 gcc_unreachable ();
1018 if (reg != DWARF_FRAME_RETURN_COLUMN)
1019 reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1022 /* Given a SET, calculate the amount of stack adjustment it
1023 contains. */
1025 static HOST_WIDE_INT
1026 stack_adjust_offset (rtx pattern)
1028 rtx src = SET_SRC (pattern);
1029 rtx dest = SET_DEST (pattern);
1030 HOST_WIDE_INT offset = 0;
1031 enum rtx_code code;
1033 if (dest == stack_pointer_rtx)
1035 /* (set (reg sp) (plus (reg sp) (const_int))) */
1036 code = GET_CODE (src);
1037 if (! (code == PLUS || code == MINUS)
1038 || XEXP (src, 0) != stack_pointer_rtx
1039 || GET_CODE (XEXP (src, 1)) != CONST_INT)
1040 return 0;
1042 offset = INTVAL (XEXP (src, 1));
1043 if (code == PLUS)
1044 offset = -offset;
1046 else if (MEM_P (dest))
1048 /* (set (mem (pre_dec (reg sp))) (foo)) */
1049 src = XEXP (dest, 0);
1050 code = GET_CODE (src);
1052 switch (code)
1054 case PRE_MODIFY:
1055 case POST_MODIFY:
1056 if (XEXP (src, 0) == stack_pointer_rtx)
1058 rtx val = XEXP (XEXP (src, 1), 1);
1059 /* We handle only adjustments by constant amount. */
1060 gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1061 && GET_CODE (val) == CONST_INT);
1062 offset = -INTVAL (val);
1063 break;
1065 return 0;
1067 case PRE_DEC:
1068 case POST_DEC:
1069 if (XEXP (src, 0) == stack_pointer_rtx)
1071 offset = GET_MODE_SIZE (GET_MODE (dest));
1072 break;
1074 return 0;
1076 case PRE_INC:
1077 case POST_INC:
1078 if (XEXP (src, 0) == stack_pointer_rtx)
1080 offset = -GET_MODE_SIZE (GET_MODE (dest));
1081 break;
1083 return 0;
1085 default:
1086 return 0;
1089 else
1090 return 0;
1092 return offset;
1095 /* Check INSN to see if it looks like a push or a stack adjustment, and
1096 make a note of it if it does. EH uses this information to find out how
1097 much extra space it needs to pop off the stack. */
1099 static void
1100 dwarf2out_stack_adjust (rtx insn, bool after_p)
1102 HOST_WIDE_INT offset;
1103 const char *label;
1104 int i;
1106 /* Don't handle epilogues at all. Certainly it would be wrong to do so
1107 with this function. Proper support would require all frame-related
1108 insns to be marked, and to be able to handle saving state around
1109 epilogues textually in the middle of the function. */
1110 if (prologue_epilogue_contains (insn) || sibcall_epilogue_contains (insn))
1111 return;
1113 /* If only calls can throw, and we have a frame pointer,
1114 save up adjustments until we see the CALL_INSN. */
1115 if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1117 if (CALL_P (insn) && !after_p)
1119 /* Extract the size of the args from the CALL rtx itself. */
1120 insn = PATTERN (insn);
1121 if (GET_CODE (insn) == PARALLEL)
1122 insn = XVECEXP (insn, 0, 0);
1123 if (GET_CODE (insn) == SET)
1124 insn = SET_SRC (insn);
1125 gcc_assert (GET_CODE (insn) == CALL);
1126 dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1128 return;
1131 if (CALL_P (insn) && !after_p)
1133 if (!flag_asynchronous_unwind_tables)
1134 dwarf2out_args_size ("", args_size);
1135 return;
1137 else if (BARRIER_P (insn))
1139 /* When we see a BARRIER, we know to reset args_size to 0. Usually
1140 the compiler will have already emitted a stack adjustment, but
1141 doesn't bother for calls to noreturn functions. */
1142 #ifdef STACK_GROWS_DOWNWARD
1143 offset = -args_size;
1144 #else
1145 offset = args_size;
1146 #endif
1148 else if (GET_CODE (PATTERN (insn)) == SET)
1149 offset = stack_adjust_offset (PATTERN (insn));
1150 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1151 || GET_CODE (PATTERN (insn)) == SEQUENCE)
1153 /* There may be stack adjustments inside compound insns. Search
1154 for them. */
1155 for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1156 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1157 offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i));
1159 else
1160 return;
1162 if (offset == 0)
1163 return;
1165 if (cfa.reg == STACK_POINTER_REGNUM)
1166 cfa.offset += offset;
1168 #ifndef STACK_GROWS_DOWNWARD
1169 offset = -offset;
1170 #endif
1172 args_size += offset;
1173 if (args_size < 0)
1174 args_size = 0;
1176 label = dwarf2out_cfi_label ();
1177 def_cfa_1 (label, &cfa);
1178 if (flag_asynchronous_unwind_tables)
1179 dwarf2out_args_size (label, args_size);
1182 #endif
1184 /* We delay emitting a register save until either (a) we reach the end
1185 of the prologue or (b) the register is clobbered. This clusters
1186 register saves so that there are fewer pc advances. */
1188 struct queued_reg_save GTY(())
1190 struct queued_reg_save *next;
1191 rtx reg;
1192 HOST_WIDE_INT cfa_offset;
1193 rtx saved_reg;
1196 static GTY(()) struct queued_reg_save *queued_reg_saves;
1198 /* The caller's ORIG_REG is saved in SAVED_IN_REG. */
1199 struct reg_saved_in_data GTY(()) {
1200 rtx orig_reg;
1201 rtx saved_in_reg;
1204 /* A list of registers saved in other registers.
1205 The list intentionally has a small maximum capacity of 4; if your
1206 port needs more than that, you might consider implementing a
1207 more efficient data structure. */
1208 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1209 static GTY(()) size_t num_regs_saved_in_regs;
1211 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1212 static const char *last_reg_save_label;
1214 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1215 SREG, or if SREG is NULL then it is saved at OFFSET to the CFA. */
1217 static void
1218 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1220 struct queued_reg_save *q;
1222 /* Duplicates waste space, but it's also necessary to remove them
1223 for correctness, since the queue gets output in reverse
1224 order. */
1225 for (q = queued_reg_saves; q != NULL; q = q->next)
1226 if (REGNO (q->reg) == REGNO (reg))
1227 break;
1229 if (q == NULL)
1231 q = ggc_alloc (sizeof (*q));
1232 q->next = queued_reg_saves;
1233 queued_reg_saves = q;
1236 q->reg = reg;
1237 q->cfa_offset = offset;
1238 q->saved_reg = sreg;
1240 last_reg_save_label = label;
1243 /* Output all the entries in QUEUED_REG_SAVES. */
1245 static void
1246 flush_queued_reg_saves (void)
1248 struct queued_reg_save *q;
1250 for (q = queued_reg_saves; q; q = q->next)
1252 size_t i;
1253 unsigned int reg, sreg;
1255 for (i = 0; i < num_regs_saved_in_regs; i++)
1256 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1257 break;
1258 if (q->saved_reg && i == num_regs_saved_in_regs)
1260 gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1261 num_regs_saved_in_regs++;
1263 if (i != num_regs_saved_in_regs)
1265 regs_saved_in_regs[i].orig_reg = q->reg;
1266 regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1269 reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1270 if (q->saved_reg)
1271 sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1272 else
1273 sreg = INVALID_REGNUM;
1274 reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1277 queued_reg_saves = NULL;
1278 last_reg_save_label = NULL;
1281 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1282 location for? Or, does it clobber a register which we've previously
1283 said that some other register is saved in, and for which we now
1284 have a new location for? */
1286 static bool
1287 clobbers_queued_reg_save (rtx insn)
1289 struct queued_reg_save *q;
1291 for (q = queued_reg_saves; q; q = q->next)
1293 size_t i;
1294 if (modified_in_p (q->reg, insn))
1295 return true;
1296 for (i = 0; i < num_regs_saved_in_regs; i++)
1297 if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1298 && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1299 return true;
1302 return false;
1305 /* Entry point for saving the first register into the second. */
1307 void
1308 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1310 size_t i;
1311 unsigned int regno, sregno;
1313 for (i = 0; i < num_regs_saved_in_regs; i++)
1314 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1315 break;
1316 if (i == num_regs_saved_in_regs)
1318 gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1319 num_regs_saved_in_regs++;
1321 regs_saved_in_regs[i].orig_reg = reg;
1322 regs_saved_in_regs[i].saved_in_reg = sreg;
1324 regno = DWARF_FRAME_REGNUM (REGNO (reg));
1325 sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1326 reg_save (label, regno, sregno, 0);
1329 /* What register, if any, is currently saved in REG? */
1331 static rtx
1332 reg_saved_in (rtx reg)
1334 unsigned int regn = REGNO (reg);
1335 size_t i;
1336 struct queued_reg_save *q;
1338 for (q = queued_reg_saves; q; q = q->next)
1339 if (q->saved_reg && regn == REGNO (q->saved_reg))
1340 return q->reg;
1342 for (i = 0; i < num_regs_saved_in_regs; i++)
1343 if (regs_saved_in_regs[i].saved_in_reg
1344 && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1345 return regs_saved_in_regs[i].orig_reg;
1347 return NULL_RTX;
1351 /* A temporary register holding an integral value used in adjusting SP
1352 or setting up the store_reg. The "offset" field holds the integer
1353 value, not an offset. */
1354 static dw_cfa_location cfa_temp;
1356 /* Record call frame debugging information for an expression EXPR,
1357 which either sets SP or FP (adjusting how we calculate the frame
1358 address) or saves a register to the stack or another register.
1359 LABEL indicates the address of EXPR.
1361 This function encodes a state machine mapping rtxes to actions on
1362 cfa, cfa_store, and cfa_temp.reg. We describe these rules so
1363 users need not read the source code.
1365 The High-Level Picture
1367 Changes in the register we use to calculate the CFA: Currently we
1368 assume that if you copy the CFA register into another register, we
1369 should take the other one as the new CFA register; this seems to
1370 work pretty well. If it's wrong for some target, it's simple
1371 enough not to set RTX_FRAME_RELATED_P on the insn in question.
1373 Changes in the register we use for saving registers to the stack:
1374 This is usually SP, but not always. Again, we deduce that if you
1375 copy SP into another register (and SP is not the CFA register),
1376 then the new register is the one we will be using for register
1377 saves. This also seems to work.
1379 Register saves: There's not much guesswork about this one; if
1380 RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1381 register save, and the register used to calculate the destination
1382 had better be the one we think we're using for this purpose.
1383 It's also assumed that a copy from a call-saved register to another
1384 register is saving that register if RTX_FRAME_RELATED_P is set on
1385 that instruction. If the copy is from a call-saved register to
1386 the *same* register, that means that the register is now the same
1387 value as in the caller.
1389 Except: If the register being saved is the CFA register, and the
1390 offset is nonzero, we are saving the CFA, so we assume we have to
1391 use DW_CFA_def_cfa_expression. If the offset is 0, we assume that
1392 the intent is to save the value of SP from the previous frame.
1394 In addition, if a register has previously been saved to a different
1395 register,
1397 Invariants / Summaries of Rules
1399 cfa current rule for calculating the CFA. It usually
1400 consists of a register and an offset.
1401 cfa_store register used by prologue code to save things to the stack
1402 cfa_store.offset is the offset from the value of
1403 cfa_store.reg to the actual CFA
1404 cfa_temp register holding an integral value. cfa_temp.offset
1405 stores the value, which will be used to adjust the
1406 stack pointer. cfa_temp is also used like cfa_store,
1407 to track stores to the stack via fp or a temp reg.
1409 Rules 1- 4: Setting a register's value to cfa.reg or an expression
1410 with cfa.reg as the first operand changes the cfa.reg and its
1411 cfa.offset. Rule 1 and 4 also set cfa_temp.reg and
1412 cfa_temp.offset.
1414 Rules 6- 9: Set a non-cfa.reg register value to a constant or an
1415 expression yielding a constant. This sets cfa_temp.reg
1416 and cfa_temp.offset.
1418 Rule 5: Create a new register cfa_store used to save items to the
1419 stack.
1421 Rules 10-14: Save a register to the stack. Define offset as the
1422 difference of the original location and cfa_store's
1423 location (or cfa_temp's location if cfa_temp is used).
1425 The Rules
1427 "{a,b}" indicates a choice of a xor b.
1428 "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1430 Rule 1:
1431 (set <reg1> <reg2>:cfa.reg)
1432 effects: cfa.reg = <reg1>
1433 cfa.offset unchanged
1434 cfa_temp.reg = <reg1>
1435 cfa_temp.offset = cfa.offset
1437 Rule 2:
1438 (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1439 {<const_int>,<reg>:cfa_temp.reg}))
1440 effects: cfa.reg = sp if fp used
1441 cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
1442 cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1443 if cfa_store.reg==sp
1445 Rule 3:
1446 (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
1447 effects: cfa.reg = fp
1448 cfa_offset += +/- <const_int>
1450 Rule 4:
1451 (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
1452 constraints: <reg1> != fp
1453 <reg1> != sp
1454 effects: cfa.reg = <reg1>
1455 cfa_temp.reg = <reg1>
1456 cfa_temp.offset = cfa.offset
1458 Rule 5:
1459 (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1460 constraints: <reg1> != fp
1461 <reg1> != sp
1462 effects: cfa_store.reg = <reg1>
1463 cfa_store.offset = cfa.offset - cfa_temp.offset
1465 Rule 6:
1466 (set <reg> <const_int>)
1467 effects: cfa_temp.reg = <reg>
1468 cfa_temp.offset = <const_int>
1470 Rule 7:
1471 (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1472 effects: cfa_temp.reg = <reg1>
1473 cfa_temp.offset |= <const_int>
1475 Rule 8:
1476 (set <reg> (high <exp>))
1477 effects: none
1479 Rule 9:
1480 (set <reg> (lo_sum <exp> <const_int>))
1481 effects: cfa_temp.reg = <reg>
1482 cfa_temp.offset = <const_int>
1484 Rule 10:
1485 (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1486 effects: cfa_store.offset -= <const_int>
1487 cfa.offset = cfa_store.offset if cfa.reg == sp
1488 cfa.reg = sp
1489 cfa.base_offset = -cfa_store.offset
1491 Rule 11:
1492 (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1493 effects: cfa_store.offset += -/+ mode_size(mem)
1494 cfa.offset = cfa_store.offset if cfa.reg == sp
1495 cfa.reg = sp
1496 cfa.base_offset = -cfa_store.offset
1498 Rule 12:
1499 (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1501 <reg2>)
1502 effects: cfa.reg = <reg1>
1503 cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
1505 Rule 13:
1506 (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1507 effects: cfa.reg = <reg1>
1508 cfa.base_offset = -{cfa_store,cfa_temp}.offset
1510 Rule 14:
1511 (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
1512 effects: cfa.reg = <reg1>
1513 cfa.base_offset = -cfa_temp.offset
1514 cfa_temp.offset -= mode_size(mem)
1516   Rule 15:
1517   (set <reg> {unspec, unspec_volatile})
1518   effects: target-dependent */
1520 static void
1521 dwarf2out_frame_debug_expr (rtx expr, const char *label)
1523 rtx src, dest;
1524 HOST_WIDE_INT offset;
1526 /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1527 the PARALLEL independently. The first element is always processed if
1528 it is a SET. This is for backward compatibility. Other elements
1529 are processed only if they are SETs and the RTX_FRAME_RELATED_P
1530 flag is set in them. */
1531 if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
1533 int par_index;
1534 int limit = XVECLEN (expr, 0);
1536 for (par_index = 0; par_index < limit; par_index++)
1537 if (GET_CODE (XVECEXP (expr, 0, par_index)) == SET
1538 && (RTX_FRAME_RELATED_P (XVECEXP (expr, 0, par_index))
1539 || par_index == 0))
1540 dwarf2out_frame_debug_expr (XVECEXP (expr, 0, par_index), label);
1542 return;
1545 gcc_assert (GET_CODE (expr) == SET);
1547 src = SET_SRC (expr);
1548 dest = SET_DEST (expr);
1550 if (REG_P (src))
1552 rtx rsi = reg_saved_in (src);
1553 if (rsi)
1554 src = rsi;
1557 switch (GET_CODE (dest))
1559 case REG:
1560 switch (GET_CODE (src))
1562 /* Setting FP from SP. */
1563 case REG:
1564 if (cfa.reg == (unsigned) REGNO (src))
1566 /* Rule 1 */
1567 /* Update the CFA rule wrt SP or FP. Make sure src is
1568 relative to the current CFA register.
1570 We used to require that dest be either SP or FP, but the
1571 ARM copies SP to a temporary register, and from there to
1572 FP. So we just rely on the backends to only set
1573 RTX_FRAME_RELATED_P on appropriate insns. */
1574 cfa.reg = REGNO (dest);
1575 cfa_temp.reg = cfa.reg;
1576 cfa_temp.offset = cfa.offset;
1578 else
1580 /* Saving a register in a register. */
1581 gcc_assert (call_used_regs [REGNO (dest)]
1582 && (!fixed_regs [REGNO (dest)]
1583 /* For the SPARC and its register window. */
1584 || DWARF_FRAME_REGNUM (REGNO (src))
1585 == DWARF_FRAME_RETURN_COLUMN));
1586 queue_reg_save (label, src, dest, 0);
1588 break;
1590 case PLUS:
1591 case MINUS:
1592 case LO_SUM:
1593 if (dest == stack_pointer_rtx)
1595 /* Rule 2 */
1596 /* Adjusting SP. */
1597 switch (GET_CODE (XEXP (src, 1)))
1599 case CONST_INT:
1600 offset = INTVAL (XEXP (src, 1));
1601 break;
1602 case REG:
1603 gcc_assert ((unsigned) REGNO (XEXP (src, 1))
1604 == cfa_temp.reg);
1605 offset = cfa_temp.offset;
1606 break;
1607 default:
1608 gcc_unreachable ();
1611 if (XEXP (src, 0) == hard_frame_pointer_rtx)
1613 /* Restoring SP from FP in the epilogue. */
1614 gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
1615 cfa.reg = STACK_POINTER_REGNUM;
1617 else if (GET_CODE (src) == LO_SUM)
1618 /* Assume we've set the source reg of the LO_SUM from sp. */
1620 else
1621 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
1623 if (GET_CODE (src) != MINUS)
1624 offset = -offset;
1625 if (cfa.reg == STACK_POINTER_REGNUM)
1626 cfa.offset += offset;
1627 if (cfa_store.reg == STACK_POINTER_REGNUM)
1628 cfa_store.offset += offset;
1630 else if (dest == hard_frame_pointer_rtx)
1632 /* Rule 3 */
1633 /* Either setting the FP from an offset of the SP,
1634 or adjusting the FP */
1635 gcc_assert (frame_pointer_needed);
1637 gcc_assert (REG_P (XEXP (src, 0))
1638 && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
1639 && GET_CODE (XEXP (src, 1)) == CONST_INT);
1640 offset = INTVAL (XEXP (src, 1));
1641 if (GET_CODE (src) != MINUS)
1642 offset = -offset;
1643 cfa.offset += offset;
1644 cfa.reg = HARD_FRAME_POINTER_REGNUM;
1646 else
1648 gcc_assert (GET_CODE (src) != MINUS);
1650 /* Rule 4 */
1651 if (REG_P (XEXP (src, 0))
1652 && REGNO (XEXP (src, 0)) == cfa.reg
1653 && GET_CODE (XEXP (src, 1)) == CONST_INT)
1655 /* Setting a temporary CFA register that will be copied
1656 into the FP later on. */
1657 offset = - INTVAL (XEXP (src, 1));
1658 cfa.offset += offset;
1659 cfa.reg = REGNO (dest);
1660 /* Or used to save regs to the stack. */
1661 cfa_temp.reg = cfa.reg;
1662 cfa_temp.offset = cfa.offset;
1665 /* Rule 5 */
1666 else if (REG_P (XEXP (src, 0))
1667 && REGNO (XEXP (src, 0)) == cfa_temp.reg
1668 && XEXP (src, 1) == stack_pointer_rtx)
1670 /* Setting a scratch register that we will use instead
1671 of SP for saving registers to the stack. */
1672 gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
1673 cfa_store.reg = REGNO (dest);
1674 cfa_store.offset = cfa.offset - cfa_temp.offset;
1677 /* Rule 9 */
1678 else if (GET_CODE (src) == LO_SUM
1679 && GET_CODE (XEXP (src, 1)) == CONST_INT)
1681 cfa_temp.reg = REGNO (dest);
1682 cfa_temp.offset = INTVAL (XEXP (src, 1));
1684 else
1685 gcc_unreachable ();
1687 break;
1689 /* Rule 6 */
1690 case CONST_INT:
1691 cfa_temp.reg = REGNO (dest);
1692 cfa_temp.offset = INTVAL (src);
1693 break;
1695 /* Rule 7 */
1696 case IOR:
1697 gcc_assert (REG_P (XEXP (src, 0))
1698 && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
1699 && GET_CODE (XEXP (src, 1)) == CONST_INT);
1701 if ((unsigned) REGNO (dest) != cfa_temp.reg)
1702 cfa_temp.reg = REGNO (dest);
1703 cfa_temp.offset |= INTVAL (XEXP (src, 1));
1704 break;
1706 /* Skip over HIGH, assuming it will be followed by a LO_SUM,
1707 which will fill in all of the bits. */
1708 /* Rule 8 */
1709 case HIGH:
1710 break;
1712 /* Rule 15 */
1713 case UNSPEC:
1714 case UNSPEC_VOLATILE:
1715 gcc_assert (targetm.dwarf_handle_frame_unspec);
1716 targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
1717 return;
1719 default:
1720 gcc_unreachable ();
1723 def_cfa_1 (label, &cfa);
1724 break;
1726 case MEM:
1727 gcc_assert (REG_P (src));
1729 /* Saving a register to the stack. Make sure dest is relative to the
1730 CFA register. */
1731 switch (GET_CODE (XEXP (dest, 0)))
1733 /* Rule 10 */
1734 /* With a push. */
1735 case PRE_MODIFY:
1736 /* We can't handle variable size modifications. */
1737 gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
1738 == CONST_INT);
1739 offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
1741 gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1742 && cfa_store.reg == STACK_POINTER_REGNUM);
1744 cfa_store.offset += offset;
1745 if (cfa.reg == STACK_POINTER_REGNUM)
1746 cfa.offset = cfa_store.offset;
1748 offset = -cfa_store.offset;
1749 break;
1751 /* Rule 11 */
1752 case PRE_INC:
1753 case PRE_DEC:
1754 offset = GET_MODE_SIZE (GET_MODE (dest));
1755 if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
1756 offset = -offset;
1758 gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1759 && cfa_store.reg == STACK_POINTER_REGNUM);
1761 cfa_store.offset += offset;
1762 if (cfa.reg == STACK_POINTER_REGNUM)
1763 cfa.offset = cfa_store.offset;
1765 offset = -cfa_store.offset;
1766 break;
1768 /* Rule 12 */
1769 /* With an offset. */
1770 case PLUS:
1771 case MINUS:
1772 case LO_SUM:
1774 int regno;
1776 gcc_assert (GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT);
1777 offset = INTVAL (XEXP (XEXP (dest, 0), 1));
1778 if (GET_CODE (XEXP (dest, 0)) == MINUS)
1779 offset = -offset;
1781 regno = REGNO (XEXP (XEXP (dest, 0), 0));
1783 if (cfa_store.reg == (unsigned) regno)
1784 offset -= cfa_store.offset;
1785 else
1787 gcc_assert (cfa_temp.reg == (unsigned) regno);
1788 offset -= cfa_temp.offset;
1791 break;
1793 /* Rule 13 */
1794 /* Without an offset. */
1795 case REG:
1797 int regno = REGNO (XEXP (dest, 0));
1799 if (cfa_store.reg == (unsigned) regno)
1800 offset = -cfa_store.offset;
1801 else
1803 gcc_assert (cfa_temp.reg == (unsigned) regno);
1804 offset = -cfa_temp.offset;
1807 break;
1809 /* Rule 14 */
1810 case POST_INC:
1811 gcc_assert (cfa_temp.reg
1812 == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
1813 offset = -cfa_temp.offset;
1814 cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
1815 break;
1817 default:
1818 gcc_unreachable ();
1821 if (REGNO (src) != STACK_POINTER_REGNUM
1822 && REGNO (src) != HARD_FRAME_POINTER_REGNUM
1823 && (unsigned) REGNO (src) == cfa.reg)
1825 /* We're storing the current CFA reg into the stack. */
1827 if (cfa.offset == 0)
1829 /* If the source register is exactly the CFA, assume
1830 we're saving SP like any other register; this happens
1831 on the ARM. */
1832 def_cfa_1 (label, &cfa);
1833 queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
1834 break;
1836 else
1838 /* Otherwise, we'll need to look in the stack to
1839 calculate the CFA. */
1840 rtx x = XEXP (dest, 0);
1842 if (!REG_P (x))
1843 x = XEXP (x, 0);
1844 gcc_assert (REG_P (x));
1846 cfa.reg = REGNO (x);
1847 cfa.base_offset = offset;
1848 cfa.indirect = 1;
1849 def_cfa_1 (label, &cfa);
1850 break;
1854 def_cfa_1 (label, &cfa);
1855 queue_reg_save (label, src, NULL_RTX, offset);
1856 break;
1858 default:
1859 gcc_unreachable ();
1863 /* Record call frame debugging information for INSN, which either
1864 sets SP or FP (adjusting how we calculate the frame address) or saves a
1865 register to the stack. If INSN is NULL_RTX, initialize our state.
1867 If AFTER_P is false, we're being called before the insn is emitted,
1868 otherwise after. Call instructions get invoked twice. */
1870 void
1871 dwarf2out_frame_debug (rtx insn, bool after_p)
1873 const char *label;
1874 rtx src;
1876 if (insn == NULL_RTX)
1878 size_t i;
1880 /* Flush any queued register saves. */
1881 flush_queued_reg_saves ();
1883 /* Set up state for generating call frame debug info. */
1884 lookup_cfa (&cfa);
1885 gcc_assert (cfa.reg
1886 == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
1888 cfa.reg = STACK_POINTER_REGNUM;
1889 cfa_store = cfa;
1890 cfa_temp.reg = -1;
1891 cfa_temp.offset = 0;
1893 for (i = 0; i < num_regs_saved_in_regs; i++)
1895 regs_saved_in_regs[i].orig_reg = NULL_RTX;
1896 regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
1898 num_regs_saved_in_regs = 0;
1899 return;
1902 if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
1903 flush_queued_reg_saves ();
1905 if (! RTX_FRAME_RELATED_P (insn))
1907 if (!ACCUMULATE_OUTGOING_ARGS)
1908 dwarf2out_stack_adjust (insn, after_p);
1909 return;
1912 label = dwarf2out_cfi_label ();
1913 src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1914 if (src)
1915 insn = XEXP (src, 0);
1916 else
1917 insn = PATTERN (insn);
1919 dwarf2out_frame_debug_expr (insn, label);
1922 #endif
1924 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
1925 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
1926 (enum dwarf_call_frame_info cfi);
1928 static enum dw_cfi_oprnd_type
1929 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
1931 switch (cfi)
1933 case DW_CFA_nop:
1934 case DW_CFA_GNU_window_save:
1935 return dw_cfi_oprnd_unused;
1937 case DW_CFA_set_loc:
1938 case DW_CFA_advance_loc1:
1939 case DW_CFA_advance_loc2:
1940 case DW_CFA_advance_loc4:
1941 case DW_CFA_MIPS_advance_loc8:
1942 return dw_cfi_oprnd_addr;
1944 case DW_CFA_offset:
1945 case DW_CFA_offset_extended:
1946 case DW_CFA_def_cfa:
1947 case DW_CFA_offset_extended_sf:
1948 case DW_CFA_def_cfa_sf:
1949 case DW_CFA_restore_extended:
1950 case DW_CFA_undefined:
1951 case DW_CFA_same_value:
1952 case DW_CFA_def_cfa_register:
1953 case DW_CFA_register:
1954 return dw_cfi_oprnd_reg_num;
1956 case DW_CFA_def_cfa_offset:
1957 case DW_CFA_GNU_args_size:
1958 case DW_CFA_def_cfa_offset_sf:
1959 return dw_cfi_oprnd_offset;
1961 case DW_CFA_def_cfa_expression:
1962 case DW_CFA_expression:
1963 return dw_cfi_oprnd_loc;
1965 default:
1966 gcc_unreachable ();
1970 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
1971 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
1972 (enum dwarf_call_frame_info cfi);
1974 static enum dw_cfi_oprnd_type
1975 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
1977 switch (cfi)
1979 case DW_CFA_def_cfa:
1980 case DW_CFA_def_cfa_sf:
1981 case DW_CFA_offset:
1982 case DW_CFA_offset_extended_sf:
1983 case DW_CFA_offset_extended:
1984 return dw_cfi_oprnd_offset;
1986 case DW_CFA_register:
1987 return dw_cfi_oprnd_reg_num;
1989 default:
1990 return dw_cfi_oprnd_unused;
1994 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1996 /* Map register numbers held in the call frame info that gcc has
1997 collected using DWARF_FRAME_REGNUM to those that should be output in
1998 .debug_frame and .eh_frame. */
1999 #ifndef DWARF2_FRAME_REG_OUT
2000 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
2001 #endif
2003 /* Output a Call Frame Information opcode and its operand(s). */
2005 static void
2006 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
2008 unsigned long r;
2009 if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
2010 dw2_asm_output_data (1, (cfi->dw_cfi_opc
2011 | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
2012 "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
2013 cfi->dw_cfi_oprnd1.dw_cfi_offset);
2014 else if (cfi->dw_cfi_opc == DW_CFA_offset)
2016 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2017 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2018 "DW_CFA_offset, column 0x%lx", r);
2019 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2021 else if (cfi->dw_cfi_opc == DW_CFA_restore)
2023 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2024 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2025 "DW_CFA_restore, column 0x%lx", r);
2027 else
2029 dw2_asm_output_data (1, cfi->dw_cfi_opc,
2030 "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
2032 switch (cfi->dw_cfi_opc)
2034 case DW_CFA_set_loc:
2035 if (for_eh)
2036 dw2_asm_output_encoded_addr_rtx (
2037 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
2038 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
2039 false, NULL);
2040 else
2041 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2042 cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
2043 break;
2045 case DW_CFA_advance_loc1:
2046 dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2047 fde->dw_fde_current_label, NULL);
2048 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2049 break;
2051 case DW_CFA_advance_loc2:
2052 dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2053 fde->dw_fde_current_label, NULL);
2054 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2055 break;
2057 case DW_CFA_advance_loc4:
2058 dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2059 fde->dw_fde_current_label, NULL);
2060 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2061 break;
2063 case DW_CFA_MIPS_advance_loc8:
2064 dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2065 fde->dw_fde_current_label, NULL);
2066 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2067 break;
2069 case DW_CFA_offset_extended:
2070 case DW_CFA_def_cfa:
2071 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2072 dw2_asm_output_data_uleb128 (r, NULL);
2073 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2074 break;
2076 case DW_CFA_offset_extended_sf:
2077 case DW_CFA_def_cfa_sf:
2078 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2079 dw2_asm_output_data_uleb128 (r, NULL);
2080 dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2081 break;
2083 case DW_CFA_restore_extended:
2084 case DW_CFA_undefined:
2085 case DW_CFA_same_value:
2086 case DW_CFA_def_cfa_register:
2087 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2088 dw2_asm_output_data_uleb128 (r, NULL);
2089 break;
2091 case DW_CFA_register:
2092 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2093 dw2_asm_output_data_uleb128 (r, NULL);
2094 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
2095 dw2_asm_output_data_uleb128 (r, NULL);
2096 break;
2098 case DW_CFA_def_cfa_offset:
2099 case DW_CFA_GNU_args_size:
2100 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2101 break;
2103 case DW_CFA_def_cfa_offset_sf:
2104 dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2105 break;
2107 case DW_CFA_GNU_window_save:
2108 break;
2110 case DW_CFA_def_cfa_expression:
2111 case DW_CFA_expression:
2112 output_cfa_loc (cfi);
2113 break;
2115 case DW_CFA_GNU_negative_offset_extended:
2116 /* Obsoleted by DW_CFA_offset_extended_sf. */
2117 gcc_unreachable ();
2119 default:
2120 break;
2125 /* Output the call frame information used to record information
2126 that relates to calculating the frame pointer, and records the
2127 location of saved registers. */
2129 static void
2130 output_call_frame_info (int for_eh)
2132 unsigned int i;
2133 dw_fde_ref fde;
2134 dw_cfi_ref cfi;
2135 char l1[20], l2[20], section_start_label[20];
2136 bool any_lsda_needed = false;
2137 char augmentation[6];
2138 int augmentation_size;
2139 int fde_encoding = DW_EH_PE_absptr;
2140 int per_encoding = DW_EH_PE_absptr;
2141 int lsda_encoding = DW_EH_PE_absptr;
2142 int return_reg;
2144 /* Don't emit a CIE if there won't be any FDEs. */
2145 if (fde_table_in_use == 0)
2146 return;
2148 /* If we make FDEs linkonce, we may have to emit an empty label for
2149 an FDE that wouldn't otherwise be emitted. We want to avoid
2150 having an FDE kept around when the function it refers to is
2151 discarded. Example where this matters: a primary function
2152 template in C++ requires EH information, but an explicit
2153 specialization doesn't. */
2154 if (TARGET_USES_WEAK_UNWIND_INFO
2155 && ! flag_asynchronous_unwind_tables
2156 && for_eh)
2157 for (i = 0; i < fde_table_in_use; i++)
2158 if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
2159 && !fde_table[i].uses_eh_lsda
2160 && ! DECL_WEAK (fde_table[i].decl))
2161 targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
2162 for_eh, /* empty */ 1);
2164 /* If we don't have any functions we'll want to unwind out of, don't
2165 emit any EH unwind information. Note that if exceptions aren't
2166 enabled, we won't have collected nothrow information, and if we
2167 asked for asynchronous tables, we always want this info. */
2168 if (for_eh)
2170 bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
2172 for (i = 0; i < fde_table_in_use; i++)
2173 if (fde_table[i].uses_eh_lsda)
2174 any_eh_needed = any_lsda_needed = true;
2175 else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2176 any_eh_needed = true;
2177 else if (! fde_table[i].nothrow
2178 && ! fde_table[i].all_throwers_are_sibcalls)
2179 any_eh_needed = true;
2181 if (! any_eh_needed)
2182 return;
2185 /* We're going to be generating comments, so turn on app. */
2186 if (flag_debug_asm)
2187 app_enable ();
2189 if (for_eh)
2190 targetm.asm_out.eh_frame_section ();
2191 else
2192 named_section_flags (DEBUG_FRAME_SECTION, SECTION_DEBUG);
2194 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
2195 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
2197 /* Output the CIE. */
2198 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
2199 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
2200 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2201 "Length of Common Information Entry");
2202 ASM_OUTPUT_LABEL (asm_out_file, l1);
2204 /* Now that the CIE pointer is PC-relative for EH,
2205 use 0 to identify the CIE. */
2206 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
2207 (for_eh ? 0 : DW_CIE_ID),
2208 "CIE Identifier Tag");
2210 dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
2212 augmentation[0] = 0;
2213 augmentation_size = 0;
2214 if (for_eh)
2216 char *p;
2218 /* Augmentation:
2219 z Indicates that a uleb128 is present to size the
2220 augmentation section.
2221 L Indicates the encoding (and thus presence) of
2222 an LSDA pointer in the FDE augmentation.
2223 R Indicates a non-default pointer encoding for
2224 FDE code pointers.
2225 P Indicates the presence of an encoding + language
2226 personality routine in the CIE augmentation. */
2228 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
2229 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
2230 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
2232 p = augmentation + 1;
2233 if (eh_personality_libfunc)
2235 *p++ = 'P';
2236 augmentation_size += 1 + size_of_encoded_value (per_encoding);
2238 if (any_lsda_needed)
2240 *p++ = 'L';
2241 augmentation_size += 1;
2243 if (fde_encoding != DW_EH_PE_absptr)
2245 *p++ = 'R';
2246 augmentation_size += 1;
2248 if (p > augmentation + 1)
2250 augmentation[0] = 'z';
2251 *p = '\0';
2254 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
2255 if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
2257 int offset = ( 4 /* Length */
2258 + 4 /* CIE Id */
2259 + 1 /* CIE version */
2260 + strlen (augmentation) + 1 /* Augmentation */
2261 + size_of_uleb128 (1) /* Code alignment */
2262 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
2263 + 1 /* RA column */
2264 + 1 /* Augmentation size */
2265 + 1 /* Personality encoding */ );
2266 int pad = -offset & (PTR_SIZE - 1);
2268 augmentation_size += pad;
2270 /* Augmentations should be small, so there's scarce need to
2271 iterate for a solution. Die if we exceed one uleb128 byte. */
2272 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
2276 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
2277 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
2278 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
2279 "CIE Data Alignment Factor");
2281 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
2282 if (DW_CIE_VERSION == 1)
2283 dw2_asm_output_data (1, return_reg, "CIE RA Column");
2284 else
2285 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
2287 if (augmentation[0])
2289 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
2290 if (eh_personality_libfunc)
2292 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
2293 eh_data_format_name (per_encoding));
2294 dw2_asm_output_encoded_addr_rtx (per_encoding,
2295 eh_personality_libfunc,
2296 true, NULL);
2299 if (any_lsda_needed)
2300 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
2301 eh_data_format_name (lsda_encoding));
2303 if (fde_encoding != DW_EH_PE_absptr)
2304 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
2305 eh_data_format_name (fde_encoding));
2308 for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
2309 output_cfi (cfi, NULL, for_eh);
2311 /* Pad the CIE out to an address sized boundary. */
2312 ASM_OUTPUT_ALIGN (asm_out_file,
2313 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
2314 ASM_OUTPUT_LABEL (asm_out_file, l2);
2316 /* Loop through all of the FDE's. */
2317 for (i = 0; i < fde_table_in_use; i++)
2319 fde = &fde_table[i];
2321 /* Don't emit EH unwind info for leaf functions that don't need it. */
2322 if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
2323 && (fde->nothrow || fde->all_throwers_are_sibcalls)
2324 && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2325 && !fde->uses_eh_lsda)
2326 continue;
2328 targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
2329 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
2330 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
2331 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
2332 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2333 "FDE Length");
2334 ASM_OUTPUT_LABEL (asm_out_file, l1);
2336 if (for_eh)
2337 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
2338 else
2339 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
2340 "FDE CIE offset");
2342 if (for_eh)
2344 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
2345 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
2346 dw2_asm_output_encoded_addr_rtx (fde_encoding,
2347 sym_ref,
2348 false,
2349 "FDE initial location");
2350 if (fde->dw_fde_switched_sections)
2352 rtx sym_ref2 = gen_rtx_SYMBOL_REF (Pmode,
2353 fde->dw_fde_unlikely_section_label);
2354 rtx sym_ref3= gen_rtx_SYMBOL_REF (Pmode,
2355 fde->dw_fde_hot_section_label);
2356 SYMBOL_REF_FLAGS (sym_ref2) |= SYMBOL_FLAG_LOCAL;
2357 SYMBOL_REF_FLAGS (sym_ref3) |= SYMBOL_FLAG_LOCAL;
2358 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref3, false,
2359 "FDE initial location");
2360 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2361 fde->dw_fde_hot_section_end_label,
2362 fde->dw_fde_hot_section_label,
2363 "FDE address range");
2364 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref2, false,
2365 "FDE initial location");
2366 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2367 fde->dw_fde_unlikely_section_end_label,
2368 fde->dw_fde_unlikely_section_label,
2369 "FDE address range");
2371 else
2372 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2373 fde->dw_fde_end, fde->dw_fde_begin,
2374 "FDE address range");
2376 else
2378 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
2379 "FDE initial location");
2380 if (fde->dw_fde_switched_sections)
2382 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2383 fde->dw_fde_hot_section_label,
2384 "FDE initial location");
2385 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2386 fde->dw_fde_hot_section_end_label,
2387 fde->dw_fde_hot_section_label,
2388 "FDE address range");
2389 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2390 fde->dw_fde_unlikely_section_label,
2391 "FDE initial location");
2392 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2393 fde->dw_fde_unlikely_section_end_label,
2394 fde->dw_fde_unlikely_section_label,
2395 "FDE address range");
2397 else
2398 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2399 fde->dw_fde_end, fde->dw_fde_begin,
2400 "FDE address range");
2403 if (augmentation[0])
2405 if (any_lsda_needed)
2407 int size = size_of_encoded_value (lsda_encoding);
2409 if (lsda_encoding == DW_EH_PE_aligned)
2411 int offset = ( 4 /* Length */
2412 + 4 /* CIE offset */
2413 + 2 * size_of_encoded_value (fde_encoding)
2414 + 1 /* Augmentation size */ );
2415 int pad = -offset & (PTR_SIZE - 1);
2417 size += pad;
2418 gcc_assert (size_of_uleb128 (size) == 1);
2421 dw2_asm_output_data_uleb128 (size, "Augmentation size");
2423 if (fde->uses_eh_lsda)
2425 ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
2426 fde->funcdef_number);
2427 dw2_asm_output_encoded_addr_rtx (
2428 lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
2429 false, "Language Specific Data Area");
2431 else
2433 if (lsda_encoding == DW_EH_PE_aligned)
2434 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2435 dw2_asm_output_data
2436 (size_of_encoded_value (lsda_encoding), 0,
2437 "Language Specific Data Area (none)");
2440 else
2441 dw2_asm_output_data_uleb128 (0, "Augmentation size");
2444 /* Loop through the Call Frame Instructions associated with
2445 this FDE. */
2446 fde->dw_fde_current_label = fde->dw_fde_begin;
2447 for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
2448 output_cfi (cfi, fde, for_eh);
2450 /* Pad the FDE out to an address sized boundary. */
2451 ASM_OUTPUT_ALIGN (asm_out_file,
2452 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
2453 ASM_OUTPUT_LABEL (asm_out_file, l2);
2456 if (for_eh && targetm.terminate_dw2_eh_frame_info)
2457 dw2_asm_output_data (4, 0, "End of Table");
2458 #ifdef MIPS_DEBUGGING_INFO
2459 /* Work around Irix 6 assembler bug whereby labels at the end of a section
2460 get a value of 0. Putting .align 0 after the label fixes it. */
2461 ASM_OUTPUT_ALIGN (asm_out_file, 0);
2462 #endif
2464 /* Turn off app to make assembly quicker. */
2465 if (flag_debug_asm)
2466 app_disable ();
2469 /* Output a marker (i.e. a label) for the beginning of a function, before
2470 the prologue. */
2472 void
2473 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
2474 const char *file ATTRIBUTE_UNUSED)
2476 char label[MAX_ARTIFICIAL_LABEL_BYTES];
2477 char * dup_label;
2478 dw_fde_ref fde;
2480 current_function_func_begin_label = NULL;
2482 #ifdef TARGET_UNWIND_INFO
2483 /* ??? current_function_func_begin_label is also used by except.c
2484 for call-site information. We must emit this label if it might
2485 be used. */
2486 if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
2487 && ! dwarf2out_do_frame ())
2488 return;
2489 #else
2490 if (! dwarf2out_do_frame ())
2491 return;
2492 #endif
2494 function_section (current_function_decl);
2495 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
2496 current_function_funcdef_no);
2497 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
2498 current_function_funcdef_no);
2499 dup_label = xstrdup (label);
2500 current_function_func_begin_label = dup_label;
2502 #ifdef TARGET_UNWIND_INFO
2503 /* We can elide the fde allocation if we're not emitting debug info. */
2504 if (! dwarf2out_do_frame ())
2505 return;
2506 #endif
2508 /* Expand the fde table if necessary. */
2509 if (fde_table_in_use == fde_table_allocated)
2511 fde_table_allocated += FDE_TABLE_INCREMENT;
2512 fde_table = ggc_realloc (fde_table,
2513 fde_table_allocated * sizeof (dw_fde_node));
2514 memset (fde_table + fde_table_in_use, 0,
2515 FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2518 /* Record the FDE associated with this function. */
2519 current_funcdef_fde = fde_table_in_use;
2521 /* Add the new FDE at the end of the fde_table. */
2522 fde = &fde_table[fde_table_in_use++];
2523 fde->decl = current_function_decl;
2524 fde->dw_fde_begin = dup_label;
2525 fde->dw_fde_current_label = NULL;
2526 fde->dw_fde_hot_section_label = NULL;
2527 fde->dw_fde_hot_section_end_label = NULL;
2528 fde->dw_fde_unlikely_section_label = NULL;
2529 fde->dw_fde_unlikely_section_end_label = NULL;
2530 fde->dw_fde_switched_sections = false;
2531 fde->dw_fde_end = NULL;
2532 fde->dw_fde_cfi = NULL;
2533 fde->funcdef_number = current_function_funcdef_no;
2534 fde->nothrow = TREE_NOTHROW (current_function_decl);
2535 fde->uses_eh_lsda = cfun->uses_eh_lsda;
2536 fde->all_throwers_are_sibcalls = cfun->all_throwers_are_sibcalls;
2538 args_size = old_args_size = 0;
2540 /* We only want to output line number information for the genuine dwarf2
2541 prologue case, not the eh frame case. */
2542 #ifdef DWARF2_DEBUGGING_INFO
2543 if (file)
2544 dwarf2out_source_line (line, file);
2545 #endif
2548 /* Output a marker (i.e. a label) for the absolute end of the generated code
2549 for a function definition. This gets called *after* the epilogue code has
2550 been generated. */
2552 void
2553 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
2554 const char *file ATTRIBUTE_UNUSED)
2556 dw_fde_ref fde;
2557 char label[MAX_ARTIFICIAL_LABEL_BYTES];
2559 /* Output a label to mark the endpoint of the code generated for this
2560 function. */
2561 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
2562 current_function_funcdef_no);
2563 ASM_OUTPUT_LABEL (asm_out_file, label);
2564 fde = &fde_table[fde_table_in_use - 1];
2565 fde->dw_fde_end = xstrdup (label);
2568 void
2569 dwarf2out_frame_init (void)
2571 /* Allocate the initial hunk of the fde_table. */
2572 fde_table = ggc_alloc_cleared (FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2573 fde_table_allocated = FDE_TABLE_INCREMENT;
2574 fde_table_in_use = 0;
2576 /* Generate the CFA instructions common to all FDE's. Do it now for the
2577 sake of lookup_cfa. */
2579 #ifdef DWARF2_UNWIND_INFO
2580 /* On entry, the Canonical Frame Address is at SP. */
2581 dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
2582 initial_return_save (INCOMING_RETURN_ADDR_RTX);
2583 #endif
2586 void
2587 dwarf2out_frame_finish (void)
2589 /* Output call frame information. */
2590 if (write_symbols == DWARF2_DEBUG
2591 || write_symbols == VMS_AND_DWARF2_DEBUG
2592 #ifdef DWARF2_FRAME_INFO
2593 || DWARF2_FRAME_INFO
2594 #endif
2596 output_call_frame_info (0);
2598 #ifndef TARGET_UNWIND_INFO
2599 /* Output another copy for the unwinder. */
2600 if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
2601 output_call_frame_info (1);
2602 #endif
2604 #endif
2606 /* And now, the subset of the debugging information support code necessary
2607 for emitting location expressions. */
2609 /* We need some way to distinguish DW_OP_addr with a direct symbol
2610 relocation from DW_OP_addr with a dtp-relative symbol relocation. */
2611 #define INTERNAL_DW_OP_tls_addr (0x100 + DW_OP_addr)
2614 typedef struct dw_val_struct *dw_val_ref;
2615 typedef struct die_struct *dw_die_ref;
2616 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
2617 typedef struct dw_loc_list_struct *dw_loc_list_ref;
2619 /* Each DIE may have a series of attribute/value pairs. Values
2620 can take on several forms. The forms that are used in this
2621 implementation are listed below. */
2623 enum dw_val_class
2625 dw_val_class_addr,
2626 dw_val_class_offset,
2627 dw_val_class_loc,
2628 dw_val_class_loc_list,
2629 dw_val_class_range_list,
2630 dw_val_class_const,
2631 dw_val_class_unsigned_const,
2632 dw_val_class_long_long,
2633 dw_val_class_vec,
2634 dw_val_class_flag,
2635 dw_val_class_die_ref,
2636 dw_val_class_fde_ref,
2637 dw_val_class_lbl_id,
2638 dw_val_class_lbl_offset,
2639 dw_val_class_str
2642 /* Describe a double word constant value. */
2643 /* ??? Every instance of long_long in the code really means CONST_DOUBLE. */
2645 typedef struct dw_long_long_struct GTY(())
2647 unsigned long hi;
2648 unsigned long low;
2650 dw_long_long_const;
2652 /* Describe a floating point constant value, or a vector constant value. */
2654 typedef struct dw_vec_struct GTY(())
2656 unsigned char * GTY((length ("%h.length"))) array;
2657 unsigned length;
2658 unsigned elt_size;
2660 dw_vec_const;
2662 /* The dw_val_node describes an attribute's value, as it is
2663 represented internally. */
2665 typedef struct dw_val_struct GTY(())
2667 enum dw_val_class val_class;
2668 union dw_val_struct_union
2670 rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
2671 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
2672 dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
2673 dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
2674 HOST_WIDE_INT GTY ((default)) val_int;
2675 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
2676 dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
2677 dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
2678 struct dw_val_die_union
2680 dw_die_ref die;
2681 int external;
2682 } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
2683 unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
2684 struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
2685 char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
2686 unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
2688 GTY ((desc ("%1.val_class"))) v;
2690 dw_val_node;
2692 /* Locations in memory are described using a sequence of stack machine
2693 operations. */
2695 typedef struct dw_loc_descr_struct GTY(())
2697 dw_loc_descr_ref dw_loc_next;
2698 enum dwarf_location_atom dw_loc_opc;
2699 dw_val_node dw_loc_oprnd1;
2700 dw_val_node dw_loc_oprnd2;
2701 int dw_loc_addr;
2703 dw_loc_descr_node;
2705 /* Location lists are ranges + location descriptions for that range,
2706 so you can track variables that are in different places over
2707 their entire life. */
2708 typedef struct dw_loc_list_struct GTY(())
2710 dw_loc_list_ref dw_loc_next;
2711 const char *begin; /* Label for begin address of range */
2712 const char *end; /* Label for end address of range */
2713 char *ll_symbol; /* Label for beginning of location list.
2714 Only on head of list */
2715 const char *section; /* Section this loclist is relative to */
2716 dw_loc_descr_ref expr;
2717 } dw_loc_list_node;
2719 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2721 static const char *dwarf_stack_op_name (unsigned);
2722 static dw_loc_descr_ref new_loc_descr (enum dwarf_location_atom,
2723 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2724 static void add_loc_descr (dw_loc_descr_ref *, dw_loc_descr_ref);
2725 static unsigned long size_of_loc_descr (dw_loc_descr_ref);
2726 static unsigned long size_of_locs (dw_loc_descr_ref);
2727 static void output_loc_operands (dw_loc_descr_ref);
2728 static void output_loc_sequence (dw_loc_descr_ref);
2730 /* Convert a DWARF stack opcode into its string name. */
2732 static const char *
2733 dwarf_stack_op_name (unsigned int op)
2735 switch (op)
2737 case DW_OP_addr:
2738 case INTERNAL_DW_OP_tls_addr:
2739 return "DW_OP_addr";
2740 case DW_OP_deref:
2741 return "DW_OP_deref";
2742 case DW_OP_const1u:
2743 return "DW_OP_const1u";
2744 case DW_OP_const1s:
2745 return "DW_OP_const1s";
2746 case DW_OP_const2u:
2747 return "DW_OP_const2u";
2748 case DW_OP_const2s:
2749 return "DW_OP_const2s";
2750 case DW_OP_const4u:
2751 return "DW_OP_const4u";
2752 case DW_OP_const4s:
2753 return "DW_OP_const4s";
2754 case DW_OP_const8u:
2755 return "DW_OP_const8u";
2756 case DW_OP_const8s:
2757 return "DW_OP_const8s";
2758 case DW_OP_constu:
2759 return "DW_OP_constu";
2760 case DW_OP_consts:
2761 return "DW_OP_consts";
2762 case DW_OP_dup:
2763 return "DW_OP_dup";
2764 case DW_OP_drop:
2765 return "DW_OP_drop";
2766 case DW_OP_over:
2767 return "DW_OP_over";
2768 case DW_OP_pick:
2769 return "DW_OP_pick";
2770 case DW_OP_swap:
2771 return "DW_OP_swap";
2772 case DW_OP_rot:
2773 return "DW_OP_rot";
2774 case DW_OP_xderef:
2775 return "DW_OP_xderef";
2776 case DW_OP_abs:
2777 return "DW_OP_abs";
2778 case DW_OP_and:
2779 return "DW_OP_and";
2780 case DW_OP_div:
2781 return "DW_OP_div";
2782 case DW_OP_minus:
2783 return "DW_OP_minus";
2784 case DW_OP_mod:
2785 return "DW_OP_mod";
2786 case DW_OP_mul:
2787 return "DW_OP_mul";
2788 case DW_OP_neg:
2789 return "DW_OP_neg";
2790 case DW_OP_not:
2791 return "DW_OP_not";
2792 case DW_OP_or:
2793 return "DW_OP_or";
2794 case DW_OP_plus:
2795 return "DW_OP_plus";
2796 case DW_OP_plus_uconst:
2797 return "DW_OP_plus_uconst";
2798 case DW_OP_shl:
2799 return "DW_OP_shl";
2800 case DW_OP_shr:
2801 return "DW_OP_shr";
2802 case DW_OP_shra:
2803 return "DW_OP_shra";
2804 case DW_OP_xor:
2805 return "DW_OP_xor";
2806 case DW_OP_bra:
2807 return "DW_OP_bra";
2808 case DW_OP_eq:
2809 return "DW_OP_eq";
2810 case DW_OP_ge:
2811 return "DW_OP_ge";
2812 case DW_OP_gt:
2813 return "DW_OP_gt";
2814 case DW_OP_le:
2815 return "DW_OP_le";
2816 case DW_OP_lt:
2817 return "DW_OP_lt";
2818 case DW_OP_ne:
2819 return "DW_OP_ne";
2820 case DW_OP_skip:
2821 return "DW_OP_skip";
2822 case DW_OP_lit0:
2823 return "DW_OP_lit0";
2824 case DW_OP_lit1:
2825 return "DW_OP_lit1";
2826 case DW_OP_lit2:
2827 return "DW_OP_lit2";
2828 case DW_OP_lit3:
2829 return "DW_OP_lit3";
2830 case DW_OP_lit4:
2831 return "DW_OP_lit4";
2832 case DW_OP_lit5:
2833 return "DW_OP_lit5";
2834 case DW_OP_lit6:
2835 return "DW_OP_lit6";
2836 case DW_OP_lit7:
2837 return "DW_OP_lit7";
2838 case DW_OP_lit8:
2839 return "DW_OP_lit8";
2840 case DW_OP_lit9:
2841 return "DW_OP_lit9";
2842 case DW_OP_lit10:
2843 return "DW_OP_lit10";
2844 case DW_OP_lit11:
2845 return "DW_OP_lit11";
2846 case DW_OP_lit12:
2847 return "DW_OP_lit12";
2848 case DW_OP_lit13:
2849 return "DW_OP_lit13";
2850 case DW_OP_lit14:
2851 return "DW_OP_lit14";
2852 case DW_OP_lit15:
2853 return "DW_OP_lit15";
2854 case DW_OP_lit16:
2855 return "DW_OP_lit16";
2856 case DW_OP_lit17:
2857 return "DW_OP_lit17";
2858 case DW_OP_lit18:
2859 return "DW_OP_lit18";
2860 case DW_OP_lit19:
2861 return "DW_OP_lit19";
2862 case DW_OP_lit20:
2863 return "DW_OP_lit20";
2864 case DW_OP_lit21:
2865 return "DW_OP_lit21";
2866 case DW_OP_lit22:
2867 return "DW_OP_lit22";
2868 case DW_OP_lit23:
2869 return "DW_OP_lit23";
2870 case DW_OP_lit24:
2871 return "DW_OP_lit24";
2872 case DW_OP_lit25:
2873 return "DW_OP_lit25";
2874 case DW_OP_lit26:
2875 return "DW_OP_lit26";
2876 case DW_OP_lit27:
2877 return "DW_OP_lit27";
2878 case DW_OP_lit28:
2879 return "DW_OP_lit28";
2880 case DW_OP_lit29:
2881 return "DW_OP_lit29";
2882 case DW_OP_lit30:
2883 return "DW_OP_lit30";
2884 case DW_OP_lit31:
2885 return "DW_OP_lit31";
2886 case DW_OP_reg0:
2887 return "DW_OP_reg0";
2888 case DW_OP_reg1:
2889 return "DW_OP_reg1";
2890 case DW_OP_reg2:
2891 return "DW_OP_reg2";
2892 case DW_OP_reg3:
2893 return "DW_OP_reg3";
2894 case DW_OP_reg4:
2895 return "DW_OP_reg4";
2896 case DW_OP_reg5:
2897 return "DW_OP_reg5";
2898 case DW_OP_reg6:
2899 return "DW_OP_reg6";
2900 case DW_OP_reg7:
2901 return "DW_OP_reg7";
2902 case DW_OP_reg8:
2903 return "DW_OP_reg8";
2904 case DW_OP_reg9:
2905 return "DW_OP_reg9";
2906 case DW_OP_reg10:
2907 return "DW_OP_reg10";
2908 case DW_OP_reg11:
2909 return "DW_OP_reg11";
2910 case DW_OP_reg12:
2911 return "DW_OP_reg12";
2912 case DW_OP_reg13:
2913 return "DW_OP_reg13";
2914 case DW_OP_reg14:
2915 return "DW_OP_reg14";
2916 case DW_OP_reg15:
2917 return "DW_OP_reg15";
2918 case DW_OP_reg16:
2919 return "DW_OP_reg16";
2920 case DW_OP_reg17:
2921 return "DW_OP_reg17";
2922 case DW_OP_reg18:
2923 return "DW_OP_reg18";
2924 case DW_OP_reg19:
2925 return "DW_OP_reg19";
2926 case DW_OP_reg20:
2927 return "DW_OP_reg20";
2928 case DW_OP_reg21:
2929 return "DW_OP_reg21";
2930 case DW_OP_reg22:
2931 return "DW_OP_reg22";
2932 case DW_OP_reg23:
2933 return "DW_OP_reg23";
2934 case DW_OP_reg24:
2935 return "DW_OP_reg24";
2936 case DW_OP_reg25:
2937 return "DW_OP_reg25";
2938 case DW_OP_reg26:
2939 return "DW_OP_reg26";
2940 case DW_OP_reg27:
2941 return "DW_OP_reg27";
2942 case DW_OP_reg28:
2943 return "DW_OP_reg28";
2944 case DW_OP_reg29:
2945 return "DW_OP_reg29";
2946 case DW_OP_reg30:
2947 return "DW_OP_reg30";
2948 case DW_OP_reg31:
2949 return "DW_OP_reg31";
2950 case DW_OP_breg0:
2951 return "DW_OP_breg0";
2952 case DW_OP_breg1:
2953 return "DW_OP_breg1";
2954 case DW_OP_breg2:
2955 return "DW_OP_breg2";
2956 case DW_OP_breg3:
2957 return "DW_OP_breg3";
2958 case DW_OP_breg4:
2959 return "DW_OP_breg4";
2960 case DW_OP_breg5:
2961 return "DW_OP_breg5";
2962 case DW_OP_breg6:
2963 return "DW_OP_breg6";
2964 case DW_OP_breg7:
2965 return "DW_OP_breg7";
2966 case DW_OP_breg8:
2967 return "DW_OP_breg8";
2968 case DW_OP_breg9:
2969 return "DW_OP_breg9";
2970 case DW_OP_breg10:
2971 return "DW_OP_breg10";
2972 case DW_OP_breg11:
2973 return "DW_OP_breg11";
2974 case DW_OP_breg12:
2975 return "DW_OP_breg12";
2976 case DW_OP_breg13:
2977 return "DW_OP_breg13";
2978 case DW_OP_breg14:
2979 return "DW_OP_breg14";
2980 case DW_OP_breg15:
2981 return "DW_OP_breg15";
2982 case DW_OP_breg16:
2983 return "DW_OP_breg16";
2984 case DW_OP_breg17:
2985 return "DW_OP_breg17";
2986 case DW_OP_breg18:
2987 return "DW_OP_breg18";
2988 case DW_OP_breg19:
2989 return "DW_OP_breg19";
2990 case DW_OP_breg20:
2991 return "DW_OP_breg20";
2992 case DW_OP_breg21:
2993 return "DW_OP_breg21";
2994 case DW_OP_breg22:
2995 return "DW_OP_breg22";
2996 case DW_OP_breg23:
2997 return "DW_OP_breg23";
2998 case DW_OP_breg24:
2999 return "DW_OP_breg24";
3000 case DW_OP_breg25:
3001 return "DW_OP_breg25";
3002 case DW_OP_breg26:
3003 return "DW_OP_breg26";
3004 case DW_OP_breg27:
3005 return "DW_OP_breg27";
3006 case DW_OP_breg28:
3007 return "DW_OP_breg28";
3008 case DW_OP_breg29:
3009 return "DW_OP_breg29";
3010 case DW_OP_breg30:
3011 return "DW_OP_breg30";
3012 case DW_OP_breg31:
3013 return "DW_OP_breg31";
3014 case DW_OP_regx:
3015 return "DW_OP_regx";
3016 case DW_OP_fbreg:
3017 return "DW_OP_fbreg";
3018 case DW_OP_bregx:
3019 return "DW_OP_bregx";
3020 case DW_OP_piece:
3021 return "DW_OP_piece";
3022 case DW_OP_deref_size:
3023 return "DW_OP_deref_size";
3024 case DW_OP_xderef_size:
3025 return "DW_OP_xderef_size";
3026 case DW_OP_nop:
3027 return "DW_OP_nop";
3028 case DW_OP_push_object_address:
3029 return "DW_OP_push_object_address";
3030 case DW_OP_call2:
3031 return "DW_OP_call2";
3032 case DW_OP_call4:
3033 return "DW_OP_call4";
3034 case DW_OP_call_ref:
3035 return "DW_OP_call_ref";
3036 case DW_OP_GNU_push_tls_address:
3037 return "DW_OP_GNU_push_tls_address";
3038 default:
3039 return "OP_<unknown>";
3043 /* Return a pointer to a newly allocated location description. Location
3044 descriptions are simple expression terms that can be strung
3045 together to form more complicated location (address) descriptions. */
3047 static inline dw_loc_descr_ref
3048 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
3049 unsigned HOST_WIDE_INT oprnd2)
3051 dw_loc_descr_ref descr = ggc_alloc_cleared (sizeof (dw_loc_descr_node));
3053 descr->dw_loc_opc = op;
3054 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
3055 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
3056 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
3057 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
3059 return descr;
3062 /* Add a location description term to a location description expression. */
3064 static inline void
3065 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
3067 dw_loc_descr_ref *d;
3069 /* Find the end of the chain. */
3070 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
3073 *d = descr;
3076 /* Return the size of a location descriptor. */
3078 static unsigned long
3079 size_of_loc_descr (dw_loc_descr_ref loc)
3081 unsigned long size = 1;
3083 switch (loc->dw_loc_opc)
3085 case DW_OP_addr:
3086 case INTERNAL_DW_OP_tls_addr:
3087 size += DWARF2_ADDR_SIZE;
3088 break;
3089 case DW_OP_const1u:
3090 case DW_OP_const1s:
3091 size += 1;
3092 break;
3093 case DW_OP_const2u:
3094 case DW_OP_const2s:
3095 size += 2;
3096 break;
3097 case DW_OP_const4u:
3098 case DW_OP_const4s:
3099 size += 4;
3100 break;
3101 case DW_OP_const8u:
3102 case DW_OP_const8s:
3103 size += 8;
3104 break;
3105 case DW_OP_constu:
3106 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3107 break;
3108 case DW_OP_consts:
3109 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3110 break;
3111 case DW_OP_pick:
3112 size += 1;
3113 break;
3114 case DW_OP_plus_uconst:
3115 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3116 break;
3117 case DW_OP_skip:
3118 case DW_OP_bra:
3119 size += 2;
3120 break;
3121 case DW_OP_breg0:
3122 case DW_OP_breg1:
3123 case DW_OP_breg2:
3124 case DW_OP_breg3:
3125 case DW_OP_breg4:
3126 case DW_OP_breg5:
3127 case DW_OP_breg6:
3128 case DW_OP_breg7:
3129 case DW_OP_breg8:
3130 case DW_OP_breg9:
3131 case DW_OP_breg10:
3132 case DW_OP_breg11:
3133 case DW_OP_breg12:
3134 case DW_OP_breg13:
3135 case DW_OP_breg14:
3136 case DW_OP_breg15:
3137 case DW_OP_breg16:
3138 case DW_OP_breg17:
3139 case DW_OP_breg18:
3140 case DW_OP_breg19:
3141 case DW_OP_breg20:
3142 case DW_OP_breg21:
3143 case DW_OP_breg22:
3144 case DW_OP_breg23:
3145 case DW_OP_breg24:
3146 case DW_OP_breg25:
3147 case DW_OP_breg26:
3148 case DW_OP_breg27:
3149 case DW_OP_breg28:
3150 case DW_OP_breg29:
3151 case DW_OP_breg30:
3152 case DW_OP_breg31:
3153 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3154 break;
3155 case DW_OP_regx:
3156 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3157 break;
3158 case DW_OP_fbreg:
3159 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3160 break;
3161 case DW_OP_bregx:
3162 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3163 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
3164 break;
3165 case DW_OP_piece:
3166 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3167 break;
3168 case DW_OP_deref_size:
3169 case DW_OP_xderef_size:
3170 size += 1;
3171 break;
3172 case DW_OP_call2:
3173 size += 2;
3174 break;
3175 case DW_OP_call4:
3176 size += 4;
3177 break;
3178 case DW_OP_call_ref:
3179 size += DWARF2_ADDR_SIZE;
3180 break;
3181 default:
3182 break;
3185 return size;
3188 /* Return the size of a series of location descriptors. */
3190 static unsigned long
3191 size_of_locs (dw_loc_descr_ref loc)
3193 unsigned long size;
3195 for (size = 0; loc != NULL; loc = loc->dw_loc_next)
3197 loc->dw_loc_addr = size;
3198 size += size_of_loc_descr (loc);
3201 return size;
3204 /* Output location description stack opcode's operands (if any). */
3206 static void
3207 output_loc_operands (dw_loc_descr_ref loc)
3209 dw_val_ref val1 = &loc->dw_loc_oprnd1;
3210 dw_val_ref val2 = &loc->dw_loc_oprnd2;
3212 switch (loc->dw_loc_opc)
3214 #ifdef DWARF2_DEBUGGING_INFO
3215 case DW_OP_addr:
3216 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
3217 break;
3218 case DW_OP_const2u:
3219 case DW_OP_const2s:
3220 dw2_asm_output_data (2, val1->v.val_int, NULL);
3221 break;
3222 case DW_OP_const4u:
3223 case DW_OP_const4s:
3224 dw2_asm_output_data (4, val1->v.val_int, NULL);
3225 break;
3226 case DW_OP_const8u:
3227 case DW_OP_const8s:
3228 gcc_assert (HOST_BITS_PER_LONG >= 64);
3229 dw2_asm_output_data (8, val1->v.val_int, NULL);
3230 break;
3231 case DW_OP_skip:
3232 case DW_OP_bra:
3234 int offset;
3236 gcc_assert (val1->val_class == dw_val_class_loc);
3237 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
3239 dw2_asm_output_data (2, offset, NULL);
3241 break;
3242 #else
3243 case DW_OP_addr:
3244 case DW_OP_const2u:
3245 case DW_OP_const2s:
3246 case DW_OP_const4u:
3247 case DW_OP_const4s:
3248 case DW_OP_const8u:
3249 case DW_OP_const8s:
3250 case DW_OP_skip:
3251 case DW_OP_bra:
3252 /* We currently don't make any attempt to make sure these are
3253 aligned properly like we do for the main unwind info, so
3254 don't support emitting things larger than a byte if we're
3255 only doing unwinding. */
3256 gcc_unreachable ();
3257 #endif
3258 case DW_OP_const1u:
3259 case DW_OP_const1s:
3260 dw2_asm_output_data (1, val1->v.val_int, NULL);
3261 break;
3262 case DW_OP_constu:
3263 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3264 break;
3265 case DW_OP_consts:
3266 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3267 break;
3268 case DW_OP_pick:
3269 dw2_asm_output_data (1, val1->v.val_int, NULL);
3270 break;
3271 case DW_OP_plus_uconst:
3272 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3273 break;
3274 case DW_OP_breg0:
3275 case DW_OP_breg1:
3276 case DW_OP_breg2:
3277 case DW_OP_breg3:
3278 case DW_OP_breg4:
3279 case DW_OP_breg5:
3280 case DW_OP_breg6:
3281 case DW_OP_breg7:
3282 case DW_OP_breg8:
3283 case DW_OP_breg9:
3284 case DW_OP_breg10:
3285 case DW_OP_breg11:
3286 case DW_OP_breg12:
3287 case DW_OP_breg13:
3288 case DW_OP_breg14:
3289 case DW_OP_breg15:
3290 case DW_OP_breg16:
3291 case DW_OP_breg17:
3292 case DW_OP_breg18:
3293 case DW_OP_breg19:
3294 case DW_OP_breg20:
3295 case DW_OP_breg21:
3296 case DW_OP_breg22:
3297 case DW_OP_breg23:
3298 case DW_OP_breg24:
3299 case DW_OP_breg25:
3300 case DW_OP_breg26:
3301 case DW_OP_breg27:
3302 case DW_OP_breg28:
3303 case DW_OP_breg29:
3304 case DW_OP_breg30:
3305 case DW_OP_breg31:
3306 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3307 break;
3308 case DW_OP_regx:
3309 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3310 break;
3311 case DW_OP_fbreg:
3312 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3313 break;
3314 case DW_OP_bregx:
3315 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3316 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
3317 break;
3318 case DW_OP_piece:
3319 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3320 break;
3321 case DW_OP_deref_size:
3322 case DW_OP_xderef_size:
3323 dw2_asm_output_data (1, val1->v.val_int, NULL);
3324 break;
3326 case INTERNAL_DW_OP_tls_addr:
3327 if (targetm.asm_out.output_dwarf_dtprel)
3329 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
3330 DWARF2_ADDR_SIZE,
3331 val1->v.val_addr);
3332 fputc ('\n', asm_out_file);
3334 else
3335 gcc_unreachable ();
3336 break;
3338 default:
3339 /* Other codes have no operands. */
3340 break;
3344 /* Output a sequence of location operations. */
3346 static void
3347 output_loc_sequence (dw_loc_descr_ref loc)
3349 for (; loc != NULL; loc = loc->dw_loc_next)
3351 /* Output the opcode. */
3352 dw2_asm_output_data (1, loc->dw_loc_opc,
3353 "%s", dwarf_stack_op_name (loc->dw_loc_opc));
3355 /* Output the operand(s) (if any). */
3356 output_loc_operands (loc);
3360 /* This routine will generate the correct assembly data for a location
3361 description based on a cfi entry with a complex address. */
3363 static void
3364 output_cfa_loc (dw_cfi_ref cfi)
3366 dw_loc_descr_ref loc;
3367 unsigned long size;
3369 /* Output the size of the block. */
3370 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
3371 size = size_of_locs (loc);
3372 dw2_asm_output_data_uleb128 (size, NULL);
3374 /* Now output the operations themselves. */
3375 output_loc_sequence (loc);
3378 /* This function builds a dwarf location descriptor sequence from
3379 a dw_cfa_location. */
3381 static struct dw_loc_descr_struct *
3382 build_cfa_loc (dw_cfa_location *cfa)
3384 struct dw_loc_descr_struct *head, *tmp;
3386 if (cfa->indirect)
3388 if (cfa->base_offset)
3390 if (cfa->reg <= 31)
3391 head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->base_offset, 0);
3392 else
3393 head = new_loc_descr (DW_OP_bregx, cfa->reg, cfa->base_offset);
3395 else if (cfa->reg <= 31)
3396 head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3397 else
3398 head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3400 head->dw_loc_oprnd1.val_class = dw_val_class_const;
3401 tmp = new_loc_descr (DW_OP_deref, 0, 0);
3402 add_loc_descr (&head, tmp);
3403 if (cfa->offset != 0)
3405 tmp = new_loc_descr (DW_OP_plus_uconst, cfa->offset, 0);
3406 add_loc_descr (&head, tmp);
3409 else
3411 if (cfa->offset == 0)
3412 if (cfa->reg <= 31)
3413 head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3414 else
3415 head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3416 else if (cfa->reg <= 31)
3417 head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->offset, 0);
3418 else
3419 head = new_loc_descr (DW_OP_bregx, cfa->reg, cfa->offset);
3422 return head;
3425 /* This function fills in aa dw_cfa_location structure from a dwarf location
3426 descriptor sequence. */
3428 static void
3429 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
3431 struct dw_loc_descr_struct *ptr;
3432 cfa->offset = 0;
3433 cfa->base_offset = 0;
3434 cfa->indirect = 0;
3435 cfa->reg = -1;
3437 for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
3439 enum dwarf_location_atom op = ptr->dw_loc_opc;
3441 switch (op)
3443 case DW_OP_reg0:
3444 case DW_OP_reg1:
3445 case DW_OP_reg2:
3446 case DW_OP_reg3:
3447 case DW_OP_reg4:
3448 case DW_OP_reg5:
3449 case DW_OP_reg6:
3450 case DW_OP_reg7:
3451 case DW_OP_reg8:
3452 case DW_OP_reg9:
3453 case DW_OP_reg10:
3454 case DW_OP_reg11:
3455 case DW_OP_reg12:
3456 case DW_OP_reg13:
3457 case DW_OP_reg14:
3458 case DW_OP_reg15:
3459 case DW_OP_reg16:
3460 case DW_OP_reg17:
3461 case DW_OP_reg18:
3462 case DW_OP_reg19:
3463 case DW_OP_reg20:
3464 case DW_OP_reg21:
3465 case DW_OP_reg22:
3466 case DW_OP_reg23:
3467 case DW_OP_reg24:
3468 case DW_OP_reg25:
3469 case DW_OP_reg26:
3470 case DW_OP_reg27:
3471 case DW_OP_reg28:
3472 case DW_OP_reg29:
3473 case DW_OP_reg30:
3474 case DW_OP_reg31:
3475 cfa->reg = op - DW_OP_reg0;
3476 break;
3477 case DW_OP_regx:
3478 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3479 break;
3480 case DW_OP_breg0:
3481 case DW_OP_breg1:
3482 case DW_OP_breg2:
3483 case DW_OP_breg3:
3484 case DW_OP_breg4:
3485 case DW_OP_breg5:
3486 case DW_OP_breg6:
3487 case DW_OP_breg7:
3488 case DW_OP_breg8:
3489 case DW_OP_breg9:
3490 case DW_OP_breg10:
3491 case DW_OP_breg11:
3492 case DW_OP_breg12:
3493 case DW_OP_breg13:
3494 case DW_OP_breg14:
3495 case DW_OP_breg15:
3496 case DW_OP_breg16:
3497 case DW_OP_breg17:
3498 case DW_OP_breg18:
3499 case DW_OP_breg19:
3500 case DW_OP_breg20:
3501 case DW_OP_breg21:
3502 case DW_OP_breg22:
3503 case DW_OP_breg23:
3504 case DW_OP_breg24:
3505 case DW_OP_breg25:
3506 case DW_OP_breg26:
3507 case DW_OP_breg27:
3508 case DW_OP_breg28:
3509 case DW_OP_breg29:
3510 case DW_OP_breg30:
3511 case DW_OP_breg31:
3512 cfa->reg = op - DW_OP_breg0;
3513 cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
3514 break;
3515 case DW_OP_bregx:
3516 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3517 cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
3518 break;
3519 case DW_OP_deref:
3520 cfa->indirect = 1;
3521 break;
3522 case DW_OP_plus_uconst:
3523 cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
3524 break;
3525 default:
3526 internal_error ("DW_LOC_OP %s not implemented",
3527 dwarf_stack_op_name (ptr->dw_loc_opc));
3531 #endif /* .debug_frame support */
3533 /* And now, the support for symbolic debugging information. */
3534 #ifdef DWARF2_DEBUGGING_INFO
3536 /* .debug_str support. */
3537 static int output_indirect_string (void **, void *);
3539 static void dwarf2out_init (const char *);
3540 static void dwarf2out_finish (const char *);
3541 static void dwarf2out_define (unsigned int, const char *);
3542 static void dwarf2out_undef (unsigned int, const char *);
3543 static void dwarf2out_start_source_file (unsigned, const char *);
3544 static void dwarf2out_end_source_file (unsigned);
3545 static void dwarf2out_begin_block (unsigned, unsigned);
3546 static void dwarf2out_end_block (unsigned, unsigned);
3547 static bool dwarf2out_ignore_block (tree);
3548 static void dwarf2out_global_decl (tree);
3549 static void dwarf2out_type_decl (tree, int);
3550 static void dwarf2out_imported_module_or_decl (tree, tree);
3551 static void dwarf2out_abstract_function (tree);
3552 static void dwarf2out_var_location (rtx);
3553 static void dwarf2out_begin_function (tree);
3554 static void dwarf2out_switch_text_section (void);
3556 /* The debug hooks structure. */
3558 const struct gcc_debug_hooks dwarf2_debug_hooks =
3560 dwarf2out_init,
3561 dwarf2out_finish,
3562 dwarf2out_define,
3563 dwarf2out_undef,
3564 dwarf2out_start_source_file,
3565 dwarf2out_end_source_file,
3566 dwarf2out_begin_block,
3567 dwarf2out_end_block,
3568 dwarf2out_ignore_block,
3569 dwarf2out_source_line,
3570 dwarf2out_begin_prologue,
3571 debug_nothing_int_charstar, /* end_prologue */
3572 dwarf2out_end_epilogue,
3573 dwarf2out_begin_function,
3574 debug_nothing_int, /* end_function */
3575 dwarf2out_decl, /* function_decl */
3576 dwarf2out_global_decl,
3577 dwarf2out_type_decl, /* type_decl */
3578 dwarf2out_imported_module_or_decl,
3579 debug_nothing_tree, /* deferred_inline_function */
3580 /* The DWARF 2 backend tries to reduce debugging bloat by not
3581 emitting the abstract description of inline functions until
3582 something tries to reference them. */
3583 dwarf2out_abstract_function, /* outlining_inline_function */
3584 debug_nothing_rtx, /* label */
3585 debug_nothing_int, /* handle_pch */
3586 dwarf2out_var_location,
3587 dwarf2out_switch_text_section,
3588 1 /* start_end_main_source_file */
3590 #endif
3592 /* NOTE: In the comments in this file, many references are made to
3593 "Debugging Information Entries". This term is abbreviated as `DIE'
3594 throughout the remainder of this file. */
3596 /* An internal representation of the DWARF output is built, and then
3597 walked to generate the DWARF debugging info. The walk of the internal
3598 representation is done after the entire program has been compiled.
3599 The types below are used to describe the internal representation. */
3601 /* Various DIE's use offsets relative to the beginning of the
3602 .debug_info section to refer to each other. */
3604 typedef long int dw_offset;
3606 /* Define typedefs here to avoid circular dependencies. */
3608 typedef struct dw_attr_struct *dw_attr_ref;
3609 typedef struct dw_line_info_struct *dw_line_info_ref;
3610 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
3611 typedef struct pubname_struct *pubname_ref;
3612 typedef struct dw_ranges_struct *dw_ranges_ref;
3614 /* Each entry in the line_info_table maintains the file and
3615 line number associated with the label generated for that
3616 entry. The label gives the PC value associated with
3617 the line number entry. */
3619 typedef struct dw_line_info_struct GTY(())
3621 unsigned long dw_file_num;
3622 unsigned long dw_line_num;
3624 dw_line_info_entry;
3626 /* Line information for functions in separate sections; each one gets its
3627 own sequence. */
3628 typedef struct dw_separate_line_info_struct GTY(())
3630 unsigned long dw_file_num;
3631 unsigned long dw_line_num;
3632 unsigned long function;
3634 dw_separate_line_info_entry;
3636 /* Each DIE attribute has a field specifying the attribute kind,
3637 a link to the next attribute in the chain, and an attribute value.
3638 Attributes are typically linked below the DIE they modify. */
3640 typedef struct dw_attr_struct GTY(())
3642 enum dwarf_attribute dw_attr;
3643 dw_attr_ref dw_attr_next;
3644 dw_val_node dw_attr_val;
3646 dw_attr_node;
3648 /* The Debugging Information Entry (DIE) structure */
3650 typedef struct die_struct GTY(())
3652 enum dwarf_tag die_tag;
3653 char *die_symbol;
3654 dw_attr_ref die_attr;
3655 dw_die_ref die_parent;
3656 dw_die_ref die_child;
3657 dw_die_ref die_sib;
3658 dw_die_ref die_definition; /* ref from a specification to its definition */
3659 dw_offset die_offset;
3660 unsigned long die_abbrev;
3661 int die_mark;
3662 unsigned int decl_id;
3664 die_node;
3666 /* The pubname structure */
3668 typedef struct pubname_struct GTY(())
3670 dw_die_ref die;
3671 char *name;
3673 pubname_entry;
3675 struct dw_ranges_struct GTY(())
3677 int block_num;
3680 /* The limbo die list structure. */
3681 typedef struct limbo_die_struct GTY(())
3683 dw_die_ref die;
3684 tree created_for;
3685 struct limbo_die_struct *next;
3687 limbo_die_node;
3689 /* How to start an assembler comment. */
3690 #ifndef ASM_COMMENT_START
3691 #define ASM_COMMENT_START ";#"
3692 #endif
3694 /* Define a macro which returns nonzero for a TYPE_DECL which was
3695 implicitly generated for a tagged type.
3697 Note that unlike the gcc front end (which generates a NULL named
3698 TYPE_DECL node for each complete tagged type, each array type, and
3699 each function type node created) the g++ front end generates a
3700 _named_ TYPE_DECL node for each tagged type node created.
3701 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3702 generate a DW_TAG_typedef DIE for them. */
3704 #define TYPE_DECL_IS_STUB(decl) \
3705 (DECL_NAME (decl) == NULL_TREE \
3706 || (DECL_ARTIFICIAL (decl) \
3707 && is_tagged_type (TREE_TYPE (decl)) \
3708 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3709 /* This is necessary for stub decls that \
3710 appear in nested inline functions. */ \
3711 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3712 && (decl_ultimate_origin (decl) \
3713 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3715 /* Information concerning the compilation unit's programming
3716 language, and compiler version. */
3718 /* Fixed size portion of the DWARF compilation unit header. */
3719 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3720 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
3722 /* Fixed size portion of public names info. */
3723 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3725 /* Fixed size portion of the address range info. */
3726 #define DWARF_ARANGES_HEADER_SIZE \
3727 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3728 DWARF2_ADDR_SIZE * 2) \
3729 - DWARF_INITIAL_LENGTH_SIZE)
3731 /* Size of padding portion in the address range info. It must be
3732 aligned to twice the pointer size. */
3733 #define DWARF_ARANGES_PAD_SIZE \
3734 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3735 DWARF2_ADDR_SIZE * 2) \
3736 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3738 /* Use assembler line directives if available. */
3739 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3740 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3741 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3742 #else
3743 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3744 #endif
3745 #endif
3747 /* Minimum line offset in a special line info. opcode.
3748 This value was chosen to give a reasonable range of values. */
3749 #define DWARF_LINE_BASE -10
3751 /* First special line opcode - leave room for the standard opcodes. */
3752 #define DWARF_LINE_OPCODE_BASE 10
3754 /* Range of line offsets in a special line info. opcode. */
3755 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3757 /* Flag that indicates the initial value of the is_stmt_start flag.
3758 In the present implementation, we do not mark any lines as
3759 the beginning of a source statement, because that information
3760 is not made available by the GCC front-end. */
3761 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3763 #ifdef DWARF2_DEBUGGING_INFO
3764 /* This location is used by calc_die_sizes() to keep track
3765 the offset of each DIE within the .debug_info section. */
3766 static unsigned long next_die_offset;
3767 #endif
3769 /* Record the root of the DIE's built for the current compilation unit. */
3770 static GTY(()) dw_die_ref comp_unit_die;
3772 /* A list of DIEs with a NULL parent waiting to be relocated. */
3773 static GTY(()) limbo_die_node *limbo_die_list;
3775 /* Filenames referenced by this compilation unit. */
3776 static GTY(()) varray_type file_table;
3777 static GTY(()) varray_type file_table_emitted;
3778 static GTY(()) size_t file_table_last_lookup_index;
3780 /* A hash table of references to DIE's that describe declarations.
3781 The key is a DECL_UID() which is a unique number identifying each decl. */
3782 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
3784 /* Node of the variable location list. */
3785 struct var_loc_node GTY ((chain_next ("%h.next")))
3787 rtx GTY (()) var_loc_note;
3788 const char * GTY (()) label;
3789 const char * GTY (()) section_label;
3790 struct var_loc_node * GTY (()) next;
3793 /* Variable location list. */
3794 struct var_loc_list_def GTY (())
3796 struct var_loc_node * GTY (()) first;
3798 /* Do not mark the last element of the chained list because
3799 it is marked through the chain. */
3800 struct var_loc_node * GTY ((skip ("%h"))) last;
3802 /* DECL_UID of the variable decl. */
3803 unsigned int decl_id;
3805 typedef struct var_loc_list_def var_loc_list;
3808 /* Table of decl location linked lists. */
3809 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
3811 /* A pointer to the base of a list of references to DIE's that
3812 are uniquely identified by their tag, presence/absence of
3813 children DIE's, and list of attribute/value pairs. */
3814 static GTY((length ("abbrev_die_table_allocated")))
3815 dw_die_ref *abbrev_die_table;
3817 /* Number of elements currently allocated for abbrev_die_table. */
3818 static GTY(()) unsigned abbrev_die_table_allocated;
3820 /* Number of elements in type_die_table currently in use. */
3821 static GTY(()) unsigned abbrev_die_table_in_use;
3823 /* Size (in elements) of increments by which we may expand the
3824 abbrev_die_table. */
3825 #define ABBREV_DIE_TABLE_INCREMENT 256
3827 /* A pointer to the base of a table that contains line information
3828 for each source code line in .text in the compilation unit. */
3829 static GTY((length ("line_info_table_allocated")))
3830 dw_line_info_ref line_info_table;
3832 /* Number of elements currently allocated for line_info_table. */
3833 static GTY(()) unsigned line_info_table_allocated;
3835 /* Number of elements in line_info_table currently in use. */
3836 static GTY(()) unsigned line_info_table_in_use;
3838 /* True if the compilation unit contains more than one .text section. */
3839 static GTY(()) bool have_switched_text_section = false;
3841 /* A pointer to the base of a table that contains line information
3842 for each source code line outside of .text in the compilation unit. */
3843 static GTY ((length ("separate_line_info_table_allocated")))
3844 dw_separate_line_info_ref separate_line_info_table;
3846 /* Number of elements currently allocated for separate_line_info_table. */
3847 static GTY(()) unsigned separate_line_info_table_allocated;
3849 /* Number of elements in separate_line_info_table currently in use. */
3850 static GTY(()) unsigned separate_line_info_table_in_use;
3852 /* Size (in elements) of increments by which we may expand the
3853 line_info_table. */
3854 #define LINE_INFO_TABLE_INCREMENT 1024
3856 /* A pointer to the base of a table that contains a list of publicly
3857 accessible names. */
3858 static GTY ((length ("pubname_table_allocated"))) pubname_ref pubname_table;
3860 /* Number of elements currently allocated for pubname_table. */
3861 static GTY(()) unsigned pubname_table_allocated;
3863 /* Number of elements in pubname_table currently in use. */
3864 static GTY(()) unsigned pubname_table_in_use;
3866 /* Size (in elements) of increments by which we may expand the
3867 pubname_table. */
3868 #define PUBNAME_TABLE_INCREMENT 64
3870 /* Array of dies for which we should generate .debug_arange info. */
3871 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
3873 /* Number of elements currently allocated for arange_table. */
3874 static GTY(()) unsigned arange_table_allocated;
3876 /* Number of elements in arange_table currently in use. */
3877 static GTY(()) unsigned arange_table_in_use;
3879 /* Size (in elements) of increments by which we may expand the
3880 arange_table. */
3881 #define ARANGE_TABLE_INCREMENT 64
3883 /* Array of dies for which we should generate .debug_ranges info. */
3884 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
3886 /* Number of elements currently allocated for ranges_table. */
3887 static GTY(()) unsigned ranges_table_allocated;
3889 /* Number of elements in ranges_table currently in use. */
3890 static GTY(()) unsigned ranges_table_in_use;
3892 /* Size (in elements) of increments by which we may expand the
3893 ranges_table. */
3894 #define RANGES_TABLE_INCREMENT 64
3896 /* Whether we have location lists that need outputting */
3897 static GTY(()) unsigned have_location_lists;
3899 /* Unique label counter. */
3900 static GTY(()) unsigned int loclabel_num;
3902 #ifdef DWARF2_DEBUGGING_INFO
3903 /* Record whether the function being analyzed contains inlined functions. */
3904 static int current_function_has_inlines;
3905 #endif
3906 #if 0 && defined (MIPS_DEBUGGING_INFO)
3907 static int comp_unit_has_inlines;
3908 #endif
3910 /* Number of file tables emitted in maybe_emit_file(). */
3911 static GTY(()) int emitcount = 0;
3913 /* Number of internal labels generated by gen_internal_sym(). */
3914 static GTY(()) int label_num;
3916 #ifdef DWARF2_DEBUGGING_INFO
3918 /* Offset from the "steady-state frame pointer" to the CFA,
3919 within the current function. */
3920 static HOST_WIDE_INT frame_pointer_cfa_offset;
3922 /* Forward declarations for functions defined in this file. */
3924 static int is_pseudo_reg (rtx);
3925 static tree type_main_variant (tree);
3926 static int is_tagged_type (tree);
3927 static const char *dwarf_tag_name (unsigned);
3928 static const char *dwarf_attr_name (unsigned);
3929 static const char *dwarf_form_name (unsigned);
3930 static tree decl_ultimate_origin (tree);
3931 static tree block_ultimate_origin (tree);
3932 static tree decl_class_context (tree);
3933 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
3934 static inline enum dw_val_class AT_class (dw_attr_ref);
3935 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3936 static inline unsigned AT_flag (dw_attr_ref);
3937 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3938 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3939 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3940 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3941 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
3942 unsigned long);
3943 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3944 unsigned int, unsigned char *);
3945 static hashval_t debug_str_do_hash (const void *);
3946 static int debug_str_eq (const void *, const void *);
3947 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3948 static inline const char *AT_string (dw_attr_ref);
3949 static int AT_string_form (dw_attr_ref);
3950 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3951 static void add_AT_specification (dw_die_ref, dw_die_ref);
3952 static inline dw_die_ref AT_ref (dw_attr_ref);
3953 static inline int AT_ref_external (dw_attr_ref);
3954 static inline void set_AT_ref_external (dw_attr_ref, int);
3955 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3956 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3957 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3958 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3959 dw_loc_list_ref);
3960 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3961 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
3962 static inline rtx AT_addr (dw_attr_ref);
3963 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3964 static void add_AT_lbl_offset (dw_die_ref, enum dwarf_attribute, const char *);
3965 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3966 unsigned HOST_WIDE_INT);
3967 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3968 unsigned long);
3969 static inline const char *AT_lbl (dw_attr_ref);
3970 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3971 static const char *get_AT_low_pc (dw_die_ref);
3972 static const char *get_AT_hi_pc (dw_die_ref);
3973 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3974 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3975 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3976 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3977 static bool is_c_family (void);
3978 static bool is_cxx (void);
3979 static bool is_java (void);
3980 static bool is_fortran (void);
3981 static bool is_ada (void);
3982 static void remove_AT (dw_die_ref, enum dwarf_attribute);
3983 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3984 static inline void free_die (dw_die_ref);
3985 static void remove_children (dw_die_ref);
3986 static void add_child_die (dw_die_ref, dw_die_ref);
3987 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3988 static dw_die_ref lookup_type_die (tree);
3989 static void equate_type_number_to_die (tree, dw_die_ref);
3990 static hashval_t decl_die_table_hash (const void *);
3991 static int decl_die_table_eq (const void *, const void *);
3992 static dw_die_ref lookup_decl_die (tree);
3993 static hashval_t decl_loc_table_hash (const void *);
3994 static int decl_loc_table_eq (const void *, const void *);
3995 static var_loc_list *lookup_decl_loc (tree);
3996 static void equate_decl_number_to_die (tree, dw_die_ref);
3997 static void add_var_loc_to_decl (tree, struct var_loc_node *);
3998 static void print_spaces (FILE *);
3999 static void print_die (dw_die_ref, FILE *);
4000 static void print_dwarf_line_table (FILE *);
4001 static void reverse_die_lists (dw_die_ref);
4002 static void reverse_all_dies (dw_die_ref);
4003 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
4004 static dw_die_ref pop_compile_unit (dw_die_ref);
4005 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
4006 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
4007 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
4008 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
4009 static int same_dw_val_p (dw_val_node *, dw_val_node *, int *);
4010 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
4011 static int same_die_p (dw_die_ref, dw_die_ref, int *);
4012 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
4013 static void compute_section_prefix (dw_die_ref);
4014 static int is_type_die (dw_die_ref);
4015 static int is_comdat_die (dw_die_ref);
4016 static int is_symbol_die (dw_die_ref);
4017 static void assign_symbol_names (dw_die_ref);
4018 static void break_out_includes (dw_die_ref);
4019 static hashval_t htab_cu_hash (const void *);
4020 static int htab_cu_eq (const void *, const void *);
4021 static void htab_cu_del (void *);
4022 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
4023 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
4024 static void add_sibling_attributes (dw_die_ref);
4025 static void build_abbrev_table (dw_die_ref);
4026 static void output_location_lists (dw_die_ref);
4027 static int constant_size (long unsigned);
4028 static unsigned long size_of_die (dw_die_ref);
4029 static void calc_die_sizes (dw_die_ref);
4030 static void mark_dies (dw_die_ref);
4031 static void unmark_dies (dw_die_ref);
4032 static void unmark_all_dies (dw_die_ref);
4033 static unsigned long size_of_pubnames (void);
4034 static unsigned long size_of_aranges (void);
4035 static enum dwarf_form value_format (dw_attr_ref);
4036 static void output_value_format (dw_attr_ref);
4037 static void output_abbrev_section (void);
4038 static void output_die_symbol (dw_die_ref);
4039 static void output_die (dw_die_ref);
4040 static void output_compilation_unit_header (void);
4041 static void output_comp_unit (dw_die_ref, int);
4042 static const char *dwarf2_name (tree, int);
4043 static void add_pubname (tree, dw_die_ref);
4044 static void output_pubnames (void);
4045 static void add_arange (tree, dw_die_ref);
4046 static void output_aranges (void);
4047 static unsigned int add_ranges (tree);
4048 static void output_ranges (void);
4049 static void output_line_info (void);
4050 static void output_file_names (void);
4051 static dw_die_ref base_type_die (tree);
4052 static tree root_type (tree);
4053 static int is_base_type (tree);
4054 static bool is_subrange_type (tree);
4055 static dw_die_ref subrange_type_die (tree, dw_die_ref);
4056 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
4057 static int type_is_enum (tree);
4058 static unsigned int dbx_reg_number (rtx);
4059 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
4060 static dw_loc_descr_ref reg_loc_descriptor (rtx);
4061 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int);
4062 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx);
4063 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4064 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT);
4065 static int is_based_loc (rtx);
4066 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode);
4067 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx);
4068 static dw_loc_descr_ref loc_descriptor (rtx);
4069 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
4070 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
4071 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
4072 static tree field_type (tree);
4073 static unsigned int simple_type_align_in_bits (tree);
4074 static unsigned int simple_decl_align_in_bits (tree);
4075 static unsigned HOST_WIDE_INT simple_type_size_in_bits (tree);
4076 static HOST_WIDE_INT field_byte_offset (tree);
4077 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
4078 dw_loc_descr_ref);
4079 static void add_data_member_location_attribute (dw_die_ref, tree);
4080 static void add_const_value_attribute (dw_die_ref, rtx);
4081 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
4082 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4083 static void insert_float (rtx, unsigned char *);
4084 static rtx rtl_for_decl_location (tree);
4085 static void add_location_or_const_value_attribute (dw_die_ref, tree,
4086 enum dwarf_attribute);
4087 static void tree_add_const_value_attribute (dw_die_ref, tree);
4088 static void add_name_attribute (dw_die_ref, const char *);
4089 static void add_comp_dir_attribute (dw_die_ref);
4090 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
4091 static void add_subscript_info (dw_die_ref, tree);
4092 static void add_byte_size_attribute (dw_die_ref, tree);
4093 static void add_bit_offset_attribute (dw_die_ref, tree);
4094 static void add_bit_size_attribute (dw_die_ref, tree);
4095 static void add_prototyped_attribute (dw_die_ref, tree);
4096 static void add_abstract_origin_attribute (dw_die_ref, tree);
4097 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
4098 static void add_src_coords_attributes (dw_die_ref, tree);
4099 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
4100 static void push_decl_scope (tree);
4101 static void pop_decl_scope (void);
4102 static dw_die_ref scope_die_for (tree, dw_die_ref);
4103 static inline int local_scope_p (dw_die_ref);
4104 static inline int class_or_namespace_scope_p (dw_die_ref);
4105 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
4106 static void add_calling_convention_attribute (dw_die_ref, tree);
4107 static const char *type_tag (tree);
4108 static tree member_declared_type (tree);
4109 #if 0
4110 static const char *decl_start_label (tree);
4111 #endif
4112 static void gen_array_type_die (tree, dw_die_ref);
4113 #if 0
4114 static void gen_entry_point_die (tree, dw_die_ref);
4115 #endif
4116 static void gen_inlined_enumeration_type_die (tree, dw_die_ref);
4117 static void gen_inlined_structure_type_die (tree, dw_die_ref);
4118 static void gen_inlined_union_type_die (tree, dw_die_ref);
4119 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
4120 static dw_die_ref gen_formal_parameter_die (tree, dw_die_ref);
4121 static void gen_unspecified_parameters_die (tree, dw_die_ref);
4122 static void gen_formal_types_die (tree, dw_die_ref);
4123 static void gen_subprogram_die (tree, dw_die_ref);
4124 static void gen_variable_die (tree, dw_die_ref);
4125 static void gen_label_die (tree, dw_die_ref);
4126 static void gen_lexical_block_die (tree, dw_die_ref, int);
4127 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
4128 static void gen_field_die (tree, dw_die_ref);
4129 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
4130 static dw_die_ref gen_compile_unit_die (const char *);
4131 static void gen_string_type_die (tree, dw_die_ref);
4132 static void gen_inheritance_die (tree, tree, dw_die_ref);
4133 static void gen_member_die (tree, dw_die_ref);
4134 static void gen_struct_or_union_type_die (tree, dw_die_ref);
4135 static void gen_subroutine_type_die (tree, dw_die_ref);
4136 static void gen_typedef_die (tree, dw_die_ref);
4137 static void gen_type_die (tree, dw_die_ref);
4138 static void gen_tagged_type_instantiation_die (tree, dw_die_ref);
4139 static void gen_block_die (tree, dw_die_ref, int);
4140 static void decls_for_scope (tree, dw_die_ref, int);
4141 static int is_redundant_typedef (tree);
4142 static void gen_namespace_die (tree);
4143 static void gen_decl_die (tree, dw_die_ref);
4144 static dw_die_ref force_decl_die (tree);
4145 static dw_die_ref force_type_die (tree);
4146 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
4147 static void declare_in_namespace (tree, dw_die_ref);
4148 static unsigned lookup_filename (const char *);
4149 static void init_file_table (void);
4150 static void retry_incomplete_types (void);
4151 static void gen_type_die_for_member (tree, tree, dw_die_ref);
4152 static void splice_child_die (dw_die_ref, dw_die_ref);
4153 static int file_info_cmp (const void *, const void *);
4154 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
4155 const char *, const char *, unsigned);
4156 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
4157 const char *, const char *,
4158 const char *);
4159 static void output_loc_list (dw_loc_list_ref);
4160 static char *gen_internal_sym (const char *);
4162 static void prune_unmark_dies (dw_die_ref);
4163 static void prune_unused_types_mark (dw_die_ref, int);
4164 static void prune_unused_types_walk (dw_die_ref);
4165 static void prune_unused_types_walk_attribs (dw_die_ref);
4166 static void prune_unused_types_prune (dw_die_ref);
4167 static void prune_unused_types (void);
4168 static int maybe_emit_file (int);
4170 /* Section names used to hold DWARF debugging information. */
4171 #ifndef DEBUG_INFO_SECTION
4172 #define DEBUG_INFO_SECTION ".debug_info"
4173 #endif
4174 #ifndef DEBUG_ABBREV_SECTION
4175 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
4176 #endif
4177 #ifndef DEBUG_ARANGES_SECTION
4178 #define DEBUG_ARANGES_SECTION ".debug_aranges"
4179 #endif
4180 #ifndef DEBUG_MACINFO_SECTION
4181 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
4182 #endif
4183 #ifndef DEBUG_LINE_SECTION
4184 #define DEBUG_LINE_SECTION ".debug_line"
4185 #endif
4186 #ifndef DEBUG_LOC_SECTION
4187 #define DEBUG_LOC_SECTION ".debug_loc"
4188 #endif
4189 #ifndef DEBUG_PUBNAMES_SECTION
4190 #define DEBUG_PUBNAMES_SECTION ".debug_pubnames"
4191 #endif
4192 #ifndef DEBUG_STR_SECTION
4193 #define DEBUG_STR_SECTION ".debug_str"
4194 #endif
4195 #ifndef DEBUG_RANGES_SECTION
4196 #define DEBUG_RANGES_SECTION ".debug_ranges"
4197 #endif
4199 /* Standard ELF section names for compiled code and data. */
4200 #ifndef TEXT_SECTION_NAME
4201 #define TEXT_SECTION_NAME ".text"
4202 #endif
4204 /* Section flags for .debug_str section. */
4205 #define DEBUG_STR_SECTION_FLAGS \
4206 (HAVE_GAS_SHF_MERGE && flag_merge_constants \
4207 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
4208 : SECTION_DEBUG)
4210 /* Labels we insert at beginning sections we can reference instead of
4211 the section names themselves. */
4213 #ifndef TEXT_SECTION_LABEL
4214 #define TEXT_SECTION_LABEL "Ltext"
4215 #endif
4216 #ifndef COLD_TEXT_SECTION_LABEL
4217 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
4218 #endif
4219 #ifndef DEBUG_LINE_SECTION_LABEL
4220 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
4221 #endif
4222 #ifndef DEBUG_INFO_SECTION_LABEL
4223 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
4224 #endif
4225 #ifndef DEBUG_ABBREV_SECTION_LABEL
4226 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
4227 #endif
4228 #ifndef DEBUG_LOC_SECTION_LABEL
4229 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
4230 #endif
4231 #ifndef DEBUG_RANGES_SECTION_LABEL
4232 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
4233 #endif
4234 #ifndef DEBUG_MACINFO_SECTION_LABEL
4235 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
4236 #endif
4238 /* Definitions of defaults for formats and names of various special
4239 (artificial) labels which may be generated within this file (when the -g
4240 options is used and DWARF2_DEBUGGING_INFO is in effect.
4241 If necessary, these may be overridden from within the tm.h file, but
4242 typically, overriding these defaults is unnecessary. */
4244 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4245 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4246 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4247 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4248 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4249 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4250 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4251 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4252 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4253 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4255 #ifndef TEXT_END_LABEL
4256 #define TEXT_END_LABEL "Letext"
4257 #endif
4258 #ifndef COLD_END_LABEL
4259 #define COLD_END_LABEL "Letext_cold"
4260 #endif
4261 #ifndef BLOCK_BEGIN_LABEL
4262 #define BLOCK_BEGIN_LABEL "LBB"
4263 #endif
4264 #ifndef BLOCK_END_LABEL
4265 #define BLOCK_END_LABEL "LBE"
4266 #endif
4267 #ifndef LINE_CODE_LABEL
4268 #define LINE_CODE_LABEL "LM"
4269 #endif
4270 #ifndef SEPARATE_LINE_CODE_LABEL
4271 #define SEPARATE_LINE_CODE_LABEL "LSM"
4272 #endif
4274 /* We allow a language front-end to designate a function that is to be
4275 called to "demangle" any name before it is put into a DIE. */
4277 static const char *(*demangle_name_func) (const char *);
4279 void
4280 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4282 demangle_name_func = func;
4285 /* Test if rtl node points to a pseudo register. */
4287 static inline int
4288 is_pseudo_reg (rtx rtl)
4290 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4291 || (GET_CODE (rtl) == SUBREG
4292 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4295 /* Return a reference to a type, with its const and volatile qualifiers
4296 removed. */
4298 static inline tree
4299 type_main_variant (tree type)
4301 type = TYPE_MAIN_VARIANT (type);
4303 /* ??? There really should be only one main variant among any group of
4304 variants of a given type (and all of the MAIN_VARIANT values for all
4305 members of the group should point to that one type) but sometimes the C
4306 front-end messes this up for array types, so we work around that bug
4307 here. */
4308 if (TREE_CODE (type) == ARRAY_TYPE)
4309 while (type != TYPE_MAIN_VARIANT (type))
4310 type = TYPE_MAIN_VARIANT (type);
4312 return type;
4315 /* Return nonzero if the given type node represents a tagged type. */
4317 static inline int
4318 is_tagged_type (tree type)
4320 enum tree_code code = TREE_CODE (type);
4322 return (code == RECORD_TYPE || code == UNION_TYPE
4323 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4326 /* Convert a DIE tag into its string name. */
4328 static const char *
4329 dwarf_tag_name (unsigned int tag)
4331 switch (tag)
4333 case DW_TAG_padding:
4334 return "DW_TAG_padding";
4335 case DW_TAG_array_type:
4336 return "DW_TAG_array_type";
4337 case DW_TAG_class_type:
4338 return "DW_TAG_class_type";
4339 case DW_TAG_entry_point:
4340 return "DW_TAG_entry_point";
4341 case DW_TAG_enumeration_type:
4342 return "DW_TAG_enumeration_type";
4343 case DW_TAG_formal_parameter:
4344 return "DW_TAG_formal_parameter";
4345 case DW_TAG_imported_declaration:
4346 return "DW_TAG_imported_declaration";
4347 case DW_TAG_label:
4348 return "DW_TAG_label";
4349 case DW_TAG_lexical_block:
4350 return "DW_TAG_lexical_block";
4351 case DW_TAG_member:
4352 return "DW_TAG_member";
4353 case DW_TAG_pointer_type:
4354 return "DW_TAG_pointer_type";
4355 case DW_TAG_reference_type:
4356 return "DW_TAG_reference_type";
4357 case DW_TAG_compile_unit:
4358 return "DW_TAG_compile_unit";
4359 case DW_TAG_string_type:
4360 return "DW_TAG_string_type";
4361 case DW_TAG_structure_type:
4362 return "DW_TAG_structure_type";
4363 case DW_TAG_subroutine_type:
4364 return "DW_TAG_subroutine_type";
4365 case DW_TAG_typedef:
4366 return "DW_TAG_typedef";
4367 case DW_TAG_union_type:
4368 return "DW_TAG_union_type";
4369 case DW_TAG_unspecified_parameters:
4370 return "DW_TAG_unspecified_parameters";
4371 case DW_TAG_variant:
4372 return "DW_TAG_variant";
4373 case DW_TAG_common_block:
4374 return "DW_TAG_common_block";
4375 case DW_TAG_common_inclusion:
4376 return "DW_TAG_common_inclusion";
4377 case DW_TAG_inheritance:
4378 return "DW_TAG_inheritance";
4379 case DW_TAG_inlined_subroutine:
4380 return "DW_TAG_inlined_subroutine";
4381 case DW_TAG_module:
4382 return "DW_TAG_module";
4383 case DW_TAG_ptr_to_member_type:
4384 return "DW_TAG_ptr_to_member_type";
4385 case DW_TAG_set_type:
4386 return "DW_TAG_set_type";
4387 case DW_TAG_subrange_type:
4388 return "DW_TAG_subrange_type";
4389 case DW_TAG_with_stmt:
4390 return "DW_TAG_with_stmt";
4391 case DW_TAG_access_declaration:
4392 return "DW_TAG_access_declaration";
4393 case DW_TAG_base_type:
4394 return "DW_TAG_base_type";
4395 case DW_TAG_catch_block:
4396 return "DW_TAG_catch_block";
4397 case DW_TAG_const_type:
4398 return "DW_TAG_const_type";
4399 case DW_TAG_constant:
4400 return "DW_TAG_constant";
4401 case DW_TAG_enumerator:
4402 return "DW_TAG_enumerator";
4403 case DW_TAG_file_type:
4404 return "DW_TAG_file_type";
4405 case DW_TAG_friend:
4406 return "DW_TAG_friend";
4407 case DW_TAG_namelist:
4408 return "DW_TAG_namelist";
4409 case DW_TAG_namelist_item:
4410 return "DW_TAG_namelist_item";
4411 case DW_TAG_namespace:
4412 return "DW_TAG_namespace";
4413 case DW_TAG_packed_type:
4414 return "DW_TAG_packed_type";
4415 case DW_TAG_subprogram:
4416 return "DW_TAG_subprogram";
4417 case DW_TAG_template_type_param:
4418 return "DW_TAG_template_type_param";
4419 case DW_TAG_template_value_param:
4420 return "DW_TAG_template_value_param";
4421 case DW_TAG_thrown_type:
4422 return "DW_TAG_thrown_type";
4423 case DW_TAG_try_block:
4424 return "DW_TAG_try_block";
4425 case DW_TAG_variant_part:
4426 return "DW_TAG_variant_part";
4427 case DW_TAG_variable:
4428 return "DW_TAG_variable";
4429 case DW_TAG_volatile_type:
4430 return "DW_TAG_volatile_type";
4431 case DW_TAG_imported_module:
4432 return "DW_TAG_imported_module";
4433 case DW_TAG_MIPS_loop:
4434 return "DW_TAG_MIPS_loop";
4435 case DW_TAG_format_label:
4436 return "DW_TAG_format_label";
4437 case DW_TAG_function_template:
4438 return "DW_TAG_function_template";
4439 case DW_TAG_class_template:
4440 return "DW_TAG_class_template";
4441 case DW_TAG_GNU_BINCL:
4442 return "DW_TAG_GNU_BINCL";
4443 case DW_TAG_GNU_EINCL:
4444 return "DW_TAG_GNU_EINCL";
4445 default:
4446 return "DW_TAG_<unknown>";
4450 /* Convert a DWARF attribute code into its string name. */
4452 static const char *
4453 dwarf_attr_name (unsigned int attr)
4455 switch (attr)
4457 case DW_AT_sibling:
4458 return "DW_AT_sibling";
4459 case DW_AT_location:
4460 return "DW_AT_location";
4461 case DW_AT_name:
4462 return "DW_AT_name";
4463 case DW_AT_ordering:
4464 return "DW_AT_ordering";
4465 case DW_AT_subscr_data:
4466 return "DW_AT_subscr_data";
4467 case DW_AT_byte_size:
4468 return "DW_AT_byte_size";
4469 case DW_AT_bit_offset:
4470 return "DW_AT_bit_offset";
4471 case DW_AT_bit_size:
4472 return "DW_AT_bit_size";
4473 case DW_AT_element_list:
4474 return "DW_AT_element_list";
4475 case DW_AT_stmt_list:
4476 return "DW_AT_stmt_list";
4477 case DW_AT_low_pc:
4478 return "DW_AT_low_pc";
4479 case DW_AT_high_pc:
4480 return "DW_AT_high_pc";
4481 case DW_AT_language:
4482 return "DW_AT_language";
4483 case DW_AT_member:
4484 return "DW_AT_member";
4485 case DW_AT_discr:
4486 return "DW_AT_discr";
4487 case DW_AT_discr_value:
4488 return "DW_AT_discr_value";
4489 case DW_AT_visibility:
4490 return "DW_AT_visibility";
4491 case DW_AT_import:
4492 return "DW_AT_import";
4493 case DW_AT_string_length:
4494 return "DW_AT_string_length";
4495 case DW_AT_common_reference:
4496 return "DW_AT_common_reference";
4497 case DW_AT_comp_dir:
4498 return "DW_AT_comp_dir";
4499 case DW_AT_const_value:
4500 return "DW_AT_const_value";
4501 case DW_AT_containing_type:
4502 return "DW_AT_containing_type";
4503 case DW_AT_default_value:
4504 return "DW_AT_default_value";
4505 case DW_AT_inline:
4506 return "DW_AT_inline";
4507 case DW_AT_is_optional:
4508 return "DW_AT_is_optional";
4509 case DW_AT_lower_bound:
4510 return "DW_AT_lower_bound";
4511 case DW_AT_producer:
4512 return "DW_AT_producer";
4513 case DW_AT_prototyped:
4514 return "DW_AT_prototyped";
4515 case DW_AT_return_addr:
4516 return "DW_AT_return_addr";
4517 case DW_AT_start_scope:
4518 return "DW_AT_start_scope";
4519 case DW_AT_stride_size:
4520 return "DW_AT_stride_size";
4521 case DW_AT_upper_bound:
4522 return "DW_AT_upper_bound";
4523 case DW_AT_abstract_origin:
4524 return "DW_AT_abstract_origin";
4525 case DW_AT_accessibility:
4526 return "DW_AT_accessibility";
4527 case DW_AT_address_class:
4528 return "DW_AT_address_class";
4529 case DW_AT_artificial:
4530 return "DW_AT_artificial";
4531 case DW_AT_base_types:
4532 return "DW_AT_base_types";
4533 case DW_AT_calling_convention:
4534 return "DW_AT_calling_convention";
4535 case DW_AT_count:
4536 return "DW_AT_count";
4537 case DW_AT_data_member_location:
4538 return "DW_AT_data_member_location";
4539 case DW_AT_decl_column:
4540 return "DW_AT_decl_column";
4541 case DW_AT_decl_file:
4542 return "DW_AT_decl_file";
4543 case DW_AT_decl_line:
4544 return "DW_AT_decl_line";
4545 case DW_AT_declaration:
4546 return "DW_AT_declaration";
4547 case DW_AT_discr_list:
4548 return "DW_AT_discr_list";
4549 case DW_AT_encoding:
4550 return "DW_AT_encoding";
4551 case DW_AT_external:
4552 return "DW_AT_external";
4553 case DW_AT_frame_base:
4554 return "DW_AT_frame_base";
4555 case DW_AT_friend:
4556 return "DW_AT_friend";
4557 case DW_AT_identifier_case:
4558 return "DW_AT_identifier_case";
4559 case DW_AT_macro_info:
4560 return "DW_AT_macro_info";
4561 case DW_AT_namelist_items:
4562 return "DW_AT_namelist_items";
4563 case DW_AT_priority:
4564 return "DW_AT_priority";
4565 case DW_AT_segment:
4566 return "DW_AT_segment";
4567 case DW_AT_specification:
4568 return "DW_AT_specification";
4569 case DW_AT_static_link:
4570 return "DW_AT_static_link";
4571 case DW_AT_type:
4572 return "DW_AT_type";
4573 case DW_AT_use_location:
4574 return "DW_AT_use_location";
4575 case DW_AT_variable_parameter:
4576 return "DW_AT_variable_parameter";
4577 case DW_AT_virtuality:
4578 return "DW_AT_virtuality";
4579 case DW_AT_vtable_elem_location:
4580 return "DW_AT_vtable_elem_location";
4582 case DW_AT_allocated:
4583 return "DW_AT_allocated";
4584 case DW_AT_associated:
4585 return "DW_AT_associated";
4586 case DW_AT_data_location:
4587 return "DW_AT_data_location";
4588 case DW_AT_stride:
4589 return "DW_AT_stride";
4590 case DW_AT_entry_pc:
4591 return "DW_AT_entry_pc";
4592 case DW_AT_use_UTF8:
4593 return "DW_AT_use_UTF8";
4594 case DW_AT_extension:
4595 return "DW_AT_extension";
4596 case DW_AT_ranges:
4597 return "DW_AT_ranges";
4598 case DW_AT_trampoline:
4599 return "DW_AT_trampoline";
4600 case DW_AT_call_column:
4601 return "DW_AT_call_column";
4602 case DW_AT_call_file:
4603 return "DW_AT_call_file";
4604 case DW_AT_call_line:
4605 return "DW_AT_call_line";
4607 case DW_AT_MIPS_fde:
4608 return "DW_AT_MIPS_fde";
4609 case DW_AT_MIPS_loop_begin:
4610 return "DW_AT_MIPS_loop_begin";
4611 case DW_AT_MIPS_tail_loop_begin:
4612 return "DW_AT_MIPS_tail_loop_begin";
4613 case DW_AT_MIPS_epilog_begin:
4614 return "DW_AT_MIPS_epilog_begin";
4615 case DW_AT_MIPS_loop_unroll_factor:
4616 return "DW_AT_MIPS_loop_unroll_factor";
4617 case DW_AT_MIPS_software_pipeline_depth:
4618 return "DW_AT_MIPS_software_pipeline_depth";
4619 case DW_AT_MIPS_linkage_name:
4620 return "DW_AT_MIPS_linkage_name";
4621 case DW_AT_MIPS_stride:
4622 return "DW_AT_MIPS_stride";
4623 case DW_AT_MIPS_abstract_name:
4624 return "DW_AT_MIPS_abstract_name";
4625 case DW_AT_MIPS_clone_origin:
4626 return "DW_AT_MIPS_clone_origin";
4627 case DW_AT_MIPS_has_inlines:
4628 return "DW_AT_MIPS_has_inlines";
4630 case DW_AT_sf_names:
4631 return "DW_AT_sf_names";
4632 case DW_AT_src_info:
4633 return "DW_AT_src_info";
4634 case DW_AT_mac_info:
4635 return "DW_AT_mac_info";
4636 case DW_AT_src_coords:
4637 return "DW_AT_src_coords";
4638 case DW_AT_body_begin:
4639 return "DW_AT_body_begin";
4640 case DW_AT_body_end:
4641 return "DW_AT_body_end";
4642 case DW_AT_GNU_vector:
4643 return "DW_AT_GNU_vector";
4645 case DW_AT_VMS_rtnbeg_pd_address:
4646 return "DW_AT_VMS_rtnbeg_pd_address";
4648 default:
4649 return "DW_AT_<unknown>";
4653 /* Convert a DWARF value form code into its string name. */
4655 static const char *
4656 dwarf_form_name (unsigned int form)
4658 switch (form)
4660 case DW_FORM_addr:
4661 return "DW_FORM_addr";
4662 case DW_FORM_block2:
4663 return "DW_FORM_block2";
4664 case DW_FORM_block4:
4665 return "DW_FORM_block4";
4666 case DW_FORM_data2:
4667 return "DW_FORM_data2";
4668 case DW_FORM_data4:
4669 return "DW_FORM_data4";
4670 case DW_FORM_data8:
4671 return "DW_FORM_data8";
4672 case DW_FORM_string:
4673 return "DW_FORM_string";
4674 case DW_FORM_block:
4675 return "DW_FORM_block";
4676 case DW_FORM_block1:
4677 return "DW_FORM_block1";
4678 case DW_FORM_data1:
4679 return "DW_FORM_data1";
4680 case DW_FORM_flag:
4681 return "DW_FORM_flag";
4682 case DW_FORM_sdata:
4683 return "DW_FORM_sdata";
4684 case DW_FORM_strp:
4685 return "DW_FORM_strp";
4686 case DW_FORM_udata:
4687 return "DW_FORM_udata";
4688 case DW_FORM_ref_addr:
4689 return "DW_FORM_ref_addr";
4690 case DW_FORM_ref1:
4691 return "DW_FORM_ref1";
4692 case DW_FORM_ref2:
4693 return "DW_FORM_ref2";
4694 case DW_FORM_ref4:
4695 return "DW_FORM_ref4";
4696 case DW_FORM_ref8:
4697 return "DW_FORM_ref8";
4698 case DW_FORM_ref_udata:
4699 return "DW_FORM_ref_udata";
4700 case DW_FORM_indirect:
4701 return "DW_FORM_indirect";
4702 default:
4703 return "DW_FORM_<unknown>";
4707 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4708 instance of an inlined instance of a decl which is local to an inline
4709 function, so we have to trace all of the way back through the origin chain
4710 to find out what sort of node actually served as the original seed for the
4711 given block. */
4713 static tree
4714 decl_ultimate_origin (tree decl)
4716 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4717 return NULL_TREE;
4719 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4720 nodes in the function to point to themselves; ignore that if
4721 we're trying to output the abstract instance of this function. */
4722 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4723 return NULL_TREE;
4725 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4726 most distant ancestor, this should never happen. */
4727 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4729 return DECL_ABSTRACT_ORIGIN (decl);
4732 /* Determine the "ultimate origin" of a block. The block may be an inlined
4733 instance of an inlined instance of a block which is local to an inline
4734 function, so we have to trace all of the way back through the origin chain
4735 to find out what sort of node actually served as the original seed for the
4736 given block. */
4738 static tree
4739 block_ultimate_origin (tree block)
4741 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
4743 /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
4744 nodes in the function to point to themselves; ignore that if
4745 we're trying to output the abstract instance of this function. */
4746 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
4747 return NULL_TREE;
4749 if (immediate_origin == NULL_TREE)
4750 return NULL_TREE;
4751 else
4753 tree ret_val;
4754 tree lookahead = immediate_origin;
4758 ret_val = lookahead;
4759 lookahead = (TREE_CODE (ret_val) == BLOCK
4760 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
4762 while (lookahead != NULL && lookahead != ret_val);
4764 /* The block's abstract origin chain may not be the *ultimate* origin of
4765 the block. It could lead to a DECL that has an abstract origin set.
4766 If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
4767 will give us if it has one). Note that DECL's abstract origins are
4768 supposed to be the most distant ancestor (or so decl_ultimate_origin
4769 claims), so we don't need to loop following the DECL origins. */
4770 if (DECL_P (ret_val))
4771 return DECL_ORIGIN (ret_val);
4773 return ret_val;
4777 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4778 of a virtual function may refer to a base class, so we check the 'this'
4779 parameter. */
4781 static tree
4782 decl_class_context (tree decl)
4784 tree context = NULL_TREE;
4786 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4787 context = DECL_CONTEXT (decl);
4788 else
4789 context = TYPE_MAIN_VARIANT
4790 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4792 if (context && !TYPE_P (context))
4793 context = NULL_TREE;
4795 return context;
4798 /* Add an attribute/value pair to a DIE. We build the lists up in reverse
4799 addition order, and correct that in reverse_all_dies. */
4801 static inline void
4802 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
4804 if (die != NULL && attr != NULL)
4806 attr->dw_attr_next = die->die_attr;
4807 die->die_attr = attr;
4811 static inline enum dw_val_class
4812 AT_class (dw_attr_ref a)
4814 return a->dw_attr_val.val_class;
4817 /* Add a flag value attribute to a DIE. */
4819 static inline void
4820 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4822 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4824 attr->dw_attr_next = NULL;
4825 attr->dw_attr = attr_kind;
4826 attr->dw_attr_val.val_class = dw_val_class_flag;
4827 attr->dw_attr_val.v.val_flag = flag;
4828 add_dwarf_attr (die, attr);
4831 static inline unsigned
4832 AT_flag (dw_attr_ref a)
4834 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4835 return a->dw_attr_val.v.val_flag;
4838 /* Add a signed integer attribute value to a DIE. */
4840 static inline void
4841 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4843 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4845 attr->dw_attr_next = NULL;
4846 attr->dw_attr = attr_kind;
4847 attr->dw_attr_val.val_class = dw_val_class_const;
4848 attr->dw_attr_val.v.val_int = int_val;
4849 add_dwarf_attr (die, attr);
4852 static inline HOST_WIDE_INT
4853 AT_int (dw_attr_ref a)
4855 gcc_assert (a && AT_class (a) == dw_val_class_const);
4856 return a->dw_attr_val.v.val_int;
4859 /* Add an unsigned integer attribute value to a DIE. */
4861 static inline void
4862 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4863 unsigned HOST_WIDE_INT unsigned_val)
4865 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4867 attr->dw_attr_next = NULL;
4868 attr->dw_attr = attr_kind;
4869 attr->dw_attr_val.val_class = dw_val_class_unsigned_const;
4870 attr->dw_attr_val.v.val_unsigned = unsigned_val;
4871 add_dwarf_attr (die, attr);
4874 static inline unsigned HOST_WIDE_INT
4875 AT_unsigned (dw_attr_ref a)
4877 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
4878 return a->dw_attr_val.v.val_unsigned;
4881 /* Add an unsigned double integer attribute value to a DIE. */
4883 static inline void
4884 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
4885 long unsigned int val_hi, long unsigned int val_low)
4887 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4889 attr->dw_attr_next = NULL;
4890 attr->dw_attr = attr_kind;
4891 attr->dw_attr_val.val_class = dw_val_class_long_long;
4892 attr->dw_attr_val.v.val_long_long.hi = val_hi;
4893 attr->dw_attr_val.v.val_long_long.low = val_low;
4894 add_dwarf_attr (die, attr);
4897 /* Add a floating point attribute value to a DIE and return it. */
4899 static inline void
4900 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4901 unsigned int length, unsigned int elt_size, unsigned char *array)
4903 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4905 attr->dw_attr_next = NULL;
4906 attr->dw_attr = attr_kind;
4907 attr->dw_attr_val.val_class = dw_val_class_vec;
4908 attr->dw_attr_val.v.val_vec.length = length;
4909 attr->dw_attr_val.v.val_vec.elt_size = elt_size;
4910 attr->dw_attr_val.v.val_vec.array = array;
4911 add_dwarf_attr (die, attr);
4914 /* Hash and equality functions for debug_str_hash. */
4916 static hashval_t
4917 debug_str_do_hash (const void *x)
4919 return htab_hash_string (((const struct indirect_string_node *)x)->str);
4922 static int
4923 debug_str_eq (const void *x1, const void *x2)
4925 return strcmp ((((const struct indirect_string_node *)x1)->str),
4926 (const char *)x2) == 0;
4929 /* Add a string attribute value to a DIE. */
4931 static inline void
4932 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4934 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4935 struct indirect_string_node *node;
4936 void **slot;
4938 if (! debug_str_hash)
4939 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
4940 debug_str_eq, NULL);
4942 slot = htab_find_slot_with_hash (debug_str_hash, str,
4943 htab_hash_string (str), INSERT);
4944 if (*slot == NULL)
4945 *slot = ggc_alloc_cleared (sizeof (struct indirect_string_node));
4946 node = (struct indirect_string_node *) *slot;
4947 node->str = ggc_strdup (str);
4948 node->refcount++;
4950 attr->dw_attr_next = NULL;
4951 attr->dw_attr = attr_kind;
4952 attr->dw_attr_val.val_class = dw_val_class_str;
4953 attr->dw_attr_val.v.val_str = node;
4954 add_dwarf_attr (die, attr);
4957 static inline const char *
4958 AT_string (dw_attr_ref a)
4960 gcc_assert (a && AT_class (a) == dw_val_class_str);
4961 return a->dw_attr_val.v.val_str->str;
4964 /* Find out whether a string should be output inline in DIE
4965 or out-of-line in .debug_str section. */
4967 static int
4968 AT_string_form (dw_attr_ref a)
4970 struct indirect_string_node *node;
4971 unsigned int len;
4972 char label[32];
4974 gcc_assert (a && AT_class (a) == dw_val_class_str);
4976 node = a->dw_attr_val.v.val_str;
4977 if (node->form)
4978 return node->form;
4980 len = strlen (node->str) + 1;
4982 /* If the string is shorter or equal to the size of the reference, it is
4983 always better to put it inline. */
4984 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4985 return node->form = DW_FORM_string;
4987 /* If we cannot expect the linker to merge strings in .debug_str
4988 section, only put it into .debug_str if it is worth even in this
4989 single module. */
4990 if ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) == 0
4991 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
4992 return node->form = DW_FORM_string;
4994 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4995 ++dw2_string_counter;
4996 node->label = xstrdup (label);
4998 return node->form = DW_FORM_strp;
5001 /* Add a DIE reference attribute value to a DIE. */
5003 static inline void
5004 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
5006 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5008 attr->dw_attr_next = NULL;
5009 attr->dw_attr = attr_kind;
5010 attr->dw_attr_val.val_class = dw_val_class_die_ref;
5011 attr->dw_attr_val.v.val_die_ref.die = targ_die;
5012 attr->dw_attr_val.v.val_die_ref.external = 0;
5013 add_dwarf_attr (die, attr);
5016 /* Add an AT_specification attribute to a DIE, and also make the back
5017 pointer from the specification to the definition. */
5019 static inline void
5020 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
5022 add_AT_die_ref (die, DW_AT_specification, targ_die);
5023 gcc_assert (!targ_die->die_definition);
5024 targ_die->die_definition = die;
5027 static inline dw_die_ref
5028 AT_ref (dw_attr_ref a)
5030 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5031 return a->dw_attr_val.v.val_die_ref.die;
5034 static inline int
5035 AT_ref_external (dw_attr_ref a)
5037 if (a && AT_class (a) == dw_val_class_die_ref)
5038 return a->dw_attr_val.v.val_die_ref.external;
5040 return 0;
5043 static inline void
5044 set_AT_ref_external (dw_attr_ref a, int i)
5046 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5047 a->dw_attr_val.v.val_die_ref.external = i;
5050 /* Add an FDE reference attribute value to a DIE. */
5052 static inline void
5053 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
5055 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5057 attr->dw_attr_next = NULL;
5058 attr->dw_attr = attr_kind;
5059 attr->dw_attr_val.val_class = dw_val_class_fde_ref;
5060 attr->dw_attr_val.v.val_fde_index = targ_fde;
5061 add_dwarf_attr (die, attr);
5064 /* Add a location description attribute value to a DIE. */
5066 static inline void
5067 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
5069 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5071 attr->dw_attr_next = NULL;
5072 attr->dw_attr = attr_kind;
5073 attr->dw_attr_val.val_class = dw_val_class_loc;
5074 attr->dw_attr_val.v.val_loc = loc;
5075 add_dwarf_attr (die, attr);
5078 static inline dw_loc_descr_ref
5079 AT_loc (dw_attr_ref a)
5081 gcc_assert (a && AT_class (a) == dw_val_class_loc);
5082 return a->dw_attr_val.v.val_loc;
5085 static inline void
5086 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
5088 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5090 attr->dw_attr_next = NULL;
5091 attr->dw_attr = attr_kind;
5092 attr->dw_attr_val.val_class = dw_val_class_loc_list;
5093 attr->dw_attr_val.v.val_loc_list = loc_list;
5094 add_dwarf_attr (die, attr);
5095 have_location_lists = 1;
5098 static inline dw_loc_list_ref
5099 AT_loc_list (dw_attr_ref a)
5101 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
5102 return a->dw_attr_val.v.val_loc_list;
5105 /* Add an address constant attribute value to a DIE. */
5107 static inline void
5108 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
5110 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5112 attr->dw_attr_next = NULL;
5113 attr->dw_attr = attr_kind;
5114 attr->dw_attr_val.val_class = dw_val_class_addr;
5115 attr->dw_attr_val.v.val_addr = addr;
5116 add_dwarf_attr (die, attr);
5119 static inline rtx
5120 AT_addr (dw_attr_ref a)
5122 gcc_assert (a && AT_class (a) == dw_val_class_addr);
5123 return a->dw_attr_val.v.val_addr;
5126 /* Add a label identifier attribute value to a DIE. */
5128 static inline void
5129 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
5131 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5133 attr->dw_attr_next = NULL;
5134 attr->dw_attr = attr_kind;
5135 attr->dw_attr_val.val_class = dw_val_class_lbl_id;
5136 attr->dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5137 add_dwarf_attr (die, attr);
5140 /* Add a section offset attribute value to a DIE. */
5142 static inline void
5143 add_AT_lbl_offset (dw_die_ref die, enum dwarf_attribute attr_kind, const char *label)
5145 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5147 attr->dw_attr_next = NULL;
5148 attr->dw_attr = attr_kind;
5149 attr->dw_attr_val.val_class = dw_val_class_lbl_offset;
5150 attr->dw_attr_val.v.val_lbl_id = xstrdup (label);
5151 add_dwarf_attr (die, attr);
5154 /* Add an offset attribute value to a DIE. */
5156 static inline void
5157 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
5158 unsigned HOST_WIDE_INT offset)
5160 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5162 attr->dw_attr_next = NULL;
5163 attr->dw_attr = attr_kind;
5164 attr->dw_attr_val.val_class = dw_val_class_offset;
5165 attr->dw_attr_val.v.val_offset = offset;
5166 add_dwarf_attr (die, attr);
5169 /* Add an range_list attribute value to a DIE. */
5171 static void
5172 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5173 long unsigned int offset)
5175 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5177 attr->dw_attr_next = NULL;
5178 attr->dw_attr = attr_kind;
5179 attr->dw_attr_val.val_class = dw_val_class_range_list;
5180 attr->dw_attr_val.v.val_offset = offset;
5181 add_dwarf_attr (die, attr);
5184 static inline const char *
5185 AT_lbl (dw_attr_ref a)
5187 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5188 || AT_class (a) == dw_val_class_lbl_offset));
5189 return a->dw_attr_val.v.val_lbl_id;
5192 /* Get the attribute of type attr_kind. */
5194 static dw_attr_ref
5195 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5197 dw_attr_ref a;
5198 dw_die_ref spec = NULL;
5200 if (die != NULL)
5202 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
5203 if (a->dw_attr == attr_kind)
5204 return a;
5205 else if (a->dw_attr == DW_AT_specification
5206 || a->dw_attr == DW_AT_abstract_origin)
5207 spec = AT_ref (a);
5209 if (spec)
5210 return get_AT (spec, attr_kind);
5213 return NULL;
5216 /* Return the "low pc" attribute value, typically associated with a subprogram
5217 DIE. Return null if the "low pc" attribute is either not present, or if it
5218 cannot be represented as an assembler label identifier. */
5220 static inline const char *
5221 get_AT_low_pc (dw_die_ref die)
5223 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
5225 return a ? AT_lbl (a) : NULL;
5228 /* Return the "high pc" attribute value, typically associated with a subprogram
5229 DIE. Return null if the "high pc" attribute is either not present, or if it
5230 cannot be represented as an assembler label identifier. */
5232 static inline const char *
5233 get_AT_hi_pc (dw_die_ref die)
5235 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
5237 return a ? AT_lbl (a) : NULL;
5240 /* Return the value of the string attribute designated by ATTR_KIND, or
5241 NULL if it is not present. */
5243 static inline const char *
5244 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5246 dw_attr_ref a = get_AT (die, attr_kind);
5248 return a ? AT_string (a) : NULL;
5251 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5252 if it is not present. */
5254 static inline int
5255 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5257 dw_attr_ref a = get_AT (die, attr_kind);
5259 return a ? AT_flag (a) : 0;
5262 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5263 if it is not present. */
5265 static inline unsigned
5266 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5268 dw_attr_ref a = get_AT (die, attr_kind);
5270 return a ? AT_unsigned (a) : 0;
5273 static inline dw_die_ref
5274 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5276 dw_attr_ref a = get_AT (die, attr_kind);
5278 return a ? AT_ref (a) : NULL;
5281 /* Return TRUE if the language is C or C++. */
5283 static inline bool
5284 is_c_family (void)
5286 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5288 return (lang == DW_LANG_C || lang == DW_LANG_C89
5289 || lang == DW_LANG_C_plus_plus);
5292 /* Return TRUE if the language is C++. */
5294 static inline bool
5295 is_cxx (void)
5297 return (get_AT_unsigned (comp_unit_die, DW_AT_language)
5298 == DW_LANG_C_plus_plus);
5301 /* Return TRUE if the language is Fortran. */
5303 static inline bool
5304 is_fortran (void)
5306 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5308 return (lang == DW_LANG_Fortran77
5309 || lang == DW_LANG_Fortran90
5310 || lang == DW_LANG_Fortran95);
5313 /* Return TRUE if the language is Java. */
5315 static inline bool
5316 is_java (void)
5318 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5320 return lang == DW_LANG_Java;
5323 /* Return TRUE if the language is Ada. */
5325 static inline bool
5326 is_ada (void)
5328 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5330 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5333 /* Free up the memory used by A. */
5335 static inline void free_AT (dw_attr_ref);
5336 static inline void
5337 free_AT (dw_attr_ref a)
5339 if (AT_class (a) == dw_val_class_str)
5340 if (a->dw_attr_val.v.val_str->refcount)
5341 a->dw_attr_val.v.val_str->refcount--;
5344 /* Remove the specified attribute if present. */
5346 static void
5347 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5349 dw_attr_ref *p;
5350 dw_attr_ref removed = NULL;
5352 if (die != NULL)
5354 for (p = &(die->die_attr); *p; p = &((*p)->dw_attr_next))
5355 if ((*p)->dw_attr == attr_kind)
5357 removed = *p;
5358 *p = (*p)->dw_attr_next;
5359 break;
5362 if (removed != 0)
5363 free_AT (removed);
5367 /* Remove child die whose die_tag is specified tag. */
5369 static void
5370 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5372 dw_die_ref current, prev, next;
5373 current = die->die_child;
5374 prev = NULL;
5375 while (current != NULL)
5377 if (current->die_tag == tag)
5379 next = current->die_sib;
5380 if (prev == NULL)
5381 die->die_child = next;
5382 else
5383 prev->die_sib = next;
5384 free_die (current);
5385 current = next;
5387 else
5389 prev = current;
5390 current = current->die_sib;
5395 /* Free up the memory used by DIE. */
5397 static inline void
5398 free_die (dw_die_ref die)
5400 remove_children (die);
5403 /* Discard the children of this DIE. */
5405 static void
5406 remove_children (dw_die_ref die)
5408 dw_die_ref child_die = die->die_child;
5410 die->die_child = NULL;
5412 while (child_die != NULL)
5414 dw_die_ref tmp_die = child_die;
5415 dw_attr_ref a;
5417 child_die = child_die->die_sib;
5419 for (a = tmp_die->die_attr; a != NULL;)
5421 dw_attr_ref tmp_a = a;
5423 a = a->dw_attr_next;
5424 free_AT (tmp_a);
5427 free_die (tmp_die);
5431 /* Add a child DIE below its parent. We build the lists up in reverse
5432 addition order, and correct that in reverse_all_dies. */
5434 static inline void
5435 add_child_die (dw_die_ref die, dw_die_ref child_die)
5437 if (die != NULL && child_die != NULL)
5439 gcc_assert (die != child_die);
5441 child_die->die_parent = die;
5442 child_die->die_sib = die->die_child;
5443 die->die_child = child_die;
5447 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5448 is the specification, to the front of PARENT's list of children. */
5450 static void
5451 splice_child_die (dw_die_ref parent, dw_die_ref child)
5453 dw_die_ref *p;
5455 /* We want the declaration DIE from inside the class, not the
5456 specification DIE at toplevel. */
5457 if (child->die_parent != parent)
5459 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5461 if (tmp)
5462 child = tmp;
5465 gcc_assert (child->die_parent == parent
5466 || (child->die_parent
5467 == get_AT_ref (parent, DW_AT_specification)));
5469 for (p = &(child->die_parent->die_child); *p; p = &((*p)->die_sib))
5470 if (*p == child)
5472 *p = child->die_sib;
5473 break;
5476 child->die_parent = parent;
5477 child->die_sib = parent->die_child;
5478 parent->die_child = child;
5481 /* Return a pointer to a newly created DIE node. */
5483 static inline dw_die_ref
5484 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5486 dw_die_ref die = ggc_alloc_cleared (sizeof (die_node));
5488 die->die_tag = tag_value;
5490 if (parent_die != NULL)
5491 add_child_die (parent_die, die);
5492 else
5494 limbo_die_node *limbo_node;
5496 limbo_node = ggc_alloc_cleared (sizeof (limbo_die_node));
5497 limbo_node->die = die;
5498 limbo_node->created_for = t;
5499 limbo_node->next = limbo_die_list;
5500 limbo_die_list = limbo_node;
5503 return die;
5506 /* Return the DIE associated with the given type specifier. */
5508 static inline dw_die_ref
5509 lookup_type_die (tree type)
5511 return TYPE_SYMTAB_DIE (type);
5514 /* Equate a DIE to a given type specifier. */
5516 static inline void
5517 equate_type_number_to_die (tree type, dw_die_ref type_die)
5519 TYPE_SYMTAB_DIE (type) = type_die;
5522 /* Returns a hash value for X (which really is a die_struct). */
5524 static hashval_t
5525 decl_die_table_hash (const void *x)
5527 return (hashval_t) ((const dw_die_ref) x)->decl_id;
5530 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5532 static int
5533 decl_die_table_eq (const void *x, const void *y)
5535 return (((const dw_die_ref) x)->decl_id == DECL_UID ((const tree) y));
5538 /* Return the DIE associated with a given declaration. */
5540 static inline dw_die_ref
5541 lookup_decl_die (tree decl)
5543 return htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
5546 /* Returns a hash value for X (which really is a var_loc_list). */
5548 static hashval_t
5549 decl_loc_table_hash (const void *x)
5551 return (hashval_t) ((const var_loc_list *) x)->decl_id;
5554 /* Return nonzero if decl_id of var_loc_list X is the same as
5555 UID of decl *Y. */
5557 static int
5558 decl_loc_table_eq (const void *x, const void *y)
5560 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const tree) y));
5563 /* Return the var_loc list associated with a given declaration. */
5565 static inline var_loc_list *
5566 lookup_decl_loc (tree decl)
5568 return htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5571 /* Equate a DIE to a particular declaration. */
5573 static void
5574 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5576 unsigned int decl_id = DECL_UID (decl);
5577 void **slot;
5579 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5580 *slot = decl_die;
5581 decl_die->decl_id = decl_id;
5584 /* Add a variable location node to the linked list for DECL. */
5586 static void
5587 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
5589 unsigned int decl_id = DECL_UID (decl);
5590 var_loc_list *temp;
5591 void **slot;
5593 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5594 if (*slot == NULL)
5596 temp = ggc_alloc_cleared (sizeof (var_loc_list));
5597 temp->decl_id = decl_id;
5598 *slot = temp;
5600 else
5601 temp = *slot;
5603 if (temp->last)
5605 /* If the current location is the same as the end of the list,
5606 we have nothing to do. */
5607 if (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
5608 NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
5610 /* Add LOC to the end of list and update LAST. */
5611 temp->last->next = loc;
5612 temp->last = loc;
5615 /* Do not add empty location to the beginning of the list. */
5616 else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
5618 temp->first = loc;
5619 temp->last = loc;
5623 /* Keep track of the number of spaces used to indent the
5624 output of the debugging routines that print the structure of
5625 the DIE internal representation. */
5626 static int print_indent;
5628 /* Indent the line the number of spaces given by print_indent. */
5630 static inline void
5631 print_spaces (FILE *outfile)
5633 fprintf (outfile, "%*s", print_indent, "");
5636 /* Print the information associated with a given DIE, and its children.
5637 This routine is a debugging aid only. */
5639 static void
5640 print_die (dw_die_ref die, FILE *outfile)
5642 dw_attr_ref a;
5643 dw_die_ref c;
5645 print_spaces (outfile);
5646 fprintf (outfile, "DIE %4lu: %s\n",
5647 die->die_offset, dwarf_tag_name (die->die_tag));
5648 print_spaces (outfile);
5649 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5650 fprintf (outfile, " offset: %lu\n", die->die_offset);
5652 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
5654 print_spaces (outfile);
5655 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5657 switch (AT_class (a))
5659 case dw_val_class_addr:
5660 fprintf (outfile, "address");
5661 break;
5662 case dw_val_class_offset:
5663 fprintf (outfile, "offset");
5664 break;
5665 case dw_val_class_loc:
5666 fprintf (outfile, "location descriptor");
5667 break;
5668 case dw_val_class_loc_list:
5669 fprintf (outfile, "location list -> label:%s",
5670 AT_loc_list (a)->ll_symbol);
5671 break;
5672 case dw_val_class_range_list:
5673 fprintf (outfile, "range list");
5674 break;
5675 case dw_val_class_const:
5676 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5677 break;
5678 case dw_val_class_unsigned_const:
5679 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5680 break;
5681 case dw_val_class_long_long:
5682 fprintf (outfile, "constant (%lu,%lu)",
5683 a->dw_attr_val.v.val_long_long.hi,
5684 a->dw_attr_val.v.val_long_long.low);
5685 break;
5686 case dw_val_class_vec:
5687 fprintf (outfile, "floating-point or vector constant");
5688 break;
5689 case dw_val_class_flag:
5690 fprintf (outfile, "%u", AT_flag (a));
5691 break;
5692 case dw_val_class_die_ref:
5693 if (AT_ref (a) != NULL)
5695 if (AT_ref (a)->die_symbol)
5696 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
5697 else
5698 fprintf (outfile, "die -> %lu", AT_ref (a)->die_offset);
5700 else
5701 fprintf (outfile, "die -> <null>");
5702 break;
5703 case dw_val_class_lbl_id:
5704 case dw_val_class_lbl_offset:
5705 fprintf (outfile, "label: %s", AT_lbl (a));
5706 break;
5707 case dw_val_class_str:
5708 if (AT_string (a) != NULL)
5709 fprintf (outfile, "\"%s\"", AT_string (a));
5710 else
5711 fprintf (outfile, "<null>");
5712 break;
5713 default:
5714 break;
5717 fprintf (outfile, "\n");
5720 if (die->die_child != NULL)
5722 print_indent += 4;
5723 for (c = die->die_child; c != NULL; c = c->die_sib)
5724 print_die (c, outfile);
5726 print_indent -= 4;
5728 if (print_indent == 0)
5729 fprintf (outfile, "\n");
5732 /* Print the contents of the source code line number correspondence table.
5733 This routine is a debugging aid only. */
5735 static void
5736 print_dwarf_line_table (FILE *outfile)
5738 unsigned i;
5739 dw_line_info_ref line_info;
5741 fprintf (outfile, "\n\nDWARF source line information\n");
5742 for (i = 1; i < line_info_table_in_use; i++)
5744 line_info = &line_info_table[i];
5745 fprintf (outfile, "%5d: ", i);
5746 fprintf (outfile, "%-20s",
5747 VARRAY_CHAR_PTR (file_table, line_info->dw_file_num));
5748 fprintf (outfile, "%6ld", line_info->dw_line_num);
5749 fprintf (outfile, "\n");
5752 fprintf (outfile, "\n\n");
5755 /* Print the information collected for a given DIE. */
5757 void
5758 debug_dwarf_die (dw_die_ref die)
5760 print_die (die, stderr);
5763 /* Print all DWARF information collected for the compilation unit.
5764 This routine is a debugging aid only. */
5766 void
5767 debug_dwarf (void)
5769 print_indent = 0;
5770 print_die (comp_unit_die, stderr);
5771 if (! DWARF2_ASM_LINE_DEBUG_INFO)
5772 print_dwarf_line_table (stderr);
5775 /* We build up the lists of children and attributes by pushing new ones
5776 onto the beginning of the list. Reverse the lists for DIE so that
5777 they are in order of addition. */
5779 static void
5780 reverse_die_lists (dw_die_ref die)
5782 dw_die_ref c, cp, cn;
5783 dw_attr_ref a, ap, an;
5785 for (a = die->die_attr, ap = 0; a; a = an)
5787 an = a->dw_attr_next;
5788 a->dw_attr_next = ap;
5789 ap = a;
5792 die->die_attr = ap;
5794 for (c = die->die_child, cp = 0; c; c = cn)
5796 cn = c->die_sib;
5797 c->die_sib = cp;
5798 cp = c;
5801 die->die_child = cp;
5804 /* reverse_die_lists only reverses the single die you pass it. Since we used to
5805 reverse all dies in add_sibling_attributes, which runs through all the dies,
5806 it would reverse all the dies. Now, however, since we don't call
5807 reverse_die_lists in add_sibling_attributes, we need a routine to
5808 recursively reverse all the dies. This is that routine. */
5810 static void
5811 reverse_all_dies (dw_die_ref die)
5813 dw_die_ref c;
5815 reverse_die_lists (die);
5817 for (c = die->die_child; c; c = c->die_sib)
5818 reverse_all_dies (c);
5821 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5822 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5823 DIE that marks the start of the DIEs for this include file. */
5825 static dw_die_ref
5826 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5828 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5829 dw_die_ref new_unit = gen_compile_unit_die (filename);
5831 new_unit->die_sib = old_unit;
5832 return new_unit;
5835 /* Close an include-file CU and reopen the enclosing one. */
5837 static dw_die_ref
5838 pop_compile_unit (dw_die_ref old_unit)
5840 dw_die_ref new_unit = old_unit->die_sib;
5842 old_unit->die_sib = NULL;
5843 return new_unit;
5846 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5847 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5849 /* Calculate the checksum of a location expression. */
5851 static inline void
5852 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5854 CHECKSUM (loc->dw_loc_opc);
5855 CHECKSUM (loc->dw_loc_oprnd1);
5856 CHECKSUM (loc->dw_loc_oprnd2);
5859 /* Calculate the checksum of an attribute. */
5861 static void
5862 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5864 dw_loc_descr_ref loc;
5865 rtx r;
5867 CHECKSUM (at->dw_attr);
5869 /* We don't care about differences in file numbering. */
5870 if (at->dw_attr == DW_AT_decl_file
5871 /* Or that this was compiled with a different compiler snapshot; if
5872 the output is the same, that's what matters. */
5873 || at->dw_attr == DW_AT_producer)
5874 return;
5876 switch (AT_class (at))
5878 case dw_val_class_const:
5879 CHECKSUM (at->dw_attr_val.v.val_int);
5880 break;
5881 case dw_val_class_unsigned_const:
5882 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5883 break;
5884 case dw_val_class_long_long:
5885 CHECKSUM (at->dw_attr_val.v.val_long_long);
5886 break;
5887 case dw_val_class_vec:
5888 CHECKSUM (at->dw_attr_val.v.val_vec);
5889 break;
5890 case dw_val_class_flag:
5891 CHECKSUM (at->dw_attr_val.v.val_flag);
5892 break;
5893 case dw_val_class_str:
5894 CHECKSUM_STRING (AT_string (at));
5895 break;
5897 case dw_val_class_addr:
5898 r = AT_addr (at);
5899 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5900 CHECKSUM_STRING (XSTR (r, 0));
5901 break;
5903 case dw_val_class_offset:
5904 CHECKSUM (at->dw_attr_val.v.val_offset);
5905 break;
5907 case dw_val_class_loc:
5908 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5909 loc_checksum (loc, ctx);
5910 break;
5912 case dw_val_class_die_ref:
5913 die_checksum (AT_ref (at), ctx, mark);
5914 break;
5916 case dw_val_class_fde_ref:
5917 case dw_val_class_lbl_id:
5918 case dw_val_class_lbl_offset:
5919 break;
5921 default:
5922 break;
5926 /* Calculate the checksum of a DIE. */
5928 static void
5929 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5931 dw_die_ref c;
5932 dw_attr_ref a;
5934 /* To avoid infinite recursion. */
5935 if (die->die_mark)
5937 CHECKSUM (die->die_mark);
5938 return;
5940 die->die_mark = ++(*mark);
5942 CHECKSUM (die->die_tag);
5944 for (a = die->die_attr; a; a = a->dw_attr_next)
5945 attr_checksum (a, ctx, mark);
5947 for (c = die->die_child; c; c = c->die_sib)
5948 die_checksum (c, ctx, mark);
5951 #undef CHECKSUM
5952 #undef CHECKSUM_STRING
5954 /* Do the location expressions look same? */
5955 static inline int
5956 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
5958 return loc1->dw_loc_opc == loc2->dw_loc_opc
5959 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
5960 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
5963 /* Do the values look the same? */
5964 static int
5965 same_dw_val_p (dw_val_node *v1, dw_val_node *v2, int *mark)
5967 dw_loc_descr_ref loc1, loc2;
5968 rtx r1, r2;
5970 if (v1->val_class != v2->val_class)
5971 return 0;
5973 switch (v1->val_class)
5975 case dw_val_class_const:
5976 return v1->v.val_int == v2->v.val_int;
5977 case dw_val_class_unsigned_const:
5978 return v1->v.val_unsigned == v2->v.val_unsigned;
5979 case dw_val_class_long_long:
5980 return v1->v.val_long_long.hi == v2->v.val_long_long.hi
5981 && v1->v.val_long_long.low == v2->v.val_long_long.low;
5982 case dw_val_class_vec:
5983 if (v1->v.val_vec.length != v2->v.val_vec.length
5984 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
5985 return 0;
5986 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
5987 v1->v.val_vec.length * v1->v.val_vec.elt_size))
5988 return 0;
5989 return 1;
5990 case dw_val_class_flag:
5991 return v1->v.val_flag == v2->v.val_flag;
5992 case dw_val_class_str:
5993 return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
5995 case dw_val_class_addr:
5996 r1 = v1->v.val_addr;
5997 r2 = v2->v.val_addr;
5998 if (GET_CODE (r1) != GET_CODE (r2))
5999 return 0;
6000 gcc_assert (GET_CODE (r1) == SYMBOL_REF);
6001 return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
6003 case dw_val_class_offset:
6004 return v1->v.val_offset == v2->v.val_offset;
6006 case dw_val_class_loc:
6007 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6008 loc1 && loc2;
6009 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6010 if (!same_loc_p (loc1, loc2, mark))
6011 return 0;
6012 return !loc1 && !loc2;
6014 case dw_val_class_die_ref:
6015 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6017 case dw_val_class_fde_ref:
6018 case dw_val_class_lbl_id:
6019 case dw_val_class_lbl_offset:
6020 return 1;
6022 default:
6023 return 1;
6027 /* Do the attributes look the same? */
6029 static int
6030 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6032 if (at1->dw_attr != at2->dw_attr)
6033 return 0;
6035 /* We don't care about differences in file numbering. */
6036 if (at1->dw_attr == DW_AT_decl_file
6037 /* Or that this was compiled with a different compiler snapshot; if
6038 the output is the same, that's what matters. */
6039 || at1->dw_attr == DW_AT_producer)
6040 return 1;
6042 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6045 /* Do the dies look the same? */
6047 static int
6048 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6050 dw_die_ref c1, c2;
6051 dw_attr_ref a1, a2;
6053 /* To avoid infinite recursion. */
6054 if (die1->die_mark)
6055 return die1->die_mark == die2->die_mark;
6056 die1->die_mark = die2->die_mark = ++(*mark);
6058 if (die1->die_tag != die2->die_tag)
6059 return 0;
6061 for (a1 = die1->die_attr, a2 = die2->die_attr;
6062 a1 && a2;
6063 a1 = a1->dw_attr_next, a2 = a2->dw_attr_next)
6064 if (!same_attr_p (a1, a2, mark))
6065 return 0;
6066 if (a1 || a2)
6067 return 0;
6069 for (c1 = die1->die_child, c2 = die2->die_child;
6070 c1 && c2;
6071 c1 = c1->die_sib, c2 = c2->die_sib)
6072 if (!same_die_p (c1, c2, mark))
6073 return 0;
6074 if (c1 || c2)
6075 return 0;
6077 return 1;
6080 /* Do the dies look the same? Wrapper around same_die_p. */
6082 static int
6083 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6085 int mark = 0;
6086 int ret = same_die_p (die1, die2, &mark);
6088 unmark_all_dies (die1);
6089 unmark_all_dies (die2);
6091 return ret;
6094 /* The prefix to attach to symbols on DIEs in the current comdat debug
6095 info section. */
6096 static char *comdat_symbol_id;
6098 /* The index of the current symbol within the current comdat CU. */
6099 static unsigned int comdat_symbol_number;
6101 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6102 children, and set comdat_symbol_id accordingly. */
6104 static void
6105 compute_section_prefix (dw_die_ref unit_die)
6107 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6108 const char *base = die_name ? lbasename (die_name) : "anonymous";
6109 char *name = alloca (strlen (base) + 64);
6110 char *p;
6111 int i, mark;
6112 unsigned char checksum[16];
6113 struct md5_ctx ctx;
6115 /* Compute the checksum of the DIE, then append part of it as hex digits to
6116 the name filename of the unit. */
6118 md5_init_ctx (&ctx);
6119 mark = 0;
6120 die_checksum (unit_die, &ctx, &mark);
6121 unmark_all_dies (unit_die);
6122 md5_finish_ctx (&ctx, checksum);
6124 sprintf (name, "%s.", base);
6125 clean_symbol_name (name);
6127 p = name + strlen (name);
6128 for (i = 0; i < 4; i++)
6130 sprintf (p, "%.2x", checksum[i]);
6131 p += 2;
6134 comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
6135 comdat_symbol_number = 0;
6138 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6140 static int
6141 is_type_die (dw_die_ref die)
6143 switch (die->die_tag)
6145 case DW_TAG_array_type:
6146 case DW_TAG_class_type:
6147 case DW_TAG_enumeration_type:
6148 case DW_TAG_pointer_type:
6149 case DW_TAG_reference_type:
6150 case DW_TAG_string_type:
6151 case DW_TAG_structure_type:
6152 case DW_TAG_subroutine_type:
6153 case DW_TAG_union_type:
6154 case DW_TAG_ptr_to_member_type:
6155 case DW_TAG_set_type:
6156 case DW_TAG_subrange_type:
6157 case DW_TAG_base_type:
6158 case DW_TAG_const_type:
6159 case DW_TAG_file_type:
6160 case DW_TAG_packed_type:
6161 case DW_TAG_volatile_type:
6162 case DW_TAG_typedef:
6163 return 1;
6164 default:
6165 return 0;
6169 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6170 Basically, we want to choose the bits that are likely to be shared between
6171 compilations (types) and leave out the bits that are specific to individual
6172 compilations (functions). */
6174 static int
6175 is_comdat_die (dw_die_ref c)
6177 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6178 we do for stabs. The advantage is a greater likelihood of sharing between
6179 objects that don't include headers in the same order (and therefore would
6180 put the base types in a different comdat). jason 8/28/00 */
6182 if (c->die_tag == DW_TAG_base_type)
6183 return 0;
6185 if (c->die_tag == DW_TAG_pointer_type
6186 || c->die_tag == DW_TAG_reference_type
6187 || c->die_tag == DW_TAG_const_type
6188 || c->die_tag == DW_TAG_volatile_type)
6190 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6192 return t ? is_comdat_die (t) : 0;
6195 return is_type_die (c);
6198 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6199 compilation unit. */
6201 static int
6202 is_symbol_die (dw_die_ref c)
6204 return (is_type_die (c)
6205 || (get_AT (c, DW_AT_declaration)
6206 && !get_AT (c, DW_AT_specification)));
6209 static char *
6210 gen_internal_sym (const char *prefix)
6212 char buf[256];
6214 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6215 return xstrdup (buf);
6218 /* Assign symbols to all worthy DIEs under DIE. */
6220 static void
6221 assign_symbol_names (dw_die_ref die)
6223 dw_die_ref c;
6225 if (is_symbol_die (die))
6227 if (comdat_symbol_id)
6229 char *p = alloca (strlen (comdat_symbol_id) + 64);
6231 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6232 comdat_symbol_id, comdat_symbol_number++);
6233 die->die_symbol = xstrdup (p);
6235 else
6236 die->die_symbol = gen_internal_sym ("LDIE");
6239 for (c = die->die_child; c != NULL; c = c->die_sib)
6240 assign_symbol_names (c);
6243 struct cu_hash_table_entry
6245 dw_die_ref cu;
6246 unsigned min_comdat_num, max_comdat_num;
6247 struct cu_hash_table_entry *next;
6250 /* Routines to manipulate hash table of CUs. */
6251 static hashval_t
6252 htab_cu_hash (const void *of)
6254 const struct cu_hash_table_entry *entry = of;
6256 return htab_hash_string (entry->cu->die_symbol);
6259 static int
6260 htab_cu_eq (const void *of1, const void *of2)
6262 const struct cu_hash_table_entry *entry1 = of1;
6263 const struct die_struct *entry2 = of2;
6265 return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
6268 static void
6269 htab_cu_del (void *what)
6271 struct cu_hash_table_entry *next, *entry = what;
6273 while (entry)
6275 next = entry->next;
6276 free (entry);
6277 entry = next;
6281 /* Check whether we have already seen this CU and set up SYM_NUM
6282 accordingly. */
6283 static int
6284 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6286 struct cu_hash_table_entry dummy;
6287 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6289 dummy.max_comdat_num = 0;
6291 slot = (struct cu_hash_table_entry **)
6292 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6293 INSERT);
6294 entry = *slot;
6296 for (; entry; last = entry, entry = entry->next)
6298 if (same_die_p_wrap (cu, entry->cu))
6299 break;
6302 if (entry)
6304 *sym_num = entry->min_comdat_num;
6305 return 1;
6308 entry = xcalloc (1, sizeof (struct cu_hash_table_entry));
6309 entry->cu = cu;
6310 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6311 entry->next = *slot;
6312 *slot = entry;
6314 return 0;
6317 /* Record SYM_NUM to record of CU in HTABLE. */
6318 static void
6319 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6321 struct cu_hash_table_entry **slot, *entry;
6323 slot = (struct cu_hash_table_entry **)
6324 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6325 NO_INSERT);
6326 entry = *slot;
6328 entry->max_comdat_num = sym_num;
6331 /* Traverse the DIE (which is always comp_unit_die), and set up
6332 additional compilation units for each of the include files we see
6333 bracketed by BINCL/EINCL. */
6335 static void
6336 break_out_includes (dw_die_ref die)
6338 dw_die_ref *ptr;
6339 dw_die_ref unit = NULL;
6340 limbo_die_node *node, **pnode;
6341 htab_t cu_hash_table;
6343 for (ptr = &(die->die_child); *ptr;)
6345 dw_die_ref c = *ptr;
6347 if (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6348 || (unit && is_comdat_die (c)))
6350 /* This DIE is for a secondary CU; remove it from the main one. */
6351 *ptr = c->die_sib;
6353 if (c->die_tag == DW_TAG_GNU_BINCL)
6355 unit = push_new_compile_unit (unit, c);
6356 free_die (c);
6358 else if (c->die_tag == DW_TAG_GNU_EINCL)
6360 unit = pop_compile_unit (unit);
6361 free_die (c);
6363 else
6364 add_child_die (unit, c);
6366 else
6368 /* Leave this DIE in the main CU. */
6369 ptr = &(c->die_sib);
6370 continue;
6374 #if 0
6375 /* We can only use this in debugging, since the frontend doesn't check
6376 to make sure that we leave every include file we enter. */
6377 gcc_assert (!unit);
6378 #endif
6380 assign_symbol_names (die);
6381 cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6382 for (node = limbo_die_list, pnode = &limbo_die_list;
6383 node;
6384 node = node->next)
6386 int is_dupl;
6388 compute_section_prefix (node->die);
6389 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6390 &comdat_symbol_number);
6391 assign_symbol_names (node->die);
6392 if (is_dupl)
6393 *pnode = node->next;
6394 else
6396 pnode = &node->next;
6397 record_comdat_symbol_number (node->die, cu_hash_table,
6398 comdat_symbol_number);
6401 htab_delete (cu_hash_table);
6404 /* Traverse the DIE and add a sibling attribute if it may have the
6405 effect of speeding up access to siblings. To save some space,
6406 avoid generating sibling attributes for DIE's without children. */
6408 static void
6409 add_sibling_attributes (dw_die_ref die)
6411 dw_die_ref c;
6413 if (die->die_tag != DW_TAG_compile_unit
6414 && die->die_sib && die->die_child != NULL)
6415 /* Add the sibling link to the front of the attribute list. */
6416 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
6418 for (c = die->die_child; c != NULL; c = c->die_sib)
6419 add_sibling_attributes (c);
6422 /* Output all location lists for the DIE and its children. */
6424 static void
6425 output_location_lists (dw_die_ref die)
6427 dw_die_ref c;
6428 dw_attr_ref d_attr;
6430 for (d_attr = die->die_attr; d_attr; d_attr = d_attr->dw_attr_next)
6431 if (AT_class (d_attr) == dw_val_class_loc_list)
6432 output_loc_list (AT_loc_list (d_attr));
6434 for (c = die->die_child; c != NULL; c = c->die_sib)
6435 output_location_lists (c);
6439 /* The format of each DIE (and its attribute value pairs) is encoded in an
6440 abbreviation table. This routine builds the abbreviation table and assigns
6441 a unique abbreviation id for each abbreviation entry. The children of each
6442 die are visited recursively. */
6444 static void
6445 build_abbrev_table (dw_die_ref die)
6447 unsigned long abbrev_id;
6448 unsigned int n_alloc;
6449 dw_die_ref c;
6450 dw_attr_ref d_attr, a_attr;
6452 /* Scan the DIE references, and mark as external any that refer to
6453 DIEs from other CUs (i.e. those which are not marked). */
6454 for (d_attr = die->die_attr; d_attr; d_attr = d_attr->dw_attr_next)
6455 if (AT_class (d_attr) == dw_val_class_die_ref
6456 && AT_ref (d_attr)->die_mark == 0)
6458 gcc_assert (AT_ref (d_attr)->die_symbol);
6460 set_AT_ref_external (d_attr, 1);
6463 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6465 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6467 if (abbrev->die_tag == die->die_tag)
6469 if ((abbrev->die_child != NULL) == (die->die_child != NULL))
6471 a_attr = abbrev->die_attr;
6472 d_attr = die->die_attr;
6474 while (a_attr != NULL && d_attr != NULL)
6476 if ((a_attr->dw_attr != d_attr->dw_attr)
6477 || (value_format (a_attr) != value_format (d_attr)))
6478 break;
6480 a_attr = a_attr->dw_attr_next;
6481 d_attr = d_attr->dw_attr_next;
6484 if (a_attr == NULL && d_attr == NULL)
6485 break;
6490 if (abbrev_id >= abbrev_die_table_in_use)
6492 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
6494 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
6495 abbrev_die_table = ggc_realloc (abbrev_die_table,
6496 sizeof (dw_die_ref) * n_alloc);
6498 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
6499 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
6500 abbrev_die_table_allocated = n_alloc;
6503 ++abbrev_die_table_in_use;
6504 abbrev_die_table[abbrev_id] = die;
6507 die->die_abbrev = abbrev_id;
6508 for (c = die->die_child; c != NULL; c = c->die_sib)
6509 build_abbrev_table (c);
6512 /* Return the power-of-two number of bytes necessary to represent VALUE. */
6514 static int
6515 constant_size (long unsigned int value)
6517 int log;
6519 if (value == 0)
6520 log = 0;
6521 else
6522 log = floor_log2 (value);
6524 log = log / 8;
6525 log = 1 << (floor_log2 (log) + 1);
6527 return log;
6530 /* Return the size of a DIE as it is represented in the
6531 .debug_info section. */
6533 static unsigned long
6534 size_of_die (dw_die_ref die)
6536 unsigned long size = 0;
6537 dw_attr_ref a;
6539 size += size_of_uleb128 (die->die_abbrev);
6540 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
6542 switch (AT_class (a))
6544 case dw_val_class_addr:
6545 size += DWARF2_ADDR_SIZE;
6546 break;
6547 case dw_val_class_offset:
6548 size += DWARF_OFFSET_SIZE;
6549 break;
6550 case dw_val_class_loc:
6552 unsigned long lsize = size_of_locs (AT_loc (a));
6554 /* Block length. */
6555 size += constant_size (lsize);
6556 size += lsize;
6558 break;
6559 case dw_val_class_loc_list:
6560 size += DWARF_OFFSET_SIZE;
6561 break;
6562 case dw_val_class_range_list:
6563 size += DWARF_OFFSET_SIZE;
6564 break;
6565 case dw_val_class_const:
6566 size += size_of_sleb128 (AT_int (a));
6567 break;
6568 case dw_val_class_unsigned_const:
6569 size += constant_size (AT_unsigned (a));
6570 break;
6571 case dw_val_class_long_long:
6572 size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
6573 break;
6574 case dw_val_class_vec:
6575 size += 1 + (a->dw_attr_val.v.val_vec.length
6576 * a->dw_attr_val.v.val_vec.elt_size); /* block */
6577 break;
6578 case dw_val_class_flag:
6579 size += 1;
6580 break;
6581 case dw_val_class_die_ref:
6582 if (AT_ref_external (a))
6583 size += DWARF2_ADDR_SIZE;
6584 else
6585 size += DWARF_OFFSET_SIZE;
6586 break;
6587 case dw_val_class_fde_ref:
6588 size += DWARF_OFFSET_SIZE;
6589 break;
6590 case dw_val_class_lbl_id:
6591 size += DWARF2_ADDR_SIZE;
6592 break;
6593 case dw_val_class_lbl_offset:
6594 size += DWARF_OFFSET_SIZE;
6595 break;
6596 case dw_val_class_str:
6597 if (AT_string_form (a) == DW_FORM_strp)
6598 size += DWARF_OFFSET_SIZE;
6599 else
6600 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
6601 break;
6602 default:
6603 gcc_unreachable ();
6607 return size;
6610 /* Size the debugging information associated with a given DIE. Visits the
6611 DIE's children recursively. Updates the global variable next_die_offset, on
6612 each time through. Uses the current value of next_die_offset to update the
6613 die_offset field in each DIE. */
6615 static void
6616 calc_die_sizes (dw_die_ref die)
6618 dw_die_ref c;
6620 die->die_offset = next_die_offset;
6621 next_die_offset += size_of_die (die);
6623 for (c = die->die_child; c != NULL; c = c->die_sib)
6624 calc_die_sizes (c);
6626 if (die->die_child != NULL)
6627 /* Count the null byte used to terminate sibling lists. */
6628 next_die_offset += 1;
6631 /* Set the marks for a die and its children. We do this so
6632 that we know whether or not a reference needs to use FORM_ref_addr; only
6633 DIEs in the same CU will be marked. We used to clear out the offset
6634 and use that as the flag, but ran into ordering problems. */
6636 static void
6637 mark_dies (dw_die_ref die)
6639 dw_die_ref c;
6641 gcc_assert (!die->die_mark);
6643 die->die_mark = 1;
6644 for (c = die->die_child; c; c = c->die_sib)
6645 mark_dies (c);
6648 /* Clear the marks for a die and its children. */
6650 static void
6651 unmark_dies (dw_die_ref die)
6653 dw_die_ref c;
6655 gcc_assert (die->die_mark);
6657 die->die_mark = 0;
6658 for (c = die->die_child; c; c = c->die_sib)
6659 unmark_dies (c);
6662 /* Clear the marks for a die, its children and referred dies. */
6664 static void
6665 unmark_all_dies (dw_die_ref die)
6667 dw_die_ref c;
6668 dw_attr_ref a;
6670 if (!die->die_mark)
6671 return;
6672 die->die_mark = 0;
6674 for (c = die->die_child; c; c = c->die_sib)
6675 unmark_all_dies (c);
6677 for (a = die->die_attr; a; a = a->dw_attr_next)
6678 if (AT_class (a) == dw_val_class_die_ref)
6679 unmark_all_dies (AT_ref (a));
6682 /* Return the size of the .debug_pubnames table generated for the
6683 compilation unit. */
6685 static unsigned long
6686 size_of_pubnames (void)
6688 unsigned long size;
6689 unsigned i;
6691 size = DWARF_PUBNAMES_HEADER_SIZE;
6692 for (i = 0; i < pubname_table_in_use; i++)
6694 pubname_ref p = &pubname_table[i];
6695 size += DWARF_OFFSET_SIZE + strlen (p->name) + 1;
6698 size += DWARF_OFFSET_SIZE;
6699 return size;
6702 /* Return the size of the information in the .debug_aranges section. */
6704 static unsigned long
6705 size_of_aranges (void)
6707 unsigned long size;
6709 size = DWARF_ARANGES_HEADER_SIZE;
6711 /* Count the address/length pair for this compilation unit. */
6712 size += 2 * DWARF2_ADDR_SIZE;
6713 size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
6715 /* Count the two zero words used to terminated the address range table. */
6716 size += 2 * DWARF2_ADDR_SIZE;
6717 return size;
6720 /* Select the encoding of an attribute value. */
6722 static enum dwarf_form
6723 value_format (dw_attr_ref a)
6725 switch (a->dw_attr_val.val_class)
6727 case dw_val_class_addr:
6728 return DW_FORM_addr;
6729 case dw_val_class_range_list:
6730 case dw_val_class_offset:
6731 switch (DWARF_OFFSET_SIZE)
6733 case 4:
6734 return DW_FORM_data4;
6735 case 8:
6736 return DW_FORM_data8;
6737 default:
6738 gcc_unreachable ();
6740 case dw_val_class_loc_list:
6741 /* FIXME: Could be DW_FORM_data8, with a > 32 bit size
6742 .debug_loc section */
6743 return DW_FORM_data4;
6744 case dw_val_class_loc:
6745 switch (constant_size (size_of_locs (AT_loc (a))))
6747 case 1:
6748 return DW_FORM_block1;
6749 case 2:
6750 return DW_FORM_block2;
6751 default:
6752 gcc_unreachable ();
6754 case dw_val_class_const:
6755 return DW_FORM_sdata;
6756 case dw_val_class_unsigned_const:
6757 switch (constant_size (AT_unsigned (a)))
6759 case 1:
6760 return DW_FORM_data1;
6761 case 2:
6762 return DW_FORM_data2;
6763 case 4:
6764 return DW_FORM_data4;
6765 case 8:
6766 return DW_FORM_data8;
6767 default:
6768 gcc_unreachable ();
6770 case dw_val_class_long_long:
6771 return DW_FORM_block1;
6772 case dw_val_class_vec:
6773 return DW_FORM_block1;
6774 case dw_val_class_flag:
6775 return DW_FORM_flag;
6776 case dw_val_class_die_ref:
6777 if (AT_ref_external (a))
6778 return DW_FORM_ref_addr;
6779 else
6780 return DW_FORM_ref;
6781 case dw_val_class_fde_ref:
6782 return DW_FORM_data;
6783 case dw_val_class_lbl_id:
6784 return DW_FORM_addr;
6785 case dw_val_class_lbl_offset:
6786 return DW_FORM_data;
6787 case dw_val_class_str:
6788 return AT_string_form (a);
6790 default:
6791 gcc_unreachable ();
6795 /* Output the encoding of an attribute value. */
6797 static void
6798 output_value_format (dw_attr_ref a)
6800 enum dwarf_form form = value_format (a);
6802 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
6805 /* Output the .debug_abbrev section which defines the DIE abbreviation
6806 table. */
6808 static void
6809 output_abbrev_section (void)
6811 unsigned long abbrev_id;
6813 dw_attr_ref a_attr;
6815 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6817 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6819 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
6820 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
6821 dwarf_tag_name (abbrev->die_tag));
6823 if (abbrev->die_child != NULL)
6824 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
6825 else
6826 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
6828 for (a_attr = abbrev->die_attr; a_attr != NULL;
6829 a_attr = a_attr->dw_attr_next)
6831 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
6832 dwarf_attr_name (a_attr->dw_attr));
6833 output_value_format (a_attr);
6836 dw2_asm_output_data (1, 0, NULL);
6837 dw2_asm_output_data (1, 0, NULL);
6840 /* Terminate the table. */
6841 dw2_asm_output_data (1, 0, NULL);
6844 /* Output a symbol we can use to refer to this DIE from another CU. */
6846 static inline void
6847 output_die_symbol (dw_die_ref die)
6849 char *sym = die->die_symbol;
6851 if (sym == 0)
6852 return;
6854 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
6855 /* We make these global, not weak; if the target doesn't support
6856 .linkonce, it doesn't support combining the sections, so debugging
6857 will break. */
6858 targetm.asm_out.globalize_label (asm_out_file, sym);
6860 ASM_OUTPUT_LABEL (asm_out_file, sym);
6863 /* Return a new location list, given the begin and end range, and the
6864 expression. gensym tells us whether to generate a new internal symbol for
6865 this location list node, which is done for the head of the list only. */
6867 static inline dw_loc_list_ref
6868 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
6869 const char *section, unsigned int gensym)
6871 dw_loc_list_ref retlist = ggc_alloc_cleared (sizeof (dw_loc_list_node));
6873 retlist->begin = begin;
6874 retlist->end = end;
6875 retlist->expr = expr;
6876 retlist->section = section;
6877 if (gensym)
6878 retlist->ll_symbol = gen_internal_sym ("LLST");
6880 return retlist;
6883 /* Add a location description expression to a location list. */
6885 static inline void
6886 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
6887 const char *begin, const char *end,
6888 const char *section)
6890 dw_loc_list_ref *d;
6892 /* Find the end of the chain. */
6893 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
6896 /* Add a new location list node to the list. */
6897 *d = new_loc_list (descr, begin, end, section, 0);
6900 static void
6901 dwarf2out_switch_text_section (void)
6903 dw_fde_ref fde;
6905 gcc_assert (cfun);
6907 fde = &fde_table[fde_table_in_use - 1];
6908 fde->dw_fde_switched_sections = true;
6909 fde->dw_fde_hot_section_label = cfun->hot_section_label;
6910 fde->dw_fde_hot_section_end_label = cfun->hot_section_end_label;
6911 fde->dw_fde_unlikely_section_label = cfun->cold_section_label;
6912 fde->dw_fde_unlikely_section_end_label = cfun->cold_section_end_label;
6913 have_switched_text_section = true;
6916 /* Output the location list given to us. */
6918 static void
6919 output_loc_list (dw_loc_list_ref list_head)
6921 dw_loc_list_ref curr = list_head;
6923 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
6925 /* Walk the location list, and output each range + expression. */
6926 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
6928 unsigned long size;
6929 if (!separate_line_info_table_in_use && !have_switched_text_section)
6931 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
6932 "Location list begin address (%s)",
6933 list_head->ll_symbol);
6934 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
6935 "Location list end address (%s)",
6936 list_head->ll_symbol);
6938 else
6940 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
6941 "Location list begin address (%s)",
6942 list_head->ll_symbol);
6943 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
6944 "Location list end address (%s)",
6945 list_head->ll_symbol);
6947 size = size_of_locs (curr->expr);
6949 /* Output the block length for this list of location operations. */
6950 gcc_assert (size <= 0xffff);
6951 dw2_asm_output_data (2, size, "%s", "Location expression size");
6953 output_loc_sequence (curr->expr);
6956 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
6957 "Location list terminator begin (%s)",
6958 list_head->ll_symbol);
6959 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
6960 "Location list terminator end (%s)",
6961 list_head->ll_symbol);
6964 /* Output the DIE and its attributes. Called recursively to generate
6965 the definitions of each child DIE. */
6967 static void
6968 output_die (dw_die_ref die)
6970 dw_attr_ref a;
6971 dw_die_ref c;
6972 unsigned long size;
6974 /* If someone in another CU might refer to us, set up a symbol for
6975 them to point to. */
6976 if (die->die_symbol)
6977 output_die_symbol (die);
6979 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
6980 die->die_offset, dwarf_tag_name (die->die_tag));
6982 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
6984 const char *name = dwarf_attr_name (a->dw_attr);
6986 switch (AT_class (a))
6988 case dw_val_class_addr:
6989 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
6990 break;
6992 case dw_val_class_offset:
6993 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
6994 "%s", name);
6995 break;
6997 case dw_val_class_range_list:
6999 char *p = strchr (ranges_section_label, '\0');
7001 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
7002 a->dw_attr_val.v.val_offset);
7003 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
7004 "%s", name);
7005 *p = '\0';
7007 break;
7009 case dw_val_class_loc:
7010 size = size_of_locs (AT_loc (a));
7012 /* Output the block length for this list of location operations. */
7013 dw2_asm_output_data (constant_size (size), size, "%s", name);
7015 output_loc_sequence (AT_loc (a));
7016 break;
7018 case dw_val_class_const:
7019 /* ??? It would be slightly more efficient to use a scheme like is
7020 used for unsigned constants below, but gdb 4.x does not sign
7021 extend. Gdb 5.x does sign extend. */
7022 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
7023 break;
7025 case dw_val_class_unsigned_const:
7026 dw2_asm_output_data (constant_size (AT_unsigned (a)),
7027 AT_unsigned (a), "%s", name);
7028 break;
7030 case dw_val_class_long_long:
7032 unsigned HOST_WIDE_INT first, second;
7034 dw2_asm_output_data (1,
7035 2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7036 "%s", name);
7038 if (WORDS_BIG_ENDIAN)
7040 first = a->dw_attr_val.v.val_long_long.hi;
7041 second = a->dw_attr_val.v.val_long_long.low;
7043 else
7045 first = a->dw_attr_val.v.val_long_long.low;
7046 second = a->dw_attr_val.v.val_long_long.hi;
7049 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7050 first, "long long constant");
7051 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7052 second, NULL);
7054 break;
7056 case dw_val_class_vec:
7058 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
7059 unsigned int len = a->dw_attr_val.v.val_vec.length;
7060 unsigned int i;
7061 unsigned char *p;
7063 dw2_asm_output_data (1, len * elt_size, "%s", name);
7064 if (elt_size > sizeof (HOST_WIDE_INT))
7066 elt_size /= 2;
7067 len *= 2;
7069 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
7070 i < len;
7071 i++, p += elt_size)
7072 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
7073 "fp or vector constant word %u", i);
7074 break;
7077 case dw_val_class_flag:
7078 dw2_asm_output_data (1, AT_flag (a), "%s", name);
7079 break;
7081 case dw_val_class_loc_list:
7083 char *sym = AT_loc_list (a)->ll_symbol;
7085 gcc_assert (sym);
7086 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, "%s", name);
7088 break;
7090 case dw_val_class_die_ref:
7091 if (AT_ref_external (a))
7093 char *sym = AT_ref (a)->die_symbol;
7095 gcc_assert (sym);
7096 dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, "%s", name);
7098 else
7100 gcc_assert (AT_ref (a)->die_offset);
7101 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
7102 "%s", name);
7104 break;
7106 case dw_val_class_fde_ref:
7108 char l1[20];
7110 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
7111 a->dw_attr_val.v.val_fde_index * 2);
7112 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, "%s", name);
7114 break;
7116 case dw_val_class_lbl_id:
7117 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
7118 break;
7120 case dw_val_class_lbl_offset:
7121 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a), "%s", name);
7122 break;
7124 case dw_val_class_str:
7125 if (AT_string_form (a) == DW_FORM_strp)
7126 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
7127 a->dw_attr_val.v.val_str->label,
7128 "%s: \"%s\"", name, AT_string (a));
7129 else
7130 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
7131 break;
7133 default:
7134 gcc_unreachable ();
7138 for (c = die->die_child; c != NULL; c = c->die_sib)
7139 output_die (c);
7141 /* Add null byte to terminate sibling list. */
7142 if (die->die_child != NULL)
7143 dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
7144 die->die_offset);
7147 /* Output the compilation unit that appears at the beginning of the
7148 .debug_info section, and precedes the DIE descriptions. */
7150 static void
7151 output_compilation_unit_header (void)
7153 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7154 dw2_asm_output_data (4, 0xffffffff,
7155 "Initial length escape value indicating 64-bit DWARF extension");
7156 dw2_asm_output_data (DWARF_OFFSET_SIZE,
7157 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
7158 "Length of Compilation Unit Info");
7159 dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
7160 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
7161 "Offset Into Abbrev. Section");
7162 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
7165 /* Output the compilation unit DIE and its children. */
7167 static void
7168 output_comp_unit (dw_die_ref die, int output_if_empty)
7170 const char *secname;
7171 char *oldsym, *tmp;
7173 /* Unless we are outputting main CU, we may throw away empty ones. */
7174 if (!output_if_empty && die->die_child == NULL)
7175 return;
7177 /* Even if there are no children of this DIE, we must output the information
7178 about the compilation unit. Otherwise, on an empty translation unit, we
7179 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
7180 will then complain when examining the file. First mark all the DIEs in
7181 this CU so we know which get local refs. */
7182 mark_dies (die);
7184 build_abbrev_table (die);
7186 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
7187 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7188 calc_die_sizes (die);
7190 oldsym = die->die_symbol;
7191 if (oldsym)
7193 tmp = alloca (strlen (oldsym) + 24);
7195 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
7196 secname = tmp;
7197 die->die_symbol = NULL;
7199 else
7200 secname = (const char *) DEBUG_INFO_SECTION;
7202 /* Output debugging information. */
7203 named_section_flags (secname, SECTION_DEBUG);
7204 output_compilation_unit_header ();
7205 output_die (die);
7207 /* Leave the marks on the main CU, so we can check them in
7208 output_pubnames. */
7209 if (oldsym)
7211 unmark_dies (die);
7212 die->die_symbol = oldsym;
7216 /* The DWARF2 pubname for a nested thingy looks like "A::f". The
7217 output of lang_hooks.decl_printable_name for C++ looks like
7218 "A::f(int)". Let's drop the argument list, and maybe the scope. */
7220 static const char *
7221 dwarf2_name (tree decl, int scope)
7223 return lang_hooks.decl_printable_name (decl, scope ? 1 : 0);
7226 /* Add a new entry to .debug_pubnames if appropriate. */
7228 static void
7229 add_pubname (tree decl, dw_die_ref die)
7231 pubname_ref p;
7233 if (! TREE_PUBLIC (decl))
7234 return;
7236 if (pubname_table_in_use == pubname_table_allocated)
7238 pubname_table_allocated += PUBNAME_TABLE_INCREMENT;
7239 pubname_table
7240 = ggc_realloc (pubname_table,
7241 (pubname_table_allocated * sizeof (pubname_entry)));
7242 memset (pubname_table + pubname_table_in_use, 0,
7243 PUBNAME_TABLE_INCREMENT * sizeof (pubname_entry));
7246 p = &pubname_table[pubname_table_in_use++];
7247 p->die = die;
7248 p->name = xstrdup (dwarf2_name (decl, 1));
7251 /* Output the public names table used to speed up access to externally
7252 visible names. For now, only generate entries for externally
7253 visible procedures. */
7255 static void
7256 output_pubnames (void)
7258 unsigned i;
7259 unsigned long pubnames_length = size_of_pubnames ();
7261 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7262 dw2_asm_output_data (4, 0xffffffff,
7263 "Initial length escape value indicating 64-bit DWARF extension");
7264 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7265 "Length of Public Names Info");
7266 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7267 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7268 "Offset of Compilation Unit Info");
7269 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
7270 "Compilation Unit Length");
7272 for (i = 0; i < pubname_table_in_use; i++)
7274 pubname_ref pub = &pubname_table[i];
7276 /* We shouldn't see pubnames for DIEs outside of the main CU. */
7277 gcc_assert (pub->die->die_mark);
7279 dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
7280 "DIE offset");
7282 dw2_asm_output_nstring (pub->name, -1, "external name");
7285 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
7288 /* Add a new entry to .debug_aranges if appropriate. */
7290 static void
7291 add_arange (tree decl, dw_die_ref die)
7293 if (! DECL_SECTION_NAME (decl))
7294 return;
7296 if (arange_table_in_use == arange_table_allocated)
7298 arange_table_allocated += ARANGE_TABLE_INCREMENT;
7299 arange_table = ggc_realloc (arange_table,
7300 (arange_table_allocated
7301 * sizeof (dw_die_ref)));
7302 memset (arange_table + arange_table_in_use, 0,
7303 ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
7306 arange_table[arange_table_in_use++] = die;
7309 /* Output the information that goes into the .debug_aranges table.
7310 Namely, define the beginning and ending address range of the
7311 text section generated for this compilation unit. */
7313 static void
7314 output_aranges (void)
7316 unsigned i;
7317 unsigned long aranges_length = size_of_aranges ();
7319 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7320 dw2_asm_output_data (4, 0xffffffff,
7321 "Initial length escape value indicating 64-bit DWARF extension");
7322 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
7323 "Length of Address Ranges Info");
7324 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7325 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7326 "Offset of Compilation Unit Info");
7327 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
7328 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
7330 /* We need to align to twice the pointer size here. */
7331 if (DWARF_ARANGES_PAD_SIZE)
7333 /* Pad using a 2 byte words so that padding is correct for any
7334 pointer size. */
7335 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
7336 2 * DWARF2_ADDR_SIZE);
7337 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
7338 dw2_asm_output_data (2, 0, NULL);
7341 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
7342 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
7343 text_section_label, "Length");
7344 if (flag_reorder_blocks_and_partition)
7346 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
7347 "Address");
7348 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
7349 cold_text_section_label, "Length");
7352 for (i = 0; i < arange_table_in_use; i++)
7354 dw_die_ref die = arange_table[i];
7356 /* We shouldn't see aranges for DIEs outside of the main CU. */
7357 gcc_assert (die->die_mark);
7359 if (die->die_tag == DW_TAG_subprogram)
7361 dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
7362 "Address");
7363 dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
7364 get_AT_low_pc (die), "Length");
7366 else
7368 /* A static variable; extract the symbol from DW_AT_location.
7369 Note that this code isn't currently hit, as we only emit
7370 aranges for functions (jason 9/23/99). */
7371 dw_attr_ref a = get_AT (die, DW_AT_location);
7372 dw_loc_descr_ref loc;
7374 gcc_assert (a && AT_class (a) == dw_val_class_loc);
7376 loc = AT_loc (a);
7377 gcc_assert (loc->dw_loc_opc == DW_OP_addr);
7379 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
7380 loc->dw_loc_oprnd1.v.val_addr, "Address");
7381 dw2_asm_output_data (DWARF2_ADDR_SIZE,
7382 get_AT_unsigned (die, DW_AT_byte_size),
7383 "Length");
7387 /* Output the terminator words. */
7388 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7389 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7392 /* Add a new entry to .debug_ranges. Return the offset at which it
7393 was placed. */
7395 static unsigned int
7396 add_ranges (tree block)
7398 unsigned int in_use = ranges_table_in_use;
7400 if (in_use == ranges_table_allocated)
7402 ranges_table_allocated += RANGES_TABLE_INCREMENT;
7403 ranges_table
7404 = ggc_realloc (ranges_table, (ranges_table_allocated
7405 * sizeof (struct dw_ranges_struct)));
7406 memset (ranges_table + ranges_table_in_use, 0,
7407 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
7410 ranges_table[in_use].block_num = (block ? BLOCK_NUMBER (block) : 0);
7411 ranges_table_in_use = in_use + 1;
7413 return in_use * 2 * DWARF2_ADDR_SIZE;
7416 static void
7417 output_ranges (void)
7419 unsigned i;
7420 static const char *const start_fmt = "Offset 0x%x";
7421 const char *fmt = start_fmt;
7423 for (i = 0; i < ranges_table_in_use; i++)
7425 int block_num = ranges_table[i].block_num;
7427 if (block_num)
7429 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
7430 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
7432 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
7433 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
7435 /* If all code is in the text section, then the compilation
7436 unit base address defaults to DW_AT_low_pc, which is the
7437 base of the text section. */
7438 if (!separate_line_info_table_in_use && !have_switched_text_section)
7440 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
7441 text_section_label,
7442 fmt, i * 2 * DWARF2_ADDR_SIZE);
7443 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
7444 text_section_label, NULL);
7447 /* Otherwise, we add a DW_AT_entry_pc attribute to force the
7448 compilation unit base address to zero, which allows us to
7449 use absolute addresses, and not worry about whether the
7450 target supports cross-section arithmetic. */
7451 else
7453 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
7454 fmt, i * 2 * DWARF2_ADDR_SIZE);
7455 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
7458 fmt = NULL;
7460 else
7462 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7463 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7464 fmt = start_fmt;
7469 /* Data structure containing information about input files. */
7470 struct file_info
7472 char *path; /* Complete file name. */
7473 char *fname; /* File name part. */
7474 int length; /* Length of entire string. */
7475 int file_idx; /* Index in input file table. */
7476 int dir_idx; /* Index in directory table. */
7479 /* Data structure containing information about directories with source
7480 files. */
7481 struct dir_info
7483 char *path; /* Path including directory name. */
7484 int length; /* Path length. */
7485 int prefix; /* Index of directory entry which is a prefix. */
7486 int count; /* Number of files in this directory. */
7487 int dir_idx; /* Index of directory used as base. */
7488 int used; /* Used in the end? */
7491 /* Callback function for file_info comparison. We sort by looking at
7492 the directories in the path. */
7494 static int
7495 file_info_cmp (const void *p1, const void *p2)
7497 const struct file_info *s1 = p1;
7498 const struct file_info *s2 = p2;
7499 unsigned char *cp1;
7500 unsigned char *cp2;
7502 /* Take care of file names without directories. We need to make sure that
7503 we return consistent values to qsort since some will get confused if
7504 we return the same value when identical operands are passed in opposite
7505 orders. So if neither has a directory, return 0 and otherwise return
7506 1 or -1 depending on which one has the directory. */
7507 if ((s1->path == s1->fname || s2->path == s2->fname))
7508 return (s2->path == s2->fname) - (s1->path == s1->fname);
7510 cp1 = (unsigned char *) s1->path;
7511 cp2 = (unsigned char *) s2->path;
7513 while (1)
7515 ++cp1;
7516 ++cp2;
7517 /* Reached the end of the first path? If so, handle like above. */
7518 if ((cp1 == (unsigned char *) s1->fname)
7519 || (cp2 == (unsigned char *) s2->fname))
7520 return ((cp2 == (unsigned char *) s2->fname)
7521 - (cp1 == (unsigned char *) s1->fname));
7523 /* Character of current path component the same? */
7524 else if (*cp1 != *cp2)
7525 return *cp1 - *cp2;
7529 /* Output the directory table and the file name table. We try to minimize
7530 the total amount of memory needed. A heuristic is used to avoid large
7531 slowdowns with many input files. */
7533 static void
7534 output_file_names (void)
7536 struct file_info *files;
7537 struct dir_info *dirs;
7538 int *saved;
7539 int *savehere;
7540 int *backmap;
7541 size_t ndirs;
7542 int idx_offset;
7543 size_t i;
7544 int idx;
7546 /* Handle the case where file_table is empty. */
7547 if (VARRAY_ACTIVE_SIZE (file_table) <= 1)
7549 dw2_asm_output_data (1, 0, "End directory table");
7550 dw2_asm_output_data (1, 0, "End file name table");
7551 return;
7554 /* Allocate the various arrays we need. */
7555 files = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (struct file_info));
7556 dirs = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (struct dir_info));
7558 /* Sort the file names. */
7559 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7561 char *f;
7563 /* Skip all leading "./". */
7564 f = VARRAY_CHAR_PTR (file_table, i);
7565 while (f[0] == '.' && f[1] == '/')
7566 f += 2;
7568 /* Create a new array entry. */
7569 files[i].path = f;
7570 files[i].length = strlen (f);
7571 files[i].file_idx = i;
7573 /* Search for the file name part. */
7574 f = strrchr (f, '/');
7575 files[i].fname = f == NULL ? files[i].path : f + 1;
7578 qsort (files + 1, VARRAY_ACTIVE_SIZE (file_table) - 1,
7579 sizeof (files[0]), file_info_cmp);
7581 /* Find all the different directories used. */
7582 dirs[0].path = files[1].path;
7583 dirs[0].length = files[1].fname - files[1].path;
7584 dirs[0].prefix = -1;
7585 dirs[0].count = 1;
7586 dirs[0].dir_idx = 0;
7587 dirs[0].used = 0;
7588 files[1].dir_idx = 0;
7589 ndirs = 1;
7591 for (i = 2; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7592 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
7593 && memcmp (dirs[ndirs - 1].path, files[i].path,
7594 dirs[ndirs - 1].length) == 0)
7596 /* Same directory as last entry. */
7597 files[i].dir_idx = ndirs - 1;
7598 ++dirs[ndirs - 1].count;
7600 else
7602 size_t j;
7604 /* This is a new directory. */
7605 dirs[ndirs].path = files[i].path;
7606 dirs[ndirs].length = files[i].fname - files[i].path;
7607 dirs[ndirs].count = 1;
7608 dirs[ndirs].dir_idx = ndirs;
7609 dirs[ndirs].used = 0;
7610 files[i].dir_idx = ndirs;
7612 /* Search for a prefix. */
7613 dirs[ndirs].prefix = -1;
7614 for (j = 0; j < ndirs; j++)
7615 if (dirs[j].length < dirs[ndirs].length
7616 && dirs[j].length > 1
7617 && (dirs[ndirs].prefix == -1
7618 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
7619 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
7620 dirs[ndirs].prefix = j;
7622 ++ndirs;
7625 /* Now to the actual work. We have to find a subset of the directories which
7626 allow expressing the file name using references to the directory table
7627 with the least amount of characters. We do not do an exhaustive search
7628 where we would have to check out every combination of every single
7629 possible prefix. Instead we use a heuristic which provides nearly optimal
7630 results in most cases and never is much off. */
7631 saved = alloca (ndirs * sizeof (int));
7632 savehere = alloca (ndirs * sizeof (int));
7634 memset (saved, '\0', ndirs * sizeof (saved[0]));
7635 for (i = 0; i < ndirs; i++)
7637 size_t j;
7638 int total;
7640 /* We can always save some space for the current directory. But this
7641 does not mean it will be enough to justify adding the directory. */
7642 savehere[i] = dirs[i].length;
7643 total = (savehere[i] - saved[i]) * dirs[i].count;
7645 for (j = i + 1; j < ndirs; j++)
7647 savehere[j] = 0;
7648 if (saved[j] < dirs[i].length)
7650 /* Determine whether the dirs[i] path is a prefix of the
7651 dirs[j] path. */
7652 int k;
7654 k = dirs[j].prefix;
7655 while (k != -1 && k != (int) i)
7656 k = dirs[k].prefix;
7658 if (k == (int) i)
7660 /* Yes it is. We can possibly safe some memory but
7661 writing the filenames in dirs[j] relative to
7662 dirs[i]. */
7663 savehere[j] = dirs[i].length;
7664 total += (savehere[j] - saved[j]) * dirs[j].count;
7669 /* Check whether we can safe enough to justify adding the dirs[i]
7670 directory. */
7671 if (total > dirs[i].length + 1)
7673 /* It's worthwhile adding. */
7674 for (j = i; j < ndirs; j++)
7675 if (savehere[j] > 0)
7677 /* Remember how much we saved for this directory so far. */
7678 saved[j] = savehere[j];
7680 /* Remember the prefix directory. */
7681 dirs[j].dir_idx = i;
7686 /* We have to emit them in the order they appear in the file_table array
7687 since the index is used in the debug info generation. To do this
7688 efficiently we generate a back-mapping of the indices first. */
7689 backmap = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (int));
7690 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7692 backmap[files[i].file_idx] = i;
7694 /* Mark this directory as used. */
7695 dirs[dirs[files[i].dir_idx].dir_idx].used = 1;
7698 /* That was it. We are ready to emit the information. First emit the
7699 directory name table. We have to make sure the first actually emitted
7700 directory name has index one; zero is reserved for the current working
7701 directory. Make sure we do not confuse these indices with the one for the
7702 constructed table (even though most of the time they are identical). */
7703 idx = 1;
7704 idx_offset = dirs[0].length > 0 ? 1 : 0;
7705 for (i = 1 - idx_offset; i < ndirs; i++)
7706 if (dirs[i].used != 0)
7708 dirs[i].used = idx++;
7709 dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
7710 "Directory Entry: 0x%x", dirs[i].used);
7713 dw2_asm_output_data (1, 0, "End directory table");
7715 /* Correct the index for the current working directory entry if it
7716 exists. */
7717 if (idx_offset == 0)
7718 dirs[0].used = 0;
7720 /* Now write all the file names. */
7721 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7723 int file_idx = backmap[i];
7724 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
7726 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
7727 "File Entry: 0x%lx", (unsigned long) i);
7729 /* Include directory index. */
7730 dw2_asm_output_data_uleb128 (dirs[dir_idx].used, NULL);
7732 /* Modification time. */
7733 dw2_asm_output_data_uleb128 (0, NULL);
7735 /* File length in bytes. */
7736 dw2_asm_output_data_uleb128 (0, NULL);
7739 dw2_asm_output_data (1, 0, "End file name table");
7743 /* Output the source line number correspondence information. This
7744 information goes into the .debug_line section. */
7746 static void
7747 output_line_info (void)
7749 char l1[20], l2[20], p1[20], p2[20];
7750 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
7751 char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
7752 unsigned opc;
7753 unsigned n_op_args;
7754 unsigned long lt_index;
7755 unsigned long current_line;
7756 long line_offset;
7757 long line_delta;
7758 unsigned long current_file;
7759 unsigned long function;
7761 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
7762 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
7763 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
7764 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
7766 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7767 dw2_asm_output_data (4, 0xffffffff,
7768 "Initial length escape value indicating 64-bit DWARF extension");
7769 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
7770 "Length of Source Line Info");
7771 ASM_OUTPUT_LABEL (asm_out_file, l1);
7773 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7774 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
7775 ASM_OUTPUT_LABEL (asm_out_file, p1);
7777 /* Define the architecture-dependent minimum instruction length (in
7778 bytes). In this implementation of DWARF, this field is used for
7779 information purposes only. Since GCC generates assembly language,
7780 we have no a priori knowledge of how many instruction bytes are
7781 generated for each source line, and therefore can use only the
7782 DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
7783 commands. Accordingly, we fix this as `1', which is "correct
7784 enough" for all architectures, and don't let the target override. */
7785 dw2_asm_output_data (1, 1,
7786 "Minimum Instruction Length");
7788 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
7789 "Default is_stmt_start flag");
7790 dw2_asm_output_data (1, DWARF_LINE_BASE,
7791 "Line Base Value (Special Opcodes)");
7792 dw2_asm_output_data (1, DWARF_LINE_RANGE,
7793 "Line Range Value (Special Opcodes)");
7794 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
7795 "Special Opcode Base");
7797 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
7799 switch (opc)
7801 case DW_LNS_advance_pc:
7802 case DW_LNS_advance_line:
7803 case DW_LNS_set_file:
7804 case DW_LNS_set_column:
7805 case DW_LNS_fixed_advance_pc:
7806 n_op_args = 1;
7807 break;
7808 default:
7809 n_op_args = 0;
7810 break;
7813 dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
7814 opc, n_op_args);
7817 /* Write out the information about the files we use. */
7818 output_file_names ();
7819 ASM_OUTPUT_LABEL (asm_out_file, p2);
7821 /* We used to set the address register to the first location in the text
7822 section here, but that didn't accomplish anything since we already
7823 have a line note for the opening brace of the first function. */
7825 /* Generate the line number to PC correspondence table, encoded as
7826 a series of state machine operations. */
7827 current_file = 1;
7828 current_line = 1;
7830 if (cfun
7831 && (last_text_section == in_unlikely_executed_text
7832 || (last_text_section == in_named
7833 && last_text_section_name == cfun->unlikely_text_section_name)))
7834 strcpy (prev_line_label, cfun->cold_section_label);
7835 else
7836 strcpy (prev_line_label, text_section_label);
7837 for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
7839 dw_line_info_ref line_info = &line_info_table[lt_index];
7841 #if 0
7842 /* Disable this optimization for now; GDB wants to see two line notes
7843 at the beginning of a function so it can find the end of the
7844 prologue. */
7846 /* Don't emit anything for redundant notes. Just updating the
7847 address doesn't accomplish anything, because we already assume
7848 that anything after the last address is this line. */
7849 if (line_info->dw_line_num == current_line
7850 && line_info->dw_file_num == current_file)
7851 continue;
7852 #endif
7854 /* Emit debug info for the address of the current line.
7856 Unfortunately, we have little choice here currently, and must always
7857 use the most general form. GCC does not know the address delta
7858 itself, so we can't use DW_LNS_advance_pc. Many ports do have length
7859 attributes which will give an upper bound on the address range. We
7860 could perhaps use length attributes to determine when it is safe to
7861 use DW_LNS_fixed_advance_pc. */
7863 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
7864 if (0)
7866 /* This can handle deltas up to 0xffff. This takes 3 bytes. */
7867 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7868 "DW_LNS_fixed_advance_pc");
7869 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
7871 else
7873 /* This can handle any delta. This takes
7874 4+DWARF2_ADDR_SIZE bytes. */
7875 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7876 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7877 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7878 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7881 strcpy (prev_line_label, line_label);
7883 /* Emit debug info for the source file of the current line, if
7884 different from the previous line. */
7885 if (line_info->dw_file_num != current_file)
7887 current_file = line_info->dw_file_num;
7888 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
7889 dw2_asm_output_data_uleb128 (current_file, "(\"%s\")",
7890 VARRAY_CHAR_PTR (file_table,
7891 current_file));
7894 /* Emit debug info for the current line number, choosing the encoding
7895 that uses the least amount of space. */
7896 if (line_info->dw_line_num != current_line)
7898 line_offset = line_info->dw_line_num - current_line;
7899 line_delta = line_offset - DWARF_LINE_BASE;
7900 current_line = line_info->dw_line_num;
7901 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
7902 /* This can handle deltas from -10 to 234, using the current
7903 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE. This
7904 takes 1 byte. */
7905 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
7906 "line %lu", current_line);
7907 else
7909 /* This can handle any delta. This takes at least 4 bytes,
7910 depending on the value being encoded. */
7911 dw2_asm_output_data (1, DW_LNS_advance_line,
7912 "advance to line %lu", current_line);
7913 dw2_asm_output_data_sleb128 (line_offset, NULL);
7914 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7917 else
7918 /* We still need to start a new row, so output a copy insn. */
7919 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7922 /* Emit debug info for the address of the end of the function. */
7923 if (0)
7925 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7926 "DW_LNS_fixed_advance_pc");
7927 dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
7929 else
7931 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7932 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7933 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7934 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
7937 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
7938 dw2_asm_output_data_uleb128 (1, NULL);
7939 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
7941 function = 0;
7942 current_file = 1;
7943 current_line = 1;
7944 for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
7946 dw_separate_line_info_ref line_info
7947 = &separate_line_info_table[lt_index];
7949 #if 0
7950 /* Don't emit anything for redundant notes. */
7951 if (line_info->dw_line_num == current_line
7952 && line_info->dw_file_num == current_file
7953 && line_info->function == function)
7954 goto cont;
7955 #endif
7957 /* Emit debug info for the address of the current line. If this is
7958 a new function, or the first line of a function, then we need
7959 to handle it differently. */
7960 ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
7961 lt_index);
7962 if (function != line_info->function)
7964 function = line_info->function;
7966 /* Set the address register to the first line in the function. */
7967 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7968 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7969 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7970 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7972 else
7974 /* ??? See the DW_LNS_advance_pc comment above. */
7975 if (0)
7977 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7978 "DW_LNS_fixed_advance_pc");
7979 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
7981 else
7983 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7984 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7985 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7986 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7990 strcpy (prev_line_label, line_label);
7992 /* Emit debug info for the source file of the current line, if
7993 different from the previous line. */
7994 if (line_info->dw_file_num != current_file)
7996 current_file = line_info->dw_file_num;
7997 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
7998 dw2_asm_output_data_uleb128 (current_file, "(\"%s\")",
7999 VARRAY_CHAR_PTR (file_table,
8000 current_file));
8003 /* Emit debug info for the current line number, choosing the encoding
8004 that uses the least amount of space. */
8005 if (line_info->dw_line_num != current_line)
8007 line_offset = line_info->dw_line_num - current_line;
8008 line_delta = line_offset - DWARF_LINE_BASE;
8009 current_line = line_info->dw_line_num;
8010 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
8011 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
8012 "line %lu", current_line);
8013 else
8015 dw2_asm_output_data (1, DW_LNS_advance_line,
8016 "advance to line %lu", current_line);
8017 dw2_asm_output_data_sleb128 (line_offset, NULL);
8018 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8021 else
8022 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8024 #if 0
8025 cont:
8026 #endif
8028 lt_index++;
8030 /* If we're done with a function, end its sequence. */
8031 if (lt_index == separate_line_info_table_in_use
8032 || separate_line_info_table[lt_index].function != function)
8034 current_file = 1;
8035 current_line = 1;
8037 /* Emit debug info for the address of the end of the function. */
8038 ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
8039 if (0)
8041 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8042 "DW_LNS_fixed_advance_pc");
8043 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8045 else
8047 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8048 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8049 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8050 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8053 /* Output the marker for the end of this sequence. */
8054 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
8055 dw2_asm_output_data_uleb128 (1, NULL);
8056 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
8060 /* Output the marker for the end of the line number info. */
8061 ASM_OUTPUT_LABEL (asm_out_file, l2);
8064 /* Given a pointer to a tree node for some base type, return a pointer to
8065 a DIE that describes the given type.
8067 This routine must only be called for GCC type nodes that correspond to
8068 Dwarf base (fundamental) types. */
8070 static dw_die_ref
8071 base_type_die (tree type)
8073 dw_die_ref base_type_result;
8074 const char *type_name;
8075 enum dwarf_type encoding;
8076 tree name = TYPE_NAME (type);
8078 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
8079 return 0;
8081 if (name)
8083 if (TREE_CODE (name) == TYPE_DECL)
8084 name = DECL_NAME (name);
8086 type_name = IDENTIFIER_POINTER (name);
8088 else
8089 type_name = "__unknown__";
8091 switch (TREE_CODE (type))
8093 case INTEGER_TYPE:
8094 /* Carefully distinguish the C character types, without messing
8095 up if the language is not C. Note that we check only for the names
8096 that contain spaces; other names might occur by coincidence in other
8097 languages. */
8098 if (! (TYPE_PRECISION (type) == CHAR_TYPE_SIZE
8099 && (TYPE_MAIN_VARIANT (type) == char_type_node
8100 || ! strcmp (type_name, "signed char")
8101 || ! strcmp (type_name, "unsigned char"))))
8103 if (TYPE_UNSIGNED (type))
8104 encoding = DW_ATE_unsigned;
8105 else
8106 encoding = DW_ATE_signed;
8107 break;
8109 /* else fall through. */
8111 case CHAR_TYPE:
8112 /* GNU Pascal/Ada CHAR type. Not used in C. */
8113 if (TYPE_UNSIGNED (type))
8114 encoding = DW_ATE_unsigned_char;
8115 else
8116 encoding = DW_ATE_signed_char;
8117 break;
8119 case REAL_TYPE:
8120 encoding = DW_ATE_float;
8121 break;
8123 /* Dwarf2 doesn't know anything about complex ints, so use
8124 a user defined type for it. */
8125 case COMPLEX_TYPE:
8126 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
8127 encoding = DW_ATE_complex_float;
8128 else
8129 encoding = DW_ATE_lo_user;
8130 break;
8132 case BOOLEAN_TYPE:
8133 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
8134 encoding = DW_ATE_boolean;
8135 break;
8137 default:
8138 /* No other TREE_CODEs are Dwarf fundamental types. */
8139 gcc_unreachable ();
8142 base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
8143 if (demangle_name_func)
8144 type_name = (*demangle_name_func) (type_name);
8146 add_AT_string (base_type_result, DW_AT_name, type_name);
8147 add_AT_unsigned (base_type_result, DW_AT_byte_size,
8148 int_size_in_bytes (type));
8149 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
8151 return base_type_result;
8154 /* Given a pointer to an arbitrary ..._TYPE tree node, return a pointer to
8155 the Dwarf "root" type for the given input type. The Dwarf "root" type of
8156 a given type is generally the same as the given type, except that if the
8157 given type is a pointer or reference type, then the root type of the given
8158 type is the root type of the "basis" type for the pointer or reference
8159 type. (This definition of the "root" type is recursive.) Also, the root
8160 type of a `const' qualified type or a `volatile' qualified type is the
8161 root type of the given type without the qualifiers. */
8163 static tree
8164 root_type (tree type)
8166 if (TREE_CODE (type) == ERROR_MARK)
8167 return error_mark_node;
8169 switch (TREE_CODE (type))
8171 case ERROR_MARK:
8172 return error_mark_node;
8174 case POINTER_TYPE:
8175 case REFERENCE_TYPE:
8176 return type_main_variant (root_type (TREE_TYPE (type)));
8178 default:
8179 return type_main_variant (type);
8183 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
8184 given input type is a Dwarf "fundamental" type. Otherwise return null. */
8186 static inline int
8187 is_base_type (tree type)
8189 switch (TREE_CODE (type))
8191 case ERROR_MARK:
8192 case VOID_TYPE:
8193 case INTEGER_TYPE:
8194 case REAL_TYPE:
8195 case COMPLEX_TYPE:
8196 case BOOLEAN_TYPE:
8197 case CHAR_TYPE:
8198 return 1;
8200 case ARRAY_TYPE:
8201 case RECORD_TYPE:
8202 case UNION_TYPE:
8203 case QUAL_UNION_TYPE:
8204 case ENUMERAL_TYPE:
8205 case FUNCTION_TYPE:
8206 case METHOD_TYPE:
8207 case POINTER_TYPE:
8208 case REFERENCE_TYPE:
8209 case OFFSET_TYPE:
8210 case LANG_TYPE:
8211 case VECTOR_TYPE:
8212 return 0;
8214 default:
8215 gcc_unreachable ();
8218 return 0;
8221 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
8222 node, return the size in bits for the type if it is a constant, or else
8223 return the alignment for the type if the type's size is not constant, or
8224 else return BITS_PER_WORD if the type actually turns out to be an
8225 ERROR_MARK node. */
8227 static inline unsigned HOST_WIDE_INT
8228 simple_type_size_in_bits (tree type)
8230 if (TREE_CODE (type) == ERROR_MARK)
8231 return BITS_PER_WORD;
8232 else if (TYPE_SIZE (type) == NULL_TREE)
8233 return 0;
8234 else if (host_integerp (TYPE_SIZE (type), 1))
8235 return tree_low_cst (TYPE_SIZE (type), 1);
8236 else
8237 return TYPE_ALIGN (type);
8240 /* Return true if the debug information for the given type should be
8241 emitted as a subrange type. */
8243 static inline bool
8244 is_subrange_type (tree type)
8246 tree subtype = TREE_TYPE (type);
8248 /* Subrange types are identified by the fact that they are integer
8249 types, and that they have a subtype which is either an integer type
8250 or an enumeral type. */
8252 if (TREE_CODE (type) != INTEGER_TYPE
8253 || subtype == NULL_TREE)
8254 return false;
8256 if (TREE_CODE (subtype) != INTEGER_TYPE
8257 && TREE_CODE (subtype) != ENUMERAL_TYPE)
8258 return false;
8260 if (TREE_CODE (type) == TREE_CODE (subtype)
8261 && int_size_in_bytes (type) == int_size_in_bytes (subtype)
8262 && TYPE_MIN_VALUE (type) != NULL
8263 && TYPE_MIN_VALUE (subtype) != NULL
8264 && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
8265 && TYPE_MAX_VALUE (type) != NULL
8266 && TYPE_MAX_VALUE (subtype) != NULL
8267 && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
8269 /* The type and its subtype have the same representation. If in
8270 addition the two types also have the same name, then the given
8271 type is not a subrange type, but rather a plain base type. */
8272 /* FIXME: brobecker/2004-03-22:
8273 Sizetype INTEGER_CSTs nodes are canonicalized. It should
8274 therefore be sufficient to check the TYPE_SIZE node pointers
8275 rather than checking the actual size. Unfortunately, we have
8276 found some cases, such as in the Ada "integer" type, where
8277 this is not the case. Until this problem is solved, we need to
8278 keep checking the actual size. */
8279 tree type_name = TYPE_NAME (type);
8280 tree subtype_name = TYPE_NAME (subtype);
8282 if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
8283 type_name = DECL_NAME (type_name);
8285 if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
8286 subtype_name = DECL_NAME (subtype_name);
8288 if (type_name == subtype_name)
8289 return false;
8292 return true;
8295 /* Given a pointer to a tree node for a subrange type, return a pointer
8296 to a DIE that describes the given type. */
8298 static dw_die_ref
8299 subrange_type_die (tree type, dw_die_ref context_die)
8301 dw_die_ref subtype_die;
8302 dw_die_ref subrange_die;
8303 tree name = TYPE_NAME (type);
8304 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
8305 tree subtype = TREE_TYPE (type);
8307 if (context_die == NULL)
8308 context_die = comp_unit_die;
8310 if (TREE_CODE (subtype) == ENUMERAL_TYPE)
8311 subtype_die = gen_enumeration_type_die (subtype, context_die);
8312 else
8313 subtype_die = base_type_die (subtype);
8315 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
8317 if (name != NULL)
8319 if (TREE_CODE (name) == TYPE_DECL)
8320 name = DECL_NAME (name);
8321 add_name_attribute (subrange_die, IDENTIFIER_POINTER (name));
8324 if (int_size_in_bytes (subtype) != size_in_bytes)
8326 /* The size of the subrange type and its base type do not match,
8327 so we need to generate a size attribute for the subrange type. */
8328 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
8331 if (TYPE_MIN_VALUE (type) != NULL)
8332 add_bound_info (subrange_die, DW_AT_lower_bound,
8333 TYPE_MIN_VALUE (type));
8334 if (TYPE_MAX_VALUE (type) != NULL)
8335 add_bound_info (subrange_die, DW_AT_upper_bound,
8336 TYPE_MAX_VALUE (type));
8337 add_AT_die_ref (subrange_die, DW_AT_type, subtype_die);
8339 return subrange_die;
8342 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
8343 entry that chains various modifiers in front of the given type. */
8345 static dw_die_ref
8346 modified_type_die (tree type, int is_const_type, int is_volatile_type,
8347 dw_die_ref context_die)
8349 enum tree_code code = TREE_CODE (type);
8350 dw_die_ref mod_type_die = NULL;
8351 dw_die_ref sub_die = NULL;
8352 tree item_type = NULL;
8354 if (code != ERROR_MARK)
8356 tree qualified_type;
8358 /* See if we already have the appropriately qualified variant of
8359 this type. */
8360 qualified_type
8361 = get_qualified_type (type,
8362 ((is_const_type ? TYPE_QUAL_CONST : 0)
8363 | (is_volatile_type
8364 ? TYPE_QUAL_VOLATILE : 0)));
8366 /* If we do, then we can just use its DIE, if it exists. */
8367 if (qualified_type)
8369 mod_type_die = lookup_type_die (qualified_type);
8370 if (mod_type_die)
8371 return mod_type_die;
8374 /* Handle C typedef types. */
8375 if (qualified_type && TYPE_NAME (qualified_type)
8376 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL
8377 && DECL_ORIGINAL_TYPE (TYPE_NAME (qualified_type)))
8379 tree type_name = TYPE_NAME (qualified_type);
8380 tree dtype = TREE_TYPE (type_name);
8382 if (qualified_type == dtype)
8384 /* For a named type, use the typedef. */
8385 gen_type_die (qualified_type, context_die);
8386 mod_type_die = lookup_type_die (qualified_type);
8388 else if (is_const_type < TYPE_READONLY (dtype)
8389 || is_volatile_type < TYPE_VOLATILE (dtype))
8390 /* cv-unqualified version of named type. Just use the unnamed
8391 type to which it refers. */
8392 mod_type_die
8393 = modified_type_die (DECL_ORIGINAL_TYPE (type_name),
8394 is_const_type, is_volatile_type,
8395 context_die);
8397 /* Else cv-qualified version of named type; fall through. */
8400 if (mod_type_die)
8401 /* OK. */
8403 else if (is_const_type)
8405 mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
8406 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
8408 else if (is_volatile_type)
8410 mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
8411 sub_die = modified_type_die (type, 0, 0, context_die);
8413 else if (code == POINTER_TYPE)
8415 mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
8416 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8417 simple_type_size_in_bits (type) / BITS_PER_UNIT);
8418 #if 0
8419 add_AT_unsigned (mod_type_die, DW_AT_address_class, 0);
8420 #endif
8421 item_type = TREE_TYPE (type);
8423 else if (code == REFERENCE_TYPE)
8425 mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
8426 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8427 simple_type_size_in_bits (type) / BITS_PER_UNIT);
8428 #if 0
8429 add_AT_unsigned (mod_type_die, DW_AT_address_class, 0);
8430 #endif
8431 item_type = TREE_TYPE (type);
8433 else if (is_subrange_type (type))
8434 mod_type_die = subrange_type_die (type, context_die);
8435 else if (is_base_type (type))
8436 mod_type_die = base_type_die (type);
8437 else
8439 gen_type_die (type, context_die);
8441 /* We have to get the type_main_variant here (and pass that to the
8442 `lookup_type_die' routine) because the ..._TYPE node we have
8443 might simply be a *copy* of some original type node (where the
8444 copy was created to help us keep track of typedef names) and
8445 that copy might have a different TYPE_UID from the original
8446 ..._TYPE node. */
8447 if (TREE_CODE (type) != VECTOR_TYPE)
8448 mod_type_die = lookup_type_die (type_main_variant (type));
8449 else
8450 /* Vectors have the debugging information in the type,
8451 not the main variant. */
8452 mod_type_die = lookup_type_die (type);
8453 gcc_assert (mod_type_die);
8456 /* We want to equate the qualified type to the die below. */
8457 type = qualified_type;
8460 if (type)
8461 equate_type_number_to_die (type, mod_type_die);
8462 if (item_type)
8463 /* We must do this after the equate_type_number_to_die call, in case
8464 this is a recursive type. This ensures that the modified_type_die
8465 recursion will terminate even if the type is recursive. Recursive
8466 types are possible in Ada. */
8467 sub_die = modified_type_die (item_type,
8468 TYPE_READONLY (item_type),
8469 TYPE_VOLATILE (item_type),
8470 context_die);
8472 if (sub_die != NULL)
8473 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
8475 return mod_type_die;
8478 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
8479 an enumerated type. */
8481 static inline int
8482 type_is_enum (tree type)
8484 return TREE_CODE (type) == ENUMERAL_TYPE;
8487 /* Return the DBX register number described by a given RTL node. */
8489 static unsigned int
8490 dbx_reg_number (rtx rtl)
8492 unsigned regno = REGNO (rtl);
8494 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
8496 #ifdef LEAF_REG_REMAP
8497 regno = LEAF_REG_REMAP (regno);
8498 #endif
8500 return DBX_REGISTER_NUMBER (regno);
8503 /* Optionally add a DW_OP_piece term to a location description expression.
8504 DW_OP_piece is only added if the location description expression already
8505 doesn't end with DW_OP_piece. */
8507 static void
8508 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
8510 dw_loc_descr_ref loc;
8512 if (*list_head != NULL)
8514 /* Find the end of the chain. */
8515 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
8518 if (loc->dw_loc_opc != DW_OP_piece)
8519 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
8523 /* Return a location descriptor that designates a machine register or
8524 zero if there is none. */
8526 static dw_loc_descr_ref
8527 reg_loc_descriptor (rtx rtl)
8529 rtx regs;
8531 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
8532 return 0;
8534 regs = targetm.dwarf_register_span (rtl);
8536 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
8537 return multiple_reg_loc_descriptor (rtl, regs);
8538 else
8539 return one_reg_loc_descriptor (dbx_reg_number (rtl));
8542 /* Return a location descriptor that designates a machine register for
8543 a given hard register number. */
8545 static dw_loc_descr_ref
8546 one_reg_loc_descriptor (unsigned int regno)
8548 if (regno <= 31)
8549 return new_loc_descr (DW_OP_reg0 + regno, 0, 0);
8550 else
8551 return new_loc_descr (DW_OP_regx, regno, 0);
8554 /* Given an RTL of a register, return a location descriptor that
8555 designates a value that spans more than one register. */
8557 static dw_loc_descr_ref
8558 multiple_reg_loc_descriptor (rtx rtl, rtx regs)
8560 int nregs, size, i;
8561 unsigned reg;
8562 dw_loc_descr_ref loc_result = NULL;
8564 reg = REGNO (rtl);
8565 #ifdef LEAF_REG_REMAP
8566 reg = LEAF_REG_REMAP (reg);
8567 #endif
8568 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
8569 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
8571 /* Simple, contiguous registers. */
8572 if (regs == NULL_RTX)
8574 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
8576 loc_result = NULL;
8577 while (nregs--)
8579 dw_loc_descr_ref t;
8581 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg));
8582 add_loc_descr (&loc_result, t);
8583 add_loc_descr_op_piece (&loc_result, size);
8584 ++reg;
8586 return loc_result;
8589 /* Now onto stupid register sets in non contiguous locations. */
8591 gcc_assert (GET_CODE (regs) == PARALLEL);
8593 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8594 loc_result = NULL;
8596 for (i = 0; i < XVECLEN (regs, 0); ++i)
8598 dw_loc_descr_ref t;
8600 t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)));
8601 add_loc_descr (&loc_result, t);
8602 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8603 add_loc_descr_op_piece (&loc_result, size);
8605 return loc_result;
8608 /* Return a location descriptor that designates a constant. */
8610 static dw_loc_descr_ref
8611 int_loc_descriptor (HOST_WIDE_INT i)
8613 enum dwarf_location_atom op;
8615 /* Pick the smallest representation of a constant, rather than just
8616 defaulting to the LEB encoding. */
8617 if (i >= 0)
8619 if (i <= 31)
8620 op = DW_OP_lit0 + i;
8621 else if (i <= 0xff)
8622 op = DW_OP_const1u;
8623 else if (i <= 0xffff)
8624 op = DW_OP_const2u;
8625 else if (HOST_BITS_PER_WIDE_INT == 32
8626 || i <= 0xffffffff)
8627 op = DW_OP_const4u;
8628 else
8629 op = DW_OP_constu;
8631 else
8633 if (i >= -0x80)
8634 op = DW_OP_const1s;
8635 else if (i >= -0x8000)
8636 op = DW_OP_const2s;
8637 else if (HOST_BITS_PER_WIDE_INT == 32
8638 || i >= -0x80000000)
8639 op = DW_OP_const4s;
8640 else
8641 op = DW_OP_consts;
8644 return new_loc_descr (op, i, 0);
8647 /* Return a location descriptor that designates a base+offset location. */
8649 static dw_loc_descr_ref
8650 based_loc_descr (rtx reg, HOST_WIDE_INT offset)
8652 unsigned int regno;
8654 /* We only use "frame base" when we're sure we're talking about the
8655 post-prologue local stack frame. We do this by *not* running
8656 register elimination until this point, and recognizing the special
8657 argument pointer and soft frame pointer rtx's. */
8658 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
8660 rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
8662 if (elim != reg)
8664 if (GET_CODE (elim) == PLUS)
8666 offset += INTVAL (XEXP (elim, 1));
8667 elim = XEXP (elim, 0);
8669 gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
8670 : stack_pointer_rtx));
8671 offset += frame_pointer_cfa_offset;
8673 return new_loc_descr (DW_OP_fbreg, offset, 0);
8677 regno = dbx_reg_number (reg);
8678 if (regno <= 31)
8679 return new_loc_descr (DW_OP_breg0 + regno, offset, 0);
8680 else
8681 return new_loc_descr (DW_OP_bregx, regno, offset);
8684 /* Return true if this RTL expression describes a base+offset calculation. */
8686 static inline int
8687 is_based_loc (rtx rtl)
8689 return (GET_CODE (rtl) == PLUS
8690 && ((REG_P (XEXP (rtl, 0))
8691 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
8692 && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
8695 /* The following routine converts the RTL for a variable or parameter
8696 (resident in memory) into an equivalent Dwarf representation of a
8697 mechanism for getting the address of that same variable onto the top of a
8698 hypothetical "address evaluation" stack.
8700 When creating memory location descriptors, we are effectively transforming
8701 the RTL for a memory-resident object into its Dwarf postfix expression
8702 equivalent. This routine recursively descends an RTL tree, turning
8703 it into Dwarf postfix code as it goes.
8705 MODE is the mode of the memory reference, needed to handle some
8706 autoincrement addressing modes.
8708 CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
8709 location list for RTL.
8711 Return 0 if we can't represent the location. */
8713 static dw_loc_descr_ref
8714 mem_loc_descriptor (rtx rtl, enum machine_mode mode)
8716 dw_loc_descr_ref mem_loc_result = NULL;
8717 enum dwarf_location_atom op;
8719 /* Note that for a dynamically sized array, the location we will generate a
8720 description of here will be the lowest numbered location which is
8721 actually within the array. That's *not* necessarily the same as the
8722 zeroth element of the array. */
8724 rtl = targetm.delegitimize_address (rtl);
8726 switch (GET_CODE (rtl))
8728 case POST_INC:
8729 case POST_DEC:
8730 case POST_MODIFY:
8731 /* POST_INC and POST_DEC can be handled just like a SUBREG. So we
8732 just fall into the SUBREG code. */
8734 /* ... fall through ... */
8736 case SUBREG:
8737 /* The case of a subreg may arise when we have a local (register)
8738 variable or a formal (register) parameter which doesn't quite fill
8739 up an entire register. For now, just assume that it is
8740 legitimate to make the Dwarf info refer to the whole register which
8741 contains the given subreg. */
8742 rtl = XEXP (rtl, 0);
8744 /* ... fall through ... */
8746 case REG:
8747 /* Whenever a register number forms a part of the description of the
8748 method for calculating the (dynamic) address of a memory resident
8749 object, DWARF rules require the register number be referred to as
8750 a "base register". This distinction is not based in any way upon
8751 what category of register the hardware believes the given register
8752 belongs to. This is strictly DWARF terminology we're dealing with
8753 here. Note that in cases where the location of a memory-resident
8754 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
8755 OP_CONST (0)) the actual DWARF location descriptor that we generate
8756 may just be OP_BASEREG (basereg). This may look deceptively like
8757 the object in question was allocated to a register (rather than in
8758 memory) so DWARF consumers need to be aware of the subtle
8759 distinction between OP_REG and OP_BASEREG. */
8760 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
8761 mem_loc_result = based_loc_descr (rtl, 0);
8762 break;
8764 case MEM:
8765 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl));
8766 if (mem_loc_result != 0)
8767 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
8768 break;
8770 case LO_SUM:
8771 rtl = XEXP (rtl, 1);
8773 /* ... fall through ... */
8775 case LABEL_REF:
8776 /* Some ports can transform a symbol ref into a label ref, because
8777 the symbol ref is too far away and has to be dumped into a constant
8778 pool. */
8779 case CONST:
8780 case SYMBOL_REF:
8781 /* Alternatively, the symbol in the constant pool might be referenced
8782 by a different symbol. */
8783 if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
8785 bool marked;
8786 rtx tmp = get_pool_constant_mark (rtl, &marked);
8788 if (GET_CODE (tmp) == SYMBOL_REF)
8790 rtl = tmp;
8791 if (CONSTANT_POOL_ADDRESS_P (tmp))
8792 get_pool_constant_mark (tmp, &marked);
8793 else
8794 marked = true;
8797 /* If all references to this pool constant were optimized away,
8798 it was not output and thus we can't represent it.
8799 FIXME: might try to use DW_OP_const_value here, though
8800 DW_OP_piece complicates it. */
8801 if (!marked)
8802 return 0;
8805 mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
8806 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
8807 mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
8808 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
8809 break;
8811 case PRE_MODIFY:
8812 /* Extract the PLUS expression nested inside and fall into
8813 PLUS code below. */
8814 rtl = XEXP (rtl, 1);
8815 goto plus;
8817 case PRE_INC:
8818 case PRE_DEC:
8819 /* Turn these into a PLUS expression and fall into the PLUS code
8820 below. */
8821 rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
8822 GEN_INT (GET_CODE (rtl) == PRE_INC
8823 ? GET_MODE_UNIT_SIZE (mode)
8824 : -GET_MODE_UNIT_SIZE (mode)));
8826 /* ... fall through ... */
8828 case PLUS:
8829 plus:
8830 if (is_based_loc (rtl))
8831 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
8832 INTVAL (XEXP (rtl, 1)));
8833 else
8835 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode);
8836 if (mem_loc_result == 0)
8837 break;
8839 if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
8840 && INTVAL (XEXP (rtl, 1)) >= 0)
8841 add_loc_descr (&mem_loc_result,
8842 new_loc_descr (DW_OP_plus_uconst,
8843 INTVAL (XEXP (rtl, 1)), 0));
8844 else
8846 add_loc_descr (&mem_loc_result,
8847 mem_loc_descriptor (XEXP (rtl, 1), mode));
8848 add_loc_descr (&mem_loc_result,
8849 new_loc_descr (DW_OP_plus, 0, 0));
8852 break;
8854 /* If a pseudo-reg is optimized away, it is possible for it to
8855 be replaced with a MEM containing a multiply or shift. */
8856 case MULT:
8857 op = DW_OP_mul;
8858 goto do_binop;
8860 case ASHIFT:
8861 op = DW_OP_shl;
8862 goto do_binop;
8864 case ASHIFTRT:
8865 op = DW_OP_shra;
8866 goto do_binop;
8868 case LSHIFTRT:
8869 op = DW_OP_shr;
8870 goto do_binop;
8872 do_binop:
8874 dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode);
8875 dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode);
8877 if (op0 == 0 || op1 == 0)
8878 break;
8880 mem_loc_result = op0;
8881 add_loc_descr (&mem_loc_result, op1);
8882 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
8883 break;
8886 case CONST_INT:
8887 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
8888 break;
8890 default:
8891 gcc_unreachable ();
8894 return mem_loc_result;
8897 /* Return a descriptor that describes the concatenation of two locations.
8898 This is typically a complex variable. */
8900 static dw_loc_descr_ref
8901 concat_loc_descriptor (rtx x0, rtx x1)
8903 dw_loc_descr_ref cc_loc_result = NULL;
8904 dw_loc_descr_ref x0_ref = loc_descriptor (x0);
8905 dw_loc_descr_ref x1_ref = loc_descriptor (x1);
8907 if (x0_ref == 0 || x1_ref == 0)
8908 return 0;
8910 cc_loc_result = x0_ref;
8911 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
8913 add_loc_descr (&cc_loc_result, x1_ref);
8914 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
8916 return cc_loc_result;
8919 /* Output a proper Dwarf location descriptor for a variable or parameter
8920 which is either allocated in a register or in a memory location. For a
8921 register, we just generate an OP_REG and the register number. For a
8922 memory location we provide a Dwarf postfix expression describing how to
8923 generate the (dynamic) address of the object onto the address stack.
8925 If we don't know how to describe it, return 0. */
8927 static dw_loc_descr_ref
8928 loc_descriptor (rtx rtl)
8930 dw_loc_descr_ref loc_result = NULL;
8932 switch (GET_CODE (rtl))
8934 case SUBREG:
8935 /* The case of a subreg may arise when we have a local (register)
8936 variable or a formal (register) parameter which doesn't quite fill
8937 up an entire register. For now, just assume that it is
8938 legitimate to make the Dwarf info refer to the whole register which
8939 contains the given subreg. */
8940 rtl = SUBREG_REG (rtl);
8942 /* ... fall through ... */
8944 case REG:
8945 loc_result = reg_loc_descriptor (rtl);
8946 break;
8948 case MEM:
8949 loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl));
8950 break;
8952 case CONCAT:
8953 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1));
8954 break;
8956 case VAR_LOCATION:
8957 /* Single part. */
8958 if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
8960 loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0));
8961 break;
8964 rtl = XEXP (rtl, 1);
8965 /* FALLTHRU */
8967 case PARALLEL:
8969 rtvec par_elems = XVEC (rtl, 0);
8970 int num_elem = GET_NUM_ELEM (par_elems);
8971 enum machine_mode mode;
8972 int i;
8974 /* Create the first one, so we have something to add to. */
8975 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0));
8976 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
8977 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
8978 for (i = 1; i < num_elem; i++)
8980 dw_loc_descr_ref temp;
8982 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0));
8983 add_loc_descr (&loc_result, temp);
8984 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
8985 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
8988 break;
8990 default:
8991 gcc_unreachable ();
8994 return loc_result;
8997 /* Similar, but generate the descriptor from trees instead of rtl. This comes
8998 up particularly with variable length arrays. WANT_ADDRESS is 2 if this is
8999 a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
9000 top-level invocation, and we require the address of LOC; is 0 if we require
9001 the value of LOC. */
9003 static dw_loc_descr_ref
9004 loc_descriptor_from_tree_1 (tree loc, int want_address)
9006 dw_loc_descr_ref ret, ret1;
9007 int have_address = 0;
9008 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
9009 enum dwarf_location_atom op;
9011 /* ??? Most of the time we do not take proper care for sign/zero
9012 extending the values properly. Hopefully this won't be a real
9013 problem... */
9015 switch (TREE_CODE (loc))
9017 case ERROR_MARK:
9018 return 0;
9020 case PLACEHOLDER_EXPR:
9021 /* This case involves extracting fields from an object to determine the
9022 position of other fields. We don't try to encode this here. The
9023 only user of this is Ada, which encodes the needed information using
9024 the names of types. */
9025 return 0;
9027 case CALL_EXPR:
9028 return 0;
9030 case PREINCREMENT_EXPR:
9031 case PREDECREMENT_EXPR:
9032 case POSTINCREMENT_EXPR:
9033 case POSTDECREMENT_EXPR:
9034 /* There are no opcodes for these operations. */
9035 return 0;
9037 case ADDR_EXPR:
9038 /* If we already want an address, there's nothing we can do. */
9039 if (want_address)
9040 return 0;
9042 /* Otherwise, process the argument and look for the address. */
9043 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
9045 case VAR_DECL:
9046 if (DECL_THREAD_LOCAL_P (loc))
9048 rtx rtl;
9050 /* If this is not defined, we have no way to emit the data. */
9051 if (!targetm.asm_out.output_dwarf_dtprel)
9052 return 0;
9054 /* The way DW_OP_GNU_push_tls_address is specified, we can only
9055 look up addresses of objects in the current module. */
9056 if (DECL_EXTERNAL (loc))
9057 return 0;
9059 rtl = rtl_for_decl_location (loc);
9060 if (rtl == NULL_RTX)
9061 return 0;
9063 if (!MEM_P (rtl))
9064 return 0;
9065 rtl = XEXP (rtl, 0);
9066 if (! CONSTANT_P (rtl))
9067 return 0;
9069 ret = new_loc_descr (INTERNAL_DW_OP_tls_addr, 0, 0);
9070 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9071 ret->dw_loc_oprnd1.v.val_addr = rtl;
9073 ret1 = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
9074 add_loc_descr (&ret, ret1);
9076 have_address = 1;
9077 break;
9079 /* FALLTHRU */
9081 case PARM_DECL:
9082 if (DECL_HAS_VALUE_EXPR_P (loc))
9083 return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
9084 want_address);
9085 /* FALLTHRU */
9087 case RESULT_DECL:
9089 rtx rtl = rtl_for_decl_location (loc);
9091 if (rtl == NULL_RTX)
9092 return 0;
9093 else if (GET_CODE (rtl) == CONST_INT)
9095 HOST_WIDE_INT val = INTVAL (rtl);
9096 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
9097 val &= GET_MODE_MASK (DECL_MODE (loc));
9098 ret = int_loc_descriptor (val);
9100 else if (GET_CODE (rtl) == CONST_STRING)
9101 return 0;
9102 else if (CONSTANT_P (rtl))
9104 ret = new_loc_descr (DW_OP_addr, 0, 0);
9105 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9106 ret->dw_loc_oprnd1.v.val_addr = rtl;
9108 else
9110 enum machine_mode mode;
9112 /* Certain constructs can only be represented at top-level. */
9113 if (want_address == 2)
9114 return loc_descriptor (rtl);
9116 mode = GET_MODE (rtl);
9117 if (MEM_P (rtl))
9119 rtl = XEXP (rtl, 0);
9120 have_address = 1;
9122 ret = mem_loc_descriptor (rtl, mode);
9125 break;
9127 case INDIRECT_REF:
9128 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9129 have_address = 1;
9130 break;
9132 case COMPOUND_EXPR:
9133 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
9135 case NOP_EXPR:
9136 case CONVERT_EXPR:
9137 case NON_LVALUE_EXPR:
9138 case VIEW_CONVERT_EXPR:
9139 case SAVE_EXPR:
9140 case MODIFY_EXPR:
9141 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), want_address);
9143 case COMPONENT_REF:
9144 case BIT_FIELD_REF:
9145 case ARRAY_REF:
9146 case ARRAY_RANGE_REF:
9148 tree obj, offset;
9149 HOST_WIDE_INT bitsize, bitpos, bytepos;
9150 enum machine_mode mode;
9151 int volatilep;
9153 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
9154 &unsignedp, &volatilep, false);
9156 if (obj == loc)
9157 return 0;
9159 ret = loc_descriptor_from_tree_1 (obj, 1);
9160 if (ret == 0
9161 || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
9162 return 0;
9164 if (offset != NULL_TREE)
9166 /* Variable offset. */
9167 add_loc_descr (&ret, loc_descriptor_from_tree_1 (offset, 0));
9168 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9171 bytepos = bitpos / BITS_PER_UNIT;
9172 if (bytepos > 0)
9173 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
9174 else if (bytepos < 0)
9176 add_loc_descr (&ret, int_loc_descriptor (bytepos));
9177 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9180 have_address = 1;
9181 break;
9184 case INTEGER_CST:
9185 if (host_integerp (loc, 0))
9186 ret = int_loc_descriptor (tree_low_cst (loc, 0));
9187 else
9188 return 0;
9189 break;
9191 case CONSTRUCTOR:
9193 /* Get an RTL for this, if something has been emitted. */
9194 rtx rtl = lookup_constant_def (loc);
9195 enum machine_mode mode;
9197 if (!rtl || !MEM_P (rtl))
9198 return 0;
9199 mode = GET_MODE (rtl);
9200 rtl = XEXP (rtl, 0);
9201 ret = mem_loc_descriptor (rtl, mode);
9202 have_address = 1;
9203 break;
9206 case TRUTH_AND_EXPR:
9207 case TRUTH_ANDIF_EXPR:
9208 case BIT_AND_EXPR:
9209 op = DW_OP_and;
9210 goto do_binop;
9212 case TRUTH_XOR_EXPR:
9213 case BIT_XOR_EXPR:
9214 op = DW_OP_xor;
9215 goto do_binop;
9217 case TRUTH_OR_EXPR:
9218 case TRUTH_ORIF_EXPR:
9219 case BIT_IOR_EXPR:
9220 op = DW_OP_or;
9221 goto do_binop;
9223 case FLOOR_DIV_EXPR:
9224 case CEIL_DIV_EXPR:
9225 case ROUND_DIV_EXPR:
9226 case TRUNC_DIV_EXPR:
9227 op = DW_OP_div;
9228 goto do_binop;
9230 case MINUS_EXPR:
9231 op = DW_OP_minus;
9232 goto do_binop;
9234 case FLOOR_MOD_EXPR:
9235 case CEIL_MOD_EXPR:
9236 case ROUND_MOD_EXPR:
9237 case TRUNC_MOD_EXPR:
9238 op = DW_OP_mod;
9239 goto do_binop;
9241 case MULT_EXPR:
9242 op = DW_OP_mul;
9243 goto do_binop;
9245 case LSHIFT_EXPR:
9246 op = DW_OP_shl;
9247 goto do_binop;
9249 case RSHIFT_EXPR:
9250 op = (unsignedp ? DW_OP_shr : DW_OP_shra);
9251 goto do_binop;
9253 case PLUS_EXPR:
9254 if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
9255 && host_integerp (TREE_OPERAND (loc, 1), 0))
9257 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9258 if (ret == 0)
9259 return 0;
9261 add_loc_descr (&ret,
9262 new_loc_descr (DW_OP_plus_uconst,
9263 tree_low_cst (TREE_OPERAND (loc, 1),
9265 0));
9266 break;
9269 op = DW_OP_plus;
9270 goto do_binop;
9272 case LE_EXPR:
9273 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9274 return 0;
9276 op = DW_OP_le;
9277 goto do_binop;
9279 case GE_EXPR:
9280 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9281 return 0;
9283 op = DW_OP_ge;
9284 goto do_binop;
9286 case LT_EXPR:
9287 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9288 return 0;
9290 op = DW_OP_lt;
9291 goto do_binop;
9293 case GT_EXPR:
9294 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9295 return 0;
9297 op = DW_OP_gt;
9298 goto do_binop;
9300 case EQ_EXPR:
9301 op = DW_OP_eq;
9302 goto do_binop;
9304 case NE_EXPR:
9305 op = DW_OP_ne;
9306 goto do_binop;
9308 do_binop:
9309 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9310 ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9311 if (ret == 0 || ret1 == 0)
9312 return 0;
9314 add_loc_descr (&ret, ret1);
9315 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9316 break;
9318 case TRUTH_NOT_EXPR:
9319 case BIT_NOT_EXPR:
9320 op = DW_OP_not;
9321 goto do_unop;
9323 case ABS_EXPR:
9324 op = DW_OP_abs;
9325 goto do_unop;
9327 case NEGATE_EXPR:
9328 op = DW_OP_neg;
9329 goto do_unop;
9331 do_unop:
9332 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9333 if (ret == 0)
9334 return 0;
9336 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9337 break;
9339 case MIN_EXPR:
9340 case MAX_EXPR:
9342 const enum tree_code code =
9343 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
9345 loc = build3 (COND_EXPR, TREE_TYPE (loc),
9346 build2 (code, integer_type_node,
9347 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
9348 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
9351 /* ... fall through ... */
9353 case COND_EXPR:
9355 dw_loc_descr_ref lhs
9356 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9357 dw_loc_descr_ref rhs
9358 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
9359 dw_loc_descr_ref bra_node, jump_node, tmp;
9361 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9362 if (ret == 0 || lhs == 0 || rhs == 0)
9363 return 0;
9365 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
9366 add_loc_descr (&ret, bra_node);
9368 add_loc_descr (&ret, rhs);
9369 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
9370 add_loc_descr (&ret, jump_node);
9372 add_loc_descr (&ret, lhs);
9373 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9374 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
9376 /* ??? Need a node to point the skip at. Use a nop. */
9377 tmp = new_loc_descr (DW_OP_nop, 0, 0);
9378 add_loc_descr (&ret, tmp);
9379 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9380 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
9382 break;
9384 case FIX_TRUNC_EXPR:
9385 case FIX_CEIL_EXPR:
9386 case FIX_FLOOR_EXPR:
9387 case FIX_ROUND_EXPR:
9388 return 0;
9390 default:
9391 /* Leave front-end specific codes as simply unknown. This comes
9392 up, for instance, with the C STMT_EXPR. */
9393 if ((unsigned int) TREE_CODE (loc)
9394 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
9395 return 0;
9397 #ifdef ENABLE_CHECKING
9398 /* Otherwise this is a generic code; we should just lists all of
9399 these explicitly. We forgot one. */
9400 gcc_unreachable ();
9401 #else
9402 /* In a release build, we want to degrade gracefully: better to
9403 generate incomplete debugging information than to crash. */
9404 return NULL;
9405 #endif
9408 /* Show if we can't fill the request for an address. */
9409 if (want_address && !have_address)
9410 return 0;
9412 /* If we've got an address and don't want one, dereference. */
9413 if (!want_address && have_address)
9415 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
9417 if (size > DWARF2_ADDR_SIZE || size == -1)
9418 return 0;
9419 else if (size == DWARF2_ADDR_SIZE)
9420 op = DW_OP_deref;
9421 else
9422 op = DW_OP_deref_size;
9424 add_loc_descr (&ret, new_loc_descr (op, size, 0));
9427 return ret;
9430 static inline dw_loc_descr_ref
9431 loc_descriptor_from_tree (tree loc)
9433 return loc_descriptor_from_tree_1 (loc, 2);
9436 /* Given a value, round it up to the lowest multiple of `boundary'
9437 which is not less than the value itself. */
9439 static inline HOST_WIDE_INT
9440 ceiling (HOST_WIDE_INT value, unsigned int boundary)
9442 return (((value + boundary - 1) / boundary) * boundary);
9445 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
9446 pointer to the declared type for the relevant field variable, or return
9447 `integer_type_node' if the given node turns out to be an
9448 ERROR_MARK node. */
9450 static inline tree
9451 field_type (tree decl)
9453 tree type;
9455 if (TREE_CODE (decl) == ERROR_MARK)
9456 return integer_type_node;
9458 type = DECL_BIT_FIELD_TYPE (decl);
9459 if (type == NULL_TREE)
9460 type = TREE_TYPE (decl);
9462 return type;
9465 /* Given a pointer to a tree node, return the alignment in bits for
9466 it, or else return BITS_PER_WORD if the node actually turns out to
9467 be an ERROR_MARK node. */
9469 static inline unsigned
9470 simple_type_align_in_bits (tree type)
9472 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
9475 static inline unsigned
9476 simple_decl_align_in_bits (tree decl)
9478 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
9481 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
9482 lowest addressed byte of the "containing object" for the given FIELD_DECL,
9483 or return 0 if we are unable to determine what that offset is, either
9484 because the argument turns out to be a pointer to an ERROR_MARK node, or
9485 because the offset is actually variable. (We can't handle the latter case
9486 just yet). */
9488 static HOST_WIDE_INT
9489 field_byte_offset (tree decl)
9491 unsigned int type_align_in_bits;
9492 unsigned int decl_align_in_bits;
9493 unsigned HOST_WIDE_INT type_size_in_bits;
9494 HOST_WIDE_INT object_offset_in_bits;
9495 tree type;
9496 tree field_size_tree;
9497 HOST_WIDE_INT bitpos_int;
9498 HOST_WIDE_INT deepest_bitpos;
9499 unsigned HOST_WIDE_INT field_size_in_bits;
9501 if (TREE_CODE (decl) == ERROR_MARK)
9502 return 0;
9504 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
9506 type = field_type (decl);
9507 field_size_tree = DECL_SIZE (decl);
9509 /* The size could be unspecified if there was an error, or for
9510 a flexible array member. */
9511 if (! field_size_tree)
9512 field_size_tree = bitsize_zero_node;
9514 /* We cannot yet cope with fields whose positions are variable, so
9515 for now, when we see such things, we simply return 0. Someday, we may
9516 be able to handle such cases, but it will be damn difficult. */
9517 if (! host_integerp (bit_position (decl), 0))
9518 return 0;
9520 bitpos_int = int_bit_position (decl);
9522 /* If we don't know the size of the field, pretend it's a full word. */
9523 if (host_integerp (field_size_tree, 1))
9524 field_size_in_bits = tree_low_cst (field_size_tree, 1);
9525 else
9526 field_size_in_bits = BITS_PER_WORD;
9528 type_size_in_bits = simple_type_size_in_bits (type);
9529 type_align_in_bits = simple_type_align_in_bits (type);
9530 decl_align_in_bits = simple_decl_align_in_bits (decl);
9532 /* The GCC front-end doesn't make any attempt to keep track of the starting
9533 bit offset (relative to the start of the containing structure type) of the
9534 hypothetical "containing object" for a bit-field. Thus, when computing
9535 the byte offset value for the start of the "containing object" of a
9536 bit-field, we must deduce this information on our own. This can be rather
9537 tricky to do in some cases. For example, handling the following structure
9538 type definition when compiling for an i386/i486 target (which only aligns
9539 long long's to 32-bit boundaries) can be very tricky:
9541 struct S { int field1; long long field2:31; };
9543 Fortunately, there is a simple rule-of-thumb which can be used in such
9544 cases. When compiling for an i386/i486, GCC will allocate 8 bytes for the
9545 structure shown above. It decides to do this based upon one simple rule
9546 for bit-field allocation. GCC allocates each "containing object" for each
9547 bit-field at the first (i.e. lowest addressed) legitimate alignment
9548 boundary (based upon the required minimum alignment for the declared type
9549 of the field) which it can possibly use, subject to the condition that
9550 there is still enough available space remaining in the containing object
9551 (when allocated at the selected point) to fully accommodate all of the
9552 bits of the bit-field itself.
9554 This simple rule makes it obvious why GCC allocates 8 bytes for each
9555 object of the structure type shown above. When looking for a place to
9556 allocate the "containing object" for `field2', the compiler simply tries
9557 to allocate a 64-bit "containing object" at each successive 32-bit
9558 boundary (starting at zero) until it finds a place to allocate that 64-
9559 bit field such that at least 31 contiguous (and previously unallocated)
9560 bits remain within that selected 64 bit field. (As it turns out, for the
9561 example above, the compiler finds it is OK to allocate the "containing
9562 object" 64-bit field at bit-offset zero within the structure type.)
9564 Here we attempt to work backwards from the limited set of facts we're
9565 given, and we try to deduce from those facts, where GCC must have believed
9566 that the containing object started (within the structure type). The value
9567 we deduce is then used (by the callers of this routine) to generate
9568 DW_AT_location and DW_AT_bit_offset attributes for fields (both bit-fields
9569 and, in the case of DW_AT_location, regular fields as well). */
9571 /* Figure out the bit-distance from the start of the structure to the
9572 "deepest" bit of the bit-field. */
9573 deepest_bitpos = bitpos_int + field_size_in_bits;
9575 /* This is the tricky part. Use some fancy footwork to deduce where the
9576 lowest addressed bit of the containing object must be. */
9577 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9579 /* Round up to type_align by default. This works best for bitfields. */
9580 object_offset_in_bits += type_align_in_bits - 1;
9581 object_offset_in_bits /= type_align_in_bits;
9582 object_offset_in_bits *= type_align_in_bits;
9584 if (object_offset_in_bits > bitpos_int)
9586 /* Sigh, the decl must be packed. */
9587 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9589 /* Round up to decl_align instead. */
9590 object_offset_in_bits += decl_align_in_bits - 1;
9591 object_offset_in_bits /= decl_align_in_bits;
9592 object_offset_in_bits *= decl_align_in_bits;
9595 return object_offset_in_bits / BITS_PER_UNIT;
9598 /* The following routines define various Dwarf attributes and any data
9599 associated with them. */
9601 /* Add a location description attribute value to a DIE.
9603 This emits location attributes suitable for whole variables and
9604 whole parameters. Note that the location attributes for struct fields are
9605 generated by the routine `data_member_location_attribute' below. */
9607 static inline void
9608 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
9609 dw_loc_descr_ref descr)
9611 if (descr != 0)
9612 add_AT_loc (die, attr_kind, descr);
9615 /* Attach the specialized form of location attribute used for data members of
9616 struct and union types. In the special case of a FIELD_DECL node which
9617 represents a bit-field, the "offset" part of this special location
9618 descriptor must indicate the distance in bytes from the lowest-addressed
9619 byte of the containing struct or union type to the lowest-addressed byte of
9620 the "containing object" for the bit-field. (See the `field_byte_offset'
9621 function above).
9623 For any given bit-field, the "containing object" is a hypothetical object
9624 (of some integral or enum type) within which the given bit-field lives. The
9625 type of this hypothetical "containing object" is always the same as the
9626 declared type of the individual bit-field itself (for GCC anyway... the
9627 DWARF spec doesn't actually mandate this). Note that it is the size (in
9628 bytes) of the hypothetical "containing object" which will be given in the
9629 DW_AT_byte_size attribute for this bit-field. (See the
9630 `byte_size_attribute' function below.) It is also used when calculating the
9631 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
9632 function below.) */
9634 static void
9635 add_data_member_location_attribute (dw_die_ref die, tree decl)
9637 HOST_WIDE_INT offset;
9638 dw_loc_descr_ref loc_descr = 0;
9640 if (TREE_CODE (decl) == TREE_BINFO)
9642 /* We're working on the TAG_inheritance for a base class. */
9643 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
9645 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
9646 aren't at a fixed offset from all (sub)objects of the same
9647 type. We need to extract the appropriate offset from our
9648 vtable. The following dwarf expression means
9650 BaseAddr = ObAddr + *((*ObAddr) - Offset)
9652 This is specific to the V3 ABI, of course. */
9654 dw_loc_descr_ref tmp;
9656 /* Make a copy of the object address. */
9657 tmp = new_loc_descr (DW_OP_dup, 0, 0);
9658 add_loc_descr (&loc_descr, tmp);
9660 /* Extract the vtable address. */
9661 tmp = new_loc_descr (DW_OP_deref, 0, 0);
9662 add_loc_descr (&loc_descr, tmp);
9664 /* Calculate the address of the offset. */
9665 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
9666 gcc_assert (offset < 0);
9668 tmp = int_loc_descriptor (-offset);
9669 add_loc_descr (&loc_descr, tmp);
9670 tmp = new_loc_descr (DW_OP_minus, 0, 0);
9671 add_loc_descr (&loc_descr, tmp);
9673 /* Extract the offset. */
9674 tmp = new_loc_descr (DW_OP_deref, 0, 0);
9675 add_loc_descr (&loc_descr, tmp);
9677 /* Add it to the object address. */
9678 tmp = new_loc_descr (DW_OP_plus, 0, 0);
9679 add_loc_descr (&loc_descr, tmp);
9681 else
9682 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
9684 else
9685 offset = field_byte_offset (decl);
9687 if (! loc_descr)
9689 enum dwarf_location_atom op;
9691 /* The DWARF2 standard says that we should assume that the structure
9692 address is already on the stack, so we can specify a structure field
9693 address by using DW_OP_plus_uconst. */
9695 #ifdef MIPS_DEBUGGING_INFO
9696 /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
9697 operator correctly. It works only if we leave the offset on the
9698 stack. */
9699 op = DW_OP_constu;
9700 #else
9701 op = DW_OP_plus_uconst;
9702 #endif
9704 loc_descr = new_loc_descr (op, offset, 0);
9707 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
9710 /* Writes integer values to dw_vec_const array. */
9712 static void
9713 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
9715 while (size != 0)
9717 *dest++ = val & 0xff;
9718 val >>= 8;
9719 --size;
9723 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
9725 static HOST_WIDE_INT
9726 extract_int (const unsigned char *src, unsigned int size)
9728 HOST_WIDE_INT val = 0;
9730 src += size;
9731 while (size != 0)
9733 val <<= 8;
9734 val |= *--src & 0xff;
9735 --size;
9737 return val;
9740 /* Writes floating point values to dw_vec_const array. */
9742 static void
9743 insert_float (rtx rtl, unsigned char *array)
9745 REAL_VALUE_TYPE rv;
9746 long val[4];
9747 int i;
9749 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
9750 real_to_target (val, &rv, GET_MODE (rtl));
9752 /* real_to_target puts 32-bit pieces in each long. Pack them. */
9753 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
9755 insert_int (val[i], 4, array);
9756 array += 4;
9760 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
9761 does not have a "location" either in memory or in a register. These
9762 things can arise in GNU C when a constant is passed as an actual parameter
9763 to an inlined function. They can also arise in C++ where declared
9764 constants do not necessarily get memory "homes". */
9766 static void
9767 add_const_value_attribute (dw_die_ref die, rtx rtl)
9769 switch (GET_CODE (rtl))
9771 case CONST_INT:
9773 HOST_WIDE_INT val = INTVAL (rtl);
9775 if (val < 0)
9776 add_AT_int (die, DW_AT_const_value, val);
9777 else
9778 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
9780 break;
9782 case CONST_DOUBLE:
9783 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
9784 floating-point constant. A CONST_DOUBLE is used whenever the
9785 constant requires more than one word in order to be adequately
9786 represented. We output CONST_DOUBLEs as blocks. */
9788 enum machine_mode mode = GET_MODE (rtl);
9790 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
9792 unsigned int length = GET_MODE_SIZE (mode);
9793 unsigned char *array = ggc_alloc (length);
9795 insert_float (rtl, array);
9796 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
9798 else
9800 /* ??? We really should be using HOST_WIDE_INT throughout. */
9801 gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
9803 add_AT_long_long (die, DW_AT_const_value,
9804 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
9807 break;
9809 case CONST_VECTOR:
9811 enum machine_mode mode = GET_MODE (rtl);
9812 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
9813 unsigned int length = CONST_VECTOR_NUNITS (rtl);
9814 unsigned char *array = ggc_alloc (length * elt_size);
9815 unsigned int i;
9816 unsigned char *p;
9818 switch (GET_MODE_CLASS (mode))
9820 case MODE_VECTOR_INT:
9821 for (i = 0, p = array; i < length; i++, p += elt_size)
9823 rtx elt = CONST_VECTOR_ELT (rtl, i);
9824 HOST_WIDE_INT lo, hi;
9826 switch (GET_CODE (elt))
9828 case CONST_INT:
9829 lo = INTVAL (elt);
9830 hi = -(lo < 0);
9831 break;
9833 case CONST_DOUBLE:
9834 lo = CONST_DOUBLE_LOW (elt);
9835 hi = CONST_DOUBLE_HIGH (elt);
9836 break;
9838 default:
9839 gcc_unreachable ();
9842 if (elt_size <= sizeof (HOST_WIDE_INT))
9843 insert_int (lo, elt_size, p);
9844 else
9846 unsigned char *p0 = p;
9847 unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
9849 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
9850 if (WORDS_BIG_ENDIAN)
9852 p0 = p1;
9853 p1 = p;
9855 insert_int (lo, sizeof (HOST_WIDE_INT), p0);
9856 insert_int (hi, sizeof (HOST_WIDE_INT), p1);
9859 break;
9861 case MODE_VECTOR_FLOAT:
9862 for (i = 0, p = array; i < length; i++, p += elt_size)
9864 rtx elt = CONST_VECTOR_ELT (rtl, i);
9865 insert_float (elt, p);
9867 break;
9869 default:
9870 gcc_unreachable ();
9873 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
9875 break;
9877 case CONST_STRING:
9878 add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
9879 break;
9881 case SYMBOL_REF:
9882 case LABEL_REF:
9883 case CONST:
9884 add_AT_addr (die, DW_AT_const_value, rtl);
9885 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
9886 break;
9888 case PLUS:
9889 /* In cases where an inlined instance of an inline function is passed
9890 the address of an `auto' variable (which is local to the caller) we
9891 can get a situation where the DECL_RTL of the artificial local
9892 variable (for the inlining) which acts as a stand-in for the
9893 corresponding formal parameter (of the inline function) will look
9894 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
9895 exactly a compile-time constant expression, but it isn't the address
9896 of the (artificial) local variable either. Rather, it represents the
9897 *value* which the artificial local variable always has during its
9898 lifetime. We currently have no way to represent such quasi-constant
9899 values in Dwarf, so for now we just punt and generate nothing. */
9900 break;
9902 default:
9903 /* No other kinds of rtx should be possible here. */
9904 gcc_unreachable ();
9909 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
9910 for use in a later add_const_value_attribute call. */
9912 static rtx
9913 rtl_for_decl_init (tree init, tree type)
9915 rtx rtl = NULL_RTX;
9917 /* If a variable is initialized with a string constant without embedded
9918 zeros, build CONST_STRING. */
9919 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
9921 tree enttype = TREE_TYPE (type);
9922 tree domain = TYPE_DOMAIN (type);
9923 enum machine_mode mode = TYPE_MODE (enttype);
9925 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
9926 && domain
9927 && integer_zerop (TYPE_MIN_VALUE (domain))
9928 && compare_tree_int (TYPE_MAX_VALUE (domain),
9929 TREE_STRING_LENGTH (init) - 1) == 0
9930 && ((size_t) TREE_STRING_LENGTH (init)
9931 == strlen (TREE_STRING_POINTER (init)) + 1))
9932 rtl = gen_rtx_CONST_STRING (VOIDmode,
9933 ggc_strdup (TREE_STRING_POINTER (init)));
9935 /* If the initializer is something that we know will expand into an
9936 immediate RTL constant, expand it now. Expanding anything else
9937 tends to produce unresolved symbols; see debug/5770 and c++/6381. */
9938 /* Aggregate, vector, and complex types may contain constructors that may
9939 result in code being generated when expand_expr is called, so we can't
9940 handle them here. Integer and float are useful and safe types to handle
9941 here. */
9942 else if ((INTEGRAL_TYPE_P (type) || SCALAR_FLOAT_TYPE_P (type))
9943 && initializer_constant_valid_p (init, type) == null_pointer_node)
9945 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
9947 /* If expand_expr returns a MEM, it wasn't immediate. */
9948 gcc_assert (!rtl || !MEM_P (rtl));
9951 return rtl;
9954 /* Generate RTL for the variable DECL to represent its location. */
9956 static rtx
9957 rtl_for_decl_location (tree decl)
9959 rtx rtl;
9961 /* Here we have to decide where we are going to say the parameter "lives"
9962 (as far as the debugger is concerned). We only have a couple of
9963 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
9965 DECL_RTL normally indicates where the parameter lives during most of the
9966 activation of the function. If optimization is enabled however, this
9967 could be either NULL or else a pseudo-reg. Both of those cases indicate
9968 that the parameter doesn't really live anywhere (as far as the code
9969 generation parts of GCC are concerned) during most of the function's
9970 activation. That will happen (for example) if the parameter is never
9971 referenced within the function.
9973 We could just generate a location descriptor here for all non-NULL
9974 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
9975 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
9976 where DECL_RTL is NULL or is a pseudo-reg.
9978 Note however that we can only get away with using DECL_INCOMING_RTL as
9979 a backup substitute for DECL_RTL in certain limited cases. In cases
9980 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
9981 we can be sure that the parameter was passed using the same type as it is
9982 declared to have within the function, and that its DECL_INCOMING_RTL
9983 points us to a place where a value of that type is passed.
9985 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
9986 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
9987 because in these cases DECL_INCOMING_RTL points us to a value of some
9988 type which is *different* from the type of the parameter itself. Thus,
9989 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
9990 such cases, the debugger would end up (for example) trying to fetch a
9991 `float' from a place which actually contains the first part of a
9992 `double'. That would lead to really incorrect and confusing
9993 output at debug-time.
9995 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
9996 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
9997 are a couple of exceptions however. On little-endian machines we can
9998 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
9999 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
10000 an integral type that is smaller than TREE_TYPE (decl). These cases arise
10001 when (on a little-endian machine) a non-prototyped function has a
10002 parameter declared to be of type `short' or `char'. In such cases,
10003 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
10004 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
10005 passed `int' value. If the debugger then uses that address to fetch
10006 a `short' or a `char' (on a little-endian machine) the result will be
10007 the correct data, so we allow for such exceptional cases below.
10009 Note that our goal here is to describe the place where the given formal
10010 parameter lives during most of the function's activation (i.e. between the
10011 end of the prologue and the start of the epilogue). We'll do that as best
10012 as we can. Note however that if the given formal parameter is modified
10013 sometime during the execution of the function, then a stack backtrace (at
10014 debug-time) will show the function as having been called with the *new*
10015 value rather than the value which was originally passed in. This happens
10016 rarely enough that it is not a major problem, but it *is* a problem, and
10017 I'd like to fix it.
10019 A future version of dwarf2out.c may generate two additional attributes for
10020 any given DW_TAG_formal_parameter DIE which will describe the "passed
10021 type" and the "passed location" for the given formal parameter in addition
10022 to the attributes we now generate to indicate the "declared type" and the
10023 "active location" for each parameter. This additional set of attributes
10024 could be used by debuggers for stack backtraces. Separately, note that
10025 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
10026 This happens (for example) for inlined-instances of inline function formal
10027 parameters which are never referenced. This really shouldn't be
10028 happening. All PARM_DECL nodes should get valid non-NULL
10029 DECL_INCOMING_RTL values. FIXME. */
10031 /* Use DECL_RTL as the "location" unless we find something better. */
10032 rtl = DECL_RTL_IF_SET (decl);
10034 /* When generating abstract instances, ignore everything except
10035 constants, symbols living in memory, and symbols living in
10036 fixed registers. */
10037 if (! reload_completed)
10039 if (rtl
10040 && (CONSTANT_P (rtl)
10041 || (MEM_P (rtl)
10042 && CONSTANT_P (XEXP (rtl, 0)))
10043 || (REG_P (rtl)
10044 && TREE_CODE (decl) == VAR_DECL
10045 && TREE_STATIC (decl))))
10047 rtl = targetm.delegitimize_address (rtl);
10048 return rtl;
10050 rtl = NULL_RTX;
10052 else if (TREE_CODE (decl) == PARM_DECL)
10054 if (rtl == NULL_RTX || is_pseudo_reg (rtl))
10056 tree declared_type = TREE_TYPE (decl);
10057 tree passed_type = DECL_ARG_TYPE (decl);
10058 enum machine_mode dmode = TYPE_MODE (declared_type);
10059 enum machine_mode pmode = TYPE_MODE (passed_type);
10061 /* This decl represents a formal parameter which was optimized out.
10062 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
10063 all cases where (rtl == NULL_RTX) just below. */
10064 if (dmode == pmode)
10065 rtl = DECL_INCOMING_RTL (decl);
10066 else if (SCALAR_INT_MODE_P (dmode)
10067 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
10068 && DECL_INCOMING_RTL (decl))
10070 rtx inc = DECL_INCOMING_RTL (decl);
10071 if (REG_P (inc))
10072 rtl = inc;
10073 else if (MEM_P (inc))
10075 if (BYTES_BIG_ENDIAN)
10076 rtl = adjust_address_nv (inc, dmode,
10077 GET_MODE_SIZE (pmode)
10078 - GET_MODE_SIZE (dmode));
10079 else
10080 rtl = inc;
10085 /* If the parm was passed in registers, but lives on the stack, then
10086 make a big endian correction if the mode of the type of the
10087 parameter is not the same as the mode of the rtl. */
10088 /* ??? This is the same series of checks that are made in dbxout.c before
10089 we reach the big endian correction code there. It isn't clear if all
10090 of these checks are necessary here, but keeping them all is the safe
10091 thing to do. */
10092 else if (MEM_P (rtl)
10093 && XEXP (rtl, 0) != const0_rtx
10094 && ! CONSTANT_P (XEXP (rtl, 0))
10095 /* Not passed in memory. */
10096 && !MEM_P (DECL_INCOMING_RTL (decl))
10097 /* Not passed by invisible reference. */
10098 && (!REG_P (XEXP (rtl, 0))
10099 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
10100 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
10101 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
10102 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
10103 #endif
10105 /* Big endian correction check. */
10106 && BYTES_BIG_ENDIAN
10107 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
10108 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
10109 < UNITS_PER_WORD))
10111 int offset = (UNITS_PER_WORD
10112 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
10114 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10115 plus_constant (XEXP (rtl, 0), offset));
10118 else if (TREE_CODE (decl) == VAR_DECL
10119 && rtl
10120 && MEM_P (rtl)
10121 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
10122 && BYTES_BIG_ENDIAN)
10124 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
10125 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
10127 /* If a variable is declared "register" yet is smaller than
10128 a register, then if we store the variable to memory, it
10129 looks like we're storing a register-sized value, when in
10130 fact we are not. We need to adjust the offset of the
10131 storage location to reflect the actual value's bytes,
10132 else gdb will not be able to display it. */
10133 if (rsize > dsize)
10134 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10135 plus_constant (XEXP (rtl, 0), rsize-dsize));
10138 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
10139 and will have been substituted directly into all expressions that use it.
10140 C does not have such a concept, but C++ and other languages do. */
10141 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
10142 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
10144 if (rtl)
10145 rtl = targetm.delegitimize_address (rtl);
10147 /* If we don't look past the constant pool, we risk emitting a
10148 reference to a constant pool entry that isn't referenced from
10149 code, and thus is not emitted. */
10150 if (rtl)
10151 rtl = avoid_constant_pool_reference (rtl);
10153 return rtl;
10156 /* We need to figure out what section we should use as the base for the
10157 address ranges where a given location is valid.
10158 1. If this particular DECL has a section associated with it, use that.
10159 2. If this function has a section associated with it, use that.
10160 3. Otherwise, use the text section.
10161 XXX: If you split a variable across multiple sections, we won't notice. */
10163 static const char *
10164 secname_for_decl (tree decl)
10166 const char *secname;
10168 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
10170 tree sectree = DECL_SECTION_NAME (decl);
10171 secname = TREE_STRING_POINTER (sectree);
10173 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
10175 tree sectree = DECL_SECTION_NAME (current_function_decl);
10176 secname = TREE_STRING_POINTER (sectree);
10178 else if (cfun
10179 && (last_text_section == in_unlikely_executed_text
10180 || (last_text_section == in_named
10181 && last_text_section_name
10182 == cfun->unlikely_text_section_name)))
10183 secname = cfun->cold_section_label;
10184 else
10185 secname = text_section_label;
10187 return secname;
10190 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
10191 data attribute for a variable or a parameter. We generate the
10192 DW_AT_const_value attribute only in those cases where the given variable
10193 or parameter does not have a true "location" either in memory or in a
10194 register. This can happen (for example) when a constant is passed as an
10195 actual argument in a call to an inline function. (It's possible that
10196 these things can crop up in other ways also.) Note that one type of
10197 constant value which can be passed into an inlined function is a constant
10198 pointer. This can happen for example if an actual argument in an inlined
10199 function call evaluates to a compile-time constant address. */
10201 static void
10202 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
10203 enum dwarf_attribute attr)
10205 rtx rtl;
10206 dw_loc_descr_ref descr;
10207 var_loc_list *loc_list;
10208 struct var_loc_node *node;
10209 if (TREE_CODE (decl) == ERROR_MARK)
10210 return;
10212 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
10213 || TREE_CODE (decl) == RESULT_DECL);
10215 /* See if we possibly have multiple locations for this variable. */
10216 loc_list = lookup_decl_loc (decl);
10218 /* If it truly has multiple locations, the first and last node will
10219 differ. */
10220 if (loc_list && loc_list->first != loc_list->last)
10222 const char *endname, *secname;
10223 dw_loc_list_ref list;
10224 rtx varloc;
10226 /* Now that we know what section we are using for a base,
10227 actually construct the list of locations.
10228 The first location information is what is passed to the
10229 function that creates the location list, and the remaining
10230 locations just get added on to that list.
10231 Note that we only know the start address for a location
10232 (IE location changes), so to build the range, we use
10233 the range [current location start, next location start].
10234 This means we have to special case the last node, and generate
10235 a range of [last location start, end of function label]. */
10237 node = loc_list->first;
10238 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10239 secname = secname_for_decl (decl);
10241 list = new_loc_list (loc_descriptor (varloc),
10242 node->label, node->next->label, secname, 1);
10243 node = node->next;
10245 for (; node->next; node = node->next)
10246 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10248 /* The variable has a location between NODE->LABEL and
10249 NODE->NEXT->LABEL. */
10250 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10251 add_loc_descr_to_loc_list (&list, loc_descriptor (varloc),
10252 node->label, node->next->label, secname);
10255 /* If the variable has a location at the last label
10256 it keeps its location until the end of function. */
10257 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10259 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
10261 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10262 if (!current_function_decl)
10263 endname = text_end_label;
10264 else
10266 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
10267 current_function_funcdef_no);
10268 endname = ggc_strdup (label_id);
10270 add_loc_descr_to_loc_list (&list, loc_descriptor (varloc),
10271 node->label, endname, secname);
10274 /* Finally, add the location list to the DIE, and we are done. */
10275 add_AT_loc_list (die, attr, list);
10276 return;
10279 /* Try to get some constant RTL for this decl, and use that as the value of
10280 the location. */
10282 rtl = rtl_for_decl_location (decl);
10283 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
10285 add_const_value_attribute (die, rtl);
10286 return;
10289 /* If we have tried to generate the location otherwise, and it
10290 didn't work out (we wouldn't be here if we did), and we have a one entry
10291 location list, try generating a location from that. */
10292 if (loc_list && loc_list->first)
10294 node = loc_list->first;
10295 descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note));
10296 if (descr)
10298 add_AT_location_description (die, attr, descr);
10299 return;
10303 /* We couldn't get any rtl, so try directly generating the location
10304 description from the tree. */
10305 descr = loc_descriptor_from_tree (decl);
10306 if (descr)
10308 add_AT_location_description (die, attr, descr);
10309 return;
10313 /* If we don't have a copy of this variable in memory for some reason (such
10314 as a C++ member constant that doesn't have an out-of-line definition),
10315 we should tell the debugger about the constant value. */
10317 static void
10318 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
10320 tree init = DECL_INITIAL (decl);
10321 tree type = TREE_TYPE (decl);
10322 rtx rtl;
10324 if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
10325 /* OK */;
10326 else
10327 return;
10329 rtl = rtl_for_decl_init (init, type);
10330 if (rtl)
10331 add_const_value_attribute (var_die, rtl);
10334 #ifdef DWARF2_UNWIND_INFO
10335 /* Convert the CFI instructions for the current function into a location
10336 list. This is used for DW_AT_frame_base when we targeting a dwarf2
10337 consumer that does not support the dwarf3 DW_OP_call_frame_cfa. */
10339 static dw_loc_list_ref
10340 convert_cfa_to_loc_list (void)
10342 dw_fde_ref fde;
10343 dw_loc_list_ref list, *list_tail;
10344 dw_cfi_ref cfi;
10345 dw_cfa_location last_cfa, next_cfa;
10346 const char *start_label, *last_label, *section;
10348 fde = &fde_table[fde_table_in_use - 1];
10350 section = secname_for_decl (current_function_decl);
10351 list_tail = &list;
10352 list = NULL;
10354 next_cfa.reg = INVALID_REGNUM;
10355 next_cfa.offset = 0;
10356 next_cfa.indirect = 0;
10357 next_cfa.base_offset = 0;
10359 start_label = fde->dw_fde_begin;
10361 /* ??? Bald assumption that the CIE opcode list does not contain
10362 advance opcodes. */
10363 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
10364 lookup_cfa_1 (cfi, &next_cfa);
10366 last_cfa = next_cfa;
10367 last_label = start_label;
10369 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
10370 switch (cfi->dw_cfi_opc)
10372 case DW_CFA_advance_loc1:
10373 case DW_CFA_advance_loc2:
10374 case DW_CFA_advance_loc4:
10375 if (!cfa_equal_p (&last_cfa, &next_cfa))
10377 *list_tail = new_loc_list (build_cfa_loc (&last_cfa), start_label,
10378 last_label, section, list == NULL);
10380 list_tail = &(*list_tail)->dw_loc_next;
10381 last_cfa = next_cfa;
10382 start_label = last_label;
10384 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
10385 break;
10387 case DW_CFA_advance_loc:
10388 /* The encoding is complex enough that we should never emit this. */
10389 case DW_CFA_remember_state:
10390 case DW_CFA_restore_state:
10391 /* We don't handle these two in this function. It would be possible
10392 if it were to be required. */
10393 gcc_unreachable ();
10395 default:
10396 lookup_cfa_1 (cfi, &next_cfa);
10397 break;
10400 if (!cfa_equal_p (&last_cfa, &next_cfa))
10402 *list_tail = new_loc_list (build_cfa_loc (&last_cfa), start_label,
10403 last_label, section, list == NULL);
10404 list_tail = &(*list_tail)->dw_loc_next;
10405 start_label = last_label;
10407 *list_tail = new_loc_list (build_cfa_loc (&next_cfa), start_label,
10408 fde->dw_fde_end, section, list == NULL);
10410 return list;
10413 /* Compute a displacement from the "steady-state frame pointer" to
10414 the CFA, and store it in frame_pointer_cfa_offset. */
10416 static void
10417 compute_frame_pointer_to_cfa_displacement (void)
10419 HOST_WIDE_INT offset;
10420 rtx reg, elim;
10422 #ifdef FRAME_POINTER_CFA_OFFSET
10423 reg = frame_pointer_rtx;
10424 offset = FRAME_POINTER_CFA_OFFSET (current_function_decl);
10425 #else
10426 reg = arg_pointer_rtx;
10427 offset = ARG_POINTER_CFA_OFFSET (current_function_decl);
10428 #endif
10430 elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10431 if (GET_CODE (elim) == PLUS)
10433 offset += INTVAL (XEXP (elim, 1));
10434 elim = XEXP (elim, 0);
10436 gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
10437 : stack_pointer_rtx));
10439 frame_pointer_cfa_offset = -offset;
10441 #endif
10443 /* Generate a DW_AT_name attribute given some string value to be included as
10444 the value of the attribute. */
10446 static void
10447 add_name_attribute (dw_die_ref die, const char *name_string)
10449 if (name_string != NULL && *name_string != 0)
10451 if (demangle_name_func)
10452 name_string = (*demangle_name_func) (name_string);
10454 add_AT_string (die, DW_AT_name, name_string);
10458 /* Generate a DW_AT_comp_dir attribute for DIE. */
10460 static void
10461 add_comp_dir_attribute (dw_die_ref die)
10463 const char *wd = get_src_pwd ();
10464 if (wd != NULL)
10465 add_AT_string (die, DW_AT_comp_dir, wd);
10468 /* Given a tree node describing an array bound (either lower or upper) output
10469 a representation for that bound. */
10471 static void
10472 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
10474 switch (TREE_CODE (bound))
10476 case ERROR_MARK:
10477 return;
10479 /* All fixed-bounds are represented by INTEGER_CST nodes. */
10480 case INTEGER_CST:
10481 if (! host_integerp (bound, 0)
10482 || (bound_attr == DW_AT_lower_bound
10483 && (((is_c_family () || is_java ()) && integer_zerop (bound))
10484 || (is_fortran () && integer_onep (bound)))))
10485 /* Use the default. */
10487 else
10488 add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
10489 break;
10491 case CONVERT_EXPR:
10492 case NOP_EXPR:
10493 case NON_LVALUE_EXPR:
10494 case VIEW_CONVERT_EXPR:
10495 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
10496 break;
10498 case SAVE_EXPR:
10499 break;
10501 case VAR_DECL:
10502 case PARM_DECL:
10503 case RESULT_DECL:
10505 dw_die_ref decl_die = lookup_decl_die (bound);
10507 /* ??? Can this happen, or should the variable have been bound
10508 first? Probably it can, since I imagine that we try to create
10509 the types of parameters in the order in which they exist in
10510 the list, and won't have created a forward reference to a
10511 later parameter. */
10512 if (decl_die != NULL)
10513 add_AT_die_ref (subrange_die, bound_attr, decl_die);
10514 break;
10517 default:
10519 /* Otherwise try to create a stack operation procedure to
10520 evaluate the value of the array bound. */
10522 dw_die_ref ctx, decl_die;
10523 dw_loc_descr_ref loc;
10525 loc = loc_descriptor_from_tree (bound);
10526 if (loc == NULL)
10527 break;
10529 if (current_function_decl == 0)
10530 ctx = comp_unit_die;
10531 else
10532 ctx = lookup_decl_die (current_function_decl);
10534 decl_die = new_die (DW_TAG_variable, ctx, bound);
10535 add_AT_flag (decl_die, DW_AT_artificial, 1);
10536 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
10537 add_AT_loc (decl_die, DW_AT_location, loc);
10539 add_AT_die_ref (subrange_die, bound_attr, decl_die);
10540 break;
10545 /* Note that the block of subscript information for an array type also
10546 includes information about the element type of type given array type. */
10548 static void
10549 add_subscript_info (dw_die_ref type_die, tree type)
10551 #ifndef MIPS_DEBUGGING_INFO
10552 unsigned dimension_number;
10553 #endif
10554 tree lower, upper;
10555 dw_die_ref subrange_die;
10557 /* The GNU compilers represent multidimensional array types as sequences of
10558 one dimensional array types whose element types are themselves array
10559 types. Here we squish that down, so that each multidimensional array
10560 type gets only one array_type DIE in the Dwarf debugging info. The draft
10561 Dwarf specification say that we are allowed to do this kind of
10562 compression in C (because there is no difference between an array or
10563 arrays and a multidimensional array in C) but for other source languages
10564 (e.g. Ada) we probably shouldn't do this. */
10566 /* ??? The SGI dwarf reader fails for multidimensional arrays with a
10567 const enum type. E.g. const enum machine_mode insn_operand_mode[2][10].
10568 We work around this by disabling this feature. See also
10569 gen_array_type_die. */
10570 #ifndef MIPS_DEBUGGING_INFO
10571 for (dimension_number = 0;
10572 TREE_CODE (type) == ARRAY_TYPE;
10573 type = TREE_TYPE (type), dimension_number++)
10574 #endif
10576 tree domain = TYPE_DOMAIN (type);
10578 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
10579 and (in GNU C only) variable bounds. Handle all three forms
10580 here. */
10581 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
10582 if (domain)
10584 /* We have an array type with specified bounds. */
10585 lower = TYPE_MIN_VALUE (domain);
10586 upper = TYPE_MAX_VALUE (domain);
10588 /* Define the index type. */
10589 if (TREE_TYPE (domain))
10591 /* ??? This is probably an Ada unnamed subrange type. Ignore the
10592 TREE_TYPE field. We can't emit debug info for this
10593 because it is an unnamed integral type. */
10594 if (TREE_CODE (domain) == INTEGER_TYPE
10595 && TYPE_NAME (domain) == NULL_TREE
10596 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
10597 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
10599 else
10600 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
10601 type_die);
10604 /* ??? If upper is NULL, the array has unspecified length,
10605 but it does have a lower bound. This happens with Fortran
10606 dimension arr(N:*)
10607 Since the debugger is definitely going to need to know N
10608 to produce useful results, go ahead and output the lower
10609 bound solo, and hope the debugger can cope. */
10611 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
10612 if (upper)
10613 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
10616 /* Otherwise we have an array type with an unspecified length. The
10617 DWARF-2 spec does not say how to handle this; let's just leave out the
10618 bounds. */
10622 static void
10623 add_byte_size_attribute (dw_die_ref die, tree tree_node)
10625 unsigned size;
10627 switch (TREE_CODE (tree_node))
10629 case ERROR_MARK:
10630 size = 0;
10631 break;
10632 case ENUMERAL_TYPE:
10633 case RECORD_TYPE:
10634 case UNION_TYPE:
10635 case QUAL_UNION_TYPE:
10636 size = int_size_in_bytes (tree_node);
10637 break;
10638 case FIELD_DECL:
10639 /* For a data member of a struct or union, the DW_AT_byte_size is
10640 generally given as the number of bytes normally allocated for an
10641 object of the *declared* type of the member itself. This is true
10642 even for bit-fields. */
10643 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
10644 break;
10645 default:
10646 gcc_unreachable ();
10649 /* Note that `size' might be -1 when we get to this point. If it is, that
10650 indicates that the byte size of the entity in question is variable. We
10651 have no good way of expressing this fact in Dwarf at the present time,
10652 so just let the -1 pass on through. */
10653 add_AT_unsigned (die, DW_AT_byte_size, size);
10656 /* For a FIELD_DECL node which represents a bit-field, output an attribute
10657 which specifies the distance in bits from the highest order bit of the
10658 "containing object" for the bit-field to the highest order bit of the
10659 bit-field itself.
10661 For any given bit-field, the "containing object" is a hypothetical object
10662 (of some integral or enum type) within which the given bit-field lives. The
10663 type of this hypothetical "containing object" is always the same as the
10664 declared type of the individual bit-field itself. The determination of the
10665 exact location of the "containing object" for a bit-field is rather
10666 complicated. It's handled by the `field_byte_offset' function (above).
10668 Note that it is the size (in bytes) of the hypothetical "containing object"
10669 which will be given in the DW_AT_byte_size attribute for this bit-field.
10670 (See `byte_size_attribute' above). */
10672 static inline void
10673 add_bit_offset_attribute (dw_die_ref die, tree decl)
10675 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
10676 tree type = DECL_BIT_FIELD_TYPE (decl);
10677 HOST_WIDE_INT bitpos_int;
10678 HOST_WIDE_INT highest_order_object_bit_offset;
10679 HOST_WIDE_INT highest_order_field_bit_offset;
10680 HOST_WIDE_INT unsigned bit_offset;
10682 /* Must be a field and a bit field. */
10683 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
10685 /* We can't yet handle bit-fields whose offsets are variable, so if we
10686 encounter such things, just return without generating any attribute
10687 whatsoever. Likewise for variable or too large size. */
10688 if (! host_integerp (bit_position (decl), 0)
10689 || ! host_integerp (DECL_SIZE (decl), 1))
10690 return;
10692 bitpos_int = int_bit_position (decl);
10694 /* Note that the bit offset is always the distance (in bits) from the
10695 highest-order bit of the "containing object" to the highest-order bit of
10696 the bit-field itself. Since the "high-order end" of any object or field
10697 is different on big-endian and little-endian machines, the computation
10698 below must take account of these differences. */
10699 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
10700 highest_order_field_bit_offset = bitpos_int;
10702 if (! BYTES_BIG_ENDIAN)
10704 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
10705 highest_order_object_bit_offset += simple_type_size_in_bits (type);
10708 bit_offset
10709 = (! BYTES_BIG_ENDIAN
10710 ? highest_order_object_bit_offset - highest_order_field_bit_offset
10711 : highest_order_field_bit_offset - highest_order_object_bit_offset);
10713 add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
10716 /* For a FIELD_DECL node which represents a bit field, output an attribute
10717 which specifies the length in bits of the given field. */
10719 static inline void
10720 add_bit_size_attribute (dw_die_ref die, tree decl)
10722 /* Must be a field and a bit field. */
10723 gcc_assert (TREE_CODE (decl) == FIELD_DECL
10724 && DECL_BIT_FIELD_TYPE (decl));
10726 if (host_integerp (DECL_SIZE (decl), 1))
10727 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
10730 /* If the compiled language is ANSI C, then add a 'prototyped'
10731 attribute, if arg types are given for the parameters of a function. */
10733 static inline void
10734 add_prototyped_attribute (dw_die_ref die, tree func_type)
10736 if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
10737 && TYPE_ARG_TYPES (func_type) != NULL)
10738 add_AT_flag (die, DW_AT_prototyped, 1);
10741 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
10742 by looking in either the type declaration or object declaration
10743 equate table. */
10745 static inline void
10746 add_abstract_origin_attribute (dw_die_ref die, tree origin)
10748 dw_die_ref origin_die = NULL;
10750 if (TREE_CODE (origin) != FUNCTION_DECL)
10752 /* We may have gotten separated from the block for the inlined
10753 function, if we're in an exception handler or some such; make
10754 sure that the abstract function has been written out.
10756 Doing this for nested functions is wrong, however; functions are
10757 distinct units, and our context might not even be inline. */
10758 tree fn = origin;
10760 if (TYPE_P (fn))
10761 fn = TYPE_STUB_DECL (fn);
10763 fn = decl_function_context (fn);
10764 if (fn)
10765 dwarf2out_abstract_function (fn);
10768 if (DECL_P (origin))
10769 origin_die = lookup_decl_die (origin);
10770 else if (TYPE_P (origin))
10771 origin_die = lookup_type_die (origin);
10773 /* XXX: Functions that are never lowered don't always have correct block
10774 trees (in the case of java, they simply have no block tree, in some other
10775 languages). For these functions, there is nothing we can really do to
10776 output correct debug info for inlined functions in all cases. Rather
10777 than die, we'll just produce deficient debug info now, in that we will
10778 have variables without a proper abstract origin. In the future, when all
10779 functions are lowered, we should re-add a gcc_assert (origin_die)
10780 here. */
10782 if (origin_die)
10783 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
10786 /* We do not currently support the pure_virtual attribute. */
10788 static inline void
10789 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
10791 if (DECL_VINDEX (func_decl))
10793 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
10795 if (host_integerp (DECL_VINDEX (func_decl), 0))
10796 add_AT_loc (die, DW_AT_vtable_elem_location,
10797 new_loc_descr (DW_OP_constu,
10798 tree_low_cst (DECL_VINDEX (func_decl), 0),
10799 0));
10801 /* GNU extension: Record what type this method came from originally. */
10802 if (debug_info_level > DINFO_LEVEL_TERSE)
10803 add_AT_die_ref (die, DW_AT_containing_type,
10804 lookup_type_die (DECL_CONTEXT (func_decl)));
10808 /* Add source coordinate attributes for the given decl. */
10810 static void
10811 add_src_coords_attributes (dw_die_ref die, tree decl)
10813 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
10814 unsigned file_index = lookup_filename (s.file);
10816 add_AT_unsigned (die, DW_AT_decl_file, file_index);
10817 add_AT_unsigned (die, DW_AT_decl_line, s.line);
10820 /* Add a DW_AT_name attribute and source coordinate attribute for the
10821 given decl, but only if it actually has a name. */
10823 static void
10824 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
10826 tree decl_name;
10828 decl_name = DECL_NAME (decl);
10829 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
10831 add_name_attribute (die, dwarf2_name (decl, 0));
10832 if (! DECL_ARTIFICIAL (decl))
10833 add_src_coords_attributes (die, decl);
10835 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
10836 && TREE_PUBLIC (decl)
10837 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
10838 && !DECL_ABSTRACT (decl)
10839 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl)))
10840 add_AT_string (die, DW_AT_MIPS_linkage_name,
10841 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
10844 #ifdef VMS_DEBUGGING_INFO
10845 /* Get the function's name, as described by its RTL. This may be different
10846 from the DECL_NAME name used in the source file. */
10847 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
10849 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
10850 XEXP (DECL_RTL (decl), 0));
10851 VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
10853 #endif
10856 /* Push a new declaration scope. */
10858 static void
10859 push_decl_scope (tree scope)
10861 VEC_safe_push (tree, gc, decl_scope_table, scope);
10864 /* Pop a declaration scope. */
10866 static inline void
10867 pop_decl_scope (void)
10869 VEC_pop (tree, decl_scope_table);
10872 /* Return the DIE for the scope that immediately contains this type.
10873 Non-named types get global scope. Named types nested in other
10874 types get their containing scope if it's open, or global scope
10875 otherwise. All other types (i.e. function-local named types) get
10876 the current active scope. */
10878 static dw_die_ref
10879 scope_die_for (tree t, dw_die_ref context_die)
10881 dw_die_ref scope_die = NULL;
10882 tree containing_scope;
10883 int i;
10885 /* Non-types always go in the current scope. */
10886 gcc_assert (TYPE_P (t));
10888 containing_scope = TYPE_CONTEXT (t);
10890 /* Use the containing namespace if it was passed in (for a declaration). */
10891 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
10893 if (context_die == lookup_decl_die (containing_scope))
10894 /* OK */;
10895 else
10896 containing_scope = NULL_TREE;
10899 /* Ignore function type "scopes" from the C frontend. They mean that
10900 a tagged type is local to a parmlist of a function declarator, but
10901 that isn't useful to DWARF. */
10902 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
10903 containing_scope = NULL_TREE;
10905 if (containing_scope == NULL_TREE)
10906 scope_die = comp_unit_die;
10907 else if (TYPE_P (containing_scope))
10909 /* For types, we can just look up the appropriate DIE. But
10910 first we check to see if we're in the middle of emitting it
10911 so we know where the new DIE should go. */
10912 for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
10913 if (VEC_index (tree, decl_scope_table, i) == containing_scope)
10914 break;
10916 if (i < 0)
10918 gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
10919 || TREE_ASM_WRITTEN (containing_scope));
10921 /* If none of the current dies are suitable, we get file scope. */
10922 scope_die = comp_unit_die;
10924 else
10925 scope_die = lookup_type_die (containing_scope);
10927 else
10928 scope_die = context_die;
10930 return scope_die;
10933 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
10935 static inline int
10936 local_scope_p (dw_die_ref context_die)
10938 for (; context_die; context_die = context_die->die_parent)
10939 if (context_die->die_tag == DW_TAG_inlined_subroutine
10940 || context_die->die_tag == DW_TAG_subprogram)
10941 return 1;
10943 return 0;
10946 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
10947 whether or not to treat a DIE in this context as a declaration. */
10949 static inline int
10950 class_or_namespace_scope_p (dw_die_ref context_die)
10952 return (context_die
10953 && (context_die->die_tag == DW_TAG_structure_type
10954 || context_die->die_tag == DW_TAG_union_type
10955 || context_die->die_tag == DW_TAG_namespace));
10958 /* Many forms of DIEs require a "type description" attribute. This
10959 routine locates the proper "type descriptor" die for the type given
10960 by 'type', and adds a DW_AT_type attribute below the given die. */
10962 static void
10963 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
10964 int decl_volatile, dw_die_ref context_die)
10966 enum tree_code code = TREE_CODE (type);
10967 dw_die_ref type_die = NULL;
10969 /* ??? If this type is an unnamed subrange type of an integral or
10970 floating-point type, use the inner type. This is because we have no
10971 support for unnamed types in base_type_die. This can happen if this is
10972 an Ada subrange type. Correct solution is emit a subrange type die. */
10973 if ((code == INTEGER_TYPE || code == REAL_TYPE)
10974 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
10975 type = TREE_TYPE (type), code = TREE_CODE (type);
10977 if (code == ERROR_MARK
10978 /* Handle a special case. For functions whose return type is void, we
10979 generate *no* type attribute. (Note that no object may have type
10980 `void', so this only applies to function return types). */
10981 || code == VOID_TYPE)
10982 return;
10984 type_die = modified_type_die (type,
10985 decl_const || TYPE_READONLY (type),
10986 decl_volatile || TYPE_VOLATILE (type),
10987 context_die);
10989 if (type_die != NULL)
10990 add_AT_die_ref (object_die, DW_AT_type, type_die);
10993 /* Given an object die, add the calling convention attribute for the
10994 function call type. */
10995 static void
10996 add_calling_convention_attribute (dw_die_ref subr_die, tree type)
10998 enum dwarf_calling_convention value = DW_CC_normal;
11000 value = targetm.dwarf_calling_convention (type);
11002 /* Only add the attribute if the backend requests it, and
11003 is not DW_CC_normal. */
11004 if (value && (value != DW_CC_normal))
11005 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
11008 /* Given a tree pointer to a struct, class, union, or enum type node, return
11009 a pointer to the (string) tag name for the given type, or zero if the type
11010 was declared without a tag. */
11012 static const char *
11013 type_tag (tree type)
11015 const char *name = 0;
11017 if (TYPE_NAME (type) != 0)
11019 tree t = 0;
11021 /* Find the IDENTIFIER_NODE for the type name. */
11022 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
11023 t = TYPE_NAME (type);
11025 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
11026 a TYPE_DECL node, regardless of whether or not a `typedef' was
11027 involved. */
11028 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11029 && ! DECL_IGNORED_P (TYPE_NAME (type)))
11030 t = DECL_NAME (TYPE_NAME (type));
11032 /* Now get the name as a string, or invent one. */
11033 if (t != 0)
11034 name = IDENTIFIER_POINTER (t);
11037 return (name == 0 || *name == '\0') ? 0 : name;
11040 /* Return the type associated with a data member, make a special check
11041 for bit field types. */
11043 static inline tree
11044 member_declared_type (tree member)
11046 return (DECL_BIT_FIELD_TYPE (member)
11047 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
11050 /* Get the decl's label, as described by its RTL. This may be different
11051 from the DECL_NAME name used in the source file. */
11053 #if 0
11054 static const char *
11055 decl_start_label (tree decl)
11057 rtx x;
11058 const char *fnname;
11060 x = DECL_RTL (decl);
11061 gcc_assert (MEM_P (x));
11063 x = XEXP (x, 0);
11064 gcc_assert (GET_CODE (x) == SYMBOL_REF);
11066 fnname = XSTR (x, 0);
11067 return fnname;
11069 #endif
11071 /* These routines generate the internal representation of the DIE's for
11072 the compilation unit. Debugging information is collected by walking
11073 the declaration trees passed in from dwarf2out_decl(). */
11075 static void
11076 gen_array_type_die (tree type, dw_die_ref context_die)
11078 dw_die_ref scope_die = scope_die_for (type, context_die);
11079 dw_die_ref array_die;
11080 tree element_type;
11082 /* ??? The SGI dwarf reader fails for array of array of enum types unless
11083 the inner array type comes before the outer array type. Thus we must
11084 call gen_type_die before we call new_die. See below also. */
11085 #ifdef MIPS_DEBUGGING_INFO
11086 gen_type_die (TREE_TYPE (type), context_die);
11087 #endif
11089 array_die = new_die (DW_TAG_array_type, scope_die, type);
11090 add_name_attribute (array_die, type_tag (type));
11091 equate_type_number_to_die (type, array_die);
11093 if (TREE_CODE (type) == VECTOR_TYPE)
11095 /* The frontend feeds us a representation for the vector as a struct
11096 containing an array. Pull out the array type. */
11097 type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
11098 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
11101 #if 0
11102 /* We default the array ordering. SDB will probably do
11103 the right things even if DW_AT_ordering is not present. It's not even
11104 an issue until we start to get into multidimensional arrays anyway. If
11105 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
11106 then we'll have to put the DW_AT_ordering attribute back in. (But if
11107 and when we find out that we need to put these in, we will only do so
11108 for multidimensional arrays. */
11109 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
11110 #endif
11112 #ifdef MIPS_DEBUGGING_INFO
11113 /* The SGI compilers handle arrays of unknown bound by setting
11114 AT_declaration and not emitting any subrange DIEs. */
11115 if (! TYPE_DOMAIN (type))
11116 add_AT_flag (array_die, DW_AT_declaration, 1);
11117 else
11118 #endif
11119 add_subscript_info (array_die, type);
11121 /* Add representation of the type of the elements of this array type. */
11122 element_type = TREE_TYPE (type);
11124 /* ??? The SGI dwarf reader fails for multidimensional arrays with a
11125 const enum type. E.g. const enum machine_mode insn_operand_mode[2][10].
11126 We work around this by disabling this feature. See also
11127 add_subscript_info. */
11128 #ifndef MIPS_DEBUGGING_INFO
11129 while (TREE_CODE (element_type) == ARRAY_TYPE)
11130 element_type = TREE_TYPE (element_type);
11132 gen_type_die (element_type, context_die);
11133 #endif
11135 add_type_attribute (array_die, element_type, 0, 0, context_die);
11138 #if 0
11139 static void
11140 gen_entry_point_die (tree decl, dw_die_ref context_die)
11142 tree origin = decl_ultimate_origin (decl);
11143 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
11145 if (origin != NULL)
11146 add_abstract_origin_attribute (decl_die, origin);
11147 else
11149 add_name_and_src_coords_attributes (decl_die, decl);
11150 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
11151 0, 0, context_die);
11154 if (DECL_ABSTRACT (decl))
11155 equate_decl_number_to_die (decl, decl_die);
11156 else
11157 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
11159 #endif
11161 /* Walk through the list of incomplete types again, trying once more to
11162 emit full debugging info for them. */
11164 static void
11165 retry_incomplete_types (void)
11167 int i;
11169 for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
11170 gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
11173 /* Generate a DIE to represent an inlined instance of an enumeration type. */
11175 static void
11176 gen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
11178 dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
11180 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11181 be incomplete and such types are not marked. */
11182 add_abstract_origin_attribute (type_die, type);
11185 /* Generate a DIE to represent an inlined instance of a structure type. */
11187 static void
11188 gen_inlined_structure_type_die (tree type, dw_die_ref context_die)
11190 dw_die_ref type_die = new_die (DW_TAG_structure_type, context_die, type);
11192 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11193 be incomplete and such types are not marked. */
11194 add_abstract_origin_attribute (type_die, type);
11197 /* Generate a DIE to represent an inlined instance of a union type. */
11199 static void
11200 gen_inlined_union_type_die (tree type, dw_die_ref context_die)
11202 dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
11204 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11205 be incomplete and such types are not marked. */
11206 add_abstract_origin_attribute (type_die, type);
11209 /* Generate a DIE to represent an enumeration type. Note that these DIEs
11210 include all of the information about the enumeration values also. Each
11211 enumerated type name/value is listed as a child of the enumerated type
11212 DIE. */
11214 static dw_die_ref
11215 gen_enumeration_type_die (tree type, dw_die_ref context_die)
11217 dw_die_ref type_die = lookup_type_die (type);
11219 if (type_die == NULL)
11221 type_die = new_die (DW_TAG_enumeration_type,
11222 scope_die_for (type, context_die), type);
11223 equate_type_number_to_die (type, type_die);
11224 add_name_attribute (type_die, type_tag (type));
11226 else if (! TYPE_SIZE (type))
11227 return type_die;
11228 else
11229 remove_AT (type_die, DW_AT_declaration);
11231 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
11232 given enum type is incomplete, do not generate the DW_AT_byte_size
11233 attribute or the DW_AT_element_list attribute. */
11234 if (TYPE_SIZE (type))
11236 tree link;
11238 TREE_ASM_WRITTEN (type) = 1;
11239 add_byte_size_attribute (type_die, type);
11240 if (TYPE_STUB_DECL (type) != NULL_TREE)
11241 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
11243 /* If the first reference to this type was as the return type of an
11244 inline function, then it may not have a parent. Fix this now. */
11245 if (type_die->die_parent == NULL)
11246 add_child_die (scope_die_for (type, context_die), type_die);
11248 for (link = TYPE_VALUES (type);
11249 link != NULL; link = TREE_CHAIN (link))
11251 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
11252 tree value = TREE_VALUE (link);
11254 add_name_attribute (enum_die,
11255 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
11257 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
11258 /* DWARF2 does not provide a way of indicating whether or
11259 not enumeration constants are signed or unsigned. GDB
11260 always assumes the values are signed, so we output all
11261 values as if they were signed. That means that
11262 enumeration constants with very large unsigned values
11263 will appear to have negative values in the debugger. */
11264 add_AT_int (enum_die, DW_AT_const_value,
11265 tree_low_cst (value, tree_int_cst_sgn (value) > 0));
11268 else
11269 add_AT_flag (type_die, DW_AT_declaration, 1);
11271 return type_die;
11274 /* Generate a DIE to represent either a real live formal parameter decl or to
11275 represent just the type of some formal parameter position in some function
11276 type.
11278 Note that this routine is a bit unusual because its argument may be a
11279 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
11280 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
11281 node. If it's the former then this function is being called to output a
11282 DIE to represent a formal parameter object (or some inlining thereof). If
11283 it's the latter, then this function is only being called to output a
11284 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
11285 argument type of some subprogram type. */
11287 static dw_die_ref
11288 gen_formal_parameter_die (tree node, dw_die_ref context_die)
11290 dw_die_ref parm_die
11291 = new_die (DW_TAG_formal_parameter, context_die, node);
11292 tree origin;
11294 switch (TREE_CODE_CLASS (TREE_CODE (node)))
11296 case tcc_declaration:
11297 origin = decl_ultimate_origin (node);
11298 if (origin != NULL)
11299 add_abstract_origin_attribute (parm_die, origin);
11300 else
11302 add_name_and_src_coords_attributes (parm_die, node);
11303 add_type_attribute (parm_die, TREE_TYPE (node),
11304 TREE_READONLY (node),
11305 TREE_THIS_VOLATILE (node),
11306 context_die);
11307 if (DECL_ARTIFICIAL (node))
11308 add_AT_flag (parm_die, DW_AT_artificial, 1);
11311 equate_decl_number_to_die (node, parm_die);
11312 if (! DECL_ABSTRACT (node))
11313 add_location_or_const_value_attribute (parm_die, node, DW_AT_location);
11315 break;
11317 case tcc_type:
11318 /* We were called with some kind of a ..._TYPE node. */
11319 add_type_attribute (parm_die, node, 0, 0, context_die);
11320 break;
11322 default:
11323 gcc_unreachable ();
11326 return parm_die;
11329 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
11330 at the end of an (ANSI prototyped) formal parameters list. */
11332 static void
11333 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
11335 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
11338 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
11339 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
11340 parameters as specified in some function type specification (except for
11341 those which appear as part of a function *definition*). */
11343 static void
11344 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
11346 tree link;
11347 tree formal_type = NULL;
11348 tree first_parm_type;
11349 tree arg;
11351 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
11353 arg = DECL_ARGUMENTS (function_or_method_type);
11354 function_or_method_type = TREE_TYPE (function_or_method_type);
11356 else
11357 arg = NULL_TREE;
11359 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
11361 /* Make our first pass over the list of formal parameter types and output a
11362 DW_TAG_formal_parameter DIE for each one. */
11363 for (link = first_parm_type; link; )
11365 dw_die_ref parm_die;
11367 formal_type = TREE_VALUE (link);
11368 if (formal_type == void_type_node)
11369 break;
11371 /* Output a (nameless) DIE to represent the formal parameter itself. */
11372 parm_die = gen_formal_parameter_die (formal_type, context_die);
11373 if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
11374 && link == first_parm_type)
11375 || (arg && DECL_ARTIFICIAL (arg)))
11376 add_AT_flag (parm_die, DW_AT_artificial, 1);
11378 link = TREE_CHAIN (link);
11379 if (arg)
11380 arg = TREE_CHAIN (arg);
11383 /* If this function type has an ellipsis, add a
11384 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
11385 if (formal_type != void_type_node)
11386 gen_unspecified_parameters_die (function_or_method_type, context_die);
11388 /* Make our second (and final) pass over the list of formal parameter types
11389 and output DIEs to represent those types (as necessary). */
11390 for (link = TYPE_ARG_TYPES (function_or_method_type);
11391 link && TREE_VALUE (link);
11392 link = TREE_CHAIN (link))
11393 gen_type_die (TREE_VALUE (link), context_die);
11396 /* We want to generate the DIE for TYPE so that we can generate the
11397 die for MEMBER, which has been defined; we will need to refer back
11398 to the member declaration nested within TYPE. If we're trying to
11399 generate minimal debug info for TYPE, processing TYPE won't do the
11400 trick; we need to attach the member declaration by hand. */
11402 static void
11403 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
11405 gen_type_die (type, context_die);
11407 /* If we're trying to avoid duplicate debug info, we may not have
11408 emitted the member decl for this function. Emit it now. */
11409 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
11410 && ! lookup_decl_die (member))
11412 dw_die_ref type_die;
11413 gcc_assert (!decl_ultimate_origin (member));
11415 push_decl_scope (type);
11416 type_die = lookup_type_die (type);
11417 if (TREE_CODE (member) == FUNCTION_DECL)
11418 gen_subprogram_die (member, type_die);
11419 else if (TREE_CODE (member) == FIELD_DECL)
11421 /* Ignore the nameless fields that are used to skip bits but handle
11422 C++ anonymous unions and structs. */
11423 if (DECL_NAME (member) != NULL_TREE
11424 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
11425 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
11427 gen_type_die (member_declared_type (member), type_die);
11428 gen_field_die (member, type_die);
11431 else
11432 gen_variable_die (member, type_die);
11434 pop_decl_scope ();
11438 /* Generate the DWARF2 info for the "abstract" instance of a function which we
11439 may later generate inlined and/or out-of-line instances of. */
11441 static void
11442 dwarf2out_abstract_function (tree decl)
11444 dw_die_ref old_die;
11445 tree save_fn;
11446 tree context;
11447 int was_abstract = DECL_ABSTRACT (decl);
11449 /* Make sure we have the actual abstract inline, not a clone. */
11450 decl = DECL_ORIGIN (decl);
11452 old_die = lookup_decl_die (decl);
11453 if (old_die && get_AT (old_die, DW_AT_inline))
11454 /* We've already generated the abstract instance. */
11455 return;
11457 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
11458 we don't get confused by DECL_ABSTRACT. */
11459 if (debug_info_level > DINFO_LEVEL_TERSE)
11461 context = decl_class_context (decl);
11462 if (context)
11463 gen_type_die_for_member
11464 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
11467 /* Pretend we've just finished compiling this function. */
11468 save_fn = current_function_decl;
11469 current_function_decl = decl;
11471 set_decl_abstract_flags (decl, 1);
11472 dwarf2out_decl (decl);
11473 if (! was_abstract)
11474 set_decl_abstract_flags (decl, 0);
11476 current_function_decl = save_fn;
11479 /* Generate a DIE to represent a declared function (either file-scope or
11480 block-local). */
11482 static void
11483 gen_subprogram_die (tree decl, dw_die_ref context_die)
11485 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
11486 tree origin = decl_ultimate_origin (decl);
11487 dw_die_ref subr_die;
11488 tree fn_arg_types;
11489 tree outer_scope;
11490 dw_die_ref old_die = lookup_decl_die (decl);
11491 int declaration = (current_function_decl != decl
11492 || class_or_namespace_scope_p (context_die));
11494 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
11495 started to generate the abstract instance of an inline, decided to output
11496 its containing class, and proceeded to emit the declaration of the inline
11497 from the member list for the class. If so, DECLARATION takes priority;
11498 we'll get back to the abstract instance when done with the class. */
11500 /* The class-scope declaration DIE must be the primary DIE. */
11501 if (origin && declaration && class_or_namespace_scope_p (context_die))
11503 origin = NULL;
11504 gcc_assert (!old_die);
11507 /* Now that the C++ front end lazily declares artificial member fns, we
11508 might need to retrofit the declaration into its class. */
11509 if (!declaration && !origin && !old_die
11510 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
11511 && !class_or_namespace_scope_p (context_die)
11512 && debug_info_level > DINFO_LEVEL_TERSE)
11513 old_die = force_decl_die (decl);
11515 if (origin != NULL)
11517 gcc_assert (!declaration || local_scope_p (context_die));
11519 /* Fixup die_parent for the abstract instance of a nested
11520 inline function. */
11521 if (old_die && old_die->die_parent == NULL)
11522 add_child_die (context_die, old_die);
11524 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11525 add_abstract_origin_attribute (subr_die, origin);
11527 else if (old_die)
11529 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11530 unsigned file_index = lookup_filename (s.file);
11532 if (!get_AT_flag (old_die, DW_AT_declaration)
11533 /* We can have a normal definition following an inline one in the
11534 case of redefinition of GNU C extern inlines.
11535 It seems reasonable to use AT_specification in this case. */
11536 && !get_AT (old_die, DW_AT_inline))
11538 /* Detect and ignore this case, where we are trying to output
11539 something we have already output. */
11540 return;
11543 /* If the definition comes from the same place as the declaration,
11544 maybe use the old DIE. We always want the DIE for this function
11545 that has the *_pc attributes to be under comp_unit_die so the
11546 debugger can find it. We also need to do this for abstract
11547 instances of inlines, since the spec requires the out-of-line copy
11548 to have the same parent. For local class methods, this doesn't
11549 apply; we just use the old DIE. */
11550 if ((old_die->die_parent == comp_unit_die || context_die == NULL)
11551 && (DECL_ARTIFICIAL (decl)
11552 || (get_AT_unsigned (old_die, DW_AT_decl_file) == file_index
11553 && (get_AT_unsigned (old_die, DW_AT_decl_line)
11554 == (unsigned) s.line))))
11556 subr_die = old_die;
11558 /* Clear out the declaration attribute and the formal parameters.
11559 Do not remove all children, because it is possible that this
11560 declaration die was forced using force_decl_die(). In such
11561 cases die that forced declaration die (e.g. TAG_imported_module)
11562 is one of the children that we do not want to remove. */
11563 remove_AT (subr_die, DW_AT_declaration);
11564 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
11566 else
11568 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11569 add_AT_specification (subr_die, old_die);
11570 if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
11571 add_AT_unsigned (subr_die, DW_AT_decl_file, file_index);
11572 if (get_AT_unsigned (old_die, DW_AT_decl_line)
11573 != (unsigned) s.line)
11574 add_AT_unsigned
11575 (subr_die, DW_AT_decl_line, s.line);
11578 else
11580 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11582 if (TREE_PUBLIC (decl))
11583 add_AT_flag (subr_die, DW_AT_external, 1);
11585 add_name_and_src_coords_attributes (subr_die, decl);
11586 if (debug_info_level > DINFO_LEVEL_TERSE)
11588 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
11589 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
11590 0, 0, context_die);
11593 add_pure_or_virtual_attribute (subr_die, decl);
11594 if (DECL_ARTIFICIAL (decl))
11595 add_AT_flag (subr_die, DW_AT_artificial, 1);
11597 if (TREE_PROTECTED (decl))
11598 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
11599 else if (TREE_PRIVATE (decl))
11600 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
11603 if (declaration)
11605 if (!old_die || !get_AT (old_die, DW_AT_inline))
11607 add_AT_flag (subr_die, DW_AT_declaration, 1);
11609 /* The first time we see a member function, it is in the context of
11610 the class to which it belongs. We make sure of this by emitting
11611 the class first. The next time is the definition, which is
11612 handled above. The two may come from the same source text.
11614 Note that force_decl_die() forces function declaration die. It is
11615 later reused to represent definition. */
11616 equate_decl_number_to_die (decl, subr_die);
11619 else if (DECL_ABSTRACT (decl))
11621 if (DECL_DECLARED_INLINE_P (decl))
11623 if (cgraph_function_possibly_inlined_p (decl))
11624 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
11625 else
11626 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
11628 else
11630 if (cgraph_function_possibly_inlined_p (decl))
11631 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
11632 else
11633 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
11636 equate_decl_number_to_die (decl, subr_die);
11638 else if (!DECL_EXTERNAL (decl))
11640 if (!old_die || !get_AT (old_die, DW_AT_inline))
11641 equate_decl_number_to_die (decl, subr_die);
11643 if (!flag_reorder_blocks_and_partition)
11645 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
11646 current_function_funcdef_no);
11647 add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
11648 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
11649 current_function_funcdef_no);
11650 add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
11652 add_pubname (decl, subr_die);
11653 add_arange (decl, subr_die);
11655 else
11656 { /* Do nothing for now; maybe need to duplicate die, one for
11657 hot section and ond for cold section, then use the hot/cold
11658 section begin/end labels to generate the aranges... */
11660 add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
11661 add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
11662 add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
11663 add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
11665 add_pubname (decl, subr_die);
11666 add_arange (decl, subr_die);
11667 add_arange (decl, subr_die);
11671 #ifdef MIPS_DEBUGGING_INFO
11672 /* Add a reference to the FDE for this routine. */
11673 add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
11674 #endif
11676 #ifdef DWARF2_UNWIND_INFO
11677 /* We define the "frame base" as the function's CFA. This is more
11678 convenient for several reasons: (1) It's stable across the prologue
11679 and epilogue, which makes it better than just a frame pointer,
11680 (2) With dwarf3, there exists a one-byte encoding that allows us
11681 to reference the .debug_frame data by proxy, but failing that,
11682 (3) We can at least reuse the code inspection and interpretation
11683 code that determines the CFA position at various points in the
11684 function. */
11685 /* ??? Use some command-line or configury switch to enable the use
11686 of dwarf3 DW_OP_call_frame_cfa. At present there are no dwarf
11687 consumers that understand it; fall back to "pure" dwarf2 and
11688 convert the CFA data into a location list. */
11690 dw_loc_list_ref list = convert_cfa_to_loc_list ();
11691 if (list->dw_loc_next)
11692 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
11693 else
11694 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
11697 /* Compute a displacement from the "steady-state frame pointer" to
11698 the CFA. The former is what all stack slots and argument slots
11699 will reference in the rtl; the later is what we've told the
11700 debugger about. We'll need to adjust all frame_base references
11701 by this displacement. */
11702 compute_frame_pointer_to_cfa_displacement ();
11703 #else
11704 /* For targets which support DWARF2, but not DWARF2 call-frame info,
11705 we just use the stack pointer or frame pointer. */
11706 /* ??? Should investigate getting better info via callbacks, or else
11707 by interpreting the IA-64 unwind info. */
11709 rtx fp_reg
11710 = frame_pointer_needed ? hard_frame_pointer_rtx : stack_pointer_rtx;
11711 add_AT_loc (subr_die, DW_AT_frame_base, reg_loc_descriptor (fp_reg));
11713 #endif
11715 if (cfun->static_chain_decl)
11716 add_AT_location_description (subr_die, DW_AT_static_link,
11717 loc_descriptor_from_tree (cfun->static_chain_decl));
11720 /* Now output descriptions of the arguments for this function. This gets
11721 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
11722 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
11723 `...' at the end of the formal parameter list. In order to find out if
11724 there was a trailing ellipsis or not, we must instead look at the type
11725 associated with the FUNCTION_DECL. This will be a node of type
11726 FUNCTION_TYPE. If the chain of type nodes hanging off of this
11727 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
11728 an ellipsis at the end. */
11730 /* In the case where we are describing a mere function declaration, all we
11731 need to do here (and all we *can* do here) is to describe the *types* of
11732 its formal parameters. */
11733 if (debug_info_level <= DINFO_LEVEL_TERSE)
11735 else if (declaration)
11736 gen_formal_types_die (decl, subr_die);
11737 else
11739 /* Generate DIEs to represent all known formal parameters. */
11740 tree arg_decls = DECL_ARGUMENTS (decl);
11741 tree parm;
11743 /* When generating DIEs, generate the unspecified_parameters DIE
11744 instead if we come across the arg "__builtin_va_alist" */
11745 for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
11746 if (TREE_CODE (parm) == PARM_DECL)
11748 if (DECL_NAME (parm)
11749 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
11750 "__builtin_va_alist"))
11751 gen_unspecified_parameters_die (parm, subr_die);
11752 else
11753 gen_decl_die (parm, subr_die);
11756 /* Decide whether we need an unspecified_parameters DIE at the end.
11757 There are 2 more cases to do this for: 1) the ansi ... declaration -
11758 this is detectable when the end of the arg list is not a
11759 void_type_node 2) an unprototyped function declaration (not a
11760 definition). This just means that we have no info about the
11761 parameters at all. */
11762 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
11763 if (fn_arg_types != NULL)
11765 /* This is the prototyped case, check for.... */
11766 if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
11767 gen_unspecified_parameters_die (decl, subr_die);
11769 else if (DECL_INITIAL (decl) == NULL_TREE)
11770 gen_unspecified_parameters_die (decl, subr_die);
11773 /* Output Dwarf info for all of the stuff within the body of the function
11774 (if it has one - it may be just a declaration). */
11775 outer_scope = DECL_INITIAL (decl);
11777 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
11778 a function. This BLOCK actually represents the outermost binding contour
11779 for the function, i.e. the contour in which the function's formal
11780 parameters and labels get declared. Curiously, it appears that the front
11781 end doesn't actually put the PARM_DECL nodes for the current function onto
11782 the BLOCK_VARS list for this outer scope, but are strung off of the
11783 DECL_ARGUMENTS list for the function instead.
11785 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
11786 the LABEL_DECL nodes for the function however, and we output DWARF info
11787 for those in decls_for_scope. Just within the `outer_scope' there will be
11788 a BLOCK node representing the function's outermost pair of curly braces,
11789 and any blocks used for the base and member initializers of a C++
11790 constructor function. */
11791 if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
11793 /* Emit a DW_TAG_variable DIE for a named return value. */
11794 if (DECL_NAME (DECL_RESULT (decl)))
11795 gen_decl_die (DECL_RESULT (decl), subr_die);
11797 current_function_has_inlines = 0;
11798 decls_for_scope (outer_scope, subr_die, 0);
11800 #if 0 && defined (MIPS_DEBUGGING_INFO)
11801 if (current_function_has_inlines)
11803 add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
11804 if (! comp_unit_has_inlines)
11806 add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
11807 comp_unit_has_inlines = 1;
11810 #endif
11812 /* Add the calling convention attribute if requested. */
11813 add_calling_convention_attribute (subr_die, TREE_TYPE (decl));
11817 /* Generate a DIE to represent a declared data object. */
11819 static void
11820 gen_variable_die (tree decl, dw_die_ref context_die)
11822 tree origin = decl_ultimate_origin (decl);
11823 dw_die_ref var_die = new_die (DW_TAG_variable, context_die, decl);
11825 dw_die_ref old_die = lookup_decl_die (decl);
11826 int declaration = (DECL_EXTERNAL (decl)
11827 /* If DECL is COMDAT and has not actually been
11828 emitted, we cannot take its address; there
11829 might end up being no definition anywhere in
11830 the program. For example, consider the C++
11831 test case:
11833 template <class T>
11834 struct S { static const int i = 7; };
11836 template <class T>
11837 const int S<T>::i;
11839 int f() { return S<int>::i; }
11841 Here, S<int>::i is not DECL_EXTERNAL, but no
11842 definition is required, so the compiler will
11843 not emit a definition. */
11844 || (TREE_CODE (decl) == VAR_DECL
11845 && DECL_COMDAT (decl) && !TREE_ASM_WRITTEN (decl))
11846 || class_or_namespace_scope_p (context_die));
11848 if (origin != NULL)
11849 add_abstract_origin_attribute (var_die, origin);
11851 /* Loop unrolling can create multiple blocks that refer to the same
11852 static variable, so we must test for the DW_AT_declaration flag.
11854 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
11855 copy decls and set the DECL_ABSTRACT flag on them instead of
11856 sharing them.
11858 ??? Duplicated blocks have been rewritten to use .debug_ranges.
11860 ??? The declare_in_namespace support causes us to get two DIEs for one
11861 variable, both of which are declarations. We want to avoid considering
11862 one to be a specification, so we must test that this DIE is not a
11863 declaration. */
11864 else if (old_die && TREE_STATIC (decl) && ! declaration
11865 && get_AT_flag (old_die, DW_AT_declaration) == 1)
11867 /* This is a definition of a C++ class level static. */
11868 add_AT_specification (var_die, old_die);
11869 if (DECL_NAME (decl))
11871 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11872 unsigned file_index = lookup_filename (s.file);
11874 if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
11875 add_AT_unsigned (var_die, DW_AT_decl_file, file_index);
11877 if (get_AT_unsigned (old_die, DW_AT_decl_line)
11878 != (unsigned) s.line)
11880 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
11883 else
11885 add_name_and_src_coords_attributes (var_die, decl);
11886 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
11887 TREE_THIS_VOLATILE (decl), context_die);
11889 if (TREE_PUBLIC (decl))
11890 add_AT_flag (var_die, DW_AT_external, 1);
11892 if (DECL_ARTIFICIAL (decl))
11893 add_AT_flag (var_die, DW_AT_artificial, 1);
11895 if (TREE_PROTECTED (decl))
11896 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
11897 else if (TREE_PRIVATE (decl))
11898 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
11901 if (declaration)
11902 add_AT_flag (var_die, DW_AT_declaration, 1);
11904 if (DECL_ABSTRACT (decl) || declaration)
11905 equate_decl_number_to_die (decl, var_die);
11907 if (! declaration && ! DECL_ABSTRACT (decl))
11909 add_location_or_const_value_attribute (var_die, decl, DW_AT_location);
11910 add_pubname (decl, var_die);
11912 else
11913 tree_add_const_value_attribute (var_die, decl);
11916 /* Generate a DIE to represent a label identifier. */
11918 static void
11919 gen_label_die (tree decl, dw_die_ref context_die)
11921 tree origin = decl_ultimate_origin (decl);
11922 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
11923 rtx insn;
11924 char label[MAX_ARTIFICIAL_LABEL_BYTES];
11926 if (origin != NULL)
11927 add_abstract_origin_attribute (lbl_die, origin);
11928 else
11929 add_name_and_src_coords_attributes (lbl_die, decl);
11931 if (DECL_ABSTRACT (decl))
11932 equate_decl_number_to_die (decl, lbl_die);
11933 else
11935 insn = DECL_RTL_IF_SET (decl);
11937 /* Deleted labels are programmer specified labels which have been
11938 eliminated because of various optimizations. We still emit them
11939 here so that it is possible to put breakpoints on them. */
11940 if (insn
11941 && (LABEL_P (insn)
11942 || ((NOTE_P (insn)
11943 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED_LABEL))))
11945 /* When optimization is enabled (via -O) some parts of the compiler
11946 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
11947 represent source-level labels which were explicitly declared by
11948 the user. This really shouldn't be happening though, so catch
11949 it if it ever does happen. */
11950 gcc_assert (!INSN_DELETED_P (insn));
11952 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
11953 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
11958 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
11959 attributes to the DIE for a block STMT, to describe where the inlined
11960 function was called from. This is similar to add_src_coords_attributes. */
11962 static inline void
11963 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
11965 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
11966 unsigned file_index = lookup_filename (s.file);
11968 add_AT_unsigned (die, DW_AT_call_file, file_index);
11969 add_AT_unsigned (die, DW_AT_call_line, s.line);
11972 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
11973 Add low_pc and high_pc attributes to the DIE for a block STMT. */
11975 static inline void
11976 add_high_low_attributes (tree stmt, dw_die_ref die)
11978 char label[MAX_ARTIFICIAL_LABEL_BYTES];
11980 if (BLOCK_FRAGMENT_CHAIN (stmt))
11982 tree chain;
11984 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
11986 chain = BLOCK_FRAGMENT_CHAIN (stmt);
11989 add_ranges (chain);
11990 chain = BLOCK_FRAGMENT_CHAIN (chain);
11992 while (chain);
11993 add_ranges (NULL);
11995 else
11997 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
11998 BLOCK_NUMBER (stmt));
11999 add_AT_lbl_id (die, DW_AT_low_pc, label);
12000 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
12001 BLOCK_NUMBER (stmt));
12002 add_AT_lbl_id (die, DW_AT_high_pc, label);
12006 /* Generate a DIE for a lexical block. */
12008 static void
12009 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
12011 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
12013 if (! BLOCK_ABSTRACT (stmt))
12014 add_high_low_attributes (stmt, stmt_die);
12016 decls_for_scope (stmt, stmt_die, depth);
12019 /* Generate a DIE for an inlined subprogram. */
12021 static void
12022 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
12024 tree decl = block_ultimate_origin (stmt);
12026 /* Emit info for the abstract instance first, if we haven't yet. We
12027 must emit this even if the block is abstract, otherwise when we
12028 emit the block below (or elsewhere), we may end up trying to emit
12029 a die whose origin die hasn't been emitted, and crashing. */
12030 dwarf2out_abstract_function (decl);
12032 if (! BLOCK_ABSTRACT (stmt))
12034 dw_die_ref subr_die
12035 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
12037 add_abstract_origin_attribute (subr_die, decl);
12038 add_high_low_attributes (stmt, subr_die);
12039 add_call_src_coords_attributes (stmt, subr_die);
12041 decls_for_scope (stmt, subr_die, depth);
12042 current_function_has_inlines = 1;
12044 else
12045 /* We may get here if we're the outer block of function A that was
12046 inlined into function B that was inlined into function C. When
12047 generating debugging info for C, dwarf2out_abstract_function(B)
12048 would mark all inlined blocks as abstract, including this one.
12049 So, we wouldn't (and shouldn't) expect labels to be generated
12050 for this one. Instead, just emit debugging info for
12051 declarations within the block. This is particularly important
12052 in the case of initializers of arguments passed from B to us:
12053 if they're statement expressions containing declarations, we
12054 wouldn't generate dies for their abstract variables, and then,
12055 when generating dies for the real variables, we'd die (pun
12056 intended :-) */
12057 gen_lexical_block_die (stmt, context_die, depth);
12060 /* Generate a DIE for a field in a record, or structure. */
12062 static void
12063 gen_field_die (tree decl, dw_die_ref context_die)
12065 dw_die_ref decl_die;
12067 if (TREE_TYPE (decl) == error_mark_node)
12068 return;
12070 decl_die = new_die (DW_TAG_member, context_die, decl);
12071 add_name_and_src_coords_attributes (decl_die, decl);
12072 add_type_attribute (decl_die, member_declared_type (decl),
12073 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
12074 context_die);
12076 if (DECL_BIT_FIELD_TYPE (decl))
12078 add_byte_size_attribute (decl_die, decl);
12079 add_bit_size_attribute (decl_die, decl);
12080 add_bit_offset_attribute (decl_die, decl);
12083 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
12084 add_data_member_location_attribute (decl_die, decl);
12086 if (DECL_ARTIFICIAL (decl))
12087 add_AT_flag (decl_die, DW_AT_artificial, 1);
12089 if (TREE_PROTECTED (decl))
12090 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
12091 else if (TREE_PRIVATE (decl))
12092 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
12094 /* Equate decl number to die, so that we can look up this decl later on. */
12095 equate_decl_number_to_die (decl, decl_die);
12098 #if 0
12099 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
12100 Use modified_type_die instead.
12101 We keep this code here just in case these types of DIEs may be needed to
12102 represent certain things in other languages (e.g. Pascal) someday. */
12104 static void
12105 gen_pointer_type_die (tree type, dw_die_ref context_die)
12107 dw_die_ref ptr_die
12108 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
12110 equate_type_number_to_die (type, ptr_die);
12111 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
12112 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
12115 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
12116 Use modified_type_die instead.
12117 We keep this code here just in case these types of DIEs may be needed to
12118 represent certain things in other languages (e.g. Pascal) someday. */
12120 static void
12121 gen_reference_type_die (tree type, dw_die_ref context_die)
12123 dw_die_ref ref_die
12124 = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
12126 equate_type_number_to_die (type, ref_die);
12127 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
12128 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
12130 #endif
12132 /* Generate a DIE for a pointer to a member type. */
12134 static void
12135 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
12137 dw_die_ref ptr_die
12138 = new_die (DW_TAG_ptr_to_member_type,
12139 scope_die_for (type, context_die), type);
12141 equate_type_number_to_die (type, ptr_die);
12142 add_AT_die_ref (ptr_die, DW_AT_containing_type,
12143 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
12144 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
12147 /* Generate the DIE for the compilation unit. */
12149 static dw_die_ref
12150 gen_compile_unit_die (const char *filename)
12152 dw_die_ref die;
12153 char producer[250];
12154 const char *language_string = lang_hooks.name;
12155 int language;
12157 die = new_die (DW_TAG_compile_unit, NULL, NULL);
12159 if (filename)
12161 add_name_attribute (die, filename);
12162 /* Don't add cwd for <built-in>. */
12163 if (filename[0] != DIR_SEPARATOR && filename[0] != '<')
12164 add_comp_dir_attribute (die);
12167 sprintf (producer, "%s %s", language_string, version_string);
12169 #ifdef MIPS_DEBUGGING_INFO
12170 /* The MIPS/SGI compilers place the 'cc' command line options in the producer
12171 string. The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
12172 not appear in the producer string, the debugger reaches the conclusion
12173 that the object file is stripped and has no debugging information.
12174 To get the MIPS/SGI debugger to believe that there is debugging
12175 information in the object file, we add a -g to the producer string. */
12176 if (debug_info_level > DINFO_LEVEL_TERSE)
12177 strcat (producer, " -g");
12178 #endif
12180 add_AT_string (die, DW_AT_producer, producer);
12182 if (strcmp (language_string, "GNU C++") == 0)
12183 language = DW_LANG_C_plus_plus;
12184 else if (strcmp (language_string, "GNU Ada") == 0)
12185 language = DW_LANG_Ada95;
12186 else if (strcmp (language_string, "GNU F77") == 0)
12187 language = DW_LANG_Fortran77;
12188 else if (strcmp (language_string, "GNU F95") == 0)
12189 language = DW_LANG_Fortran95;
12190 else if (strcmp (language_string, "GNU Pascal") == 0)
12191 language = DW_LANG_Pascal83;
12192 else if (strcmp (language_string, "GNU Java") == 0)
12193 language = DW_LANG_Java;
12194 else
12195 language = DW_LANG_C89;
12197 add_AT_unsigned (die, DW_AT_language, language);
12198 return die;
12201 /* Generate a DIE for a string type. */
12203 static void
12204 gen_string_type_die (tree type, dw_die_ref context_die)
12206 dw_die_ref type_die
12207 = new_die (DW_TAG_string_type, scope_die_for (type, context_die), type);
12209 equate_type_number_to_die (type, type_die);
12211 /* ??? Fudge the string length attribute for now.
12212 TODO: add string length info. */
12213 #if 0
12214 string_length_attribute (TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
12215 bound_representation (upper_bound, 0, 'u');
12216 #endif
12219 /* Generate the DIE for a base class. */
12221 static void
12222 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
12224 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
12226 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
12227 add_data_member_location_attribute (die, binfo);
12229 if (BINFO_VIRTUAL_P (binfo))
12230 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
12232 if (access == access_public_node)
12233 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
12234 else if (access == access_protected_node)
12235 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
12238 /* Generate a DIE for a class member. */
12240 static void
12241 gen_member_die (tree type, dw_die_ref context_die)
12243 tree member;
12244 tree binfo = TYPE_BINFO (type);
12245 dw_die_ref child;
12247 /* If this is not an incomplete type, output descriptions of each of its
12248 members. Note that as we output the DIEs necessary to represent the
12249 members of this record or union type, we will also be trying to output
12250 DIEs to represent the *types* of those members. However the `type'
12251 function (above) will specifically avoid generating type DIEs for member
12252 types *within* the list of member DIEs for this (containing) type except
12253 for those types (of members) which are explicitly marked as also being
12254 members of this (containing) type themselves. The g++ front- end can
12255 force any given type to be treated as a member of some other (containing)
12256 type by setting the TYPE_CONTEXT of the given (member) type to point to
12257 the TREE node representing the appropriate (containing) type. */
12259 /* First output info about the base classes. */
12260 if (binfo)
12262 VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
12263 int i;
12264 tree base;
12266 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
12267 gen_inheritance_die (base,
12268 (accesses ? VEC_index (tree, accesses, i)
12269 : access_public_node), context_die);
12272 /* Now output info about the data members and type members. */
12273 for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
12275 /* If we thought we were generating minimal debug info for TYPE
12276 and then changed our minds, some of the member declarations
12277 may have already been defined. Don't define them again, but
12278 do put them in the right order. */
12280 child = lookup_decl_die (member);
12281 if (child)
12282 splice_child_die (context_die, child);
12283 else
12284 gen_decl_die (member, context_die);
12287 /* Now output info about the function members (if any). */
12288 for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
12290 /* Don't include clones in the member list. */
12291 if (DECL_ABSTRACT_ORIGIN (member))
12292 continue;
12294 child = lookup_decl_die (member);
12295 if (child)
12296 splice_child_die (context_die, child);
12297 else
12298 gen_decl_die (member, context_die);
12302 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
12303 is set, we pretend that the type was never defined, so we only get the
12304 member DIEs needed by later specification DIEs. */
12306 static void
12307 gen_struct_or_union_type_die (tree type, dw_die_ref context_die)
12309 dw_die_ref type_die = lookup_type_die (type);
12310 dw_die_ref scope_die = 0;
12311 int nested = 0;
12312 int complete = (TYPE_SIZE (type)
12313 && (! TYPE_STUB_DECL (type)
12314 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
12315 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
12317 if (type_die && ! complete)
12318 return;
12320 if (TYPE_CONTEXT (type) != NULL_TREE
12321 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
12322 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
12323 nested = 1;
12325 scope_die = scope_die_for (type, context_die);
12327 if (! type_die || (nested && scope_die == comp_unit_die))
12328 /* First occurrence of type or toplevel definition of nested class. */
12330 dw_die_ref old_die = type_die;
12332 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
12333 ? DW_TAG_structure_type : DW_TAG_union_type,
12334 scope_die, type);
12335 equate_type_number_to_die (type, type_die);
12336 if (old_die)
12337 add_AT_specification (type_die, old_die);
12338 else
12339 add_name_attribute (type_die, type_tag (type));
12341 else
12342 remove_AT (type_die, DW_AT_declaration);
12344 /* If this type has been completed, then give it a byte_size attribute and
12345 then give a list of members. */
12346 if (complete && !ns_decl)
12348 /* Prevent infinite recursion in cases where the type of some member of
12349 this type is expressed in terms of this type itself. */
12350 TREE_ASM_WRITTEN (type) = 1;
12351 add_byte_size_attribute (type_die, type);
12352 if (TYPE_STUB_DECL (type) != NULL_TREE)
12353 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
12355 /* If the first reference to this type was as the return type of an
12356 inline function, then it may not have a parent. Fix this now. */
12357 if (type_die->die_parent == NULL)
12358 add_child_die (scope_die, type_die);
12360 push_decl_scope (type);
12361 gen_member_die (type, type_die);
12362 pop_decl_scope ();
12364 /* GNU extension: Record what type our vtable lives in. */
12365 if (TYPE_VFIELD (type))
12367 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
12369 gen_type_die (vtype, context_die);
12370 add_AT_die_ref (type_die, DW_AT_containing_type,
12371 lookup_type_die (vtype));
12374 else
12376 add_AT_flag (type_die, DW_AT_declaration, 1);
12378 /* We don't need to do this for function-local types. */
12379 if (TYPE_STUB_DECL (type)
12380 && ! decl_function_context (TYPE_STUB_DECL (type)))
12381 VEC_safe_push (tree, gc, incomplete_types, type);
12385 /* Generate a DIE for a subroutine _type_. */
12387 static void
12388 gen_subroutine_type_die (tree type, dw_die_ref context_die)
12390 tree return_type = TREE_TYPE (type);
12391 dw_die_ref subr_die
12392 = new_die (DW_TAG_subroutine_type,
12393 scope_die_for (type, context_die), type);
12395 equate_type_number_to_die (type, subr_die);
12396 add_prototyped_attribute (subr_die, type);
12397 add_type_attribute (subr_die, return_type, 0, 0, context_die);
12398 gen_formal_types_die (type, subr_die);
12401 /* Generate a DIE for a type definition. */
12403 static void
12404 gen_typedef_die (tree decl, dw_die_ref context_die)
12406 dw_die_ref type_die;
12407 tree origin;
12409 if (TREE_ASM_WRITTEN (decl))
12410 return;
12412 TREE_ASM_WRITTEN (decl) = 1;
12413 type_die = new_die (DW_TAG_typedef, context_die, decl);
12414 origin = decl_ultimate_origin (decl);
12415 if (origin != NULL)
12416 add_abstract_origin_attribute (type_die, origin);
12417 else
12419 tree type;
12421 add_name_and_src_coords_attributes (type_die, decl);
12422 if (DECL_ORIGINAL_TYPE (decl))
12424 type = DECL_ORIGINAL_TYPE (decl);
12426 gcc_assert (type != TREE_TYPE (decl));
12427 equate_type_number_to_die (TREE_TYPE (decl), type_die);
12429 else
12430 type = TREE_TYPE (decl);
12432 add_type_attribute (type_die, type, TREE_READONLY (decl),
12433 TREE_THIS_VOLATILE (decl), context_die);
12436 if (DECL_ABSTRACT (decl))
12437 equate_decl_number_to_die (decl, type_die);
12440 /* Generate a type description DIE. */
12442 static void
12443 gen_type_die (tree type, dw_die_ref context_die)
12445 int need_pop;
12447 if (type == NULL_TREE || type == error_mark_node)
12448 return;
12450 if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12451 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
12453 if (TREE_ASM_WRITTEN (type))
12454 return;
12456 /* Prevent broken recursion; we can't hand off to the same type. */
12457 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
12459 TREE_ASM_WRITTEN (type) = 1;
12460 gen_decl_die (TYPE_NAME (type), context_die);
12461 return;
12464 /* We are going to output a DIE to represent the unqualified version
12465 of this type (i.e. without any const or volatile qualifiers) so
12466 get the main variant (i.e. the unqualified version) of this type
12467 now. (Vectors are special because the debugging info is in the
12468 cloned type itself). */
12469 if (TREE_CODE (type) != VECTOR_TYPE)
12470 type = type_main_variant (type);
12472 if (TREE_ASM_WRITTEN (type))
12473 return;
12475 switch (TREE_CODE (type))
12477 case ERROR_MARK:
12478 break;
12480 case POINTER_TYPE:
12481 case REFERENCE_TYPE:
12482 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
12483 ensures that the gen_type_die recursion will terminate even if the
12484 type is recursive. Recursive types are possible in Ada. */
12485 /* ??? We could perhaps do this for all types before the switch
12486 statement. */
12487 TREE_ASM_WRITTEN (type) = 1;
12489 /* For these types, all that is required is that we output a DIE (or a
12490 set of DIEs) to represent the "basis" type. */
12491 gen_type_die (TREE_TYPE (type), context_die);
12492 break;
12494 case OFFSET_TYPE:
12495 /* This code is used for C++ pointer-to-data-member types.
12496 Output a description of the relevant class type. */
12497 gen_type_die (TYPE_OFFSET_BASETYPE (type), context_die);
12499 /* Output a description of the type of the object pointed to. */
12500 gen_type_die (TREE_TYPE (type), context_die);
12502 /* Now output a DIE to represent this pointer-to-data-member type
12503 itself. */
12504 gen_ptr_to_mbr_type_die (type, context_die);
12505 break;
12507 case FUNCTION_TYPE:
12508 /* Force out return type (in case it wasn't forced out already). */
12509 gen_type_die (TREE_TYPE (type), context_die);
12510 gen_subroutine_type_die (type, context_die);
12511 break;
12513 case METHOD_TYPE:
12514 /* Force out return type (in case it wasn't forced out already). */
12515 gen_type_die (TREE_TYPE (type), context_die);
12516 gen_subroutine_type_die (type, context_die);
12517 break;
12519 case ARRAY_TYPE:
12520 if (TYPE_STRING_FLAG (type) && TREE_CODE (TREE_TYPE (type)) == CHAR_TYPE)
12522 gen_type_die (TREE_TYPE (type), context_die);
12523 gen_string_type_die (type, context_die);
12525 else
12526 gen_array_type_die (type, context_die);
12527 break;
12529 case VECTOR_TYPE:
12530 gen_array_type_die (type, context_die);
12531 break;
12533 case ENUMERAL_TYPE:
12534 case RECORD_TYPE:
12535 case UNION_TYPE:
12536 case QUAL_UNION_TYPE:
12537 /* If this is a nested type whose containing class hasn't been written
12538 out yet, writing it out will cover this one, too. This does not apply
12539 to instantiations of member class templates; they need to be added to
12540 the containing class as they are generated. FIXME: This hurts the
12541 idea of combining type decls from multiple TUs, since we can't predict
12542 what set of template instantiations we'll get. */
12543 if (TYPE_CONTEXT (type)
12544 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
12545 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
12547 gen_type_die (TYPE_CONTEXT (type), context_die);
12549 if (TREE_ASM_WRITTEN (type))
12550 return;
12552 /* If that failed, attach ourselves to the stub. */
12553 push_decl_scope (TYPE_CONTEXT (type));
12554 context_die = lookup_type_die (TYPE_CONTEXT (type));
12555 need_pop = 1;
12557 else
12559 declare_in_namespace (type, context_die);
12560 need_pop = 0;
12563 if (TREE_CODE (type) == ENUMERAL_TYPE)
12564 gen_enumeration_type_die (type, context_die);
12565 else
12566 gen_struct_or_union_type_die (type, context_die);
12568 if (need_pop)
12569 pop_decl_scope ();
12571 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
12572 it up if it is ever completed. gen_*_type_die will set it for us
12573 when appropriate. */
12574 return;
12576 case VOID_TYPE:
12577 case INTEGER_TYPE:
12578 case REAL_TYPE:
12579 case COMPLEX_TYPE:
12580 case BOOLEAN_TYPE:
12581 case CHAR_TYPE:
12582 /* No DIEs needed for fundamental types. */
12583 break;
12585 case LANG_TYPE:
12586 /* No Dwarf representation currently defined. */
12587 break;
12589 default:
12590 gcc_unreachable ();
12593 TREE_ASM_WRITTEN (type) = 1;
12596 /* Generate a DIE for a tagged type instantiation. */
12598 static void
12599 gen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
12601 if (type == NULL_TREE || type == error_mark_node)
12602 return;
12604 /* We are going to output a DIE to represent the unqualified version of
12605 this type (i.e. without any const or volatile qualifiers) so make sure
12606 that we have the main variant (i.e. the unqualified version) of this
12607 type now. */
12608 gcc_assert (type == type_main_variant (type));
12610 /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
12611 an instance of an unresolved type. */
12613 switch (TREE_CODE (type))
12615 case ERROR_MARK:
12616 break;
12618 case ENUMERAL_TYPE:
12619 gen_inlined_enumeration_type_die (type, context_die);
12620 break;
12622 case RECORD_TYPE:
12623 gen_inlined_structure_type_die (type, context_die);
12624 break;
12626 case UNION_TYPE:
12627 case QUAL_UNION_TYPE:
12628 gen_inlined_union_type_die (type, context_die);
12629 break;
12631 default:
12632 gcc_unreachable ();
12636 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
12637 things which are local to the given block. */
12639 static void
12640 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
12642 int must_output_die = 0;
12643 tree origin;
12644 tree decl;
12645 enum tree_code origin_code;
12647 /* Ignore blocks that are NULL. */
12648 if (stmt == NULL_TREE)
12649 return;
12651 /* If the block is one fragment of a non-contiguous block, do not
12652 process the variables, since they will have been done by the
12653 origin block. Do process subblocks. */
12654 if (BLOCK_FRAGMENT_ORIGIN (stmt))
12656 tree sub;
12658 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
12659 gen_block_die (sub, context_die, depth + 1);
12661 return;
12664 /* Determine the "ultimate origin" of this block. This block may be an
12665 inlined instance of an inlined instance of inline function, so we have
12666 to trace all of the way back through the origin chain to find out what
12667 sort of node actually served as the original seed for the creation of
12668 the current block. */
12669 origin = block_ultimate_origin (stmt);
12670 origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
12672 /* Determine if we need to output any Dwarf DIEs at all to represent this
12673 block. */
12674 if (origin_code == FUNCTION_DECL)
12675 /* The outer scopes for inlinings *must* always be represented. We
12676 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
12677 must_output_die = 1;
12678 else
12680 /* In the case where the current block represents an inlining of the
12681 "body block" of an inline function, we must *NOT* output any DIE for
12682 this block because we have already output a DIE to represent the whole
12683 inlined function scope and the "body block" of any function doesn't
12684 really represent a different scope according to ANSI C rules. So we
12685 check here to make sure that this block does not represent a "body
12686 block inlining" before trying to set the MUST_OUTPUT_DIE flag. */
12687 if (! is_body_block (origin ? origin : stmt))
12689 /* Determine if this block directly contains any "significant"
12690 local declarations which we will need to output DIEs for. */
12691 if (debug_info_level > DINFO_LEVEL_TERSE)
12692 /* We are not in terse mode so *any* local declaration counts
12693 as being a "significant" one. */
12694 must_output_die = (BLOCK_VARS (stmt) != NULL
12695 && (TREE_USED (stmt)
12696 || TREE_ASM_WRITTEN (stmt)
12697 || BLOCK_ABSTRACT (stmt)));
12698 else
12699 /* We are in terse mode, so only local (nested) function
12700 definitions count as "significant" local declarations. */
12701 for (decl = BLOCK_VARS (stmt);
12702 decl != NULL; decl = TREE_CHAIN (decl))
12703 if (TREE_CODE (decl) == FUNCTION_DECL
12704 && DECL_INITIAL (decl))
12706 must_output_die = 1;
12707 break;
12712 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
12713 DIE for any block which contains no significant local declarations at
12714 all. Rather, in such cases we just call `decls_for_scope' so that any
12715 needed Dwarf info for any sub-blocks will get properly generated. Note
12716 that in terse mode, our definition of what constitutes a "significant"
12717 local declaration gets restricted to include only inlined function
12718 instances and local (nested) function definitions. */
12719 if (must_output_die)
12721 if (origin_code == FUNCTION_DECL)
12722 gen_inlined_subroutine_die (stmt, context_die, depth);
12723 else
12724 gen_lexical_block_die (stmt, context_die, depth);
12726 else
12727 decls_for_scope (stmt, context_die, depth);
12730 /* Generate all of the decls declared within a given scope and (recursively)
12731 all of its sub-blocks. */
12733 static void
12734 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
12736 tree decl;
12737 tree subblocks;
12739 /* Ignore NULL blocks. */
12740 if (stmt == NULL_TREE)
12741 return;
12743 if (TREE_USED (stmt))
12745 /* Output the DIEs to represent all of the data objects and typedefs
12746 declared directly within this block but not within any nested
12747 sub-blocks. Also, nested function and tag DIEs have been
12748 generated with a parent of NULL; fix that up now. */
12749 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
12751 dw_die_ref die;
12753 if (TREE_CODE (decl) == FUNCTION_DECL)
12754 die = lookup_decl_die (decl);
12755 else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
12756 die = lookup_type_die (TREE_TYPE (decl));
12757 else
12758 die = NULL;
12760 if (die != NULL && die->die_parent == NULL)
12761 add_child_die (context_die, die);
12762 /* Do not produce debug information for static variables since
12763 these might be optimized out. We are called for these later
12764 in cgraph_varpool_analyze_pending_decls. */
12765 if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
12767 else
12768 gen_decl_die (decl, context_die);
12772 /* If we're at -g1, we're not interested in subblocks. */
12773 if (debug_info_level <= DINFO_LEVEL_TERSE)
12774 return;
12776 /* Output the DIEs to represent all sub-blocks (and the items declared
12777 therein) of this block. */
12778 for (subblocks = BLOCK_SUBBLOCKS (stmt);
12779 subblocks != NULL;
12780 subblocks = BLOCK_CHAIN (subblocks))
12781 gen_block_die (subblocks, context_die, depth + 1);
12784 /* Is this a typedef we can avoid emitting? */
12786 static inline int
12787 is_redundant_typedef (tree decl)
12789 if (TYPE_DECL_IS_STUB (decl))
12790 return 1;
12792 if (DECL_ARTIFICIAL (decl)
12793 && DECL_CONTEXT (decl)
12794 && is_tagged_type (DECL_CONTEXT (decl))
12795 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
12796 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
12797 /* Also ignore the artificial member typedef for the class name. */
12798 return 1;
12800 return 0;
12803 /* Returns the DIE for decl. A DIE will always be returned. */
12805 static dw_die_ref
12806 force_decl_die (tree decl)
12808 dw_die_ref decl_die;
12809 unsigned saved_external_flag;
12810 tree save_fn = NULL_TREE;
12811 decl_die = lookup_decl_die (decl);
12812 if (!decl_die)
12814 dw_die_ref context_die;
12815 tree decl_context = DECL_CONTEXT (decl);
12816 if (decl_context)
12818 /* Find die that represents this context. */
12819 if (TYPE_P (decl_context))
12820 context_die = force_type_die (decl_context);
12821 else
12822 context_die = force_decl_die (decl_context);
12824 else
12825 context_die = comp_unit_die;
12827 decl_die = lookup_decl_die (decl);
12828 if (decl_die)
12829 return decl_die;
12831 switch (TREE_CODE (decl))
12833 case FUNCTION_DECL:
12834 /* Clear current_function_decl, so that gen_subprogram_die thinks
12835 that this is a declaration. At this point, we just want to force
12836 declaration die. */
12837 save_fn = current_function_decl;
12838 current_function_decl = NULL_TREE;
12839 gen_subprogram_die (decl, context_die);
12840 current_function_decl = save_fn;
12841 break;
12843 case VAR_DECL:
12844 /* Set external flag to force declaration die. Restore it after
12845 gen_decl_die() call. */
12846 saved_external_flag = DECL_EXTERNAL (decl);
12847 DECL_EXTERNAL (decl) = 1;
12848 gen_decl_die (decl, context_die);
12849 DECL_EXTERNAL (decl) = saved_external_flag;
12850 break;
12852 case NAMESPACE_DECL:
12853 dwarf2out_decl (decl);
12854 break;
12856 default:
12857 gcc_unreachable ();
12860 /* We should be able to find the DIE now. */
12861 if (!decl_die)
12862 decl_die = lookup_decl_die (decl);
12863 gcc_assert (decl_die);
12866 return decl_die;
12869 /* Returns the DIE for TYPE. A DIE is always returned. */
12871 static dw_die_ref
12872 force_type_die (tree type)
12874 dw_die_ref type_die;
12876 type_die = lookup_type_die (type);
12877 if (!type_die)
12879 dw_die_ref context_die;
12880 if (TYPE_CONTEXT (type))
12882 if (TYPE_P (TYPE_CONTEXT (type)))
12883 context_die = force_type_die (TYPE_CONTEXT (type));
12884 else
12885 context_die = force_decl_die (TYPE_CONTEXT (type));
12887 else
12888 context_die = comp_unit_die;
12890 type_die = lookup_type_die (type);
12891 if (type_die)
12892 return type_die;
12893 gen_type_die (type, context_die);
12894 type_die = lookup_type_die (type);
12895 gcc_assert (type_die);
12897 return type_die;
12900 /* Force out any required namespaces to be able to output DECL,
12901 and return the new context_die for it, if it's changed. */
12903 static dw_die_ref
12904 setup_namespace_context (tree thing, dw_die_ref context_die)
12906 tree context = (DECL_P (thing)
12907 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
12908 if (context && TREE_CODE (context) == NAMESPACE_DECL)
12909 /* Force out the namespace. */
12910 context_die = force_decl_die (context);
12912 return context_die;
12915 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
12916 type) within its namespace, if appropriate.
12918 For compatibility with older debuggers, namespace DIEs only contain
12919 declarations; all definitions are emitted at CU scope. */
12921 static void
12922 declare_in_namespace (tree thing, dw_die_ref context_die)
12924 dw_die_ref ns_context;
12926 if (debug_info_level <= DINFO_LEVEL_TERSE)
12927 return;
12929 /* If this decl is from an inlined function, then don't try to emit it in its
12930 namespace, as we will get confused. It would have already been emitted
12931 when the abstract instance of the inline function was emitted anyways. */
12932 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
12933 return;
12935 ns_context = setup_namespace_context (thing, context_die);
12937 if (ns_context != context_die)
12939 if (DECL_P (thing))
12940 gen_decl_die (thing, ns_context);
12941 else
12942 gen_type_die (thing, ns_context);
12946 /* Generate a DIE for a namespace or namespace alias. */
12948 static void
12949 gen_namespace_die (tree decl)
12951 dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
12953 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
12954 they are an alias of. */
12955 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
12957 /* Output a real namespace. */
12958 dw_die_ref namespace_die
12959 = new_die (DW_TAG_namespace, context_die, decl);
12960 add_name_and_src_coords_attributes (namespace_die, decl);
12961 equate_decl_number_to_die (decl, namespace_die);
12963 else
12965 /* Output a namespace alias. */
12967 /* Force out the namespace we are an alias of, if necessary. */
12968 dw_die_ref origin_die
12969 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
12971 /* Now create the namespace alias DIE. */
12972 dw_die_ref namespace_die
12973 = new_die (DW_TAG_imported_declaration, context_die, decl);
12974 add_name_and_src_coords_attributes (namespace_die, decl);
12975 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
12976 equate_decl_number_to_die (decl, namespace_die);
12980 /* Generate Dwarf debug information for a decl described by DECL. */
12982 static void
12983 gen_decl_die (tree decl, dw_die_ref context_die)
12985 tree origin;
12987 if (DECL_P (decl) && DECL_IGNORED_P (decl))
12988 return;
12990 switch (TREE_CODE (decl))
12992 case ERROR_MARK:
12993 break;
12995 case CONST_DECL:
12996 /* The individual enumerators of an enum type get output when we output
12997 the Dwarf representation of the relevant enum type itself. */
12998 break;
13000 case FUNCTION_DECL:
13001 /* Don't output any DIEs to represent mere function declarations,
13002 unless they are class members or explicit block externs. */
13003 if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
13004 && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
13005 break;
13007 #if 0
13008 /* FIXME */
13009 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
13010 on local redeclarations of global functions. That seems broken. */
13011 if (current_function_decl != decl)
13012 /* This is only a declaration. */;
13013 #endif
13015 /* If we're emitting a clone, emit info for the abstract instance. */
13016 if (DECL_ORIGIN (decl) != decl)
13017 dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
13019 /* If we're emitting an out-of-line copy of an inline function,
13020 emit info for the abstract instance and set up to refer to it. */
13021 else if (cgraph_function_possibly_inlined_p (decl)
13022 && ! DECL_ABSTRACT (decl)
13023 && ! class_or_namespace_scope_p (context_die)
13024 /* dwarf2out_abstract_function won't emit a die if this is just
13025 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
13026 that case, because that works only if we have a die. */
13027 && DECL_INITIAL (decl) != NULL_TREE)
13029 dwarf2out_abstract_function (decl);
13030 set_decl_origin_self (decl);
13033 /* Otherwise we're emitting the primary DIE for this decl. */
13034 else if (debug_info_level > DINFO_LEVEL_TERSE)
13036 /* Before we describe the FUNCTION_DECL itself, make sure that we
13037 have described its return type. */
13038 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
13040 /* And its virtual context. */
13041 if (DECL_VINDEX (decl) != NULL_TREE)
13042 gen_type_die (DECL_CONTEXT (decl), context_die);
13044 /* And its containing type. */
13045 origin = decl_class_context (decl);
13046 if (origin != NULL_TREE)
13047 gen_type_die_for_member (origin, decl, context_die);
13049 /* And its containing namespace. */
13050 declare_in_namespace (decl, context_die);
13053 /* Now output a DIE to represent the function itself. */
13054 gen_subprogram_die (decl, context_die);
13055 break;
13057 case TYPE_DECL:
13058 /* If we are in terse mode, don't generate any DIEs to represent any
13059 actual typedefs. */
13060 if (debug_info_level <= DINFO_LEVEL_TERSE)
13061 break;
13063 /* In the special case of a TYPE_DECL node representing the declaration
13064 of some type tag, if the given TYPE_DECL is marked as having been
13065 instantiated from some other (original) TYPE_DECL node (e.g. one which
13066 was generated within the original definition of an inline function) we
13067 have to generate a special (abbreviated) DW_TAG_structure_type,
13068 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. */
13069 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
13071 gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
13072 break;
13075 if (is_redundant_typedef (decl))
13076 gen_type_die (TREE_TYPE (decl), context_die);
13077 else
13078 /* Output a DIE to represent the typedef itself. */
13079 gen_typedef_die (decl, context_die);
13080 break;
13082 case LABEL_DECL:
13083 if (debug_info_level >= DINFO_LEVEL_NORMAL)
13084 gen_label_die (decl, context_die);
13085 break;
13087 case VAR_DECL:
13088 case RESULT_DECL:
13089 /* If we are in terse mode, don't generate any DIEs to represent any
13090 variable declarations or definitions. */
13091 if (debug_info_level <= DINFO_LEVEL_TERSE)
13092 break;
13094 /* Output any DIEs that are needed to specify the type of this data
13095 object. */
13096 gen_type_die (TREE_TYPE (decl), context_die);
13098 /* And its containing type. */
13099 origin = decl_class_context (decl);
13100 if (origin != NULL_TREE)
13101 gen_type_die_for_member (origin, decl, context_die);
13103 /* And its containing namespace. */
13104 declare_in_namespace (decl, context_die);
13106 /* Now output the DIE to represent the data object itself. This gets
13107 complicated because of the possibility that the VAR_DECL really
13108 represents an inlined instance of a formal parameter for an inline
13109 function. */
13110 origin = decl_ultimate_origin (decl);
13111 if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
13112 gen_formal_parameter_die (decl, context_die);
13113 else
13114 gen_variable_die (decl, context_die);
13115 break;
13117 case FIELD_DECL:
13118 /* Ignore the nameless fields that are used to skip bits but handle C++
13119 anonymous unions and structs. */
13120 if (DECL_NAME (decl) != NULL_TREE
13121 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
13122 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
13124 gen_type_die (member_declared_type (decl), context_die);
13125 gen_field_die (decl, context_die);
13127 break;
13129 case PARM_DECL:
13130 gen_type_die (TREE_TYPE (decl), context_die);
13131 gen_formal_parameter_die (decl, context_die);
13132 break;
13134 case NAMESPACE_DECL:
13135 gen_namespace_die (decl);
13136 break;
13138 default:
13139 /* Probably some frontend-internal decl. Assume we don't care. */
13140 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
13141 break;
13145 /* Add Ada "use" clause information for SGI Workshop debugger. */
13147 void
13148 dwarf2out_add_library_unit_info (const char *filename, const char *context_list)
13150 unsigned int file_index;
13152 if (filename != NULL)
13154 dw_die_ref unit_die = new_die (DW_TAG_module, comp_unit_die, NULL);
13155 tree context_list_decl
13156 = build_decl (LABEL_DECL, get_identifier (context_list),
13157 void_type_node);
13159 TREE_PUBLIC (context_list_decl) = TRUE;
13160 add_name_attribute (unit_die, context_list);
13161 file_index = lookup_filename (filename);
13162 add_AT_unsigned (unit_die, DW_AT_decl_file, file_index);
13163 add_pubname (context_list_decl, unit_die);
13167 /* Output debug information for global decl DECL. Called from toplev.c after
13168 compilation proper has finished. */
13170 static void
13171 dwarf2out_global_decl (tree decl)
13173 /* Output DWARF2 information for file-scope tentative data object
13174 declarations, file-scope (extern) function declarations (which had no
13175 corresponding body) and file-scope tagged type declarations and
13176 definitions which have not yet been forced out. */
13177 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
13178 dwarf2out_decl (decl);
13181 /* Output debug information for type decl DECL. Called from toplev.c
13182 and from language front ends (to record built-in types). */
13183 static void
13184 dwarf2out_type_decl (tree decl, int local)
13186 if (!local)
13187 dwarf2out_decl (decl);
13190 /* Output debug information for imported module or decl. */
13192 static void
13193 dwarf2out_imported_module_or_decl (tree decl, tree context)
13195 dw_die_ref imported_die, at_import_die;
13196 dw_die_ref scope_die;
13197 unsigned file_index;
13198 expanded_location xloc;
13200 if (debug_info_level <= DINFO_LEVEL_TERSE)
13201 return;
13203 gcc_assert (decl);
13205 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
13206 We need decl DIE for reference and scope die. First, get DIE for the decl
13207 itself. */
13209 /* Get the scope die for decl context. Use comp_unit_die for global module
13210 or decl. If die is not found for non globals, force new die. */
13211 if (!context)
13212 scope_die = comp_unit_die;
13213 else if (TYPE_P (context))
13214 scope_die = force_type_die (context);
13215 else
13216 scope_die = force_decl_die (context);
13218 /* For TYPE_DECL or CONST_DECL, lookup TREE_TYPE. */
13219 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
13220 at_import_die = force_type_die (TREE_TYPE (decl));
13221 else
13223 at_import_die = lookup_decl_die (decl);
13224 if (!at_import_die)
13226 /* If we're trying to avoid duplicate debug info, we may not have
13227 emitted the member decl for this field. Emit it now. */
13228 if (TREE_CODE (decl) == FIELD_DECL)
13230 tree type = DECL_CONTEXT (decl);
13231 dw_die_ref type_context_die;
13233 if (TYPE_CONTEXT (type))
13234 if (TYPE_P (TYPE_CONTEXT (type)))
13235 type_context_die = force_type_die (TYPE_CONTEXT (type));
13236 else
13237 type_context_die = force_decl_die (TYPE_CONTEXT (type));
13238 else
13239 type_context_die = comp_unit_die;
13240 gen_type_die_for_member (type, decl, type_context_die);
13242 at_import_die = force_decl_die (decl);
13246 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
13247 if (TREE_CODE (decl) == NAMESPACE_DECL)
13248 imported_die = new_die (DW_TAG_imported_module, scope_die, context);
13249 else
13250 imported_die = new_die (DW_TAG_imported_declaration, scope_die, context);
13252 xloc = expand_location (input_location);
13253 file_index = lookup_filename (xloc.file);
13254 add_AT_unsigned (imported_die, DW_AT_decl_file, file_index);
13255 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
13256 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
13259 /* Write the debugging output for DECL. */
13261 void
13262 dwarf2out_decl (tree decl)
13264 dw_die_ref context_die = comp_unit_die;
13266 switch (TREE_CODE (decl))
13268 case ERROR_MARK:
13269 return;
13271 case FUNCTION_DECL:
13272 /* What we would really like to do here is to filter out all mere
13273 file-scope declarations of file-scope functions which are never
13274 referenced later within this translation unit (and keep all of ones
13275 that *are* referenced later on) but we aren't clairvoyant, so we have
13276 no idea which functions will be referenced in the future (i.e. later
13277 on within the current translation unit). So here we just ignore all
13278 file-scope function declarations which are not also definitions. If
13279 and when the debugger needs to know something about these functions,
13280 it will have to hunt around and find the DWARF information associated
13281 with the definition of the function.
13283 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
13284 nodes represent definitions and which ones represent mere
13285 declarations. We have to check DECL_INITIAL instead. That's because
13286 the C front-end supports some weird semantics for "extern inline"
13287 function definitions. These can get inlined within the current
13288 translation unit (and thus, we need to generate Dwarf info for their
13289 abstract instances so that the Dwarf info for the concrete inlined
13290 instances can have something to refer to) but the compiler never
13291 generates any out-of-lines instances of such things (despite the fact
13292 that they *are* definitions).
13294 The important point is that the C front-end marks these "extern
13295 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
13296 them anyway. Note that the C++ front-end also plays some similar games
13297 for inline function definitions appearing within include files which
13298 also contain `#pragma interface' pragmas. */
13299 if (DECL_INITIAL (decl) == NULL_TREE)
13300 return;
13302 /* If we're a nested function, initially use a parent of NULL; if we're
13303 a plain function, this will be fixed up in decls_for_scope. If
13304 we're a method, it will be ignored, since we already have a DIE. */
13305 if (decl_function_context (decl)
13306 /* But if we're in terse mode, we don't care about scope. */
13307 && debug_info_level > DINFO_LEVEL_TERSE)
13308 context_die = NULL;
13309 break;
13311 case VAR_DECL:
13312 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
13313 declaration and if the declaration was never even referenced from
13314 within this entire compilation unit. We suppress these DIEs in
13315 order to save space in the .debug section (by eliminating entries
13316 which are probably useless). Note that we must not suppress
13317 block-local extern declarations (whether used or not) because that
13318 would screw-up the debugger's name lookup mechanism and cause it to
13319 miss things which really ought to be in scope at a given point. */
13320 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
13321 return;
13323 /* For local statics lookup proper context die. */
13324 if (TREE_STATIC (decl) && decl_function_context (decl))
13325 context_die = lookup_decl_die (DECL_CONTEXT (decl));
13327 /* If we are in terse mode, don't generate any DIEs to represent any
13328 variable declarations or definitions. */
13329 if (debug_info_level <= DINFO_LEVEL_TERSE)
13330 return;
13331 break;
13333 case NAMESPACE_DECL:
13334 if (debug_info_level <= DINFO_LEVEL_TERSE)
13335 return;
13336 if (lookup_decl_die (decl) != NULL)
13337 return;
13338 break;
13340 case TYPE_DECL:
13341 /* Don't emit stubs for types unless they are needed by other DIEs. */
13342 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
13343 return;
13345 /* Don't bother trying to generate any DIEs to represent any of the
13346 normal built-in types for the language we are compiling. */
13347 if (DECL_IS_BUILTIN (decl))
13349 /* OK, we need to generate one for `bool' so GDB knows what type
13350 comparisons have. */
13351 if ((get_AT_unsigned (comp_unit_die, DW_AT_language)
13352 == DW_LANG_C_plus_plus)
13353 && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
13354 && ! DECL_IGNORED_P (decl))
13355 modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
13357 return;
13360 /* If we are in terse mode, don't generate any DIEs for types. */
13361 if (debug_info_level <= DINFO_LEVEL_TERSE)
13362 return;
13364 /* If we're a function-scope tag, initially use a parent of NULL;
13365 this will be fixed up in decls_for_scope. */
13366 if (decl_function_context (decl))
13367 context_die = NULL;
13369 break;
13371 default:
13372 return;
13375 gen_decl_die (decl, context_die);
13378 /* Output a marker (i.e. a label) for the beginning of the generated code for
13379 a lexical block. */
13381 static void
13382 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
13383 unsigned int blocknum)
13385 current_function_section (current_function_decl);
13386 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
13389 /* Output a marker (i.e. a label) for the end of the generated code for a
13390 lexical block. */
13392 static void
13393 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
13395 current_function_section (current_function_decl);
13396 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
13399 /* Returns nonzero if it is appropriate not to emit any debugging
13400 information for BLOCK, because it doesn't contain any instructions.
13402 Don't allow this for blocks with nested functions or local classes
13403 as we would end up with orphans, and in the presence of scheduling
13404 we may end up calling them anyway. */
13406 static bool
13407 dwarf2out_ignore_block (tree block)
13409 tree decl;
13411 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
13412 if (TREE_CODE (decl) == FUNCTION_DECL
13413 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
13414 return 0;
13416 return 1;
13419 /* Lookup FILE_NAME (in the list of filenames that we know about here in
13420 dwarf2out.c) and return its "index". The index of each (known) filename is
13421 just a unique number which is associated with only that one filename. We
13422 need such numbers for the sake of generating labels (in the .debug_sfnames
13423 section) and references to those files numbers (in the .debug_srcinfo
13424 and.debug_macinfo sections). If the filename given as an argument is not
13425 found in our current list, add it to the list and assign it the next
13426 available unique index number. In order to speed up searches, we remember
13427 the index of the filename was looked up last. This handles the majority of
13428 all searches. */
13430 static unsigned
13431 lookup_filename (const char *file_name)
13433 size_t i, n;
13434 char *save_file_name;
13436 /* Check to see if the file name that was searched on the previous
13437 call matches this file name. If so, return the index. */
13438 if (file_table_last_lookup_index != 0)
13440 const char *last
13441 = VARRAY_CHAR_PTR (file_table, file_table_last_lookup_index);
13442 if (strcmp (file_name, last) == 0)
13443 return file_table_last_lookup_index;
13446 /* Didn't match the previous lookup, search the table. */
13447 n = VARRAY_ACTIVE_SIZE (file_table);
13448 for (i = 1; i < n; i++)
13449 if (strcmp (file_name, VARRAY_CHAR_PTR (file_table, i)) == 0)
13451 file_table_last_lookup_index = i;
13452 return i;
13455 /* Add the new entry to the end of the filename table. */
13456 file_table_last_lookup_index = n;
13457 save_file_name = (char *) ggc_strdup (file_name);
13458 VARRAY_PUSH_CHAR_PTR (file_table, save_file_name);
13459 VARRAY_PUSH_UINT (file_table_emitted, 0);
13461 /* If the assembler is emitting the file table, and we aren't eliminating
13462 unused debug types, then we must emit .file here. If we are eliminating
13463 unused debug types, then this will be done by the maybe_emit_file call in
13464 prune_unused_types_walk_attribs. */
13466 if (DWARF2_ASM_LINE_DEBUG_INFO && ! flag_eliminate_unused_debug_types)
13467 return maybe_emit_file (i);
13469 return i;
13472 /* If the assembler will construct the file table, then translate the compiler
13473 internal file table number into the assembler file table number, and emit
13474 a .file directive if we haven't already emitted one yet. The file table
13475 numbers are different because we prune debug info for unused variables and
13476 types, which may include filenames. */
13478 static int
13479 maybe_emit_file (int fileno)
13481 if (DWARF2_ASM_LINE_DEBUG_INFO && fileno > 0)
13483 if (!VARRAY_UINT (file_table_emitted, fileno))
13485 VARRAY_UINT (file_table_emitted, fileno) = ++emitcount;
13486 fprintf (asm_out_file, "\t.file %u ",
13487 VARRAY_UINT (file_table_emitted, fileno));
13488 output_quoted_string (asm_out_file,
13489 VARRAY_CHAR_PTR (file_table, fileno));
13490 fputc ('\n', asm_out_file);
13492 return VARRAY_UINT (file_table_emitted, fileno);
13494 else
13495 return fileno;
13498 /* Initialize the compiler internal file table. */
13500 static void
13501 init_file_table (void)
13503 /* Allocate the initial hunk of the file_table. */
13504 VARRAY_CHAR_PTR_INIT (file_table, 64, "file_table");
13505 VARRAY_UINT_INIT (file_table_emitted, 64, "file_table_emitted");
13507 /* Skip the first entry - file numbers begin at 1. */
13508 VARRAY_PUSH_CHAR_PTR (file_table, NULL);
13509 VARRAY_PUSH_UINT (file_table_emitted, 0);
13510 file_table_last_lookup_index = 0;
13513 /* Called by the final INSN scan whenever we see a var location. We
13514 use it to drop labels in the right places, and throw the location in
13515 our lookup table. */
13517 static void
13518 dwarf2out_var_location (rtx loc_note)
13520 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
13521 struct var_loc_node *newloc;
13522 rtx prev_insn;
13523 static rtx last_insn;
13524 static const char *last_label;
13525 tree decl;
13527 if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
13528 return;
13529 prev_insn = PREV_INSN (loc_note);
13531 newloc = ggc_alloc_cleared (sizeof (struct var_loc_node));
13532 /* If the insn we processed last time is the previous insn
13533 and it is also a var location note, use the label we emitted
13534 last time. */
13535 if (last_insn != NULL_RTX
13536 && last_insn == prev_insn
13537 && NOTE_P (prev_insn)
13538 && NOTE_LINE_NUMBER (prev_insn) == NOTE_INSN_VAR_LOCATION)
13540 newloc->label = last_label;
13542 else
13544 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
13545 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
13546 loclabel_num++;
13547 newloc->label = ggc_strdup (loclabel);
13549 newloc->var_loc_note = loc_note;
13550 newloc->next = NULL;
13552 if (cfun
13553 && (last_text_section == in_unlikely_executed_text
13554 || (last_text_section == in_named
13555 && last_text_section_name == cfun->unlikely_text_section_name)))
13556 newloc->section_label = cfun->cold_section_label;
13557 else
13558 newloc->section_label = text_section_label;
13560 last_insn = loc_note;
13561 last_label = newloc->label;
13562 decl = NOTE_VAR_LOCATION_DECL (loc_note);
13563 if (DECL_DEBUG_EXPR_IS_FROM (decl) && DECL_DEBUG_EXPR (decl)
13564 && DECL_P (DECL_DEBUG_EXPR (decl)))
13565 decl = DECL_DEBUG_EXPR (decl);
13566 add_var_loc_to_decl (decl, newloc);
13569 /* We need to reset the locations at the beginning of each
13570 function. We can't do this in the end_function hook, because the
13571 declarations that use the locations won't have been outputted when
13572 that hook is called. */
13574 static void
13575 dwarf2out_begin_function (tree unused ATTRIBUTE_UNUSED)
13577 htab_empty (decl_loc_table);
13580 /* Output a label to mark the beginning of a source code line entry
13581 and record information relating to this source line, in
13582 'line_info_table' for later output of the .debug_line section. */
13584 static void
13585 dwarf2out_source_line (unsigned int line, const char *filename)
13587 if (debug_info_level >= DINFO_LEVEL_NORMAL
13588 && line != 0)
13590 current_function_section (current_function_decl);
13592 /* If requested, emit something human-readable. */
13593 if (flag_debug_asm)
13594 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
13595 filename, line);
13597 if (DWARF2_ASM_LINE_DEBUG_INFO)
13599 unsigned file_num = lookup_filename (filename);
13601 file_num = maybe_emit_file (file_num);
13603 /* Emit the .loc directive understood by GNU as. */
13604 fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
13606 /* Indicate that line number info exists. */
13607 line_info_table_in_use++;
13609 /* Indicate that multiple line number tables exist. */
13610 if (DECL_SECTION_NAME (current_function_decl))
13611 separate_line_info_table_in_use++;
13613 else if (DECL_SECTION_NAME (current_function_decl))
13615 dw_separate_line_info_ref line_info;
13616 targetm.asm_out.internal_label (asm_out_file, SEPARATE_LINE_CODE_LABEL,
13617 separate_line_info_table_in_use);
13619 /* Expand the line info table if necessary. */
13620 if (separate_line_info_table_in_use
13621 == separate_line_info_table_allocated)
13623 separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
13624 separate_line_info_table
13625 = ggc_realloc (separate_line_info_table,
13626 separate_line_info_table_allocated
13627 * sizeof (dw_separate_line_info_entry));
13628 memset (separate_line_info_table
13629 + separate_line_info_table_in_use,
13631 (LINE_INFO_TABLE_INCREMENT
13632 * sizeof (dw_separate_line_info_entry)));
13635 /* Add the new entry at the end of the line_info_table. */
13636 line_info
13637 = &separate_line_info_table[separate_line_info_table_in_use++];
13638 line_info->dw_file_num = lookup_filename (filename);
13639 line_info->dw_line_num = line;
13640 line_info->function = current_function_funcdef_no;
13642 else
13644 dw_line_info_ref line_info;
13646 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
13647 line_info_table_in_use);
13649 /* Expand the line info table if necessary. */
13650 if (line_info_table_in_use == line_info_table_allocated)
13652 line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
13653 line_info_table
13654 = ggc_realloc (line_info_table,
13655 (line_info_table_allocated
13656 * sizeof (dw_line_info_entry)));
13657 memset (line_info_table + line_info_table_in_use, 0,
13658 LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
13661 /* Add the new entry at the end of the line_info_table. */
13662 line_info = &line_info_table[line_info_table_in_use++];
13663 line_info->dw_file_num = lookup_filename (filename);
13664 line_info->dw_line_num = line;
13669 /* Record the beginning of a new source file. */
13671 static void
13672 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
13674 if (flag_eliminate_dwarf2_dups)
13676 /* Record the beginning of the file for break_out_includes. */
13677 dw_die_ref bincl_die;
13679 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
13680 add_AT_string (bincl_die, DW_AT_name, filename);
13683 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13685 int fileno;
13687 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13688 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
13689 dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
13690 lineno);
13692 fileno = maybe_emit_file (lookup_filename (filename));
13693 dw2_asm_output_data_uleb128 (fileno, "Filename we just started");
13697 /* Record the end of a source file. */
13699 static void
13700 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
13702 if (flag_eliminate_dwarf2_dups)
13703 /* Record the end of the file for break_out_includes. */
13704 new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
13706 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13708 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13709 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
13713 /* Called from debug_define in toplev.c. The `buffer' parameter contains
13714 the tail part of the directive line, i.e. the part which is past the
13715 initial whitespace, #, whitespace, directive-name, whitespace part. */
13717 static void
13718 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
13719 const char *buffer ATTRIBUTE_UNUSED)
13721 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13723 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13724 dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
13725 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
13726 dw2_asm_output_nstring (buffer, -1, "The macro");
13730 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
13731 the tail part of the directive line, i.e. the part which is past the
13732 initial whitespace, #, whitespace, directive-name, whitespace part. */
13734 static void
13735 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
13736 const char *buffer ATTRIBUTE_UNUSED)
13738 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13740 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13741 dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
13742 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
13743 dw2_asm_output_nstring (buffer, -1, "The macro");
13747 /* Set up for Dwarf output at the start of compilation. */
13749 static void
13750 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
13752 init_file_table ();
13754 /* Allocate the decl_die_table. */
13755 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
13756 decl_die_table_eq, NULL);
13758 /* Allocate the decl_loc_table. */
13759 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
13760 decl_loc_table_eq, NULL);
13762 /* Allocate the initial hunk of the decl_scope_table. */
13763 decl_scope_table = VEC_alloc (tree, gc, 256);
13765 /* Allocate the initial hunk of the abbrev_die_table. */
13766 abbrev_die_table = ggc_alloc_cleared (ABBREV_DIE_TABLE_INCREMENT
13767 * sizeof (dw_die_ref));
13768 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
13769 /* Zero-th entry is allocated, but unused. */
13770 abbrev_die_table_in_use = 1;
13772 /* Allocate the initial hunk of the line_info_table. */
13773 line_info_table = ggc_alloc_cleared (LINE_INFO_TABLE_INCREMENT
13774 * sizeof (dw_line_info_entry));
13775 line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
13777 /* Zero-th entry is allocated, but unused. */
13778 line_info_table_in_use = 1;
13780 /* Generate the initial DIE for the .debug section. Note that the (string)
13781 value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
13782 will (typically) be a relative pathname and that this pathname should be
13783 taken as being relative to the directory from which the compiler was
13784 invoked when the given (base) source file was compiled. We will fill
13785 in this value in dwarf2out_finish. */
13786 comp_unit_die = gen_compile_unit_die (NULL);
13788 incomplete_types = VEC_alloc (tree, gc, 64);
13790 used_rtx_array = VEC_alloc (rtx, gc, 32);
13792 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
13793 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
13794 DEBUG_ABBREV_SECTION_LABEL, 0);
13795 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
13796 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
13797 COLD_TEXT_SECTION_LABEL, 0);
13798 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
13800 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
13801 DEBUG_INFO_SECTION_LABEL, 0);
13802 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
13803 DEBUG_LINE_SECTION_LABEL, 0);
13804 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
13805 DEBUG_RANGES_SECTION_LABEL, 0);
13806 named_section_flags (DEBUG_ABBREV_SECTION, SECTION_DEBUG);
13807 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
13808 named_section_flags (DEBUG_INFO_SECTION, SECTION_DEBUG);
13809 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
13810 named_section_flags (DEBUG_LINE_SECTION, SECTION_DEBUG);
13811 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
13813 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13815 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13816 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
13817 DEBUG_MACINFO_SECTION_LABEL, 0);
13818 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
13821 text_section ();
13822 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
13823 if (flag_reorder_blocks_and_partition)
13825 unlikely_text_section ();
13826 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
13830 /* A helper function for dwarf2out_finish called through
13831 ht_forall. Emit one queued .debug_str string. */
13833 static int
13834 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
13836 struct indirect_string_node *node = (struct indirect_string_node *) *h;
13838 if (node->form == DW_FORM_strp)
13840 named_section_flags (DEBUG_STR_SECTION, DEBUG_STR_SECTION_FLAGS);
13841 ASM_OUTPUT_LABEL (asm_out_file, node->label);
13842 assemble_string (node->str, strlen (node->str) + 1);
13845 return 1;
13850 /* Clear the marks for a die and its children.
13851 Be cool if the mark isn't set. */
13853 static void
13854 prune_unmark_dies (dw_die_ref die)
13856 dw_die_ref c;
13857 die->die_mark = 0;
13858 for (c = die->die_child; c; c = c->die_sib)
13859 prune_unmark_dies (c);
13863 /* Given DIE that we're marking as used, find any other dies
13864 it references as attributes and mark them as used. */
13866 static void
13867 prune_unused_types_walk_attribs (dw_die_ref die)
13869 dw_attr_ref a;
13871 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
13873 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
13875 /* A reference to another DIE.
13876 Make sure that it will get emitted. */
13877 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
13879 else if (a->dw_attr == DW_AT_decl_file || a->dw_attr == DW_AT_call_file)
13881 /* A reference to a file. Make sure the file name is emitted. */
13882 a->dw_attr_val.v.val_unsigned =
13883 maybe_emit_file (a->dw_attr_val.v.val_unsigned);
13889 /* Mark DIE as being used. If DOKIDS is true, then walk down
13890 to DIE's children. */
13892 static void
13893 prune_unused_types_mark (dw_die_ref die, int dokids)
13895 dw_die_ref c;
13897 if (die->die_mark == 0)
13899 /* We haven't done this node yet. Mark it as used. */
13900 die->die_mark = 1;
13902 /* We also have to mark its parents as used.
13903 (But we don't want to mark our parents' kids due to this.) */
13904 if (die->die_parent)
13905 prune_unused_types_mark (die->die_parent, 0);
13907 /* Mark any referenced nodes. */
13908 prune_unused_types_walk_attribs (die);
13910 /* If this node is a specification,
13911 also mark the definition, if it exists. */
13912 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
13913 prune_unused_types_mark (die->die_definition, 1);
13916 if (dokids && die->die_mark != 2)
13918 /* We need to walk the children, but haven't done so yet.
13919 Remember that we've walked the kids. */
13920 die->die_mark = 2;
13922 /* Walk them. */
13923 for (c = die->die_child; c; c = c->die_sib)
13925 /* If this is an array type, we need to make sure our
13926 kids get marked, even if they're types. */
13927 if (die->die_tag == DW_TAG_array_type)
13928 prune_unused_types_mark (c, 1);
13929 else
13930 prune_unused_types_walk (c);
13936 /* Walk the tree DIE and mark types that we actually use. */
13938 static void
13939 prune_unused_types_walk (dw_die_ref die)
13941 dw_die_ref c;
13943 /* Don't do anything if this node is already marked. */
13944 if (die->die_mark)
13945 return;
13947 switch (die->die_tag) {
13948 case DW_TAG_const_type:
13949 case DW_TAG_packed_type:
13950 case DW_TAG_pointer_type:
13951 case DW_TAG_reference_type:
13952 case DW_TAG_volatile_type:
13953 case DW_TAG_typedef:
13954 case DW_TAG_array_type:
13955 case DW_TAG_structure_type:
13956 case DW_TAG_union_type:
13957 case DW_TAG_class_type:
13958 case DW_TAG_friend:
13959 case DW_TAG_variant_part:
13960 case DW_TAG_enumeration_type:
13961 case DW_TAG_subroutine_type:
13962 case DW_TAG_string_type:
13963 case DW_TAG_set_type:
13964 case DW_TAG_subrange_type:
13965 case DW_TAG_ptr_to_member_type:
13966 case DW_TAG_file_type:
13967 /* It's a type node --- don't mark it. */
13968 return;
13970 default:
13971 /* Mark everything else. */
13972 break;
13975 die->die_mark = 1;
13977 /* Now, mark any dies referenced from here. */
13978 prune_unused_types_walk_attribs (die);
13980 /* Mark children. */
13981 for (c = die->die_child; c; c = c->die_sib)
13982 prune_unused_types_walk (c);
13986 /* Remove from the tree DIE any dies that aren't marked. */
13988 static void
13989 prune_unused_types_prune (dw_die_ref die)
13991 dw_die_ref c, p, n;
13993 gcc_assert (die->die_mark);
13995 p = NULL;
13996 for (c = die->die_child; c; c = n)
13998 n = c->die_sib;
13999 if (c->die_mark)
14001 prune_unused_types_prune (c);
14002 p = c;
14004 else
14006 if (p)
14007 p->die_sib = n;
14008 else
14009 die->die_child = n;
14010 free_die (c);
14016 /* Remove dies representing declarations that we never use. */
14018 static void
14019 prune_unused_types (void)
14021 unsigned int i;
14022 limbo_die_node *node;
14024 /* Clear all the marks. */
14025 prune_unmark_dies (comp_unit_die);
14026 for (node = limbo_die_list; node; node = node->next)
14027 prune_unmark_dies (node->die);
14029 /* Set the mark on nodes that are actually used. */
14030 prune_unused_types_walk (comp_unit_die);
14031 for (node = limbo_die_list; node; node = node->next)
14032 prune_unused_types_walk (node->die);
14034 /* Also set the mark on nodes referenced from the
14035 pubname_table or arange_table. */
14036 for (i = 0; i < pubname_table_in_use; i++)
14037 prune_unused_types_mark (pubname_table[i].die, 1);
14038 for (i = 0; i < arange_table_in_use; i++)
14039 prune_unused_types_mark (arange_table[i], 1);
14041 /* Get rid of nodes that aren't marked. */
14042 prune_unused_types_prune (comp_unit_die);
14043 for (node = limbo_die_list; node; node = node->next)
14044 prune_unused_types_prune (node->die);
14046 /* Leave the marks clear. */
14047 prune_unmark_dies (comp_unit_die);
14048 for (node = limbo_die_list; node; node = node->next)
14049 prune_unmark_dies (node->die);
14052 /* Output stuff that dwarf requires at the end of every file,
14053 and generate the DWARF-2 debugging info. */
14055 static void
14056 dwarf2out_finish (const char *filename)
14058 limbo_die_node *node, *next_node;
14059 dw_die_ref die = 0;
14061 /* Add the name for the main input file now. We delayed this from
14062 dwarf2out_init to avoid complications with PCH. */
14063 add_name_attribute (comp_unit_die, filename);
14064 if (filename[0] != DIR_SEPARATOR)
14065 add_comp_dir_attribute (comp_unit_die);
14066 else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
14068 size_t i;
14069 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
14070 if (VARRAY_CHAR_PTR (file_table, i)[0] != DIR_SEPARATOR
14071 /* Don't add cwd for <built-in>. */
14072 && VARRAY_CHAR_PTR (file_table, i)[0] != '<')
14074 add_comp_dir_attribute (comp_unit_die);
14075 break;
14079 /* Traverse the limbo die list, and add parent/child links. The only
14080 dies without parents that should be here are concrete instances of
14081 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
14082 For concrete instances, we can get the parent die from the abstract
14083 instance. */
14084 for (node = limbo_die_list; node; node = next_node)
14086 next_node = node->next;
14087 die = node->die;
14089 if (die->die_parent == NULL)
14091 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
14093 if (origin)
14094 add_child_die (origin->die_parent, die);
14095 else if (die == comp_unit_die)
14097 else if (errorcount > 0 || sorrycount > 0)
14098 /* It's OK to be confused by errors in the input. */
14099 add_child_die (comp_unit_die, die);
14100 else
14102 /* In certain situations, the lexical block containing a
14103 nested function can be optimized away, which results
14104 in the nested function die being orphaned. Likewise
14105 with the return type of that nested function. Force
14106 this to be a child of the containing function.
14108 It may happen that even the containing function got fully
14109 inlined and optimized out. In that case we are lost and
14110 assign the empty child. This should not be big issue as
14111 the function is likely unreachable too. */
14112 tree context = NULL_TREE;
14114 gcc_assert (node->created_for);
14116 if (DECL_P (node->created_for))
14117 context = DECL_CONTEXT (node->created_for);
14118 else if (TYPE_P (node->created_for))
14119 context = TYPE_CONTEXT (node->created_for);
14121 gcc_assert (context && TREE_CODE (context) == FUNCTION_DECL);
14123 origin = lookup_decl_die (context);
14124 if (origin)
14125 add_child_die (origin, die);
14126 else
14127 add_child_die (comp_unit_die, die);
14132 limbo_die_list = NULL;
14134 /* Walk through the list of incomplete types again, trying once more to
14135 emit full debugging info for them. */
14136 retry_incomplete_types ();
14138 /* We need to reverse all the dies before break_out_includes, or
14139 we'll see the end of an include file before the beginning. */
14140 reverse_all_dies (comp_unit_die);
14142 if (flag_eliminate_unused_debug_types)
14143 prune_unused_types ();
14145 /* Generate separate CUs for each of the include files we've seen.
14146 They will go into limbo_die_list. */
14147 if (flag_eliminate_dwarf2_dups)
14148 break_out_includes (comp_unit_die);
14150 /* Traverse the DIE's and add add sibling attributes to those DIE's
14151 that have children. */
14152 add_sibling_attributes (comp_unit_die);
14153 for (node = limbo_die_list; node; node = node->next)
14154 add_sibling_attributes (node->die);
14156 /* Output a terminator label for the .text section. */
14157 text_section ();
14158 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
14159 if (flag_reorder_blocks_and_partition)
14161 unlikely_text_section ();
14162 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
14165 /* Output the source line correspondence table. We must do this
14166 even if there is no line information. Otherwise, on an empty
14167 translation unit, we will generate a present, but empty,
14168 .debug_info section. IRIX 6.5 `nm' will then complain when
14169 examining the file. */
14170 if (! DWARF2_ASM_LINE_DEBUG_INFO)
14172 named_section_flags (DEBUG_LINE_SECTION, SECTION_DEBUG);
14173 output_line_info ();
14176 /* Output location list section if necessary. */
14177 if (have_location_lists)
14179 /* Output the location lists info. */
14180 named_section_flags (DEBUG_LOC_SECTION, SECTION_DEBUG);
14181 ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
14182 DEBUG_LOC_SECTION_LABEL, 0);
14183 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
14184 output_location_lists (die);
14185 have_location_lists = 0;
14188 /* We can only use the low/high_pc attributes if all of the code was
14189 in .text. */
14190 if (!separate_line_info_table_in_use && !have_switched_text_section)
14192 add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
14193 add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
14196 /* If it wasn't, we need to give .debug_loc and .debug_ranges an appropriate
14197 "base address". Use zero so that these addresses become absolute. */
14198 else if (have_location_lists || ranges_table_in_use)
14199 add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
14201 if (debug_info_level >= DINFO_LEVEL_NORMAL)
14202 add_AT_lbl_offset (comp_unit_die, DW_AT_stmt_list,
14203 debug_line_section_label);
14205 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14206 add_AT_lbl_offset (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
14208 /* Output all of the compilation units. We put the main one last so that
14209 the offsets are available to output_pubnames. */
14210 for (node = limbo_die_list; node; node = node->next)
14211 output_comp_unit (node->die, 0);
14213 output_comp_unit (comp_unit_die, 0);
14215 /* Output the abbreviation table. */
14216 named_section_flags (DEBUG_ABBREV_SECTION, SECTION_DEBUG);
14217 output_abbrev_section ();
14219 /* Output public names table if necessary. */
14220 if (pubname_table_in_use)
14222 named_section_flags (DEBUG_PUBNAMES_SECTION, SECTION_DEBUG);
14223 output_pubnames ();
14226 /* Output the address range information. We only put functions in the arange
14227 table, so don't write it out if we don't have any. */
14228 if (fde_table_in_use)
14230 named_section_flags (DEBUG_ARANGES_SECTION, SECTION_DEBUG);
14231 output_aranges ();
14234 /* Output ranges section if necessary. */
14235 if (ranges_table_in_use)
14237 named_section_flags (DEBUG_RANGES_SECTION, SECTION_DEBUG);
14238 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
14239 output_ranges ();
14242 /* Have to end the macro section. */
14243 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14245 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
14246 dw2_asm_output_data (1, 0, "End compilation unit");
14249 /* If we emitted any DW_FORM_strp form attribute, output the string
14250 table too. */
14251 if (debug_str_hash)
14252 htab_traverse (debug_str_hash, output_indirect_string, NULL);
14254 #else
14256 /* This should never be used, but its address is needed for comparisons. */
14257 const struct gcc_debug_hooks dwarf2_debug_hooks;
14259 #endif /* DWARF2_DEBUGGING_INFO */
14261 #include "gt-dwarf2out.h"