2005-03-29 Paul Brook <paul@codesourcery.com>
[official-gcc.git] / gcc / dwarf2out.c
blob24e573b2ea4cd3885eed324b324b25ce4d9c83d3
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, 59 Temple Place - Suite 330, Boston, MA
23 02111-1307, 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 /* Array of RTXes referenced by the debugging information, which therefore
174 must be kept around forever. */
175 static GTY(()) varray_type used_rtx_varray;
177 /* A pointer to the base of a list of incomplete types which might be
178 completed at some later time. incomplete_types_list needs to be a VARRAY
179 because we want to tell the garbage collector about it. */
180 static GTY(()) varray_type incomplete_types;
182 /* A pointer to the base of a table of references to declaration
183 scopes. This table is a display which tracks the nesting
184 of declaration scopes at the current scope and containing
185 scopes. This table is used to find the proper place to
186 define type declaration DIE's. */
187 static GTY(()) varray_type decl_scope_table;
189 /* How to start an assembler comment. */
190 #ifndef ASM_COMMENT_START
191 #define ASM_COMMENT_START ";#"
192 #endif
194 typedef struct dw_cfi_struct *dw_cfi_ref;
195 typedef struct dw_fde_struct *dw_fde_ref;
196 typedef union dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
198 /* Call frames are described using a sequence of Call Frame
199 Information instructions. The register number, offset
200 and address fields are provided as possible operands;
201 their use is selected by the opcode field. */
203 enum dw_cfi_oprnd_type {
204 dw_cfi_oprnd_unused,
205 dw_cfi_oprnd_reg_num,
206 dw_cfi_oprnd_offset,
207 dw_cfi_oprnd_addr,
208 dw_cfi_oprnd_loc
211 typedef union dw_cfi_oprnd_struct GTY(())
213 unsigned long GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
214 HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
215 const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
216 struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
218 dw_cfi_oprnd;
220 typedef struct dw_cfi_struct GTY(())
222 dw_cfi_ref dw_cfi_next;
223 enum dwarf_call_frame_info dw_cfi_opc;
224 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
225 dw_cfi_oprnd1;
226 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
227 dw_cfi_oprnd2;
229 dw_cfi_node;
231 /* This is how we define the location of the CFA. We use to handle it
232 as REG + OFFSET all the time, but now it can be more complex.
233 It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
234 Instead of passing around REG and OFFSET, we pass a copy
235 of this structure. */
236 typedef struct cfa_loc GTY(())
238 unsigned long reg;
239 HOST_WIDE_INT offset;
240 HOST_WIDE_INT base_offset;
241 int indirect; /* 1 if CFA is accessed via a dereference. */
242 } dw_cfa_location;
244 /* All call frame descriptions (FDE's) in the GCC generated DWARF
245 refer to a single Common Information Entry (CIE), defined at
246 the beginning of the .debug_frame section. This use of a single
247 CIE obviates the need to keep track of multiple CIE's
248 in the DWARF generation routines below. */
250 typedef struct dw_fde_struct GTY(())
252 tree decl;
253 const char *dw_fde_begin;
254 const char *dw_fde_current_label;
255 const char *dw_fde_end;
256 dw_cfi_ref dw_fde_cfi;
257 unsigned funcdef_number;
258 unsigned all_throwers_are_sibcalls : 1;
259 unsigned nothrow : 1;
260 unsigned uses_eh_lsda : 1;
262 dw_fde_node;
264 /* Maximum size (in bytes) of an artificially generated label. */
265 #define MAX_ARTIFICIAL_LABEL_BYTES 30
267 /* The size of addresses as they appear in the Dwarf 2 data.
268 Some architectures use word addresses to refer to code locations,
269 but Dwarf 2 info always uses byte addresses. On such machines,
270 Dwarf 2 addresses need to be larger than the architecture's
271 pointers. */
272 #ifndef DWARF2_ADDR_SIZE
273 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
274 #endif
276 /* The size in bytes of a DWARF field indicating an offset or length
277 relative to a debug info section, specified to be 4 bytes in the
278 DWARF-2 specification. The SGI/MIPS ABI defines it to be the same
279 as PTR_SIZE. */
281 #ifndef DWARF_OFFSET_SIZE
282 #define DWARF_OFFSET_SIZE 4
283 #endif
285 /* According to the (draft) DWARF 3 specification, the initial length
286 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
287 bytes are 0xffffffff, followed by the length stored in the next 8
288 bytes.
290 However, the SGI/MIPS ABI uses an initial length which is equal to
291 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
293 #ifndef DWARF_INITIAL_LENGTH_SIZE
294 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
295 #endif
297 #define DWARF_VERSION 2
299 /* Round SIZE up to the nearest BOUNDARY. */
300 #define DWARF_ROUND(SIZE,BOUNDARY) \
301 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
303 /* Offsets recorded in opcodes are a multiple of this alignment factor. */
304 #ifndef DWARF_CIE_DATA_ALIGNMENT
305 #ifdef STACK_GROWS_DOWNWARD
306 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
307 #else
308 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
309 #endif
310 #endif
312 /* A pointer to the base of a table that contains frame description
313 information for each routine. */
314 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
316 /* Number of elements currently allocated for fde_table. */
317 static GTY(()) unsigned fde_table_allocated;
319 /* Number of elements in fde_table currently in use. */
320 static GTY(()) unsigned fde_table_in_use;
322 /* Size (in elements) of increments by which we may expand the
323 fde_table. */
324 #define FDE_TABLE_INCREMENT 256
326 /* A list of call frame insns for the CIE. */
327 static GTY(()) dw_cfi_ref cie_cfi_head;
329 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
330 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
331 attribute that accelerates the lookup of the FDE associated
332 with the subprogram. This variable holds the table index of the FDE
333 associated with the current function (body) definition. */
334 static unsigned current_funcdef_fde;
335 #endif
337 struct indirect_string_node GTY(())
339 const char *str;
340 unsigned int refcount;
341 unsigned int form;
342 char *label;
345 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
347 static GTY(()) int dw2_string_counter;
348 static GTY(()) unsigned long dwarf2out_cfi_label_num;
350 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
352 /* Forward declarations for functions defined in this file. */
354 static char *stripattributes (const char *);
355 static const char *dwarf_cfi_name (unsigned);
356 static dw_cfi_ref new_cfi (void);
357 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
358 static void add_fde_cfi (const char *, dw_cfi_ref);
359 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *);
360 static void lookup_cfa (dw_cfa_location *);
361 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
362 static void initial_return_save (rtx);
363 static HOST_WIDE_INT stack_adjust_offset (rtx);
364 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
365 static void output_call_frame_info (int);
366 static void dwarf2out_stack_adjust (rtx, bool);
367 static void flush_queued_reg_saves (void);
368 static bool clobbers_queued_reg_save (rtx);
369 static void dwarf2out_frame_debug_expr (rtx, const char *);
371 /* Support for complex CFA locations. */
372 static void output_cfa_loc (dw_cfi_ref);
373 static void get_cfa_from_loc_descr (dw_cfa_location *,
374 struct dw_loc_descr_struct *);
375 static struct dw_loc_descr_struct *build_cfa_loc
376 (dw_cfa_location *);
377 static void def_cfa_1 (const char *, dw_cfa_location *);
379 /* How to start an assembler comment. */
380 #ifndef ASM_COMMENT_START
381 #define ASM_COMMENT_START ";#"
382 #endif
384 /* Data and reference forms for relocatable data. */
385 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
386 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
388 #ifndef DEBUG_FRAME_SECTION
389 #define DEBUG_FRAME_SECTION ".debug_frame"
390 #endif
392 #ifndef FUNC_BEGIN_LABEL
393 #define FUNC_BEGIN_LABEL "LFB"
394 #endif
396 #ifndef FUNC_END_LABEL
397 #define FUNC_END_LABEL "LFE"
398 #endif
400 #ifndef FRAME_BEGIN_LABEL
401 #define FRAME_BEGIN_LABEL "Lframe"
402 #endif
403 #define CIE_AFTER_SIZE_LABEL "LSCIE"
404 #define CIE_END_LABEL "LECIE"
405 #define FDE_LABEL "LSFDE"
406 #define FDE_AFTER_SIZE_LABEL "LASFDE"
407 #define FDE_END_LABEL "LEFDE"
408 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
409 #define LINE_NUMBER_END_LABEL "LELT"
410 #define LN_PROLOG_AS_LABEL "LASLTP"
411 #define LN_PROLOG_END_LABEL "LELTP"
412 #define DIE_LABEL_PREFIX "DW"
414 /* The DWARF 2 CFA column which tracks the return address. Normally this
415 is the column for PC, or the first column after all of the hard
416 registers. */
417 #ifndef DWARF_FRAME_RETURN_COLUMN
418 #ifdef PC_REGNUM
419 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGNUM (PC_REGNUM)
420 #else
421 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGISTERS
422 #endif
423 #endif
425 /* The mapping from gcc register number to DWARF 2 CFA column number. By
426 default, we just provide columns for all registers. */
427 #ifndef DWARF_FRAME_REGNUM
428 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
429 #endif
431 /* The offset from the incoming value of %sp to the top of the stack frame
432 for the current function. */
433 #ifndef INCOMING_FRAME_SP_OFFSET
434 #define INCOMING_FRAME_SP_OFFSET 0
435 #endif
437 /* Hook used by __throw. */
440 expand_builtin_dwarf_sp_column (void)
442 return GEN_INT (DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
445 /* Return a pointer to a copy of the section string name S with all
446 attributes stripped off, and an asterisk prepended (for assemble_name). */
448 static inline char *
449 stripattributes (const char *s)
451 char *stripped = xmalloc (strlen (s) + 2);
452 char *p = stripped;
454 *p++ = '*';
456 while (*s && *s != ',')
457 *p++ = *s++;
459 *p = '\0';
460 return stripped;
463 /* Generate code to initialize the register size table. */
465 void
466 expand_builtin_init_dwarf_reg_sizes (tree address)
468 int i;
469 enum machine_mode mode = TYPE_MODE (char_type_node);
470 rtx addr = expand_expr (address, NULL_RTX, VOIDmode, 0);
471 rtx mem = gen_rtx_MEM (BLKmode, addr);
472 bool wrote_return_column = false;
474 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
475 if (DWARF_FRAME_REGNUM (i) < DWARF_FRAME_REGISTERS)
477 HOST_WIDE_INT offset = DWARF_FRAME_REGNUM (i) * GET_MODE_SIZE (mode);
478 enum machine_mode save_mode = reg_raw_mode[i];
479 HOST_WIDE_INT size;
481 if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
482 save_mode = choose_hard_reg_mode (i, 1, true);
483 if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
485 if (save_mode == VOIDmode)
486 continue;
487 wrote_return_column = true;
489 size = GET_MODE_SIZE (save_mode);
490 if (offset < 0)
491 continue;
493 emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
496 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
497 gcc_assert (wrote_return_column);
498 i = DWARF_ALT_FRAME_RETURN_COLUMN;
499 wrote_return_column = false;
500 #else
501 i = DWARF_FRAME_RETURN_COLUMN;
502 #endif
504 if (! wrote_return_column)
506 enum machine_mode save_mode = Pmode;
507 HOST_WIDE_INT offset = i * GET_MODE_SIZE (mode);
508 HOST_WIDE_INT size = GET_MODE_SIZE (save_mode);
509 emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
513 /* Convert a DWARF call frame info. operation to its string name */
515 static const char *
516 dwarf_cfi_name (unsigned int cfi_opc)
518 switch (cfi_opc)
520 case DW_CFA_advance_loc:
521 return "DW_CFA_advance_loc";
522 case DW_CFA_offset:
523 return "DW_CFA_offset";
524 case DW_CFA_restore:
525 return "DW_CFA_restore";
526 case DW_CFA_nop:
527 return "DW_CFA_nop";
528 case DW_CFA_set_loc:
529 return "DW_CFA_set_loc";
530 case DW_CFA_advance_loc1:
531 return "DW_CFA_advance_loc1";
532 case DW_CFA_advance_loc2:
533 return "DW_CFA_advance_loc2";
534 case DW_CFA_advance_loc4:
535 return "DW_CFA_advance_loc4";
536 case DW_CFA_offset_extended:
537 return "DW_CFA_offset_extended";
538 case DW_CFA_restore_extended:
539 return "DW_CFA_restore_extended";
540 case DW_CFA_undefined:
541 return "DW_CFA_undefined";
542 case DW_CFA_same_value:
543 return "DW_CFA_same_value";
544 case DW_CFA_register:
545 return "DW_CFA_register";
546 case DW_CFA_remember_state:
547 return "DW_CFA_remember_state";
548 case DW_CFA_restore_state:
549 return "DW_CFA_restore_state";
550 case DW_CFA_def_cfa:
551 return "DW_CFA_def_cfa";
552 case DW_CFA_def_cfa_register:
553 return "DW_CFA_def_cfa_register";
554 case DW_CFA_def_cfa_offset:
555 return "DW_CFA_def_cfa_offset";
557 /* DWARF 3 */
558 case DW_CFA_def_cfa_expression:
559 return "DW_CFA_def_cfa_expression";
560 case DW_CFA_expression:
561 return "DW_CFA_expression";
562 case DW_CFA_offset_extended_sf:
563 return "DW_CFA_offset_extended_sf";
564 case DW_CFA_def_cfa_sf:
565 return "DW_CFA_def_cfa_sf";
566 case DW_CFA_def_cfa_offset_sf:
567 return "DW_CFA_def_cfa_offset_sf";
569 /* SGI/MIPS specific */
570 case DW_CFA_MIPS_advance_loc8:
571 return "DW_CFA_MIPS_advance_loc8";
573 /* GNU extensions */
574 case DW_CFA_GNU_window_save:
575 return "DW_CFA_GNU_window_save";
576 case DW_CFA_GNU_args_size:
577 return "DW_CFA_GNU_args_size";
578 case DW_CFA_GNU_negative_offset_extended:
579 return "DW_CFA_GNU_negative_offset_extended";
581 default:
582 return "DW_CFA_<unknown>";
586 /* Return a pointer to a newly allocated Call Frame Instruction. */
588 static inline dw_cfi_ref
589 new_cfi (void)
591 dw_cfi_ref cfi = ggc_alloc (sizeof (dw_cfi_node));
593 cfi->dw_cfi_next = NULL;
594 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
595 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
597 return cfi;
600 /* Add a Call Frame Instruction to list of instructions. */
602 static inline void
603 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
605 dw_cfi_ref *p;
607 /* Find the end of the chain. */
608 for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
611 *p = cfi;
614 /* Generate a new label for the CFI info to refer to. */
616 char *
617 dwarf2out_cfi_label (void)
619 static char label[20];
621 ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
622 ASM_OUTPUT_LABEL (asm_out_file, label);
623 return label;
626 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
627 or to the CIE if LABEL is NULL. */
629 static void
630 add_fde_cfi (const char *label, dw_cfi_ref cfi)
632 if (label)
634 dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
636 if (*label == 0)
637 label = dwarf2out_cfi_label ();
639 if (fde->dw_fde_current_label == NULL
640 || strcmp (label, fde->dw_fde_current_label) != 0)
642 dw_cfi_ref xcfi;
644 fde->dw_fde_current_label = label = xstrdup (label);
646 /* Set the location counter to the new label. */
647 xcfi = new_cfi ();
648 xcfi->dw_cfi_opc = DW_CFA_advance_loc4;
649 xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
650 add_cfi (&fde->dw_fde_cfi, xcfi);
653 add_cfi (&fde->dw_fde_cfi, cfi);
656 else
657 add_cfi (&cie_cfi_head, cfi);
660 /* Subroutine of lookup_cfa. */
662 static inline void
663 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc)
665 switch (cfi->dw_cfi_opc)
667 case DW_CFA_def_cfa_offset:
668 loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
669 break;
670 case DW_CFA_def_cfa_register:
671 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
672 break;
673 case DW_CFA_def_cfa:
674 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
675 loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
676 break;
677 case DW_CFA_def_cfa_expression:
678 get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
679 break;
680 default:
681 break;
685 /* Find the previous value for the CFA. */
687 static void
688 lookup_cfa (dw_cfa_location *loc)
690 dw_cfi_ref cfi;
692 loc->reg = (unsigned long) -1;
693 loc->offset = 0;
694 loc->indirect = 0;
695 loc->base_offset = 0;
697 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
698 lookup_cfa_1 (cfi, loc);
700 if (fde_table_in_use)
702 dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
703 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
704 lookup_cfa_1 (cfi, loc);
708 /* The current rule for calculating the DWARF2 canonical frame address. */
709 static dw_cfa_location cfa;
711 /* The register used for saving registers to the stack, and its offset
712 from the CFA. */
713 static dw_cfa_location cfa_store;
715 /* The running total of the size of arguments pushed onto the stack. */
716 static HOST_WIDE_INT args_size;
718 /* The last args_size we actually output. */
719 static HOST_WIDE_INT old_args_size;
721 /* Entry point to update the canonical frame address (CFA).
722 LABEL is passed to add_fde_cfi. The value of CFA is now to be
723 calculated from REG+OFFSET. */
725 void
726 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
728 dw_cfa_location loc;
729 loc.indirect = 0;
730 loc.base_offset = 0;
731 loc.reg = reg;
732 loc.offset = offset;
733 def_cfa_1 (label, &loc);
736 /* This routine does the actual work. The CFA is now calculated from
737 the dw_cfa_location structure. */
739 static void
740 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
742 dw_cfi_ref cfi;
743 dw_cfa_location old_cfa, loc;
745 cfa = *loc_p;
746 loc = *loc_p;
748 if (cfa_store.reg == loc.reg && loc.indirect == 0)
749 cfa_store.offset = loc.offset;
751 loc.reg = DWARF_FRAME_REGNUM (loc.reg);
752 lookup_cfa (&old_cfa);
754 /* If nothing changed, no need to issue any call frame instructions. */
755 if (loc.reg == old_cfa.reg && loc.offset == old_cfa.offset
756 && loc.indirect == old_cfa.indirect
757 && (loc.indirect == 0 || loc.base_offset == old_cfa.base_offset))
758 return;
760 cfi = new_cfi ();
762 if (loc.reg == old_cfa.reg && !loc.indirect)
764 /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction,
765 indicating the CFA register did not change but the offset
766 did. */
767 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
768 cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
771 #ifndef MIPS_DEBUGGING_INFO /* SGI dbx thinks this means no offset. */
772 else if (loc.offset == old_cfa.offset && old_cfa.reg != (unsigned long) -1
773 && !loc.indirect)
775 /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
776 indicating the CFA register has changed to <register> but the
777 offset has not changed. */
778 cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
779 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
781 #endif
783 else if (loc.indirect == 0)
785 /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
786 indicating the CFA register has changed to <register> with
787 the specified offset. */
788 cfi->dw_cfi_opc = DW_CFA_def_cfa;
789 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
790 cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
792 else
794 /* Construct a DW_CFA_def_cfa_expression instruction to
795 calculate the CFA using a full location expression since no
796 register-offset pair is available. */
797 struct dw_loc_descr_struct *loc_list;
799 cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
800 loc_list = build_cfa_loc (&loc);
801 cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
804 add_fde_cfi (label, cfi);
807 /* Add the CFI for saving a register. REG is the CFA column number.
808 LABEL is passed to add_fde_cfi.
809 If SREG is -1, the register is saved at OFFSET from the CFA;
810 otherwise it is saved in SREG. */
812 static void
813 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
815 dw_cfi_ref cfi = new_cfi ();
817 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
819 if (sreg == INVALID_REGNUM)
821 if (reg & ~0x3f)
822 /* The register number won't fit in 6 bits, so we have to use
823 the long form. */
824 cfi->dw_cfi_opc = DW_CFA_offset_extended;
825 else
826 cfi->dw_cfi_opc = DW_CFA_offset;
828 #ifdef ENABLE_CHECKING
830 /* If we get an offset that is not a multiple of
831 DWARF_CIE_DATA_ALIGNMENT, there is either a bug in the
832 definition of DWARF_CIE_DATA_ALIGNMENT, or a bug in the machine
833 description. */
834 HOST_WIDE_INT check_offset = offset / DWARF_CIE_DATA_ALIGNMENT;
836 gcc_assert (check_offset * DWARF_CIE_DATA_ALIGNMENT == offset);
838 #endif
839 offset /= DWARF_CIE_DATA_ALIGNMENT;
840 if (offset < 0)
841 cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
843 cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
845 else if (sreg == reg)
846 cfi->dw_cfi_opc = DW_CFA_same_value;
847 else
849 cfi->dw_cfi_opc = DW_CFA_register;
850 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
853 add_fde_cfi (label, cfi);
856 /* Add the CFI for saving a register window. LABEL is passed to reg_save.
857 This CFI tells the unwinder that it needs to restore the window registers
858 from the previous frame's window save area.
860 ??? Perhaps we should note in the CIE where windows are saved (instead of
861 assuming 0(cfa)) and what registers are in the window. */
863 void
864 dwarf2out_window_save (const char *label)
866 dw_cfi_ref cfi = new_cfi ();
868 cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
869 add_fde_cfi (label, cfi);
872 /* Add a CFI to update the running total of the size of arguments
873 pushed onto the stack. */
875 void
876 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
878 dw_cfi_ref cfi;
880 if (size == old_args_size)
881 return;
883 old_args_size = size;
885 cfi = new_cfi ();
886 cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
887 cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
888 add_fde_cfi (label, cfi);
891 /* Entry point for saving a register to the stack. REG is the GCC register
892 number. LABEL and OFFSET are passed to reg_save. */
894 void
895 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
897 reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
900 /* Entry point for saving the return address in the stack.
901 LABEL and OFFSET are passed to reg_save. */
903 void
904 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
906 reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
909 /* Entry point for saving the return address in a register.
910 LABEL and SREG are passed to reg_save. */
912 void
913 dwarf2out_return_reg (const char *label, unsigned int sreg)
915 reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
918 /* Record the initial position of the return address. RTL is
919 INCOMING_RETURN_ADDR_RTX. */
921 static void
922 initial_return_save (rtx rtl)
924 unsigned int reg = INVALID_REGNUM;
925 HOST_WIDE_INT offset = 0;
927 switch (GET_CODE (rtl))
929 case REG:
930 /* RA is in a register. */
931 reg = DWARF_FRAME_REGNUM (REGNO (rtl));
932 break;
934 case MEM:
935 /* RA is on the stack. */
936 rtl = XEXP (rtl, 0);
937 switch (GET_CODE (rtl))
939 case REG:
940 gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
941 offset = 0;
942 break;
944 case PLUS:
945 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
946 offset = INTVAL (XEXP (rtl, 1));
947 break;
949 case MINUS:
950 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
951 offset = -INTVAL (XEXP (rtl, 1));
952 break;
954 default:
955 gcc_unreachable ();
958 break;
960 case PLUS:
961 /* The return address is at some offset from any value we can
962 actually load. For instance, on the SPARC it is in %i7+8. Just
963 ignore the offset for now; it doesn't matter for unwinding frames. */
964 gcc_assert (GET_CODE (XEXP (rtl, 1)) == CONST_INT);
965 initial_return_save (XEXP (rtl, 0));
966 return;
968 default:
969 gcc_unreachable ();
972 if (reg != DWARF_FRAME_RETURN_COLUMN)
973 reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
976 /* Given a SET, calculate the amount of stack adjustment it
977 contains. */
979 static HOST_WIDE_INT
980 stack_adjust_offset (rtx pattern)
982 rtx src = SET_SRC (pattern);
983 rtx dest = SET_DEST (pattern);
984 HOST_WIDE_INT offset = 0;
985 enum rtx_code code;
987 if (dest == stack_pointer_rtx)
989 /* (set (reg sp) (plus (reg sp) (const_int))) */
990 code = GET_CODE (src);
991 if (! (code == PLUS || code == MINUS)
992 || XEXP (src, 0) != stack_pointer_rtx
993 || GET_CODE (XEXP (src, 1)) != CONST_INT)
994 return 0;
996 offset = INTVAL (XEXP (src, 1));
997 if (code == PLUS)
998 offset = -offset;
1000 else if (MEM_P (dest))
1002 /* (set (mem (pre_dec (reg sp))) (foo)) */
1003 src = XEXP (dest, 0);
1004 code = GET_CODE (src);
1006 switch (code)
1008 case PRE_MODIFY:
1009 case POST_MODIFY:
1010 if (XEXP (src, 0) == stack_pointer_rtx)
1012 rtx val = XEXP (XEXP (src, 1), 1);
1013 /* We handle only adjustments by constant amount. */
1014 gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1015 && GET_CODE (val) == CONST_INT);
1016 offset = -INTVAL (val);
1017 break;
1019 return 0;
1021 case PRE_DEC:
1022 case POST_DEC:
1023 if (XEXP (src, 0) == stack_pointer_rtx)
1025 offset = GET_MODE_SIZE (GET_MODE (dest));
1026 break;
1028 return 0;
1030 case PRE_INC:
1031 case POST_INC:
1032 if (XEXP (src, 0) == stack_pointer_rtx)
1034 offset = -GET_MODE_SIZE (GET_MODE (dest));
1035 break;
1037 return 0;
1039 default:
1040 return 0;
1043 else
1044 return 0;
1046 return offset;
1049 /* Check INSN to see if it looks like a push or a stack adjustment, and
1050 make a note of it if it does. EH uses this information to find out how
1051 much extra space it needs to pop off the stack. */
1053 static void
1054 dwarf2out_stack_adjust (rtx insn, bool after_p)
1056 HOST_WIDE_INT offset;
1057 const char *label;
1058 int i;
1060 /* Don't handle epilogues at all. Certainly it would be wrong to do so
1061 with this function. Proper support would require all frame-related
1062 insns to be marked, and to be able to handle saving state around
1063 epilogues textually in the middle of the function. */
1064 if (prologue_epilogue_contains (insn) || sibcall_epilogue_contains (insn))
1065 return;
1067 /* If only calls can throw, and we have a frame pointer,
1068 save up adjustments until we see the CALL_INSN. */
1069 if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1071 if (CALL_P (insn) && !after_p)
1073 /* Extract the size of the args from the CALL rtx itself. */
1074 insn = PATTERN (insn);
1075 if (GET_CODE (insn) == PARALLEL)
1076 insn = XVECEXP (insn, 0, 0);
1077 if (GET_CODE (insn) == SET)
1078 insn = SET_SRC (insn);
1079 gcc_assert (GET_CODE (insn) == CALL);
1080 dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1082 return;
1085 if (CALL_P (insn) && !after_p)
1087 if (!flag_asynchronous_unwind_tables)
1088 dwarf2out_args_size ("", args_size);
1089 return;
1091 else if (BARRIER_P (insn))
1093 /* When we see a BARRIER, we know to reset args_size to 0. Usually
1094 the compiler will have already emitted a stack adjustment, but
1095 doesn't bother for calls to noreturn functions. */
1096 #ifdef STACK_GROWS_DOWNWARD
1097 offset = -args_size;
1098 #else
1099 offset = args_size;
1100 #endif
1102 else if (GET_CODE (PATTERN (insn)) == SET)
1103 offset = stack_adjust_offset (PATTERN (insn));
1104 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1105 || GET_CODE (PATTERN (insn)) == SEQUENCE)
1107 /* There may be stack adjustments inside compound insns. Search
1108 for them. */
1109 for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1110 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1111 offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i));
1113 else
1114 return;
1116 if (offset == 0)
1117 return;
1119 if (cfa.reg == STACK_POINTER_REGNUM)
1120 cfa.offset += offset;
1122 #ifndef STACK_GROWS_DOWNWARD
1123 offset = -offset;
1124 #endif
1126 args_size += offset;
1127 if (args_size < 0)
1128 args_size = 0;
1130 label = dwarf2out_cfi_label ();
1131 def_cfa_1 (label, &cfa);
1132 if (flag_asynchronous_unwind_tables)
1133 dwarf2out_args_size (label, args_size);
1136 #endif
1138 /* We delay emitting a register save until either (a) we reach the end
1139 of the prologue or (b) the register is clobbered. This clusters
1140 register saves so that there are fewer pc advances. */
1142 struct queued_reg_save GTY(())
1144 struct queued_reg_save *next;
1145 rtx reg;
1146 HOST_WIDE_INT cfa_offset;
1147 rtx saved_reg;
1150 static GTY(()) struct queued_reg_save *queued_reg_saves;
1152 /* The caller's ORIG_REG is saved in SAVED_IN_REG. */
1153 struct reg_saved_in_data GTY(()) {
1154 rtx orig_reg;
1155 rtx saved_in_reg;
1158 /* A list of registers saved in other registers.
1159 The list intentionally has a small maximum capacity of 4; if your
1160 port needs more than that, you might consider implementing a
1161 more efficient data structure. */
1162 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1163 static GTY(()) size_t num_regs_saved_in_regs;
1165 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1166 static const char *last_reg_save_label;
1168 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1169 SREG, or if SREG is NULL then it is saved at OFFSET to the CFA. */
1171 static void
1172 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1174 struct queued_reg_save *q;
1176 /* Duplicates waste space, but it's also necessary to remove them
1177 for correctness, since the queue gets output in reverse
1178 order. */
1179 for (q = queued_reg_saves; q != NULL; q = q->next)
1180 if (REGNO (q->reg) == REGNO (reg))
1181 break;
1183 if (q == NULL)
1185 q = ggc_alloc (sizeof (*q));
1186 q->next = queued_reg_saves;
1187 queued_reg_saves = q;
1190 q->reg = reg;
1191 q->cfa_offset = offset;
1192 q->saved_reg = sreg;
1194 last_reg_save_label = label;
1197 /* Output all the entries in QUEUED_REG_SAVES. */
1199 static void
1200 flush_queued_reg_saves (void)
1202 struct queued_reg_save *q;
1204 for (q = queued_reg_saves; q; q = q->next)
1206 size_t i;
1207 unsigned int reg, sreg;
1209 for (i = 0; i < num_regs_saved_in_regs; i++)
1210 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1211 break;
1212 if (q->saved_reg && i == num_regs_saved_in_regs)
1214 gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1215 num_regs_saved_in_regs++;
1217 if (i != num_regs_saved_in_regs)
1219 regs_saved_in_regs[i].orig_reg = q->reg;
1220 regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1223 reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1224 if (q->saved_reg)
1225 sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1226 else
1227 sreg = INVALID_REGNUM;
1228 reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1231 queued_reg_saves = NULL;
1232 last_reg_save_label = NULL;
1235 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1236 location for? Or, does it clobber a register which we've previously
1237 said that some other register is saved in, and for which we now
1238 have a new location for? */
1240 static bool
1241 clobbers_queued_reg_save (rtx insn)
1243 struct queued_reg_save *q;
1245 for (q = queued_reg_saves; q; q = q->next)
1247 size_t i;
1248 if (modified_in_p (q->reg, insn))
1249 return true;
1250 for (i = 0; i < num_regs_saved_in_regs; i++)
1251 if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1252 && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1253 return true;
1256 return false;
1259 /* What register, if any, is currently saved in REG? */
1261 static rtx
1262 reg_saved_in (rtx reg)
1264 unsigned int regn = REGNO (reg);
1265 size_t i;
1266 struct queued_reg_save *q;
1268 for (q = queued_reg_saves; q; q = q->next)
1269 if (q->saved_reg && regn == REGNO (q->saved_reg))
1270 return q->reg;
1272 for (i = 0; i < num_regs_saved_in_regs; i++)
1273 if (regs_saved_in_regs[i].saved_in_reg
1274 && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1275 return regs_saved_in_regs[i].orig_reg;
1277 return NULL_RTX;
1281 /* A temporary register holding an integral value used in adjusting SP
1282 or setting up the store_reg. The "offset" field holds the integer
1283 value, not an offset. */
1284 static dw_cfa_location cfa_temp;
1286 /* Record call frame debugging information for an expression EXPR,
1287 which either sets SP or FP (adjusting how we calculate the frame
1288 address) or saves a register to the stack or another register.
1289 LABEL indicates the address of EXPR.
1291 This function encodes a state machine mapping rtxes to actions on
1292 cfa, cfa_store, and cfa_temp.reg. We describe these rules so
1293 users need not read the source code.
1295 The High-Level Picture
1297 Changes in the register we use to calculate the CFA: Currently we
1298 assume that if you copy the CFA register into another register, we
1299 should take the other one as the new CFA register; this seems to
1300 work pretty well. If it's wrong for some target, it's simple
1301 enough not to set RTX_FRAME_RELATED_P on the insn in question.
1303 Changes in the register we use for saving registers to the stack:
1304 This is usually SP, but not always. Again, we deduce that if you
1305 copy SP into another register (and SP is not the CFA register),
1306 then the new register is the one we will be using for register
1307 saves. This also seems to work.
1309 Register saves: There's not much guesswork about this one; if
1310 RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1311 register save, and the register used to calculate the destination
1312 had better be the one we think we're using for this purpose.
1313 It's also assumed that a copy from a call-saved register to another
1314 register is saving that register if RTX_FRAME_RELATED_P is set on
1315 that instruction. If the copy is from a call-saved register to
1316 the *same* register, that means that the register is now the same
1317 value as in the caller.
1319 Except: If the register being saved is the CFA register, and the
1320 offset is nonzero, we are saving the CFA, so we assume we have to
1321 use DW_CFA_def_cfa_expression. If the offset is 0, we assume that
1322 the intent is to save the value of SP from the previous frame.
1324 In addition, if a register has previously been saved to a different
1325 register,
1327 Invariants / Summaries of Rules
1329 cfa current rule for calculating the CFA. It usually
1330 consists of a register and an offset.
1331 cfa_store register used by prologue code to save things to the stack
1332 cfa_store.offset is the offset from the value of
1333 cfa_store.reg to the actual CFA
1334 cfa_temp register holding an integral value. cfa_temp.offset
1335 stores the value, which will be used to adjust the
1336 stack pointer. cfa_temp is also used like cfa_store,
1337 to track stores to the stack via fp or a temp reg.
1339 Rules 1- 4: Setting a register's value to cfa.reg or an expression
1340 with cfa.reg as the first operand changes the cfa.reg and its
1341 cfa.offset. Rule 1 and 4 also set cfa_temp.reg and
1342 cfa_temp.offset.
1344 Rules 6- 9: Set a non-cfa.reg register value to a constant or an
1345 expression yielding a constant. This sets cfa_temp.reg
1346 and cfa_temp.offset.
1348 Rule 5: Create a new register cfa_store used to save items to the
1349 stack.
1351 Rules 10-14: Save a register to the stack. Define offset as the
1352 difference of the original location and cfa_store's
1353 location (or cfa_temp's location if cfa_temp is used).
1355 The Rules
1357 "{a,b}" indicates a choice of a xor b.
1358 "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1360 Rule 1:
1361 (set <reg1> <reg2>:cfa.reg)
1362 effects: cfa.reg = <reg1>
1363 cfa.offset unchanged
1364 cfa_temp.reg = <reg1>
1365 cfa_temp.offset = cfa.offset
1367 Rule 2:
1368 (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1369 {<const_int>,<reg>:cfa_temp.reg}))
1370 effects: cfa.reg = sp if fp used
1371 cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
1372 cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1373 if cfa_store.reg==sp
1375 Rule 3:
1376 (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
1377 effects: cfa.reg = fp
1378 cfa_offset += +/- <const_int>
1380 Rule 4:
1381 (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
1382 constraints: <reg1> != fp
1383 <reg1> != sp
1384 effects: cfa.reg = <reg1>
1385 cfa_temp.reg = <reg1>
1386 cfa_temp.offset = cfa.offset
1388 Rule 5:
1389 (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1390 constraints: <reg1> != fp
1391 <reg1> != sp
1392 effects: cfa_store.reg = <reg1>
1393 cfa_store.offset = cfa.offset - cfa_temp.offset
1395 Rule 6:
1396 (set <reg> <const_int>)
1397 effects: cfa_temp.reg = <reg>
1398 cfa_temp.offset = <const_int>
1400 Rule 7:
1401 (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1402 effects: cfa_temp.reg = <reg1>
1403 cfa_temp.offset |= <const_int>
1405 Rule 8:
1406 (set <reg> (high <exp>))
1407 effects: none
1409 Rule 9:
1410 (set <reg> (lo_sum <exp> <const_int>))
1411 effects: cfa_temp.reg = <reg>
1412 cfa_temp.offset = <const_int>
1414 Rule 10:
1415 (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1416 effects: cfa_store.offset -= <const_int>
1417 cfa.offset = cfa_store.offset if cfa.reg == sp
1418 cfa.reg = sp
1419 cfa.base_offset = -cfa_store.offset
1421 Rule 11:
1422 (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1423 effects: cfa_store.offset += -/+ mode_size(mem)
1424 cfa.offset = cfa_store.offset if cfa.reg == sp
1425 cfa.reg = sp
1426 cfa.base_offset = -cfa_store.offset
1428 Rule 12:
1429 (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1431 <reg2>)
1432 effects: cfa.reg = <reg1>
1433 cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
1435 Rule 13:
1436 (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1437 effects: cfa.reg = <reg1>
1438 cfa.base_offset = -{cfa_store,cfa_temp}.offset
1440 Rule 14:
1441 (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
1442 effects: cfa.reg = <reg1>
1443 cfa.base_offset = -cfa_temp.offset
1444 cfa_temp.offset -= mode_size(mem)
1446   Rule 15:
1447   (set <reg> {unspec, unspec_volatile})
1448   effects: target-dependent */
1450 static void
1451 dwarf2out_frame_debug_expr (rtx expr, const char *label)
1453 rtx src, dest;
1454 HOST_WIDE_INT offset;
1456 /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1457 the PARALLEL independently. The first element is always processed if
1458 it is a SET. This is for backward compatibility. Other elements
1459 are processed only if they are SETs and the RTX_FRAME_RELATED_P
1460 flag is set in them. */
1461 if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
1463 int par_index;
1464 int limit = XVECLEN (expr, 0);
1466 for (par_index = 0; par_index < limit; par_index++)
1467 if (GET_CODE (XVECEXP (expr, 0, par_index)) == SET
1468 && (RTX_FRAME_RELATED_P (XVECEXP (expr, 0, par_index))
1469 || par_index == 0))
1470 dwarf2out_frame_debug_expr (XVECEXP (expr, 0, par_index), label);
1472 return;
1475 gcc_assert (GET_CODE (expr) == SET);
1477 src = SET_SRC (expr);
1478 dest = SET_DEST (expr);
1480 if (GET_CODE (src) == REG)
1482 rtx rsi = reg_saved_in (src);
1483 if (rsi)
1484 src = rsi;
1487 switch (GET_CODE (dest))
1489 case REG:
1490 switch (GET_CODE (src))
1492 /* Setting FP from SP. */
1493 case REG:
1494 if (cfa.reg == (unsigned) REGNO (src))
1496 /* Rule 1 */
1497 /* Update the CFA rule wrt SP or FP. Make sure src is
1498 relative to the current CFA register.
1500 We used to require that dest be either SP or FP, but the
1501 ARM copies SP to a temporary register, and from there to
1502 FP. So we just rely on the backends to only set
1503 RTX_FRAME_RELATED_P on appropriate insns. */
1504 cfa.reg = REGNO (dest);
1505 cfa_temp.reg = cfa.reg;
1506 cfa_temp.offset = cfa.offset;
1508 else
1510 /* Saving a register in a register. */
1511 gcc_assert (call_used_regs [REGNO (dest)]
1512 && (!fixed_regs [REGNO (dest)]
1513 /* For the SPARC and its register window. */
1514 || DWARF_FRAME_REGNUM (REGNO (src))
1515 == DWARF_FRAME_RETURN_COLUMN));
1516 queue_reg_save (label, src, dest, 0);
1518 break;
1520 case PLUS:
1521 case MINUS:
1522 case LO_SUM:
1523 if (dest == stack_pointer_rtx)
1525 /* Rule 2 */
1526 /* Adjusting SP. */
1527 switch (GET_CODE (XEXP (src, 1)))
1529 case CONST_INT:
1530 offset = INTVAL (XEXP (src, 1));
1531 break;
1532 case REG:
1533 gcc_assert ((unsigned) REGNO (XEXP (src, 1))
1534 == cfa_temp.reg);
1535 offset = cfa_temp.offset;
1536 break;
1537 default:
1538 gcc_unreachable ();
1541 if (XEXP (src, 0) == hard_frame_pointer_rtx)
1543 /* Restoring SP from FP in the epilogue. */
1544 gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
1545 cfa.reg = STACK_POINTER_REGNUM;
1547 else if (GET_CODE (src) == LO_SUM)
1548 /* Assume we've set the source reg of the LO_SUM from sp. */
1550 else
1551 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
1553 if (GET_CODE (src) != MINUS)
1554 offset = -offset;
1555 if (cfa.reg == STACK_POINTER_REGNUM)
1556 cfa.offset += offset;
1557 if (cfa_store.reg == STACK_POINTER_REGNUM)
1558 cfa_store.offset += offset;
1560 else if (dest == hard_frame_pointer_rtx)
1562 /* Rule 3 */
1563 /* Either setting the FP from an offset of the SP,
1564 or adjusting the FP */
1565 gcc_assert (frame_pointer_needed);
1567 gcc_assert (REG_P (XEXP (src, 0))
1568 && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
1569 && GET_CODE (XEXP (src, 1)) == CONST_INT);
1570 offset = INTVAL (XEXP (src, 1));
1571 if (GET_CODE (src) != MINUS)
1572 offset = -offset;
1573 cfa.offset += offset;
1574 cfa.reg = HARD_FRAME_POINTER_REGNUM;
1576 else
1578 gcc_assert (GET_CODE (src) != MINUS);
1580 /* Rule 4 */
1581 if (REG_P (XEXP (src, 0))
1582 && REGNO (XEXP (src, 0)) == cfa.reg
1583 && GET_CODE (XEXP (src, 1)) == CONST_INT)
1585 /* Setting a temporary CFA register that will be copied
1586 into the FP later on. */
1587 offset = - INTVAL (XEXP (src, 1));
1588 cfa.offset += offset;
1589 cfa.reg = REGNO (dest);
1590 /* Or used to save regs to the stack. */
1591 cfa_temp.reg = cfa.reg;
1592 cfa_temp.offset = cfa.offset;
1595 /* Rule 5 */
1596 else if (REG_P (XEXP (src, 0))
1597 && REGNO (XEXP (src, 0)) == cfa_temp.reg
1598 && XEXP (src, 1) == stack_pointer_rtx)
1600 /* Setting a scratch register that we will use instead
1601 of SP for saving registers to the stack. */
1602 gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
1603 cfa_store.reg = REGNO (dest);
1604 cfa_store.offset = cfa.offset - cfa_temp.offset;
1607 /* Rule 9 */
1608 else if (GET_CODE (src) == LO_SUM
1609 && GET_CODE (XEXP (src, 1)) == CONST_INT)
1611 cfa_temp.reg = REGNO (dest);
1612 cfa_temp.offset = INTVAL (XEXP (src, 1));
1614 else
1615 gcc_unreachable ();
1617 break;
1619 /* Rule 6 */
1620 case CONST_INT:
1621 cfa_temp.reg = REGNO (dest);
1622 cfa_temp.offset = INTVAL (src);
1623 break;
1625 /* Rule 7 */
1626 case IOR:
1627 gcc_assert (REG_P (XEXP (src, 0))
1628 && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
1629 && GET_CODE (XEXP (src, 1)) == CONST_INT);
1631 if ((unsigned) REGNO (dest) != cfa_temp.reg)
1632 cfa_temp.reg = REGNO (dest);
1633 cfa_temp.offset |= INTVAL (XEXP (src, 1));
1634 break;
1636 /* Skip over HIGH, assuming it will be followed by a LO_SUM,
1637 which will fill in all of the bits. */
1638 /* Rule 8 */
1639 case HIGH:
1640 break;
1642 /* Rule 15 */
1643 case UNSPEC:
1644 case UNSPEC_VOLATILE:
1645 gcc_assert (targetm.dwarf_handle_frame_unspec);
1646 targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
1647 break;
1649 default:
1650 gcc_unreachable ();
1653 def_cfa_1 (label, &cfa);
1654 break;
1656 case MEM:
1657 gcc_assert (REG_P (src));
1659 /* Saving a register to the stack. Make sure dest is relative to the
1660 CFA register. */
1661 switch (GET_CODE (XEXP (dest, 0)))
1663 /* Rule 10 */
1664 /* With a push. */
1665 case PRE_MODIFY:
1666 /* We can't handle variable size modifications. */
1667 gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
1668 == CONST_INT);
1669 offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
1671 gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1672 && cfa_store.reg == STACK_POINTER_REGNUM);
1674 cfa_store.offset += offset;
1675 if (cfa.reg == STACK_POINTER_REGNUM)
1676 cfa.offset = cfa_store.offset;
1678 offset = -cfa_store.offset;
1679 break;
1681 /* Rule 11 */
1682 case PRE_INC:
1683 case PRE_DEC:
1684 offset = GET_MODE_SIZE (GET_MODE (dest));
1685 if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
1686 offset = -offset;
1688 gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1689 && cfa_store.reg == STACK_POINTER_REGNUM);
1691 cfa_store.offset += offset;
1692 if (cfa.reg == STACK_POINTER_REGNUM)
1693 cfa.offset = cfa_store.offset;
1695 offset = -cfa_store.offset;
1696 break;
1698 /* Rule 12 */
1699 /* With an offset. */
1700 case PLUS:
1701 case MINUS:
1702 case LO_SUM:
1704 int regno;
1706 gcc_assert (GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT);
1707 offset = INTVAL (XEXP (XEXP (dest, 0), 1));
1708 if (GET_CODE (XEXP (dest, 0)) == MINUS)
1709 offset = -offset;
1711 regno = REGNO (XEXP (XEXP (dest, 0), 0));
1713 if (cfa_store.reg == (unsigned) regno)
1714 offset -= cfa_store.offset;
1715 else
1717 gcc_assert (cfa_temp.reg == (unsigned) regno);
1718 offset -= cfa_temp.offset;
1721 break;
1723 /* Rule 13 */
1724 /* Without an offset. */
1725 case REG:
1727 int regno = REGNO (XEXP (dest, 0));
1729 if (cfa_store.reg == (unsigned) regno)
1730 offset = -cfa_store.offset;
1731 else
1733 gcc_assert (cfa_temp.reg == (unsigned) regno);
1734 offset = -cfa_temp.offset;
1737 break;
1739 /* Rule 14 */
1740 case POST_INC:
1741 gcc_assert (cfa_temp.reg
1742 == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
1743 offset = -cfa_temp.offset;
1744 cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
1745 break;
1747 default:
1748 gcc_unreachable ();
1751 if (REGNO (src) != STACK_POINTER_REGNUM
1752 && REGNO (src) != HARD_FRAME_POINTER_REGNUM
1753 && (unsigned) REGNO (src) == cfa.reg)
1755 /* We're storing the current CFA reg into the stack. */
1757 if (cfa.offset == 0)
1759 /* If the source register is exactly the CFA, assume
1760 we're saving SP like any other register; this happens
1761 on the ARM. */
1762 def_cfa_1 (label, &cfa);
1763 queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
1764 break;
1766 else
1768 /* Otherwise, we'll need to look in the stack to
1769 calculate the CFA. */
1770 rtx x = XEXP (dest, 0);
1772 if (!REG_P (x))
1773 x = XEXP (x, 0);
1774 gcc_assert (REG_P (x));
1776 cfa.reg = REGNO (x);
1777 cfa.base_offset = offset;
1778 cfa.indirect = 1;
1779 def_cfa_1 (label, &cfa);
1780 break;
1784 def_cfa_1 (label, &cfa);
1785 queue_reg_save (label, src, NULL_RTX, offset);
1786 break;
1788 default:
1789 gcc_unreachable ();
1793 /* Record call frame debugging information for INSN, which either
1794 sets SP or FP (adjusting how we calculate the frame address) or saves a
1795 register to the stack. If INSN is NULL_RTX, initialize our state.
1797 If AFTER_P is false, we're being called before the insn is emitted,
1798 otherwise after. Call instructions get invoked twice. */
1800 void
1801 dwarf2out_frame_debug (rtx insn, bool after_p)
1803 const char *label;
1804 rtx src;
1806 if (insn == NULL_RTX)
1808 size_t i;
1810 /* Flush any queued register saves. */
1811 flush_queued_reg_saves ();
1813 /* Set up state for generating call frame debug info. */
1814 lookup_cfa (&cfa);
1815 gcc_assert (cfa.reg
1816 == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
1818 cfa.reg = STACK_POINTER_REGNUM;
1819 cfa_store = cfa;
1820 cfa_temp.reg = -1;
1821 cfa_temp.offset = 0;
1823 for (i = 0; i < num_regs_saved_in_regs; i++)
1825 regs_saved_in_regs[i].orig_reg = NULL_RTX;
1826 regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
1828 num_regs_saved_in_regs = 0;
1829 return;
1832 if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
1833 flush_queued_reg_saves ();
1835 if (! RTX_FRAME_RELATED_P (insn))
1837 if (!ACCUMULATE_OUTGOING_ARGS)
1838 dwarf2out_stack_adjust (insn, after_p);
1839 return;
1842 label = dwarf2out_cfi_label ();
1843 src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1844 if (src)
1845 insn = XEXP (src, 0);
1846 else
1847 insn = PATTERN (insn);
1849 dwarf2out_frame_debug_expr (insn, label);
1852 #endif
1854 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
1855 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
1856 (enum dwarf_call_frame_info cfi);
1858 static enum dw_cfi_oprnd_type
1859 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
1861 switch (cfi)
1863 case DW_CFA_nop:
1864 case DW_CFA_GNU_window_save:
1865 return dw_cfi_oprnd_unused;
1867 case DW_CFA_set_loc:
1868 case DW_CFA_advance_loc1:
1869 case DW_CFA_advance_loc2:
1870 case DW_CFA_advance_loc4:
1871 case DW_CFA_MIPS_advance_loc8:
1872 return dw_cfi_oprnd_addr;
1874 case DW_CFA_offset:
1875 case DW_CFA_offset_extended:
1876 case DW_CFA_def_cfa:
1877 case DW_CFA_offset_extended_sf:
1878 case DW_CFA_def_cfa_sf:
1879 case DW_CFA_restore_extended:
1880 case DW_CFA_undefined:
1881 case DW_CFA_same_value:
1882 case DW_CFA_def_cfa_register:
1883 case DW_CFA_register:
1884 return dw_cfi_oprnd_reg_num;
1886 case DW_CFA_def_cfa_offset:
1887 case DW_CFA_GNU_args_size:
1888 case DW_CFA_def_cfa_offset_sf:
1889 return dw_cfi_oprnd_offset;
1891 case DW_CFA_def_cfa_expression:
1892 case DW_CFA_expression:
1893 return dw_cfi_oprnd_loc;
1895 default:
1896 gcc_unreachable ();
1900 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
1901 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
1902 (enum dwarf_call_frame_info cfi);
1904 static enum dw_cfi_oprnd_type
1905 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
1907 switch (cfi)
1909 case DW_CFA_def_cfa:
1910 case DW_CFA_def_cfa_sf:
1911 case DW_CFA_offset:
1912 case DW_CFA_offset_extended_sf:
1913 case DW_CFA_offset_extended:
1914 return dw_cfi_oprnd_offset;
1916 case DW_CFA_register:
1917 return dw_cfi_oprnd_reg_num;
1919 default:
1920 return dw_cfi_oprnd_unused;
1924 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1926 /* Map register numbers held in the call frame info that gcc has
1927 collected using DWARF_FRAME_REGNUM to those that should be output in
1928 .debug_frame and .eh_frame. */
1929 #ifndef DWARF2_FRAME_REG_OUT
1930 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
1931 #endif
1933 /* Output a Call Frame Information opcode and its operand(s). */
1935 static void
1936 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
1938 unsigned long r;
1939 if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
1940 dw2_asm_output_data (1, (cfi->dw_cfi_opc
1941 | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
1942 "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
1943 cfi->dw_cfi_oprnd1.dw_cfi_offset);
1944 else if (cfi->dw_cfi_opc == DW_CFA_offset)
1946 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
1947 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
1948 "DW_CFA_offset, column 0x%lx", r);
1949 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
1951 else if (cfi->dw_cfi_opc == DW_CFA_restore)
1953 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
1954 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
1955 "DW_CFA_restore, column 0x%lx", r);
1957 else
1959 dw2_asm_output_data (1, cfi->dw_cfi_opc,
1960 "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
1962 switch (cfi->dw_cfi_opc)
1964 case DW_CFA_set_loc:
1965 if (for_eh)
1966 dw2_asm_output_encoded_addr_rtx (
1967 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
1968 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
1969 NULL);
1970 else
1971 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
1972 cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
1973 break;
1975 case DW_CFA_advance_loc1:
1976 dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
1977 fde->dw_fde_current_label, NULL);
1978 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
1979 break;
1981 case DW_CFA_advance_loc2:
1982 dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
1983 fde->dw_fde_current_label, NULL);
1984 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
1985 break;
1987 case DW_CFA_advance_loc4:
1988 dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
1989 fde->dw_fde_current_label, NULL);
1990 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
1991 break;
1993 case DW_CFA_MIPS_advance_loc8:
1994 dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
1995 fde->dw_fde_current_label, NULL);
1996 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
1997 break;
1999 case DW_CFA_offset_extended:
2000 case DW_CFA_def_cfa:
2001 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2002 dw2_asm_output_data_uleb128 (r, NULL);
2003 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2004 break;
2006 case DW_CFA_offset_extended_sf:
2007 case DW_CFA_def_cfa_sf:
2008 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2009 dw2_asm_output_data_uleb128 (r, NULL);
2010 dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2011 break;
2013 case DW_CFA_restore_extended:
2014 case DW_CFA_undefined:
2015 case DW_CFA_same_value:
2016 case DW_CFA_def_cfa_register:
2017 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2018 dw2_asm_output_data_uleb128 (r, NULL);
2019 break;
2021 case DW_CFA_register:
2022 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2023 dw2_asm_output_data_uleb128 (r, NULL);
2024 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
2025 dw2_asm_output_data_uleb128 (r, NULL);
2026 break;
2028 case DW_CFA_def_cfa_offset:
2029 case DW_CFA_GNU_args_size:
2030 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2031 break;
2033 case DW_CFA_def_cfa_offset_sf:
2034 dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2035 break;
2037 case DW_CFA_GNU_window_save:
2038 break;
2040 case DW_CFA_def_cfa_expression:
2041 case DW_CFA_expression:
2042 output_cfa_loc (cfi);
2043 break;
2045 case DW_CFA_GNU_negative_offset_extended:
2046 /* Obsoleted by DW_CFA_offset_extended_sf. */
2047 gcc_unreachable ();
2049 default:
2050 break;
2055 /* Output the call frame information used to record information
2056 that relates to calculating the frame pointer, and records the
2057 location of saved registers. */
2059 static void
2060 output_call_frame_info (int for_eh)
2062 unsigned int i;
2063 dw_fde_ref fde;
2064 dw_cfi_ref cfi;
2065 char l1[20], l2[20], section_start_label[20];
2066 bool any_lsda_needed = false;
2067 char augmentation[6];
2068 int augmentation_size;
2069 int fde_encoding = DW_EH_PE_absptr;
2070 int per_encoding = DW_EH_PE_absptr;
2071 int lsda_encoding = DW_EH_PE_absptr;
2073 /* Don't emit a CIE if there won't be any FDEs. */
2074 if (fde_table_in_use == 0)
2075 return;
2077 /* If we make FDEs linkonce, we may have to emit an empty label for
2078 an FDE that wouldn't otherwise be emitted. We want to avoid
2079 having an FDE kept around when the function it refers to is
2080 discarded. Example where this matters: a primary function
2081 template in C++ requires EH information, but an explicit
2082 specialization doesn't. */
2083 if (TARGET_USES_WEAK_UNWIND_INFO
2084 && ! flag_asynchronous_unwind_tables
2085 && for_eh)
2086 for (i = 0; i < fde_table_in_use; i++)
2087 if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
2088 && !fde_table[i].uses_eh_lsda
2089 && ! DECL_WEAK (fde_table[i].decl))
2090 targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
2091 for_eh, /* empty */ 1);
2093 /* If we don't have any functions we'll want to unwind out of, don't
2094 emit any EH unwind information. Note that if exceptions aren't
2095 enabled, we won't have collected nothrow information, and if we
2096 asked for asynchronous tables, we always want this info. */
2097 if (for_eh)
2099 bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
2101 for (i = 0; i < fde_table_in_use; i++)
2102 if (fde_table[i].uses_eh_lsda)
2103 any_eh_needed = any_lsda_needed = true;
2104 else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2105 any_eh_needed = true;
2106 else if (! fde_table[i].nothrow
2107 && ! fde_table[i].all_throwers_are_sibcalls)
2108 any_eh_needed = true;
2110 if (! any_eh_needed)
2111 return;
2114 /* We're going to be generating comments, so turn on app. */
2115 if (flag_debug_asm)
2116 app_enable ();
2118 if (for_eh)
2119 targetm.asm_out.eh_frame_section ();
2120 else
2121 named_section_flags (DEBUG_FRAME_SECTION, SECTION_DEBUG);
2123 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
2124 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
2126 /* Output the CIE. */
2127 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
2128 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
2129 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2130 "Length of Common Information Entry");
2131 ASM_OUTPUT_LABEL (asm_out_file, l1);
2133 /* Now that the CIE pointer is PC-relative for EH,
2134 use 0 to identify the CIE. */
2135 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
2136 (for_eh ? 0 : DW_CIE_ID),
2137 "CIE Identifier Tag");
2139 dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
2141 augmentation[0] = 0;
2142 augmentation_size = 0;
2143 if (for_eh)
2145 char *p;
2147 /* Augmentation:
2148 z Indicates that a uleb128 is present to size the
2149 augmentation section.
2150 L Indicates the encoding (and thus presence) of
2151 an LSDA pointer in the FDE augmentation.
2152 R Indicates a non-default pointer encoding for
2153 FDE code pointers.
2154 P Indicates the presence of an encoding + language
2155 personality routine in the CIE augmentation. */
2157 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
2158 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
2159 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
2161 p = augmentation + 1;
2162 if (eh_personality_libfunc)
2164 *p++ = 'P';
2165 augmentation_size += 1 + size_of_encoded_value (per_encoding);
2167 if (any_lsda_needed)
2169 *p++ = 'L';
2170 augmentation_size += 1;
2172 if (fde_encoding != DW_EH_PE_absptr)
2174 *p++ = 'R';
2175 augmentation_size += 1;
2177 if (p > augmentation + 1)
2179 augmentation[0] = 'z';
2180 *p = '\0';
2183 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
2184 if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
2186 int offset = ( 4 /* Length */
2187 + 4 /* CIE Id */
2188 + 1 /* CIE version */
2189 + strlen (augmentation) + 1 /* Augmentation */
2190 + size_of_uleb128 (1) /* Code alignment */
2191 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
2192 + 1 /* RA column */
2193 + 1 /* Augmentation size */
2194 + 1 /* Personality encoding */ );
2195 int pad = -offset & (PTR_SIZE - 1);
2197 augmentation_size += pad;
2199 /* Augmentations should be small, so there's scarce need to
2200 iterate for a solution. Die if we exceed one uleb128 byte. */
2201 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
2205 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
2206 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
2207 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
2208 "CIE Data Alignment Factor");
2210 if (DW_CIE_VERSION == 1)
2211 dw2_asm_output_data (1, DWARF_FRAME_RETURN_COLUMN, "CIE RA Column");
2212 else
2213 dw2_asm_output_data_uleb128 (DWARF_FRAME_RETURN_COLUMN, "CIE RA Column");
2215 if (augmentation[0])
2217 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
2218 if (eh_personality_libfunc)
2220 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
2221 eh_data_format_name (per_encoding));
2222 dw2_asm_output_encoded_addr_rtx (per_encoding,
2223 eh_personality_libfunc, NULL);
2226 if (any_lsda_needed)
2227 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
2228 eh_data_format_name (lsda_encoding));
2230 if (fde_encoding != DW_EH_PE_absptr)
2231 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
2232 eh_data_format_name (fde_encoding));
2235 for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
2236 output_cfi (cfi, NULL, for_eh);
2238 /* Pad the CIE out to an address sized boundary. */
2239 ASM_OUTPUT_ALIGN (asm_out_file,
2240 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
2241 ASM_OUTPUT_LABEL (asm_out_file, l2);
2243 /* Loop through all of the FDE's. */
2244 for (i = 0; i < fde_table_in_use; i++)
2246 fde = &fde_table[i];
2248 /* Don't emit EH unwind info for leaf functions that don't need it. */
2249 if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
2250 && (fde->nothrow || fde->all_throwers_are_sibcalls)
2251 && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2252 && !fde->uses_eh_lsda)
2253 continue;
2255 targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
2256 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
2257 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
2258 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
2259 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2260 "FDE Length");
2261 ASM_OUTPUT_LABEL (asm_out_file, l1);
2263 if (for_eh)
2264 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
2265 else
2266 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
2267 "FDE CIE offset");
2269 if (for_eh)
2271 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
2272 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
2273 dw2_asm_output_encoded_addr_rtx (fde_encoding,
2274 sym_ref,
2275 "FDE initial location");
2276 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2277 fde->dw_fde_end, fde->dw_fde_begin,
2278 "FDE address range");
2280 else
2282 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
2283 "FDE initial location");
2284 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2285 fde->dw_fde_end, fde->dw_fde_begin,
2286 "FDE address range");
2289 if (augmentation[0])
2291 if (any_lsda_needed)
2293 int size = size_of_encoded_value (lsda_encoding);
2295 if (lsda_encoding == DW_EH_PE_aligned)
2297 int offset = ( 4 /* Length */
2298 + 4 /* CIE offset */
2299 + 2 * size_of_encoded_value (fde_encoding)
2300 + 1 /* Augmentation size */ );
2301 int pad = -offset & (PTR_SIZE - 1);
2303 size += pad;
2304 gcc_assert (size_of_uleb128 (size) == 1);
2307 dw2_asm_output_data_uleb128 (size, "Augmentation size");
2309 if (fde->uses_eh_lsda)
2311 ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
2312 fde->funcdef_number);
2313 dw2_asm_output_encoded_addr_rtx (
2314 lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
2315 "Language Specific Data Area");
2317 else
2319 if (lsda_encoding == DW_EH_PE_aligned)
2320 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2321 dw2_asm_output_data
2322 (size_of_encoded_value (lsda_encoding), 0,
2323 "Language Specific Data Area (none)");
2326 else
2327 dw2_asm_output_data_uleb128 (0, "Augmentation size");
2330 /* Loop through the Call Frame Instructions associated with
2331 this FDE. */
2332 fde->dw_fde_current_label = fde->dw_fde_begin;
2333 for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
2334 output_cfi (cfi, fde, for_eh);
2336 /* Pad the FDE out to an address sized boundary. */
2337 ASM_OUTPUT_ALIGN (asm_out_file,
2338 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
2339 ASM_OUTPUT_LABEL (asm_out_file, l2);
2342 if (for_eh && targetm.terminate_dw2_eh_frame_info)
2343 dw2_asm_output_data (4, 0, "End of Table");
2344 #ifdef MIPS_DEBUGGING_INFO
2345 /* Work around Irix 6 assembler bug whereby labels at the end of a section
2346 get a value of 0. Putting .align 0 after the label fixes it. */
2347 ASM_OUTPUT_ALIGN (asm_out_file, 0);
2348 #endif
2350 /* Turn off app to make assembly quicker. */
2351 if (flag_debug_asm)
2352 app_disable ();
2355 /* Output a marker (i.e. a label) for the beginning of a function, before
2356 the prologue. */
2358 void
2359 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
2360 const char *file ATTRIBUTE_UNUSED)
2362 char label[MAX_ARTIFICIAL_LABEL_BYTES];
2363 char * dup_label;
2364 dw_fde_ref fde;
2366 current_function_func_begin_label = NULL;
2368 #ifdef TARGET_UNWIND_INFO
2369 /* ??? current_function_func_begin_label is also used by except.c
2370 for call-site information. We must emit this label if it might
2371 be used. */
2372 if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
2373 && ! dwarf2out_do_frame ())
2374 return;
2375 #else
2376 if (! dwarf2out_do_frame ())
2377 return;
2378 #endif
2380 function_section (current_function_decl);
2381 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
2382 current_function_funcdef_no);
2383 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
2384 current_function_funcdef_no);
2385 dup_label = xstrdup (label);
2386 current_function_func_begin_label = dup_label;
2388 #ifdef TARGET_UNWIND_INFO
2389 /* We can elide the fde allocation if we're not emitting debug info. */
2390 if (! dwarf2out_do_frame ())
2391 return;
2392 #endif
2394 /* Expand the fde table if necessary. */
2395 if (fde_table_in_use == fde_table_allocated)
2397 fde_table_allocated += FDE_TABLE_INCREMENT;
2398 fde_table = ggc_realloc (fde_table,
2399 fde_table_allocated * sizeof (dw_fde_node));
2400 memset (fde_table + fde_table_in_use, 0,
2401 FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2404 /* Record the FDE associated with this function. */
2405 current_funcdef_fde = fde_table_in_use;
2407 /* Add the new FDE at the end of the fde_table. */
2408 fde = &fde_table[fde_table_in_use++];
2409 fde->decl = current_function_decl;
2410 fde->dw_fde_begin = dup_label;
2411 fde->dw_fde_current_label = NULL;
2412 fde->dw_fde_end = NULL;
2413 fde->dw_fde_cfi = NULL;
2414 fde->funcdef_number = current_function_funcdef_no;
2415 fde->nothrow = TREE_NOTHROW (current_function_decl);
2416 fde->uses_eh_lsda = cfun->uses_eh_lsda;
2417 fde->all_throwers_are_sibcalls = cfun->all_throwers_are_sibcalls;
2419 args_size = old_args_size = 0;
2421 /* We only want to output line number information for the genuine dwarf2
2422 prologue case, not the eh frame case. */
2423 #ifdef DWARF2_DEBUGGING_INFO
2424 if (file)
2425 dwarf2out_source_line (line, file);
2426 #endif
2429 /* Output a marker (i.e. a label) for the absolute end of the generated code
2430 for a function definition. This gets called *after* the epilogue code has
2431 been generated. */
2433 void
2434 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
2435 const char *file ATTRIBUTE_UNUSED)
2437 dw_fde_ref fde;
2438 char label[MAX_ARTIFICIAL_LABEL_BYTES];
2440 /* Output a label to mark the endpoint of the code generated for this
2441 function. */
2442 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
2443 current_function_funcdef_no);
2444 ASM_OUTPUT_LABEL (asm_out_file, label);
2445 fde = &fde_table[fde_table_in_use - 1];
2446 fde->dw_fde_end = xstrdup (label);
2449 void
2450 dwarf2out_frame_init (void)
2452 /* Allocate the initial hunk of the fde_table. */
2453 fde_table = ggc_alloc_cleared (FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2454 fde_table_allocated = FDE_TABLE_INCREMENT;
2455 fde_table_in_use = 0;
2457 /* Generate the CFA instructions common to all FDE's. Do it now for the
2458 sake of lookup_cfa. */
2460 #ifdef DWARF2_UNWIND_INFO
2461 /* On entry, the Canonical Frame Address is at SP. */
2462 dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
2463 initial_return_save (INCOMING_RETURN_ADDR_RTX);
2464 #endif
2467 void
2468 dwarf2out_frame_finish (void)
2470 /* Output call frame information. */
2471 if (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
2472 output_call_frame_info (0);
2474 #ifndef TARGET_UNWIND_INFO
2475 /* Output another copy for the unwinder. */
2476 if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
2477 output_call_frame_info (1);
2478 #endif
2480 #endif
2482 /* And now, the subset of the debugging information support code necessary
2483 for emitting location expressions. */
2485 /* We need some way to distinguish DW_OP_addr with a direct symbol
2486 relocation from DW_OP_addr with a dtp-relative symbol relocation. */
2487 #define INTERNAL_DW_OP_tls_addr (0x100 + DW_OP_addr)
2490 typedef struct dw_val_struct *dw_val_ref;
2491 typedef struct die_struct *dw_die_ref;
2492 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
2493 typedef struct dw_loc_list_struct *dw_loc_list_ref;
2495 /* Each DIE may have a series of attribute/value pairs. Values
2496 can take on several forms. The forms that are used in this
2497 implementation are listed below. */
2499 enum dw_val_class
2501 dw_val_class_addr,
2502 dw_val_class_offset,
2503 dw_val_class_loc,
2504 dw_val_class_loc_list,
2505 dw_val_class_range_list,
2506 dw_val_class_const,
2507 dw_val_class_unsigned_const,
2508 dw_val_class_long_long,
2509 dw_val_class_vec,
2510 dw_val_class_flag,
2511 dw_val_class_die_ref,
2512 dw_val_class_fde_ref,
2513 dw_val_class_lbl_id,
2514 dw_val_class_lbl_offset,
2515 dw_val_class_str
2518 /* Describe a double word constant value. */
2519 /* ??? Every instance of long_long in the code really means CONST_DOUBLE. */
2521 typedef struct dw_long_long_struct GTY(())
2523 unsigned long hi;
2524 unsigned long low;
2526 dw_long_long_const;
2528 /* Describe a floating point constant value, or a vector constant value. */
2530 typedef struct dw_vec_struct GTY(())
2532 unsigned char * GTY((length ("%h.length"))) array;
2533 unsigned length;
2534 unsigned elt_size;
2536 dw_vec_const;
2538 /* The dw_val_node describes an attribute's value, as it is
2539 represented internally. */
2541 typedef struct dw_val_struct GTY(())
2543 enum dw_val_class val_class;
2544 union dw_val_struct_union
2546 rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
2547 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
2548 dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
2549 dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
2550 HOST_WIDE_INT GTY ((default)) val_int;
2551 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
2552 dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
2553 dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
2554 struct dw_val_die_union
2556 dw_die_ref die;
2557 int external;
2558 } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
2559 unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
2560 struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
2561 char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
2562 unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
2564 GTY ((desc ("%1.val_class"))) v;
2566 dw_val_node;
2568 /* Locations in memory are described using a sequence of stack machine
2569 operations. */
2571 typedef struct dw_loc_descr_struct GTY(())
2573 dw_loc_descr_ref dw_loc_next;
2574 enum dwarf_location_atom dw_loc_opc;
2575 dw_val_node dw_loc_oprnd1;
2576 dw_val_node dw_loc_oprnd2;
2577 int dw_loc_addr;
2579 dw_loc_descr_node;
2581 /* Location lists are ranges + location descriptions for that range,
2582 so you can track variables that are in different places over
2583 their entire life. */
2584 typedef struct dw_loc_list_struct GTY(())
2586 dw_loc_list_ref dw_loc_next;
2587 const char *begin; /* Label for begin address of range */
2588 const char *end; /* Label for end address of range */
2589 char *ll_symbol; /* Label for beginning of location list.
2590 Only on head of list */
2591 const char *section; /* Section this loclist is relative to */
2592 dw_loc_descr_ref expr;
2593 } dw_loc_list_node;
2595 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2597 static const char *dwarf_stack_op_name (unsigned);
2598 static dw_loc_descr_ref new_loc_descr (enum dwarf_location_atom,
2599 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2600 static void add_loc_descr (dw_loc_descr_ref *, dw_loc_descr_ref);
2601 static unsigned long size_of_loc_descr (dw_loc_descr_ref);
2602 static unsigned long size_of_locs (dw_loc_descr_ref);
2603 static void output_loc_operands (dw_loc_descr_ref);
2604 static void output_loc_sequence (dw_loc_descr_ref);
2606 /* Convert a DWARF stack opcode into its string name. */
2608 static const char *
2609 dwarf_stack_op_name (unsigned int op)
2611 switch (op)
2613 case DW_OP_addr:
2614 case INTERNAL_DW_OP_tls_addr:
2615 return "DW_OP_addr";
2616 case DW_OP_deref:
2617 return "DW_OP_deref";
2618 case DW_OP_const1u:
2619 return "DW_OP_const1u";
2620 case DW_OP_const1s:
2621 return "DW_OP_const1s";
2622 case DW_OP_const2u:
2623 return "DW_OP_const2u";
2624 case DW_OP_const2s:
2625 return "DW_OP_const2s";
2626 case DW_OP_const4u:
2627 return "DW_OP_const4u";
2628 case DW_OP_const4s:
2629 return "DW_OP_const4s";
2630 case DW_OP_const8u:
2631 return "DW_OP_const8u";
2632 case DW_OP_const8s:
2633 return "DW_OP_const8s";
2634 case DW_OP_constu:
2635 return "DW_OP_constu";
2636 case DW_OP_consts:
2637 return "DW_OP_consts";
2638 case DW_OP_dup:
2639 return "DW_OP_dup";
2640 case DW_OP_drop:
2641 return "DW_OP_drop";
2642 case DW_OP_over:
2643 return "DW_OP_over";
2644 case DW_OP_pick:
2645 return "DW_OP_pick";
2646 case DW_OP_swap:
2647 return "DW_OP_swap";
2648 case DW_OP_rot:
2649 return "DW_OP_rot";
2650 case DW_OP_xderef:
2651 return "DW_OP_xderef";
2652 case DW_OP_abs:
2653 return "DW_OP_abs";
2654 case DW_OP_and:
2655 return "DW_OP_and";
2656 case DW_OP_div:
2657 return "DW_OP_div";
2658 case DW_OP_minus:
2659 return "DW_OP_minus";
2660 case DW_OP_mod:
2661 return "DW_OP_mod";
2662 case DW_OP_mul:
2663 return "DW_OP_mul";
2664 case DW_OP_neg:
2665 return "DW_OP_neg";
2666 case DW_OP_not:
2667 return "DW_OP_not";
2668 case DW_OP_or:
2669 return "DW_OP_or";
2670 case DW_OP_plus:
2671 return "DW_OP_plus";
2672 case DW_OP_plus_uconst:
2673 return "DW_OP_plus_uconst";
2674 case DW_OP_shl:
2675 return "DW_OP_shl";
2676 case DW_OP_shr:
2677 return "DW_OP_shr";
2678 case DW_OP_shra:
2679 return "DW_OP_shra";
2680 case DW_OP_xor:
2681 return "DW_OP_xor";
2682 case DW_OP_bra:
2683 return "DW_OP_bra";
2684 case DW_OP_eq:
2685 return "DW_OP_eq";
2686 case DW_OP_ge:
2687 return "DW_OP_ge";
2688 case DW_OP_gt:
2689 return "DW_OP_gt";
2690 case DW_OP_le:
2691 return "DW_OP_le";
2692 case DW_OP_lt:
2693 return "DW_OP_lt";
2694 case DW_OP_ne:
2695 return "DW_OP_ne";
2696 case DW_OP_skip:
2697 return "DW_OP_skip";
2698 case DW_OP_lit0:
2699 return "DW_OP_lit0";
2700 case DW_OP_lit1:
2701 return "DW_OP_lit1";
2702 case DW_OP_lit2:
2703 return "DW_OP_lit2";
2704 case DW_OP_lit3:
2705 return "DW_OP_lit3";
2706 case DW_OP_lit4:
2707 return "DW_OP_lit4";
2708 case DW_OP_lit5:
2709 return "DW_OP_lit5";
2710 case DW_OP_lit6:
2711 return "DW_OP_lit6";
2712 case DW_OP_lit7:
2713 return "DW_OP_lit7";
2714 case DW_OP_lit8:
2715 return "DW_OP_lit8";
2716 case DW_OP_lit9:
2717 return "DW_OP_lit9";
2718 case DW_OP_lit10:
2719 return "DW_OP_lit10";
2720 case DW_OP_lit11:
2721 return "DW_OP_lit11";
2722 case DW_OP_lit12:
2723 return "DW_OP_lit12";
2724 case DW_OP_lit13:
2725 return "DW_OP_lit13";
2726 case DW_OP_lit14:
2727 return "DW_OP_lit14";
2728 case DW_OP_lit15:
2729 return "DW_OP_lit15";
2730 case DW_OP_lit16:
2731 return "DW_OP_lit16";
2732 case DW_OP_lit17:
2733 return "DW_OP_lit17";
2734 case DW_OP_lit18:
2735 return "DW_OP_lit18";
2736 case DW_OP_lit19:
2737 return "DW_OP_lit19";
2738 case DW_OP_lit20:
2739 return "DW_OP_lit20";
2740 case DW_OP_lit21:
2741 return "DW_OP_lit21";
2742 case DW_OP_lit22:
2743 return "DW_OP_lit22";
2744 case DW_OP_lit23:
2745 return "DW_OP_lit23";
2746 case DW_OP_lit24:
2747 return "DW_OP_lit24";
2748 case DW_OP_lit25:
2749 return "DW_OP_lit25";
2750 case DW_OP_lit26:
2751 return "DW_OP_lit26";
2752 case DW_OP_lit27:
2753 return "DW_OP_lit27";
2754 case DW_OP_lit28:
2755 return "DW_OP_lit28";
2756 case DW_OP_lit29:
2757 return "DW_OP_lit29";
2758 case DW_OP_lit30:
2759 return "DW_OP_lit30";
2760 case DW_OP_lit31:
2761 return "DW_OP_lit31";
2762 case DW_OP_reg0:
2763 return "DW_OP_reg0";
2764 case DW_OP_reg1:
2765 return "DW_OP_reg1";
2766 case DW_OP_reg2:
2767 return "DW_OP_reg2";
2768 case DW_OP_reg3:
2769 return "DW_OP_reg3";
2770 case DW_OP_reg4:
2771 return "DW_OP_reg4";
2772 case DW_OP_reg5:
2773 return "DW_OP_reg5";
2774 case DW_OP_reg6:
2775 return "DW_OP_reg6";
2776 case DW_OP_reg7:
2777 return "DW_OP_reg7";
2778 case DW_OP_reg8:
2779 return "DW_OP_reg8";
2780 case DW_OP_reg9:
2781 return "DW_OP_reg9";
2782 case DW_OP_reg10:
2783 return "DW_OP_reg10";
2784 case DW_OP_reg11:
2785 return "DW_OP_reg11";
2786 case DW_OP_reg12:
2787 return "DW_OP_reg12";
2788 case DW_OP_reg13:
2789 return "DW_OP_reg13";
2790 case DW_OP_reg14:
2791 return "DW_OP_reg14";
2792 case DW_OP_reg15:
2793 return "DW_OP_reg15";
2794 case DW_OP_reg16:
2795 return "DW_OP_reg16";
2796 case DW_OP_reg17:
2797 return "DW_OP_reg17";
2798 case DW_OP_reg18:
2799 return "DW_OP_reg18";
2800 case DW_OP_reg19:
2801 return "DW_OP_reg19";
2802 case DW_OP_reg20:
2803 return "DW_OP_reg20";
2804 case DW_OP_reg21:
2805 return "DW_OP_reg21";
2806 case DW_OP_reg22:
2807 return "DW_OP_reg22";
2808 case DW_OP_reg23:
2809 return "DW_OP_reg23";
2810 case DW_OP_reg24:
2811 return "DW_OP_reg24";
2812 case DW_OP_reg25:
2813 return "DW_OP_reg25";
2814 case DW_OP_reg26:
2815 return "DW_OP_reg26";
2816 case DW_OP_reg27:
2817 return "DW_OP_reg27";
2818 case DW_OP_reg28:
2819 return "DW_OP_reg28";
2820 case DW_OP_reg29:
2821 return "DW_OP_reg29";
2822 case DW_OP_reg30:
2823 return "DW_OP_reg30";
2824 case DW_OP_reg31:
2825 return "DW_OP_reg31";
2826 case DW_OP_breg0:
2827 return "DW_OP_breg0";
2828 case DW_OP_breg1:
2829 return "DW_OP_breg1";
2830 case DW_OP_breg2:
2831 return "DW_OP_breg2";
2832 case DW_OP_breg3:
2833 return "DW_OP_breg3";
2834 case DW_OP_breg4:
2835 return "DW_OP_breg4";
2836 case DW_OP_breg5:
2837 return "DW_OP_breg5";
2838 case DW_OP_breg6:
2839 return "DW_OP_breg6";
2840 case DW_OP_breg7:
2841 return "DW_OP_breg7";
2842 case DW_OP_breg8:
2843 return "DW_OP_breg8";
2844 case DW_OP_breg9:
2845 return "DW_OP_breg9";
2846 case DW_OP_breg10:
2847 return "DW_OP_breg10";
2848 case DW_OP_breg11:
2849 return "DW_OP_breg11";
2850 case DW_OP_breg12:
2851 return "DW_OP_breg12";
2852 case DW_OP_breg13:
2853 return "DW_OP_breg13";
2854 case DW_OP_breg14:
2855 return "DW_OP_breg14";
2856 case DW_OP_breg15:
2857 return "DW_OP_breg15";
2858 case DW_OP_breg16:
2859 return "DW_OP_breg16";
2860 case DW_OP_breg17:
2861 return "DW_OP_breg17";
2862 case DW_OP_breg18:
2863 return "DW_OP_breg18";
2864 case DW_OP_breg19:
2865 return "DW_OP_breg19";
2866 case DW_OP_breg20:
2867 return "DW_OP_breg20";
2868 case DW_OP_breg21:
2869 return "DW_OP_breg21";
2870 case DW_OP_breg22:
2871 return "DW_OP_breg22";
2872 case DW_OP_breg23:
2873 return "DW_OP_breg23";
2874 case DW_OP_breg24:
2875 return "DW_OP_breg24";
2876 case DW_OP_breg25:
2877 return "DW_OP_breg25";
2878 case DW_OP_breg26:
2879 return "DW_OP_breg26";
2880 case DW_OP_breg27:
2881 return "DW_OP_breg27";
2882 case DW_OP_breg28:
2883 return "DW_OP_breg28";
2884 case DW_OP_breg29:
2885 return "DW_OP_breg29";
2886 case DW_OP_breg30:
2887 return "DW_OP_breg30";
2888 case DW_OP_breg31:
2889 return "DW_OP_breg31";
2890 case DW_OP_regx:
2891 return "DW_OP_regx";
2892 case DW_OP_fbreg:
2893 return "DW_OP_fbreg";
2894 case DW_OP_bregx:
2895 return "DW_OP_bregx";
2896 case DW_OP_piece:
2897 return "DW_OP_piece";
2898 case DW_OP_deref_size:
2899 return "DW_OP_deref_size";
2900 case DW_OP_xderef_size:
2901 return "DW_OP_xderef_size";
2902 case DW_OP_nop:
2903 return "DW_OP_nop";
2904 case DW_OP_push_object_address:
2905 return "DW_OP_push_object_address";
2906 case DW_OP_call2:
2907 return "DW_OP_call2";
2908 case DW_OP_call4:
2909 return "DW_OP_call4";
2910 case DW_OP_call_ref:
2911 return "DW_OP_call_ref";
2912 case DW_OP_GNU_push_tls_address:
2913 return "DW_OP_GNU_push_tls_address";
2914 default:
2915 return "OP_<unknown>";
2919 /* Return a pointer to a newly allocated location description. Location
2920 descriptions are simple expression terms that can be strung
2921 together to form more complicated location (address) descriptions. */
2923 static inline dw_loc_descr_ref
2924 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
2925 unsigned HOST_WIDE_INT oprnd2)
2927 dw_loc_descr_ref descr = ggc_alloc_cleared (sizeof (dw_loc_descr_node));
2929 descr->dw_loc_opc = op;
2930 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
2931 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
2932 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
2933 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
2935 return descr;
2939 /* Add a location description term to a location description expression. */
2941 static inline void
2942 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
2944 dw_loc_descr_ref *d;
2946 /* Find the end of the chain. */
2947 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
2950 *d = descr;
2953 /* Return the size of a location descriptor. */
2955 static unsigned long
2956 size_of_loc_descr (dw_loc_descr_ref loc)
2958 unsigned long size = 1;
2960 switch (loc->dw_loc_opc)
2962 case DW_OP_addr:
2963 case INTERNAL_DW_OP_tls_addr:
2964 size += DWARF2_ADDR_SIZE;
2965 break;
2966 case DW_OP_const1u:
2967 case DW_OP_const1s:
2968 size += 1;
2969 break;
2970 case DW_OP_const2u:
2971 case DW_OP_const2s:
2972 size += 2;
2973 break;
2974 case DW_OP_const4u:
2975 case DW_OP_const4s:
2976 size += 4;
2977 break;
2978 case DW_OP_const8u:
2979 case DW_OP_const8s:
2980 size += 8;
2981 break;
2982 case DW_OP_constu:
2983 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2984 break;
2985 case DW_OP_consts:
2986 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
2987 break;
2988 case DW_OP_pick:
2989 size += 1;
2990 break;
2991 case DW_OP_plus_uconst:
2992 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2993 break;
2994 case DW_OP_skip:
2995 case DW_OP_bra:
2996 size += 2;
2997 break;
2998 case DW_OP_breg0:
2999 case DW_OP_breg1:
3000 case DW_OP_breg2:
3001 case DW_OP_breg3:
3002 case DW_OP_breg4:
3003 case DW_OP_breg5:
3004 case DW_OP_breg6:
3005 case DW_OP_breg7:
3006 case DW_OP_breg8:
3007 case DW_OP_breg9:
3008 case DW_OP_breg10:
3009 case DW_OP_breg11:
3010 case DW_OP_breg12:
3011 case DW_OP_breg13:
3012 case DW_OP_breg14:
3013 case DW_OP_breg15:
3014 case DW_OP_breg16:
3015 case DW_OP_breg17:
3016 case DW_OP_breg18:
3017 case DW_OP_breg19:
3018 case DW_OP_breg20:
3019 case DW_OP_breg21:
3020 case DW_OP_breg22:
3021 case DW_OP_breg23:
3022 case DW_OP_breg24:
3023 case DW_OP_breg25:
3024 case DW_OP_breg26:
3025 case DW_OP_breg27:
3026 case DW_OP_breg28:
3027 case DW_OP_breg29:
3028 case DW_OP_breg30:
3029 case DW_OP_breg31:
3030 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3031 break;
3032 case DW_OP_regx:
3033 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3034 break;
3035 case DW_OP_fbreg:
3036 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3037 break;
3038 case DW_OP_bregx:
3039 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3040 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
3041 break;
3042 case DW_OP_piece:
3043 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3044 break;
3045 case DW_OP_deref_size:
3046 case DW_OP_xderef_size:
3047 size += 1;
3048 break;
3049 case DW_OP_call2:
3050 size += 2;
3051 break;
3052 case DW_OP_call4:
3053 size += 4;
3054 break;
3055 case DW_OP_call_ref:
3056 size += DWARF2_ADDR_SIZE;
3057 break;
3058 default:
3059 break;
3062 return size;
3065 /* Return the size of a series of location descriptors. */
3067 static unsigned long
3068 size_of_locs (dw_loc_descr_ref loc)
3070 unsigned long size;
3072 for (size = 0; loc != NULL; loc = loc->dw_loc_next)
3074 loc->dw_loc_addr = size;
3075 size += size_of_loc_descr (loc);
3078 return size;
3081 /* Output location description stack opcode's operands (if any). */
3083 static void
3084 output_loc_operands (dw_loc_descr_ref loc)
3086 dw_val_ref val1 = &loc->dw_loc_oprnd1;
3087 dw_val_ref val2 = &loc->dw_loc_oprnd2;
3089 switch (loc->dw_loc_opc)
3091 #ifdef DWARF2_DEBUGGING_INFO
3092 case DW_OP_addr:
3093 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
3094 break;
3095 case DW_OP_const2u:
3096 case DW_OP_const2s:
3097 dw2_asm_output_data (2, val1->v.val_int, NULL);
3098 break;
3099 case DW_OP_const4u:
3100 case DW_OP_const4s:
3101 dw2_asm_output_data (4, val1->v.val_int, NULL);
3102 break;
3103 case DW_OP_const8u:
3104 case DW_OP_const8s:
3105 gcc_assert (HOST_BITS_PER_LONG >= 64);
3106 dw2_asm_output_data (8, val1->v.val_int, NULL);
3107 break;
3108 case DW_OP_skip:
3109 case DW_OP_bra:
3111 int offset;
3113 gcc_assert (val1->val_class == dw_val_class_loc);
3114 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
3116 dw2_asm_output_data (2, offset, NULL);
3118 break;
3119 #else
3120 case DW_OP_addr:
3121 case DW_OP_const2u:
3122 case DW_OP_const2s:
3123 case DW_OP_const4u:
3124 case DW_OP_const4s:
3125 case DW_OP_const8u:
3126 case DW_OP_const8s:
3127 case DW_OP_skip:
3128 case DW_OP_bra:
3129 /* We currently don't make any attempt to make sure these are
3130 aligned properly like we do for the main unwind info, so
3131 don't support emitting things larger than a byte if we're
3132 only doing unwinding. */
3133 gcc_unreachable ();
3134 #endif
3135 case DW_OP_const1u:
3136 case DW_OP_const1s:
3137 dw2_asm_output_data (1, val1->v.val_int, NULL);
3138 break;
3139 case DW_OP_constu:
3140 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3141 break;
3142 case DW_OP_consts:
3143 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3144 break;
3145 case DW_OP_pick:
3146 dw2_asm_output_data (1, val1->v.val_int, NULL);
3147 break;
3148 case DW_OP_plus_uconst:
3149 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3150 break;
3151 case DW_OP_breg0:
3152 case DW_OP_breg1:
3153 case DW_OP_breg2:
3154 case DW_OP_breg3:
3155 case DW_OP_breg4:
3156 case DW_OP_breg5:
3157 case DW_OP_breg6:
3158 case DW_OP_breg7:
3159 case DW_OP_breg8:
3160 case DW_OP_breg9:
3161 case DW_OP_breg10:
3162 case DW_OP_breg11:
3163 case DW_OP_breg12:
3164 case DW_OP_breg13:
3165 case DW_OP_breg14:
3166 case DW_OP_breg15:
3167 case DW_OP_breg16:
3168 case DW_OP_breg17:
3169 case DW_OP_breg18:
3170 case DW_OP_breg19:
3171 case DW_OP_breg20:
3172 case DW_OP_breg21:
3173 case DW_OP_breg22:
3174 case DW_OP_breg23:
3175 case DW_OP_breg24:
3176 case DW_OP_breg25:
3177 case DW_OP_breg26:
3178 case DW_OP_breg27:
3179 case DW_OP_breg28:
3180 case DW_OP_breg29:
3181 case DW_OP_breg30:
3182 case DW_OP_breg31:
3183 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3184 break;
3185 case DW_OP_regx:
3186 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3187 break;
3188 case DW_OP_fbreg:
3189 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3190 break;
3191 case DW_OP_bregx:
3192 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3193 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
3194 break;
3195 case DW_OP_piece:
3196 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3197 break;
3198 case DW_OP_deref_size:
3199 case DW_OP_xderef_size:
3200 dw2_asm_output_data (1, val1->v.val_int, NULL);
3201 break;
3203 case INTERNAL_DW_OP_tls_addr:
3204 #ifdef ASM_OUTPUT_DWARF_DTPREL
3205 ASM_OUTPUT_DWARF_DTPREL (asm_out_file, DWARF2_ADDR_SIZE,
3206 val1->v.val_addr);
3207 fputc ('\n', asm_out_file);
3208 #else
3209 gcc_unreachable ();
3210 #endif
3211 break;
3213 default:
3214 /* Other codes have no operands. */
3215 break;
3219 /* Output a sequence of location operations. */
3221 static void
3222 output_loc_sequence (dw_loc_descr_ref loc)
3224 for (; loc != NULL; loc = loc->dw_loc_next)
3226 /* Output the opcode. */
3227 dw2_asm_output_data (1, loc->dw_loc_opc,
3228 "%s", dwarf_stack_op_name (loc->dw_loc_opc));
3230 /* Output the operand(s) (if any). */
3231 output_loc_operands (loc);
3235 /* This routine will generate the correct assembly data for a location
3236 description based on a cfi entry with a complex address. */
3238 static void
3239 output_cfa_loc (dw_cfi_ref cfi)
3241 dw_loc_descr_ref loc;
3242 unsigned long size;
3244 /* Output the size of the block. */
3245 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
3246 size = size_of_locs (loc);
3247 dw2_asm_output_data_uleb128 (size, NULL);
3249 /* Now output the operations themselves. */
3250 output_loc_sequence (loc);
3253 /* This function builds a dwarf location descriptor sequence from
3254 a dw_cfa_location. */
3256 static struct dw_loc_descr_struct *
3257 build_cfa_loc (dw_cfa_location *cfa)
3259 struct dw_loc_descr_struct *head, *tmp;
3261 gcc_assert (cfa->indirect);
3263 if (cfa->base_offset)
3265 if (cfa->reg <= 31)
3266 head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->base_offset, 0);
3267 else
3268 head = new_loc_descr (DW_OP_bregx, cfa->reg, cfa->base_offset);
3270 else if (cfa->reg <= 31)
3271 head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3272 else
3273 head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3275 head->dw_loc_oprnd1.val_class = dw_val_class_const;
3276 tmp = new_loc_descr (DW_OP_deref, 0, 0);
3277 add_loc_descr (&head, tmp);
3278 if (cfa->offset != 0)
3280 tmp = new_loc_descr (DW_OP_plus_uconst, cfa->offset, 0);
3281 add_loc_descr (&head, tmp);
3284 return head;
3287 /* This function fills in aa dw_cfa_location structure from a dwarf location
3288 descriptor sequence. */
3290 static void
3291 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
3293 struct dw_loc_descr_struct *ptr;
3294 cfa->offset = 0;
3295 cfa->base_offset = 0;
3296 cfa->indirect = 0;
3297 cfa->reg = -1;
3299 for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
3301 enum dwarf_location_atom op = ptr->dw_loc_opc;
3303 switch (op)
3305 case DW_OP_reg0:
3306 case DW_OP_reg1:
3307 case DW_OP_reg2:
3308 case DW_OP_reg3:
3309 case DW_OP_reg4:
3310 case DW_OP_reg5:
3311 case DW_OP_reg6:
3312 case DW_OP_reg7:
3313 case DW_OP_reg8:
3314 case DW_OP_reg9:
3315 case DW_OP_reg10:
3316 case DW_OP_reg11:
3317 case DW_OP_reg12:
3318 case DW_OP_reg13:
3319 case DW_OP_reg14:
3320 case DW_OP_reg15:
3321 case DW_OP_reg16:
3322 case DW_OP_reg17:
3323 case DW_OP_reg18:
3324 case DW_OP_reg19:
3325 case DW_OP_reg20:
3326 case DW_OP_reg21:
3327 case DW_OP_reg22:
3328 case DW_OP_reg23:
3329 case DW_OP_reg24:
3330 case DW_OP_reg25:
3331 case DW_OP_reg26:
3332 case DW_OP_reg27:
3333 case DW_OP_reg28:
3334 case DW_OP_reg29:
3335 case DW_OP_reg30:
3336 case DW_OP_reg31:
3337 cfa->reg = op - DW_OP_reg0;
3338 break;
3339 case DW_OP_regx:
3340 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3341 break;
3342 case DW_OP_breg0:
3343 case DW_OP_breg1:
3344 case DW_OP_breg2:
3345 case DW_OP_breg3:
3346 case DW_OP_breg4:
3347 case DW_OP_breg5:
3348 case DW_OP_breg6:
3349 case DW_OP_breg7:
3350 case DW_OP_breg8:
3351 case DW_OP_breg9:
3352 case DW_OP_breg10:
3353 case DW_OP_breg11:
3354 case DW_OP_breg12:
3355 case DW_OP_breg13:
3356 case DW_OP_breg14:
3357 case DW_OP_breg15:
3358 case DW_OP_breg16:
3359 case DW_OP_breg17:
3360 case DW_OP_breg18:
3361 case DW_OP_breg19:
3362 case DW_OP_breg20:
3363 case DW_OP_breg21:
3364 case DW_OP_breg22:
3365 case DW_OP_breg23:
3366 case DW_OP_breg24:
3367 case DW_OP_breg25:
3368 case DW_OP_breg26:
3369 case DW_OP_breg27:
3370 case DW_OP_breg28:
3371 case DW_OP_breg29:
3372 case DW_OP_breg30:
3373 case DW_OP_breg31:
3374 cfa->reg = op - DW_OP_breg0;
3375 cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
3376 break;
3377 case DW_OP_bregx:
3378 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3379 cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
3380 break;
3381 case DW_OP_deref:
3382 cfa->indirect = 1;
3383 break;
3384 case DW_OP_plus_uconst:
3385 cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
3386 break;
3387 default:
3388 internal_error ("DW_LOC_OP %s not implemented\n",
3389 dwarf_stack_op_name (ptr->dw_loc_opc));
3393 #endif /* .debug_frame support */
3395 /* And now, the support for symbolic debugging information. */
3396 #ifdef DWARF2_DEBUGGING_INFO
3398 /* .debug_str support. */
3399 static int output_indirect_string (void **, void *);
3401 static void dwarf2out_init (const char *);
3402 static void dwarf2out_finish (const char *);
3403 static void dwarf2out_define (unsigned int, const char *);
3404 static void dwarf2out_undef (unsigned int, const char *);
3405 static void dwarf2out_start_source_file (unsigned, const char *);
3406 static void dwarf2out_end_source_file (unsigned);
3407 static void dwarf2out_begin_block (unsigned, unsigned);
3408 static void dwarf2out_end_block (unsigned, unsigned);
3409 static bool dwarf2out_ignore_block (tree);
3410 static void dwarf2out_global_decl (tree);
3411 static void dwarf2out_type_decl (tree, int);
3412 static void dwarf2out_imported_module_or_decl (tree, tree);
3413 static void dwarf2out_abstract_function (tree);
3414 static void dwarf2out_var_location (rtx);
3415 static void dwarf2out_begin_function (tree);
3417 /* The debug hooks structure. */
3419 const struct gcc_debug_hooks dwarf2_debug_hooks =
3421 dwarf2out_init,
3422 dwarf2out_finish,
3423 dwarf2out_define,
3424 dwarf2out_undef,
3425 dwarf2out_start_source_file,
3426 dwarf2out_end_source_file,
3427 dwarf2out_begin_block,
3428 dwarf2out_end_block,
3429 dwarf2out_ignore_block,
3430 dwarf2out_source_line,
3431 dwarf2out_begin_prologue,
3432 debug_nothing_int_charstar, /* end_prologue */
3433 dwarf2out_end_epilogue,
3434 dwarf2out_begin_function,
3435 debug_nothing_int, /* end_function */
3436 dwarf2out_decl, /* function_decl */
3437 dwarf2out_global_decl,
3438 dwarf2out_type_decl, /* type_decl */
3439 dwarf2out_imported_module_or_decl,
3440 debug_nothing_tree, /* deferred_inline_function */
3441 /* The DWARF 2 backend tries to reduce debugging bloat by not
3442 emitting the abstract description of inline functions until
3443 something tries to reference them. */
3444 dwarf2out_abstract_function, /* outlining_inline_function */
3445 debug_nothing_rtx, /* label */
3446 debug_nothing_int, /* handle_pch */
3447 dwarf2out_var_location,
3448 1 /* start_end_main_source_file */
3450 #endif
3452 /* NOTE: In the comments in this file, many references are made to
3453 "Debugging Information Entries". This term is abbreviated as `DIE'
3454 throughout the remainder of this file. */
3456 /* An internal representation of the DWARF output is built, and then
3457 walked to generate the DWARF debugging info. The walk of the internal
3458 representation is done after the entire program has been compiled.
3459 The types below are used to describe the internal representation. */
3461 /* Various DIE's use offsets relative to the beginning of the
3462 .debug_info section to refer to each other. */
3464 typedef long int dw_offset;
3466 /* Define typedefs here to avoid circular dependencies. */
3468 typedef struct dw_attr_struct *dw_attr_ref;
3469 typedef struct dw_line_info_struct *dw_line_info_ref;
3470 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
3471 typedef struct pubname_struct *pubname_ref;
3472 typedef struct dw_ranges_struct *dw_ranges_ref;
3474 /* Each entry in the line_info_table maintains the file and
3475 line number associated with the label generated for that
3476 entry. The label gives the PC value associated with
3477 the line number entry. */
3479 typedef struct dw_line_info_struct GTY(())
3481 unsigned long dw_file_num;
3482 unsigned long dw_line_num;
3484 dw_line_info_entry;
3486 /* Line information for functions in separate sections; each one gets its
3487 own sequence. */
3488 typedef struct dw_separate_line_info_struct GTY(())
3490 unsigned long dw_file_num;
3491 unsigned long dw_line_num;
3492 unsigned long function;
3494 dw_separate_line_info_entry;
3496 /* Each DIE attribute has a field specifying the attribute kind,
3497 a link to the next attribute in the chain, and an attribute value.
3498 Attributes are typically linked below the DIE they modify. */
3500 typedef struct dw_attr_struct GTY(())
3502 enum dwarf_attribute dw_attr;
3503 dw_attr_ref dw_attr_next;
3504 dw_val_node dw_attr_val;
3506 dw_attr_node;
3508 /* The Debugging Information Entry (DIE) structure */
3510 typedef struct die_struct GTY(())
3512 enum dwarf_tag die_tag;
3513 char *die_symbol;
3514 dw_attr_ref die_attr;
3515 dw_die_ref die_parent;
3516 dw_die_ref die_child;
3517 dw_die_ref die_sib;
3518 dw_die_ref die_definition; /* ref from a specification to its definition */
3519 dw_offset die_offset;
3520 unsigned long die_abbrev;
3521 int die_mark;
3522 unsigned int decl_id;
3524 die_node;
3526 /* The pubname structure */
3528 typedef struct pubname_struct GTY(())
3530 dw_die_ref die;
3531 char *name;
3533 pubname_entry;
3535 struct dw_ranges_struct GTY(())
3537 int block_num;
3540 /* The limbo die list structure. */
3541 typedef struct limbo_die_struct GTY(())
3543 dw_die_ref die;
3544 tree created_for;
3545 struct limbo_die_struct *next;
3547 limbo_die_node;
3549 /* How to start an assembler comment. */
3550 #ifndef ASM_COMMENT_START
3551 #define ASM_COMMENT_START ";#"
3552 #endif
3554 /* Define a macro which returns nonzero for a TYPE_DECL which was
3555 implicitly generated for a tagged type.
3557 Note that unlike the gcc front end (which generates a NULL named
3558 TYPE_DECL node for each complete tagged type, each array type, and
3559 each function type node created) the g++ front end generates a
3560 _named_ TYPE_DECL node for each tagged type node created.
3561 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3562 generate a DW_TAG_typedef DIE for them. */
3564 #define TYPE_DECL_IS_STUB(decl) \
3565 (DECL_NAME (decl) == NULL_TREE \
3566 || (DECL_ARTIFICIAL (decl) \
3567 && is_tagged_type (TREE_TYPE (decl)) \
3568 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3569 /* This is necessary for stub decls that \
3570 appear in nested inline functions. */ \
3571 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3572 && (decl_ultimate_origin (decl) \
3573 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3575 /* Information concerning the compilation unit's programming
3576 language, and compiler version. */
3578 /* Fixed size portion of the DWARF compilation unit header. */
3579 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3580 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
3582 /* Fixed size portion of public names info. */
3583 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3585 /* Fixed size portion of the address range info. */
3586 #define DWARF_ARANGES_HEADER_SIZE \
3587 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3588 DWARF2_ADDR_SIZE * 2) \
3589 - DWARF_INITIAL_LENGTH_SIZE)
3591 /* Size of padding portion in the address range info. It must be
3592 aligned to twice the pointer size. */
3593 #define DWARF_ARANGES_PAD_SIZE \
3594 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3595 DWARF2_ADDR_SIZE * 2) \
3596 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3598 /* Use assembler line directives if available. */
3599 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3600 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3601 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3602 #else
3603 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3604 #endif
3605 #endif
3607 /* Minimum line offset in a special line info. opcode.
3608 This value was chosen to give a reasonable range of values. */
3609 #define DWARF_LINE_BASE -10
3611 /* First special line opcode - leave room for the standard opcodes. */
3612 #define DWARF_LINE_OPCODE_BASE 10
3614 /* Range of line offsets in a special line info. opcode. */
3615 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3617 /* Flag that indicates the initial value of the is_stmt_start flag.
3618 In the present implementation, we do not mark any lines as
3619 the beginning of a source statement, because that information
3620 is not made available by the GCC front-end. */
3621 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3623 #ifdef DWARF2_DEBUGGING_INFO
3624 /* This location is used by calc_die_sizes() to keep track
3625 the offset of each DIE within the .debug_info section. */
3626 static unsigned long next_die_offset;
3627 #endif
3629 /* Record the root of the DIE's built for the current compilation unit. */
3630 static GTY(()) dw_die_ref comp_unit_die;
3632 /* A list of DIEs with a NULL parent waiting to be relocated. */
3633 static GTY(()) limbo_die_node *limbo_die_list;
3635 /* Filenames referenced by this compilation unit. */
3636 static GTY(()) varray_type file_table;
3637 static GTY(()) varray_type file_table_emitted;
3638 static GTY(()) size_t file_table_last_lookup_index;
3640 /* A hash table of references to DIE's that describe declarations.
3641 The key is a DECL_UID() which is a unique number identifying each decl. */
3642 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
3644 /* Node of the variable location list. */
3645 struct var_loc_node GTY ((chain_next ("%h.next")))
3647 rtx GTY (()) var_loc_note;
3648 const char * GTY (()) label;
3649 struct var_loc_node * GTY (()) next;
3652 /* Variable location list. */
3653 struct var_loc_list_def GTY (())
3655 struct var_loc_node * GTY (()) first;
3657 /* Do not mark the last element of the chained list because
3658 it is marked through the chain. */
3659 struct var_loc_node * GTY ((skip ("%h"))) last;
3661 /* DECL_UID of the variable decl. */
3662 unsigned int decl_id;
3664 typedef struct var_loc_list_def var_loc_list;
3667 /* Table of decl location linked lists. */
3668 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
3670 /* A pointer to the base of a list of references to DIE's that
3671 are uniquely identified by their tag, presence/absence of
3672 children DIE's, and list of attribute/value pairs. */
3673 static GTY((length ("abbrev_die_table_allocated")))
3674 dw_die_ref *abbrev_die_table;
3676 /* Number of elements currently allocated for abbrev_die_table. */
3677 static GTY(()) unsigned abbrev_die_table_allocated;
3679 /* Number of elements in type_die_table currently in use. */
3680 static GTY(()) unsigned abbrev_die_table_in_use;
3682 /* Size (in elements) of increments by which we may expand the
3683 abbrev_die_table. */
3684 #define ABBREV_DIE_TABLE_INCREMENT 256
3686 /* A pointer to the base of a table that contains line information
3687 for each source code line in .text in the compilation unit. */
3688 static GTY((length ("line_info_table_allocated")))
3689 dw_line_info_ref line_info_table;
3691 /* Number of elements currently allocated for line_info_table. */
3692 static GTY(()) unsigned line_info_table_allocated;
3694 /* Number of elements in line_info_table currently in use. */
3695 static GTY(()) unsigned line_info_table_in_use;
3697 /* A pointer to the base of a table that contains line information
3698 for each source code line outside of .text in the compilation unit. */
3699 static GTY ((length ("separate_line_info_table_allocated")))
3700 dw_separate_line_info_ref separate_line_info_table;
3702 /* Number of elements currently allocated for separate_line_info_table. */
3703 static GTY(()) unsigned separate_line_info_table_allocated;
3705 /* Number of elements in separate_line_info_table currently in use. */
3706 static GTY(()) unsigned separate_line_info_table_in_use;
3708 /* Size (in elements) of increments by which we may expand the
3709 line_info_table. */
3710 #define LINE_INFO_TABLE_INCREMENT 1024
3712 /* A pointer to the base of a table that contains a list of publicly
3713 accessible names. */
3714 static GTY ((length ("pubname_table_allocated"))) pubname_ref pubname_table;
3716 /* Number of elements currently allocated for pubname_table. */
3717 static GTY(()) unsigned pubname_table_allocated;
3719 /* Number of elements in pubname_table currently in use. */
3720 static GTY(()) unsigned pubname_table_in_use;
3722 /* Size (in elements) of increments by which we may expand the
3723 pubname_table. */
3724 #define PUBNAME_TABLE_INCREMENT 64
3726 /* Array of dies for which we should generate .debug_arange info. */
3727 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
3729 /* Number of elements currently allocated for arange_table. */
3730 static GTY(()) unsigned arange_table_allocated;
3732 /* Number of elements in arange_table currently in use. */
3733 static GTY(()) unsigned arange_table_in_use;
3735 /* Size (in elements) of increments by which we may expand the
3736 arange_table. */
3737 #define ARANGE_TABLE_INCREMENT 64
3739 /* Array of dies for which we should generate .debug_ranges info. */
3740 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
3742 /* Number of elements currently allocated for ranges_table. */
3743 static GTY(()) unsigned ranges_table_allocated;
3745 /* Number of elements in ranges_table currently in use. */
3746 static GTY(()) unsigned ranges_table_in_use;
3748 /* Size (in elements) of increments by which we may expand the
3749 ranges_table. */
3750 #define RANGES_TABLE_INCREMENT 64
3752 /* Whether we have location lists that need outputting */
3753 static GTY(()) unsigned have_location_lists;
3755 /* Unique label counter. */
3756 static GTY(()) unsigned int loclabel_num;
3758 #ifdef DWARF2_DEBUGGING_INFO
3759 /* Record whether the function being analyzed contains inlined functions. */
3760 static int current_function_has_inlines;
3761 #endif
3762 #if 0 && defined (MIPS_DEBUGGING_INFO)
3763 static int comp_unit_has_inlines;
3764 #endif
3766 /* Number of file tables emitted in maybe_emit_file(). */
3767 static GTY(()) int emitcount = 0;
3769 /* Number of internal labels generated by gen_internal_sym(). */
3770 static GTY(()) int label_num;
3772 #ifdef DWARF2_DEBUGGING_INFO
3774 /* Forward declarations for functions defined in this file. */
3776 static int is_pseudo_reg (rtx);
3777 static tree type_main_variant (tree);
3778 static int is_tagged_type (tree);
3779 static const char *dwarf_tag_name (unsigned);
3780 static const char *dwarf_attr_name (unsigned);
3781 static const char *dwarf_form_name (unsigned);
3782 static tree decl_ultimate_origin (tree);
3783 static tree block_ultimate_origin (tree);
3784 static tree decl_class_context (tree);
3785 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
3786 static inline enum dw_val_class AT_class (dw_attr_ref);
3787 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3788 static inline unsigned AT_flag (dw_attr_ref);
3789 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3790 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3791 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3792 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3793 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
3794 unsigned long);
3795 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3796 unsigned int, unsigned char *);
3797 static hashval_t debug_str_do_hash (const void *);
3798 static int debug_str_eq (const void *, const void *);
3799 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3800 static inline const char *AT_string (dw_attr_ref);
3801 static int AT_string_form (dw_attr_ref);
3802 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3803 static void add_AT_specification (dw_die_ref, dw_die_ref);
3804 static inline dw_die_ref AT_ref (dw_attr_ref);
3805 static inline int AT_ref_external (dw_attr_ref);
3806 static inline void set_AT_ref_external (dw_attr_ref, int);
3807 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3808 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3809 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3810 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3811 dw_loc_list_ref);
3812 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3813 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
3814 static inline rtx AT_addr (dw_attr_ref);
3815 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3816 static void add_AT_lbl_offset (dw_die_ref, enum dwarf_attribute, const char *);
3817 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3818 unsigned HOST_WIDE_INT);
3819 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3820 unsigned long);
3821 static inline const char *AT_lbl (dw_attr_ref);
3822 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3823 static const char *get_AT_low_pc (dw_die_ref);
3824 static const char *get_AT_hi_pc (dw_die_ref);
3825 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3826 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3827 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3828 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3829 static bool is_c_family (void);
3830 static bool is_cxx (void);
3831 static bool is_java (void);
3832 static bool is_fortran (void);
3833 static bool is_ada (void);
3834 static void remove_AT (dw_die_ref, enum dwarf_attribute);
3835 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3836 static inline void free_die (dw_die_ref);
3837 static void remove_children (dw_die_ref);
3838 static void add_child_die (dw_die_ref, dw_die_ref);
3839 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3840 static dw_die_ref lookup_type_die (tree);
3841 static void equate_type_number_to_die (tree, dw_die_ref);
3842 static hashval_t decl_die_table_hash (const void *);
3843 static int decl_die_table_eq (const void *, const void *);
3844 static dw_die_ref lookup_decl_die (tree);
3845 static hashval_t decl_loc_table_hash (const void *);
3846 static int decl_loc_table_eq (const void *, const void *);
3847 static var_loc_list *lookup_decl_loc (tree);
3848 static void equate_decl_number_to_die (tree, dw_die_ref);
3849 static void add_var_loc_to_decl (tree, struct var_loc_node *);
3850 static void print_spaces (FILE *);
3851 static void print_die (dw_die_ref, FILE *);
3852 static void print_dwarf_line_table (FILE *);
3853 static void reverse_die_lists (dw_die_ref);
3854 static void reverse_all_dies (dw_die_ref);
3855 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3856 static dw_die_ref pop_compile_unit (dw_die_ref);
3857 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3858 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3859 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3860 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3861 static int same_dw_val_p (dw_val_node *, dw_val_node *, int *);
3862 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3863 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3864 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3865 static void compute_section_prefix (dw_die_ref);
3866 static int is_type_die (dw_die_ref);
3867 static int is_comdat_die (dw_die_ref);
3868 static int is_symbol_die (dw_die_ref);
3869 static void assign_symbol_names (dw_die_ref);
3870 static void break_out_includes (dw_die_ref);
3871 static hashval_t htab_cu_hash (const void *);
3872 static int htab_cu_eq (const void *, const void *);
3873 static void htab_cu_del (void *);
3874 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
3875 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
3876 static void add_sibling_attributes (dw_die_ref);
3877 static void build_abbrev_table (dw_die_ref);
3878 static void output_location_lists (dw_die_ref);
3879 static int constant_size (long unsigned);
3880 static unsigned long size_of_die (dw_die_ref);
3881 static void calc_die_sizes (dw_die_ref);
3882 static void mark_dies (dw_die_ref);
3883 static void unmark_dies (dw_die_ref);
3884 static void unmark_all_dies (dw_die_ref);
3885 static unsigned long size_of_pubnames (void);
3886 static unsigned long size_of_aranges (void);
3887 static enum dwarf_form value_format (dw_attr_ref);
3888 static void output_value_format (dw_attr_ref);
3889 static void output_abbrev_section (void);
3890 static void output_die_symbol (dw_die_ref);
3891 static void output_die (dw_die_ref);
3892 static void output_compilation_unit_header (void);
3893 static void output_comp_unit (dw_die_ref, int);
3894 static const char *dwarf2_name (tree, int);
3895 static void add_pubname (tree, dw_die_ref);
3896 static void output_pubnames (void);
3897 static void add_arange (tree, dw_die_ref);
3898 static void output_aranges (void);
3899 static unsigned int add_ranges (tree);
3900 static void output_ranges (void);
3901 static void output_line_info (void);
3902 static void output_file_names (void);
3903 static dw_die_ref base_type_die (tree);
3904 static tree root_type (tree);
3905 static int is_base_type (tree);
3906 static bool is_subrange_type (tree);
3907 static dw_die_ref subrange_type_die (tree, dw_die_ref);
3908 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
3909 static int type_is_enum (tree);
3910 static unsigned int dbx_reg_number (rtx);
3911 static dw_loc_descr_ref reg_loc_descriptor (rtx);
3912 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int);
3913 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx);
3914 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
3915 static dw_loc_descr_ref based_loc_descr (unsigned, HOST_WIDE_INT, bool);
3916 static int is_based_loc (rtx);
3917 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode, bool);
3918 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx);
3919 static dw_loc_descr_ref loc_descriptor (rtx, bool);
3920 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
3921 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
3922 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3923 static tree field_type (tree);
3924 static unsigned int simple_type_align_in_bits (tree);
3925 static unsigned int simple_decl_align_in_bits (tree);
3926 static unsigned HOST_WIDE_INT simple_type_size_in_bits (tree);
3927 static HOST_WIDE_INT field_byte_offset (tree);
3928 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3929 dw_loc_descr_ref);
3930 static void add_data_member_location_attribute (dw_die_ref, tree);
3931 static void add_const_value_attribute (dw_die_ref, rtx);
3932 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3933 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
3934 static void insert_float (rtx, unsigned char *);
3935 static rtx rtl_for_decl_location (tree);
3936 static void add_location_or_const_value_attribute (dw_die_ref, tree,
3937 enum dwarf_attribute);
3938 static void tree_add_const_value_attribute (dw_die_ref, tree);
3939 static void add_name_attribute (dw_die_ref, const char *);
3940 static void add_comp_dir_attribute (dw_die_ref);
3941 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3942 static void add_subscript_info (dw_die_ref, tree);
3943 static void add_byte_size_attribute (dw_die_ref, tree);
3944 static void add_bit_offset_attribute (dw_die_ref, tree);
3945 static void add_bit_size_attribute (dw_die_ref, tree);
3946 static void add_prototyped_attribute (dw_die_ref, tree);
3947 static void add_abstract_origin_attribute (dw_die_ref, tree);
3948 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3949 static void add_src_coords_attributes (dw_die_ref, tree);
3950 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3951 static void push_decl_scope (tree);
3952 static void pop_decl_scope (void);
3953 static dw_die_ref scope_die_for (tree, dw_die_ref);
3954 static inline int local_scope_p (dw_die_ref);
3955 static inline int class_or_namespace_scope_p (dw_die_ref);
3956 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
3957 static void add_calling_convention_attribute (dw_die_ref, tree);
3958 static const char *type_tag (tree);
3959 static tree member_declared_type (tree);
3960 #if 0
3961 static const char *decl_start_label (tree);
3962 #endif
3963 static void gen_array_type_die (tree, dw_die_ref);
3964 #if 0
3965 static void gen_entry_point_die (tree, dw_die_ref);
3966 #endif
3967 static void gen_inlined_enumeration_type_die (tree, dw_die_ref);
3968 static void gen_inlined_structure_type_die (tree, dw_die_ref);
3969 static void gen_inlined_union_type_die (tree, dw_die_ref);
3970 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3971 static dw_die_ref gen_formal_parameter_die (tree, dw_die_ref);
3972 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3973 static void gen_formal_types_die (tree, dw_die_ref);
3974 static void gen_subprogram_die (tree, dw_die_ref);
3975 static void gen_variable_die (tree, dw_die_ref);
3976 static void gen_label_die (tree, dw_die_ref);
3977 static void gen_lexical_block_die (tree, dw_die_ref, int);
3978 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3979 static void gen_field_die (tree, dw_die_ref);
3980 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3981 static dw_die_ref gen_compile_unit_die (const char *);
3982 static void gen_string_type_die (tree, dw_die_ref);
3983 static void gen_inheritance_die (tree, tree, dw_die_ref);
3984 static void gen_member_die (tree, dw_die_ref);
3985 static void gen_struct_or_union_type_die (tree, dw_die_ref);
3986 static void gen_subroutine_type_die (tree, dw_die_ref);
3987 static void gen_typedef_die (tree, dw_die_ref);
3988 static void gen_type_die (tree, dw_die_ref);
3989 static void gen_tagged_type_instantiation_die (tree, dw_die_ref);
3990 static void gen_block_die (tree, dw_die_ref, int);
3991 static void decls_for_scope (tree, dw_die_ref, int);
3992 static int is_redundant_typedef (tree);
3993 static void gen_namespace_die (tree);
3994 static void gen_decl_die (tree, dw_die_ref);
3995 static dw_die_ref force_decl_die (tree);
3996 static dw_die_ref force_type_die (tree);
3997 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3998 static void declare_in_namespace (tree, dw_die_ref);
3999 static unsigned lookup_filename (const char *);
4000 static void init_file_table (void);
4001 static void retry_incomplete_types (void);
4002 static void gen_type_die_for_member (tree, tree, dw_die_ref);
4003 static void splice_child_die (dw_die_ref, dw_die_ref);
4004 static int file_info_cmp (const void *, const void *);
4005 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
4006 const char *, const char *, unsigned);
4007 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
4008 const char *, const char *,
4009 const char *);
4010 static void output_loc_list (dw_loc_list_ref);
4011 static char *gen_internal_sym (const char *);
4013 static void prune_unmark_dies (dw_die_ref);
4014 static void prune_unused_types_mark (dw_die_ref, int);
4015 static void prune_unused_types_walk (dw_die_ref);
4016 static void prune_unused_types_walk_attribs (dw_die_ref);
4017 static void prune_unused_types_prune (dw_die_ref);
4018 static void prune_unused_types (void);
4019 static int maybe_emit_file (int);
4021 /* Section names used to hold DWARF debugging information. */
4022 #ifndef DEBUG_INFO_SECTION
4023 #define DEBUG_INFO_SECTION ".debug_info"
4024 #endif
4025 #ifndef DEBUG_ABBREV_SECTION
4026 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
4027 #endif
4028 #ifndef DEBUG_ARANGES_SECTION
4029 #define DEBUG_ARANGES_SECTION ".debug_aranges"
4030 #endif
4031 #ifndef DEBUG_MACINFO_SECTION
4032 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
4033 #endif
4034 #ifndef DEBUG_LINE_SECTION
4035 #define DEBUG_LINE_SECTION ".debug_line"
4036 #endif
4037 #ifndef DEBUG_LOC_SECTION
4038 #define DEBUG_LOC_SECTION ".debug_loc"
4039 #endif
4040 #ifndef DEBUG_PUBNAMES_SECTION
4041 #define DEBUG_PUBNAMES_SECTION ".debug_pubnames"
4042 #endif
4043 #ifndef DEBUG_STR_SECTION
4044 #define DEBUG_STR_SECTION ".debug_str"
4045 #endif
4046 #ifndef DEBUG_RANGES_SECTION
4047 #define DEBUG_RANGES_SECTION ".debug_ranges"
4048 #endif
4050 /* Standard ELF section names for compiled code and data. */
4051 #ifndef TEXT_SECTION_NAME
4052 #define TEXT_SECTION_NAME ".text"
4053 #endif
4055 /* Section flags for .debug_str section. */
4056 #define DEBUG_STR_SECTION_FLAGS \
4057 (HAVE_GAS_SHF_MERGE && flag_merge_constants \
4058 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
4059 : SECTION_DEBUG)
4061 /* Labels we insert at beginning sections we can reference instead of
4062 the section names themselves. */
4064 #ifndef TEXT_SECTION_LABEL
4065 #define TEXT_SECTION_LABEL "Ltext"
4066 #endif
4067 #ifndef DEBUG_LINE_SECTION_LABEL
4068 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
4069 #endif
4070 #ifndef DEBUG_INFO_SECTION_LABEL
4071 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
4072 #endif
4073 #ifndef DEBUG_ABBREV_SECTION_LABEL
4074 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
4075 #endif
4076 #ifndef DEBUG_LOC_SECTION_LABEL
4077 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
4078 #endif
4079 #ifndef DEBUG_RANGES_SECTION_LABEL
4080 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
4081 #endif
4082 #ifndef DEBUG_MACINFO_SECTION_LABEL
4083 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
4084 #endif
4086 /* Definitions of defaults for formats and names of various special
4087 (artificial) labels which may be generated within this file (when the -g
4088 options is used and DWARF2_DEBUGGING_INFO is in effect.
4089 If necessary, these may be overridden from within the tm.h file, but
4090 typically, overriding these defaults is unnecessary. */
4092 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4093 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4094 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4095 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4096 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4097 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4098 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4099 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4101 #ifndef TEXT_END_LABEL
4102 #define TEXT_END_LABEL "Letext"
4103 #endif
4104 #ifndef BLOCK_BEGIN_LABEL
4105 #define BLOCK_BEGIN_LABEL "LBB"
4106 #endif
4107 #ifndef BLOCK_END_LABEL
4108 #define BLOCK_END_LABEL "LBE"
4109 #endif
4110 #ifndef LINE_CODE_LABEL
4111 #define LINE_CODE_LABEL "LM"
4112 #endif
4113 #ifndef SEPARATE_LINE_CODE_LABEL
4114 #define SEPARATE_LINE_CODE_LABEL "LSM"
4115 #endif
4117 /* We allow a language front-end to designate a function that is to be
4118 called to "demangle" any name before it is put into a DIE. */
4120 static const char *(*demangle_name_func) (const char *);
4122 void
4123 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4125 demangle_name_func = func;
4128 /* Test if rtl node points to a pseudo register. */
4130 static inline int
4131 is_pseudo_reg (rtx rtl)
4133 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4134 || (GET_CODE (rtl) == SUBREG
4135 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4138 /* Return a reference to a type, with its const and volatile qualifiers
4139 removed. */
4141 static inline tree
4142 type_main_variant (tree type)
4144 type = TYPE_MAIN_VARIANT (type);
4146 /* ??? There really should be only one main variant among any group of
4147 variants of a given type (and all of the MAIN_VARIANT values for all
4148 members of the group should point to that one type) but sometimes the C
4149 front-end messes this up for array types, so we work around that bug
4150 here. */
4151 if (TREE_CODE (type) == ARRAY_TYPE)
4152 while (type != TYPE_MAIN_VARIANT (type))
4153 type = TYPE_MAIN_VARIANT (type);
4155 return type;
4158 /* Return nonzero if the given type node represents a tagged type. */
4160 static inline int
4161 is_tagged_type (tree type)
4163 enum tree_code code = TREE_CODE (type);
4165 return (code == RECORD_TYPE || code == UNION_TYPE
4166 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4169 /* Convert a DIE tag into its string name. */
4171 static const char *
4172 dwarf_tag_name (unsigned int tag)
4174 switch (tag)
4176 case DW_TAG_padding:
4177 return "DW_TAG_padding";
4178 case DW_TAG_array_type:
4179 return "DW_TAG_array_type";
4180 case DW_TAG_class_type:
4181 return "DW_TAG_class_type";
4182 case DW_TAG_entry_point:
4183 return "DW_TAG_entry_point";
4184 case DW_TAG_enumeration_type:
4185 return "DW_TAG_enumeration_type";
4186 case DW_TAG_formal_parameter:
4187 return "DW_TAG_formal_parameter";
4188 case DW_TAG_imported_declaration:
4189 return "DW_TAG_imported_declaration";
4190 case DW_TAG_label:
4191 return "DW_TAG_label";
4192 case DW_TAG_lexical_block:
4193 return "DW_TAG_lexical_block";
4194 case DW_TAG_member:
4195 return "DW_TAG_member";
4196 case DW_TAG_pointer_type:
4197 return "DW_TAG_pointer_type";
4198 case DW_TAG_reference_type:
4199 return "DW_TAG_reference_type";
4200 case DW_TAG_compile_unit:
4201 return "DW_TAG_compile_unit";
4202 case DW_TAG_string_type:
4203 return "DW_TAG_string_type";
4204 case DW_TAG_structure_type:
4205 return "DW_TAG_structure_type";
4206 case DW_TAG_subroutine_type:
4207 return "DW_TAG_subroutine_type";
4208 case DW_TAG_typedef:
4209 return "DW_TAG_typedef";
4210 case DW_TAG_union_type:
4211 return "DW_TAG_union_type";
4212 case DW_TAG_unspecified_parameters:
4213 return "DW_TAG_unspecified_parameters";
4214 case DW_TAG_variant:
4215 return "DW_TAG_variant";
4216 case DW_TAG_common_block:
4217 return "DW_TAG_common_block";
4218 case DW_TAG_common_inclusion:
4219 return "DW_TAG_common_inclusion";
4220 case DW_TAG_inheritance:
4221 return "DW_TAG_inheritance";
4222 case DW_TAG_inlined_subroutine:
4223 return "DW_TAG_inlined_subroutine";
4224 case DW_TAG_module:
4225 return "DW_TAG_module";
4226 case DW_TAG_ptr_to_member_type:
4227 return "DW_TAG_ptr_to_member_type";
4228 case DW_TAG_set_type:
4229 return "DW_TAG_set_type";
4230 case DW_TAG_subrange_type:
4231 return "DW_TAG_subrange_type";
4232 case DW_TAG_with_stmt:
4233 return "DW_TAG_with_stmt";
4234 case DW_TAG_access_declaration:
4235 return "DW_TAG_access_declaration";
4236 case DW_TAG_base_type:
4237 return "DW_TAG_base_type";
4238 case DW_TAG_catch_block:
4239 return "DW_TAG_catch_block";
4240 case DW_TAG_const_type:
4241 return "DW_TAG_const_type";
4242 case DW_TAG_constant:
4243 return "DW_TAG_constant";
4244 case DW_TAG_enumerator:
4245 return "DW_TAG_enumerator";
4246 case DW_TAG_file_type:
4247 return "DW_TAG_file_type";
4248 case DW_TAG_friend:
4249 return "DW_TAG_friend";
4250 case DW_TAG_namelist:
4251 return "DW_TAG_namelist";
4252 case DW_TAG_namelist_item:
4253 return "DW_TAG_namelist_item";
4254 case DW_TAG_namespace:
4255 return "DW_TAG_namespace";
4256 case DW_TAG_packed_type:
4257 return "DW_TAG_packed_type";
4258 case DW_TAG_subprogram:
4259 return "DW_TAG_subprogram";
4260 case DW_TAG_template_type_param:
4261 return "DW_TAG_template_type_param";
4262 case DW_TAG_template_value_param:
4263 return "DW_TAG_template_value_param";
4264 case DW_TAG_thrown_type:
4265 return "DW_TAG_thrown_type";
4266 case DW_TAG_try_block:
4267 return "DW_TAG_try_block";
4268 case DW_TAG_variant_part:
4269 return "DW_TAG_variant_part";
4270 case DW_TAG_variable:
4271 return "DW_TAG_variable";
4272 case DW_TAG_volatile_type:
4273 return "DW_TAG_volatile_type";
4274 case DW_TAG_imported_module:
4275 return "DW_TAG_imported_module";
4276 case DW_TAG_MIPS_loop:
4277 return "DW_TAG_MIPS_loop";
4278 case DW_TAG_format_label:
4279 return "DW_TAG_format_label";
4280 case DW_TAG_function_template:
4281 return "DW_TAG_function_template";
4282 case DW_TAG_class_template:
4283 return "DW_TAG_class_template";
4284 case DW_TAG_GNU_BINCL:
4285 return "DW_TAG_GNU_BINCL";
4286 case DW_TAG_GNU_EINCL:
4287 return "DW_TAG_GNU_EINCL";
4288 default:
4289 return "DW_TAG_<unknown>";
4293 /* Convert a DWARF attribute code into its string name. */
4295 static const char *
4296 dwarf_attr_name (unsigned int attr)
4298 switch (attr)
4300 case DW_AT_sibling:
4301 return "DW_AT_sibling";
4302 case DW_AT_location:
4303 return "DW_AT_location";
4304 case DW_AT_name:
4305 return "DW_AT_name";
4306 case DW_AT_ordering:
4307 return "DW_AT_ordering";
4308 case DW_AT_subscr_data:
4309 return "DW_AT_subscr_data";
4310 case DW_AT_byte_size:
4311 return "DW_AT_byte_size";
4312 case DW_AT_bit_offset:
4313 return "DW_AT_bit_offset";
4314 case DW_AT_bit_size:
4315 return "DW_AT_bit_size";
4316 case DW_AT_element_list:
4317 return "DW_AT_element_list";
4318 case DW_AT_stmt_list:
4319 return "DW_AT_stmt_list";
4320 case DW_AT_low_pc:
4321 return "DW_AT_low_pc";
4322 case DW_AT_high_pc:
4323 return "DW_AT_high_pc";
4324 case DW_AT_language:
4325 return "DW_AT_language";
4326 case DW_AT_member:
4327 return "DW_AT_member";
4328 case DW_AT_discr:
4329 return "DW_AT_discr";
4330 case DW_AT_discr_value:
4331 return "DW_AT_discr_value";
4332 case DW_AT_visibility:
4333 return "DW_AT_visibility";
4334 case DW_AT_import:
4335 return "DW_AT_import";
4336 case DW_AT_string_length:
4337 return "DW_AT_string_length";
4338 case DW_AT_common_reference:
4339 return "DW_AT_common_reference";
4340 case DW_AT_comp_dir:
4341 return "DW_AT_comp_dir";
4342 case DW_AT_const_value:
4343 return "DW_AT_const_value";
4344 case DW_AT_containing_type:
4345 return "DW_AT_containing_type";
4346 case DW_AT_default_value:
4347 return "DW_AT_default_value";
4348 case DW_AT_inline:
4349 return "DW_AT_inline";
4350 case DW_AT_is_optional:
4351 return "DW_AT_is_optional";
4352 case DW_AT_lower_bound:
4353 return "DW_AT_lower_bound";
4354 case DW_AT_producer:
4355 return "DW_AT_producer";
4356 case DW_AT_prototyped:
4357 return "DW_AT_prototyped";
4358 case DW_AT_return_addr:
4359 return "DW_AT_return_addr";
4360 case DW_AT_start_scope:
4361 return "DW_AT_start_scope";
4362 case DW_AT_stride_size:
4363 return "DW_AT_stride_size";
4364 case DW_AT_upper_bound:
4365 return "DW_AT_upper_bound";
4366 case DW_AT_abstract_origin:
4367 return "DW_AT_abstract_origin";
4368 case DW_AT_accessibility:
4369 return "DW_AT_accessibility";
4370 case DW_AT_address_class:
4371 return "DW_AT_address_class";
4372 case DW_AT_artificial:
4373 return "DW_AT_artificial";
4374 case DW_AT_base_types:
4375 return "DW_AT_base_types";
4376 case DW_AT_calling_convention:
4377 return "DW_AT_calling_convention";
4378 case DW_AT_count:
4379 return "DW_AT_count";
4380 case DW_AT_data_member_location:
4381 return "DW_AT_data_member_location";
4382 case DW_AT_decl_column:
4383 return "DW_AT_decl_column";
4384 case DW_AT_decl_file:
4385 return "DW_AT_decl_file";
4386 case DW_AT_decl_line:
4387 return "DW_AT_decl_line";
4388 case DW_AT_declaration:
4389 return "DW_AT_declaration";
4390 case DW_AT_discr_list:
4391 return "DW_AT_discr_list";
4392 case DW_AT_encoding:
4393 return "DW_AT_encoding";
4394 case DW_AT_external:
4395 return "DW_AT_external";
4396 case DW_AT_frame_base:
4397 return "DW_AT_frame_base";
4398 case DW_AT_friend:
4399 return "DW_AT_friend";
4400 case DW_AT_identifier_case:
4401 return "DW_AT_identifier_case";
4402 case DW_AT_macro_info:
4403 return "DW_AT_macro_info";
4404 case DW_AT_namelist_items:
4405 return "DW_AT_namelist_items";
4406 case DW_AT_priority:
4407 return "DW_AT_priority";
4408 case DW_AT_segment:
4409 return "DW_AT_segment";
4410 case DW_AT_specification:
4411 return "DW_AT_specification";
4412 case DW_AT_static_link:
4413 return "DW_AT_static_link";
4414 case DW_AT_type:
4415 return "DW_AT_type";
4416 case DW_AT_use_location:
4417 return "DW_AT_use_location";
4418 case DW_AT_variable_parameter:
4419 return "DW_AT_variable_parameter";
4420 case DW_AT_virtuality:
4421 return "DW_AT_virtuality";
4422 case DW_AT_vtable_elem_location:
4423 return "DW_AT_vtable_elem_location";
4425 case DW_AT_allocated:
4426 return "DW_AT_allocated";
4427 case DW_AT_associated:
4428 return "DW_AT_associated";
4429 case DW_AT_data_location:
4430 return "DW_AT_data_location";
4431 case DW_AT_stride:
4432 return "DW_AT_stride";
4433 case DW_AT_entry_pc:
4434 return "DW_AT_entry_pc";
4435 case DW_AT_use_UTF8:
4436 return "DW_AT_use_UTF8";
4437 case DW_AT_extension:
4438 return "DW_AT_extension";
4439 case DW_AT_ranges:
4440 return "DW_AT_ranges";
4441 case DW_AT_trampoline:
4442 return "DW_AT_trampoline";
4443 case DW_AT_call_column:
4444 return "DW_AT_call_column";
4445 case DW_AT_call_file:
4446 return "DW_AT_call_file";
4447 case DW_AT_call_line:
4448 return "DW_AT_call_line";
4450 case DW_AT_MIPS_fde:
4451 return "DW_AT_MIPS_fde";
4452 case DW_AT_MIPS_loop_begin:
4453 return "DW_AT_MIPS_loop_begin";
4454 case DW_AT_MIPS_tail_loop_begin:
4455 return "DW_AT_MIPS_tail_loop_begin";
4456 case DW_AT_MIPS_epilog_begin:
4457 return "DW_AT_MIPS_epilog_begin";
4458 case DW_AT_MIPS_loop_unroll_factor:
4459 return "DW_AT_MIPS_loop_unroll_factor";
4460 case DW_AT_MIPS_software_pipeline_depth:
4461 return "DW_AT_MIPS_software_pipeline_depth";
4462 case DW_AT_MIPS_linkage_name:
4463 return "DW_AT_MIPS_linkage_name";
4464 case DW_AT_MIPS_stride:
4465 return "DW_AT_MIPS_stride";
4466 case DW_AT_MIPS_abstract_name:
4467 return "DW_AT_MIPS_abstract_name";
4468 case DW_AT_MIPS_clone_origin:
4469 return "DW_AT_MIPS_clone_origin";
4470 case DW_AT_MIPS_has_inlines:
4471 return "DW_AT_MIPS_has_inlines";
4473 case DW_AT_sf_names:
4474 return "DW_AT_sf_names";
4475 case DW_AT_src_info:
4476 return "DW_AT_src_info";
4477 case DW_AT_mac_info:
4478 return "DW_AT_mac_info";
4479 case DW_AT_src_coords:
4480 return "DW_AT_src_coords";
4481 case DW_AT_body_begin:
4482 return "DW_AT_body_begin";
4483 case DW_AT_body_end:
4484 return "DW_AT_body_end";
4485 case DW_AT_GNU_vector:
4486 return "DW_AT_GNU_vector";
4488 case DW_AT_VMS_rtnbeg_pd_address:
4489 return "DW_AT_VMS_rtnbeg_pd_address";
4491 default:
4492 return "DW_AT_<unknown>";
4496 /* Convert a DWARF value form code into its string name. */
4498 static const char *
4499 dwarf_form_name (unsigned int form)
4501 switch (form)
4503 case DW_FORM_addr:
4504 return "DW_FORM_addr";
4505 case DW_FORM_block2:
4506 return "DW_FORM_block2";
4507 case DW_FORM_block4:
4508 return "DW_FORM_block4";
4509 case DW_FORM_data2:
4510 return "DW_FORM_data2";
4511 case DW_FORM_data4:
4512 return "DW_FORM_data4";
4513 case DW_FORM_data8:
4514 return "DW_FORM_data8";
4515 case DW_FORM_string:
4516 return "DW_FORM_string";
4517 case DW_FORM_block:
4518 return "DW_FORM_block";
4519 case DW_FORM_block1:
4520 return "DW_FORM_block1";
4521 case DW_FORM_data1:
4522 return "DW_FORM_data1";
4523 case DW_FORM_flag:
4524 return "DW_FORM_flag";
4525 case DW_FORM_sdata:
4526 return "DW_FORM_sdata";
4527 case DW_FORM_strp:
4528 return "DW_FORM_strp";
4529 case DW_FORM_udata:
4530 return "DW_FORM_udata";
4531 case DW_FORM_ref_addr:
4532 return "DW_FORM_ref_addr";
4533 case DW_FORM_ref1:
4534 return "DW_FORM_ref1";
4535 case DW_FORM_ref2:
4536 return "DW_FORM_ref2";
4537 case DW_FORM_ref4:
4538 return "DW_FORM_ref4";
4539 case DW_FORM_ref8:
4540 return "DW_FORM_ref8";
4541 case DW_FORM_ref_udata:
4542 return "DW_FORM_ref_udata";
4543 case DW_FORM_indirect:
4544 return "DW_FORM_indirect";
4545 default:
4546 return "DW_FORM_<unknown>";
4550 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4551 instance of an inlined instance of a decl which is local to an inline
4552 function, so we have to trace all of the way back through the origin chain
4553 to find out what sort of node actually served as the original seed for the
4554 given block. */
4556 static tree
4557 decl_ultimate_origin (tree decl)
4559 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4560 nodes in the function to point to themselves; ignore that if
4561 we're trying to output the abstract instance of this function. */
4562 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4563 return NULL_TREE;
4565 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4566 most distant ancestor, this should never happen. */
4567 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4569 return DECL_ABSTRACT_ORIGIN (decl);
4572 /* Determine the "ultimate origin" of a block. The block may be an inlined
4573 instance of an inlined instance of a block which is local to an inline
4574 function, so we have to trace all of the way back through the origin chain
4575 to find out what sort of node actually served as the original seed for the
4576 given block. */
4578 static tree
4579 block_ultimate_origin (tree block)
4581 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
4583 /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
4584 nodes in the function to point to themselves; ignore that if
4585 we're trying to output the abstract instance of this function. */
4586 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
4587 return NULL_TREE;
4589 if (immediate_origin == NULL_TREE)
4590 return NULL_TREE;
4591 else
4593 tree ret_val;
4594 tree lookahead = immediate_origin;
4598 ret_val = lookahead;
4599 lookahead = (TREE_CODE (ret_val) == BLOCK
4600 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
4602 while (lookahead != NULL && lookahead != ret_val);
4604 /* The block's abstract origin chain may not be the *ultimate* origin of
4605 the block. It could lead to a DECL that has an abstract origin set.
4606 If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
4607 will give us if it has one). Note that DECL's abstract origins are
4608 supposed to be the most distant ancestor (or so decl_ultimate_origin
4609 claims), so we don't need to loop following the DECL origins. */
4610 if (DECL_P (ret_val))
4611 return DECL_ORIGIN (ret_val);
4613 return ret_val;
4617 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4618 of a virtual function may refer to a base class, so we check the 'this'
4619 parameter. */
4621 static tree
4622 decl_class_context (tree decl)
4624 tree context = NULL_TREE;
4626 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4627 context = DECL_CONTEXT (decl);
4628 else
4629 context = TYPE_MAIN_VARIANT
4630 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4632 if (context && !TYPE_P (context))
4633 context = NULL_TREE;
4635 return context;
4638 /* Add an attribute/value pair to a DIE. We build the lists up in reverse
4639 addition order, and correct that in reverse_all_dies. */
4641 static inline void
4642 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
4644 if (die != NULL && attr != NULL)
4646 attr->dw_attr_next = die->die_attr;
4647 die->die_attr = attr;
4651 static inline enum dw_val_class
4652 AT_class (dw_attr_ref a)
4654 return a->dw_attr_val.val_class;
4657 /* Add a flag value attribute to a DIE. */
4659 static inline void
4660 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4662 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4664 attr->dw_attr_next = NULL;
4665 attr->dw_attr = attr_kind;
4666 attr->dw_attr_val.val_class = dw_val_class_flag;
4667 attr->dw_attr_val.v.val_flag = flag;
4668 add_dwarf_attr (die, attr);
4671 static inline unsigned
4672 AT_flag (dw_attr_ref a)
4674 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4675 return a->dw_attr_val.v.val_flag;
4678 /* Add a signed integer attribute value to a DIE. */
4680 static inline void
4681 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4683 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4685 attr->dw_attr_next = NULL;
4686 attr->dw_attr = attr_kind;
4687 attr->dw_attr_val.val_class = dw_val_class_const;
4688 attr->dw_attr_val.v.val_int = int_val;
4689 add_dwarf_attr (die, attr);
4692 static inline HOST_WIDE_INT
4693 AT_int (dw_attr_ref a)
4695 gcc_assert (a && AT_class (a) == dw_val_class_const);
4696 return a->dw_attr_val.v.val_int;
4699 /* Add an unsigned integer attribute value to a DIE. */
4701 static inline void
4702 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4703 unsigned HOST_WIDE_INT unsigned_val)
4705 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4707 attr->dw_attr_next = NULL;
4708 attr->dw_attr = attr_kind;
4709 attr->dw_attr_val.val_class = dw_val_class_unsigned_const;
4710 attr->dw_attr_val.v.val_unsigned = unsigned_val;
4711 add_dwarf_attr (die, attr);
4714 static inline unsigned HOST_WIDE_INT
4715 AT_unsigned (dw_attr_ref a)
4717 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
4718 return a->dw_attr_val.v.val_unsigned;
4721 /* Add an unsigned double integer attribute value to a DIE. */
4723 static inline void
4724 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
4725 long unsigned int val_hi, long unsigned int val_low)
4727 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4729 attr->dw_attr_next = NULL;
4730 attr->dw_attr = attr_kind;
4731 attr->dw_attr_val.val_class = dw_val_class_long_long;
4732 attr->dw_attr_val.v.val_long_long.hi = val_hi;
4733 attr->dw_attr_val.v.val_long_long.low = val_low;
4734 add_dwarf_attr (die, attr);
4737 /* Add a floating point attribute value to a DIE and return it. */
4739 static inline void
4740 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4741 unsigned int length, unsigned int elt_size, unsigned char *array)
4743 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4745 attr->dw_attr_next = NULL;
4746 attr->dw_attr = attr_kind;
4747 attr->dw_attr_val.val_class = dw_val_class_vec;
4748 attr->dw_attr_val.v.val_vec.length = length;
4749 attr->dw_attr_val.v.val_vec.elt_size = elt_size;
4750 attr->dw_attr_val.v.val_vec.array = array;
4751 add_dwarf_attr (die, attr);
4754 /* Hash and equality functions for debug_str_hash. */
4756 static hashval_t
4757 debug_str_do_hash (const void *x)
4759 return htab_hash_string (((const struct indirect_string_node *)x)->str);
4762 static int
4763 debug_str_eq (const void *x1, const void *x2)
4765 return strcmp ((((const struct indirect_string_node *)x1)->str),
4766 (const char *)x2) == 0;
4769 /* Add a string attribute value to a DIE. */
4771 static inline void
4772 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4774 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4775 struct indirect_string_node *node;
4776 void **slot;
4778 if (! debug_str_hash)
4779 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
4780 debug_str_eq, NULL);
4782 slot = htab_find_slot_with_hash (debug_str_hash, str,
4783 htab_hash_string (str), INSERT);
4784 if (*slot == NULL)
4785 *slot = ggc_alloc_cleared (sizeof (struct indirect_string_node));
4786 node = (struct indirect_string_node *) *slot;
4787 node->str = ggc_strdup (str);
4788 node->refcount++;
4790 attr->dw_attr_next = NULL;
4791 attr->dw_attr = attr_kind;
4792 attr->dw_attr_val.val_class = dw_val_class_str;
4793 attr->dw_attr_val.v.val_str = node;
4794 add_dwarf_attr (die, attr);
4797 static inline const char *
4798 AT_string (dw_attr_ref a)
4800 gcc_assert (a && AT_class (a) == dw_val_class_str);
4801 return a->dw_attr_val.v.val_str->str;
4804 /* Find out whether a string should be output inline in DIE
4805 or out-of-line in .debug_str section. */
4807 static int
4808 AT_string_form (dw_attr_ref a)
4810 struct indirect_string_node *node;
4811 unsigned int len;
4812 char label[32];
4814 gcc_assert (a && AT_class (a) == dw_val_class_str);
4816 node = a->dw_attr_val.v.val_str;
4817 if (node->form)
4818 return node->form;
4820 len = strlen (node->str) + 1;
4822 /* If the string is shorter or equal to the size of the reference, it is
4823 always better to put it inline. */
4824 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4825 return node->form = DW_FORM_string;
4827 /* If we cannot expect the linker to merge strings in .debug_str
4828 section, only put it into .debug_str if it is worth even in this
4829 single module. */
4830 if ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) == 0
4831 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
4832 return node->form = DW_FORM_string;
4834 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4835 ++dw2_string_counter;
4836 node->label = xstrdup (label);
4838 return node->form = DW_FORM_strp;
4841 /* Add a DIE reference attribute value to a DIE. */
4843 static inline void
4844 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4846 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4848 attr->dw_attr_next = NULL;
4849 attr->dw_attr = attr_kind;
4850 attr->dw_attr_val.val_class = dw_val_class_die_ref;
4851 attr->dw_attr_val.v.val_die_ref.die = targ_die;
4852 attr->dw_attr_val.v.val_die_ref.external = 0;
4853 add_dwarf_attr (die, attr);
4856 /* Add an AT_specification attribute to a DIE, and also make the back
4857 pointer from the specification to the definition. */
4859 static inline void
4860 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4862 add_AT_die_ref (die, DW_AT_specification, targ_die);
4863 gcc_assert (!targ_die->die_definition);
4864 targ_die->die_definition = die;
4867 static inline dw_die_ref
4868 AT_ref (dw_attr_ref a)
4870 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4871 return a->dw_attr_val.v.val_die_ref.die;
4874 static inline int
4875 AT_ref_external (dw_attr_ref a)
4877 if (a && AT_class (a) == dw_val_class_die_ref)
4878 return a->dw_attr_val.v.val_die_ref.external;
4880 return 0;
4883 static inline void
4884 set_AT_ref_external (dw_attr_ref a, int i)
4886 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4887 a->dw_attr_val.v.val_die_ref.external = i;
4890 /* Add an FDE reference attribute value to a DIE. */
4892 static inline void
4893 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4895 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4897 attr->dw_attr_next = NULL;
4898 attr->dw_attr = attr_kind;
4899 attr->dw_attr_val.val_class = dw_val_class_fde_ref;
4900 attr->dw_attr_val.v.val_fde_index = targ_fde;
4901 add_dwarf_attr (die, attr);
4904 /* Add a location description attribute value to a DIE. */
4906 static inline void
4907 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4909 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4911 attr->dw_attr_next = NULL;
4912 attr->dw_attr = attr_kind;
4913 attr->dw_attr_val.val_class = dw_val_class_loc;
4914 attr->dw_attr_val.v.val_loc = loc;
4915 add_dwarf_attr (die, attr);
4918 static inline dw_loc_descr_ref
4919 AT_loc (dw_attr_ref a)
4921 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4922 return a->dw_attr_val.v.val_loc;
4925 static inline void
4926 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4928 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4930 attr->dw_attr_next = NULL;
4931 attr->dw_attr = attr_kind;
4932 attr->dw_attr_val.val_class = dw_val_class_loc_list;
4933 attr->dw_attr_val.v.val_loc_list = loc_list;
4934 add_dwarf_attr (die, attr);
4935 have_location_lists = 1;
4938 static inline dw_loc_list_ref
4939 AT_loc_list (dw_attr_ref a)
4941 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4942 return a->dw_attr_val.v.val_loc_list;
4945 /* Add an address constant attribute value to a DIE. */
4947 static inline void
4948 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
4950 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4952 attr->dw_attr_next = NULL;
4953 attr->dw_attr = attr_kind;
4954 attr->dw_attr_val.val_class = dw_val_class_addr;
4955 attr->dw_attr_val.v.val_addr = addr;
4956 add_dwarf_attr (die, attr);
4959 static inline rtx
4960 AT_addr (dw_attr_ref a)
4962 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4963 return a->dw_attr_val.v.val_addr;
4966 /* Add a label identifier attribute value to a DIE. */
4968 static inline void
4969 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
4971 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4973 attr->dw_attr_next = NULL;
4974 attr->dw_attr = attr_kind;
4975 attr->dw_attr_val.val_class = dw_val_class_lbl_id;
4976 attr->dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4977 add_dwarf_attr (die, attr);
4980 /* Add a section offset attribute value to a DIE. */
4982 static inline void
4983 add_AT_lbl_offset (dw_die_ref die, enum dwarf_attribute attr_kind, const char *label)
4985 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4987 attr->dw_attr_next = NULL;
4988 attr->dw_attr = attr_kind;
4989 attr->dw_attr_val.val_class = dw_val_class_lbl_offset;
4990 attr->dw_attr_val.v.val_lbl_id = xstrdup (label);
4991 add_dwarf_attr (die, attr);
4994 /* Add an offset attribute value to a DIE. */
4996 static inline void
4997 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4998 unsigned HOST_WIDE_INT offset)
5000 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5002 attr->dw_attr_next = NULL;
5003 attr->dw_attr = attr_kind;
5004 attr->dw_attr_val.val_class = dw_val_class_offset;
5005 attr->dw_attr_val.v.val_offset = offset;
5006 add_dwarf_attr (die, attr);
5009 /* Add an range_list attribute value to a DIE. */
5011 static void
5012 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5013 long unsigned int offset)
5015 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5017 attr->dw_attr_next = NULL;
5018 attr->dw_attr = attr_kind;
5019 attr->dw_attr_val.val_class = dw_val_class_range_list;
5020 attr->dw_attr_val.v.val_offset = offset;
5021 add_dwarf_attr (die, attr);
5024 static inline const char *
5025 AT_lbl (dw_attr_ref a)
5027 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5028 || AT_class (a) == dw_val_class_lbl_offset));
5029 return a->dw_attr_val.v.val_lbl_id;
5032 /* Get the attribute of type attr_kind. */
5034 static dw_attr_ref
5035 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5037 dw_attr_ref a;
5038 dw_die_ref spec = NULL;
5040 if (die != NULL)
5042 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
5043 if (a->dw_attr == attr_kind)
5044 return a;
5045 else if (a->dw_attr == DW_AT_specification
5046 || a->dw_attr == DW_AT_abstract_origin)
5047 spec = AT_ref (a);
5049 if (spec)
5050 return get_AT (spec, attr_kind);
5053 return NULL;
5056 /* Return the "low pc" attribute value, typically associated with a subprogram
5057 DIE. Return null if the "low pc" attribute is either not present, or if it
5058 cannot be represented as an assembler label identifier. */
5060 static inline const char *
5061 get_AT_low_pc (dw_die_ref die)
5063 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
5065 return a ? AT_lbl (a) : NULL;
5068 /* Return the "high pc" attribute value, typically associated with a subprogram
5069 DIE. Return null if the "high pc" attribute is either not present, or if it
5070 cannot be represented as an assembler label identifier. */
5072 static inline const char *
5073 get_AT_hi_pc (dw_die_ref die)
5075 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
5077 return a ? AT_lbl (a) : NULL;
5080 /* Return the value of the string attribute designated by ATTR_KIND, or
5081 NULL if it is not present. */
5083 static inline const char *
5084 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5086 dw_attr_ref a = get_AT (die, attr_kind);
5088 return a ? AT_string (a) : NULL;
5091 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5092 if it is not present. */
5094 static inline int
5095 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5097 dw_attr_ref a = get_AT (die, attr_kind);
5099 return a ? AT_flag (a) : 0;
5102 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5103 if it is not present. */
5105 static inline unsigned
5106 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5108 dw_attr_ref a = get_AT (die, attr_kind);
5110 return a ? AT_unsigned (a) : 0;
5113 static inline dw_die_ref
5114 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5116 dw_attr_ref a = get_AT (die, attr_kind);
5118 return a ? AT_ref (a) : NULL;
5121 /* Return TRUE if the language is C or C++. */
5123 static inline bool
5124 is_c_family (void)
5126 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5128 return (lang == DW_LANG_C || lang == DW_LANG_C89
5129 || lang == DW_LANG_C_plus_plus);
5132 /* Return TRUE if the language is C++. */
5134 static inline bool
5135 is_cxx (void)
5137 return (get_AT_unsigned (comp_unit_die, DW_AT_language)
5138 == DW_LANG_C_plus_plus);
5141 /* Return TRUE if the language is Fortran. */
5143 static inline bool
5144 is_fortran (void)
5146 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5148 return (lang == DW_LANG_Fortran77
5149 || lang == DW_LANG_Fortran90
5150 || lang == DW_LANG_Fortran95);
5153 /* Return TRUE if the language is Java. */
5155 static inline bool
5156 is_java (void)
5158 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5160 return lang == DW_LANG_Java;
5163 /* Return TRUE if the language is Ada. */
5165 static inline bool
5166 is_ada (void)
5168 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5170 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5173 /* Free up the memory used by A. */
5175 static inline void free_AT (dw_attr_ref);
5176 static inline void
5177 free_AT (dw_attr_ref a)
5179 if (AT_class (a) == dw_val_class_str)
5180 if (a->dw_attr_val.v.val_str->refcount)
5181 a->dw_attr_val.v.val_str->refcount--;
5184 /* Remove the specified attribute if present. */
5186 static void
5187 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5189 dw_attr_ref *p;
5190 dw_attr_ref removed = NULL;
5192 if (die != NULL)
5194 for (p = &(die->die_attr); *p; p = &((*p)->dw_attr_next))
5195 if ((*p)->dw_attr == attr_kind)
5197 removed = *p;
5198 *p = (*p)->dw_attr_next;
5199 break;
5202 if (removed != 0)
5203 free_AT (removed);
5207 /* Remove child die whose die_tag is specified tag. */
5209 static void
5210 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5212 dw_die_ref current, prev, next;
5213 current = die->die_child;
5214 prev = NULL;
5215 while (current != NULL)
5217 if (current->die_tag == tag)
5219 next = current->die_sib;
5220 if (prev == NULL)
5221 die->die_child = next;
5222 else
5223 prev->die_sib = next;
5224 free_die (current);
5225 current = next;
5227 else
5229 prev = current;
5230 current = current->die_sib;
5235 /* Free up the memory used by DIE. */
5237 static inline void
5238 free_die (dw_die_ref die)
5240 remove_children (die);
5243 /* Discard the children of this DIE. */
5245 static void
5246 remove_children (dw_die_ref die)
5248 dw_die_ref child_die = die->die_child;
5250 die->die_child = NULL;
5252 while (child_die != NULL)
5254 dw_die_ref tmp_die = child_die;
5255 dw_attr_ref a;
5257 child_die = child_die->die_sib;
5259 for (a = tmp_die->die_attr; a != NULL;)
5261 dw_attr_ref tmp_a = a;
5263 a = a->dw_attr_next;
5264 free_AT (tmp_a);
5267 free_die (tmp_die);
5271 /* Add a child DIE below its parent. We build the lists up in reverse
5272 addition order, and correct that in reverse_all_dies. */
5274 static inline void
5275 add_child_die (dw_die_ref die, dw_die_ref child_die)
5277 if (die != NULL && child_die != NULL)
5279 gcc_assert (die != child_die);
5281 child_die->die_parent = die;
5282 child_die->die_sib = die->die_child;
5283 die->die_child = child_die;
5287 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5288 is the specification, to the front of PARENT's list of children. */
5290 static void
5291 splice_child_die (dw_die_ref parent, dw_die_ref child)
5293 dw_die_ref *p;
5295 /* We want the declaration DIE from inside the class, not the
5296 specification DIE at toplevel. */
5297 if (child->die_parent != parent)
5299 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5301 if (tmp)
5302 child = tmp;
5305 gcc_assert (child->die_parent == parent
5306 || (child->die_parent
5307 == get_AT_ref (parent, DW_AT_specification)));
5309 for (p = &(child->die_parent->die_child); *p; p = &((*p)->die_sib))
5310 if (*p == child)
5312 *p = child->die_sib;
5313 break;
5316 child->die_parent = parent;
5317 child->die_sib = parent->die_child;
5318 parent->die_child = child;
5321 /* Return a pointer to a newly created DIE node. */
5323 static inline dw_die_ref
5324 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5326 dw_die_ref die = ggc_alloc_cleared (sizeof (die_node));
5328 die->die_tag = tag_value;
5330 if (parent_die != NULL)
5331 add_child_die (parent_die, die);
5332 else
5334 limbo_die_node *limbo_node;
5336 limbo_node = ggc_alloc_cleared (sizeof (limbo_die_node));
5337 limbo_node->die = die;
5338 limbo_node->created_for = t;
5339 limbo_node->next = limbo_die_list;
5340 limbo_die_list = limbo_node;
5343 return die;
5346 /* Return the DIE associated with the given type specifier. */
5348 static inline dw_die_ref
5349 lookup_type_die (tree type)
5351 return TYPE_SYMTAB_DIE (type);
5354 /* Equate a DIE to a given type specifier. */
5356 static inline void
5357 equate_type_number_to_die (tree type, dw_die_ref type_die)
5359 TYPE_SYMTAB_DIE (type) = type_die;
5362 /* Returns a hash value for X (which really is a die_struct). */
5364 static hashval_t
5365 decl_die_table_hash (const void *x)
5367 return (hashval_t) ((const dw_die_ref) x)->decl_id;
5370 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5372 static int
5373 decl_die_table_eq (const void *x, const void *y)
5375 return (((const dw_die_ref) x)->decl_id == DECL_UID ((const tree) y));
5378 /* Return the DIE associated with a given declaration. */
5380 static inline dw_die_ref
5381 lookup_decl_die (tree decl)
5383 return htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
5386 /* Returns a hash value for X (which really is a var_loc_list). */
5388 static hashval_t
5389 decl_loc_table_hash (const void *x)
5391 return (hashval_t) ((const var_loc_list *) x)->decl_id;
5394 /* Return nonzero if decl_id of var_loc_list X is the same as
5395 UID of decl *Y. */
5397 static int
5398 decl_loc_table_eq (const void *x, const void *y)
5400 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const tree) y));
5403 /* Return the var_loc list associated with a given declaration. */
5405 static inline var_loc_list *
5406 lookup_decl_loc (tree decl)
5408 return htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5411 /* Equate a DIE to a particular declaration. */
5413 static void
5414 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5416 unsigned int decl_id = DECL_UID (decl);
5417 void **slot;
5419 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5420 *slot = decl_die;
5421 decl_die->decl_id = decl_id;
5424 /* Add a variable location node to the linked list for DECL. */
5426 static void
5427 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
5429 unsigned int decl_id = DECL_UID (decl);
5430 var_loc_list *temp;
5431 void **slot;
5433 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5434 if (*slot == NULL)
5436 temp = ggc_alloc_cleared (sizeof (var_loc_list));
5437 temp->decl_id = decl_id;
5438 *slot = temp;
5440 else
5441 temp = *slot;
5443 if (temp->last)
5445 /* If the current location is the same as the end of the list,
5446 we have nothing to do. */
5447 if (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
5448 NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
5450 /* Add LOC to the end of list and update LAST. */
5451 temp->last->next = loc;
5452 temp->last = loc;
5455 /* Do not add empty location to the beginning of the list. */
5456 else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
5458 temp->first = loc;
5459 temp->last = loc;
5463 /* Keep track of the number of spaces used to indent the
5464 output of the debugging routines that print the structure of
5465 the DIE internal representation. */
5466 static int print_indent;
5468 /* Indent the line the number of spaces given by print_indent. */
5470 static inline void
5471 print_spaces (FILE *outfile)
5473 fprintf (outfile, "%*s", print_indent, "");
5476 /* Print the information associated with a given DIE, and its children.
5477 This routine is a debugging aid only. */
5479 static void
5480 print_die (dw_die_ref die, FILE *outfile)
5482 dw_attr_ref a;
5483 dw_die_ref c;
5485 print_spaces (outfile);
5486 fprintf (outfile, "DIE %4lu: %s\n",
5487 die->die_offset, dwarf_tag_name (die->die_tag));
5488 print_spaces (outfile);
5489 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5490 fprintf (outfile, " offset: %lu\n", die->die_offset);
5492 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
5494 print_spaces (outfile);
5495 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5497 switch (AT_class (a))
5499 case dw_val_class_addr:
5500 fprintf (outfile, "address");
5501 break;
5502 case dw_val_class_offset:
5503 fprintf (outfile, "offset");
5504 break;
5505 case dw_val_class_loc:
5506 fprintf (outfile, "location descriptor");
5507 break;
5508 case dw_val_class_loc_list:
5509 fprintf (outfile, "location list -> label:%s",
5510 AT_loc_list (a)->ll_symbol);
5511 break;
5512 case dw_val_class_range_list:
5513 fprintf (outfile, "range list");
5514 break;
5515 case dw_val_class_const:
5516 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5517 break;
5518 case dw_val_class_unsigned_const:
5519 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5520 break;
5521 case dw_val_class_long_long:
5522 fprintf (outfile, "constant (%lu,%lu)",
5523 a->dw_attr_val.v.val_long_long.hi,
5524 a->dw_attr_val.v.val_long_long.low);
5525 break;
5526 case dw_val_class_vec:
5527 fprintf (outfile, "floating-point or vector constant");
5528 break;
5529 case dw_val_class_flag:
5530 fprintf (outfile, "%u", AT_flag (a));
5531 break;
5532 case dw_val_class_die_ref:
5533 if (AT_ref (a) != NULL)
5535 if (AT_ref (a)->die_symbol)
5536 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
5537 else
5538 fprintf (outfile, "die -> %lu", AT_ref (a)->die_offset);
5540 else
5541 fprintf (outfile, "die -> <null>");
5542 break;
5543 case dw_val_class_lbl_id:
5544 case dw_val_class_lbl_offset:
5545 fprintf (outfile, "label: %s", AT_lbl (a));
5546 break;
5547 case dw_val_class_str:
5548 if (AT_string (a) != NULL)
5549 fprintf (outfile, "\"%s\"", AT_string (a));
5550 else
5551 fprintf (outfile, "<null>");
5552 break;
5553 default:
5554 break;
5557 fprintf (outfile, "\n");
5560 if (die->die_child != NULL)
5562 print_indent += 4;
5563 for (c = die->die_child; c != NULL; c = c->die_sib)
5564 print_die (c, outfile);
5566 print_indent -= 4;
5568 if (print_indent == 0)
5569 fprintf (outfile, "\n");
5572 /* Print the contents of the source code line number correspondence table.
5573 This routine is a debugging aid only. */
5575 static void
5576 print_dwarf_line_table (FILE *outfile)
5578 unsigned i;
5579 dw_line_info_ref line_info;
5581 fprintf (outfile, "\n\nDWARF source line information\n");
5582 for (i = 1; i < line_info_table_in_use; i++)
5584 line_info = &line_info_table[i];
5585 fprintf (outfile, "%5d: ", i);
5586 fprintf (outfile, "%-20s",
5587 VARRAY_CHAR_PTR (file_table, line_info->dw_file_num));
5588 fprintf (outfile, "%6ld", line_info->dw_line_num);
5589 fprintf (outfile, "\n");
5592 fprintf (outfile, "\n\n");
5595 /* Print the information collected for a given DIE. */
5597 void
5598 debug_dwarf_die (dw_die_ref die)
5600 print_die (die, stderr);
5603 /* Print all DWARF information collected for the compilation unit.
5604 This routine is a debugging aid only. */
5606 void
5607 debug_dwarf (void)
5609 print_indent = 0;
5610 print_die (comp_unit_die, stderr);
5611 if (! DWARF2_ASM_LINE_DEBUG_INFO)
5612 print_dwarf_line_table (stderr);
5615 /* We build up the lists of children and attributes by pushing new ones
5616 onto the beginning of the list. Reverse the lists for DIE so that
5617 they are in order of addition. */
5619 static void
5620 reverse_die_lists (dw_die_ref die)
5622 dw_die_ref c, cp, cn;
5623 dw_attr_ref a, ap, an;
5625 for (a = die->die_attr, ap = 0; a; a = an)
5627 an = a->dw_attr_next;
5628 a->dw_attr_next = ap;
5629 ap = a;
5632 die->die_attr = ap;
5634 for (c = die->die_child, cp = 0; c; c = cn)
5636 cn = c->die_sib;
5637 c->die_sib = cp;
5638 cp = c;
5641 die->die_child = cp;
5644 /* reverse_die_lists only reverses the single die you pass it. Since we used to
5645 reverse all dies in add_sibling_attributes, which runs through all the dies,
5646 it would reverse all the dies. Now, however, since we don't call
5647 reverse_die_lists in add_sibling_attributes, we need a routine to
5648 recursively reverse all the dies. This is that routine. */
5650 static void
5651 reverse_all_dies (dw_die_ref die)
5653 dw_die_ref c;
5655 reverse_die_lists (die);
5657 for (c = die->die_child; c; c = c->die_sib)
5658 reverse_all_dies (c);
5661 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5662 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5663 DIE that marks the start of the DIEs for this include file. */
5665 static dw_die_ref
5666 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5668 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5669 dw_die_ref new_unit = gen_compile_unit_die (filename);
5671 new_unit->die_sib = old_unit;
5672 return new_unit;
5675 /* Close an include-file CU and reopen the enclosing one. */
5677 static dw_die_ref
5678 pop_compile_unit (dw_die_ref old_unit)
5680 dw_die_ref new_unit = old_unit->die_sib;
5682 old_unit->die_sib = NULL;
5683 return new_unit;
5686 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5687 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5689 /* Calculate the checksum of a location expression. */
5691 static inline void
5692 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5694 CHECKSUM (loc->dw_loc_opc);
5695 CHECKSUM (loc->dw_loc_oprnd1);
5696 CHECKSUM (loc->dw_loc_oprnd2);
5699 /* Calculate the checksum of an attribute. */
5701 static void
5702 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5704 dw_loc_descr_ref loc;
5705 rtx r;
5707 CHECKSUM (at->dw_attr);
5709 /* We don't care about differences in file numbering. */
5710 if (at->dw_attr == DW_AT_decl_file
5711 /* Or that this was compiled with a different compiler snapshot; if
5712 the output is the same, that's what matters. */
5713 || at->dw_attr == DW_AT_producer)
5714 return;
5716 switch (AT_class (at))
5718 case dw_val_class_const:
5719 CHECKSUM (at->dw_attr_val.v.val_int);
5720 break;
5721 case dw_val_class_unsigned_const:
5722 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5723 break;
5724 case dw_val_class_long_long:
5725 CHECKSUM (at->dw_attr_val.v.val_long_long);
5726 break;
5727 case dw_val_class_vec:
5728 CHECKSUM (at->dw_attr_val.v.val_vec);
5729 break;
5730 case dw_val_class_flag:
5731 CHECKSUM (at->dw_attr_val.v.val_flag);
5732 break;
5733 case dw_val_class_str:
5734 CHECKSUM_STRING (AT_string (at));
5735 break;
5737 case dw_val_class_addr:
5738 r = AT_addr (at);
5739 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5740 CHECKSUM_STRING (XSTR (r, 0));
5741 break;
5743 case dw_val_class_offset:
5744 CHECKSUM (at->dw_attr_val.v.val_offset);
5745 break;
5747 case dw_val_class_loc:
5748 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5749 loc_checksum (loc, ctx);
5750 break;
5752 case dw_val_class_die_ref:
5753 die_checksum (AT_ref (at), ctx, mark);
5754 break;
5756 case dw_val_class_fde_ref:
5757 case dw_val_class_lbl_id:
5758 case dw_val_class_lbl_offset:
5759 break;
5761 default:
5762 break;
5766 /* Calculate the checksum of a DIE. */
5768 static void
5769 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5771 dw_die_ref c;
5772 dw_attr_ref a;
5774 /* To avoid infinite recursion. */
5775 if (die->die_mark)
5777 CHECKSUM (die->die_mark);
5778 return;
5780 die->die_mark = ++(*mark);
5782 CHECKSUM (die->die_tag);
5784 for (a = die->die_attr; a; a = a->dw_attr_next)
5785 attr_checksum (a, ctx, mark);
5787 for (c = die->die_child; c; c = c->die_sib)
5788 die_checksum (c, ctx, mark);
5791 #undef CHECKSUM
5792 #undef CHECKSUM_STRING
5794 /* Do the location expressions look same? */
5795 static inline int
5796 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
5798 return loc1->dw_loc_opc == loc2->dw_loc_opc
5799 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
5800 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
5803 /* Do the values look the same? */
5804 static int
5805 same_dw_val_p (dw_val_node *v1, dw_val_node *v2, int *mark)
5807 dw_loc_descr_ref loc1, loc2;
5808 rtx r1, r2;
5810 if (v1->val_class != v2->val_class)
5811 return 0;
5813 switch (v1->val_class)
5815 case dw_val_class_const:
5816 return v1->v.val_int == v2->v.val_int;
5817 case dw_val_class_unsigned_const:
5818 return v1->v.val_unsigned == v2->v.val_unsigned;
5819 case dw_val_class_long_long:
5820 return v1->v.val_long_long.hi == v2->v.val_long_long.hi
5821 && v1->v.val_long_long.low == v2->v.val_long_long.low;
5822 case dw_val_class_vec:
5823 if (v1->v.val_vec.length != v2->v.val_vec.length
5824 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
5825 return 0;
5826 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
5827 v1->v.val_vec.length * v1->v.val_vec.elt_size))
5828 return 0;
5829 return 1;
5830 case dw_val_class_flag:
5831 return v1->v.val_flag == v2->v.val_flag;
5832 case dw_val_class_str:
5833 return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
5835 case dw_val_class_addr:
5836 r1 = v1->v.val_addr;
5837 r2 = v2->v.val_addr;
5838 if (GET_CODE (r1) != GET_CODE (r2))
5839 return 0;
5840 gcc_assert (GET_CODE (r1) == SYMBOL_REF);
5841 return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
5843 case dw_val_class_offset:
5844 return v1->v.val_offset == v2->v.val_offset;
5846 case dw_val_class_loc:
5847 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
5848 loc1 && loc2;
5849 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
5850 if (!same_loc_p (loc1, loc2, mark))
5851 return 0;
5852 return !loc1 && !loc2;
5854 case dw_val_class_die_ref:
5855 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
5857 case dw_val_class_fde_ref:
5858 case dw_val_class_lbl_id:
5859 case dw_val_class_lbl_offset:
5860 return 1;
5862 default:
5863 return 1;
5867 /* Do the attributes look the same? */
5869 static int
5870 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
5872 if (at1->dw_attr != at2->dw_attr)
5873 return 0;
5875 /* We don't care about differences in file numbering. */
5876 if (at1->dw_attr == DW_AT_decl_file
5877 /* Or that this was compiled with a different compiler snapshot; if
5878 the output is the same, that's what matters. */
5879 || at1->dw_attr == DW_AT_producer)
5880 return 1;
5882 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
5885 /* Do the dies look the same? */
5887 static int
5888 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
5890 dw_die_ref c1, c2;
5891 dw_attr_ref a1, a2;
5893 /* To avoid infinite recursion. */
5894 if (die1->die_mark)
5895 return die1->die_mark == die2->die_mark;
5896 die1->die_mark = die2->die_mark = ++(*mark);
5898 if (die1->die_tag != die2->die_tag)
5899 return 0;
5901 for (a1 = die1->die_attr, a2 = die2->die_attr;
5902 a1 && a2;
5903 a1 = a1->dw_attr_next, a2 = a2->dw_attr_next)
5904 if (!same_attr_p (a1, a2, mark))
5905 return 0;
5906 if (a1 || a2)
5907 return 0;
5909 for (c1 = die1->die_child, c2 = die2->die_child;
5910 c1 && c2;
5911 c1 = c1->die_sib, c2 = c2->die_sib)
5912 if (!same_die_p (c1, c2, mark))
5913 return 0;
5914 if (c1 || c2)
5915 return 0;
5917 return 1;
5920 /* Do the dies look the same? Wrapper around same_die_p. */
5922 static int
5923 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
5925 int mark = 0;
5926 int ret = same_die_p (die1, die2, &mark);
5928 unmark_all_dies (die1);
5929 unmark_all_dies (die2);
5931 return ret;
5934 /* The prefix to attach to symbols on DIEs in the current comdat debug
5935 info section. */
5936 static char *comdat_symbol_id;
5938 /* The index of the current symbol within the current comdat CU. */
5939 static unsigned int comdat_symbol_number;
5941 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
5942 children, and set comdat_symbol_id accordingly. */
5944 static void
5945 compute_section_prefix (dw_die_ref unit_die)
5947 const char *die_name = get_AT_string (unit_die, DW_AT_name);
5948 const char *base = die_name ? lbasename (die_name) : "anonymous";
5949 char *name = alloca (strlen (base) + 64);
5950 char *p;
5951 int i, mark;
5952 unsigned char checksum[16];
5953 struct md5_ctx ctx;
5955 /* Compute the checksum of the DIE, then append part of it as hex digits to
5956 the name filename of the unit. */
5958 md5_init_ctx (&ctx);
5959 mark = 0;
5960 die_checksum (unit_die, &ctx, &mark);
5961 unmark_all_dies (unit_die);
5962 md5_finish_ctx (&ctx, checksum);
5964 sprintf (name, "%s.", base);
5965 clean_symbol_name (name);
5967 p = name + strlen (name);
5968 for (i = 0; i < 4; i++)
5970 sprintf (p, "%.2x", checksum[i]);
5971 p += 2;
5974 comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
5975 comdat_symbol_number = 0;
5978 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
5980 static int
5981 is_type_die (dw_die_ref die)
5983 switch (die->die_tag)
5985 case DW_TAG_array_type:
5986 case DW_TAG_class_type:
5987 case DW_TAG_enumeration_type:
5988 case DW_TAG_pointer_type:
5989 case DW_TAG_reference_type:
5990 case DW_TAG_string_type:
5991 case DW_TAG_structure_type:
5992 case DW_TAG_subroutine_type:
5993 case DW_TAG_union_type:
5994 case DW_TAG_ptr_to_member_type:
5995 case DW_TAG_set_type:
5996 case DW_TAG_subrange_type:
5997 case DW_TAG_base_type:
5998 case DW_TAG_const_type:
5999 case DW_TAG_file_type:
6000 case DW_TAG_packed_type:
6001 case DW_TAG_volatile_type:
6002 case DW_TAG_typedef:
6003 return 1;
6004 default:
6005 return 0;
6009 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6010 Basically, we want to choose the bits that are likely to be shared between
6011 compilations (types) and leave out the bits that are specific to individual
6012 compilations (functions). */
6014 static int
6015 is_comdat_die (dw_die_ref c)
6017 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6018 we do for stabs. The advantage is a greater likelihood of sharing between
6019 objects that don't include headers in the same order (and therefore would
6020 put the base types in a different comdat). jason 8/28/00 */
6022 if (c->die_tag == DW_TAG_base_type)
6023 return 0;
6025 if (c->die_tag == DW_TAG_pointer_type
6026 || c->die_tag == DW_TAG_reference_type
6027 || c->die_tag == DW_TAG_const_type
6028 || c->die_tag == DW_TAG_volatile_type)
6030 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6032 return t ? is_comdat_die (t) : 0;
6035 return is_type_die (c);
6038 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6039 compilation unit. */
6041 static int
6042 is_symbol_die (dw_die_ref c)
6044 return (is_type_die (c)
6045 || (get_AT (c, DW_AT_declaration)
6046 && !get_AT (c, DW_AT_specification)));
6049 static char *
6050 gen_internal_sym (const char *prefix)
6052 char buf[256];
6054 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6055 return xstrdup (buf);
6058 /* Assign symbols to all worthy DIEs under DIE. */
6060 static void
6061 assign_symbol_names (dw_die_ref die)
6063 dw_die_ref c;
6065 if (is_symbol_die (die))
6067 if (comdat_symbol_id)
6069 char *p = alloca (strlen (comdat_symbol_id) + 64);
6071 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6072 comdat_symbol_id, comdat_symbol_number++);
6073 die->die_symbol = xstrdup (p);
6075 else
6076 die->die_symbol = gen_internal_sym ("LDIE");
6079 for (c = die->die_child; c != NULL; c = c->die_sib)
6080 assign_symbol_names (c);
6083 struct cu_hash_table_entry
6085 dw_die_ref cu;
6086 unsigned min_comdat_num, max_comdat_num;
6087 struct cu_hash_table_entry *next;
6090 /* Routines to manipulate hash table of CUs. */
6091 static hashval_t
6092 htab_cu_hash (const void *of)
6094 const struct cu_hash_table_entry *entry = of;
6096 return htab_hash_string (entry->cu->die_symbol);
6099 static int
6100 htab_cu_eq (const void *of1, const void *of2)
6102 const struct cu_hash_table_entry *entry1 = of1;
6103 const struct die_struct *entry2 = of2;
6105 return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
6108 static void
6109 htab_cu_del (void *what)
6111 struct cu_hash_table_entry *next, *entry = what;
6113 while (entry)
6115 next = entry->next;
6116 free (entry);
6117 entry = next;
6121 /* Check whether we have already seen this CU and set up SYM_NUM
6122 accordingly. */
6123 static int
6124 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6126 struct cu_hash_table_entry dummy;
6127 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6129 dummy.max_comdat_num = 0;
6131 slot = (struct cu_hash_table_entry **)
6132 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6133 INSERT);
6134 entry = *slot;
6136 for (; entry; last = entry, entry = entry->next)
6138 if (same_die_p_wrap (cu, entry->cu))
6139 break;
6142 if (entry)
6144 *sym_num = entry->min_comdat_num;
6145 return 1;
6148 entry = xcalloc (1, sizeof (struct cu_hash_table_entry));
6149 entry->cu = cu;
6150 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6151 entry->next = *slot;
6152 *slot = entry;
6154 return 0;
6157 /* Record SYM_NUM to record of CU in HTABLE. */
6158 static void
6159 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6161 struct cu_hash_table_entry **slot, *entry;
6163 slot = (struct cu_hash_table_entry **)
6164 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6165 NO_INSERT);
6166 entry = *slot;
6168 entry->max_comdat_num = sym_num;
6171 /* Traverse the DIE (which is always comp_unit_die), and set up
6172 additional compilation units for each of the include files we see
6173 bracketed by BINCL/EINCL. */
6175 static void
6176 break_out_includes (dw_die_ref die)
6178 dw_die_ref *ptr;
6179 dw_die_ref unit = NULL;
6180 limbo_die_node *node, **pnode;
6181 htab_t cu_hash_table;
6183 for (ptr = &(die->die_child); *ptr;)
6185 dw_die_ref c = *ptr;
6187 if (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6188 || (unit && is_comdat_die (c)))
6190 /* This DIE is for a secondary CU; remove it from the main one. */
6191 *ptr = c->die_sib;
6193 if (c->die_tag == DW_TAG_GNU_BINCL)
6195 unit = push_new_compile_unit (unit, c);
6196 free_die (c);
6198 else if (c->die_tag == DW_TAG_GNU_EINCL)
6200 unit = pop_compile_unit (unit);
6201 free_die (c);
6203 else
6204 add_child_die (unit, c);
6206 else
6208 /* Leave this DIE in the main CU. */
6209 ptr = &(c->die_sib);
6210 continue;
6214 #if 0
6215 /* We can only use this in debugging, since the frontend doesn't check
6216 to make sure that we leave every include file we enter. */
6217 gcc_assert (!unit);
6218 #endif
6220 assign_symbol_names (die);
6221 cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6222 for (node = limbo_die_list, pnode = &limbo_die_list;
6223 node;
6224 node = node->next)
6226 int is_dupl;
6228 compute_section_prefix (node->die);
6229 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6230 &comdat_symbol_number);
6231 assign_symbol_names (node->die);
6232 if (is_dupl)
6233 *pnode = node->next;
6234 else
6236 pnode = &node->next;
6237 record_comdat_symbol_number (node->die, cu_hash_table,
6238 comdat_symbol_number);
6241 htab_delete (cu_hash_table);
6244 /* Traverse the DIE and add a sibling attribute if it may have the
6245 effect of speeding up access to siblings. To save some space,
6246 avoid generating sibling attributes for DIE's without children. */
6248 static void
6249 add_sibling_attributes (dw_die_ref die)
6251 dw_die_ref c;
6253 if (die->die_tag != DW_TAG_compile_unit
6254 && die->die_sib && die->die_child != NULL)
6255 /* Add the sibling link to the front of the attribute list. */
6256 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
6258 for (c = die->die_child; c != NULL; c = c->die_sib)
6259 add_sibling_attributes (c);
6262 /* Output all location lists for the DIE and its children. */
6264 static void
6265 output_location_lists (dw_die_ref die)
6267 dw_die_ref c;
6268 dw_attr_ref d_attr;
6270 for (d_attr = die->die_attr; d_attr; d_attr = d_attr->dw_attr_next)
6271 if (AT_class (d_attr) == dw_val_class_loc_list)
6272 output_loc_list (AT_loc_list (d_attr));
6274 for (c = die->die_child; c != NULL; c = c->die_sib)
6275 output_location_lists (c);
6279 /* The format of each DIE (and its attribute value pairs) is encoded in an
6280 abbreviation table. This routine builds the abbreviation table and assigns
6281 a unique abbreviation id for each abbreviation entry. The children of each
6282 die are visited recursively. */
6284 static void
6285 build_abbrev_table (dw_die_ref die)
6287 unsigned long abbrev_id;
6288 unsigned int n_alloc;
6289 dw_die_ref c;
6290 dw_attr_ref d_attr, a_attr;
6292 /* Scan the DIE references, and mark as external any that refer to
6293 DIEs from other CUs (i.e. those which are not marked). */
6294 for (d_attr = die->die_attr; d_attr; d_attr = d_attr->dw_attr_next)
6295 if (AT_class (d_attr) == dw_val_class_die_ref
6296 && AT_ref (d_attr)->die_mark == 0)
6298 gcc_assert (AT_ref (d_attr)->die_symbol);
6300 set_AT_ref_external (d_attr, 1);
6303 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6305 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6307 if (abbrev->die_tag == die->die_tag)
6309 if ((abbrev->die_child != NULL) == (die->die_child != NULL))
6311 a_attr = abbrev->die_attr;
6312 d_attr = die->die_attr;
6314 while (a_attr != NULL && d_attr != NULL)
6316 if ((a_attr->dw_attr != d_attr->dw_attr)
6317 || (value_format (a_attr) != value_format (d_attr)))
6318 break;
6320 a_attr = a_attr->dw_attr_next;
6321 d_attr = d_attr->dw_attr_next;
6324 if (a_attr == NULL && d_attr == NULL)
6325 break;
6330 if (abbrev_id >= abbrev_die_table_in_use)
6332 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
6334 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
6335 abbrev_die_table = ggc_realloc (abbrev_die_table,
6336 sizeof (dw_die_ref) * n_alloc);
6338 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
6339 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
6340 abbrev_die_table_allocated = n_alloc;
6343 ++abbrev_die_table_in_use;
6344 abbrev_die_table[abbrev_id] = die;
6347 die->die_abbrev = abbrev_id;
6348 for (c = die->die_child; c != NULL; c = c->die_sib)
6349 build_abbrev_table (c);
6352 /* Return the power-of-two number of bytes necessary to represent VALUE. */
6354 static int
6355 constant_size (long unsigned int value)
6357 int log;
6359 if (value == 0)
6360 log = 0;
6361 else
6362 log = floor_log2 (value);
6364 log = log / 8;
6365 log = 1 << (floor_log2 (log) + 1);
6367 return log;
6370 /* Return the size of a DIE as it is represented in the
6371 .debug_info section. */
6373 static unsigned long
6374 size_of_die (dw_die_ref die)
6376 unsigned long size = 0;
6377 dw_attr_ref a;
6379 size += size_of_uleb128 (die->die_abbrev);
6380 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
6382 switch (AT_class (a))
6384 case dw_val_class_addr:
6385 size += DWARF2_ADDR_SIZE;
6386 break;
6387 case dw_val_class_offset:
6388 size += DWARF_OFFSET_SIZE;
6389 break;
6390 case dw_val_class_loc:
6392 unsigned long lsize = size_of_locs (AT_loc (a));
6394 /* Block length. */
6395 size += constant_size (lsize);
6396 size += lsize;
6398 break;
6399 case dw_val_class_loc_list:
6400 size += DWARF_OFFSET_SIZE;
6401 break;
6402 case dw_val_class_range_list:
6403 size += DWARF_OFFSET_SIZE;
6404 break;
6405 case dw_val_class_const:
6406 size += size_of_sleb128 (AT_int (a));
6407 break;
6408 case dw_val_class_unsigned_const:
6409 size += constant_size (AT_unsigned (a));
6410 break;
6411 case dw_val_class_long_long:
6412 size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
6413 break;
6414 case dw_val_class_vec:
6415 size += 1 + (a->dw_attr_val.v.val_vec.length
6416 * a->dw_attr_val.v.val_vec.elt_size); /* block */
6417 break;
6418 case dw_val_class_flag:
6419 size += 1;
6420 break;
6421 case dw_val_class_die_ref:
6422 if (AT_ref_external (a))
6423 size += DWARF2_ADDR_SIZE;
6424 else
6425 size += DWARF_OFFSET_SIZE;
6426 break;
6427 case dw_val_class_fde_ref:
6428 size += DWARF_OFFSET_SIZE;
6429 break;
6430 case dw_val_class_lbl_id:
6431 size += DWARF2_ADDR_SIZE;
6432 break;
6433 case dw_val_class_lbl_offset:
6434 size += DWARF_OFFSET_SIZE;
6435 break;
6436 case dw_val_class_str:
6437 if (AT_string_form (a) == DW_FORM_strp)
6438 size += DWARF_OFFSET_SIZE;
6439 else
6440 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
6441 break;
6442 default:
6443 gcc_unreachable ();
6447 return size;
6450 /* Size the debugging information associated with a given DIE. Visits the
6451 DIE's children recursively. Updates the global variable next_die_offset, on
6452 each time through. Uses the current value of next_die_offset to update the
6453 die_offset field in each DIE. */
6455 static void
6456 calc_die_sizes (dw_die_ref die)
6458 dw_die_ref c;
6460 die->die_offset = next_die_offset;
6461 next_die_offset += size_of_die (die);
6463 for (c = die->die_child; c != NULL; c = c->die_sib)
6464 calc_die_sizes (c);
6466 if (die->die_child != NULL)
6467 /* Count the null byte used to terminate sibling lists. */
6468 next_die_offset += 1;
6471 /* Set the marks for a die and its children. We do this so
6472 that we know whether or not a reference needs to use FORM_ref_addr; only
6473 DIEs in the same CU will be marked. We used to clear out the offset
6474 and use that as the flag, but ran into ordering problems. */
6476 static void
6477 mark_dies (dw_die_ref die)
6479 dw_die_ref c;
6481 gcc_assert (!die->die_mark);
6483 die->die_mark = 1;
6484 for (c = die->die_child; c; c = c->die_sib)
6485 mark_dies (c);
6488 /* Clear the marks for a die and its children. */
6490 static void
6491 unmark_dies (dw_die_ref die)
6493 dw_die_ref c;
6495 gcc_assert (die->die_mark);
6497 die->die_mark = 0;
6498 for (c = die->die_child; c; c = c->die_sib)
6499 unmark_dies (c);
6502 /* Clear the marks for a die, its children and referred dies. */
6504 static void
6505 unmark_all_dies (dw_die_ref die)
6507 dw_die_ref c;
6508 dw_attr_ref a;
6510 if (!die->die_mark)
6511 return;
6512 die->die_mark = 0;
6514 for (c = die->die_child; c; c = c->die_sib)
6515 unmark_all_dies (c);
6517 for (a = die->die_attr; a; a = a->dw_attr_next)
6518 if (AT_class (a) == dw_val_class_die_ref)
6519 unmark_all_dies (AT_ref (a));
6522 /* Return the size of the .debug_pubnames table generated for the
6523 compilation unit. */
6525 static unsigned long
6526 size_of_pubnames (void)
6528 unsigned long size;
6529 unsigned i;
6531 size = DWARF_PUBNAMES_HEADER_SIZE;
6532 for (i = 0; i < pubname_table_in_use; i++)
6534 pubname_ref p = &pubname_table[i];
6535 size += DWARF_OFFSET_SIZE + strlen (p->name) + 1;
6538 size += DWARF_OFFSET_SIZE;
6539 return size;
6542 /* Return the size of the information in the .debug_aranges section. */
6544 static unsigned long
6545 size_of_aranges (void)
6547 unsigned long size;
6549 size = DWARF_ARANGES_HEADER_SIZE;
6551 /* Count the address/length pair for this compilation unit. */
6552 size += 2 * DWARF2_ADDR_SIZE;
6553 size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
6555 /* Count the two zero words used to terminated the address range table. */
6556 size += 2 * DWARF2_ADDR_SIZE;
6557 return size;
6560 /* Select the encoding of an attribute value. */
6562 static enum dwarf_form
6563 value_format (dw_attr_ref a)
6565 switch (a->dw_attr_val.val_class)
6567 case dw_val_class_addr:
6568 return DW_FORM_addr;
6569 case dw_val_class_range_list:
6570 case dw_val_class_offset:
6571 switch (DWARF_OFFSET_SIZE)
6573 case 4:
6574 return DW_FORM_data4;
6575 case 8:
6576 return DW_FORM_data8;
6577 default:
6578 gcc_unreachable ();
6580 case dw_val_class_loc_list:
6581 /* FIXME: Could be DW_FORM_data8, with a > 32 bit size
6582 .debug_loc section */
6583 return DW_FORM_data4;
6584 case dw_val_class_loc:
6585 switch (constant_size (size_of_locs (AT_loc (a))))
6587 case 1:
6588 return DW_FORM_block1;
6589 case 2:
6590 return DW_FORM_block2;
6591 default:
6592 gcc_unreachable ();
6594 case dw_val_class_const:
6595 return DW_FORM_sdata;
6596 case dw_val_class_unsigned_const:
6597 switch (constant_size (AT_unsigned (a)))
6599 case 1:
6600 return DW_FORM_data1;
6601 case 2:
6602 return DW_FORM_data2;
6603 case 4:
6604 return DW_FORM_data4;
6605 case 8:
6606 return DW_FORM_data8;
6607 default:
6608 gcc_unreachable ();
6610 case dw_val_class_long_long:
6611 return DW_FORM_block1;
6612 case dw_val_class_vec:
6613 return DW_FORM_block1;
6614 case dw_val_class_flag:
6615 return DW_FORM_flag;
6616 case dw_val_class_die_ref:
6617 if (AT_ref_external (a))
6618 return DW_FORM_ref_addr;
6619 else
6620 return DW_FORM_ref;
6621 case dw_val_class_fde_ref:
6622 return DW_FORM_data;
6623 case dw_val_class_lbl_id:
6624 return DW_FORM_addr;
6625 case dw_val_class_lbl_offset:
6626 return DW_FORM_data;
6627 case dw_val_class_str:
6628 return AT_string_form (a);
6630 default:
6631 gcc_unreachable ();
6635 /* Output the encoding of an attribute value. */
6637 static void
6638 output_value_format (dw_attr_ref a)
6640 enum dwarf_form form = value_format (a);
6642 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
6645 /* Output the .debug_abbrev section which defines the DIE abbreviation
6646 table. */
6648 static void
6649 output_abbrev_section (void)
6651 unsigned long abbrev_id;
6653 dw_attr_ref a_attr;
6655 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6657 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6659 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
6660 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
6661 dwarf_tag_name (abbrev->die_tag));
6663 if (abbrev->die_child != NULL)
6664 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
6665 else
6666 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
6668 for (a_attr = abbrev->die_attr; a_attr != NULL;
6669 a_attr = a_attr->dw_attr_next)
6671 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
6672 dwarf_attr_name (a_attr->dw_attr));
6673 output_value_format (a_attr);
6676 dw2_asm_output_data (1, 0, NULL);
6677 dw2_asm_output_data (1, 0, NULL);
6680 /* Terminate the table. */
6681 dw2_asm_output_data (1, 0, NULL);
6684 /* Output a symbol we can use to refer to this DIE from another CU. */
6686 static inline void
6687 output_die_symbol (dw_die_ref die)
6689 char *sym = die->die_symbol;
6691 if (sym == 0)
6692 return;
6694 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
6695 /* We make these global, not weak; if the target doesn't support
6696 .linkonce, it doesn't support combining the sections, so debugging
6697 will break. */
6698 targetm.asm_out.globalize_label (asm_out_file, sym);
6700 ASM_OUTPUT_LABEL (asm_out_file, sym);
6703 /* Return a new location list, given the begin and end range, and the
6704 expression. gensym tells us whether to generate a new internal symbol for
6705 this location list node, which is done for the head of the list only. */
6707 static inline dw_loc_list_ref
6708 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
6709 const char *section, unsigned int gensym)
6711 dw_loc_list_ref retlist = ggc_alloc_cleared (sizeof (dw_loc_list_node));
6713 retlist->begin = begin;
6714 retlist->end = end;
6715 retlist->expr = expr;
6716 retlist->section = section;
6717 if (gensym)
6718 retlist->ll_symbol = gen_internal_sym ("LLST");
6720 return retlist;
6723 /* Add a location description expression to a location list. */
6725 static inline void
6726 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
6727 const char *begin, const char *end,
6728 const char *section)
6730 dw_loc_list_ref *d;
6732 /* Find the end of the chain. */
6733 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
6736 /* Add a new location list node to the list. */
6737 *d = new_loc_list (descr, begin, end, section, 0);
6740 /* Output the location list given to us. */
6742 static void
6743 output_loc_list (dw_loc_list_ref list_head)
6745 dw_loc_list_ref curr = list_head;
6747 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
6749 /* Walk the location list, and output each range + expression. */
6750 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
6752 unsigned long size;
6753 if (separate_line_info_table_in_use == 0)
6755 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
6756 "Location list begin address (%s)",
6757 list_head->ll_symbol);
6758 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
6759 "Location list end address (%s)",
6760 list_head->ll_symbol);
6762 else
6764 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
6765 "Location list begin address (%s)",
6766 list_head->ll_symbol);
6767 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
6768 "Location list end address (%s)",
6769 list_head->ll_symbol);
6771 size = size_of_locs (curr->expr);
6773 /* Output the block length for this list of location operations. */
6774 gcc_assert (size <= 0xffff);
6775 dw2_asm_output_data (2, size, "%s", "Location expression size");
6777 output_loc_sequence (curr->expr);
6780 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
6781 "Location list terminator begin (%s)",
6782 list_head->ll_symbol);
6783 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
6784 "Location list terminator end (%s)",
6785 list_head->ll_symbol);
6788 /* Output the DIE and its attributes. Called recursively to generate
6789 the definitions of each child DIE. */
6791 static void
6792 output_die (dw_die_ref die)
6794 dw_attr_ref a;
6795 dw_die_ref c;
6796 unsigned long size;
6798 /* If someone in another CU might refer to us, set up a symbol for
6799 them to point to. */
6800 if (die->die_symbol)
6801 output_die_symbol (die);
6803 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
6804 die->die_offset, dwarf_tag_name (die->die_tag));
6806 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
6808 const char *name = dwarf_attr_name (a->dw_attr);
6810 switch (AT_class (a))
6812 case dw_val_class_addr:
6813 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
6814 break;
6816 case dw_val_class_offset:
6817 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
6818 "%s", name);
6819 break;
6821 case dw_val_class_range_list:
6823 char *p = strchr (ranges_section_label, '\0');
6825 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
6826 a->dw_attr_val.v.val_offset);
6827 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
6828 "%s", name);
6829 *p = '\0';
6831 break;
6833 case dw_val_class_loc:
6834 size = size_of_locs (AT_loc (a));
6836 /* Output the block length for this list of location operations. */
6837 dw2_asm_output_data (constant_size (size), size, "%s", name);
6839 output_loc_sequence (AT_loc (a));
6840 break;
6842 case dw_val_class_const:
6843 /* ??? It would be slightly more efficient to use a scheme like is
6844 used for unsigned constants below, but gdb 4.x does not sign
6845 extend. Gdb 5.x does sign extend. */
6846 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
6847 break;
6849 case dw_val_class_unsigned_const:
6850 dw2_asm_output_data (constant_size (AT_unsigned (a)),
6851 AT_unsigned (a), "%s", name);
6852 break;
6854 case dw_val_class_long_long:
6856 unsigned HOST_WIDE_INT first, second;
6858 dw2_asm_output_data (1,
6859 2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
6860 "%s", name);
6862 if (WORDS_BIG_ENDIAN)
6864 first = a->dw_attr_val.v.val_long_long.hi;
6865 second = a->dw_attr_val.v.val_long_long.low;
6867 else
6869 first = a->dw_attr_val.v.val_long_long.low;
6870 second = a->dw_attr_val.v.val_long_long.hi;
6873 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
6874 first, "long long constant");
6875 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
6876 second, NULL);
6878 break;
6880 case dw_val_class_vec:
6882 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
6883 unsigned int len = a->dw_attr_val.v.val_vec.length;
6884 unsigned int i;
6885 unsigned char *p;
6887 dw2_asm_output_data (1, len * elt_size, "%s", name);
6888 if (elt_size > sizeof (HOST_WIDE_INT))
6890 elt_size /= 2;
6891 len *= 2;
6893 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
6894 i < len;
6895 i++, p += elt_size)
6896 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
6897 "fp or vector constant word %u", i);
6898 break;
6901 case dw_val_class_flag:
6902 dw2_asm_output_data (1, AT_flag (a), "%s", name);
6903 break;
6905 case dw_val_class_loc_list:
6907 char *sym = AT_loc_list (a)->ll_symbol;
6909 gcc_assert (sym);
6910 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, "%s", name);
6912 break;
6914 case dw_val_class_die_ref:
6915 if (AT_ref_external (a))
6917 char *sym = AT_ref (a)->die_symbol;
6919 gcc_assert (sym);
6920 dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, "%s", name);
6922 else
6924 gcc_assert (AT_ref (a)->die_offset);
6925 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
6926 "%s", name);
6928 break;
6930 case dw_val_class_fde_ref:
6932 char l1[20];
6934 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
6935 a->dw_attr_val.v.val_fde_index * 2);
6936 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, "%s", name);
6938 break;
6940 case dw_val_class_lbl_id:
6941 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
6942 break;
6944 case dw_val_class_lbl_offset:
6945 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a), "%s", name);
6946 break;
6948 case dw_val_class_str:
6949 if (AT_string_form (a) == DW_FORM_strp)
6950 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
6951 a->dw_attr_val.v.val_str->label,
6952 "%s: \"%s\"", name, AT_string (a));
6953 else
6954 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
6955 break;
6957 default:
6958 gcc_unreachable ();
6962 for (c = die->die_child; c != NULL; c = c->die_sib)
6963 output_die (c);
6965 /* Add null byte to terminate sibling list. */
6966 if (die->die_child != NULL)
6967 dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
6968 die->die_offset);
6971 /* Output the compilation unit that appears at the beginning of the
6972 .debug_info section, and precedes the DIE descriptions. */
6974 static void
6975 output_compilation_unit_header (void)
6977 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
6978 dw2_asm_output_data (4, 0xffffffff,
6979 "Initial length escape value indicating 64-bit DWARF extension");
6980 dw2_asm_output_data (DWARF_OFFSET_SIZE,
6981 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
6982 "Length of Compilation Unit Info");
6983 dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
6984 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
6985 "Offset Into Abbrev. Section");
6986 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
6989 /* Output the compilation unit DIE and its children. */
6991 static void
6992 output_comp_unit (dw_die_ref die, int output_if_empty)
6994 const char *secname;
6995 char *oldsym, *tmp;
6997 /* Unless we are outputting main CU, we may throw away empty ones. */
6998 if (!output_if_empty && die->die_child == NULL)
6999 return;
7001 /* Even if there are no children of this DIE, we must output the information
7002 about the compilation unit. Otherwise, on an empty translation unit, we
7003 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
7004 will then complain when examining the file. First mark all the DIEs in
7005 this CU so we know which get local refs. */
7006 mark_dies (die);
7008 build_abbrev_table (die);
7010 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
7011 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7012 calc_die_sizes (die);
7014 oldsym = die->die_symbol;
7015 if (oldsym)
7017 tmp = alloca (strlen (oldsym) + 24);
7019 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
7020 secname = tmp;
7021 die->die_symbol = NULL;
7023 else
7024 secname = (const char *) DEBUG_INFO_SECTION;
7026 /* Output debugging information. */
7027 named_section_flags (secname, SECTION_DEBUG);
7028 output_compilation_unit_header ();
7029 output_die (die);
7031 /* Leave the marks on the main CU, so we can check them in
7032 output_pubnames. */
7033 if (oldsym)
7035 unmark_dies (die);
7036 die->die_symbol = oldsym;
7040 /* The DWARF2 pubname for a nested thingy looks like "A::f". The
7041 output of lang_hooks.decl_printable_name for C++ looks like
7042 "A::f(int)". Let's drop the argument list, and maybe the scope. */
7044 static const char *
7045 dwarf2_name (tree decl, int scope)
7047 return lang_hooks.decl_printable_name (decl, scope ? 1 : 0);
7050 /* Add a new entry to .debug_pubnames if appropriate. */
7052 static void
7053 add_pubname (tree decl, dw_die_ref die)
7055 pubname_ref p;
7057 if (! TREE_PUBLIC (decl))
7058 return;
7060 if (pubname_table_in_use == pubname_table_allocated)
7062 pubname_table_allocated += PUBNAME_TABLE_INCREMENT;
7063 pubname_table
7064 = ggc_realloc (pubname_table,
7065 (pubname_table_allocated * sizeof (pubname_entry)));
7066 memset (pubname_table + pubname_table_in_use, 0,
7067 PUBNAME_TABLE_INCREMENT * sizeof (pubname_entry));
7070 p = &pubname_table[pubname_table_in_use++];
7071 p->die = die;
7072 p->name = xstrdup (dwarf2_name (decl, 1));
7075 /* Output the public names table used to speed up access to externally
7076 visible names. For now, only generate entries for externally
7077 visible procedures. */
7079 static void
7080 output_pubnames (void)
7082 unsigned i;
7083 unsigned long pubnames_length = size_of_pubnames ();
7085 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7086 dw2_asm_output_data (4, 0xffffffff,
7087 "Initial length escape value indicating 64-bit DWARF extension");
7088 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7089 "Length of Public Names Info");
7090 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7091 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7092 "Offset of Compilation Unit Info");
7093 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
7094 "Compilation Unit Length");
7096 for (i = 0; i < pubname_table_in_use; i++)
7098 pubname_ref pub = &pubname_table[i];
7100 /* We shouldn't see pubnames for DIEs outside of the main CU. */
7101 gcc_assert (pub->die->die_mark);
7103 dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
7104 "DIE offset");
7106 dw2_asm_output_nstring (pub->name, -1, "external name");
7109 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
7112 /* Add a new entry to .debug_aranges if appropriate. */
7114 static void
7115 add_arange (tree decl, dw_die_ref die)
7117 if (! DECL_SECTION_NAME (decl))
7118 return;
7120 if (arange_table_in_use == arange_table_allocated)
7122 arange_table_allocated += ARANGE_TABLE_INCREMENT;
7123 arange_table = ggc_realloc (arange_table,
7124 (arange_table_allocated
7125 * sizeof (dw_die_ref)));
7126 memset (arange_table + arange_table_in_use, 0,
7127 ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
7130 arange_table[arange_table_in_use++] = die;
7133 /* Output the information that goes into the .debug_aranges table.
7134 Namely, define the beginning and ending address range of the
7135 text section generated for this compilation unit. */
7137 static void
7138 output_aranges (void)
7140 unsigned i;
7141 unsigned long aranges_length = size_of_aranges ();
7143 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7144 dw2_asm_output_data (4, 0xffffffff,
7145 "Initial length escape value indicating 64-bit DWARF extension");
7146 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
7147 "Length of Address Ranges Info");
7148 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7149 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7150 "Offset of Compilation Unit Info");
7151 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
7152 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
7154 /* We need to align to twice the pointer size here. */
7155 if (DWARF_ARANGES_PAD_SIZE)
7157 /* Pad using a 2 byte words so that padding is correct for any
7158 pointer size. */
7159 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
7160 2 * DWARF2_ADDR_SIZE);
7161 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
7162 dw2_asm_output_data (2, 0, NULL);
7165 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
7166 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
7167 text_section_label, "Length");
7169 for (i = 0; i < arange_table_in_use; i++)
7171 dw_die_ref die = arange_table[i];
7173 /* We shouldn't see aranges for DIEs outside of the main CU. */
7174 gcc_assert (die->die_mark);
7176 if (die->die_tag == DW_TAG_subprogram)
7178 dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
7179 "Address");
7180 dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
7181 get_AT_low_pc (die), "Length");
7183 else
7185 /* A static variable; extract the symbol from DW_AT_location.
7186 Note that this code isn't currently hit, as we only emit
7187 aranges for functions (jason 9/23/99). */
7188 dw_attr_ref a = get_AT (die, DW_AT_location);
7189 dw_loc_descr_ref loc;
7191 gcc_assert (a && AT_class (a) == dw_val_class_loc);
7193 loc = AT_loc (a);
7194 gcc_assert (loc->dw_loc_opc == DW_OP_addr);
7196 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
7197 loc->dw_loc_oprnd1.v.val_addr, "Address");
7198 dw2_asm_output_data (DWARF2_ADDR_SIZE,
7199 get_AT_unsigned (die, DW_AT_byte_size),
7200 "Length");
7204 /* Output the terminator words. */
7205 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7206 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7209 /* Add a new entry to .debug_ranges. Return the offset at which it
7210 was placed. */
7212 static unsigned int
7213 add_ranges (tree block)
7215 unsigned int in_use = ranges_table_in_use;
7217 if (in_use == ranges_table_allocated)
7219 ranges_table_allocated += RANGES_TABLE_INCREMENT;
7220 ranges_table
7221 = ggc_realloc (ranges_table, (ranges_table_allocated
7222 * sizeof (struct dw_ranges_struct)));
7223 memset (ranges_table + ranges_table_in_use, 0,
7224 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
7227 ranges_table[in_use].block_num = (block ? BLOCK_NUMBER (block) : 0);
7228 ranges_table_in_use = in_use + 1;
7230 return in_use * 2 * DWARF2_ADDR_SIZE;
7233 static void
7234 output_ranges (void)
7236 unsigned i;
7237 static const char *const start_fmt = "Offset 0x%x";
7238 const char *fmt = start_fmt;
7240 for (i = 0; i < ranges_table_in_use; i++)
7242 int block_num = ranges_table[i].block_num;
7244 if (block_num)
7246 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
7247 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
7249 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
7250 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
7252 /* If all code is in the text section, then the compilation
7253 unit base address defaults to DW_AT_low_pc, which is the
7254 base of the text section. */
7255 if (separate_line_info_table_in_use == 0)
7257 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
7258 text_section_label,
7259 fmt, i * 2 * DWARF2_ADDR_SIZE);
7260 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
7261 text_section_label, NULL);
7264 /* Otherwise, we add a DW_AT_entry_pc attribute to force the
7265 compilation unit base address to zero, which allows us to
7266 use absolute addresses, and not worry about whether the
7267 target supports cross-section arithmetic. */
7268 else
7270 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
7271 fmt, i * 2 * DWARF2_ADDR_SIZE);
7272 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
7275 fmt = NULL;
7277 else
7279 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7280 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7281 fmt = start_fmt;
7286 /* Data structure containing information about input files. */
7287 struct file_info
7289 char *path; /* Complete file name. */
7290 char *fname; /* File name part. */
7291 int length; /* Length of entire string. */
7292 int file_idx; /* Index in input file table. */
7293 int dir_idx; /* Index in directory table. */
7296 /* Data structure containing information about directories with source
7297 files. */
7298 struct dir_info
7300 char *path; /* Path including directory name. */
7301 int length; /* Path length. */
7302 int prefix; /* Index of directory entry which is a prefix. */
7303 int count; /* Number of files in this directory. */
7304 int dir_idx; /* Index of directory used as base. */
7305 int used; /* Used in the end? */
7308 /* Callback function for file_info comparison. We sort by looking at
7309 the directories in the path. */
7311 static int
7312 file_info_cmp (const void *p1, const void *p2)
7314 const struct file_info *s1 = p1;
7315 const struct file_info *s2 = p2;
7316 unsigned char *cp1;
7317 unsigned char *cp2;
7319 /* Take care of file names without directories. We need to make sure that
7320 we return consistent values to qsort since some will get confused if
7321 we return the same value when identical operands are passed in opposite
7322 orders. So if neither has a directory, return 0 and otherwise return
7323 1 or -1 depending on which one has the directory. */
7324 if ((s1->path == s1->fname || s2->path == s2->fname))
7325 return (s2->path == s2->fname) - (s1->path == s1->fname);
7327 cp1 = (unsigned char *) s1->path;
7328 cp2 = (unsigned char *) s2->path;
7330 while (1)
7332 ++cp1;
7333 ++cp2;
7334 /* Reached the end of the first path? If so, handle like above. */
7335 if ((cp1 == (unsigned char *) s1->fname)
7336 || (cp2 == (unsigned char *) s2->fname))
7337 return ((cp2 == (unsigned char *) s2->fname)
7338 - (cp1 == (unsigned char *) s1->fname));
7340 /* Character of current path component the same? */
7341 else if (*cp1 != *cp2)
7342 return *cp1 - *cp2;
7346 /* Output the directory table and the file name table. We try to minimize
7347 the total amount of memory needed. A heuristic is used to avoid large
7348 slowdowns with many input files. */
7350 static void
7351 output_file_names (void)
7353 struct file_info *files;
7354 struct dir_info *dirs;
7355 int *saved;
7356 int *savehere;
7357 int *backmap;
7358 size_t ndirs;
7359 int idx_offset;
7360 size_t i;
7361 int idx;
7363 /* Handle the case where file_table is empty. */
7364 if (VARRAY_ACTIVE_SIZE (file_table) <= 1)
7366 dw2_asm_output_data (1, 0, "End directory table");
7367 dw2_asm_output_data (1, 0, "End file name table");
7368 return;
7371 /* Allocate the various arrays we need. */
7372 files = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (struct file_info));
7373 dirs = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (struct dir_info));
7375 /* Sort the file names. */
7376 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7378 char *f;
7380 /* Skip all leading "./". */
7381 f = VARRAY_CHAR_PTR (file_table, i);
7382 while (f[0] == '.' && f[1] == '/')
7383 f += 2;
7385 /* Create a new array entry. */
7386 files[i].path = f;
7387 files[i].length = strlen (f);
7388 files[i].file_idx = i;
7390 /* Search for the file name part. */
7391 f = strrchr (f, '/');
7392 files[i].fname = f == NULL ? files[i].path : f + 1;
7395 qsort (files + 1, VARRAY_ACTIVE_SIZE (file_table) - 1,
7396 sizeof (files[0]), file_info_cmp);
7398 /* Find all the different directories used. */
7399 dirs[0].path = files[1].path;
7400 dirs[0].length = files[1].fname - files[1].path;
7401 dirs[0].prefix = -1;
7402 dirs[0].count = 1;
7403 dirs[0].dir_idx = 0;
7404 dirs[0].used = 0;
7405 files[1].dir_idx = 0;
7406 ndirs = 1;
7408 for (i = 2; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7409 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
7410 && memcmp (dirs[ndirs - 1].path, files[i].path,
7411 dirs[ndirs - 1].length) == 0)
7413 /* Same directory as last entry. */
7414 files[i].dir_idx = ndirs - 1;
7415 ++dirs[ndirs - 1].count;
7417 else
7419 size_t j;
7421 /* This is a new directory. */
7422 dirs[ndirs].path = files[i].path;
7423 dirs[ndirs].length = files[i].fname - files[i].path;
7424 dirs[ndirs].count = 1;
7425 dirs[ndirs].dir_idx = ndirs;
7426 dirs[ndirs].used = 0;
7427 files[i].dir_idx = ndirs;
7429 /* Search for a prefix. */
7430 dirs[ndirs].prefix = -1;
7431 for (j = 0; j < ndirs; j++)
7432 if (dirs[j].length < dirs[ndirs].length
7433 && dirs[j].length > 1
7434 && (dirs[ndirs].prefix == -1
7435 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
7436 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
7437 dirs[ndirs].prefix = j;
7439 ++ndirs;
7442 /* Now to the actual work. We have to find a subset of the directories which
7443 allow expressing the file name using references to the directory table
7444 with the least amount of characters. We do not do an exhaustive search
7445 where we would have to check out every combination of every single
7446 possible prefix. Instead we use a heuristic which provides nearly optimal
7447 results in most cases and never is much off. */
7448 saved = alloca (ndirs * sizeof (int));
7449 savehere = alloca (ndirs * sizeof (int));
7451 memset (saved, '\0', ndirs * sizeof (saved[0]));
7452 for (i = 0; i < ndirs; i++)
7454 size_t j;
7455 int total;
7457 /* We can always save some space for the current directory. But this
7458 does not mean it will be enough to justify adding the directory. */
7459 savehere[i] = dirs[i].length;
7460 total = (savehere[i] - saved[i]) * dirs[i].count;
7462 for (j = i + 1; j < ndirs; j++)
7464 savehere[j] = 0;
7465 if (saved[j] < dirs[i].length)
7467 /* Determine whether the dirs[i] path is a prefix of the
7468 dirs[j] path. */
7469 int k;
7471 k = dirs[j].prefix;
7472 while (k != -1 && k != (int) i)
7473 k = dirs[k].prefix;
7475 if (k == (int) i)
7477 /* Yes it is. We can possibly safe some memory but
7478 writing the filenames in dirs[j] relative to
7479 dirs[i]. */
7480 savehere[j] = dirs[i].length;
7481 total += (savehere[j] - saved[j]) * dirs[j].count;
7486 /* Check whether we can safe enough to justify adding the dirs[i]
7487 directory. */
7488 if (total > dirs[i].length + 1)
7490 /* It's worthwhile adding. */
7491 for (j = i; j < ndirs; j++)
7492 if (savehere[j] > 0)
7494 /* Remember how much we saved for this directory so far. */
7495 saved[j] = savehere[j];
7497 /* Remember the prefix directory. */
7498 dirs[j].dir_idx = i;
7503 /* We have to emit them in the order they appear in the file_table array
7504 since the index is used in the debug info generation. To do this
7505 efficiently we generate a back-mapping of the indices first. */
7506 backmap = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (int));
7507 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7509 backmap[files[i].file_idx] = i;
7511 /* Mark this directory as used. */
7512 dirs[dirs[files[i].dir_idx].dir_idx].used = 1;
7515 /* That was it. We are ready to emit the information. First emit the
7516 directory name table. We have to make sure the first actually emitted
7517 directory name has index one; zero is reserved for the current working
7518 directory. Make sure we do not confuse these indices with the one for the
7519 constructed table (even though most of the time they are identical). */
7520 idx = 1;
7521 idx_offset = dirs[0].length > 0 ? 1 : 0;
7522 for (i = 1 - idx_offset; i < ndirs; i++)
7523 if (dirs[i].used != 0)
7525 dirs[i].used = idx++;
7526 dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
7527 "Directory Entry: 0x%x", dirs[i].used);
7530 dw2_asm_output_data (1, 0, "End directory table");
7532 /* Correct the index for the current working directory entry if it
7533 exists. */
7534 if (idx_offset == 0)
7535 dirs[0].used = 0;
7537 /* Now write all the file names. */
7538 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7540 int file_idx = backmap[i];
7541 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
7543 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
7544 "File Entry: 0x%lx", (unsigned long) i);
7546 /* Include directory index. */
7547 dw2_asm_output_data_uleb128 (dirs[dir_idx].used, NULL);
7549 /* Modification time. */
7550 dw2_asm_output_data_uleb128 (0, NULL);
7552 /* File length in bytes. */
7553 dw2_asm_output_data_uleb128 (0, NULL);
7556 dw2_asm_output_data (1, 0, "End file name table");
7560 /* Output the source line number correspondence information. This
7561 information goes into the .debug_line section. */
7563 static void
7564 output_line_info (void)
7566 char l1[20], l2[20], p1[20], p2[20];
7567 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
7568 char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
7569 unsigned opc;
7570 unsigned n_op_args;
7571 unsigned long lt_index;
7572 unsigned long current_line;
7573 long line_offset;
7574 long line_delta;
7575 unsigned long current_file;
7576 unsigned long function;
7578 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
7579 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
7580 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
7581 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
7583 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7584 dw2_asm_output_data (4, 0xffffffff,
7585 "Initial length escape value indicating 64-bit DWARF extension");
7586 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
7587 "Length of Source Line Info");
7588 ASM_OUTPUT_LABEL (asm_out_file, l1);
7590 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7591 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
7592 ASM_OUTPUT_LABEL (asm_out_file, p1);
7594 /* Define the architecture-dependent minimum instruction length (in
7595 bytes). In this implementation of DWARF, this field is used for
7596 information purposes only. Since GCC generates assembly language,
7597 we have no a priori knowledge of how many instruction bytes are
7598 generated for each source line, and therefore can use only the
7599 DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
7600 commands. Accordingly, we fix this as `1', which is "correct
7601 enough" for all architectures, and don't let the target override. */
7602 dw2_asm_output_data (1, 1,
7603 "Minimum Instruction Length");
7605 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
7606 "Default is_stmt_start flag");
7607 dw2_asm_output_data (1, DWARF_LINE_BASE,
7608 "Line Base Value (Special Opcodes)");
7609 dw2_asm_output_data (1, DWARF_LINE_RANGE,
7610 "Line Range Value (Special Opcodes)");
7611 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
7612 "Special Opcode Base");
7614 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
7616 switch (opc)
7618 case DW_LNS_advance_pc:
7619 case DW_LNS_advance_line:
7620 case DW_LNS_set_file:
7621 case DW_LNS_set_column:
7622 case DW_LNS_fixed_advance_pc:
7623 n_op_args = 1;
7624 break;
7625 default:
7626 n_op_args = 0;
7627 break;
7630 dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
7631 opc, n_op_args);
7634 /* Write out the information about the files we use. */
7635 output_file_names ();
7636 ASM_OUTPUT_LABEL (asm_out_file, p2);
7638 /* We used to set the address register to the first location in the text
7639 section here, but that didn't accomplish anything since we already
7640 have a line note for the opening brace of the first function. */
7642 /* Generate the line number to PC correspondence table, encoded as
7643 a series of state machine operations. */
7644 current_file = 1;
7645 current_line = 1;
7646 strcpy (prev_line_label, text_section_label);
7647 for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
7649 dw_line_info_ref line_info = &line_info_table[lt_index];
7651 #if 0
7652 /* Disable this optimization for now; GDB wants to see two line notes
7653 at the beginning of a function so it can find the end of the
7654 prologue. */
7656 /* Don't emit anything for redundant notes. Just updating the
7657 address doesn't accomplish anything, because we already assume
7658 that anything after the last address is this line. */
7659 if (line_info->dw_line_num == current_line
7660 && line_info->dw_file_num == current_file)
7661 continue;
7662 #endif
7664 /* Emit debug info for the address of the current line.
7666 Unfortunately, we have little choice here currently, and must always
7667 use the most general form. GCC does not know the address delta
7668 itself, so we can't use DW_LNS_advance_pc. Many ports do have length
7669 attributes which will give an upper bound on the address range. We
7670 could perhaps use length attributes to determine when it is safe to
7671 use DW_LNS_fixed_advance_pc. */
7673 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
7674 if (0)
7676 /* This can handle deltas up to 0xffff. This takes 3 bytes. */
7677 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7678 "DW_LNS_fixed_advance_pc");
7679 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
7681 else
7683 /* This can handle any delta. This takes
7684 4+DWARF2_ADDR_SIZE bytes. */
7685 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7686 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7687 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7688 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7691 strcpy (prev_line_label, line_label);
7693 /* Emit debug info for the source file of the current line, if
7694 different from the previous line. */
7695 if (line_info->dw_file_num != current_file)
7697 current_file = line_info->dw_file_num;
7698 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
7699 dw2_asm_output_data_uleb128 (current_file, "(\"%s\")",
7700 VARRAY_CHAR_PTR (file_table,
7701 current_file));
7704 /* Emit debug info for the current line number, choosing the encoding
7705 that uses the least amount of space. */
7706 if (line_info->dw_line_num != current_line)
7708 line_offset = line_info->dw_line_num - current_line;
7709 line_delta = line_offset - DWARF_LINE_BASE;
7710 current_line = line_info->dw_line_num;
7711 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
7712 /* This can handle deltas from -10 to 234, using the current
7713 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE. This
7714 takes 1 byte. */
7715 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
7716 "line %lu", current_line);
7717 else
7719 /* This can handle any delta. This takes at least 4 bytes,
7720 depending on the value being encoded. */
7721 dw2_asm_output_data (1, DW_LNS_advance_line,
7722 "advance to line %lu", current_line);
7723 dw2_asm_output_data_sleb128 (line_offset, NULL);
7724 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7727 else
7728 /* We still need to start a new row, so output a copy insn. */
7729 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7732 /* Emit debug info for the address of the end of the function. */
7733 if (0)
7735 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7736 "DW_LNS_fixed_advance_pc");
7737 dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
7739 else
7741 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7742 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7743 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7744 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
7747 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
7748 dw2_asm_output_data_uleb128 (1, NULL);
7749 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
7751 function = 0;
7752 current_file = 1;
7753 current_line = 1;
7754 for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
7756 dw_separate_line_info_ref line_info
7757 = &separate_line_info_table[lt_index];
7759 #if 0
7760 /* Don't emit anything for redundant notes. */
7761 if (line_info->dw_line_num == current_line
7762 && line_info->dw_file_num == current_file
7763 && line_info->function == function)
7764 goto cont;
7765 #endif
7767 /* Emit debug info for the address of the current line. If this is
7768 a new function, or the first line of a function, then we need
7769 to handle it differently. */
7770 ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
7771 lt_index);
7772 if (function != line_info->function)
7774 function = line_info->function;
7776 /* Set the address register to the first line in the function. */
7777 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7778 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7779 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7780 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7782 else
7784 /* ??? See the DW_LNS_advance_pc comment above. */
7785 if (0)
7787 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7788 "DW_LNS_fixed_advance_pc");
7789 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
7791 else
7793 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7794 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7795 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7796 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7800 strcpy (prev_line_label, line_label);
7802 /* Emit debug info for the source file of the current line, if
7803 different from the previous line. */
7804 if (line_info->dw_file_num != current_file)
7806 current_file = line_info->dw_file_num;
7807 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
7808 dw2_asm_output_data_uleb128 (current_file, "(\"%s\")",
7809 VARRAY_CHAR_PTR (file_table,
7810 current_file));
7813 /* Emit debug info for the current line number, choosing the encoding
7814 that uses the least amount of space. */
7815 if (line_info->dw_line_num != current_line)
7817 line_offset = line_info->dw_line_num - current_line;
7818 line_delta = line_offset - DWARF_LINE_BASE;
7819 current_line = line_info->dw_line_num;
7820 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
7821 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
7822 "line %lu", current_line);
7823 else
7825 dw2_asm_output_data (1, DW_LNS_advance_line,
7826 "advance to line %lu", current_line);
7827 dw2_asm_output_data_sleb128 (line_offset, NULL);
7828 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7831 else
7832 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7834 #if 0
7835 cont:
7836 #endif
7838 lt_index++;
7840 /* If we're done with a function, end its sequence. */
7841 if (lt_index == separate_line_info_table_in_use
7842 || separate_line_info_table[lt_index].function != function)
7844 current_file = 1;
7845 current_line = 1;
7847 /* Emit debug info for the address of the end of the function. */
7848 ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
7849 if (0)
7851 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7852 "DW_LNS_fixed_advance_pc");
7853 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
7855 else
7857 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7858 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7859 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7860 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7863 /* Output the marker for the end of this sequence. */
7864 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
7865 dw2_asm_output_data_uleb128 (1, NULL);
7866 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
7870 /* Output the marker for the end of the line number info. */
7871 ASM_OUTPUT_LABEL (asm_out_file, l2);
7874 /* Given a pointer to a tree node for some base type, return a pointer to
7875 a DIE that describes the given type.
7877 This routine must only be called for GCC type nodes that correspond to
7878 Dwarf base (fundamental) types. */
7880 static dw_die_ref
7881 base_type_die (tree type)
7883 dw_die_ref base_type_result;
7884 const char *type_name;
7885 enum dwarf_type encoding;
7886 tree name = TYPE_NAME (type);
7888 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
7889 return 0;
7891 if (name)
7893 if (TREE_CODE (name) == TYPE_DECL)
7894 name = DECL_NAME (name);
7896 type_name = IDENTIFIER_POINTER (name);
7898 else
7899 type_name = "__unknown__";
7901 switch (TREE_CODE (type))
7903 case INTEGER_TYPE:
7904 /* Carefully distinguish the C character types, without messing
7905 up if the language is not C. Note that we check only for the names
7906 that contain spaces; other names might occur by coincidence in other
7907 languages. */
7908 if (! (TYPE_PRECISION (type) == CHAR_TYPE_SIZE
7909 && (type == char_type_node
7910 || ! strcmp (type_name, "signed char")
7911 || ! strcmp (type_name, "unsigned char"))))
7913 if (TYPE_UNSIGNED (type))
7914 encoding = DW_ATE_unsigned;
7915 else
7916 encoding = DW_ATE_signed;
7917 break;
7919 /* else fall through. */
7921 case CHAR_TYPE:
7922 /* GNU Pascal/Ada CHAR type. Not used in C. */
7923 if (TYPE_UNSIGNED (type))
7924 encoding = DW_ATE_unsigned_char;
7925 else
7926 encoding = DW_ATE_signed_char;
7927 break;
7929 case REAL_TYPE:
7930 encoding = DW_ATE_float;
7931 break;
7933 /* Dwarf2 doesn't know anything about complex ints, so use
7934 a user defined type for it. */
7935 case COMPLEX_TYPE:
7936 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
7937 encoding = DW_ATE_complex_float;
7938 else
7939 encoding = DW_ATE_lo_user;
7940 break;
7942 case BOOLEAN_TYPE:
7943 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
7944 encoding = DW_ATE_boolean;
7945 break;
7947 default:
7948 /* No other TREE_CODEs are Dwarf fundamental types. */
7949 gcc_unreachable ();
7952 base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
7953 if (demangle_name_func)
7954 type_name = (*demangle_name_func) (type_name);
7956 add_AT_string (base_type_result, DW_AT_name, type_name);
7957 add_AT_unsigned (base_type_result, DW_AT_byte_size,
7958 int_size_in_bytes (type));
7959 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
7961 return base_type_result;
7964 /* Given a pointer to an arbitrary ..._TYPE tree node, return a pointer to
7965 the Dwarf "root" type for the given input type. The Dwarf "root" type of
7966 a given type is generally the same as the given type, except that if the
7967 given type is a pointer or reference type, then the root type of the given
7968 type is the root type of the "basis" type for the pointer or reference
7969 type. (This definition of the "root" type is recursive.) Also, the root
7970 type of a `const' qualified type or a `volatile' qualified type is the
7971 root type of the given type without the qualifiers. */
7973 static tree
7974 root_type (tree type)
7976 if (TREE_CODE (type) == ERROR_MARK)
7977 return error_mark_node;
7979 switch (TREE_CODE (type))
7981 case ERROR_MARK:
7982 return error_mark_node;
7984 case POINTER_TYPE:
7985 case REFERENCE_TYPE:
7986 return type_main_variant (root_type (TREE_TYPE (type)));
7988 default:
7989 return type_main_variant (type);
7993 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
7994 given input type is a Dwarf "fundamental" type. Otherwise return null. */
7996 static inline int
7997 is_base_type (tree type)
7999 switch (TREE_CODE (type))
8001 case ERROR_MARK:
8002 case VOID_TYPE:
8003 case INTEGER_TYPE:
8004 case REAL_TYPE:
8005 case COMPLEX_TYPE:
8006 case BOOLEAN_TYPE:
8007 case CHAR_TYPE:
8008 return 1;
8010 case ARRAY_TYPE:
8011 case RECORD_TYPE:
8012 case UNION_TYPE:
8013 case QUAL_UNION_TYPE:
8014 case ENUMERAL_TYPE:
8015 case FUNCTION_TYPE:
8016 case METHOD_TYPE:
8017 case POINTER_TYPE:
8018 case REFERENCE_TYPE:
8019 case FILE_TYPE:
8020 case OFFSET_TYPE:
8021 case LANG_TYPE:
8022 case VECTOR_TYPE:
8023 return 0;
8025 default:
8026 gcc_unreachable ();
8029 return 0;
8032 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
8033 node, return the size in bits for the type if it is a constant, or else
8034 return the alignment for the type if the type's size is not constant, or
8035 else return BITS_PER_WORD if the type actually turns out to be an
8036 ERROR_MARK node. */
8038 static inline unsigned HOST_WIDE_INT
8039 simple_type_size_in_bits (tree type)
8041 if (TREE_CODE (type) == ERROR_MARK)
8042 return BITS_PER_WORD;
8043 else if (TYPE_SIZE (type) == NULL_TREE)
8044 return 0;
8045 else if (host_integerp (TYPE_SIZE (type), 1))
8046 return tree_low_cst (TYPE_SIZE (type), 1);
8047 else
8048 return TYPE_ALIGN (type);
8051 /* Return true if the debug information for the given type should be
8052 emitted as a subrange type. */
8054 static inline bool
8055 is_subrange_type (tree type)
8057 tree subtype = TREE_TYPE (type);
8059 /* Subrange types are identified by the fact that they are integer
8060 types, and that they have a subtype which is either an integer type
8061 or an enumeral type. */
8063 if (TREE_CODE (type) != INTEGER_TYPE
8064 || subtype == NULL_TREE)
8065 return false;
8067 if (TREE_CODE (subtype) != INTEGER_TYPE
8068 && TREE_CODE (subtype) != ENUMERAL_TYPE)
8069 return false;
8071 if (TREE_CODE (type) == TREE_CODE (subtype)
8072 && int_size_in_bytes (type) == int_size_in_bytes (subtype)
8073 && TYPE_MIN_VALUE (type) != NULL
8074 && TYPE_MIN_VALUE (subtype) != NULL
8075 && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
8076 && TYPE_MAX_VALUE (type) != NULL
8077 && TYPE_MAX_VALUE (subtype) != NULL
8078 && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
8080 /* The type and its subtype have the same representation. If in
8081 addition the two types also have the same name, then the given
8082 type is not a subrange type, but rather a plain base type. */
8083 /* FIXME: brobecker/2004-03-22:
8084 Sizetype INTEGER_CSTs nodes are canonicalized. It should
8085 therefore be sufficient to check the TYPE_SIZE node pointers
8086 rather than checking the actual size. Unfortunately, we have
8087 found some cases, such as in the Ada "integer" type, where
8088 this is not the case. Until this problem is solved, we need to
8089 keep checking the actual size. */
8090 tree type_name = TYPE_NAME (type);
8091 tree subtype_name = TYPE_NAME (subtype);
8093 if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
8094 type_name = DECL_NAME (type_name);
8096 if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
8097 subtype_name = DECL_NAME (subtype_name);
8099 if (type_name == subtype_name)
8100 return false;
8103 return true;
8106 /* Given a pointer to a tree node for a subrange type, return a pointer
8107 to a DIE that describes the given type. */
8109 static dw_die_ref
8110 subrange_type_die (tree type, dw_die_ref context_die)
8112 dw_die_ref subtype_die;
8113 dw_die_ref subrange_die;
8114 tree name = TYPE_NAME (type);
8115 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
8116 tree subtype = TREE_TYPE (type);
8118 if (context_die == NULL)
8119 context_die = comp_unit_die;
8121 if (TREE_CODE (subtype) == ENUMERAL_TYPE)
8122 subtype_die = gen_enumeration_type_die (subtype, context_die);
8123 else
8124 subtype_die = base_type_die (subtype);
8126 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
8128 if (name != NULL)
8130 if (TREE_CODE (name) == TYPE_DECL)
8131 name = DECL_NAME (name);
8132 add_name_attribute (subrange_die, IDENTIFIER_POINTER (name));
8135 if (int_size_in_bytes (subtype) != size_in_bytes)
8137 /* The size of the subrange type and its base type do not match,
8138 so we need to generate a size attribute for the subrange type. */
8139 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
8142 if (TYPE_MIN_VALUE (type) != NULL)
8143 add_bound_info (subrange_die, DW_AT_lower_bound,
8144 TYPE_MIN_VALUE (type));
8145 if (TYPE_MAX_VALUE (type) != NULL)
8146 add_bound_info (subrange_die, DW_AT_upper_bound,
8147 TYPE_MAX_VALUE (type));
8148 add_AT_die_ref (subrange_die, DW_AT_type, subtype_die);
8150 return subrange_die;
8153 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
8154 entry that chains various modifiers in front of the given type. */
8156 static dw_die_ref
8157 modified_type_die (tree type, int is_const_type, int is_volatile_type,
8158 dw_die_ref context_die)
8160 enum tree_code code = TREE_CODE (type);
8161 dw_die_ref mod_type_die = NULL;
8162 dw_die_ref sub_die = NULL;
8163 tree item_type = NULL;
8165 if (code != ERROR_MARK)
8167 tree qualified_type;
8169 /* See if we already have the appropriately qualified variant of
8170 this type. */
8171 qualified_type
8172 = get_qualified_type (type,
8173 ((is_const_type ? TYPE_QUAL_CONST : 0)
8174 | (is_volatile_type
8175 ? TYPE_QUAL_VOLATILE : 0)));
8177 /* If we do, then we can just use its DIE, if it exists. */
8178 if (qualified_type)
8180 mod_type_die = lookup_type_die (qualified_type);
8181 if (mod_type_die)
8182 return mod_type_die;
8185 /* Handle C typedef types. */
8186 if (qualified_type && TYPE_NAME (qualified_type)
8187 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL
8188 && DECL_ORIGINAL_TYPE (TYPE_NAME (qualified_type)))
8190 tree type_name = TYPE_NAME (qualified_type);
8191 tree dtype = TREE_TYPE (type_name);
8193 if (qualified_type == dtype)
8195 /* For a named type, use the typedef. */
8196 gen_type_die (qualified_type, context_die);
8197 mod_type_die = lookup_type_die (qualified_type);
8199 else if (is_const_type < TYPE_READONLY (dtype)
8200 || is_volatile_type < TYPE_VOLATILE (dtype))
8201 /* cv-unqualified version of named type. Just use the unnamed
8202 type to which it refers. */
8203 mod_type_die
8204 = modified_type_die (DECL_ORIGINAL_TYPE (type_name),
8205 is_const_type, is_volatile_type,
8206 context_die);
8208 /* Else cv-qualified version of named type; fall through. */
8211 if (mod_type_die)
8212 /* OK. */
8214 else if (is_const_type)
8216 mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
8217 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
8219 else if (is_volatile_type)
8221 mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
8222 sub_die = modified_type_die (type, 0, 0, context_die);
8224 else if (code == POINTER_TYPE)
8226 mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
8227 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8228 simple_type_size_in_bits (type) / BITS_PER_UNIT);
8229 #if 0
8230 add_AT_unsigned (mod_type_die, DW_AT_address_class, 0);
8231 #endif
8232 item_type = TREE_TYPE (type);
8234 else if (code == REFERENCE_TYPE)
8236 mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
8237 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8238 simple_type_size_in_bits (type) / BITS_PER_UNIT);
8239 #if 0
8240 add_AT_unsigned (mod_type_die, DW_AT_address_class, 0);
8241 #endif
8242 item_type = TREE_TYPE (type);
8244 else if (is_subrange_type (type))
8245 mod_type_die = subrange_type_die (type, context_die);
8246 else if (is_base_type (type))
8247 mod_type_die = base_type_die (type);
8248 else
8250 gen_type_die (type, context_die);
8252 /* We have to get the type_main_variant here (and pass that to the
8253 `lookup_type_die' routine) because the ..._TYPE node we have
8254 might simply be a *copy* of some original type node (where the
8255 copy was created to help us keep track of typedef names) and
8256 that copy might have a different TYPE_UID from the original
8257 ..._TYPE node. */
8258 if (TREE_CODE (type) != VECTOR_TYPE)
8259 mod_type_die = lookup_type_die (type_main_variant (type));
8260 else
8261 /* Vectors have the debugging information in the type,
8262 not the main variant. */
8263 mod_type_die = lookup_type_die (type);
8264 gcc_assert (mod_type_die);
8267 /* We want to equate the qualified type to the die below. */
8268 type = qualified_type;
8271 if (type)
8272 equate_type_number_to_die (type, mod_type_die);
8273 if (item_type)
8274 /* We must do this after the equate_type_number_to_die call, in case
8275 this is a recursive type. This ensures that the modified_type_die
8276 recursion will terminate even if the type is recursive. Recursive
8277 types are possible in Ada. */
8278 sub_die = modified_type_die (item_type,
8279 TYPE_READONLY (item_type),
8280 TYPE_VOLATILE (item_type),
8281 context_die);
8283 if (sub_die != NULL)
8284 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
8286 return mod_type_die;
8289 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
8290 an enumerated type. */
8292 static inline int
8293 type_is_enum (tree type)
8295 return TREE_CODE (type) == ENUMERAL_TYPE;
8298 /* Return the DBX register number described by a given RTL node. */
8300 static unsigned int
8301 dbx_reg_number (rtx rtl)
8303 unsigned regno = REGNO (rtl);
8305 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
8307 return DBX_REGISTER_NUMBER (regno);
8310 /* Return a location descriptor that designates a machine register or
8311 zero if there is none. */
8313 static dw_loc_descr_ref
8314 reg_loc_descriptor (rtx rtl)
8316 unsigned reg;
8317 rtx regs;
8319 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
8320 return 0;
8322 reg = dbx_reg_number (rtl);
8323 regs = targetm.dwarf_register_span (rtl);
8325 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1
8326 || regs)
8327 return multiple_reg_loc_descriptor (rtl, regs);
8328 else
8329 return one_reg_loc_descriptor (reg);
8332 /* Return a location descriptor that designates a machine register for
8333 a given hard register number. */
8335 static dw_loc_descr_ref
8336 one_reg_loc_descriptor (unsigned int regno)
8338 if (regno <= 31)
8339 return new_loc_descr (DW_OP_reg0 + regno, 0, 0);
8340 else
8341 return new_loc_descr (DW_OP_regx, regno, 0);
8344 /* Given an RTL of a register, return a location descriptor that
8345 designates a value that spans more than one register. */
8347 static dw_loc_descr_ref
8348 multiple_reg_loc_descriptor (rtx rtl, rtx regs)
8350 int nregs, size, i;
8351 unsigned reg;
8352 dw_loc_descr_ref loc_result = NULL;
8354 reg = dbx_reg_number (rtl);
8355 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
8357 /* Simple, contiguous registers. */
8358 if (regs == NULL_RTX)
8360 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
8362 loc_result = NULL;
8363 while (nregs--)
8365 dw_loc_descr_ref t;
8367 t = one_reg_loc_descriptor (reg);
8368 add_loc_descr (&loc_result, t);
8369 add_loc_descr (&loc_result, new_loc_descr (DW_OP_piece, size, 0));
8370 ++reg;
8372 return loc_result;
8375 /* Now onto stupid register sets in non contiguous locations. */
8377 gcc_assert (GET_CODE (regs) == PARALLEL);
8379 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8380 loc_result = NULL;
8382 for (i = 0; i < XVECLEN (regs, 0); ++i)
8384 dw_loc_descr_ref t;
8386 t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)));
8387 add_loc_descr (&loc_result, t);
8388 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8389 add_loc_descr (&loc_result, new_loc_descr (DW_OP_piece, size, 0));
8391 return loc_result;
8394 /* Return a location descriptor that designates a constant. */
8396 static dw_loc_descr_ref
8397 int_loc_descriptor (HOST_WIDE_INT i)
8399 enum dwarf_location_atom op;
8401 /* Pick the smallest representation of a constant, rather than just
8402 defaulting to the LEB encoding. */
8403 if (i >= 0)
8405 if (i <= 31)
8406 op = DW_OP_lit0 + i;
8407 else if (i <= 0xff)
8408 op = DW_OP_const1u;
8409 else if (i <= 0xffff)
8410 op = DW_OP_const2u;
8411 else if (HOST_BITS_PER_WIDE_INT == 32
8412 || i <= 0xffffffff)
8413 op = DW_OP_const4u;
8414 else
8415 op = DW_OP_constu;
8417 else
8419 if (i >= -0x80)
8420 op = DW_OP_const1s;
8421 else if (i >= -0x8000)
8422 op = DW_OP_const2s;
8423 else if (HOST_BITS_PER_WIDE_INT == 32
8424 || i >= -0x80000000)
8425 op = DW_OP_const4s;
8426 else
8427 op = DW_OP_consts;
8430 return new_loc_descr (op, i, 0);
8433 /* Return a location descriptor that designates a base+offset location. */
8435 static dw_loc_descr_ref
8436 based_loc_descr (unsigned int reg, HOST_WIDE_INT offset, bool can_use_fbreg)
8438 dw_loc_descr_ref loc_result;
8439 /* For the "frame base", we use the frame pointer or stack pointer
8440 registers, since the RTL for local variables is relative to one of
8441 them. */
8442 unsigned fp_reg = DBX_REGISTER_NUMBER (frame_pointer_needed
8443 ? HARD_FRAME_POINTER_REGNUM
8444 : STACK_POINTER_REGNUM);
8446 if (reg == fp_reg && can_use_fbreg)
8447 loc_result = new_loc_descr (DW_OP_fbreg, offset, 0);
8448 else if (reg <= 31)
8449 loc_result = new_loc_descr (DW_OP_breg0 + reg, offset, 0);
8450 else
8451 loc_result = new_loc_descr (DW_OP_bregx, reg, offset);
8453 return loc_result;
8456 /* Return true if this RTL expression describes a base+offset calculation. */
8458 static inline int
8459 is_based_loc (rtx rtl)
8461 return (GET_CODE (rtl) == PLUS
8462 && ((REG_P (XEXP (rtl, 0))
8463 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
8464 && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
8467 /* The following routine converts the RTL for a variable or parameter
8468 (resident in memory) into an equivalent Dwarf representation of a
8469 mechanism for getting the address of that same variable onto the top of a
8470 hypothetical "address evaluation" stack.
8472 When creating memory location descriptors, we are effectively transforming
8473 the RTL for a memory-resident object into its Dwarf postfix expression
8474 equivalent. This routine recursively descends an RTL tree, turning
8475 it into Dwarf postfix code as it goes.
8477 MODE is the mode of the memory reference, needed to handle some
8478 autoincrement addressing modes.
8480 CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the location
8481 list for RTL. We can't use it when we are emitting location list for
8482 virtual variable frame_base_decl (i.e. a location list for DW_AT_frame_base)
8483 which describes how frame base changes when !frame_pointer_needed.
8485 Return 0 if we can't represent the location. */
8487 static dw_loc_descr_ref
8488 mem_loc_descriptor (rtx rtl, enum machine_mode mode, bool can_use_fbreg)
8490 dw_loc_descr_ref mem_loc_result = NULL;
8491 enum dwarf_location_atom op;
8493 /* Note that for a dynamically sized array, the location we will generate a
8494 description of here will be the lowest numbered location which is
8495 actually within the array. That's *not* necessarily the same as the
8496 zeroth element of the array. */
8498 rtl = targetm.delegitimize_address (rtl);
8500 switch (GET_CODE (rtl))
8502 case POST_INC:
8503 case POST_DEC:
8504 case POST_MODIFY:
8505 /* POST_INC and POST_DEC can be handled just like a SUBREG. So we
8506 just fall into the SUBREG code. */
8508 /* ... fall through ... */
8510 case SUBREG:
8511 /* The case of a subreg may arise when we have a local (register)
8512 variable or a formal (register) parameter which doesn't quite fill
8513 up an entire register. For now, just assume that it is
8514 legitimate to make the Dwarf info refer to the whole register which
8515 contains the given subreg. */
8516 rtl = SUBREG_REG (rtl);
8518 /* ... fall through ... */
8520 case REG:
8521 /* Whenever a register number forms a part of the description of the
8522 method for calculating the (dynamic) address of a memory resident
8523 object, DWARF rules require the register number be referred to as
8524 a "base register". This distinction is not based in any way upon
8525 what category of register the hardware believes the given register
8526 belongs to. This is strictly DWARF terminology we're dealing with
8527 here. Note that in cases where the location of a memory-resident
8528 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
8529 OP_CONST (0)) the actual DWARF location descriptor that we generate
8530 may just be OP_BASEREG (basereg). This may look deceptively like
8531 the object in question was allocated to a register (rather than in
8532 memory) so DWARF consumers need to be aware of the subtle
8533 distinction between OP_REG and OP_BASEREG. */
8534 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
8535 mem_loc_result = based_loc_descr (dbx_reg_number (rtl), 0,
8536 can_use_fbreg);
8537 break;
8539 case MEM:
8540 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
8541 can_use_fbreg);
8542 if (mem_loc_result != 0)
8543 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
8544 break;
8546 case LO_SUM:
8547 rtl = XEXP (rtl, 1);
8549 /* ... fall through ... */
8551 case LABEL_REF:
8552 /* Some ports can transform a symbol ref into a label ref, because
8553 the symbol ref is too far away and has to be dumped into a constant
8554 pool. */
8555 case CONST:
8556 case SYMBOL_REF:
8557 /* Alternatively, the symbol in the constant pool might be referenced
8558 by a different symbol. */
8559 if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
8561 bool marked;
8562 rtx tmp = get_pool_constant_mark (rtl, &marked);
8564 if (GET_CODE (tmp) == SYMBOL_REF)
8566 rtl = tmp;
8567 if (CONSTANT_POOL_ADDRESS_P (tmp))
8568 get_pool_constant_mark (tmp, &marked);
8569 else
8570 marked = true;
8573 /* If all references to this pool constant were optimized away,
8574 it was not output and thus we can't represent it.
8575 FIXME: might try to use DW_OP_const_value here, though
8576 DW_OP_piece complicates it. */
8577 if (!marked)
8578 return 0;
8581 mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
8582 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
8583 mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
8584 VARRAY_PUSH_RTX (used_rtx_varray, rtl);
8585 break;
8587 case PRE_MODIFY:
8588 /* Extract the PLUS expression nested inside and fall into
8589 PLUS code below. */
8590 rtl = XEXP (rtl, 1);
8591 goto plus;
8593 case PRE_INC:
8594 case PRE_DEC:
8595 /* Turn these into a PLUS expression and fall into the PLUS code
8596 below. */
8597 rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
8598 GEN_INT (GET_CODE (rtl) == PRE_INC
8599 ? GET_MODE_UNIT_SIZE (mode)
8600 : -GET_MODE_UNIT_SIZE (mode)));
8602 /* ... fall through ... */
8604 case PLUS:
8605 plus:
8606 if (is_based_loc (rtl))
8607 mem_loc_result = based_loc_descr (dbx_reg_number (XEXP (rtl, 0)),
8608 INTVAL (XEXP (rtl, 1)),
8609 can_use_fbreg);
8610 else
8612 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
8613 can_use_fbreg);
8614 if (mem_loc_result == 0)
8615 break;
8617 if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
8618 && INTVAL (XEXP (rtl, 1)) >= 0)
8619 add_loc_descr (&mem_loc_result,
8620 new_loc_descr (DW_OP_plus_uconst,
8621 INTVAL (XEXP (rtl, 1)), 0));
8622 else
8624 add_loc_descr (&mem_loc_result,
8625 mem_loc_descriptor (XEXP (rtl, 1), mode,
8626 can_use_fbreg));
8627 add_loc_descr (&mem_loc_result,
8628 new_loc_descr (DW_OP_plus, 0, 0));
8631 break;
8633 /* If a pseudo-reg is optimized away, it is possible for it to
8634 be replaced with a MEM containing a multiply or shift. */
8635 case MULT:
8636 op = DW_OP_mul;
8637 goto do_binop;
8639 case ASHIFT:
8640 op = DW_OP_shl;
8641 goto do_binop;
8643 case ASHIFTRT:
8644 op = DW_OP_shra;
8645 goto do_binop;
8647 case LSHIFTRT:
8648 op = DW_OP_shr;
8649 goto do_binop;
8651 do_binop:
8653 dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
8654 can_use_fbreg);
8655 dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
8656 can_use_fbreg);
8658 if (op0 == 0 || op1 == 0)
8659 break;
8661 mem_loc_result = op0;
8662 add_loc_descr (&mem_loc_result, op1);
8663 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
8664 break;
8667 case CONST_INT:
8668 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
8669 break;
8671 default:
8672 gcc_unreachable ();
8675 return mem_loc_result;
8678 /* Return a descriptor that describes the concatenation of two locations.
8679 This is typically a complex variable. */
8681 static dw_loc_descr_ref
8682 concat_loc_descriptor (rtx x0, rtx x1)
8684 dw_loc_descr_ref cc_loc_result = NULL;
8685 dw_loc_descr_ref x0_ref = loc_descriptor (x0, false);
8686 dw_loc_descr_ref x1_ref = loc_descriptor (x1, false);
8688 if (x0_ref == 0 || x1_ref == 0)
8689 return 0;
8691 cc_loc_result = x0_ref;
8692 add_loc_descr (&cc_loc_result,
8693 new_loc_descr (DW_OP_piece,
8694 GET_MODE_SIZE (GET_MODE (x0)), 0));
8696 add_loc_descr (&cc_loc_result, x1_ref);
8697 add_loc_descr (&cc_loc_result,
8698 new_loc_descr (DW_OP_piece,
8699 GET_MODE_SIZE (GET_MODE (x1)), 0));
8701 return cc_loc_result;
8704 /* Output a proper Dwarf location descriptor for a variable or parameter
8705 which is either allocated in a register or in a memory location. For a
8706 register, we just generate an OP_REG and the register number. For a
8707 memory location we provide a Dwarf postfix expression describing how to
8708 generate the (dynamic) address of the object onto the address stack.
8710 If we don't know how to describe it, return 0. */
8712 static dw_loc_descr_ref
8713 loc_descriptor (rtx rtl, bool can_use_fbreg)
8715 dw_loc_descr_ref loc_result = NULL;
8717 switch (GET_CODE (rtl))
8719 case SUBREG:
8720 /* The case of a subreg may arise when we have a local (register)
8721 variable or a formal (register) parameter which doesn't quite fill
8722 up an entire register. For now, just assume that it is
8723 legitimate to make the Dwarf info refer to the whole register which
8724 contains the given subreg. */
8725 rtl = SUBREG_REG (rtl);
8727 /* ... fall through ... */
8729 case REG:
8730 loc_result = reg_loc_descriptor (rtl);
8731 break;
8733 case MEM:
8734 loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
8735 can_use_fbreg);
8736 break;
8738 case CONCAT:
8739 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1));
8740 break;
8742 case VAR_LOCATION:
8743 /* Single part. */
8744 if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
8746 loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), can_use_fbreg);
8747 break;
8750 rtl = XEXP (rtl, 1);
8751 /* FALLTHRU */
8753 case PARALLEL:
8755 rtvec par_elems = XVEC (rtl, 0);
8756 int num_elem = GET_NUM_ELEM (par_elems);
8757 enum machine_mode mode;
8758 int i;
8760 /* Create the first one, so we have something to add to. */
8761 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
8762 can_use_fbreg);
8763 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
8764 add_loc_descr (&loc_result,
8765 new_loc_descr (DW_OP_piece, GET_MODE_SIZE (mode), 0));
8766 for (i = 1; i < num_elem; i++)
8768 dw_loc_descr_ref temp;
8770 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
8771 can_use_fbreg);
8772 add_loc_descr (&loc_result, temp);
8773 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
8774 add_loc_descr (&loc_result,
8775 new_loc_descr (DW_OP_piece,
8776 GET_MODE_SIZE (mode), 0));
8779 break;
8781 default:
8782 gcc_unreachable ();
8785 return loc_result;
8788 /* Similar, but generate the descriptor from trees instead of rtl. This comes
8789 up particularly with variable length arrays. WANT_ADDRESS is 2 if this is
8790 a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
8791 top-level invocation, and we require the address of LOC; is 0 if we require
8792 the value of LOC. */
8794 static dw_loc_descr_ref
8795 loc_descriptor_from_tree_1 (tree loc, int want_address)
8797 dw_loc_descr_ref ret, ret1;
8798 int have_address = 0;
8799 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
8800 enum dwarf_location_atom op;
8802 /* ??? Most of the time we do not take proper care for sign/zero
8803 extending the values properly. Hopefully this won't be a real
8804 problem... */
8806 switch (TREE_CODE (loc))
8808 case ERROR_MARK:
8809 return 0;
8811 case PLACEHOLDER_EXPR:
8812 /* This case involves extracting fields from an object to determine the
8813 position of other fields. We don't try to encode this here. The
8814 only user of this is Ada, which encodes the needed information using
8815 the names of types. */
8816 return 0;
8818 case CALL_EXPR:
8819 return 0;
8821 case PREINCREMENT_EXPR:
8822 case PREDECREMENT_EXPR:
8823 case POSTINCREMENT_EXPR:
8824 case POSTDECREMENT_EXPR:
8825 /* There are no opcodes for these operations. */
8826 return 0;
8828 case ADDR_EXPR:
8829 /* If we already want an address, there's nothing we can do. */
8830 if (want_address)
8831 return 0;
8833 /* Otherwise, process the argument and look for the address. */
8834 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
8836 case VAR_DECL:
8837 if (DECL_THREAD_LOCAL (loc))
8839 rtx rtl;
8841 #ifndef ASM_OUTPUT_DWARF_DTPREL
8842 /* If this is not defined, we have no way to emit the data. */
8843 return 0;
8844 #endif
8846 /* The way DW_OP_GNU_push_tls_address is specified, we can only
8847 look up addresses of objects in the current module. */
8848 if (DECL_EXTERNAL (loc))
8849 return 0;
8851 rtl = rtl_for_decl_location (loc);
8852 if (rtl == NULL_RTX)
8853 return 0;
8855 if (!MEM_P (rtl))
8856 return 0;
8857 rtl = XEXP (rtl, 0);
8858 if (! CONSTANT_P (rtl))
8859 return 0;
8861 ret = new_loc_descr (INTERNAL_DW_OP_tls_addr, 0, 0);
8862 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
8863 ret->dw_loc_oprnd1.v.val_addr = rtl;
8865 ret1 = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
8866 add_loc_descr (&ret, ret1);
8868 have_address = 1;
8869 break;
8871 /* FALLTHRU */
8873 case PARM_DECL:
8874 if (DECL_VALUE_EXPR (loc))
8875 return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc), want_address);
8876 /* FALLTHRU */
8878 case RESULT_DECL:
8880 rtx rtl = rtl_for_decl_location (loc);
8882 if (rtl == NULL_RTX)
8883 return 0;
8884 else if (GET_CODE (rtl) == CONST_INT)
8886 HOST_WIDE_INT val = INTVAL (rtl);
8887 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
8888 val &= GET_MODE_MASK (DECL_MODE (loc));
8889 ret = int_loc_descriptor (val);
8891 else if (GET_CODE (rtl) == CONST_STRING)
8892 return 0;
8893 else if (CONSTANT_P (rtl))
8895 ret = new_loc_descr (DW_OP_addr, 0, 0);
8896 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
8897 ret->dw_loc_oprnd1.v.val_addr = rtl;
8899 else
8901 enum machine_mode mode;
8903 /* Certain constructs can only be represented at top-level. */
8904 if (want_address == 2)
8905 return loc_descriptor (rtl, false);
8907 mode = GET_MODE (rtl);
8908 if (MEM_P (rtl))
8910 rtl = XEXP (rtl, 0);
8911 have_address = 1;
8913 ret = mem_loc_descriptor (rtl, mode, false);
8916 break;
8918 case INDIRECT_REF:
8919 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
8920 have_address = 1;
8921 break;
8923 case COMPOUND_EXPR:
8924 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
8926 case NOP_EXPR:
8927 case CONVERT_EXPR:
8928 case NON_LVALUE_EXPR:
8929 case VIEW_CONVERT_EXPR:
8930 case SAVE_EXPR:
8931 case MODIFY_EXPR:
8932 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), want_address);
8934 case COMPONENT_REF:
8935 case BIT_FIELD_REF:
8936 case ARRAY_REF:
8937 case ARRAY_RANGE_REF:
8939 tree obj, offset;
8940 HOST_WIDE_INT bitsize, bitpos, bytepos;
8941 enum machine_mode mode;
8942 int volatilep;
8944 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
8945 &unsignedp, &volatilep, false);
8947 if (obj == loc)
8948 return 0;
8950 ret = loc_descriptor_from_tree_1 (obj, 1);
8951 if (ret == 0
8952 || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
8953 return 0;
8955 if (offset != NULL_TREE)
8957 /* Variable offset. */
8958 add_loc_descr (&ret, loc_descriptor_from_tree_1 (offset, 0));
8959 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
8962 bytepos = bitpos / BITS_PER_UNIT;
8963 if (bytepos > 0)
8964 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
8965 else if (bytepos < 0)
8967 add_loc_descr (&ret, int_loc_descriptor (bytepos));
8968 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
8971 have_address = 1;
8972 break;
8975 case INTEGER_CST:
8976 if (host_integerp (loc, 0))
8977 ret = int_loc_descriptor (tree_low_cst (loc, 0));
8978 else
8979 return 0;
8980 break;
8982 case CONSTRUCTOR:
8984 /* Get an RTL for this, if something has been emitted. */
8985 rtx rtl = lookup_constant_def (loc);
8986 enum machine_mode mode;
8988 if (!rtl || !MEM_P (rtl))
8989 return 0;
8990 mode = GET_MODE (rtl);
8991 rtl = XEXP (rtl, 0);
8992 ret = mem_loc_descriptor (rtl, mode, false);
8993 have_address = 1;
8994 break;
8997 case TRUTH_AND_EXPR:
8998 case TRUTH_ANDIF_EXPR:
8999 case BIT_AND_EXPR:
9000 op = DW_OP_and;
9001 goto do_binop;
9003 case TRUTH_XOR_EXPR:
9004 case BIT_XOR_EXPR:
9005 op = DW_OP_xor;
9006 goto do_binop;
9008 case TRUTH_OR_EXPR:
9009 case TRUTH_ORIF_EXPR:
9010 case BIT_IOR_EXPR:
9011 op = DW_OP_or;
9012 goto do_binop;
9014 case FLOOR_DIV_EXPR:
9015 case CEIL_DIV_EXPR:
9016 case ROUND_DIV_EXPR:
9017 case TRUNC_DIV_EXPR:
9018 op = DW_OP_div;
9019 goto do_binop;
9021 case MINUS_EXPR:
9022 op = DW_OP_minus;
9023 goto do_binop;
9025 case FLOOR_MOD_EXPR:
9026 case CEIL_MOD_EXPR:
9027 case ROUND_MOD_EXPR:
9028 case TRUNC_MOD_EXPR:
9029 op = DW_OP_mod;
9030 goto do_binop;
9032 case MULT_EXPR:
9033 op = DW_OP_mul;
9034 goto do_binop;
9036 case LSHIFT_EXPR:
9037 op = DW_OP_shl;
9038 goto do_binop;
9040 case RSHIFT_EXPR:
9041 op = (unsignedp ? DW_OP_shr : DW_OP_shra);
9042 goto do_binop;
9044 case PLUS_EXPR:
9045 if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
9046 && host_integerp (TREE_OPERAND (loc, 1), 0))
9048 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9049 if (ret == 0)
9050 return 0;
9052 add_loc_descr (&ret,
9053 new_loc_descr (DW_OP_plus_uconst,
9054 tree_low_cst (TREE_OPERAND (loc, 1),
9056 0));
9057 break;
9060 op = DW_OP_plus;
9061 goto do_binop;
9063 case LE_EXPR:
9064 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9065 return 0;
9067 op = DW_OP_le;
9068 goto do_binop;
9070 case GE_EXPR:
9071 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9072 return 0;
9074 op = DW_OP_ge;
9075 goto do_binop;
9077 case LT_EXPR:
9078 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9079 return 0;
9081 op = DW_OP_lt;
9082 goto do_binop;
9084 case GT_EXPR:
9085 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9086 return 0;
9088 op = DW_OP_gt;
9089 goto do_binop;
9091 case EQ_EXPR:
9092 op = DW_OP_eq;
9093 goto do_binop;
9095 case NE_EXPR:
9096 op = DW_OP_ne;
9097 goto do_binop;
9099 do_binop:
9100 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9101 ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9102 if (ret == 0 || ret1 == 0)
9103 return 0;
9105 add_loc_descr (&ret, ret1);
9106 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9107 break;
9109 case TRUTH_NOT_EXPR:
9110 case BIT_NOT_EXPR:
9111 op = DW_OP_not;
9112 goto do_unop;
9114 case ABS_EXPR:
9115 op = DW_OP_abs;
9116 goto do_unop;
9118 case NEGATE_EXPR:
9119 op = DW_OP_neg;
9120 goto do_unop;
9122 do_unop:
9123 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9124 if (ret == 0)
9125 return 0;
9127 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9128 break;
9130 case MIN_EXPR:
9131 case MAX_EXPR:
9133 const enum tree_code code =
9134 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
9136 loc = build3 (COND_EXPR, TREE_TYPE (loc),
9137 build2 (code, integer_type_node,
9138 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
9139 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
9142 /* ... fall through ... */
9144 case COND_EXPR:
9146 dw_loc_descr_ref lhs
9147 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9148 dw_loc_descr_ref rhs
9149 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
9150 dw_loc_descr_ref bra_node, jump_node, tmp;
9152 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9153 if (ret == 0 || lhs == 0 || rhs == 0)
9154 return 0;
9156 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
9157 add_loc_descr (&ret, bra_node);
9159 add_loc_descr (&ret, rhs);
9160 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
9161 add_loc_descr (&ret, jump_node);
9163 add_loc_descr (&ret, lhs);
9164 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9165 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
9167 /* ??? Need a node to point the skip at. Use a nop. */
9168 tmp = new_loc_descr (DW_OP_nop, 0, 0);
9169 add_loc_descr (&ret, tmp);
9170 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9171 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
9173 break;
9175 case FIX_TRUNC_EXPR:
9176 case FIX_CEIL_EXPR:
9177 case FIX_FLOOR_EXPR:
9178 case FIX_ROUND_EXPR:
9179 return 0;
9181 default:
9182 /* Leave front-end specific codes as simply unknown. This comes
9183 up, for instance, with the C STMT_EXPR. */
9184 if ((unsigned int) TREE_CODE (loc)
9185 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
9186 return 0;
9188 #ifdef ENABLE_CHECKING
9189 /* Otherwise this is a generic code; we should just lists all of
9190 these explicitly. Aborting means we forgot one. */
9191 gcc_unreachable ();
9192 #else
9193 /* In a release build, we want to degrade gracefully: better to
9194 generate incomplete debugging information than to crash. */
9195 return NULL;
9196 #endif
9199 /* Show if we can't fill the request for an address. */
9200 if (want_address && !have_address)
9201 return 0;
9203 /* If we've got an address and don't want one, dereference. */
9204 if (!want_address && have_address)
9206 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
9208 if (size > DWARF2_ADDR_SIZE || size == -1)
9209 return 0;
9210 else if (size == DWARF2_ADDR_SIZE)
9211 op = DW_OP_deref;
9212 else
9213 op = DW_OP_deref_size;
9215 add_loc_descr (&ret, new_loc_descr (op, size, 0));
9218 return ret;
9221 static inline dw_loc_descr_ref
9222 loc_descriptor_from_tree (tree loc)
9224 return loc_descriptor_from_tree_1 (loc, 2);
9227 /* Given a value, round it up to the lowest multiple of `boundary'
9228 which is not less than the value itself. */
9230 static inline HOST_WIDE_INT
9231 ceiling (HOST_WIDE_INT value, unsigned int boundary)
9233 return (((value + boundary - 1) / boundary) * boundary);
9236 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
9237 pointer to the declared type for the relevant field variable, or return
9238 `integer_type_node' if the given node turns out to be an
9239 ERROR_MARK node. */
9241 static inline tree
9242 field_type (tree decl)
9244 tree type;
9246 if (TREE_CODE (decl) == ERROR_MARK)
9247 return integer_type_node;
9249 type = DECL_BIT_FIELD_TYPE (decl);
9250 if (type == NULL_TREE)
9251 type = TREE_TYPE (decl);
9253 return type;
9256 /* Given a pointer to a tree node, return the alignment in bits for
9257 it, or else return BITS_PER_WORD if the node actually turns out to
9258 be an ERROR_MARK node. */
9260 static inline unsigned
9261 simple_type_align_in_bits (tree type)
9263 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
9266 static inline unsigned
9267 simple_decl_align_in_bits (tree decl)
9269 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
9272 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
9273 lowest addressed byte of the "containing object" for the given FIELD_DECL,
9274 or return 0 if we are unable to determine what that offset is, either
9275 because the argument turns out to be a pointer to an ERROR_MARK node, or
9276 because the offset is actually variable. (We can't handle the latter case
9277 just yet). */
9279 static HOST_WIDE_INT
9280 field_byte_offset (tree decl)
9282 unsigned int type_align_in_bits;
9283 unsigned int decl_align_in_bits;
9284 unsigned HOST_WIDE_INT type_size_in_bits;
9285 HOST_WIDE_INT object_offset_in_bits;
9286 tree type;
9287 tree field_size_tree;
9288 HOST_WIDE_INT bitpos_int;
9289 HOST_WIDE_INT deepest_bitpos;
9290 unsigned HOST_WIDE_INT field_size_in_bits;
9292 if (TREE_CODE (decl) == ERROR_MARK)
9293 return 0;
9295 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
9297 type = field_type (decl);
9298 field_size_tree = DECL_SIZE (decl);
9300 /* The size could be unspecified if there was an error, or for
9301 a flexible array member. */
9302 if (! field_size_tree)
9303 field_size_tree = bitsize_zero_node;
9305 /* We cannot yet cope with fields whose positions are variable, so
9306 for now, when we see such things, we simply return 0. Someday, we may
9307 be able to handle such cases, but it will be damn difficult. */
9308 if (! host_integerp (bit_position (decl), 0))
9309 return 0;
9311 bitpos_int = int_bit_position (decl);
9313 /* If we don't know the size of the field, pretend it's a full word. */
9314 if (host_integerp (field_size_tree, 1))
9315 field_size_in_bits = tree_low_cst (field_size_tree, 1);
9316 else
9317 field_size_in_bits = BITS_PER_WORD;
9319 type_size_in_bits = simple_type_size_in_bits (type);
9320 type_align_in_bits = simple_type_align_in_bits (type);
9321 decl_align_in_bits = simple_decl_align_in_bits (decl);
9323 /* The GCC front-end doesn't make any attempt to keep track of the starting
9324 bit offset (relative to the start of the containing structure type) of the
9325 hypothetical "containing object" for a bit-field. Thus, when computing
9326 the byte offset value for the start of the "containing object" of a
9327 bit-field, we must deduce this information on our own. This can be rather
9328 tricky to do in some cases. For example, handling the following structure
9329 type definition when compiling for an i386/i486 target (which only aligns
9330 long long's to 32-bit boundaries) can be very tricky:
9332 struct S { int field1; long long field2:31; };
9334 Fortunately, there is a simple rule-of-thumb which can be used in such
9335 cases. When compiling for an i386/i486, GCC will allocate 8 bytes for the
9336 structure shown above. It decides to do this based upon one simple rule
9337 for bit-field allocation. GCC allocates each "containing object" for each
9338 bit-field at the first (i.e. lowest addressed) legitimate alignment
9339 boundary (based upon the required minimum alignment for the declared type
9340 of the field) which it can possibly use, subject to the condition that
9341 there is still enough available space remaining in the containing object
9342 (when allocated at the selected point) to fully accommodate all of the
9343 bits of the bit-field itself.
9345 This simple rule makes it obvious why GCC allocates 8 bytes for each
9346 object of the structure type shown above. When looking for a place to
9347 allocate the "containing object" for `field2', the compiler simply tries
9348 to allocate a 64-bit "containing object" at each successive 32-bit
9349 boundary (starting at zero) until it finds a place to allocate that 64-
9350 bit field such that at least 31 contiguous (and previously unallocated)
9351 bits remain within that selected 64 bit field. (As it turns out, for the
9352 example above, the compiler finds it is OK to allocate the "containing
9353 object" 64-bit field at bit-offset zero within the structure type.)
9355 Here we attempt to work backwards from the limited set of facts we're
9356 given, and we try to deduce from those facts, where GCC must have believed
9357 that the containing object started (within the structure type). The value
9358 we deduce is then used (by the callers of this routine) to generate
9359 DW_AT_location and DW_AT_bit_offset attributes for fields (both bit-fields
9360 and, in the case of DW_AT_location, regular fields as well). */
9362 /* Figure out the bit-distance from the start of the structure to the
9363 "deepest" bit of the bit-field. */
9364 deepest_bitpos = bitpos_int + field_size_in_bits;
9366 /* This is the tricky part. Use some fancy footwork to deduce where the
9367 lowest addressed bit of the containing object must be. */
9368 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9370 /* Round up to type_align by default. This works best for bitfields. */
9371 object_offset_in_bits += type_align_in_bits - 1;
9372 object_offset_in_bits /= type_align_in_bits;
9373 object_offset_in_bits *= type_align_in_bits;
9375 if (object_offset_in_bits > bitpos_int)
9377 /* Sigh, the decl must be packed. */
9378 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9380 /* Round up to decl_align instead. */
9381 object_offset_in_bits += decl_align_in_bits - 1;
9382 object_offset_in_bits /= decl_align_in_bits;
9383 object_offset_in_bits *= decl_align_in_bits;
9386 return object_offset_in_bits / BITS_PER_UNIT;
9389 /* The following routines define various Dwarf attributes and any data
9390 associated with them. */
9392 /* Add a location description attribute value to a DIE.
9394 This emits location attributes suitable for whole variables and
9395 whole parameters. Note that the location attributes for struct fields are
9396 generated by the routine `data_member_location_attribute' below. */
9398 static inline void
9399 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
9400 dw_loc_descr_ref descr)
9402 if (descr != 0)
9403 add_AT_loc (die, attr_kind, descr);
9406 /* Attach the specialized form of location attribute used for data members of
9407 struct and union types. In the special case of a FIELD_DECL node which
9408 represents a bit-field, the "offset" part of this special location
9409 descriptor must indicate the distance in bytes from the lowest-addressed
9410 byte of the containing struct or union type to the lowest-addressed byte of
9411 the "containing object" for the bit-field. (See the `field_byte_offset'
9412 function above).
9414 For any given bit-field, the "containing object" is a hypothetical object
9415 (of some integral or enum type) within which the given bit-field lives. The
9416 type of this hypothetical "containing object" is always the same as the
9417 declared type of the individual bit-field itself (for GCC anyway... the
9418 DWARF spec doesn't actually mandate this). Note that it is the size (in
9419 bytes) of the hypothetical "containing object" which will be given in the
9420 DW_AT_byte_size attribute for this bit-field. (See the
9421 `byte_size_attribute' function below.) It is also used when calculating the
9422 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
9423 function below.) */
9425 static void
9426 add_data_member_location_attribute (dw_die_ref die, tree decl)
9428 HOST_WIDE_INT offset;
9429 dw_loc_descr_ref loc_descr = 0;
9431 if (TREE_CODE (decl) == TREE_BINFO)
9433 /* We're working on the TAG_inheritance for a base class. */
9434 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
9436 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
9437 aren't at a fixed offset from all (sub)objects of the same
9438 type. We need to extract the appropriate offset from our
9439 vtable. The following dwarf expression means
9441 BaseAddr = ObAddr + *((*ObAddr) - Offset)
9443 This is specific to the V3 ABI, of course. */
9445 dw_loc_descr_ref tmp;
9447 /* Make a copy of the object address. */
9448 tmp = new_loc_descr (DW_OP_dup, 0, 0);
9449 add_loc_descr (&loc_descr, tmp);
9451 /* Extract the vtable address. */
9452 tmp = new_loc_descr (DW_OP_deref, 0, 0);
9453 add_loc_descr (&loc_descr, tmp);
9455 /* Calculate the address of the offset. */
9456 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
9457 gcc_assert (offset < 0);
9459 tmp = int_loc_descriptor (-offset);
9460 add_loc_descr (&loc_descr, tmp);
9461 tmp = new_loc_descr (DW_OP_minus, 0, 0);
9462 add_loc_descr (&loc_descr, tmp);
9464 /* Extract the offset. */
9465 tmp = new_loc_descr (DW_OP_deref, 0, 0);
9466 add_loc_descr (&loc_descr, tmp);
9468 /* Add it to the object address. */
9469 tmp = new_loc_descr (DW_OP_plus, 0, 0);
9470 add_loc_descr (&loc_descr, tmp);
9472 else
9473 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
9475 else
9476 offset = field_byte_offset (decl);
9478 if (! loc_descr)
9480 enum dwarf_location_atom op;
9482 /* The DWARF2 standard says that we should assume that the structure
9483 address is already on the stack, so we can specify a structure field
9484 address by using DW_OP_plus_uconst. */
9486 #ifdef MIPS_DEBUGGING_INFO
9487 /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
9488 operator correctly. It works only if we leave the offset on the
9489 stack. */
9490 op = DW_OP_constu;
9491 #else
9492 op = DW_OP_plus_uconst;
9493 #endif
9495 loc_descr = new_loc_descr (op, offset, 0);
9498 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
9501 /* Writes integer values to dw_vec_const array. */
9503 static void
9504 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
9506 while (size != 0)
9508 *dest++ = val & 0xff;
9509 val >>= 8;
9510 --size;
9514 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
9516 static HOST_WIDE_INT
9517 extract_int (const unsigned char *src, unsigned int size)
9519 HOST_WIDE_INT val = 0;
9521 src += size;
9522 while (size != 0)
9524 val <<= 8;
9525 val |= *--src & 0xff;
9526 --size;
9528 return val;
9531 /* Writes floating point values to dw_vec_const array. */
9533 static void
9534 insert_float (rtx rtl, unsigned char *array)
9536 REAL_VALUE_TYPE rv;
9537 long val[4];
9538 int i;
9540 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
9541 real_to_target (val, &rv, GET_MODE (rtl));
9543 /* real_to_target puts 32-bit pieces in each long. Pack them. */
9544 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
9546 insert_int (val[i], 4, array);
9547 array += 4;
9551 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
9552 does not have a "location" either in memory or in a register. These
9553 things can arise in GNU C when a constant is passed as an actual parameter
9554 to an inlined function. They can also arise in C++ where declared
9555 constants do not necessarily get memory "homes". */
9557 static void
9558 add_const_value_attribute (dw_die_ref die, rtx rtl)
9560 switch (GET_CODE (rtl))
9562 case CONST_INT:
9564 HOST_WIDE_INT val = INTVAL (rtl);
9566 if (val < 0)
9567 add_AT_int (die, DW_AT_const_value, val);
9568 else
9569 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
9571 break;
9573 case CONST_DOUBLE:
9574 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
9575 floating-point constant. A CONST_DOUBLE is used whenever the
9576 constant requires more than one word in order to be adequately
9577 represented. We output CONST_DOUBLEs as blocks. */
9579 enum machine_mode mode = GET_MODE (rtl);
9581 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
9583 unsigned int length = GET_MODE_SIZE (mode);
9584 unsigned char *array = ggc_alloc (length);
9586 insert_float (rtl, array);
9587 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
9589 else
9591 /* ??? We really should be using HOST_WIDE_INT throughout. */
9592 gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
9594 add_AT_long_long (die, DW_AT_const_value,
9595 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
9598 break;
9600 case CONST_VECTOR:
9602 enum machine_mode mode = GET_MODE (rtl);
9603 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
9604 unsigned int length = CONST_VECTOR_NUNITS (rtl);
9605 unsigned char *array = ggc_alloc (length * elt_size);
9606 unsigned int i;
9607 unsigned char *p;
9609 switch (GET_MODE_CLASS (mode))
9611 case MODE_VECTOR_INT:
9612 for (i = 0, p = array; i < length; i++, p += elt_size)
9614 rtx elt = CONST_VECTOR_ELT (rtl, i);
9615 HOST_WIDE_INT lo, hi;
9617 switch (GET_CODE (elt))
9619 case CONST_INT:
9620 lo = INTVAL (elt);
9621 hi = -(lo < 0);
9622 break;
9624 case CONST_DOUBLE:
9625 lo = CONST_DOUBLE_LOW (elt);
9626 hi = CONST_DOUBLE_HIGH (elt);
9627 break;
9629 default:
9630 gcc_unreachable ();
9633 if (elt_size <= sizeof (HOST_WIDE_INT))
9634 insert_int (lo, elt_size, p);
9635 else
9637 unsigned char *p0 = p;
9638 unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
9640 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
9641 if (WORDS_BIG_ENDIAN)
9643 p0 = p1;
9644 p1 = p;
9646 insert_int (lo, sizeof (HOST_WIDE_INT), p0);
9647 insert_int (hi, sizeof (HOST_WIDE_INT), p1);
9650 break;
9652 case MODE_VECTOR_FLOAT:
9653 for (i = 0, p = array; i < length; i++, p += elt_size)
9655 rtx elt = CONST_VECTOR_ELT (rtl, i);
9656 insert_float (elt, p);
9658 break;
9660 default:
9661 gcc_unreachable ();
9664 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
9666 break;
9668 case CONST_STRING:
9669 add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
9670 break;
9672 case SYMBOL_REF:
9673 case LABEL_REF:
9674 case CONST:
9675 add_AT_addr (die, DW_AT_const_value, rtl);
9676 VARRAY_PUSH_RTX (used_rtx_varray, rtl);
9677 break;
9679 case PLUS:
9680 /* In cases where an inlined instance of an inline function is passed
9681 the address of an `auto' variable (which is local to the caller) we
9682 can get a situation where the DECL_RTL of the artificial local
9683 variable (for the inlining) which acts as a stand-in for the
9684 corresponding formal parameter (of the inline function) will look
9685 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
9686 exactly a compile-time constant expression, but it isn't the address
9687 of the (artificial) local variable either. Rather, it represents the
9688 *value* which the artificial local variable always has during its
9689 lifetime. We currently have no way to represent such quasi-constant
9690 values in Dwarf, so for now we just punt and generate nothing. */
9691 break;
9693 default:
9694 /* No other kinds of rtx should be possible here. */
9695 gcc_unreachable ();
9700 static rtx
9701 rtl_for_decl_location (tree decl)
9703 rtx rtl;
9705 /* Here we have to decide where we are going to say the parameter "lives"
9706 (as far as the debugger is concerned). We only have a couple of
9707 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
9709 DECL_RTL normally indicates where the parameter lives during most of the
9710 activation of the function. If optimization is enabled however, this
9711 could be either NULL or else a pseudo-reg. Both of those cases indicate
9712 that the parameter doesn't really live anywhere (as far as the code
9713 generation parts of GCC are concerned) during most of the function's
9714 activation. That will happen (for example) if the parameter is never
9715 referenced within the function.
9717 We could just generate a location descriptor here for all non-NULL
9718 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
9719 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
9720 where DECL_RTL is NULL or is a pseudo-reg.
9722 Note however that we can only get away with using DECL_INCOMING_RTL as
9723 a backup substitute for DECL_RTL in certain limited cases. In cases
9724 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
9725 we can be sure that the parameter was passed using the same type as it is
9726 declared to have within the function, and that its DECL_INCOMING_RTL
9727 points us to a place where a value of that type is passed.
9729 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
9730 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
9731 because in these cases DECL_INCOMING_RTL points us to a value of some
9732 type which is *different* from the type of the parameter itself. Thus,
9733 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
9734 such cases, the debugger would end up (for example) trying to fetch a
9735 `float' from a place which actually contains the first part of a
9736 `double'. That would lead to really incorrect and confusing
9737 output at debug-time.
9739 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
9740 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
9741 are a couple of exceptions however. On little-endian machines we can
9742 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
9743 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
9744 an integral type that is smaller than TREE_TYPE (decl). These cases arise
9745 when (on a little-endian machine) a non-prototyped function has a
9746 parameter declared to be of type `short' or `char'. In such cases,
9747 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
9748 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
9749 passed `int' value. If the debugger then uses that address to fetch
9750 a `short' or a `char' (on a little-endian machine) the result will be
9751 the correct data, so we allow for such exceptional cases below.
9753 Note that our goal here is to describe the place where the given formal
9754 parameter lives during most of the function's activation (i.e. between the
9755 end of the prologue and the start of the epilogue). We'll do that as best
9756 as we can. Note however that if the given formal parameter is modified
9757 sometime during the execution of the function, then a stack backtrace (at
9758 debug-time) will show the function as having been called with the *new*
9759 value rather than the value which was originally passed in. This happens
9760 rarely enough that it is not a major problem, but it *is* a problem, and
9761 I'd like to fix it.
9763 A future version of dwarf2out.c may generate two additional attributes for
9764 any given DW_TAG_formal_parameter DIE which will describe the "passed
9765 type" and the "passed location" for the given formal parameter in addition
9766 to the attributes we now generate to indicate the "declared type" and the
9767 "active location" for each parameter. This additional set of attributes
9768 could be used by debuggers for stack backtraces. Separately, note that
9769 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
9770 This happens (for example) for inlined-instances of inline function formal
9771 parameters which are never referenced. This really shouldn't be
9772 happening. All PARM_DECL nodes should get valid non-NULL
9773 DECL_INCOMING_RTL values. FIXME. */
9775 /* Use DECL_RTL as the "location" unless we find something better. */
9776 rtl = DECL_RTL_IF_SET (decl);
9778 /* When generating abstract instances, ignore everything except
9779 constants, symbols living in memory, and symbols living in
9780 fixed registers. */
9781 if (! reload_completed)
9783 if (rtl
9784 && (CONSTANT_P (rtl)
9785 || (MEM_P (rtl)
9786 && CONSTANT_P (XEXP (rtl, 0)))
9787 || (REG_P (rtl)
9788 && TREE_CODE (decl) == VAR_DECL
9789 && TREE_STATIC (decl))))
9791 rtl = targetm.delegitimize_address (rtl);
9792 return rtl;
9794 rtl = NULL_RTX;
9796 else if (TREE_CODE (decl) == PARM_DECL)
9798 if (rtl == NULL_RTX || is_pseudo_reg (rtl))
9800 tree declared_type = TREE_TYPE (decl);
9801 tree passed_type = DECL_ARG_TYPE (decl);
9802 enum machine_mode dmode = TYPE_MODE (declared_type);
9803 enum machine_mode pmode = TYPE_MODE (passed_type);
9805 /* This decl represents a formal parameter which was optimized out.
9806 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
9807 all cases where (rtl == NULL_RTX) just below. */
9808 if (dmode == pmode)
9809 rtl = DECL_INCOMING_RTL (decl);
9810 else if (SCALAR_INT_MODE_P (dmode)
9811 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
9812 && DECL_INCOMING_RTL (decl))
9814 rtx inc = DECL_INCOMING_RTL (decl);
9815 if (REG_P (inc))
9816 rtl = inc;
9817 else if (MEM_P (inc))
9819 if (BYTES_BIG_ENDIAN)
9820 rtl = adjust_address_nv (inc, dmode,
9821 GET_MODE_SIZE (pmode)
9822 - GET_MODE_SIZE (dmode));
9823 else
9824 rtl = inc;
9829 /* If the parm was passed in registers, but lives on the stack, then
9830 make a big endian correction if the mode of the type of the
9831 parameter is not the same as the mode of the rtl. */
9832 /* ??? This is the same series of checks that are made in dbxout.c before
9833 we reach the big endian correction code there. It isn't clear if all
9834 of these checks are necessary here, but keeping them all is the safe
9835 thing to do. */
9836 else if (MEM_P (rtl)
9837 && XEXP (rtl, 0) != const0_rtx
9838 && ! CONSTANT_P (XEXP (rtl, 0))
9839 /* Not passed in memory. */
9840 && !MEM_P (DECL_INCOMING_RTL (decl))
9841 /* Not passed by invisible reference. */
9842 && (!REG_P (XEXP (rtl, 0))
9843 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
9844 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
9845 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
9846 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
9847 #endif
9849 /* Big endian correction check. */
9850 && BYTES_BIG_ENDIAN
9851 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
9852 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
9853 < UNITS_PER_WORD))
9855 int offset = (UNITS_PER_WORD
9856 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
9858 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
9859 plus_constant (XEXP (rtl, 0), offset));
9862 else if (TREE_CODE (decl) == VAR_DECL
9863 && rtl
9864 && MEM_P (rtl)
9865 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
9866 && BYTES_BIG_ENDIAN)
9868 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
9869 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
9871 /* If a variable is declared "register" yet is smaller than
9872 a register, then if we store the variable to memory, it
9873 looks like we're storing a register-sized value, when in
9874 fact we are not. We need to adjust the offset of the
9875 storage location to reflect the actual value's bytes,
9876 else gdb will not be able to display it. */
9877 if (rsize > dsize)
9878 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
9879 plus_constant (XEXP (rtl, 0), rsize-dsize));
9882 if (rtl != NULL_RTX)
9884 rtl = eliminate_regs (rtl, 0, NULL_RTX);
9885 #ifdef LEAF_REG_REMAP
9886 if (current_function_uses_only_leaf_regs)
9887 leaf_renumber_regs_insn (rtl);
9888 #endif
9891 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
9892 and will have been substituted directly into all expressions that use it.
9893 C does not have such a concept, but C++ and other languages do. */
9894 else if (TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
9896 /* If a variable is initialized with a string constant without embedded
9897 zeros, build CONST_STRING. */
9898 if (TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
9899 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
9901 tree arrtype = TREE_TYPE (decl);
9902 tree enttype = TREE_TYPE (arrtype);
9903 tree domain = TYPE_DOMAIN (arrtype);
9904 tree init = DECL_INITIAL (decl);
9905 enum machine_mode mode = TYPE_MODE (enttype);
9907 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
9908 && domain
9909 && integer_zerop (TYPE_MIN_VALUE (domain))
9910 && compare_tree_int (TYPE_MAX_VALUE (domain),
9911 TREE_STRING_LENGTH (init) - 1) == 0
9912 && ((size_t) TREE_STRING_LENGTH (init)
9913 == strlen (TREE_STRING_POINTER (init)) + 1))
9914 rtl = gen_rtx_CONST_STRING (VOIDmode,
9915 ggc_strdup (TREE_STRING_POINTER (init)));
9917 /* If the initializer is something that we know will expand into an
9918 immediate RTL constant, expand it now. Expanding anything else
9919 tends to produce unresolved symbols; see debug/5770 and c++/6381. */
9920 else if (TREE_CODE (DECL_INITIAL (decl)) == INTEGER_CST
9921 || TREE_CODE (DECL_INITIAL (decl)) == REAL_CST)
9923 rtl = expand_expr (DECL_INITIAL (decl), NULL_RTX, VOIDmode,
9924 EXPAND_INITIALIZER);
9925 /* If expand_expr returns a MEM, it wasn't immediate. */
9926 gcc_assert (!rtl || !MEM_P (rtl));
9930 if (rtl)
9931 rtl = targetm.delegitimize_address (rtl);
9933 /* If we don't look past the constant pool, we risk emitting a
9934 reference to a constant pool entry that isn't referenced from
9935 code, and thus is not emitted. */
9936 if (rtl)
9937 rtl = avoid_constant_pool_reference (rtl);
9939 return rtl;
9942 /* Return true if DECL's containing function has a frame base attribute.
9943 Return false otherwise. */
9945 static bool
9946 containing_function_has_frame_base (tree decl)
9948 tree declcontext = decl_function_context (decl);
9949 dw_die_ref context;
9950 dw_attr_ref attr;
9952 if (!declcontext)
9953 return false;
9955 context = lookup_decl_die (declcontext);
9956 if (!context)
9957 return false;
9959 for (attr = context->die_attr; attr; attr = attr->dw_attr_next)
9960 if (attr->dw_attr == DW_AT_frame_base)
9961 return true;
9962 return false;
9965 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
9966 data attribute for a variable or a parameter. We generate the
9967 DW_AT_const_value attribute only in those cases where the given variable
9968 or parameter does not have a true "location" either in memory or in a
9969 register. This can happen (for example) when a constant is passed as an
9970 actual argument in a call to an inline function. (It's possible that
9971 these things can crop up in other ways also.) Note that one type of
9972 constant value which can be passed into an inlined function is a constant
9973 pointer. This can happen for example if an actual argument in an inlined
9974 function call evaluates to a compile-time constant address. */
9976 static void
9977 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
9978 enum dwarf_attribute attr)
9980 rtx rtl;
9981 dw_loc_descr_ref descr;
9982 var_loc_list *loc_list;
9983 bool can_use_fb;
9984 struct var_loc_node *node;
9985 if (TREE_CODE (decl) == ERROR_MARK)
9986 return;
9988 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
9989 || TREE_CODE (decl) == RESULT_DECL);
9991 can_use_fb = containing_function_has_frame_base (decl);
9993 /* See if we possibly have multiple locations for this variable. */
9994 loc_list = lookup_decl_loc (decl);
9996 /* If it truly has multiple locations, the first and last node will
9997 differ. */
9998 if (loc_list && loc_list->first != loc_list->last)
10000 const char *secname;
10001 const char *endname;
10002 dw_loc_list_ref list;
10003 rtx varloc;
10006 /* We need to figure out what section we should use as the base
10007 for the address ranges where a given location is valid.
10008 1. If this particular DECL has a section associated with it,
10009 use that.
10010 2. If this function has a section associated with it, use
10011 that.
10012 3. Otherwise, use the text section.
10013 XXX: If you split a variable across multiple sections, this
10014 won't notice. */
10016 if (DECL_SECTION_NAME (decl))
10018 tree sectree = DECL_SECTION_NAME (decl);
10019 secname = TREE_STRING_POINTER (sectree);
10021 else if (current_function_decl
10022 && DECL_SECTION_NAME (current_function_decl))
10024 tree sectree = DECL_SECTION_NAME (current_function_decl);
10025 secname = TREE_STRING_POINTER (sectree);
10027 else
10028 secname = text_section_label;
10030 /* Now that we know what section we are using for a base,
10031 actually construct the list of locations.
10032 The first location information is what is passed to the
10033 function that creates the location list, and the remaining
10034 locations just get added on to that list.
10035 Note that we only know the start address for a location
10036 (IE location changes), so to build the range, we use
10037 the range [current location start, next location start].
10038 This means we have to special case the last node, and generate
10039 a range of [last location start, end of function label]. */
10041 node = loc_list->first;
10042 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10043 list = new_loc_list (loc_descriptor (varloc, can_use_fb),
10044 node->label, node->next->label, secname, 1);
10045 node = node->next;
10047 for (; node->next; node = node->next)
10048 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10050 /* The variable has a location between NODE->LABEL and
10051 NODE->NEXT->LABEL. */
10052 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10053 add_loc_descr_to_loc_list (&list,
10054 loc_descriptor (varloc,
10055 can_use_fb),
10056 node->label, node->next->label, secname);
10059 /* If the variable has a location at the last label
10060 it keeps its location until the end of function. */
10061 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10063 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
10065 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10066 if (!current_function_decl)
10067 endname = text_end_label;
10068 else
10070 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
10071 current_function_funcdef_no);
10072 endname = ggc_strdup (label_id);
10074 add_loc_descr_to_loc_list (&list,
10075 loc_descriptor (varloc,
10076 can_use_fb),
10077 node->label, endname, secname);
10080 /* Finally, add the location list to the DIE, and we are done. */
10081 add_AT_loc_list (die, attr, list);
10082 return;
10085 /* Try to get some constant RTL for this decl, and use that as the value of
10086 the location. */
10088 rtl = rtl_for_decl_location (decl);
10089 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
10091 add_const_value_attribute (die, rtl);
10092 return;
10095 /* We couldn't get any rtl, and we had no >1 element location list, so try
10096 directly generating the location description from the tree. */
10097 descr = loc_descriptor_from_tree (decl);
10098 if (descr)
10100 add_AT_location_description (die, attr, descr);
10101 return;
10104 /* Lastly, if we have tried to generate the location otherwise, and it
10105 didn't work out (we wouldn't be here if we did), and we have a one entry
10106 location list, try generating a location from that. */
10107 if (loc_list && loc_list->first)
10109 node = loc_list->first;
10110 descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note),
10111 can_use_fb);
10112 if (descr)
10113 add_AT_location_description (die, attr, descr);
10117 /* If we don't have a copy of this variable in memory for some reason (such
10118 as a C++ member constant that doesn't have an out-of-line definition),
10119 we should tell the debugger about the constant value. */
10121 static void
10122 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
10124 tree init = DECL_INITIAL (decl);
10125 tree type = TREE_TYPE (decl);
10127 if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init
10128 && initializer_constant_valid_p (init, type) == null_pointer_node)
10129 /* OK */;
10130 else
10131 return;
10133 switch (TREE_CODE (type))
10135 case INTEGER_TYPE:
10136 if (host_integerp (init, 0))
10137 add_AT_unsigned (var_die, DW_AT_const_value,
10138 tree_low_cst (init, 0));
10139 else
10140 add_AT_long_long (var_die, DW_AT_const_value,
10141 TREE_INT_CST_HIGH (init),
10142 TREE_INT_CST_LOW (init));
10143 break;
10145 default:;
10149 /* Generate a DW_AT_name attribute given some string value to be included as
10150 the value of the attribute. */
10152 static void
10153 add_name_attribute (dw_die_ref die, const char *name_string)
10155 if (name_string != NULL && *name_string != 0)
10157 if (demangle_name_func)
10158 name_string = (*demangle_name_func) (name_string);
10160 add_AT_string (die, DW_AT_name, name_string);
10164 /* Generate a DW_AT_comp_dir attribute for DIE. */
10166 static void
10167 add_comp_dir_attribute (dw_die_ref die)
10169 const char *wd = get_src_pwd ();
10170 if (wd != NULL)
10171 add_AT_string (die, DW_AT_comp_dir, wd);
10174 /* Given a tree node describing an array bound (either lower or upper) output
10175 a representation for that bound. */
10177 static void
10178 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
10180 switch (TREE_CODE (bound))
10182 case ERROR_MARK:
10183 return;
10185 /* All fixed-bounds are represented by INTEGER_CST nodes. */
10186 case INTEGER_CST:
10187 if (! host_integerp (bound, 0)
10188 || (bound_attr == DW_AT_lower_bound
10189 && (((is_c_family () || is_java ()) && integer_zerop (bound))
10190 || (is_fortran () && integer_onep (bound)))))
10191 /* Use the default. */
10193 else
10194 add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
10195 break;
10197 case CONVERT_EXPR:
10198 case NOP_EXPR:
10199 case NON_LVALUE_EXPR:
10200 case VIEW_CONVERT_EXPR:
10201 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
10202 break;
10204 case SAVE_EXPR:
10205 break;
10207 case VAR_DECL:
10208 case PARM_DECL:
10209 case RESULT_DECL:
10211 dw_die_ref decl_die = lookup_decl_die (bound);
10213 /* ??? Can this happen, or should the variable have been bound
10214 first? Probably it can, since I imagine that we try to create
10215 the types of parameters in the order in which they exist in
10216 the list, and won't have created a forward reference to a
10217 later parameter. */
10218 if (decl_die != NULL)
10219 add_AT_die_ref (subrange_die, bound_attr, decl_die);
10220 break;
10223 default:
10225 /* Otherwise try to create a stack operation procedure to
10226 evaluate the value of the array bound. */
10228 dw_die_ref ctx, decl_die;
10229 dw_loc_descr_ref loc;
10231 loc = loc_descriptor_from_tree (bound);
10232 if (loc == NULL)
10233 break;
10235 if (current_function_decl == 0)
10236 ctx = comp_unit_die;
10237 else
10238 ctx = lookup_decl_die (current_function_decl);
10240 decl_die = new_die (DW_TAG_variable, ctx, bound);
10241 add_AT_flag (decl_die, DW_AT_artificial, 1);
10242 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
10243 add_AT_loc (decl_die, DW_AT_location, loc);
10245 add_AT_die_ref (subrange_die, bound_attr, decl_die);
10246 break;
10251 /* Note that the block of subscript information for an array type also
10252 includes information about the element type of type given array type. */
10254 static void
10255 add_subscript_info (dw_die_ref type_die, tree type)
10257 #ifndef MIPS_DEBUGGING_INFO
10258 unsigned dimension_number;
10259 #endif
10260 tree lower, upper;
10261 dw_die_ref subrange_die;
10263 /* The GNU compilers represent multidimensional array types as sequences of
10264 one dimensional array types whose element types are themselves array
10265 types. Here we squish that down, so that each multidimensional array
10266 type gets only one array_type DIE in the Dwarf debugging info. The draft
10267 Dwarf specification say that we are allowed to do this kind of
10268 compression in C (because there is no difference between an array or
10269 arrays and a multidimensional array in C) but for other source languages
10270 (e.g. Ada) we probably shouldn't do this. */
10272 /* ??? The SGI dwarf reader fails for multidimensional arrays with a
10273 const enum type. E.g. const enum machine_mode insn_operand_mode[2][10].
10274 We work around this by disabling this feature. See also
10275 gen_array_type_die. */
10276 #ifndef MIPS_DEBUGGING_INFO
10277 for (dimension_number = 0;
10278 TREE_CODE (type) == ARRAY_TYPE;
10279 type = TREE_TYPE (type), dimension_number++)
10280 #endif
10282 tree domain = TYPE_DOMAIN (type);
10284 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
10285 and (in GNU C only) variable bounds. Handle all three forms
10286 here. */
10287 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
10288 if (domain)
10290 /* We have an array type with specified bounds. */
10291 lower = TYPE_MIN_VALUE (domain);
10292 upper = TYPE_MAX_VALUE (domain);
10294 /* Define the index type. */
10295 if (TREE_TYPE (domain))
10297 /* ??? This is probably an Ada unnamed subrange type. Ignore the
10298 TREE_TYPE field. We can't emit debug info for this
10299 because it is an unnamed integral type. */
10300 if (TREE_CODE (domain) == INTEGER_TYPE
10301 && TYPE_NAME (domain) == NULL_TREE
10302 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
10303 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
10305 else
10306 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
10307 type_die);
10310 /* ??? If upper is NULL, the array has unspecified length,
10311 but it does have a lower bound. This happens with Fortran
10312 dimension arr(N:*)
10313 Since the debugger is definitely going to need to know N
10314 to produce useful results, go ahead and output the lower
10315 bound solo, and hope the debugger can cope. */
10317 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
10318 if (upper)
10319 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
10322 /* Otherwise we have an array type with an unspecified length. The
10323 DWARF-2 spec does not say how to handle this; let's just leave out the
10324 bounds. */
10328 static void
10329 add_byte_size_attribute (dw_die_ref die, tree tree_node)
10331 unsigned size;
10333 switch (TREE_CODE (tree_node))
10335 case ERROR_MARK:
10336 size = 0;
10337 break;
10338 case ENUMERAL_TYPE:
10339 case RECORD_TYPE:
10340 case UNION_TYPE:
10341 case QUAL_UNION_TYPE:
10342 size = int_size_in_bytes (tree_node);
10343 break;
10344 case FIELD_DECL:
10345 /* For a data member of a struct or union, the DW_AT_byte_size is
10346 generally given as the number of bytes normally allocated for an
10347 object of the *declared* type of the member itself. This is true
10348 even for bit-fields. */
10349 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
10350 break;
10351 default:
10352 gcc_unreachable ();
10355 /* Note that `size' might be -1 when we get to this point. If it is, that
10356 indicates that the byte size of the entity in question is variable. We
10357 have no good way of expressing this fact in Dwarf at the present time,
10358 so just let the -1 pass on through. */
10359 add_AT_unsigned (die, DW_AT_byte_size, size);
10362 /* For a FIELD_DECL node which represents a bit-field, output an attribute
10363 which specifies the distance in bits from the highest order bit of the
10364 "containing object" for the bit-field to the highest order bit of the
10365 bit-field itself.
10367 For any given bit-field, the "containing object" is a hypothetical object
10368 (of some integral or enum type) within which the given bit-field lives. The
10369 type of this hypothetical "containing object" is always the same as the
10370 declared type of the individual bit-field itself. The determination of the
10371 exact location of the "containing object" for a bit-field is rather
10372 complicated. It's handled by the `field_byte_offset' function (above).
10374 Note that it is the size (in bytes) of the hypothetical "containing object"
10375 which will be given in the DW_AT_byte_size attribute for this bit-field.
10376 (See `byte_size_attribute' above). */
10378 static inline void
10379 add_bit_offset_attribute (dw_die_ref die, tree decl)
10381 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
10382 tree type = DECL_BIT_FIELD_TYPE (decl);
10383 HOST_WIDE_INT bitpos_int;
10384 HOST_WIDE_INT highest_order_object_bit_offset;
10385 HOST_WIDE_INT highest_order_field_bit_offset;
10386 HOST_WIDE_INT unsigned bit_offset;
10388 /* Must be a field and a bit field. */
10389 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
10391 /* We can't yet handle bit-fields whose offsets are variable, so if we
10392 encounter such things, just return without generating any attribute
10393 whatsoever. Likewise for variable or too large size. */
10394 if (! host_integerp (bit_position (decl), 0)
10395 || ! host_integerp (DECL_SIZE (decl), 1))
10396 return;
10398 bitpos_int = int_bit_position (decl);
10400 /* Note that the bit offset is always the distance (in bits) from the
10401 highest-order bit of the "containing object" to the highest-order bit of
10402 the bit-field itself. Since the "high-order end" of any object or field
10403 is different on big-endian and little-endian machines, the computation
10404 below must take account of these differences. */
10405 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
10406 highest_order_field_bit_offset = bitpos_int;
10408 if (! BYTES_BIG_ENDIAN)
10410 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
10411 highest_order_object_bit_offset += simple_type_size_in_bits (type);
10414 bit_offset
10415 = (! BYTES_BIG_ENDIAN
10416 ? highest_order_object_bit_offset - highest_order_field_bit_offset
10417 : highest_order_field_bit_offset - highest_order_object_bit_offset);
10419 add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
10422 /* For a FIELD_DECL node which represents a bit field, output an attribute
10423 which specifies the length in bits of the given field. */
10425 static inline void
10426 add_bit_size_attribute (dw_die_ref die, tree decl)
10428 /* Must be a field and a bit field. */
10429 gcc_assert (TREE_CODE (decl) == FIELD_DECL
10430 && DECL_BIT_FIELD_TYPE (decl));
10432 if (host_integerp (DECL_SIZE (decl), 1))
10433 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
10436 /* If the compiled language is ANSI C, then add a 'prototyped'
10437 attribute, if arg types are given for the parameters of a function. */
10439 static inline void
10440 add_prototyped_attribute (dw_die_ref die, tree func_type)
10442 if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
10443 && TYPE_ARG_TYPES (func_type) != NULL)
10444 add_AT_flag (die, DW_AT_prototyped, 1);
10447 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
10448 by looking in either the type declaration or object declaration
10449 equate table. */
10451 static inline void
10452 add_abstract_origin_attribute (dw_die_ref die, tree origin)
10454 dw_die_ref origin_die = NULL;
10456 if (TREE_CODE (origin) != FUNCTION_DECL)
10458 /* We may have gotten separated from the block for the inlined
10459 function, if we're in an exception handler or some such; make
10460 sure that the abstract function has been written out.
10462 Doing this for nested functions is wrong, however; functions are
10463 distinct units, and our context might not even be inline. */
10464 tree fn = origin;
10466 if (TYPE_P (fn))
10467 fn = TYPE_STUB_DECL (fn);
10469 /* TYPE_STUB_DECL may have given us a NULL, which decl_function_context
10470 won't like. */
10471 if (fn)
10472 fn = decl_function_context (fn);
10474 if (fn)
10475 dwarf2out_abstract_function (fn);
10478 if (DECL_P (origin))
10479 origin_die = lookup_decl_die (origin);
10480 else if (TYPE_P (origin))
10481 origin_die = lookup_type_die (origin);
10483 /* XXX: Functions that are never lowered don't always have correct block
10484 trees (in the case of java, they simply have no block tree, in some other
10485 languages). For these functions, there is nothing we can really do to
10486 output correct debug info for inlined functions in all cases. Rather
10487 than abort, we'll just produce deficient debug info now, in that we will
10488 have variables without a proper abstract origin. In the future, when all
10489 functions are lowered, we should re-add a gcc_assert (origin_die)
10490 here. */
10492 if (origin_die)
10493 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
10496 /* We do not currently support the pure_virtual attribute. */
10498 static inline void
10499 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
10501 if (DECL_VINDEX (func_decl))
10503 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
10505 if (host_integerp (DECL_VINDEX (func_decl), 0))
10506 add_AT_loc (die, DW_AT_vtable_elem_location,
10507 new_loc_descr (DW_OP_constu,
10508 tree_low_cst (DECL_VINDEX (func_decl), 0),
10509 0));
10511 /* GNU extension: Record what type this method came from originally. */
10512 if (debug_info_level > DINFO_LEVEL_TERSE)
10513 add_AT_die_ref (die, DW_AT_containing_type,
10514 lookup_type_die (DECL_CONTEXT (func_decl)));
10518 /* Add source coordinate attributes for the given decl. */
10520 static void
10521 add_src_coords_attributes (dw_die_ref die, tree decl)
10523 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
10524 unsigned file_index = lookup_filename (s.file);
10526 add_AT_unsigned (die, DW_AT_decl_file, file_index);
10527 add_AT_unsigned (die, DW_AT_decl_line, s.line);
10530 /* Add a DW_AT_name attribute and source coordinate attribute for the
10531 given decl, but only if it actually has a name. */
10533 static void
10534 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
10536 tree decl_name;
10538 decl_name = DECL_NAME (decl);
10539 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
10541 add_name_attribute (die, dwarf2_name (decl, 0));
10542 if (! DECL_ARTIFICIAL (decl))
10543 add_src_coords_attributes (die, decl);
10545 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
10546 && TREE_PUBLIC (decl)
10547 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
10548 && !DECL_ABSTRACT (decl))
10549 add_AT_string (die, DW_AT_MIPS_linkage_name,
10550 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
10553 #ifdef VMS_DEBUGGING_INFO
10554 /* Get the function's name, as described by its RTL. This may be different
10555 from the DECL_NAME name used in the source file. */
10556 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
10558 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
10559 XEXP (DECL_RTL (decl), 0));
10560 VARRAY_PUSH_RTX (used_rtx_varray, XEXP (DECL_RTL (decl), 0));
10562 #endif
10565 /* Push a new declaration scope. */
10567 static void
10568 push_decl_scope (tree scope)
10570 VARRAY_PUSH_TREE (decl_scope_table, scope);
10573 /* Pop a declaration scope. */
10575 static inline void
10576 pop_decl_scope (void)
10578 gcc_assert (VARRAY_ACTIVE_SIZE (decl_scope_table) > 0);
10580 VARRAY_POP (decl_scope_table);
10583 /* Return the DIE for the scope that immediately contains this type.
10584 Non-named types get global scope. Named types nested in other
10585 types get their containing scope if it's open, or global scope
10586 otherwise. All other types (i.e. function-local named types) get
10587 the current active scope. */
10589 static dw_die_ref
10590 scope_die_for (tree t, dw_die_ref context_die)
10592 dw_die_ref scope_die = NULL;
10593 tree containing_scope;
10594 int i;
10596 /* Non-types always go in the current scope. */
10597 gcc_assert (TYPE_P (t));
10599 containing_scope = TYPE_CONTEXT (t);
10601 /* Use the containing namespace if it was passed in (for a declaration). */
10602 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
10604 if (context_die == lookup_decl_die (containing_scope))
10605 /* OK */;
10606 else
10607 containing_scope = NULL_TREE;
10610 /* Ignore function type "scopes" from the C frontend. They mean that
10611 a tagged type is local to a parmlist of a function declarator, but
10612 that isn't useful to DWARF. */
10613 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
10614 containing_scope = NULL_TREE;
10616 if (containing_scope == NULL_TREE)
10617 scope_die = comp_unit_die;
10618 else if (TYPE_P (containing_scope))
10620 /* For types, we can just look up the appropriate DIE. But
10621 first we check to see if we're in the middle of emitting it
10622 so we know where the new DIE should go. */
10623 for (i = VARRAY_ACTIVE_SIZE (decl_scope_table) - 1; i >= 0; --i)
10624 if (VARRAY_TREE (decl_scope_table, i) == containing_scope)
10625 break;
10627 if (i < 0)
10629 gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
10630 || TREE_ASM_WRITTEN (containing_scope));
10632 /* If none of the current dies are suitable, we get file scope. */
10633 scope_die = comp_unit_die;
10635 else
10636 scope_die = lookup_type_die (containing_scope);
10638 else
10639 scope_die = context_die;
10641 return scope_die;
10644 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
10646 static inline int
10647 local_scope_p (dw_die_ref context_die)
10649 for (; context_die; context_die = context_die->die_parent)
10650 if (context_die->die_tag == DW_TAG_inlined_subroutine
10651 || context_die->die_tag == DW_TAG_subprogram)
10652 return 1;
10654 return 0;
10657 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
10658 whether or not to treat a DIE in this context as a declaration. */
10660 static inline int
10661 class_or_namespace_scope_p (dw_die_ref context_die)
10663 return (context_die
10664 && (context_die->die_tag == DW_TAG_structure_type
10665 || context_die->die_tag == DW_TAG_union_type
10666 || context_die->die_tag == DW_TAG_namespace));
10669 /* Many forms of DIEs require a "type description" attribute. This
10670 routine locates the proper "type descriptor" die for the type given
10671 by 'type', and adds a DW_AT_type attribute below the given die. */
10673 static void
10674 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
10675 int decl_volatile, dw_die_ref context_die)
10677 enum tree_code code = TREE_CODE (type);
10678 dw_die_ref type_die = NULL;
10680 /* ??? If this type is an unnamed subrange type of an integral or
10681 floating-point type, use the inner type. This is because we have no
10682 support for unnamed types in base_type_die. This can happen if this is
10683 an Ada subrange type. Correct solution is emit a subrange type die. */
10684 if ((code == INTEGER_TYPE || code == REAL_TYPE)
10685 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
10686 type = TREE_TYPE (type), code = TREE_CODE (type);
10688 if (code == ERROR_MARK
10689 /* Handle a special case. For functions whose return type is void, we
10690 generate *no* type attribute. (Note that no object may have type
10691 `void', so this only applies to function return types). */
10692 || code == VOID_TYPE)
10693 return;
10695 type_die = modified_type_die (type,
10696 decl_const || TYPE_READONLY (type),
10697 decl_volatile || TYPE_VOLATILE (type),
10698 context_die);
10700 if (type_die != NULL)
10701 add_AT_die_ref (object_die, DW_AT_type, type_die);
10704 /* Given an object die, add the calling convention attribute for the
10705 function call type. */
10706 static void
10707 add_calling_convention_attribute (dw_die_ref subr_die, tree type)
10709 enum dwarf_calling_convention value = DW_CC_normal;
10711 value = targetm.dwarf_calling_convention (type);
10713 /* Only add the attribute if the backend requests it, and
10714 is not DW_CC_normal. */
10715 if (value && (value != DW_CC_normal))
10716 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
10719 /* Given a tree pointer to a struct, class, union, or enum type node, return
10720 a pointer to the (string) tag name for the given type, or zero if the type
10721 was declared without a tag. */
10723 static const char *
10724 type_tag (tree type)
10726 const char *name = 0;
10728 if (TYPE_NAME (type) != 0)
10730 tree t = 0;
10732 /* Find the IDENTIFIER_NODE for the type name. */
10733 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
10734 t = TYPE_NAME (type);
10736 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
10737 a TYPE_DECL node, regardless of whether or not a `typedef' was
10738 involved. */
10739 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10740 && ! DECL_IGNORED_P (TYPE_NAME (type)))
10741 t = DECL_NAME (TYPE_NAME (type));
10743 /* Now get the name as a string, or invent one. */
10744 if (t != 0)
10745 name = IDENTIFIER_POINTER (t);
10748 return (name == 0 || *name == '\0') ? 0 : name;
10751 /* Return the type associated with a data member, make a special check
10752 for bit field types. */
10754 static inline tree
10755 member_declared_type (tree member)
10757 return (DECL_BIT_FIELD_TYPE (member)
10758 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
10761 /* Get the decl's label, as described by its RTL. This may be different
10762 from the DECL_NAME name used in the source file. */
10764 #if 0
10765 static const char *
10766 decl_start_label (tree decl)
10768 rtx x;
10769 const char *fnname;
10771 x = DECL_RTL (decl);
10772 gcc_assert (MEM_P (x));
10774 x = XEXP (x, 0);
10775 gcc_assert (GET_CODE (x) == SYMBOL_REF);
10777 fnname = XSTR (x, 0);
10778 return fnname;
10780 #endif
10782 /* These routines generate the internal representation of the DIE's for
10783 the compilation unit. Debugging information is collected by walking
10784 the declaration trees passed in from dwarf2out_decl(). */
10786 static void
10787 gen_array_type_die (tree type, dw_die_ref context_die)
10789 dw_die_ref scope_die = scope_die_for (type, context_die);
10790 dw_die_ref array_die;
10791 tree element_type;
10793 /* ??? The SGI dwarf reader fails for array of array of enum types unless
10794 the inner array type comes before the outer array type. Thus we must
10795 call gen_type_die before we call new_die. See below also. */
10796 #ifdef MIPS_DEBUGGING_INFO
10797 gen_type_die (TREE_TYPE (type), context_die);
10798 #endif
10800 array_die = new_die (DW_TAG_array_type, scope_die, type);
10801 add_name_attribute (array_die, type_tag (type));
10802 equate_type_number_to_die (type, array_die);
10804 if (TREE_CODE (type) == VECTOR_TYPE)
10806 /* The frontend feeds us a representation for the vector as a struct
10807 containing an array. Pull out the array type. */
10808 type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
10809 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
10812 #if 0
10813 /* We default the array ordering. SDB will probably do
10814 the right things even if DW_AT_ordering is not present. It's not even
10815 an issue until we start to get into multidimensional arrays anyway. If
10816 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
10817 then we'll have to put the DW_AT_ordering attribute back in. (But if
10818 and when we find out that we need to put these in, we will only do so
10819 for multidimensional arrays. */
10820 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
10821 #endif
10823 #ifdef MIPS_DEBUGGING_INFO
10824 /* The SGI compilers handle arrays of unknown bound by setting
10825 AT_declaration and not emitting any subrange DIEs. */
10826 if (! TYPE_DOMAIN (type))
10827 add_AT_flag (array_die, DW_AT_declaration, 1);
10828 else
10829 #endif
10830 add_subscript_info (array_die, type);
10832 /* Add representation of the type of the elements of this array type. */
10833 element_type = TREE_TYPE (type);
10835 /* ??? The SGI dwarf reader fails for multidimensional arrays with a
10836 const enum type. E.g. const enum machine_mode insn_operand_mode[2][10].
10837 We work around this by disabling this feature. See also
10838 add_subscript_info. */
10839 #ifndef MIPS_DEBUGGING_INFO
10840 while (TREE_CODE (element_type) == ARRAY_TYPE)
10841 element_type = TREE_TYPE (element_type);
10843 gen_type_die (element_type, context_die);
10844 #endif
10846 add_type_attribute (array_die, element_type, 0, 0, context_die);
10849 #if 0
10850 static void
10851 gen_entry_point_die (tree decl, dw_die_ref context_die)
10853 tree origin = decl_ultimate_origin (decl);
10854 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
10856 if (origin != NULL)
10857 add_abstract_origin_attribute (decl_die, origin);
10858 else
10860 add_name_and_src_coords_attributes (decl_die, decl);
10861 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
10862 0, 0, context_die);
10865 if (DECL_ABSTRACT (decl))
10866 equate_decl_number_to_die (decl, decl_die);
10867 else
10868 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
10870 #endif
10872 /* Walk through the list of incomplete types again, trying once more to
10873 emit full debugging info for them. */
10875 static void
10876 retry_incomplete_types (void)
10878 int i;
10880 for (i = VARRAY_ACTIVE_SIZE (incomplete_types) - 1; i >= 0; i--)
10881 gen_type_die (VARRAY_TREE (incomplete_types, i), comp_unit_die);
10884 /* Generate a DIE to represent an inlined instance of an enumeration type. */
10886 static void
10887 gen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
10889 dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
10891 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
10892 be incomplete and such types are not marked. */
10893 add_abstract_origin_attribute (type_die, type);
10896 /* Generate a DIE to represent an inlined instance of a structure type. */
10898 static void
10899 gen_inlined_structure_type_die (tree type, dw_die_ref context_die)
10901 dw_die_ref type_die = new_die (DW_TAG_structure_type, context_die, type);
10903 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
10904 be incomplete and such types are not marked. */
10905 add_abstract_origin_attribute (type_die, type);
10908 /* Generate a DIE to represent an inlined instance of a union type. */
10910 static void
10911 gen_inlined_union_type_die (tree type, dw_die_ref context_die)
10913 dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
10915 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
10916 be incomplete and such types are not marked. */
10917 add_abstract_origin_attribute (type_die, type);
10920 /* Generate a DIE to represent an enumeration type. Note that these DIEs
10921 include all of the information about the enumeration values also. Each
10922 enumerated type name/value is listed as a child of the enumerated type
10923 DIE. */
10925 static dw_die_ref
10926 gen_enumeration_type_die (tree type, dw_die_ref context_die)
10928 dw_die_ref type_die = lookup_type_die (type);
10930 if (type_die == NULL)
10932 type_die = new_die (DW_TAG_enumeration_type,
10933 scope_die_for (type, context_die), type);
10934 equate_type_number_to_die (type, type_die);
10935 add_name_attribute (type_die, type_tag (type));
10937 else if (! TYPE_SIZE (type))
10938 return type_die;
10939 else
10940 remove_AT (type_die, DW_AT_declaration);
10942 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
10943 given enum type is incomplete, do not generate the DW_AT_byte_size
10944 attribute or the DW_AT_element_list attribute. */
10945 if (TYPE_SIZE (type))
10947 tree link;
10949 TREE_ASM_WRITTEN (type) = 1;
10950 add_byte_size_attribute (type_die, type);
10951 if (TYPE_STUB_DECL (type) != NULL_TREE)
10952 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
10954 /* If the first reference to this type was as the return type of an
10955 inline function, then it may not have a parent. Fix this now. */
10956 if (type_die->die_parent == NULL)
10957 add_child_die (scope_die_for (type, context_die), type_die);
10959 for (link = TYPE_VALUES (type);
10960 link != NULL; link = TREE_CHAIN (link))
10962 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
10963 tree value = TREE_VALUE (link);
10965 add_name_attribute (enum_die,
10966 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
10968 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
10969 /* DWARF2 does not provide a way of indicating whether or
10970 not enumeration constants are signed or unsigned. GDB
10971 always assumes the values are signed, so we output all
10972 values as if they were signed. That means that
10973 enumeration constants with very large unsigned values
10974 will appear to have negative values in the debugger. */
10975 add_AT_int (enum_die, DW_AT_const_value,
10976 tree_low_cst (value, tree_int_cst_sgn (value) > 0));
10979 else
10980 add_AT_flag (type_die, DW_AT_declaration, 1);
10982 return type_die;
10985 /* Generate a DIE to represent either a real live formal parameter decl or to
10986 represent just the type of some formal parameter position in some function
10987 type.
10989 Note that this routine is a bit unusual because its argument may be a
10990 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
10991 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
10992 node. If it's the former then this function is being called to output a
10993 DIE to represent a formal parameter object (or some inlining thereof). If
10994 it's the latter, then this function is only being called to output a
10995 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
10996 argument type of some subprogram type. */
10998 static dw_die_ref
10999 gen_formal_parameter_die (tree node, dw_die_ref context_die)
11001 dw_die_ref parm_die
11002 = new_die (DW_TAG_formal_parameter, context_die, node);
11003 tree origin;
11005 switch (TREE_CODE_CLASS (TREE_CODE (node)))
11007 case tcc_declaration:
11008 origin = decl_ultimate_origin (node);
11009 if (origin != NULL)
11010 add_abstract_origin_attribute (parm_die, origin);
11011 else
11013 add_name_and_src_coords_attributes (parm_die, node);
11014 add_type_attribute (parm_die, TREE_TYPE (node),
11015 TREE_READONLY (node),
11016 TREE_THIS_VOLATILE (node),
11017 context_die);
11018 if (DECL_ARTIFICIAL (node))
11019 add_AT_flag (parm_die, DW_AT_artificial, 1);
11022 equate_decl_number_to_die (node, parm_die);
11023 if (! DECL_ABSTRACT (node))
11024 add_location_or_const_value_attribute (parm_die, node, DW_AT_location);
11026 break;
11028 case tcc_type:
11029 /* We were called with some kind of a ..._TYPE node. */
11030 add_type_attribute (parm_die, node, 0, 0, context_die);
11031 break;
11033 default:
11034 gcc_unreachable ();
11037 return parm_die;
11040 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
11041 at the end of an (ANSI prototyped) formal parameters list. */
11043 static void
11044 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
11046 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
11049 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
11050 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
11051 parameters as specified in some function type specification (except for
11052 those which appear as part of a function *definition*). */
11054 static void
11055 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
11057 tree link;
11058 tree formal_type = NULL;
11059 tree first_parm_type;
11060 tree arg;
11062 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
11064 arg = DECL_ARGUMENTS (function_or_method_type);
11065 function_or_method_type = TREE_TYPE (function_or_method_type);
11067 else
11068 arg = NULL_TREE;
11070 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
11072 /* Make our first pass over the list of formal parameter types and output a
11073 DW_TAG_formal_parameter DIE for each one. */
11074 for (link = first_parm_type; link; )
11076 dw_die_ref parm_die;
11078 formal_type = TREE_VALUE (link);
11079 if (formal_type == void_type_node)
11080 break;
11082 /* Output a (nameless) DIE to represent the formal parameter itself. */
11083 parm_die = gen_formal_parameter_die (formal_type, context_die);
11084 if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
11085 && link == first_parm_type)
11086 || (arg && DECL_ARTIFICIAL (arg)))
11087 add_AT_flag (parm_die, DW_AT_artificial, 1);
11089 link = TREE_CHAIN (link);
11090 if (arg)
11091 arg = TREE_CHAIN (arg);
11094 /* If this function type has an ellipsis, add a
11095 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
11096 if (formal_type != void_type_node)
11097 gen_unspecified_parameters_die (function_or_method_type, context_die);
11099 /* Make our second (and final) pass over the list of formal parameter types
11100 and output DIEs to represent those types (as necessary). */
11101 for (link = TYPE_ARG_TYPES (function_or_method_type);
11102 link && TREE_VALUE (link);
11103 link = TREE_CHAIN (link))
11104 gen_type_die (TREE_VALUE (link), context_die);
11107 /* We want to generate the DIE for TYPE so that we can generate the
11108 die for MEMBER, which has been defined; we will need to refer back
11109 to the member declaration nested within TYPE. If we're trying to
11110 generate minimal debug info for TYPE, processing TYPE won't do the
11111 trick; we need to attach the member declaration by hand. */
11113 static void
11114 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
11116 gen_type_die (type, context_die);
11118 /* If we're trying to avoid duplicate debug info, we may not have
11119 emitted the member decl for this function. Emit it now. */
11120 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
11121 && ! lookup_decl_die (member))
11123 gcc_assert (!decl_ultimate_origin (member));
11125 push_decl_scope (type);
11126 if (TREE_CODE (member) == FUNCTION_DECL)
11127 gen_subprogram_die (member, lookup_type_die (type));
11128 else
11129 gen_variable_die (member, lookup_type_die (type));
11131 pop_decl_scope ();
11135 /* Generate the DWARF2 info for the "abstract" instance of a function which we
11136 may later generate inlined and/or out-of-line instances of. */
11138 static void
11139 dwarf2out_abstract_function (tree decl)
11141 dw_die_ref old_die;
11142 tree save_fn;
11143 tree context;
11144 int was_abstract = DECL_ABSTRACT (decl);
11146 /* Make sure we have the actual abstract inline, not a clone. */
11147 decl = DECL_ORIGIN (decl);
11149 old_die = lookup_decl_die (decl);
11150 if (old_die && get_AT (old_die, DW_AT_inline))
11151 /* We've already generated the abstract instance. */
11152 return;
11154 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
11155 we don't get confused by DECL_ABSTRACT. */
11156 if (debug_info_level > DINFO_LEVEL_TERSE)
11158 context = decl_class_context (decl);
11159 if (context)
11160 gen_type_die_for_member
11161 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
11164 /* Pretend we've just finished compiling this function. */
11165 save_fn = current_function_decl;
11166 current_function_decl = decl;
11168 set_decl_abstract_flags (decl, 1);
11169 dwarf2out_decl (decl);
11170 if (! was_abstract)
11171 set_decl_abstract_flags (decl, 0);
11173 current_function_decl = save_fn;
11176 /* Generate a DIE to represent a declared function (either file-scope or
11177 block-local). */
11179 static void
11180 gen_subprogram_die (tree decl, dw_die_ref context_die)
11182 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
11183 tree origin = decl_ultimate_origin (decl);
11184 dw_die_ref subr_die;
11185 rtx fp_reg;
11186 tree fn_arg_types;
11187 tree outer_scope;
11188 dw_die_ref old_die = lookup_decl_die (decl);
11189 int declaration = (current_function_decl != decl
11190 || class_or_namespace_scope_p (context_die));
11192 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
11193 started to generate the abstract instance of an inline, decided to output
11194 its containing class, and proceeded to emit the declaration of the inline
11195 from the member list for the class. If so, DECLARATION takes priority;
11196 we'll get back to the abstract instance when done with the class. */
11198 /* The class-scope declaration DIE must be the primary DIE. */
11199 if (origin && declaration && class_or_namespace_scope_p (context_die))
11201 origin = NULL;
11202 gcc_assert (!old_die);
11205 if (origin != NULL)
11207 gcc_assert (!declaration || local_scope_p (context_die));
11209 /* Fixup die_parent for the abstract instance of a nested
11210 inline function. */
11211 if (old_die && old_die->die_parent == NULL)
11212 add_child_die (context_die, old_die);
11214 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11215 add_abstract_origin_attribute (subr_die, origin);
11217 else if (old_die)
11219 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11220 unsigned file_index = lookup_filename (s.file);
11222 if (!get_AT_flag (old_die, DW_AT_declaration)
11223 /* We can have a normal definition following an inline one in the
11224 case of redefinition of GNU C extern inlines.
11225 It seems reasonable to use AT_specification in this case. */
11226 && !get_AT (old_die, DW_AT_inline))
11228 /* Detect and ignore this case, where we are trying to output
11229 something we have already output. */
11230 return;
11233 /* If the definition comes from the same place as the declaration,
11234 maybe use the old DIE. We always want the DIE for this function
11235 that has the *_pc attributes to be under comp_unit_die so the
11236 debugger can find it. We also need to do this for abstract
11237 instances of inlines, since the spec requires the out-of-line copy
11238 to have the same parent. For local class methods, this doesn't
11239 apply; we just use the old DIE. */
11240 if ((old_die->die_parent == comp_unit_die || context_die == NULL)
11241 && (DECL_ARTIFICIAL (decl)
11242 || (get_AT_unsigned (old_die, DW_AT_decl_file) == file_index
11243 && (get_AT_unsigned (old_die, DW_AT_decl_line)
11244 == (unsigned) s.line))))
11246 subr_die = old_die;
11248 /* Clear out the declaration attribute and the formal parameters.
11249 Do not remove all children, because it is possible that this
11250 declaration die was forced using force_decl_die(). In such
11251 cases die that forced declaration die (e.g. TAG_imported_module)
11252 is one of the children that we do not want to remove. */
11253 remove_AT (subr_die, DW_AT_declaration);
11254 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
11256 else
11258 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11259 add_AT_specification (subr_die, old_die);
11260 if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
11261 add_AT_unsigned (subr_die, DW_AT_decl_file, file_index);
11262 if (get_AT_unsigned (old_die, DW_AT_decl_line)
11263 != (unsigned) s.line)
11264 add_AT_unsigned
11265 (subr_die, DW_AT_decl_line, s.line);
11268 else
11270 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11272 if (TREE_PUBLIC (decl))
11273 add_AT_flag (subr_die, DW_AT_external, 1);
11275 add_name_and_src_coords_attributes (subr_die, decl);
11276 if (debug_info_level > DINFO_LEVEL_TERSE)
11278 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
11279 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
11280 0, 0, context_die);
11283 add_pure_or_virtual_attribute (subr_die, decl);
11284 if (DECL_ARTIFICIAL (decl))
11285 add_AT_flag (subr_die, DW_AT_artificial, 1);
11287 if (TREE_PROTECTED (decl))
11288 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
11289 else if (TREE_PRIVATE (decl))
11290 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
11293 if (declaration)
11295 if (!old_die || !get_AT (old_die, DW_AT_inline))
11297 add_AT_flag (subr_die, DW_AT_declaration, 1);
11299 /* The first time we see a member function, it is in the context of
11300 the class to which it belongs. We make sure of this by emitting
11301 the class first. The next time is the definition, which is
11302 handled above. The two may come from the same source text.
11304 Note that force_decl_die() forces function declaration die. It is
11305 later reused to represent definition. */
11306 equate_decl_number_to_die (decl, subr_die);
11309 else if (DECL_ABSTRACT (decl))
11311 if (DECL_DECLARED_INLINE_P (decl))
11313 if (cgraph_function_possibly_inlined_p (decl))
11314 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
11315 else
11316 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
11318 else
11320 if (cgraph_function_possibly_inlined_p (decl))
11321 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
11322 else
11323 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
11326 equate_decl_number_to_die (decl, subr_die);
11328 else if (!DECL_EXTERNAL (decl))
11330 if (!old_die || !get_AT (old_die, DW_AT_inline))
11331 equate_decl_number_to_die (decl, subr_die);
11333 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
11334 current_function_funcdef_no);
11335 add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
11336 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
11337 current_function_funcdef_no);
11338 add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
11340 add_pubname (decl, subr_die);
11341 add_arange (decl, subr_die);
11343 #ifdef MIPS_DEBUGGING_INFO
11344 /* Add a reference to the FDE for this routine. */
11345 add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
11346 #endif
11348 /* Define the "frame base" location for this routine. We use the
11349 frame pointer or stack pointer registers, since the RTL for local
11350 variables is relative to one of them. */
11351 if (frame_base_decl && lookup_decl_loc (frame_base_decl) != NULL)
11353 add_location_or_const_value_attribute (subr_die, frame_base_decl,
11354 DW_AT_frame_base);
11356 else
11358 fp_reg
11359 = frame_pointer_needed ? hard_frame_pointer_rtx : stack_pointer_rtx;
11360 add_AT_loc (subr_die, DW_AT_frame_base, reg_loc_descriptor (fp_reg));
11363 if (cfun->static_chain_decl)
11364 add_AT_location_description (subr_die, DW_AT_static_link,
11365 loc_descriptor_from_tree (cfun->static_chain_decl));
11368 /* Now output descriptions of the arguments for this function. This gets
11369 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
11370 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
11371 `...' at the end of the formal parameter list. In order to find out if
11372 there was a trailing ellipsis or not, we must instead look at the type
11373 associated with the FUNCTION_DECL. This will be a node of type
11374 FUNCTION_TYPE. If the chain of type nodes hanging off of this
11375 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
11376 an ellipsis at the end. */
11378 /* In the case where we are describing a mere function declaration, all we
11379 need to do here (and all we *can* do here) is to describe the *types* of
11380 its formal parameters. */
11381 if (debug_info_level <= DINFO_LEVEL_TERSE)
11383 else if (declaration)
11384 gen_formal_types_die (decl, subr_die);
11385 else
11387 /* Generate DIEs to represent all known formal parameters. */
11388 tree arg_decls = DECL_ARGUMENTS (decl);
11389 tree parm;
11391 /* When generating DIEs, generate the unspecified_parameters DIE
11392 instead if we come across the arg "__builtin_va_alist" */
11393 for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
11394 if (TREE_CODE (parm) == PARM_DECL)
11396 if (DECL_NAME (parm)
11397 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
11398 "__builtin_va_alist"))
11399 gen_unspecified_parameters_die (parm, subr_die);
11400 else
11401 gen_decl_die (parm, subr_die);
11404 /* Decide whether we need an unspecified_parameters DIE at the end.
11405 There are 2 more cases to do this for: 1) the ansi ... declaration -
11406 this is detectable when the end of the arg list is not a
11407 void_type_node 2) an unprototyped function declaration (not a
11408 definition). This just means that we have no info about the
11409 parameters at all. */
11410 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
11411 if (fn_arg_types != NULL)
11413 /* This is the prototyped case, check for.... */
11414 if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
11415 gen_unspecified_parameters_die (decl, subr_die);
11417 else if (DECL_INITIAL (decl) == NULL_TREE)
11418 gen_unspecified_parameters_die (decl, subr_die);
11421 /* Output Dwarf info for all of the stuff within the body of the function
11422 (if it has one - it may be just a declaration). */
11423 outer_scope = DECL_INITIAL (decl);
11425 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
11426 a function. This BLOCK actually represents the outermost binding contour
11427 for the function, i.e. the contour in which the function's formal
11428 parameters and labels get declared. Curiously, it appears that the front
11429 end doesn't actually put the PARM_DECL nodes for the current function onto
11430 the BLOCK_VARS list for this outer scope, but are strung off of the
11431 DECL_ARGUMENTS list for the function instead.
11433 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
11434 the LABEL_DECL nodes for the function however, and we output DWARF info
11435 for those in decls_for_scope. Just within the `outer_scope' there will be
11436 a BLOCK node representing the function's outermost pair of curly braces,
11437 and any blocks used for the base and member initializers of a C++
11438 constructor function. */
11439 if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
11441 /* Emit a DW_TAG_variable DIE for a named return value. */
11442 if (DECL_NAME (DECL_RESULT (decl)))
11443 gen_decl_die (DECL_RESULT (decl), subr_die);
11445 current_function_has_inlines = 0;
11446 decls_for_scope (outer_scope, subr_die, 0);
11448 #if 0 && defined (MIPS_DEBUGGING_INFO)
11449 if (current_function_has_inlines)
11451 add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
11452 if (! comp_unit_has_inlines)
11454 add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
11455 comp_unit_has_inlines = 1;
11458 #endif
11460 /* Add the calling convention attribute if requested. */
11461 add_calling_convention_attribute (subr_die, TREE_TYPE (decl));
11465 /* Generate a DIE to represent a declared data object. */
11467 static void
11468 gen_variable_die (tree decl, dw_die_ref context_die)
11470 tree origin = decl_ultimate_origin (decl);
11471 dw_die_ref var_die = new_die (DW_TAG_variable, context_die, decl);
11473 dw_die_ref old_die = lookup_decl_die (decl);
11474 int declaration = (DECL_EXTERNAL (decl)
11475 || class_or_namespace_scope_p (context_die));
11477 if (origin != NULL)
11478 add_abstract_origin_attribute (var_die, origin);
11480 /* Loop unrolling can create multiple blocks that refer to the same
11481 static variable, so we must test for the DW_AT_declaration flag.
11483 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
11484 copy decls and set the DECL_ABSTRACT flag on them instead of
11485 sharing them.
11487 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
11488 else if (old_die && TREE_STATIC (decl)
11489 && get_AT_flag (old_die, DW_AT_declaration) == 1)
11491 /* This is a definition of a C++ class level static. */
11492 add_AT_specification (var_die, old_die);
11493 if (DECL_NAME (decl))
11495 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11496 unsigned file_index = lookup_filename (s.file);
11498 if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
11499 add_AT_unsigned (var_die, DW_AT_decl_file, file_index);
11501 if (get_AT_unsigned (old_die, DW_AT_decl_line)
11502 != (unsigned) s.line)
11504 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
11507 else
11509 add_name_and_src_coords_attributes (var_die, decl);
11510 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
11511 TREE_THIS_VOLATILE (decl), context_die);
11513 if (TREE_PUBLIC (decl))
11514 add_AT_flag (var_die, DW_AT_external, 1);
11516 if (DECL_ARTIFICIAL (decl))
11517 add_AT_flag (var_die, DW_AT_artificial, 1);
11519 if (TREE_PROTECTED (decl))
11520 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
11521 else if (TREE_PRIVATE (decl))
11522 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
11525 if (declaration)
11526 add_AT_flag (var_die, DW_AT_declaration, 1);
11528 if (DECL_ABSTRACT (decl) || declaration)
11529 equate_decl_number_to_die (decl, var_die);
11531 if (! declaration && ! DECL_ABSTRACT (decl))
11533 add_location_or_const_value_attribute (var_die, decl, DW_AT_location);
11534 add_pubname (decl, var_die);
11536 else
11537 tree_add_const_value_attribute (var_die, decl);
11540 /* Generate a DIE to represent a label identifier. */
11542 static void
11543 gen_label_die (tree decl, dw_die_ref context_die)
11545 tree origin = decl_ultimate_origin (decl);
11546 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
11547 rtx insn;
11548 char label[MAX_ARTIFICIAL_LABEL_BYTES];
11550 if (origin != NULL)
11551 add_abstract_origin_attribute (lbl_die, origin);
11552 else
11553 add_name_and_src_coords_attributes (lbl_die, decl);
11555 if (DECL_ABSTRACT (decl))
11556 equate_decl_number_to_die (decl, lbl_die);
11557 else
11559 insn = DECL_RTL_IF_SET (decl);
11561 /* Deleted labels are programmer specified labels which have been
11562 eliminated because of various optimizations. We still emit them
11563 here so that it is possible to put breakpoints on them. */
11564 if (insn
11565 && (LABEL_P (insn)
11566 || ((NOTE_P (insn)
11567 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED_LABEL))))
11569 /* When optimization is enabled (via -O) some parts of the compiler
11570 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
11571 represent source-level labels which were explicitly declared by
11572 the user. This really shouldn't be happening though, so catch
11573 it if it ever does happen. */
11574 gcc_assert (!INSN_DELETED_P (insn));
11576 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
11577 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
11582 /* Generate a DIE for a lexical block. */
11584 static void
11585 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
11587 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
11588 char label[MAX_ARTIFICIAL_LABEL_BYTES];
11590 if (! BLOCK_ABSTRACT (stmt))
11592 if (BLOCK_FRAGMENT_CHAIN (stmt))
11594 tree chain;
11596 add_AT_range_list (stmt_die, DW_AT_ranges, add_ranges (stmt));
11598 chain = BLOCK_FRAGMENT_CHAIN (stmt);
11601 add_ranges (chain);
11602 chain = BLOCK_FRAGMENT_CHAIN (chain);
11604 while (chain);
11605 add_ranges (NULL);
11607 else
11609 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
11610 BLOCK_NUMBER (stmt));
11611 add_AT_lbl_id (stmt_die, DW_AT_low_pc, label);
11612 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
11613 BLOCK_NUMBER (stmt));
11614 add_AT_lbl_id (stmt_die, DW_AT_high_pc, label);
11618 decls_for_scope (stmt, stmt_die, depth);
11621 /* Generate a DIE for an inlined subprogram. */
11623 static void
11624 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
11626 tree decl = block_ultimate_origin (stmt);
11628 /* Emit info for the abstract instance first, if we haven't yet. We
11629 must emit this even if the block is abstract, otherwise when we
11630 emit the block below (or elsewhere), we may end up trying to emit
11631 a die whose origin die hasn't been emitted, and crashing. */
11632 dwarf2out_abstract_function (decl);
11634 if (! BLOCK_ABSTRACT (stmt))
11636 dw_die_ref subr_die
11637 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
11638 char label[MAX_ARTIFICIAL_LABEL_BYTES];
11640 add_abstract_origin_attribute (subr_die, decl);
11641 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
11642 BLOCK_NUMBER (stmt));
11643 add_AT_lbl_id (subr_die, DW_AT_low_pc, label);
11644 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
11645 BLOCK_NUMBER (stmt));
11646 add_AT_lbl_id (subr_die, DW_AT_high_pc, label);
11647 decls_for_scope (stmt, subr_die, depth);
11648 current_function_has_inlines = 1;
11650 else
11651 /* We may get here if we're the outer block of function A that was
11652 inlined into function B that was inlined into function C. When
11653 generating debugging info for C, dwarf2out_abstract_function(B)
11654 would mark all inlined blocks as abstract, including this one.
11655 So, we wouldn't (and shouldn't) expect labels to be generated
11656 for this one. Instead, just emit debugging info for
11657 declarations within the block. This is particularly important
11658 in the case of initializers of arguments passed from B to us:
11659 if they're statement expressions containing declarations, we
11660 wouldn't generate dies for their abstract variables, and then,
11661 when generating dies for the real variables, we'd die (pun
11662 intended :-) */
11663 gen_lexical_block_die (stmt, context_die, depth);
11666 /* Generate a DIE for a field in a record, or structure. */
11668 static void
11669 gen_field_die (tree decl, dw_die_ref context_die)
11671 dw_die_ref decl_die;
11673 if (TREE_TYPE (decl) == error_mark_node)
11674 return;
11676 decl_die = new_die (DW_TAG_member, context_die, decl);
11677 add_name_and_src_coords_attributes (decl_die, decl);
11678 add_type_attribute (decl_die, member_declared_type (decl),
11679 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
11680 context_die);
11682 if (DECL_BIT_FIELD_TYPE (decl))
11684 add_byte_size_attribute (decl_die, decl);
11685 add_bit_size_attribute (decl_die, decl);
11686 add_bit_offset_attribute (decl_die, decl);
11689 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
11690 add_data_member_location_attribute (decl_die, decl);
11692 if (DECL_ARTIFICIAL (decl))
11693 add_AT_flag (decl_die, DW_AT_artificial, 1);
11695 if (TREE_PROTECTED (decl))
11696 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
11697 else if (TREE_PRIVATE (decl))
11698 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
11700 /* Equate decl number to die, so that we can look up this decl later on. */
11701 equate_decl_number_to_die (decl, decl_die);
11704 #if 0
11705 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
11706 Use modified_type_die instead.
11707 We keep this code here just in case these types of DIEs may be needed to
11708 represent certain things in other languages (e.g. Pascal) someday. */
11710 static void
11711 gen_pointer_type_die (tree type, dw_die_ref context_die)
11713 dw_die_ref ptr_die
11714 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
11716 equate_type_number_to_die (type, ptr_die);
11717 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
11718 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
11721 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
11722 Use modified_type_die instead.
11723 We keep this code here just in case these types of DIEs may be needed to
11724 represent certain things in other languages (e.g. Pascal) someday. */
11726 static void
11727 gen_reference_type_die (tree type, dw_die_ref context_die)
11729 dw_die_ref ref_die
11730 = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
11732 equate_type_number_to_die (type, ref_die);
11733 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
11734 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
11736 #endif
11738 /* Generate a DIE for a pointer to a member type. */
11740 static void
11741 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
11743 dw_die_ref ptr_die
11744 = new_die (DW_TAG_ptr_to_member_type,
11745 scope_die_for (type, context_die), type);
11747 equate_type_number_to_die (type, ptr_die);
11748 add_AT_die_ref (ptr_die, DW_AT_containing_type,
11749 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
11750 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
11753 /* Generate the DIE for the compilation unit. */
11755 static dw_die_ref
11756 gen_compile_unit_die (const char *filename)
11758 dw_die_ref die;
11759 char producer[250];
11760 const char *language_string = lang_hooks.name;
11761 int language;
11763 die = new_die (DW_TAG_compile_unit, NULL, NULL);
11765 if (filename)
11767 add_name_attribute (die, filename);
11768 /* Don't add cwd for <built-in>. */
11769 if (filename[0] != DIR_SEPARATOR && filename[0] != '<')
11770 add_comp_dir_attribute (die);
11773 sprintf (producer, "%s %s", language_string, version_string);
11775 #ifdef MIPS_DEBUGGING_INFO
11776 /* The MIPS/SGI compilers place the 'cc' command line options in the producer
11777 string. The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
11778 not appear in the producer string, the debugger reaches the conclusion
11779 that the object file is stripped and has no debugging information.
11780 To get the MIPS/SGI debugger to believe that there is debugging
11781 information in the object file, we add a -g to the producer string. */
11782 if (debug_info_level > DINFO_LEVEL_TERSE)
11783 strcat (producer, " -g");
11784 #endif
11786 add_AT_string (die, DW_AT_producer, producer);
11788 if (strcmp (language_string, "GNU C++") == 0)
11789 language = DW_LANG_C_plus_plus;
11790 else if (strcmp (language_string, "GNU Ada") == 0)
11791 language = DW_LANG_Ada95;
11792 else if (strcmp (language_string, "GNU F77") == 0)
11793 language = DW_LANG_Fortran77;
11794 else if (strcmp (language_string, "GNU F95") == 0)
11795 language = DW_LANG_Fortran95;
11796 else if (strcmp (language_string, "GNU Pascal") == 0)
11797 language = DW_LANG_Pascal83;
11798 else if (strcmp (language_string, "GNU Java") == 0)
11799 language = DW_LANG_Java;
11800 else
11801 language = DW_LANG_C89;
11803 add_AT_unsigned (die, DW_AT_language, language);
11804 return die;
11807 /* Generate a DIE for a string type. */
11809 static void
11810 gen_string_type_die (tree type, dw_die_ref context_die)
11812 dw_die_ref type_die
11813 = new_die (DW_TAG_string_type, scope_die_for (type, context_die), type);
11815 equate_type_number_to_die (type, type_die);
11817 /* ??? Fudge the string length attribute for now.
11818 TODO: add string length info. */
11819 #if 0
11820 string_length_attribute (TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
11821 bound_representation (upper_bound, 0, 'u');
11822 #endif
11825 /* Generate the DIE for a base class. */
11827 static void
11828 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
11830 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
11832 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
11833 add_data_member_location_attribute (die, binfo);
11835 if (BINFO_VIRTUAL_P (binfo))
11836 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
11838 if (access == access_public_node)
11839 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
11840 else if (access == access_protected_node)
11841 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
11844 /* Generate a DIE for a class member. */
11846 static void
11847 gen_member_die (tree type, dw_die_ref context_die)
11849 tree member;
11850 tree binfo = TYPE_BINFO (type);
11851 dw_die_ref child;
11853 /* If this is not an incomplete type, output descriptions of each of its
11854 members. Note that as we output the DIEs necessary to represent the
11855 members of this record or union type, we will also be trying to output
11856 DIEs to represent the *types* of those members. However the `type'
11857 function (above) will specifically avoid generating type DIEs for member
11858 types *within* the list of member DIEs for this (containing) type except
11859 for those types (of members) which are explicitly marked as also being
11860 members of this (containing) type themselves. The g++ front- end can
11861 force any given type to be treated as a member of some other (containing)
11862 type by setting the TYPE_CONTEXT of the given (member) type to point to
11863 the TREE node representing the appropriate (containing) type. */
11865 /* First output info about the base classes. */
11866 if (binfo)
11868 VEC (tree) *accesses = BINFO_BASE_ACCESSES (binfo);
11869 int i;
11870 tree base;
11872 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
11873 gen_inheritance_die (base,
11874 (accesses ? VEC_index (tree, accesses, i)
11875 : access_public_node), context_die);
11878 /* Now output info about the data members and type members. */
11879 for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
11881 /* If we thought we were generating minimal debug info for TYPE
11882 and then changed our minds, some of the member declarations
11883 may have already been defined. Don't define them again, but
11884 do put them in the right order. */
11886 child = lookup_decl_die (member);
11887 if (child)
11888 splice_child_die (context_die, child);
11889 else
11890 gen_decl_die (member, context_die);
11893 /* Now output info about the function members (if any). */
11894 for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
11896 /* Don't include clones in the member list. */
11897 if (DECL_ABSTRACT_ORIGIN (member))
11898 continue;
11900 child = lookup_decl_die (member);
11901 if (child)
11902 splice_child_die (context_die, child);
11903 else
11904 gen_decl_die (member, context_die);
11908 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
11909 is set, we pretend that the type was never defined, so we only get the
11910 member DIEs needed by later specification DIEs. */
11912 static void
11913 gen_struct_or_union_type_die (tree type, dw_die_ref context_die)
11915 dw_die_ref type_die = lookup_type_die (type);
11916 dw_die_ref scope_die = 0;
11917 int nested = 0;
11918 int complete = (TYPE_SIZE (type)
11919 && (! TYPE_STUB_DECL (type)
11920 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
11921 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
11923 if (type_die && ! complete)
11924 return;
11926 if (TYPE_CONTEXT (type) != NULL_TREE
11927 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
11928 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
11929 nested = 1;
11931 scope_die = scope_die_for (type, context_die);
11933 if (! type_die || (nested && scope_die == comp_unit_die))
11934 /* First occurrence of type or toplevel definition of nested class. */
11936 dw_die_ref old_die = type_die;
11938 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
11939 ? DW_TAG_structure_type : DW_TAG_union_type,
11940 scope_die, type);
11941 equate_type_number_to_die (type, type_die);
11942 if (old_die)
11943 add_AT_specification (type_die, old_die);
11944 else
11945 add_name_attribute (type_die, type_tag (type));
11947 else
11948 remove_AT (type_die, DW_AT_declaration);
11950 /* If this type has been completed, then give it a byte_size attribute and
11951 then give a list of members. */
11952 if (complete && !ns_decl)
11954 /* Prevent infinite recursion in cases where the type of some member of
11955 this type is expressed in terms of this type itself. */
11956 TREE_ASM_WRITTEN (type) = 1;
11957 add_byte_size_attribute (type_die, type);
11958 if (TYPE_STUB_DECL (type) != NULL_TREE)
11959 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
11961 /* If the first reference to this type was as the return type of an
11962 inline function, then it may not have a parent. Fix this now. */
11963 if (type_die->die_parent == NULL)
11964 add_child_die (scope_die, type_die);
11966 push_decl_scope (type);
11967 gen_member_die (type, type_die);
11968 pop_decl_scope ();
11970 /* GNU extension: Record what type our vtable lives in. */
11971 if (TYPE_VFIELD (type))
11973 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
11975 gen_type_die (vtype, context_die);
11976 add_AT_die_ref (type_die, DW_AT_containing_type,
11977 lookup_type_die (vtype));
11980 else
11982 add_AT_flag (type_die, DW_AT_declaration, 1);
11984 /* We don't need to do this for function-local types. */
11985 if (TYPE_STUB_DECL (type)
11986 && ! decl_function_context (TYPE_STUB_DECL (type)))
11987 VARRAY_PUSH_TREE (incomplete_types, type);
11991 /* Generate a DIE for a subroutine _type_. */
11993 static void
11994 gen_subroutine_type_die (tree type, dw_die_ref context_die)
11996 tree return_type = TREE_TYPE (type);
11997 dw_die_ref subr_die
11998 = new_die (DW_TAG_subroutine_type,
11999 scope_die_for (type, context_die), type);
12001 equate_type_number_to_die (type, subr_die);
12002 add_prototyped_attribute (subr_die, type);
12003 add_type_attribute (subr_die, return_type, 0, 0, context_die);
12004 gen_formal_types_die (type, subr_die);
12007 /* Generate a DIE for a type definition. */
12009 static void
12010 gen_typedef_die (tree decl, dw_die_ref context_die)
12012 dw_die_ref type_die;
12013 tree origin;
12015 if (TREE_ASM_WRITTEN (decl))
12016 return;
12018 TREE_ASM_WRITTEN (decl) = 1;
12019 type_die = new_die (DW_TAG_typedef, context_die, decl);
12020 origin = decl_ultimate_origin (decl);
12021 if (origin != NULL)
12022 add_abstract_origin_attribute (type_die, origin);
12023 else
12025 tree type;
12027 add_name_and_src_coords_attributes (type_die, decl);
12028 if (DECL_ORIGINAL_TYPE (decl))
12030 type = DECL_ORIGINAL_TYPE (decl);
12032 gcc_assert (type != TREE_TYPE (decl));
12033 equate_type_number_to_die (TREE_TYPE (decl), type_die);
12035 else
12036 type = TREE_TYPE (decl);
12038 add_type_attribute (type_die, type, TREE_READONLY (decl),
12039 TREE_THIS_VOLATILE (decl), context_die);
12042 if (DECL_ABSTRACT (decl))
12043 equate_decl_number_to_die (decl, type_die);
12046 /* Generate a type description DIE. */
12048 static void
12049 gen_type_die (tree type, dw_die_ref context_die)
12051 int need_pop;
12053 if (type == NULL_TREE || type == error_mark_node)
12054 return;
12056 if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12057 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
12059 if (TREE_ASM_WRITTEN (type))
12060 return;
12062 /* Prevent broken recursion; we can't hand off to the same type. */
12063 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
12065 TREE_ASM_WRITTEN (type) = 1;
12066 gen_decl_die (TYPE_NAME (type), context_die);
12067 return;
12070 /* We are going to output a DIE to represent the unqualified version
12071 of this type (i.e. without any const or volatile qualifiers) so
12072 get the main variant (i.e. the unqualified version) of this type
12073 now. (Vectors are special because the debugging info is in the
12074 cloned type itself). */
12075 if (TREE_CODE (type) != VECTOR_TYPE)
12076 type = type_main_variant (type);
12078 if (TREE_ASM_WRITTEN (type))
12079 return;
12081 switch (TREE_CODE (type))
12083 case ERROR_MARK:
12084 break;
12086 case POINTER_TYPE:
12087 case REFERENCE_TYPE:
12088 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
12089 ensures that the gen_type_die recursion will terminate even if the
12090 type is recursive. Recursive types are possible in Ada. */
12091 /* ??? We could perhaps do this for all types before the switch
12092 statement. */
12093 TREE_ASM_WRITTEN (type) = 1;
12095 /* For these types, all that is required is that we output a DIE (or a
12096 set of DIEs) to represent the "basis" type. */
12097 gen_type_die (TREE_TYPE (type), context_die);
12098 break;
12100 case OFFSET_TYPE:
12101 /* This code is used for C++ pointer-to-data-member types.
12102 Output a description of the relevant class type. */
12103 gen_type_die (TYPE_OFFSET_BASETYPE (type), context_die);
12105 /* Output a description of the type of the object pointed to. */
12106 gen_type_die (TREE_TYPE (type), context_die);
12108 /* Now output a DIE to represent this pointer-to-data-member type
12109 itself. */
12110 gen_ptr_to_mbr_type_die (type, context_die);
12111 break;
12113 case FILE_TYPE:
12114 gen_type_die (TREE_TYPE (type), context_die);
12115 /* No way to represent these in Dwarf yet! */
12116 gcc_unreachable ();
12117 break;
12119 case FUNCTION_TYPE:
12120 /* Force out return type (in case it wasn't forced out already). */
12121 gen_type_die (TREE_TYPE (type), context_die);
12122 gen_subroutine_type_die (type, context_die);
12123 break;
12125 case METHOD_TYPE:
12126 /* Force out return type (in case it wasn't forced out already). */
12127 gen_type_die (TREE_TYPE (type), context_die);
12128 gen_subroutine_type_die (type, context_die);
12129 break;
12131 case ARRAY_TYPE:
12132 if (TYPE_STRING_FLAG (type) && TREE_CODE (TREE_TYPE (type)) == CHAR_TYPE)
12134 gen_type_die (TREE_TYPE (type), context_die);
12135 gen_string_type_die (type, context_die);
12137 else
12138 gen_array_type_die (type, context_die);
12139 break;
12141 case VECTOR_TYPE:
12142 gen_array_type_die (type, context_die);
12143 break;
12145 case ENUMERAL_TYPE:
12146 case RECORD_TYPE:
12147 case UNION_TYPE:
12148 case QUAL_UNION_TYPE:
12149 /* If this is a nested type whose containing class hasn't been written
12150 out yet, writing it out will cover this one, too. This does not apply
12151 to instantiations of member class templates; they need to be added to
12152 the containing class as they are generated. FIXME: This hurts the
12153 idea of combining type decls from multiple TUs, since we can't predict
12154 what set of template instantiations we'll get. */
12155 if (TYPE_CONTEXT (type)
12156 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
12157 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
12159 gen_type_die (TYPE_CONTEXT (type), context_die);
12161 if (TREE_ASM_WRITTEN (type))
12162 return;
12164 /* If that failed, attach ourselves to the stub. */
12165 push_decl_scope (TYPE_CONTEXT (type));
12166 context_die = lookup_type_die (TYPE_CONTEXT (type));
12167 need_pop = 1;
12169 else
12171 declare_in_namespace (type, context_die);
12172 need_pop = 0;
12175 if (TREE_CODE (type) == ENUMERAL_TYPE)
12176 gen_enumeration_type_die (type, context_die);
12177 else
12178 gen_struct_or_union_type_die (type, context_die);
12180 if (need_pop)
12181 pop_decl_scope ();
12183 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
12184 it up if it is ever completed. gen_*_type_die will set it for us
12185 when appropriate. */
12186 return;
12188 case VOID_TYPE:
12189 case INTEGER_TYPE:
12190 case REAL_TYPE:
12191 case COMPLEX_TYPE:
12192 case BOOLEAN_TYPE:
12193 case CHAR_TYPE:
12194 /* No DIEs needed for fundamental types. */
12195 break;
12197 case LANG_TYPE:
12198 /* No Dwarf representation currently defined. */
12199 break;
12201 default:
12202 gcc_unreachable ();
12205 TREE_ASM_WRITTEN (type) = 1;
12208 /* Generate a DIE for a tagged type instantiation. */
12210 static void
12211 gen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
12213 if (type == NULL_TREE || type == error_mark_node)
12214 return;
12216 /* We are going to output a DIE to represent the unqualified version of
12217 this type (i.e. without any const or volatile qualifiers) so make sure
12218 that we have the main variant (i.e. the unqualified version) of this
12219 type now. */
12220 gcc_assert (type == type_main_variant (type));
12222 /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
12223 an instance of an unresolved type. */
12225 switch (TREE_CODE (type))
12227 case ERROR_MARK:
12228 break;
12230 case ENUMERAL_TYPE:
12231 gen_inlined_enumeration_type_die (type, context_die);
12232 break;
12234 case RECORD_TYPE:
12235 gen_inlined_structure_type_die (type, context_die);
12236 break;
12238 case UNION_TYPE:
12239 case QUAL_UNION_TYPE:
12240 gen_inlined_union_type_die (type, context_die);
12241 break;
12243 default:
12244 gcc_unreachable ();
12248 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
12249 things which are local to the given block. */
12251 static void
12252 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
12254 int must_output_die = 0;
12255 tree origin;
12256 tree decl;
12257 enum tree_code origin_code;
12259 /* Ignore blocks that are NULL. */
12260 if (stmt == NULL_TREE)
12261 return;
12263 /* If the block is one fragment of a non-contiguous block, do not
12264 process the variables, since they will have been done by the
12265 origin block. Do process subblocks. */
12266 if (BLOCK_FRAGMENT_ORIGIN (stmt))
12268 tree sub;
12270 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
12271 gen_block_die (sub, context_die, depth + 1);
12273 return;
12276 /* Determine the "ultimate origin" of this block. This block may be an
12277 inlined instance of an inlined instance of inline function, so we have
12278 to trace all of the way back through the origin chain to find out what
12279 sort of node actually served as the original seed for the creation of
12280 the current block. */
12281 origin = block_ultimate_origin (stmt);
12282 origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
12284 /* Determine if we need to output any Dwarf DIEs at all to represent this
12285 block. */
12286 if (origin_code == FUNCTION_DECL)
12287 /* The outer scopes for inlinings *must* always be represented. We
12288 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
12289 must_output_die = 1;
12290 else
12292 /* In the case where the current block represents an inlining of the
12293 "body block" of an inline function, we must *NOT* output any DIE for
12294 this block because we have already output a DIE to represent the whole
12295 inlined function scope and the "body block" of any function doesn't
12296 really represent a different scope according to ANSI C rules. So we
12297 check here to make sure that this block does not represent a "body
12298 block inlining" before trying to set the MUST_OUTPUT_DIE flag. */
12299 if (! is_body_block (origin ? origin : stmt))
12301 /* Determine if this block directly contains any "significant"
12302 local declarations which we will need to output DIEs for. */
12303 if (debug_info_level > DINFO_LEVEL_TERSE)
12304 /* We are not in terse mode so *any* local declaration counts
12305 as being a "significant" one. */
12306 must_output_die = (BLOCK_VARS (stmt) != NULL
12307 && (TREE_USED (stmt)
12308 || TREE_ASM_WRITTEN (stmt)
12309 || BLOCK_ABSTRACT (stmt)));
12310 else
12311 /* We are in terse mode, so only local (nested) function
12312 definitions count as "significant" local declarations. */
12313 for (decl = BLOCK_VARS (stmt);
12314 decl != NULL; decl = TREE_CHAIN (decl))
12315 if (TREE_CODE (decl) == FUNCTION_DECL
12316 && DECL_INITIAL (decl))
12318 must_output_die = 1;
12319 break;
12324 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
12325 DIE for any block which contains no significant local declarations at
12326 all. Rather, in such cases we just call `decls_for_scope' so that any
12327 needed Dwarf info for any sub-blocks will get properly generated. Note
12328 that in terse mode, our definition of what constitutes a "significant"
12329 local declaration gets restricted to include only inlined function
12330 instances and local (nested) function definitions. */
12331 if (must_output_die)
12333 if (origin_code == FUNCTION_DECL)
12334 gen_inlined_subroutine_die (stmt, context_die, depth);
12335 else
12336 gen_lexical_block_die (stmt, context_die, depth);
12338 else
12339 decls_for_scope (stmt, context_die, depth);
12342 /* Generate all of the decls declared within a given scope and (recursively)
12343 all of its sub-blocks. */
12345 static void
12346 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
12348 tree decl;
12349 tree subblocks;
12351 /* Ignore NULL blocks. */
12352 if (stmt == NULL_TREE)
12353 return;
12355 if (TREE_USED (stmt))
12357 /* Output the DIEs to represent all of the data objects and typedefs
12358 declared directly within this block but not within any nested
12359 sub-blocks. Also, nested function and tag DIEs have been
12360 generated with a parent of NULL; fix that up now. */
12361 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
12363 dw_die_ref die;
12365 if (TREE_CODE (decl) == FUNCTION_DECL)
12366 die = lookup_decl_die (decl);
12367 else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
12368 die = lookup_type_die (TREE_TYPE (decl));
12369 else
12370 die = NULL;
12372 if (die != NULL && die->die_parent == NULL)
12373 add_child_die (context_die, die);
12374 else
12375 gen_decl_die (decl, context_die);
12379 /* If we're at -g1, we're not interested in subblocks. */
12380 if (debug_info_level <= DINFO_LEVEL_TERSE)
12381 return;
12383 /* Output the DIEs to represent all sub-blocks (and the items declared
12384 therein) of this block. */
12385 for (subblocks = BLOCK_SUBBLOCKS (stmt);
12386 subblocks != NULL;
12387 subblocks = BLOCK_CHAIN (subblocks))
12388 gen_block_die (subblocks, context_die, depth + 1);
12391 /* Is this a typedef we can avoid emitting? */
12393 static inline int
12394 is_redundant_typedef (tree decl)
12396 if (TYPE_DECL_IS_STUB (decl))
12397 return 1;
12399 if (DECL_ARTIFICIAL (decl)
12400 && DECL_CONTEXT (decl)
12401 && is_tagged_type (DECL_CONTEXT (decl))
12402 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
12403 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
12404 /* Also ignore the artificial member typedef for the class name. */
12405 return 1;
12407 return 0;
12410 /* Returns the DIE for decl or aborts. */
12412 static dw_die_ref
12413 force_decl_die (tree decl)
12415 dw_die_ref decl_die;
12416 unsigned saved_external_flag;
12417 tree save_fn = NULL_TREE;
12418 decl_die = lookup_decl_die (decl);
12419 if (!decl_die)
12421 dw_die_ref context_die;
12422 tree decl_context = DECL_CONTEXT (decl);
12423 if (decl_context)
12425 /* Find die that represents this context. */
12426 if (TYPE_P (decl_context))
12427 context_die = force_type_die (decl_context);
12428 else
12429 context_die = force_decl_die (decl_context);
12431 else
12432 context_die = comp_unit_die;
12434 switch (TREE_CODE (decl))
12436 case FUNCTION_DECL:
12437 /* Clear current_function_decl, so that gen_subprogram_die thinks
12438 that this is a declaration. At this point, we just want to force
12439 declaration die. */
12440 save_fn = current_function_decl;
12441 current_function_decl = NULL_TREE;
12442 gen_subprogram_die (decl, context_die);
12443 current_function_decl = save_fn;
12444 break;
12446 case VAR_DECL:
12447 /* Set external flag to force declaration die. Restore it after
12448 gen_decl_die() call. */
12449 saved_external_flag = DECL_EXTERNAL (decl);
12450 DECL_EXTERNAL (decl) = 1;
12451 gen_decl_die (decl, context_die);
12452 DECL_EXTERNAL (decl) = saved_external_flag;
12453 break;
12455 case NAMESPACE_DECL:
12456 dwarf2out_decl (decl);
12457 break;
12459 default:
12460 gcc_unreachable ();
12463 /* See if we can find the die for this deci now.
12464 If not then abort. */
12465 if (!decl_die)
12466 decl_die = lookup_decl_die (decl);
12467 gcc_assert (decl_die);
12470 return decl_die;
12473 /* Returns the DIE for decl or aborts. */
12475 static dw_die_ref
12476 force_type_die (tree type)
12478 dw_die_ref type_die;
12480 type_die = lookup_type_die (type);
12481 if (!type_die)
12483 dw_die_ref context_die;
12484 if (TYPE_CONTEXT (type))
12485 if (TYPE_P (TYPE_CONTEXT (type)))
12486 context_die = force_type_die (TYPE_CONTEXT (type));
12487 else
12488 context_die = force_decl_die (TYPE_CONTEXT (type));
12489 else
12490 context_die = comp_unit_die;
12492 gen_type_die (type, context_die);
12493 type_die = lookup_type_die (type);
12494 gcc_assert (type_die);
12496 return type_die;
12499 /* Force out any required namespaces to be able to output DECL,
12500 and return the new context_die for it, if it's changed. */
12502 static dw_die_ref
12503 setup_namespace_context (tree thing, dw_die_ref context_die)
12505 tree context = (DECL_P (thing)
12506 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
12507 if (context && TREE_CODE (context) == NAMESPACE_DECL)
12508 /* Force out the namespace. */
12509 context_die = force_decl_die (context);
12511 return context_die;
12514 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
12515 type) within its namespace, if appropriate.
12517 For compatibility with older debuggers, namespace DIEs only contain
12518 declarations; all definitions are emitted at CU scope. */
12520 static void
12521 declare_in_namespace (tree thing, dw_die_ref context_die)
12523 dw_die_ref ns_context;
12525 if (debug_info_level <= DINFO_LEVEL_TERSE)
12526 return;
12528 /* If this decl is from an inlined function, then don't try to emit it in its
12529 namespace, as we will get confused. It would have already been emitted
12530 when the abstract instance of the inline function was emitted anyways. */
12531 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
12532 return;
12534 ns_context = setup_namespace_context (thing, context_die);
12536 if (ns_context != context_die)
12538 if (DECL_P (thing))
12539 gen_decl_die (thing, ns_context);
12540 else
12541 gen_type_die (thing, ns_context);
12545 /* Generate a DIE for a namespace or namespace alias. */
12547 static void
12548 gen_namespace_die (tree decl)
12550 dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
12552 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
12553 they are an alias of. */
12554 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
12556 /* Output a real namespace. */
12557 dw_die_ref namespace_die
12558 = new_die (DW_TAG_namespace, context_die, decl);
12559 add_name_and_src_coords_attributes (namespace_die, decl);
12560 equate_decl_number_to_die (decl, namespace_die);
12562 else
12564 /* Output a namespace alias. */
12566 /* Force out the namespace we are an alias of, if necessary. */
12567 dw_die_ref origin_die
12568 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
12570 /* Now create the namespace alias DIE. */
12571 dw_die_ref namespace_die
12572 = new_die (DW_TAG_imported_declaration, context_die, decl);
12573 add_name_and_src_coords_attributes (namespace_die, decl);
12574 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
12575 equate_decl_number_to_die (decl, namespace_die);
12579 /* Generate Dwarf debug information for a decl described by DECL. */
12581 static void
12582 gen_decl_die (tree decl, dw_die_ref context_die)
12584 tree origin;
12586 if (DECL_P (decl) && DECL_IGNORED_P (decl))
12587 return;
12589 switch (TREE_CODE (decl))
12591 case ERROR_MARK:
12592 break;
12594 case CONST_DECL:
12595 /* The individual enumerators of an enum type get output when we output
12596 the Dwarf representation of the relevant enum type itself. */
12597 break;
12599 case FUNCTION_DECL:
12600 /* Don't output any DIEs to represent mere function declarations,
12601 unless they are class members or explicit block externs. */
12602 if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
12603 && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
12604 break;
12606 #if 0
12607 /* FIXME */
12608 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
12609 on local redeclarations of global functions. That seems broken. */
12610 if (current_function_decl != decl)
12611 /* This is only a declaration. */;
12612 #endif
12614 /* If we're emitting a clone, emit info for the abstract instance. */
12615 if (DECL_ORIGIN (decl) != decl)
12616 dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
12618 /* If we're emitting an out-of-line copy of an inline function,
12619 emit info for the abstract instance and set up to refer to it. */
12620 else if (cgraph_function_possibly_inlined_p (decl)
12621 && ! DECL_ABSTRACT (decl)
12622 && ! class_or_namespace_scope_p (context_die)
12623 /* dwarf2out_abstract_function won't emit a die if this is just
12624 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
12625 that case, because that works only if we have a die. */
12626 && DECL_INITIAL (decl) != NULL_TREE)
12628 dwarf2out_abstract_function (decl);
12629 set_decl_origin_self (decl);
12632 /* Otherwise we're emitting the primary DIE for this decl. */
12633 else if (debug_info_level > DINFO_LEVEL_TERSE)
12635 /* Before we describe the FUNCTION_DECL itself, make sure that we
12636 have described its return type. */
12637 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
12639 /* And its virtual context. */
12640 if (DECL_VINDEX (decl) != NULL_TREE)
12641 gen_type_die (DECL_CONTEXT (decl), context_die);
12643 /* And its containing type. */
12644 origin = decl_class_context (decl);
12645 if (origin != NULL_TREE)
12646 gen_type_die_for_member (origin, decl, context_die);
12648 /* And its containing namespace. */
12649 declare_in_namespace (decl, context_die);
12652 /* Now output a DIE to represent the function itself. */
12653 gen_subprogram_die (decl, context_die);
12654 break;
12656 case TYPE_DECL:
12657 /* If we are in terse mode, don't generate any DIEs to represent any
12658 actual typedefs. */
12659 if (debug_info_level <= DINFO_LEVEL_TERSE)
12660 break;
12662 /* In the special case of a TYPE_DECL node representing the declaration
12663 of some type tag, if the given TYPE_DECL is marked as having been
12664 instantiated from some other (original) TYPE_DECL node (e.g. one which
12665 was generated within the original definition of an inline function) we
12666 have to generate a special (abbreviated) DW_TAG_structure_type,
12667 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. */
12668 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
12670 gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
12671 break;
12674 if (is_redundant_typedef (decl))
12675 gen_type_die (TREE_TYPE (decl), context_die);
12676 else
12677 /* Output a DIE to represent the typedef itself. */
12678 gen_typedef_die (decl, context_die);
12679 break;
12681 case LABEL_DECL:
12682 if (debug_info_level >= DINFO_LEVEL_NORMAL)
12683 gen_label_die (decl, context_die);
12684 break;
12686 case VAR_DECL:
12687 case RESULT_DECL:
12688 /* If we are in terse mode, don't generate any DIEs to represent any
12689 variable declarations or definitions. */
12690 if (debug_info_level <= DINFO_LEVEL_TERSE)
12691 break;
12693 /* Output any DIEs that are needed to specify the type of this data
12694 object. */
12695 gen_type_die (TREE_TYPE (decl), context_die);
12697 /* And its containing type. */
12698 origin = decl_class_context (decl);
12699 if (origin != NULL_TREE)
12700 gen_type_die_for_member (origin, decl, context_die);
12702 /* And its containing namespace. */
12703 declare_in_namespace (decl, context_die);
12705 /* Now output the DIE to represent the data object itself. This gets
12706 complicated because of the possibility that the VAR_DECL really
12707 represents an inlined instance of a formal parameter for an inline
12708 function. */
12709 origin = decl_ultimate_origin (decl);
12710 if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
12711 gen_formal_parameter_die (decl, context_die);
12712 else
12713 gen_variable_die (decl, context_die);
12714 break;
12716 case FIELD_DECL:
12717 /* Ignore the nameless fields that are used to skip bits but handle C++
12718 anonymous unions and structs. */
12719 if (DECL_NAME (decl) != NULL_TREE
12720 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
12721 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
12723 gen_type_die (member_declared_type (decl), context_die);
12724 gen_field_die (decl, context_die);
12726 break;
12728 case PARM_DECL:
12729 gen_type_die (TREE_TYPE (decl), context_die);
12730 gen_formal_parameter_die (decl, context_die);
12731 break;
12733 case NAMESPACE_DECL:
12734 gen_namespace_die (decl);
12735 break;
12737 default:
12738 /* Probably some frontend-internal decl. Assume we don't care. */
12739 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
12740 break;
12744 /* Add Ada "use" clause information for SGI Workshop debugger. */
12746 void
12747 dwarf2out_add_library_unit_info (const char *filename, const char *context_list)
12749 unsigned int file_index;
12751 if (filename != NULL)
12753 dw_die_ref unit_die = new_die (DW_TAG_module, comp_unit_die, NULL);
12754 tree context_list_decl
12755 = build_decl (LABEL_DECL, get_identifier (context_list),
12756 void_type_node);
12758 TREE_PUBLIC (context_list_decl) = TRUE;
12759 add_name_attribute (unit_die, context_list);
12760 file_index = lookup_filename (filename);
12761 add_AT_unsigned (unit_die, DW_AT_decl_file, file_index);
12762 add_pubname (context_list_decl, unit_die);
12766 /* Output debug information for global decl DECL. Called from toplev.c after
12767 compilation proper has finished. */
12769 static void
12770 dwarf2out_global_decl (tree decl)
12772 /* Output DWARF2 information for file-scope tentative data object
12773 declarations, file-scope (extern) function declarations (which had no
12774 corresponding body) and file-scope tagged type declarations and
12775 definitions which have not yet been forced out. */
12776 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
12777 dwarf2out_decl (decl);
12780 /* Output debug information for type decl DECL. Called from toplev.c
12781 and from language front ends (to record built-in types). */
12782 static void
12783 dwarf2out_type_decl (tree decl, int local)
12785 if (!local)
12786 dwarf2out_decl (decl);
12789 /* Output debug information for imported module or decl. */
12791 static void
12792 dwarf2out_imported_module_or_decl (tree decl, tree context)
12794 dw_die_ref imported_die, at_import_die;
12795 dw_die_ref scope_die;
12796 unsigned file_index;
12797 expanded_location xloc;
12799 if (debug_info_level <= DINFO_LEVEL_TERSE)
12800 return;
12802 gcc_assert (decl);
12804 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
12805 We need decl DIE for reference and scope die. First, get DIE for the decl
12806 itself. */
12808 /* Get the scope die for decl context. Use comp_unit_die for global module
12809 or decl. If die is not found for non globals, force new die. */
12810 if (!context)
12811 scope_die = comp_unit_die;
12812 else if (TYPE_P (context))
12813 scope_die = force_type_die (context);
12814 else
12815 scope_die = force_decl_die (context);
12817 /* For TYPE_DECL or CONST_DECL, lookup TREE_TYPE. */
12818 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
12819 at_import_die = force_type_die (TREE_TYPE (decl));
12820 else
12821 at_import_die = force_decl_die (decl);
12823 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
12824 if (TREE_CODE (decl) == NAMESPACE_DECL)
12825 imported_die = new_die (DW_TAG_imported_module, scope_die, context);
12826 else
12827 imported_die = new_die (DW_TAG_imported_declaration, scope_die, context);
12829 xloc = expand_location (input_location);
12830 file_index = lookup_filename (xloc.file);
12831 add_AT_unsigned (imported_die, DW_AT_decl_file, file_index);
12832 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
12833 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
12836 /* Write the debugging output for DECL. */
12838 void
12839 dwarf2out_decl (tree decl)
12841 dw_die_ref context_die = comp_unit_die;
12843 switch (TREE_CODE (decl))
12845 case ERROR_MARK:
12846 return;
12848 case FUNCTION_DECL:
12849 /* What we would really like to do here is to filter out all mere
12850 file-scope declarations of file-scope functions which are never
12851 referenced later within this translation unit (and keep all of ones
12852 that *are* referenced later on) but we aren't clairvoyant, so we have
12853 no idea which functions will be referenced in the future (i.e. later
12854 on within the current translation unit). So here we just ignore all
12855 file-scope function declarations which are not also definitions. If
12856 and when the debugger needs to know something about these functions,
12857 it will have to hunt around and find the DWARF information associated
12858 with the definition of the function.
12860 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
12861 nodes represent definitions and which ones represent mere
12862 declarations. We have to check DECL_INITIAL instead. That's because
12863 the C front-end supports some weird semantics for "extern inline"
12864 function definitions. These can get inlined within the current
12865 translation unit (an thus, we need to generate Dwarf info for their
12866 abstract instances so that the Dwarf info for the concrete inlined
12867 instances can have something to refer to) but the compiler never
12868 generates any out-of-lines instances of such things (despite the fact
12869 that they *are* definitions).
12871 The important point is that the C front-end marks these "extern
12872 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
12873 them anyway. Note that the C++ front-end also plays some similar games
12874 for inline function definitions appearing within include files which
12875 also contain `#pragma interface' pragmas. */
12876 if (DECL_INITIAL (decl) == NULL_TREE)
12877 return;
12879 /* If we're a nested function, initially use a parent of NULL; if we're
12880 a plain function, this will be fixed up in decls_for_scope. If
12881 we're a method, it will be ignored, since we already have a DIE. */
12882 if (decl_function_context (decl)
12883 /* But if we're in terse mode, we don't care about scope. */
12884 && debug_info_level > DINFO_LEVEL_TERSE)
12885 context_die = NULL;
12886 break;
12888 case VAR_DECL:
12889 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
12890 declaration and if the declaration was never even referenced from
12891 within this entire compilation unit. We suppress these DIEs in
12892 order to save space in the .debug section (by eliminating entries
12893 which are probably useless). Note that we must not suppress
12894 block-local extern declarations (whether used or not) because that
12895 would screw-up the debugger's name lookup mechanism and cause it to
12896 miss things which really ought to be in scope at a given point. */
12897 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
12898 return;
12900 /* If we are in terse mode, don't generate any DIEs to represent any
12901 variable declarations or definitions. */
12902 if (debug_info_level <= DINFO_LEVEL_TERSE)
12903 return;
12904 break;
12906 case NAMESPACE_DECL:
12907 if (debug_info_level <= DINFO_LEVEL_TERSE)
12908 return;
12909 if (lookup_decl_die (decl) != NULL)
12910 return;
12911 break;
12913 case TYPE_DECL:
12914 /* Don't emit stubs for types unless they are needed by other DIEs. */
12915 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
12916 return;
12918 /* Don't bother trying to generate any DIEs to represent any of the
12919 normal built-in types for the language we are compiling. */
12920 if (DECL_IS_BUILTIN (decl))
12922 /* OK, we need to generate one for `bool' so GDB knows what type
12923 comparisons have. */
12924 if ((get_AT_unsigned (comp_unit_die, DW_AT_language)
12925 == DW_LANG_C_plus_plus)
12926 && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
12927 && ! DECL_IGNORED_P (decl))
12928 modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
12930 return;
12933 /* If we are in terse mode, don't generate any DIEs for types. */
12934 if (debug_info_level <= DINFO_LEVEL_TERSE)
12935 return;
12937 /* If we're a function-scope tag, initially use a parent of NULL;
12938 this will be fixed up in decls_for_scope. */
12939 if (decl_function_context (decl))
12940 context_die = NULL;
12942 break;
12944 default:
12945 return;
12948 gen_decl_die (decl, context_die);
12951 /* Output a marker (i.e. a label) for the beginning of the generated code for
12952 a lexical block. */
12954 static void
12955 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
12956 unsigned int blocknum)
12958 function_section (current_function_decl);
12959 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
12962 /* Output a marker (i.e. a label) for the end of the generated code for a
12963 lexical block. */
12965 static void
12966 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
12968 function_section (current_function_decl);
12969 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
12972 /* Returns nonzero if it is appropriate not to emit any debugging
12973 information for BLOCK, because it doesn't contain any instructions.
12975 Don't allow this for blocks with nested functions or local classes
12976 as we would end up with orphans, and in the presence of scheduling
12977 we may end up calling them anyway. */
12979 static bool
12980 dwarf2out_ignore_block (tree block)
12982 tree decl;
12984 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
12985 if (TREE_CODE (decl) == FUNCTION_DECL
12986 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
12987 return 0;
12989 return 1;
12992 /* Lookup FILE_NAME (in the list of filenames that we know about here in
12993 dwarf2out.c) and return its "index". The index of each (known) filename is
12994 just a unique number which is associated with only that one filename. We
12995 need such numbers for the sake of generating labels (in the .debug_sfnames
12996 section) and references to those files numbers (in the .debug_srcinfo
12997 and.debug_macinfo sections). If the filename given as an argument is not
12998 found in our current list, add it to the list and assign it the next
12999 available unique index number. In order to speed up searches, we remember
13000 the index of the filename was looked up last. This handles the majority of
13001 all searches. */
13003 static unsigned
13004 lookup_filename (const char *file_name)
13006 size_t i, n;
13007 char *save_file_name;
13009 /* Check to see if the file name that was searched on the previous
13010 call matches this file name. If so, return the index. */
13011 if (file_table_last_lookup_index != 0)
13013 const char *last
13014 = VARRAY_CHAR_PTR (file_table, file_table_last_lookup_index);
13015 if (strcmp (file_name, last) == 0)
13016 return file_table_last_lookup_index;
13019 /* Didn't match the previous lookup, search the table. */
13020 n = VARRAY_ACTIVE_SIZE (file_table);
13021 for (i = 1; i < n; i++)
13022 if (strcmp (file_name, VARRAY_CHAR_PTR (file_table, i)) == 0)
13024 file_table_last_lookup_index = i;
13025 return i;
13028 /* Add the new entry to the end of the filename table. */
13029 file_table_last_lookup_index = n;
13030 save_file_name = (char *) ggc_strdup (file_name);
13031 VARRAY_PUSH_CHAR_PTR (file_table, save_file_name);
13032 VARRAY_PUSH_UINT (file_table_emitted, 0);
13034 return i;
13037 static int
13038 maybe_emit_file (int fileno)
13040 if (DWARF2_ASM_LINE_DEBUG_INFO && fileno > 0)
13042 if (!VARRAY_UINT (file_table_emitted, fileno))
13044 VARRAY_UINT (file_table_emitted, fileno) = ++emitcount;
13045 fprintf (asm_out_file, "\t.file %u ",
13046 VARRAY_UINT (file_table_emitted, fileno));
13047 output_quoted_string (asm_out_file,
13048 VARRAY_CHAR_PTR (file_table, fileno));
13049 fputc ('\n', asm_out_file);
13051 return VARRAY_UINT (file_table_emitted, fileno);
13053 else
13054 return fileno;
13057 static void
13058 init_file_table (void)
13060 /* Allocate the initial hunk of the file_table. */
13061 VARRAY_CHAR_PTR_INIT (file_table, 64, "file_table");
13062 VARRAY_UINT_INIT (file_table_emitted, 64, "file_table_emitted");
13064 /* Skip the first entry - file numbers begin at 1. */
13065 VARRAY_PUSH_CHAR_PTR (file_table, NULL);
13066 VARRAY_PUSH_UINT (file_table_emitted, 0);
13067 file_table_last_lookup_index = 0;
13070 /* Called by the final INSN scan whenever we see a var location. We
13071 use it to drop labels in the right places, and throw the location in
13072 our lookup table. */
13074 static void
13075 dwarf2out_var_location (rtx loc_note)
13077 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
13078 struct var_loc_node *newloc;
13079 rtx prev_insn;
13080 static rtx last_insn;
13081 static const char *last_label;
13082 tree decl;
13084 if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
13085 return;
13086 prev_insn = PREV_INSN (loc_note);
13088 newloc = ggc_alloc_cleared (sizeof (struct var_loc_node));
13089 /* If the insn we processed last time is the previous insn
13090 and it is also a var location note, use the label we emitted
13091 last time. */
13092 if (last_insn != NULL_RTX
13093 && last_insn == prev_insn
13094 && NOTE_P (prev_insn)
13095 && NOTE_LINE_NUMBER (prev_insn) == NOTE_INSN_VAR_LOCATION)
13097 newloc->label = last_label;
13099 else
13101 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
13102 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
13103 loclabel_num++;
13104 newloc->label = ggc_strdup (loclabel);
13106 newloc->var_loc_note = loc_note;
13107 newloc->next = NULL;
13109 last_insn = loc_note;
13110 last_label = newloc->label;
13111 decl = NOTE_VAR_LOCATION_DECL (loc_note);
13112 if (DECL_DEBUG_EXPR (decl) && DECL_DEBUG_EXPR_IS_FROM (decl)
13113 && DECL_P (DECL_DEBUG_EXPR (decl)))
13114 decl = DECL_DEBUG_EXPR (decl);
13115 add_var_loc_to_decl (decl, newloc);
13118 /* We need to reset the locations at the beginning of each
13119 function. We can't do this in the end_function hook, because the
13120 declarations that use the locations won't have been outputted when
13121 that hook is called. */
13123 static void
13124 dwarf2out_begin_function (tree unused ATTRIBUTE_UNUSED)
13126 htab_empty (decl_loc_table);
13129 /* Output a label to mark the beginning of a source code line entry
13130 and record information relating to this source line, in
13131 'line_info_table' for later output of the .debug_line section. */
13133 static void
13134 dwarf2out_source_line (unsigned int line, const char *filename)
13136 if (debug_info_level >= DINFO_LEVEL_NORMAL
13137 && line != 0)
13139 function_section (current_function_decl);
13141 /* If requested, emit something human-readable. */
13142 if (flag_debug_asm)
13143 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
13144 filename, line);
13146 if (DWARF2_ASM_LINE_DEBUG_INFO)
13148 unsigned file_num = lookup_filename (filename);
13150 file_num = maybe_emit_file (file_num);
13152 /* Emit the .loc directive understood by GNU as. */
13153 fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
13155 /* Indicate that line number info exists. */
13156 line_info_table_in_use++;
13158 /* Indicate that multiple line number tables exist. */
13159 if (DECL_SECTION_NAME (current_function_decl))
13160 separate_line_info_table_in_use++;
13162 else if (DECL_SECTION_NAME (current_function_decl))
13164 dw_separate_line_info_ref line_info;
13165 targetm.asm_out.internal_label (asm_out_file, SEPARATE_LINE_CODE_LABEL,
13166 separate_line_info_table_in_use);
13168 /* Expand the line info table if necessary. */
13169 if (separate_line_info_table_in_use
13170 == separate_line_info_table_allocated)
13172 separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
13173 separate_line_info_table
13174 = ggc_realloc (separate_line_info_table,
13175 separate_line_info_table_allocated
13176 * sizeof (dw_separate_line_info_entry));
13177 memset (separate_line_info_table
13178 + separate_line_info_table_in_use,
13180 (LINE_INFO_TABLE_INCREMENT
13181 * sizeof (dw_separate_line_info_entry)));
13184 /* Add the new entry at the end of the line_info_table. */
13185 line_info
13186 = &separate_line_info_table[separate_line_info_table_in_use++];
13187 line_info->dw_file_num = lookup_filename (filename);
13188 line_info->dw_line_num = line;
13189 line_info->function = current_function_funcdef_no;
13191 else
13193 dw_line_info_ref line_info;
13195 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
13196 line_info_table_in_use);
13198 /* Expand the line info table if necessary. */
13199 if (line_info_table_in_use == line_info_table_allocated)
13201 line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
13202 line_info_table
13203 = ggc_realloc (line_info_table,
13204 (line_info_table_allocated
13205 * sizeof (dw_line_info_entry)));
13206 memset (line_info_table + line_info_table_in_use, 0,
13207 LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
13210 /* Add the new entry at the end of the line_info_table. */
13211 line_info = &line_info_table[line_info_table_in_use++];
13212 line_info->dw_file_num = lookup_filename (filename);
13213 line_info->dw_line_num = line;
13218 /* Record the beginning of a new source file. */
13220 static void
13221 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
13223 if (flag_eliminate_dwarf2_dups)
13225 /* Record the beginning of the file for break_out_includes. */
13226 dw_die_ref bincl_die;
13228 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
13229 add_AT_string (bincl_die, DW_AT_name, filename);
13232 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13234 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13235 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
13236 dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
13237 lineno);
13238 maybe_emit_file (lookup_filename (filename));
13239 dw2_asm_output_data_uleb128 (lookup_filename (filename),
13240 "Filename we just started");
13244 /* Record the end of a source file. */
13246 static void
13247 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
13249 if (flag_eliminate_dwarf2_dups)
13250 /* Record the end of the file for break_out_includes. */
13251 new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
13253 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13255 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13256 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
13260 /* Called from debug_define in toplev.c. The `buffer' parameter contains
13261 the tail part of the directive line, i.e. the part which is past the
13262 initial whitespace, #, whitespace, directive-name, whitespace part. */
13264 static void
13265 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
13266 const char *buffer ATTRIBUTE_UNUSED)
13268 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13270 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13271 dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
13272 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
13273 dw2_asm_output_nstring (buffer, -1, "The macro");
13277 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
13278 the tail part of the directive line, i.e. the part which is past the
13279 initial whitespace, #, whitespace, directive-name, whitespace part. */
13281 static void
13282 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
13283 const char *buffer ATTRIBUTE_UNUSED)
13285 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13287 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13288 dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
13289 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
13290 dw2_asm_output_nstring (buffer, -1, "The macro");
13294 /* Set up for Dwarf output at the start of compilation. */
13296 static void
13297 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
13299 init_file_table ();
13301 /* Allocate the decl_die_table. */
13302 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
13303 decl_die_table_eq, NULL);
13305 /* Allocate the decl_loc_table. */
13306 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
13307 decl_loc_table_eq, NULL);
13309 /* Allocate the initial hunk of the decl_scope_table. */
13310 VARRAY_TREE_INIT (decl_scope_table, 256, "decl_scope_table");
13312 /* Allocate the initial hunk of the abbrev_die_table. */
13313 abbrev_die_table = ggc_alloc_cleared (ABBREV_DIE_TABLE_INCREMENT
13314 * sizeof (dw_die_ref));
13315 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
13316 /* Zero-th entry is allocated, but unused. */
13317 abbrev_die_table_in_use = 1;
13319 /* Allocate the initial hunk of the line_info_table. */
13320 line_info_table = ggc_alloc_cleared (LINE_INFO_TABLE_INCREMENT
13321 * sizeof (dw_line_info_entry));
13322 line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
13324 /* Zero-th entry is allocated, but unused. */
13325 line_info_table_in_use = 1;
13327 /* Generate the initial DIE for the .debug section. Note that the (string)
13328 value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
13329 will (typically) be a relative pathname and that this pathname should be
13330 taken as being relative to the directory from which the compiler was
13331 invoked when the given (base) source file was compiled. We will fill
13332 in this value in dwarf2out_finish. */
13333 comp_unit_die = gen_compile_unit_die (NULL);
13335 VARRAY_TREE_INIT (incomplete_types, 64, "incomplete_types");
13337 VARRAY_RTX_INIT (used_rtx_varray, 32, "used_rtx_varray");
13339 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
13340 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
13341 DEBUG_ABBREV_SECTION_LABEL, 0);
13342 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
13344 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
13345 DEBUG_INFO_SECTION_LABEL, 0);
13346 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
13347 DEBUG_LINE_SECTION_LABEL, 0);
13348 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
13349 DEBUG_RANGES_SECTION_LABEL, 0);
13350 named_section_flags (DEBUG_ABBREV_SECTION, SECTION_DEBUG);
13351 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
13352 named_section_flags (DEBUG_INFO_SECTION, SECTION_DEBUG);
13353 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
13354 named_section_flags (DEBUG_LINE_SECTION, SECTION_DEBUG);
13355 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
13357 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13359 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13360 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
13361 DEBUG_MACINFO_SECTION_LABEL, 0);
13362 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
13365 text_section ();
13366 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
13369 /* A helper function for dwarf2out_finish called through
13370 ht_forall. Emit one queued .debug_str string. */
13372 static int
13373 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
13375 struct indirect_string_node *node = (struct indirect_string_node *) *h;
13377 if (node->form == DW_FORM_strp)
13379 named_section_flags (DEBUG_STR_SECTION, DEBUG_STR_SECTION_FLAGS);
13380 ASM_OUTPUT_LABEL (asm_out_file, node->label);
13381 assemble_string (node->str, strlen (node->str) + 1);
13384 return 1;
13389 /* Clear the marks for a die and its children.
13390 Be cool if the mark isn't set. */
13392 static void
13393 prune_unmark_dies (dw_die_ref die)
13395 dw_die_ref c;
13396 die->die_mark = 0;
13397 for (c = die->die_child; c; c = c->die_sib)
13398 prune_unmark_dies (c);
13402 /* Given DIE that we're marking as used, find any other dies
13403 it references as attributes and mark them as used. */
13405 static void
13406 prune_unused_types_walk_attribs (dw_die_ref die)
13408 dw_attr_ref a;
13410 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
13412 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
13414 /* A reference to another DIE.
13415 Make sure that it will get emitted. */
13416 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
13418 else if (a->dw_attr == DW_AT_decl_file)
13420 /* A reference to a file. Make sure the file name is emitted. */
13421 a->dw_attr_val.v.val_unsigned =
13422 maybe_emit_file (a->dw_attr_val.v.val_unsigned);
13428 /* Mark DIE as being used. If DOKIDS is true, then walk down
13429 to DIE's children. */
13431 static void
13432 prune_unused_types_mark (dw_die_ref die, int dokids)
13434 dw_die_ref c;
13436 if (die->die_mark == 0)
13438 /* We haven't done this node yet. Mark it as used. */
13439 die->die_mark = 1;
13441 /* We also have to mark its parents as used.
13442 (But we don't want to mark our parents' kids due to this.) */
13443 if (die->die_parent)
13444 prune_unused_types_mark (die->die_parent, 0);
13446 /* Mark any referenced nodes. */
13447 prune_unused_types_walk_attribs (die);
13449 /* If this node is a specification,
13450 also mark the definition, if it exists. */
13451 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
13452 prune_unused_types_mark (die->die_definition, 1);
13455 if (dokids && die->die_mark != 2)
13457 /* We need to walk the children, but haven't done so yet.
13458 Remember that we've walked the kids. */
13459 die->die_mark = 2;
13461 /* Walk them. */
13462 for (c = die->die_child; c; c = c->die_sib)
13464 /* If this is an array type, we need to make sure our
13465 kids get marked, even if they're types. */
13466 if (die->die_tag == DW_TAG_array_type)
13467 prune_unused_types_mark (c, 1);
13468 else
13469 prune_unused_types_walk (c);
13475 /* Walk the tree DIE and mark types that we actually use. */
13477 static void
13478 prune_unused_types_walk (dw_die_ref die)
13480 dw_die_ref c;
13482 /* Don't do anything if this node is already marked. */
13483 if (die->die_mark)
13484 return;
13486 switch (die->die_tag) {
13487 case DW_TAG_const_type:
13488 case DW_TAG_packed_type:
13489 case DW_TAG_pointer_type:
13490 case DW_TAG_reference_type:
13491 case DW_TAG_volatile_type:
13492 case DW_TAG_typedef:
13493 case DW_TAG_array_type:
13494 case DW_TAG_structure_type:
13495 case DW_TAG_union_type:
13496 case DW_TAG_class_type:
13497 case DW_TAG_friend:
13498 case DW_TAG_variant_part:
13499 case DW_TAG_enumeration_type:
13500 case DW_TAG_subroutine_type:
13501 case DW_TAG_string_type:
13502 case DW_TAG_set_type:
13503 case DW_TAG_subrange_type:
13504 case DW_TAG_ptr_to_member_type:
13505 case DW_TAG_file_type:
13506 /* It's a type node --- don't mark it. */
13507 return;
13509 default:
13510 /* Mark everything else. */
13511 break;
13514 die->die_mark = 1;
13516 /* Now, mark any dies referenced from here. */
13517 prune_unused_types_walk_attribs (die);
13519 /* Mark children. */
13520 for (c = die->die_child; c; c = c->die_sib)
13521 prune_unused_types_walk (c);
13525 /* Remove from the tree DIE any dies that aren't marked. */
13527 static void
13528 prune_unused_types_prune (dw_die_ref die)
13530 dw_die_ref c, p, n;
13532 gcc_assert (die->die_mark);
13534 p = NULL;
13535 for (c = die->die_child; c; c = n)
13537 n = c->die_sib;
13538 if (c->die_mark)
13540 prune_unused_types_prune (c);
13541 p = c;
13543 else
13545 if (p)
13546 p->die_sib = n;
13547 else
13548 die->die_child = n;
13549 free_die (c);
13555 /* Remove dies representing declarations that we never use. */
13557 static void
13558 prune_unused_types (void)
13560 unsigned int i;
13561 limbo_die_node *node;
13563 /* Clear all the marks. */
13564 prune_unmark_dies (comp_unit_die);
13565 for (node = limbo_die_list; node; node = node->next)
13566 prune_unmark_dies (node->die);
13568 /* Set the mark on nodes that are actually used. */
13569 prune_unused_types_walk (comp_unit_die);
13570 for (node = limbo_die_list; node; node = node->next)
13571 prune_unused_types_walk (node->die);
13573 /* Also set the mark on nodes referenced from the
13574 pubname_table or arange_table. */
13575 for (i = 0; i < pubname_table_in_use; i++)
13576 prune_unused_types_mark (pubname_table[i].die, 1);
13577 for (i = 0; i < arange_table_in_use; i++)
13578 prune_unused_types_mark (arange_table[i], 1);
13580 /* Get rid of nodes that aren't marked. */
13581 prune_unused_types_prune (comp_unit_die);
13582 for (node = limbo_die_list; node; node = node->next)
13583 prune_unused_types_prune (node->die);
13585 /* Leave the marks clear. */
13586 prune_unmark_dies (comp_unit_die);
13587 for (node = limbo_die_list; node; node = node->next)
13588 prune_unmark_dies (node->die);
13591 /* Output stuff that dwarf requires at the end of every file,
13592 and generate the DWARF-2 debugging info. */
13594 static void
13595 dwarf2out_finish (const char *filename)
13597 limbo_die_node *node, *next_node;
13598 dw_die_ref die = 0;
13600 /* Add the name for the main input file now. We delayed this from
13601 dwarf2out_init to avoid complications with PCH. */
13602 add_name_attribute (comp_unit_die, filename);
13603 if (filename[0] != DIR_SEPARATOR)
13604 add_comp_dir_attribute (comp_unit_die);
13605 else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
13607 size_t i;
13608 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
13609 if (VARRAY_CHAR_PTR (file_table, i)[0] != DIR_SEPARATOR
13610 /* Don't add cwd for <built-in>. */
13611 && VARRAY_CHAR_PTR (file_table, i)[0] != '<')
13613 add_comp_dir_attribute (comp_unit_die);
13614 break;
13618 /* Traverse the limbo die list, and add parent/child links. The only
13619 dies without parents that should be here are concrete instances of
13620 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
13621 For concrete instances, we can get the parent die from the abstract
13622 instance. */
13623 for (node = limbo_die_list; node; node = next_node)
13625 next_node = node->next;
13626 die = node->die;
13628 if (die->die_parent == NULL)
13630 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
13632 if (origin)
13633 add_child_die (origin->die_parent, die);
13634 else if (die == comp_unit_die)
13636 else if (errorcount > 0 || sorrycount > 0)
13637 /* It's OK to be confused by errors in the input. */
13638 add_child_die (comp_unit_die, die);
13639 else
13641 /* In certain situations, the lexical block containing a
13642 nested function can be optimized away, which results
13643 in the nested function die being orphaned. Likewise
13644 with the return type of that nested function. Force
13645 this to be a child of the containing function.
13647 It may happen that even the containing function got fully
13648 inlined and optimized out. In that case we are lost and
13649 assign the empty child. This should not be big issue as
13650 the function is likely unreachable too. */
13651 tree context = NULL_TREE;
13653 gcc_assert (node->created_for);
13655 if (DECL_P (node->created_for))
13656 context = DECL_CONTEXT (node->created_for);
13657 else if (TYPE_P (node->created_for))
13658 context = TYPE_CONTEXT (node->created_for);
13660 gcc_assert (context && TREE_CODE (context) == FUNCTION_DECL);
13662 origin = lookup_decl_die (context);
13663 if (origin)
13664 add_child_die (origin, die);
13665 else
13666 add_child_die (comp_unit_die, die);
13671 limbo_die_list = NULL;
13673 /* Walk through the list of incomplete types again, trying once more to
13674 emit full debugging info for them. */
13675 retry_incomplete_types ();
13677 /* We need to reverse all the dies before break_out_includes, or
13678 we'll see the end of an include file before the beginning. */
13679 reverse_all_dies (comp_unit_die);
13681 if (flag_eliminate_unused_debug_types)
13682 prune_unused_types ();
13684 /* Generate separate CUs for each of the include files we've seen.
13685 They will go into limbo_die_list. */
13686 if (flag_eliminate_dwarf2_dups)
13687 break_out_includes (comp_unit_die);
13689 /* Traverse the DIE's and add add sibling attributes to those DIE's
13690 that have children. */
13691 add_sibling_attributes (comp_unit_die);
13692 for (node = limbo_die_list; node; node = node->next)
13693 add_sibling_attributes (node->die);
13695 /* Output a terminator label for the .text section. */
13696 text_section ();
13697 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
13699 /* Output the source line correspondence table. We must do this
13700 even if there is no line information. Otherwise, on an empty
13701 translation unit, we will generate a present, but empty,
13702 .debug_info section. IRIX 6.5 `nm' will then complain when
13703 examining the file. */
13704 if (! DWARF2_ASM_LINE_DEBUG_INFO)
13706 named_section_flags (DEBUG_LINE_SECTION, SECTION_DEBUG);
13707 output_line_info ();
13710 /* Output location list section if necessary. */
13711 if (have_location_lists)
13713 /* Output the location lists info. */
13714 named_section_flags (DEBUG_LOC_SECTION, SECTION_DEBUG);
13715 ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
13716 DEBUG_LOC_SECTION_LABEL, 0);
13717 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
13718 output_location_lists (die);
13719 have_location_lists = 0;
13722 /* We can only use the low/high_pc attributes if all of the code was
13723 in .text. */
13724 if (separate_line_info_table_in_use == 0)
13726 add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
13727 add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
13730 /* If it wasn't, we need to give .debug_loc and .debug_ranges an appropriate
13731 "base address". Use zero so that these addresses become absolute. */
13732 else if (have_location_lists || ranges_table_in_use)
13733 add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
13735 if (debug_info_level >= DINFO_LEVEL_NORMAL)
13736 add_AT_lbl_offset (comp_unit_die, DW_AT_stmt_list,
13737 debug_line_section_label);
13739 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13740 add_AT_lbl_offset (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
13742 /* Output all of the compilation units. We put the main one last so that
13743 the offsets are available to output_pubnames. */
13744 for (node = limbo_die_list; node; node = node->next)
13745 output_comp_unit (node->die, 0);
13747 output_comp_unit (comp_unit_die, 0);
13749 /* Output the abbreviation table. */
13750 named_section_flags (DEBUG_ABBREV_SECTION, SECTION_DEBUG);
13751 output_abbrev_section ();
13753 /* Output public names table if necessary. */
13754 if (pubname_table_in_use)
13756 named_section_flags (DEBUG_PUBNAMES_SECTION, SECTION_DEBUG);
13757 output_pubnames ();
13760 /* Output the address range information. We only put functions in the arange
13761 table, so don't write it out if we don't have any. */
13762 if (fde_table_in_use)
13764 named_section_flags (DEBUG_ARANGES_SECTION, SECTION_DEBUG);
13765 output_aranges ();
13768 /* Output ranges section if necessary. */
13769 if (ranges_table_in_use)
13771 named_section_flags (DEBUG_RANGES_SECTION, SECTION_DEBUG);
13772 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
13773 output_ranges ();
13776 /* Have to end the macro section. */
13777 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13779 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13780 dw2_asm_output_data (1, 0, "End compilation unit");
13783 /* If we emitted any DW_FORM_strp form attribute, output the string
13784 table too. */
13785 if (debug_str_hash)
13786 htab_traverse (debug_str_hash, output_indirect_string, NULL);
13788 #else
13790 /* This should never be used, but its address is needed for comparisons. */
13791 const struct gcc_debug_hooks dwarf2_debug_hooks;
13793 #endif /* DWARF2_DEBUGGING_INFO */
13795 #include "gt-dwarf2out.h"