Fix a date.
[official-gcc.git] / gcc / dwarf2out.c
blob044a65c4892d0a2fff6a1fa5a340ae87044c0e97
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 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);
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)
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 (!flag_asynchronous_unwind_tables && CALL_P (insn))
1069 /* Extract the size of the args from the CALL rtx itself. */
1070 insn = PATTERN (insn);
1071 if (GET_CODE (insn) == PARALLEL)
1072 insn = XVECEXP (insn, 0, 0);
1073 if (GET_CODE (insn) == SET)
1074 insn = SET_SRC (insn);
1075 gcc_assert (GET_CODE (insn) == CALL);
1077 dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1078 return;
1081 /* If only calls can throw, and we have a frame pointer,
1082 save up adjustments until we see the CALL_INSN. */
1083 else if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1084 return;
1086 if (BARRIER_P (insn))
1088 /* When we see a BARRIER, we know to reset args_size to 0. Usually
1089 the compiler will have already emitted a stack adjustment, but
1090 doesn't bother for calls to noreturn functions. */
1091 #ifdef STACK_GROWS_DOWNWARD
1092 offset = -args_size;
1093 #else
1094 offset = args_size;
1095 #endif
1097 else if (GET_CODE (PATTERN (insn)) == SET)
1098 offset = stack_adjust_offset (PATTERN (insn));
1099 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1100 || GET_CODE (PATTERN (insn)) == SEQUENCE)
1102 /* There may be stack adjustments inside compound insns. Search
1103 for them. */
1104 for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1105 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1106 offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i));
1108 else
1109 return;
1111 if (offset == 0)
1112 return;
1114 if (cfa.reg == STACK_POINTER_REGNUM)
1115 cfa.offset += offset;
1117 #ifndef STACK_GROWS_DOWNWARD
1118 offset = -offset;
1119 #endif
1121 args_size += offset;
1122 if (args_size < 0)
1123 args_size = 0;
1125 label = dwarf2out_cfi_label ();
1126 def_cfa_1 (label, &cfa);
1127 dwarf2out_args_size (label, args_size);
1130 #endif
1132 /* We delay emitting a register save until either (a) we reach the end
1133 of the prologue or (b) the register is clobbered. This clusters
1134 register saves so that there are fewer pc advances. */
1136 struct queued_reg_save GTY(())
1138 struct queued_reg_save *next;
1139 rtx reg;
1140 HOST_WIDE_INT cfa_offset;
1141 rtx saved_reg;
1144 static GTY(()) struct queued_reg_save *queued_reg_saves;
1146 /* The caller's ORIG_REG is saved in SAVED_IN_REG. */
1147 struct reg_saved_in_data GTY(()) {
1148 rtx orig_reg;
1149 rtx saved_in_reg;
1152 /* A list of registers saved in other registers.
1153 The list intentionally has a small maximum capacity of 4; if your
1154 port needs more than that, you might consider implementing a
1155 more efficient data structure. */
1156 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1157 static GTY(()) size_t num_regs_saved_in_regs;
1159 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1160 static const char *last_reg_save_label;
1162 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1163 SREG, or if SREG is NULL then it is saved at OFFSET to the CFA. */
1165 static void
1166 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1168 struct queued_reg_save *q;
1170 /* Duplicates waste space, but it's also necessary to remove them
1171 for correctness, since the queue gets output in reverse
1172 order. */
1173 for (q = queued_reg_saves; q != NULL; q = q->next)
1174 if (REGNO (q->reg) == REGNO (reg))
1175 break;
1177 if (q == NULL)
1179 q = ggc_alloc (sizeof (*q));
1180 q->next = queued_reg_saves;
1181 queued_reg_saves = q;
1184 q->reg = reg;
1185 q->cfa_offset = offset;
1186 q->saved_reg = sreg;
1188 last_reg_save_label = label;
1191 /* Output all the entries in QUEUED_REG_SAVES. */
1193 static void
1194 flush_queued_reg_saves (void)
1196 struct queued_reg_save *q;
1198 for (q = queued_reg_saves; q; q = q->next)
1200 size_t i;
1201 unsigned int reg, sreg;
1203 for (i = 0; i < num_regs_saved_in_regs; i++)
1204 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1205 break;
1206 if (q->saved_reg && i == num_regs_saved_in_regs)
1208 gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1209 num_regs_saved_in_regs++;
1211 if (i != num_regs_saved_in_regs)
1213 regs_saved_in_regs[i].orig_reg = q->reg;
1214 regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1217 reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1218 if (q->saved_reg)
1219 sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1220 else
1221 sreg = INVALID_REGNUM;
1222 reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1225 queued_reg_saves = NULL;
1226 last_reg_save_label = NULL;
1229 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1230 location for? Or, does it clobber a register which we've previously
1231 said that some other register is saved in, and for which we now
1232 have a new location for? */
1234 static bool
1235 clobbers_queued_reg_save (rtx insn)
1237 struct queued_reg_save *q;
1239 for (q = queued_reg_saves; q; q = q->next)
1241 size_t i;
1242 if (modified_in_p (q->reg, insn))
1243 return true;
1244 for (i = 0; i < num_regs_saved_in_regs; i++)
1245 if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1246 && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1247 return true;
1250 return false;
1253 /* What register, if any, is currently saved in REG? */
1255 static rtx
1256 reg_saved_in (rtx reg)
1258 unsigned int regn = REGNO (reg);
1259 size_t i;
1260 struct queued_reg_save *q;
1262 for (q = queued_reg_saves; q; q = q->next)
1263 if (q->saved_reg && regn == REGNO (q->saved_reg))
1264 return q->reg;
1266 for (i = 0; i < num_regs_saved_in_regs; i++)
1267 if (regs_saved_in_regs[i].saved_in_reg
1268 && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1269 return regs_saved_in_regs[i].orig_reg;
1271 return NULL_RTX;
1275 /* A temporary register holding an integral value used in adjusting SP
1276 or setting up the store_reg. The "offset" field holds the integer
1277 value, not an offset. */
1278 static dw_cfa_location cfa_temp;
1280 /* Record call frame debugging information for an expression EXPR,
1281 which either sets SP or FP (adjusting how we calculate the frame
1282 address) or saves a register to the stack or another register.
1283 LABEL indicates the address of EXPR.
1285 This function encodes a state machine mapping rtxes to actions on
1286 cfa, cfa_store, and cfa_temp.reg. We describe these rules so
1287 users need not read the source code.
1289 The High-Level Picture
1291 Changes in the register we use to calculate the CFA: Currently we
1292 assume that if you copy the CFA register into another register, we
1293 should take the other one as the new CFA register; this seems to
1294 work pretty well. If it's wrong for some target, it's simple
1295 enough not to set RTX_FRAME_RELATED_P on the insn in question.
1297 Changes in the register we use for saving registers to the stack:
1298 This is usually SP, but not always. Again, we deduce that if you
1299 copy SP into another register (and SP is not the CFA register),
1300 then the new register is the one we will be using for register
1301 saves. This also seems to work.
1303 Register saves: There's not much guesswork about this one; if
1304 RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1305 register save, and the register used to calculate the destination
1306 had better be the one we think we're using for this purpose.
1307 It's also assumed that a copy from a call-saved register to another
1308 register is saving that register if RTX_FRAME_RELATED_P is set on
1309 that instruction. If the copy is from a call-saved register to
1310 the *same* register, that means that the register is now the same
1311 value as in the caller.
1313 Except: If the register being saved is the CFA register, and the
1314 offset is nonzero, we are saving the CFA, so we assume we have to
1315 use DW_CFA_def_cfa_expression. If the offset is 0, we assume that
1316 the intent is to save the value of SP from the previous frame.
1318 In addition, if a register has previously been saved to a different
1319 register,
1321 Invariants / Summaries of Rules
1323 cfa current rule for calculating the CFA. It usually
1324 consists of a register and an offset.
1325 cfa_store register used by prologue code to save things to the stack
1326 cfa_store.offset is the offset from the value of
1327 cfa_store.reg to the actual CFA
1328 cfa_temp register holding an integral value. cfa_temp.offset
1329 stores the value, which will be used to adjust the
1330 stack pointer. cfa_temp is also used like cfa_store,
1331 to track stores to the stack via fp or a temp reg.
1333 Rules 1- 4: Setting a register's value to cfa.reg or an expression
1334 with cfa.reg as the first operand changes the cfa.reg and its
1335 cfa.offset. Rule 1 and 4 also set cfa_temp.reg and
1336 cfa_temp.offset.
1338 Rules 6- 9: Set a non-cfa.reg register value to a constant or an
1339 expression yielding a constant. This sets cfa_temp.reg
1340 and cfa_temp.offset.
1342 Rule 5: Create a new register cfa_store used to save items to the
1343 stack.
1345 Rules 10-14: Save a register to the stack. Define offset as the
1346 difference of the original location and cfa_store's
1347 location (or cfa_temp's location if cfa_temp is used).
1349 The Rules
1351 "{a,b}" indicates a choice of a xor b.
1352 "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1354 Rule 1:
1355 (set <reg1> <reg2>:cfa.reg)
1356 effects: cfa.reg = <reg1>
1357 cfa.offset unchanged
1358 cfa_temp.reg = <reg1>
1359 cfa_temp.offset = cfa.offset
1361 Rule 2:
1362 (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1363 {<const_int>,<reg>:cfa_temp.reg}))
1364 effects: cfa.reg = sp if fp used
1365 cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
1366 cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1367 if cfa_store.reg==sp
1369 Rule 3:
1370 (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
1371 effects: cfa.reg = fp
1372 cfa_offset += +/- <const_int>
1374 Rule 4:
1375 (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
1376 constraints: <reg1> != fp
1377 <reg1> != sp
1378 effects: cfa.reg = <reg1>
1379 cfa_temp.reg = <reg1>
1380 cfa_temp.offset = cfa.offset
1382 Rule 5:
1383 (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1384 constraints: <reg1> != fp
1385 <reg1> != sp
1386 effects: cfa_store.reg = <reg1>
1387 cfa_store.offset = cfa.offset - cfa_temp.offset
1389 Rule 6:
1390 (set <reg> <const_int>)
1391 effects: cfa_temp.reg = <reg>
1392 cfa_temp.offset = <const_int>
1394 Rule 7:
1395 (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1396 effects: cfa_temp.reg = <reg1>
1397 cfa_temp.offset |= <const_int>
1399 Rule 8:
1400 (set <reg> (high <exp>))
1401 effects: none
1403 Rule 9:
1404 (set <reg> (lo_sum <exp> <const_int>))
1405 effects: cfa_temp.reg = <reg>
1406 cfa_temp.offset = <const_int>
1408 Rule 10:
1409 (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1410 effects: cfa_store.offset -= <const_int>
1411 cfa.offset = cfa_store.offset if cfa.reg == sp
1412 cfa.reg = sp
1413 cfa.base_offset = -cfa_store.offset
1415 Rule 11:
1416 (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1417 effects: cfa_store.offset += -/+ mode_size(mem)
1418 cfa.offset = cfa_store.offset if cfa.reg == sp
1419 cfa.reg = sp
1420 cfa.base_offset = -cfa_store.offset
1422 Rule 12:
1423 (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1425 <reg2>)
1426 effects: cfa.reg = <reg1>
1427 cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
1429 Rule 13:
1430 (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1431 effects: cfa.reg = <reg1>
1432 cfa.base_offset = -{cfa_store,cfa_temp}.offset
1434 Rule 14:
1435 (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
1436 effects: cfa.reg = <reg1>
1437 cfa.base_offset = -cfa_temp.offset
1438 cfa_temp.offset -= mode_size(mem) */
1440 static void
1441 dwarf2out_frame_debug_expr (rtx expr, const char *label)
1443 rtx src, dest;
1444 HOST_WIDE_INT offset;
1446 /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1447 the PARALLEL independently. The first element is always processed if
1448 it is a SET. This is for backward compatibility. Other elements
1449 are processed only if they are SETs and the RTX_FRAME_RELATED_P
1450 flag is set in them. */
1451 if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
1453 int par_index;
1454 int limit = XVECLEN (expr, 0);
1456 for (par_index = 0; par_index < limit; par_index++)
1457 if (GET_CODE (XVECEXP (expr, 0, par_index)) == SET
1458 && (RTX_FRAME_RELATED_P (XVECEXP (expr, 0, par_index))
1459 || par_index == 0))
1460 dwarf2out_frame_debug_expr (XVECEXP (expr, 0, par_index), label);
1462 return;
1465 gcc_assert (GET_CODE (expr) == SET);
1467 src = SET_SRC (expr);
1468 dest = SET_DEST (expr);
1470 if (GET_CODE (src) == REG)
1472 rtx rsi = reg_saved_in (src);
1473 if (rsi)
1474 src = rsi;
1477 switch (GET_CODE (dest))
1479 case REG:
1480 switch (GET_CODE (src))
1482 /* Setting FP from SP. */
1483 case REG:
1484 if (cfa.reg == (unsigned) REGNO (src))
1486 /* Rule 1 */
1487 /* Update the CFA rule wrt SP or FP. Make sure src is
1488 relative to the current CFA register.
1490 We used to require that dest be either SP or FP, but the
1491 ARM copies SP to a temporary register, and from there to
1492 FP. So we just rely on the backends to only set
1493 RTX_FRAME_RELATED_P on appropriate insns. */
1494 cfa.reg = REGNO (dest);
1495 cfa_temp.reg = cfa.reg;
1496 cfa_temp.offset = cfa.offset;
1498 else
1500 /* Saving a register in a register. */
1501 gcc_assert (call_used_regs [REGNO (dest)]
1502 && !fixed_regs [REGNO (dest)]);
1503 queue_reg_save (label, src, dest, 0);
1505 break;
1507 case PLUS:
1508 case MINUS:
1509 case LO_SUM:
1510 if (dest == stack_pointer_rtx)
1512 /* Rule 2 */
1513 /* Adjusting SP. */
1514 switch (GET_CODE (XEXP (src, 1)))
1516 case CONST_INT:
1517 offset = INTVAL (XEXP (src, 1));
1518 break;
1519 case REG:
1520 gcc_assert ((unsigned) REGNO (XEXP (src, 1))
1521 == cfa_temp.reg);
1522 offset = cfa_temp.offset;
1523 break;
1524 default:
1525 gcc_unreachable ();
1528 if (XEXP (src, 0) == hard_frame_pointer_rtx)
1530 /* Restoring SP from FP in the epilogue. */
1531 gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
1532 cfa.reg = STACK_POINTER_REGNUM;
1534 else if (GET_CODE (src) == LO_SUM)
1535 /* Assume we've set the source reg of the LO_SUM from sp. */
1537 else
1538 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
1540 if (GET_CODE (src) != MINUS)
1541 offset = -offset;
1542 if (cfa.reg == STACK_POINTER_REGNUM)
1543 cfa.offset += offset;
1544 if (cfa_store.reg == STACK_POINTER_REGNUM)
1545 cfa_store.offset += offset;
1547 else if (dest == hard_frame_pointer_rtx)
1549 /* Rule 3 */
1550 /* Either setting the FP from an offset of the SP,
1551 or adjusting the FP */
1552 gcc_assert (frame_pointer_needed);
1554 gcc_assert (REG_P (XEXP (src, 0))
1555 && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
1556 && GET_CODE (XEXP (src, 1)) == CONST_INT);
1557 offset = INTVAL (XEXP (src, 1));
1558 if (GET_CODE (src) != MINUS)
1559 offset = -offset;
1560 cfa.offset += offset;
1561 cfa.reg = HARD_FRAME_POINTER_REGNUM;
1563 else
1565 gcc_assert (GET_CODE (src) != MINUS);
1567 /* Rule 4 */
1568 if (REG_P (XEXP (src, 0))
1569 && REGNO (XEXP (src, 0)) == cfa.reg
1570 && GET_CODE (XEXP (src, 1)) == CONST_INT)
1572 /* Setting a temporary CFA register that will be copied
1573 into the FP later on. */
1574 offset = - INTVAL (XEXP (src, 1));
1575 cfa.offset += offset;
1576 cfa.reg = REGNO (dest);
1577 /* Or used to save regs to the stack. */
1578 cfa_temp.reg = cfa.reg;
1579 cfa_temp.offset = cfa.offset;
1582 /* Rule 5 */
1583 else if (REG_P (XEXP (src, 0))
1584 && REGNO (XEXP (src, 0)) == cfa_temp.reg
1585 && XEXP (src, 1) == stack_pointer_rtx)
1587 /* Setting a scratch register that we will use instead
1588 of SP for saving registers to the stack. */
1589 gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
1590 cfa_store.reg = REGNO (dest);
1591 cfa_store.offset = cfa.offset - cfa_temp.offset;
1594 /* Rule 9 */
1595 else if (GET_CODE (src) == LO_SUM
1596 && GET_CODE (XEXP (src, 1)) == CONST_INT)
1598 cfa_temp.reg = REGNO (dest);
1599 cfa_temp.offset = INTVAL (XEXP (src, 1));
1601 else
1602 gcc_unreachable ();
1604 break;
1606 /* Rule 6 */
1607 case CONST_INT:
1608 cfa_temp.reg = REGNO (dest);
1609 cfa_temp.offset = INTVAL (src);
1610 break;
1612 /* Rule 7 */
1613 case IOR:
1614 gcc_assert (REG_P (XEXP (src, 0))
1615 && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
1616 && GET_CODE (XEXP (src, 1)) == CONST_INT);
1618 if ((unsigned) REGNO (dest) != cfa_temp.reg)
1619 cfa_temp.reg = REGNO (dest);
1620 cfa_temp.offset |= INTVAL (XEXP (src, 1));
1621 break;
1623 /* Skip over HIGH, assuming it will be followed by a LO_SUM,
1624 which will fill in all of the bits. */
1625 /* Rule 8 */
1626 case HIGH:
1627 break;
1629 default:
1630 gcc_unreachable ();
1633 def_cfa_1 (label, &cfa);
1634 break;
1636 case MEM:
1637 gcc_assert (REG_P (src));
1639 /* Saving a register to the stack. Make sure dest is relative to the
1640 CFA register. */
1641 switch (GET_CODE (XEXP (dest, 0)))
1643 /* Rule 10 */
1644 /* With a push. */
1645 case PRE_MODIFY:
1646 /* We can't handle variable size modifications. */
1647 gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
1648 == CONST_INT);
1649 offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
1651 gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1652 && cfa_store.reg == STACK_POINTER_REGNUM);
1654 cfa_store.offset += offset;
1655 if (cfa.reg == STACK_POINTER_REGNUM)
1656 cfa.offset = cfa_store.offset;
1658 offset = -cfa_store.offset;
1659 break;
1661 /* Rule 11 */
1662 case PRE_INC:
1663 case PRE_DEC:
1664 offset = GET_MODE_SIZE (GET_MODE (dest));
1665 if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
1666 offset = -offset;
1668 gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1669 && cfa_store.reg == STACK_POINTER_REGNUM);
1671 cfa_store.offset += offset;
1672 if (cfa.reg == STACK_POINTER_REGNUM)
1673 cfa.offset = cfa_store.offset;
1675 offset = -cfa_store.offset;
1676 break;
1678 /* Rule 12 */
1679 /* With an offset. */
1680 case PLUS:
1681 case MINUS:
1682 case LO_SUM:
1684 int regno;
1686 gcc_assert (GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT);
1687 offset = INTVAL (XEXP (XEXP (dest, 0), 1));
1688 if (GET_CODE (XEXP (dest, 0)) == MINUS)
1689 offset = -offset;
1691 regno = REGNO (XEXP (XEXP (dest, 0), 0));
1693 if (cfa_store.reg == (unsigned) regno)
1694 offset -= cfa_store.offset;
1695 else
1697 gcc_assert (cfa_temp.reg == (unsigned) regno);
1698 offset -= cfa_temp.offset;
1701 break;
1703 /* Rule 13 */
1704 /* Without an offset. */
1705 case REG:
1707 int regno = REGNO (XEXP (dest, 0));
1709 if (cfa_store.reg == (unsigned) regno)
1710 offset = -cfa_store.offset;
1711 else
1713 gcc_assert (cfa_temp.reg == (unsigned) regno);
1714 offset = -cfa_temp.offset;
1717 break;
1719 /* Rule 14 */
1720 case POST_INC:
1721 gcc_assert (cfa_temp.reg
1722 == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
1723 offset = -cfa_temp.offset;
1724 cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
1725 break;
1727 default:
1728 gcc_unreachable ();
1731 if (REGNO (src) != STACK_POINTER_REGNUM
1732 && REGNO (src) != HARD_FRAME_POINTER_REGNUM
1733 && (unsigned) REGNO (src) == cfa.reg)
1735 /* We're storing the current CFA reg into the stack. */
1737 if (cfa.offset == 0)
1739 /* If the source register is exactly the CFA, assume
1740 we're saving SP like any other register; this happens
1741 on the ARM. */
1742 def_cfa_1 (label, &cfa);
1743 queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
1744 break;
1746 else
1748 /* Otherwise, we'll need to look in the stack to
1749 calculate the CFA. */
1750 rtx x = XEXP (dest, 0);
1752 if (!REG_P (x))
1753 x = XEXP (x, 0);
1754 gcc_assert (REG_P (x));
1756 cfa.reg = REGNO (x);
1757 cfa.base_offset = offset;
1758 cfa.indirect = 1;
1759 def_cfa_1 (label, &cfa);
1760 break;
1764 def_cfa_1 (label, &cfa);
1765 queue_reg_save (label, src, NULL_RTX, offset);
1766 break;
1768 default:
1769 gcc_unreachable ();
1773 /* Record call frame debugging information for INSN, which either
1774 sets SP or FP (adjusting how we calculate the frame address) or saves a
1775 register to the stack. If INSN is NULL_RTX, initialize our state. */
1777 void
1778 dwarf2out_frame_debug (rtx insn)
1780 const char *label;
1781 rtx src;
1783 if (insn == NULL_RTX)
1785 size_t i;
1787 /* Flush any queued register saves. */
1788 flush_queued_reg_saves ();
1790 /* Set up state for generating call frame debug info. */
1791 lookup_cfa (&cfa);
1792 gcc_assert (cfa.reg
1793 == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
1795 cfa.reg = STACK_POINTER_REGNUM;
1796 cfa_store = cfa;
1797 cfa_temp.reg = -1;
1798 cfa_temp.offset = 0;
1800 for (i = 0; i < num_regs_saved_in_regs; i++)
1802 regs_saved_in_regs[i].orig_reg = NULL_RTX;
1803 regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
1805 num_regs_saved_in_regs = 0;
1806 return;
1809 if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
1810 flush_queued_reg_saves ();
1812 if (! RTX_FRAME_RELATED_P (insn))
1814 if (!ACCUMULATE_OUTGOING_ARGS)
1815 dwarf2out_stack_adjust (insn);
1817 return;
1820 label = dwarf2out_cfi_label ();
1821 src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1822 if (src)
1823 insn = XEXP (src, 0);
1824 else
1825 insn = PATTERN (insn);
1827 dwarf2out_frame_debug_expr (insn, label);
1830 #endif
1832 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
1833 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
1834 (enum dwarf_call_frame_info cfi);
1836 static enum dw_cfi_oprnd_type
1837 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
1839 switch (cfi)
1841 case DW_CFA_nop:
1842 case DW_CFA_GNU_window_save:
1843 return dw_cfi_oprnd_unused;
1845 case DW_CFA_set_loc:
1846 case DW_CFA_advance_loc1:
1847 case DW_CFA_advance_loc2:
1848 case DW_CFA_advance_loc4:
1849 case DW_CFA_MIPS_advance_loc8:
1850 return dw_cfi_oprnd_addr;
1852 case DW_CFA_offset:
1853 case DW_CFA_offset_extended:
1854 case DW_CFA_def_cfa:
1855 case DW_CFA_offset_extended_sf:
1856 case DW_CFA_def_cfa_sf:
1857 case DW_CFA_restore_extended:
1858 case DW_CFA_undefined:
1859 case DW_CFA_same_value:
1860 case DW_CFA_def_cfa_register:
1861 case DW_CFA_register:
1862 return dw_cfi_oprnd_reg_num;
1864 case DW_CFA_def_cfa_offset:
1865 case DW_CFA_GNU_args_size:
1866 case DW_CFA_def_cfa_offset_sf:
1867 return dw_cfi_oprnd_offset;
1869 case DW_CFA_def_cfa_expression:
1870 case DW_CFA_expression:
1871 return dw_cfi_oprnd_loc;
1873 default:
1874 gcc_unreachable ();
1878 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
1879 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
1880 (enum dwarf_call_frame_info cfi);
1882 static enum dw_cfi_oprnd_type
1883 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
1885 switch (cfi)
1887 case DW_CFA_def_cfa:
1888 case DW_CFA_def_cfa_sf:
1889 case DW_CFA_offset:
1890 case DW_CFA_offset_extended_sf:
1891 case DW_CFA_offset_extended:
1892 return dw_cfi_oprnd_offset;
1894 case DW_CFA_register:
1895 return dw_cfi_oprnd_reg_num;
1897 default:
1898 return dw_cfi_oprnd_unused;
1902 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1904 /* Map register numbers held in the call frame info that gcc has
1905 collected using DWARF_FRAME_REGNUM to those that should be output in
1906 .debug_frame and .eh_frame. */
1907 #ifndef DWARF2_FRAME_REG_OUT
1908 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
1909 #endif
1911 /* Output a Call Frame Information opcode and its operand(s). */
1913 static void
1914 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
1916 unsigned long r;
1917 if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
1918 dw2_asm_output_data (1, (cfi->dw_cfi_opc
1919 | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
1920 "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
1921 cfi->dw_cfi_oprnd1.dw_cfi_offset);
1922 else if (cfi->dw_cfi_opc == DW_CFA_offset)
1924 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
1925 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
1926 "DW_CFA_offset, column 0x%lx", r);
1927 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
1929 else if (cfi->dw_cfi_opc == DW_CFA_restore)
1931 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
1932 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
1933 "DW_CFA_restore, column 0x%lx", r);
1935 else
1937 dw2_asm_output_data (1, cfi->dw_cfi_opc,
1938 "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
1940 switch (cfi->dw_cfi_opc)
1942 case DW_CFA_set_loc:
1943 if (for_eh)
1944 dw2_asm_output_encoded_addr_rtx (
1945 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
1946 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
1947 NULL);
1948 else
1949 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
1950 cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
1951 break;
1953 case DW_CFA_advance_loc1:
1954 dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
1955 fde->dw_fde_current_label, NULL);
1956 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
1957 break;
1959 case DW_CFA_advance_loc2:
1960 dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
1961 fde->dw_fde_current_label, NULL);
1962 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
1963 break;
1965 case DW_CFA_advance_loc4:
1966 dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
1967 fde->dw_fde_current_label, NULL);
1968 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
1969 break;
1971 case DW_CFA_MIPS_advance_loc8:
1972 dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
1973 fde->dw_fde_current_label, NULL);
1974 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
1975 break;
1977 case DW_CFA_offset_extended:
1978 case DW_CFA_def_cfa:
1979 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
1980 dw2_asm_output_data_uleb128 (r, NULL);
1981 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
1982 break;
1984 case DW_CFA_offset_extended_sf:
1985 case DW_CFA_def_cfa_sf:
1986 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
1987 dw2_asm_output_data_uleb128 (r, NULL);
1988 dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
1989 break;
1991 case DW_CFA_restore_extended:
1992 case DW_CFA_undefined:
1993 case DW_CFA_same_value:
1994 case DW_CFA_def_cfa_register:
1995 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
1996 dw2_asm_output_data_uleb128 (r, NULL);
1997 break;
1999 case DW_CFA_register:
2000 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2001 dw2_asm_output_data_uleb128 (r, NULL);
2002 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
2003 dw2_asm_output_data_uleb128 (r, NULL);
2004 break;
2006 case DW_CFA_def_cfa_offset:
2007 case DW_CFA_GNU_args_size:
2008 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2009 break;
2011 case DW_CFA_def_cfa_offset_sf:
2012 dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2013 break;
2015 case DW_CFA_GNU_window_save:
2016 break;
2018 case DW_CFA_def_cfa_expression:
2019 case DW_CFA_expression:
2020 output_cfa_loc (cfi);
2021 break;
2023 case DW_CFA_GNU_negative_offset_extended:
2024 /* Obsoleted by DW_CFA_offset_extended_sf. */
2025 gcc_unreachable ();
2027 default:
2028 break;
2033 /* Output the call frame information used to record information
2034 that relates to calculating the frame pointer, and records the
2035 location of saved registers. */
2037 static void
2038 output_call_frame_info (int for_eh)
2040 unsigned int i;
2041 dw_fde_ref fde;
2042 dw_cfi_ref cfi;
2043 char l1[20], l2[20], section_start_label[20];
2044 bool any_lsda_needed = false;
2045 char augmentation[6];
2046 int augmentation_size;
2047 int fde_encoding = DW_EH_PE_absptr;
2048 int per_encoding = DW_EH_PE_absptr;
2049 int lsda_encoding = DW_EH_PE_absptr;
2051 /* Don't emit a CIE if there won't be any FDEs. */
2052 if (fde_table_in_use == 0)
2053 return;
2055 /* If we make FDEs linkonce, we may have to emit an empty label for
2056 an FDE that wouldn't otherwise be emitted. We want to avoid
2057 having an FDE kept around when the function it refers to is
2058 discarded. Example where this matters: a primary function
2059 template in C++ requires EH information, but an explicit
2060 specialization doesn't. */
2061 if (TARGET_USES_WEAK_UNWIND_INFO
2062 && ! flag_asynchronous_unwind_tables
2063 && for_eh)
2064 for (i = 0; i < fde_table_in_use; i++)
2065 if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
2066 && !fde_table[i].uses_eh_lsda
2067 && ! DECL_WEAK (fde_table[i].decl))
2068 targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
2069 for_eh, /* empty */ 1);
2071 /* If we don't have any functions we'll want to unwind out of, don't
2072 emit any EH unwind information. Note that if exceptions aren't
2073 enabled, we won't have collected nothrow information, and if we
2074 asked for asynchronous tables, we always want this info. */
2075 if (for_eh)
2077 bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
2079 for (i = 0; i < fde_table_in_use; i++)
2080 if (fde_table[i].uses_eh_lsda)
2081 any_eh_needed = any_lsda_needed = true;
2082 else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2083 any_eh_needed = true;
2084 else if (! fde_table[i].nothrow
2085 && ! fde_table[i].all_throwers_are_sibcalls)
2086 any_eh_needed = true;
2088 if (! any_eh_needed)
2089 return;
2092 /* We're going to be generating comments, so turn on app. */
2093 if (flag_debug_asm)
2094 app_enable ();
2096 if (for_eh)
2097 targetm.asm_out.eh_frame_section ();
2098 else
2099 named_section_flags (DEBUG_FRAME_SECTION, SECTION_DEBUG);
2101 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
2102 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
2104 /* Output the CIE. */
2105 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
2106 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
2107 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2108 "Length of Common Information Entry");
2109 ASM_OUTPUT_LABEL (asm_out_file, l1);
2111 /* Now that the CIE pointer is PC-relative for EH,
2112 use 0 to identify the CIE. */
2113 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
2114 (for_eh ? 0 : DW_CIE_ID),
2115 "CIE Identifier Tag");
2117 dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
2119 augmentation[0] = 0;
2120 augmentation_size = 0;
2121 if (for_eh)
2123 char *p;
2125 /* Augmentation:
2126 z Indicates that a uleb128 is present to size the
2127 augmentation section.
2128 L Indicates the encoding (and thus presence) of
2129 an LSDA pointer in the FDE augmentation.
2130 R Indicates a non-default pointer encoding for
2131 FDE code pointers.
2132 P Indicates the presence of an encoding + language
2133 personality routine in the CIE augmentation. */
2135 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
2136 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
2137 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
2139 p = augmentation + 1;
2140 if (eh_personality_libfunc)
2142 *p++ = 'P';
2143 augmentation_size += 1 + size_of_encoded_value (per_encoding);
2145 if (any_lsda_needed)
2147 *p++ = 'L';
2148 augmentation_size += 1;
2150 if (fde_encoding != DW_EH_PE_absptr)
2152 *p++ = 'R';
2153 augmentation_size += 1;
2155 if (p > augmentation + 1)
2157 augmentation[0] = 'z';
2158 *p = '\0';
2161 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
2162 if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
2164 int offset = ( 4 /* Length */
2165 + 4 /* CIE Id */
2166 + 1 /* CIE version */
2167 + strlen (augmentation) + 1 /* Augmentation */
2168 + size_of_uleb128 (1) /* Code alignment */
2169 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
2170 + 1 /* RA column */
2171 + 1 /* Augmentation size */
2172 + 1 /* Personality encoding */ );
2173 int pad = -offset & (PTR_SIZE - 1);
2175 augmentation_size += pad;
2177 /* Augmentations should be small, so there's scarce need to
2178 iterate for a solution. Die if we exceed one uleb128 byte. */
2179 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
2183 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
2184 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
2185 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
2186 "CIE Data Alignment Factor");
2188 if (DW_CIE_VERSION == 1)
2189 dw2_asm_output_data (1, DWARF_FRAME_RETURN_COLUMN, "CIE RA Column");
2190 else
2191 dw2_asm_output_data_uleb128 (DWARF_FRAME_RETURN_COLUMN, "CIE RA Column");
2193 if (augmentation[0])
2195 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
2196 if (eh_personality_libfunc)
2198 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
2199 eh_data_format_name (per_encoding));
2200 dw2_asm_output_encoded_addr_rtx (per_encoding,
2201 eh_personality_libfunc, NULL);
2204 if (any_lsda_needed)
2205 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
2206 eh_data_format_name (lsda_encoding));
2208 if (fde_encoding != DW_EH_PE_absptr)
2209 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
2210 eh_data_format_name (fde_encoding));
2213 for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
2214 output_cfi (cfi, NULL, for_eh);
2216 /* Pad the CIE out to an address sized boundary. */
2217 ASM_OUTPUT_ALIGN (asm_out_file,
2218 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
2219 ASM_OUTPUT_LABEL (asm_out_file, l2);
2221 /* Loop through all of the FDE's. */
2222 for (i = 0; i < fde_table_in_use; i++)
2224 fde = &fde_table[i];
2226 /* Don't emit EH unwind info for leaf functions that don't need it. */
2227 if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
2228 && (fde->nothrow || fde->all_throwers_are_sibcalls)
2229 && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2230 && !fde->uses_eh_lsda)
2231 continue;
2233 targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
2234 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
2235 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
2236 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
2237 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2238 "FDE Length");
2239 ASM_OUTPUT_LABEL (asm_out_file, l1);
2241 if (for_eh)
2242 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
2243 else
2244 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
2245 "FDE CIE offset");
2247 if (for_eh)
2249 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
2250 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
2251 dw2_asm_output_encoded_addr_rtx (fde_encoding,
2252 sym_ref,
2253 "FDE initial location");
2254 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2255 fde->dw_fde_end, fde->dw_fde_begin,
2256 "FDE address range");
2258 else
2260 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
2261 "FDE initial location");
2262 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2263 fde->dw_fde_end, fde->dw_fde_begin,
2264 "FDE address range");
2267 if (augmentation[0])
2269 if (any_lsda_needed)
2271 int size = size_of_encoded_value (lsda_encoding);
2273 if (lsda_encoding == DW_EH_PE_aligned)
2275 int offset = ( 4 /* Length */
2276 + 4 /* CIE offset */
2277 + 2 * size_of_encoded_value (fde_encoding)
2278 + 1 /* Augmentation size */ );
2279 int pad = -offset & (PTR_SIZE - 1);
2281 size += pad;
2282 gcc_assert (size_of_uleb128 (size) == 1);
2285 dw2_asm_output_data_uleb128 (size, "Augmentation size");
2287 if (fde->uses_eh_lsda)
2289 ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
2290 fde->funcdef_number);
2291 dw2_asm_output_encoded_addr_rtx (
2292 lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
2293 "Language Specific Data Area");
2295 else
2297 if (lsda_encoding == DW_EH_PE_aligned)
2298 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2299 dw2_asm_output_data
2300 (size_of_encoded_value (lsda_encoding), 0,
2301 "Language Specific Data Area (none)");
2304 else
2305 dw2_asm_output_data_uleb128 (0, "Augmentation size");
2308 /* Loop through the Call Frame Instructions associated with
2309 this FDE. */
2310 fde->dw_fde_current_label = fde->dw_fde_begin;
2311 for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
2312 output_cfi (cfi, fde, for_eh);
2314 /* Pad the FDE out to an address sized boundary. */
2315 ASM_OUTPUT_ALIGN (asm_out_file,
2316 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
2317 ASM_OUTPUT_LABEL (asm_out_file, l2);
2320 if (for_eh && targetm.terminate_dw2_eh_frame_info)
2321 dw2_asm_output_data (4, 0, "End of Table");
2322 #ifdef MIPS_DEBUGGING_INFO
2323 /* Work around Irix 6 assembler bug whereby labels at the end of a section
2324 get a value of 0. Putting .align 0 after the label fixes it. */
2325 ASM_OUTPUT_ALIGN (asm_out_file, 0);
2326 #endif
2328 /* Turn off app to make assembly quicker. */
2329 if (flag_debug_asm)
2330 app_disable ();
2333 /* Output a marker (i.e. a label) for the beginning of a function, before
2334 the prologue. */
2336 void
2337 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
2338 const char *file ATTRIBUTE_UNUSED)
2340 char label[MAX_ARTIFICIAL_LABEL_BYTES];
2341 char * dup_label;
2342 dw_fde_ref fde;
2344 current_function_func_begin_label = NULL;
2346 #ifdef TARGET_UNWIND_INFO
2347 /* ??? current_function_func_begin_label is also used by except.c
2348 for call-site information. We must emit this label if it might
2349 be used. */
2350 if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
2351 && ! dwarf2out_do_frame ())
2352 return;
2353 #else
2354 if (! dwarf2out_do_frame ())
2355 return;
2356 #endif
2358 function_section (current_function_decl);
2359 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
2360 current_function_funcdef_no);
2361 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
2362 current_function_funcdef_no);
2363 dup_label = xstrdup (label);
2364 current_function_func_begin_label = dup_label;
2366 #ifdef TARGET_UNWIND_INFO
2367 /* We can elide the fde allocation if we're not emitting debug info. */
2368 if (! dwarf2out_do_frame ())
2369 return;
2370 #endif
2372 /* Expand the fde table if necessary. */
2373 if (fde_table_in_use == fde_table_allocated)
2375 fde_table_allocated += FDE_TABLE_INCREMENT;
2376 fde_table = ggc_realloc (fde_table,
2377 fde_table_allocated * sizeof (dw_fde_node));
2378 memset (fde_table + fde_table_in_use, 0,
2379 FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2382 /* Record the FDE associated with this function. */
2383 current_funcdef_fde = fde_table_in_use;
2385 /* Add the new FDE at the end of the fde_table. */
2386 fde = &fde_table[fde_table_in_use++];
2387 fde->decl = current_function_decl;
2388 fde->dw_fde_begin = dup_label;
2389 fde->dw_fde_current_label = NULL;
2390 fde->dw_fde_end = NULL;
2391 fde->dw_fde_cfi = NULL;
2392 fde->funcdef_number = current_function_funcdef_no;
2393 fde->nothrow = TREE_NOTHROW (current_function_decl);
2394 fde->uses_eh_lsda = cfun->uses_eh_lsda;
2395 fde->all_throwers_are_sibcalls = cfun->all_throwers_are_sibcalls;
2397 args_size = old_args_size = 0;
2399 /* We only want to output line number information for the genuine dwarf2
2400 prologue case, not the eh frame case. */
2401 #ifdef DWARF2_DEBUGGING_INFO
2402 if (file)
2403 dwarf2out_source_line (line, file);
2404 #endif
2407 /* Output a marker (i.e. a label) for the absolute end of the generated code
2408 for a function definition. This gets called *after* the epilogue code has
2409 been generated. */
2411 void
2412 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
2413 const char *file ATTRIBUTE_UNUSED)
2415 dw_fde_ref fde;
2416 char label[MAX_ARTIFICIAL_LABEL_BYTES];
2418 /* Output a label to mark the endpoint of the code generated for this
2419 function. */
2420 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
2421 current_function_funcdef_no);
2422 ASM_OUTPUT_LABEL (asm_out_file, label);
2423 fde = &fde_table[fde_table_in_use - 1];
2424 fde->dw_fde_end = xstrdup (label);
2427 void
2428 dwarf2out_frame_init (void)
2430 /* Allocate the initial hunk of the fde_table. */
2431 fde_table = ggc_alloc_cleared (FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2432 fde_table_allocated = FDE_TABLE_INCREMENT;
2433 fde_table_in_use = 0;
2435 /* Generate the CFA instructions common to all FDE's. Do it now for the
2436 sake of lookup_cfa. */
2438 #ifdef DWARF2_UNWIND_INFO
2439 /* On entry, the Canonical Frame Address is at SP. */
2440 dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
2441 initial_return_save (INCOMING_RETURN_ADDR_RTX);
2442 #endif
2445 void
2446 dwarf2out_frame_finish (void)
2448 /* Output call frame information. */
2449 if (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
2450 output_call_frame_info (0);
2452 #ifndef TARGET_UNWIND_INFO
2453 /* Output another copy for the unwinder. */
2454 if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
2455 output_call_frame_info (1);
2456 #endif
2458 #endif
2460 /* And now, the subset of the debugging information support code necessary
2461 for emitting location expressions. */
2463 /* We need some way to distinguish DW_OP_addr with a direct symbol
2464 relocation from DW_OP_addr with a dtp-relative symbol relocation. */
2465 #define INTERNAL_DW_OP_tls_addr (0x100 + DW_OP_addr)
2468 typedef struct dw_val_struct *dw_val_ref;
2469 typedef struct die_struct *dw_die_ref;
2470 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
2471 typedef struct dw_loc_list_struct *dw_loc_list_ref;
2473 /* Each DIE may have a series of attribute/value pairs. Values
2474 can take on several forms. The forms that are used in this
2475 implementation are listed below. */
2477 enum dw_val_class
2479 dw_val_class_addr,
2480 dw_val_class_offset,
2481 dw_val_class_loc,
2482 dw_val_class_loc_list,
2483 dw_val_class_range_list,
2484 dw_val_class_const,
2485 dw_val_class_unsigned_const,
2486 dw_val_class_long_long,
2487 dw_val_class_vec,
2488 dw_val_class_flag,
2489 dw_val_class_die_ref,
2490 dw_val_class_fde_ref,
2491 dw_val_class_lbl_id,
2492 dw_val_class_lbl_offset,
2493 dw_val_class_str
2496 /* Describe a double word constant value. */
2497 /* ??? Every instance of long_long in the code really means CONST_DOUBLE. */
2499 typedef struct dw_long_long_struct GTY(())
2501 unsigned long hi;
2502 unsigned long low;
2504 dw_long_long_const;
2506 /* Describe a floating point constant value, or a vector constant value. */
2508 typedef struct dw_vec_struct GTY(())
2510 unsigned char * GTY((length ("%h.length"))) array;
2511 unsigned length;
2512 unsigned elt_size;
2514 dw_vec_const;
2516 /* The dw_val_node describes an attribute's value, as it is
2517 represented internally. */
2519 typedef struct dw_val_struct GTY(())
2521 enum dw_val_class val_class;
2522 union dw_val_struct_union
2524 rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
2525 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
2526 dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
2527 dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
2528 HOST_WIDE_INT GTY ((default)) val_int;
2529 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
2530 dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
2531 dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
2532 struct dw_val_die_union
2534 dw_die_ref die;
2535 int external;
2536 } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
2537 unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
2538 struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
2539 char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
2540 unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
2542 GTY ((desc ("%1.val_class"))) v;
2544 dw_val_node;
2546 /* Locations in memory are described using a sequence of stack machine
2547 operations. */
2549 typedef struct dw_loc_descr_struct GTY(())
2551 dw_loc_descr_ref dw_loc_next;
2552 enum dwarf_location_atom dw_loc_opc;
2553 dw_val_node dw_loc_oprnd1;
2554 dw_val_node dw_loc_oprnd2;
2555 int dw_loc_addr;
2557 dw_loc_descr_node;
2559 /* Location lists are ranges + location descriptions for that range,
2560 so you can track variables that are in different places over
2561 their entire life. */
2562 typedef struct dw_loc_list_struct GTY(())
2564 dw_loc_list_ref dw_loc_next;
2565 const char *begin; /* Label for begin address of range */
2566 const char *end; /* Label for end address of range */
2567 char *ll_symbol; /* Label for beginning of location list.
2568 Only on head of list */
2569 const char *section; /* Section this loclist is relative to */
2570 dw_loc_descr_ref expr;
2571 } dw_loc_list_node;
2573 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2575 static const char *dwarf_stack_op_name (unsigned);
2576 static dw_loc_descr_ref new_loc_descr (enum dwarf_location_atom,
2577 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2578 static void add_loc_descr (dw_loc_descr_ref *, dw_loc_descr_ref);
2579 static unsigned long size_of_loc_descr (dw_loc_descr_ref);
2580 static unsigned long size_of_locs (dw_loc_descr_ref);
2581 static void output_loc_operands (dw_loc_descr_ref);
2582 static void output_loc_sequence (dw_loc_descr_ref);
2584 /* Convert a DWARF stack opcode into its string name. */
2586 static const char *
2587 dwarf_stack_op_name (unsigned int op)
2589 switch (op)
2591 case DW_OP_addr:
2592 case INTERNAL_DW_OP_tls_addr:
2593 return "DW_OP_addr";
2594 case DW_OP_deref:
2595 return "DW_OP_deref";
2596 case DW_OP_const1u:
2597 return "DW_OP_const1u";
2598 case DW_OP_const1s:
2599 return "DW_OP_const1s";
2600 case DW_OP_const2u:
2601 return "DW_OP_const2u";
2602 case DW_OP_const2s:
2603 return "DW_OP_const2s";
2604 case DW_OP_const4u:
2605 return "DW_OP_const4u";
2606 case DW_OP_const4s:
2607 return "DW_OP_const4s";
2608 case DW_OP_const8u:
2609 return "DW_OP_const8u";
2610 case DW_OP_const8s:
2611 return "DW_OP_const8s";
2612 case DW_OP_constu:
2613 return "DW_OP_constu";
2614 case DW_OP_consts:
2615 return "DW_OP_consts";
2616 case DW_OP_dup:
2617 return "DW_OP_dup";
2618 case DW_OP_drop:
2619 return "DW_OP_drop";
2620 case DW_OP_over:
2621 return "DW_OP_over";
2622 case DW_OP_pick:
2623 return "DW_OP_pick";
2624 case DW_OP_swap:
2625 return "DW_OP_swap";
2626 case DW_OP_rot:
2627 return "DW_OP_rot";
2628 case DW_OP_xderef:
2629 return "DW_OP_xderef";
2630 case DW_OP_abs:
2631 return "DW_OP_abs";
2632 case DW_OP_and:
2633 return "DW_OP_and";
2634 case DW_OP_div:
2635 return "DW_OP_div";
2636 case DW_OP_minus:
2637 return "DW_OP_minus";
2638 case DW_OP_mod:
2639 return "DW_OP_mod";
2640 case DW_OP_mul:
2641 return "DW_OP_mul";
2642 case DW_OP_neg:
2643 return "DW_OP_neg";
2644 case DW_OP_not:
2645 return "DW_OP_not";
2646 case DW_OP_or:
2647 return "DW_OP_or";
2648 case DW_OP_plus:
2649 return "DW_OP_plus";
2650 case DW_OP_plus_uconst:
2651 return "DW_OP_plus_uconst";
2652 case DW_OP_shl:
2653 return "DW_OP_shl";
2654 case DW_OP_shr:
2655 return "DW_OP_shr";
2656 case DW_OP_shra:
2657 return "DW_OP_shra";
2658 case DW_OP_xor:
2659 return "DW_OP_xor";
2660 case DW_OP_bra:
2661 return "DW_OP_bra";
2662 case DW_OP_eq:
2663 return "DW_OP_eq";
2664 case DW_OP_ge:
2665 return "DW_OP_ge";
2666 case DW_OP_gt:
2667 return "DW_OP_gt";
2668 case DW_OP_le:
2669 return "DW_OP_le";
2670 case DW_OP_lt:
2671 return "DW_OP_lt";
2672 case DW_OP_ne:
2673 return "DW_OP_ne";
2674 case DW_OP_skip:
2675 return "DW_OP_skip";
2676 case DW_OP_lit0:
2677 return "DW_OP_lit0";
2678 case DW_OP_lit1:
2679 return "DW_OP_lit1";
2680 case DW_OP_lit2:
2681 return "DW_OP_lit2";
2682 case DW_OP_lit3:
2683 return "DW_OP_lit3";
2684 case DW_OP_lit4:
2685 return "DW_OP_lit4";
2686 case DW_OP_lit5:
2687 return "DW_OP_lit5";
2688 case DW_OP_lit6:
2689 return "DW_OP_lit6";
2690 case DW_OP_lit7:
2691 return "DW_OP_lit7";
2692 case DW_OP_lit8:
2693 return "DW_OP_lit8";
2694 case DW_OP_lit9:
2695 return "DW_OP_lit9";
2696 case DW_OP_lit10:
2697 return "DW_OP_lit10";
2698 case DW_OP_lit11:
2699 return "DW_OP_lit11";
2700 case DW_OP_lit12:
2701 return "DW_OP_lit12";
2702 case DW_OP_lit13:
2703 return "DW_OP_lit13";
2704 case DW_OP_lit14:
2705 return "DW_OP_lit14";
2706 case DW_OP_lit15:
2707 return "DW_OP_lit15";
2708 case DW_OP_lit16:
2709 return "DW_OP_lit16";
2710 case DW_OP_lit17:
2711 return "DW_OP_lit17";
2712 case DW_OP_lit18:
2713 return "DW_OP_lit18";
2714 case DW_OP_lit19:
2715 return "DW_OP_lit19";
2716 case DW_OP_lit20:
2717 return "DW_OP_lit20";
2718 case DW_OP_lit21:
2719 return "DW_OP_lit21";
2720 case DW_OP_lit22:
2721 return "DW_OP_lit22";
2722 case DW_OP_lit23:
2723 return "DW_OP_lit23";
2724 case DW_OP_lit24:
2725 return "DW_OP_lit24";
2726 case DW_OP_lit25:
2727 return "DW_OP_lit25";
2728 case DW_OP_lit26:
2729 return "DW_OP_lit26";
2730 case DW_OP_lit27:
2731 return "DW_OP_lit27";
2732 case DW_OP_lit28:
2733 return "DW_OP_lit28";
2734 case DW_OP_lit29:
2735 return "DW_OP_lit29";
2736 case DW_OP_lit30:
2737 return "DW_OP_lit30";
2738 case DW_OP_lit31:
2739 return "DW_OP_lit31";
2740 case DW_OP_reg0:
2741 return "DW_OP_reg0";
2742 case DW_OP_reg1:
2743 return "DW_OP_reg1";
2744 case DW_OP_reg2:
2745 return "DW_OP_reg2";
2746 case DW_OP_reg3:
2747 return "DW_OP_reg3";
2748 case DW_OP_reg4:
2749 return "DW_OP_reg4";
2750 case DW_OP_reg5:
2751 return "DW_OP_reg5";
2752 case DW_OP_reg6:
2753 return "DW_OP_reg6";
2754 case DW_OP_reg7:
2755 return "DW_OP_reg7";
2756 case DW_OP_reg8:
2757 return "DW_OP_reg8";
2758 case DW_OP_reg9:
2759 return "DW_OP_reg9";
2760 case DW_OP_reg10:
2761 return "DW_OP_reg10";
2762 case DW_OP_reg11:
2763 return "DW_OP_reg11";
2764 case DW_OP_reg12:
2765 return "DW_OP_reg12";
2766 case DW_OP_reg13:
2767 return "DW_OP_reg13";
2768 case DW_OP_reg14:
2769 return "DW_OP_reg14";
2770 case DW_OP_reg15:
2771 return "DW_OP_reg15";
2772 case DW_OP_reg16:
2773 return "DW_OP_reg16";
2774 case DW_OP_reg17:
2775 return "DW_OP_reg17";
2776 case DW_OP_reg18:
2777 return "DW_OP_reg18";
2778 case DW_OP_reg19:
2779 return "DW_OP_reg19";
2780 case DW_OP_reg20:
2781 return "DW_OP_reg20";
2782 case DW_OP_reg21:
2783 return "DW_OP_reg21";
2784 case DW_OP_reg22:
2785 return "DW_OP_reg22";
2786 case DW_OP_reg23:
2787 return "DW_OP_reg23";
2788 case DW_OP_reg24:
2789 return "DW_OP_reg24";
2790 case DW_OP_reg25:
2791 return "DW_OP_reg25";
2792 case DW_OP_reg26:
2793 return "DW_OP_reg26";
2794 case DW_OP_reg27:
2795 return "DW_OP_reg27";
2796 case DW_OP_reg28:
2797 return "DW_OP_reg28";
2798 case DW_OP_reg29:
2799 return "DW_OP_reg29";
2800 case DW_OP_reg30:
2801 return "DW_OP_reg30";
2802 case DW_OP_reg31:
2803 return "DW_OP_reg31";
2804 case DW_OP_breg0:
2805 return "DW_OP_breg0";
2806 case DW_OP_breg1:
2807 return "DW_OP_breg1";
2808 case DW_OP_breg2:
2809 return "DW_OP_breg2";
2810 case DW_OP_breg3:
2811 return "DW_OP_breg3";
2812 case DW_OP_breg4:
2813 return "DW_OP_breg4";
2814 case DW_OP_breg5:
2815 return "DW_OP_breg5";
2816 case DW_OP_breg6:
2817 return "DW_OP_breg6";
2818 case DW_OP_breg7:
2819 return "DW_OP_breg7";
2820 case DW_OP_breg8:
2821 return "DW_OP_breg8";
2822 case DW_OP_breg9:
2823 return "DW_OP_breg9";
2824 case DW_OP_breg10:
2825 return "DW_OP_breg10";
2826 case DW_OP_breg11:
2827 return "DW_OP_breg11";
2828 case DW_OP_breg12:
2829 return "DW_OP_breg12";
2830 case DW_OP_breg13:
2831 return "DW_OP_breg13";
2832 case DW_OP_breg14:
2833 return "DW_OP_breg14";
2834 case DW_OP_breg15:
2835 return "DW_OP_breg15";
2836 case DW_OP_breg16:
2837 return "DW_OP_breg16";
2838 case DW_OP_breg17:
2839 return "DW_OP_breg17";
2840 case DW_OP_breg18:
2841 return "DW_OP_breg18";
2842 case DW_OP_breg19:
2843 return "DW_OP_breg19";
2844 case DW_OP_breg20:
2845 return "DW_OP_breg20";
2846 case DW_OP_breg21:
2847 return "DW_OP_breg21";
2848 case DW_OP_breg22:
2849 return "DW_OP_breg22";
2850 case DW_OP_breg23:
2851 return "DW_OP_breg23";
2852 case DW_OP_breg24:
2853 return "DW_OP_breg24";
2854 case DW_OP_breg25:
2855 return "DW_OP_breg25";
2856 case DW_OP_breg26:
2857 return "DW_OP_breg26";
2858 case DW_OP_breg27:
2859 return "DW_OP_breg27";
2860 case DW_OP_breg28:
2861 return "DW_OP_breg28";
2862 case DW_OP_breg29:
2863 return "DW_OP_breg29";
2864 case DW_OP_breg30:
2865 return "DW_OP_breg30";
2866 case DW_OP_breg31:
2867 return "DW_OP_breg31";
2868 case DW_OP_regx:
2869 return "DW_OP_regx";
2870 case DW_OP_fbreg:
2871 return "DW_OP_fbreg";
2872 case DW_OP_bregx:
2873 return "DW_OP_bregx";
2874 case DW_OP_piece:
2875 return "DW_OP_piece";
2876 case DW_OP_deref_size:
2877 return "DW_OP_deref_size";
2878 case DW_OP_xderef_size:
2879 return "DW_OP_xderef_size";
2880 case DW_OP_nop:
2881 return "DW_OP_nop";
2882 case DW_OP_push_object_address:
2883 return "DW_OP_push_object_address";
2884 case DW_OP_call2:
2885 return "DW_OP_call2";
2886 case DW_OP_call4:
2887 return "DW_OP_call4";
2888 case DW_OP_call_ref:
2889 return "DW_OP_call_ref";
2890 case DW_OP_GNU_push_tls_address:
2891 return "DW_OP_GNU_push_tls_address";
2892 default:
2893 return "OP_<unknown>";
2897 /* Return a pointer to a newly allocated location description. Location
2898 descriptions are simple expression terms that can be strung
2899 together to form more complicated location (address) descriptions. */
2901 static inline dw_loc_descr_ref
2902 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
2903 unsigned HOST_WIDE_INT oprnd2)
2905 dw_loc_descr_ref descr = ggc_alloc_cleared (sizeof (dw_loc_descr_node));
2907 descr->dw_loc_opc = op;
2908 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
2909 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
2910 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
2911 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
2913 return descr;
2917 /* Add a location description term to a location description expression. */
2919 static inline void
2920 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
2922 dw_loc_descr_ref *d;
2924 /* Find the end of the chain. */
2925 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
2928 *d = descr;
2931 /* Return the size of a location descriptor. */
2933 static unsigned long
2934 size_of_loc_descr (dw_loc_descr_ref loc)
2936 unsigned long size = 1;
2938 switch (loc->dw_loc_opc)
2940 case DW_OP_addr:
2941 case INTERNAL_DW_OP_tls_addr:
2942 size += DWARF2_ADDR_SIZE;
2943 break;
2944 case DW_OP_const1u:
2945 case DW_OP_const1s:
2946 size += 1;
2947 break;
2948 case DW_OP_const2u:
2949 case DW_OP_const2s:
2950 size += 2;
2951 break;
2952 case DW_OP_const4u:
2953 case DW_OP_const4s:
2954 size += 4;
2955 break;
2956 case DW_OP_const8u:
2957 case DW_OP_const8s:
2958 size += 8;
2959 break;
2960 case DW_OP_constu:
2961 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2962 break;
2963 case DW_OP_consts:
2964 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
2965 break;
2966 case DW_OP_pick:
2967 size += 1;
2968 break;
2969 case DW_OP_plus_uconst:
2970 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2971 break;
2972 case DW_OP_skip:
2973 case DW_OP_bra:
2974 size += 2;
2975 break;
2976 case DW_OP_breg0:
2977 case DW_OP_breg1:
2978 case DW_OP_breg2:
2979 case DW_OP_breg3:
2980 case DW_OP_breg4:
2981 case DW_OP_breg5:
2982 case DW_OP_breg6:
2983 case DW_OP_breg7:
2984 case DW_OP_breg8:
2985 case DW_OP_breg9:
2986 case DW_OP_breg10:
2987 case DW_OP_breg11:
2988 case DW_OP_breg12:
2989 case DW_OP_breg13:
2990 case DW_OP_breg14:
2991 case DW_OP_breg15:
2992 case DW_OP_breg16:
2993 case DW_OP_breg17:
2994 case DW_OP_breg18:
2995 case DW_OP_breg19:
2996 case DW_OP_breg20:
2997 case DW_OP_breg21:
2998 case DW_OP_breg22:
2999 case DW_OP_breg23:
3000 case DW_OP_breg24:
3001 case DW_OP_breg25:
3002 case DW_OP_breg26:
3003 case DW_OP_breg27:
3004 case DW_OP_breg28:
3005 case DW_OP_breg29:
3006 case DW_OP_breg30:
3007 case DW_OP_breg31:
3008 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3009 break;
3010 case DW_OP_regx:
3011 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3012 break;
3013 case DW_OP_fbreg:
3014 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3015 break;
3016 case DW_OP_bregx:
3017 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3018 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
3019 break;
3020 case DW_OP_piece:
3021 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3022 break;
3023 case DW_OP_deref_size:
3024 case DW_OP_xderef_size:
3025 size += 1;
3026 break;
3027 case DW_OP_call2:
3028 size += 2;
3029 break;
3030 case DW_OP_call4:
3031 size += 4;
3032 break;
3033 case DW_OP_call_ref:
3034 size += DWARF2_ADDR_SIZE;
3035 break;
3036 default:
3037 break;
3040 return size;
3043 /* Return the size of a series of location descriptors. */
3045 static unsigned long
3046 size_of_locs (dw_loc_descr_ref loc)
3048 unsigned long size;
3050 for (size = 0; loc != NULL; loc = loc->dw_loc_next)
3052 loc->dw_loc_addr = size;
3053 size += size_of_loc_descr (loc);
3056 return size;
3059 /* Output location description stack opcode's operands (if any). */
3061 static void
3062 output_loc_operands (dw_loc_descr_ref loc)
3064 dw_val_ref val1 = &loc->dw_loc_oprnd1;
3065 dw_val_ref val2 = &loc->dw_loc_oprnd2;
3067 switch (loc->dw_loc_opc)
3069 #ifdef DWARF2_DEBUGGING_INFO
3070 case DW_OP_addr:
3071 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
3072 break;
3073 case DW_OP_const2u:
3074 case DW_OP_const2s:
3075 dw2_asm_output_data (2, val1->v.val_int, NULL);
3076 break;
3077 case DW_OP_const4u:
3078 case DW_OP_const4s:
3079 dw2_asm_output_data (4, val1->v.val_int, NULL);
3080 break;
3081 case DW_OP_const8u:
3082 case DW_OP_const8s:
3083 gcc_assert (HOST_BITS_PER_LONG >= 64);
3084 dw2_asm_output_data (8, val1->v.val_int, NULL);
3085 break;
3086 case DW_OP_skip:
3087 case DW_OP_bra:
3089 int offset;
3091 gcc_assert (val1->val_class == dw_val_class_loc);
3092 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
3094 dw2_asm_output_data (2, offset, NULL);
3096 break;
3097 #else
3098 case DW_OP_addr:
3099 case DW_OP_const2u:
3100 case DW_OP_const2s:
3101 case DW_OP_const4u:
3102 case DW_OP_const4s:
3103 case DW_OP_const8u:
3104 case DW_OP_const8s:
3105 case DW_OP_skip:
3106 case DW_OP_bra:
3107 /* We currently don't make any attempt to make sure these are
3108 aligned properly like we do for the main unwind info, so
3109 don't support emitting things larger than a byte if we're
3110 only doing unwinding. */
3111 gcc_unreachable ();
3112 #endif
3113 case DW_OP_const1u:
3114 case DW_OP_const1s:
3115 dw2_asm_output_data (1, val1->v.val_int, NULL);
3116 break;
3117 case DW_OP_constu:
3118 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3119 break;
3120 case DW_OP_consts:
3121 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3122 break;
3123 case DW_OP_pick:
3124 dw2_asm_output_data (1, val1->v.val_int, NULL);
3125 break;
3126 case DW_OP_plus_uconst:
3127 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3128 break;
3129 case DW_OP_breg0:
3130 case DW_OP_breg1:
3131 case DW_OP_breg2:
3132 case DW_OP_breg3:
3133 case DW_OP_breg4:
3134 case DW_OP_breg5:
3135 case DW_OP_breg6:
3136 case DW_OP_breg7:
3137 case DW_OP_breg8:
3138 case DW_OP_breg9:
3139 case DW_OP_breg10:
3140 case DW_OP_breg11:
3141 case DW_OP_breg12:
3142 case DW_OP_breg13:
3143 case DW_OP_breg14:
3144 case DW_OP_breg15:
3145 case DW_OP_breg16:
3146 case DW_OP_breg17:
3147 case DW_OP_breg18:
3148 case DW_OP_breg19:
3149 case DW_OP_breg20:
3150 case DW_OP_breg21:
3151 case DW_OP_breg22:
3152 case DW_OP_breg23:
3153 case DW_OP_breg24:
3154 case DW_OP_breg25:
3155 case DW_OP_breg26:
3156 case DW_OP_breg27:
3157 case DW_OP_breg28:
3158 case DW_OP_breg29:
3159 case DW_OP_breg30:
3160 case DW_OP_breg31:
3161 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3162 break;
3163 case DW_OP_regx:
3164 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3165 break;
3166 case DW_OP_fbreg:
3167 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3168 break;
3169 case DW_OP_bregx:
3170 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3171 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
3172 break;
3173 case DW_OP_piece:
3174 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3175 break;
3176 case DW_OP_deref_size:
3177 case DW_OP_xderef_size:
3178 dw2_asm_output_data (1, val1->v.val_int, NULL);
3179 break;
3181 case INTERNAL_DW_OP_tls_addr:
3182 #ifdef ASM_OUTPUT_DWARF_DTPREL
3183 ASM_OUTPUT_DWARF_DTPREL (asm_out_file, DWARF2_ADDR_SIZE,
3184 val1->v.val_addr);
3185 fputc ('\n', asm_out_file);
3186 #else
3187 gcc_unreachable ();
3188 #endif
3189 break;
3191 default:
3192 /* Other codes have no operands. */
3193 break;
3197 /* Output a sequence of location operations. */
3199 static void
3200 output_loc_sequence (dw_loc_descr_ref loc)
3202 for (; loc != NULL; loc = loc->dw_loc_next)
3204 /* Output the opcode. */
3205 dw2_asm_output_data (1, loc->dw_loc_opc,
3206 "%s", dwarf_stack_op_name (loc->dw_loc_opc));
3208 /* Output the operand(s) (if any). */
3209 output_loc_operands (loc);
3213 /* This routine will generate the correct assembly data for a location
3214 description based on a cfi entry with a complex address. */
3216 static void
3217 output_cfa_loc (dw_cfi_ref cfi)
3219 dw_loc_descr_ref loc;
3220 unsigned long size;
3222 /* Output the size of the block. */
3223 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
3224 size = size_of_locs (loc);
3225 dw2_asm_output_data_uleb128 (size, NULL);
3227 /* Now output the operations themselves. */
3228 output_loc_sequence (loc);
3231 /* This function builds a dwarf location descriptor sequence from
3232 a dw_cfa_location. */
3234 static struct dw_loc_descr_struct *
3235 build_cfa_loc (dw_cfa_location *cfa)
3237 struct dw_loc_descr_struct *head, *tmp;
3239 gcc_assert (cfa->indirect);
3241 if (cfa->base_offset)
3243 if (cfa->reg <= 31)
3244 head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->base_offset, 0);
3245 else
3246 head = new_loc_descr (DW_OP_bregx, cfa->reg, cfa->base_offset);
3248 else if (cfa->reg <= 31)
3249 head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3250 else
3251 head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3253 head->dw_loc_oprnd1.val_class = dw_val_class_const;
3254 tmp = new_loc_descr (DW_OP_deref, 0, 0);
3255 add_loc_descr (&head, tmp);
3256 if (cfa->offset != 0)
3258 tmp = new_loc_descr (DW_OP_plus_uconst, cfa->offset, 0);
3259 add_loc_descr (&head, tmp);
3262 return head;
3265 /* This function fills in aa dw_cfa_location structure from a dwarf location
3266 descriptor sequence. */
3268 static void
3269 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
3271 struct dw_loc_descr_struct *ptr;
3272 cfa->offset = 0;
3273 cfa->base_offset = 0;
3274 cfa->indirect = 0;
3275 cfa->reg = -1;
3277 for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
3279 enum dwarf_location_atom op = ptr->dw_loc_opc;
3281 switch (op)
3283 case DW_OP_reg0:
3284 case DW_OP_reg1:
3285 case DW_OP_reg2:
3286 case DW_OP_reg3:
3287 case DW_OP_reg4:
3288 case DW_OP_reg5:
3289 case DW_OP_reg6:
3290 case DW_OP_reg7:
3291 case DW_OP_reg8:
3292 case DW_OP_reg9:
3293 case DW_OP_reg10:
3294 case DW_OP_reg11:
3295 case DW_OP_reg12:
3296 case DW_OP_reg13:
3297 case DW_OP_reg14:
3298 case DW_OP_reg15:
3299 case DW_OP_reg16:
3300 case DW_OP_reg17:
3301 case DW_OP_reg18:
3302 case DW_OP_reg19:
3303 case DW_OP_reg20:
3304 case DW_OP_reg21:
3305 case DW_OP_reg22:
3306 case DW_OP_reg23:
3307 case DW_OP_reg24:
3308 case DW_OP_reg25:
3309 case DW_OP_reg26:
3310 case DW_OP_reg27:
3311 case DW_OP_reg28:
3312 case DW_OP_reg29:
3313 case DW_OP_reg30:
3314 case DW_OP_reg31:
3315 cfa->reg = op - DW_OP_reg0;
3316 break;
3317 case DW_OP_regx:
3318 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3319 break;
3320 case DW_OP_breg0:
3321 case DW_OP_breg1:
3322 case DW_OP_breg2:
3323 case DW_OP_breg3:
3324 case DW_OP_breg4:
3325 case DW_OP_breg5:
3326 case DW_OP_breg6:
3327 case DW_OP_breg7:
3328 case DW_OP_breg8:
3329 case DW_OP_breg9:
3330 case DW_OP_breg10:
3331 case DW_OP_breg11:
3332 case DW_OP_breg12:
3333 case DW_OP_breg13:
3334 case DW_OP_breg14:
3335 case DW_OP_breg15:
3336 case DW_OP_breg16:
3337 case DW_OP_breg17:
3338 case DW_OP_breg18:
3339 case DW_OP_breg19:
3340 case DW_OP_breg20:
3341 case DW_OP_breg21:
3342 case DW_OP_breg22:
3343 case DW_OP_breg23:
3344 case DW_OP_breg24:
3345 case DW_OP_breg25:
3346 case DW_OP_breg26:
3347 case DW_OP_breg27:
3348 case DW_OP_breg28:
3349 case DW_OP_breg29:
3350 case DW_OP_breg30:
3351 case DW_OP_breg31:
3352 cfa->reg = op - DW_OP_breg0;
3353 cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
3354 break;
3355 case DW_OP_bregx:
3356 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3357 cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
3358 break;
3359 case DW_OP_deref:
3360 cfa->indirect = 1;
3361 break;
3362 case DW_OP_plus_uconst:
3363 cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
3364 break;
3365 default:
3366 internal_error ("DW_LOC_OP %s not implemented\n",
3367 dwarf_stack_op_name (ptr->dw_loc_opc));
3371 #endif /* .debug_frame support */
3373 /* And now, the support for symbolic debugging information. */
3374 #ifdef DWARF2_DEBUGGING_INFO
3376 /* .debug_str support. */
3377 static int output_indirect_string (void **, void *);
3379 static void dwarf2out_init (const char *);
3380 static void dwarf2out_finish (const char *);
3381 static void dwarf2out_define (unsigned int, const char *);
3382 static void dwarf2out_undef (unsigned int, const char *);
3383 static void dwarf2out_start_source_file (unsigned, const char *);
3384 static void dwarf2out_end_source_file (unsigned);
3385 static void dwarf2out_begin_block (unsigned, unsigned);
3386 static void dwarf2out_end_block (unsigned, unsigned);
3387 static bool dwarf2out_ignore_block (tree);
3388 static void dwarf2out_global_decl (tree);
3389 static void dwarf2out_type_decl (tree, int);
3390 static void dwarf2out_imported_module_or_decl (tree, tree);
3391 static void dwarf2out_abstract_function (tree);
3392 static void dwarf2out_var_location (rtx);
3393 static void dwarf2out_begin_function (tree);
3395 /* The debug hooks structure. */
3397 const struct gcc_debug_hooks dwarf2_debug_hooks =
3399 dwarf2out_init,
3400 dwarf2out_finish,
3401 dwarf2out_define,
3402 dwarf2out_undef,
3403 dwarf2out_start_source_file,
3404 dwarf2out_end_source_file,
3405 dwarf2out_begin_block,
3406 dwarf2out_end_block,
3407 dwarf2out_ignore_block,
3408 dwarf2out_source_line,
3409 dwarf2out_begin_prologue,
3410 debug_nothing_int_charstar, /* end_prologue */
3411 dwarf2out_end_epilogue,
3412 dwarf2out_begin_function,
3413 debug_nothing_int, /* end_function */
3414 dwarf2out_decl, /* function_decl */
3415 dwarf2out_global_decl,
3416 dwarf2out_type_decl, /* type_decl */
3417 dwarf2out_imported_module_or_decl,
3418 debug_nothing_tree, /* deferred_inline_function */
3419 /* The DWARF 2 backend tries to reduce debugging bloat by not
3420 emitting the abstract description of inline functions until
3421 something tries to reference them. */
3422 dwarf2out_abstract_function, /* outlining_inline_function */
3423 debug_nothing_rtx, /* label */
3424 debug_nothing_int, /* handle_pch */
3425 dwarf2out_var_location
3427 #endif
3429 /* NOTE: In the comments in this file, many references are made to
3430 "Debugging Information Entries". This term is abbreviated as `DIE'
3431 throughout the remainder of this file. */
3433 /* An internal representation of the DWARF output is built, and then
3434 walked to generate the DWARF debugging info. The walk of the internal
3435 representation is done after the entire program has been compiled.
3436 The types below are used to describe the internal representation. */
3438 /* Various DIE's use offsets relative to the beginning of the
3439 .debug_info section to refer to each other. */
3441 typedef long int dw_offset;
3443 /* Define typedefs here to avoid circular dependencies. */
3445 typedef struct dw_attr_struct *dw_attr_ref;
3446 typedef struct dw_line_info_struct *dw_line_info_ref;
3447 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
3448 typedef struct pubname_struct *pubname_ref;
3449 typedef struct dw_ranges_struct *dw_ranges_ref;
3451 /* Each entry in the line_info_table maintains the file and
3452 line number associated with the label generated for that
3453 entry. The label gives the PC value associated with
3454 the line number entry. */
3456 typedef struct dw_line_info_struct GTY(())
3458 unsigned long dw_file_num;
3459 unsigned long dw_line_num;
3461 dw_line_info_entry;
3463 /* Line information for functions in separate sections; each one gets its
3464 own sequence. */
3465 typedef struct dw_separate_line_info_struct GTY(())
3467 unsigned long dw_file_num;
3468 unsigned long dw_line_num;
3469 unsigned long function;
3471 dw_separate_line_info_entry;
3473 /* Each DIE attribute has a field specifying the attribute kind,
3474 a link to the next attribute in the chain, and an attribute value.
3475 Attributes are typically linked below the DIE they modify. */
3477 typedef struct dw_attr_struct GTY(())
3479 enum dwarf_attribute dw_attr;
3480 dw_attr_ref dw_attr_next;
3481 dw_val_node dw_attr_val;
3483 dw_attr_node;
3485 /* The Debugging Information Entry (DIE) structure */
3487 typedef struct die_struct GTY(())
3489 enum dwarf_tag die_tag;
3490 char *die_symbol;
3491 dw_attr_ref die_attr;
3492 dw_die_ref die_parent;
3493 dw_die_ref die_child;
3494 dw_die_ref die_sib;
3495 dw_die_ref die_definition; /* ref from a specification to its definition */
3496 dw_offset die_offset;
3497 unsigned long die_abbrev;
3498 int die_mark;
3499 unsigned int decl_id;
3501 die_node;
3503 /* The pubname structure */
3505 typedef struct pubname_struct GTY(())
3507 dw_die_ref die;
3508 char *name;
3510 pubname_entry;
3512 struct dw_ranges_struct GTY(())
3514 int block_num;
3517 /* The limbo die list structure. */
3518 typedef struct limbo_die_struct GTY(())
3520 dw_die_ref die;
3521 tree created_for;
3522 struct limbo_die_struct *next;
3524 limbo_die_node;
3526 /* How to start an assembler comment. */
3527 #ifndef ASM_COMMENT_START
3528 #define ASM_COMMENT_START ";#"
3529 #endif
3531 /* Define a macro which returns nonzero for a TYPE_DECL which was
3532 implicitly generated for a tagged type.
3534 Note that unlike the gcc front end (which generates a NULL named
3535 TYPE_DECL node for each complete tagged type, each array type, and
3536 each function type node created) the g++ front end generates a
3537 _named_ TYPE_DECL node for each tagged type node created.
3538 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3539 generate a DW_TAG_typedef DIE for them. */
3541 #define TYPE_DECL_IS_STUB(decl) \
3542 (DECL_NAME (decl) == NULL_TREE \
3543 || (DECL_ARTIFICIAL (decl) \
3544 && is_tagged_type (TREE_TYPE (decl)) \
3545 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3546 /* This is necessary for stub decls that \
3547 appear in nested inline functions. */ \
3548 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3549 && (decl_ultimate_origin (decl) \
3550 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3552 /* Information concerning the compilation unit's programming
3553 language, and compiler version. */
3555 /* Fixed size portion of the DWARF compilation unit header. */
3556 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3557 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
3559 /* Fixed size portion of public names info. */
3560 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3562 /* Fixed size portion of the address range info. */
3563 #define DWARF_ARANGES_HEADER_SIZE \
3564 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3565 DWARF2_ADDR_SIZE * 2) \
3566 - DWARF_INITIAL_LENGTH_SIZE)
3568 /* Size of padding portion in the address range info. It must be
3569 aligned to twice the pointer size. */
3570 #define DWARF_ARANGES_PAD_SIZE \
3571 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3572 DWARF2_ADDR_SIZE * 2) \
3573 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3575 /* Use assembler line directives if available. */
3576 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3577 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3578 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3579 #else
3580 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3581 #endif
3582 #endif
3584 /* Minimum line offset in a special line info. opcode.
3585 This value was chosen to give a reasonable range of values. */
3586 #define DWARF_LINE_BASE -10
3588 /* First special line opcode - leave room for the standard opcodes. */
3589 #define DWARF_LINE_OPCODE_BASE 10
3591 /* Range of line offsets in a special line info. opcode. */
3592 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3594 /* Flag that indicates the initial value of the is_stmt_start flag.
3595 In the present implementation, we do not mark any lines as
3596 the beginning of a source statement, because that information
3597 is not made available by the GCC front-end. */
3598 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3600 #ifdef DWARF2_DEBUGGING_INFO
3601 /* This location is used by calc_die_sizes() to keep track
3602 the offset of each DIE within the .debug_info section. */
3603 static unsigned long next_die_offset;
3604 #endif
3606 /* Record the root of the DIE's built for the current compilation unit. */
3607 static GTY(()) dw_die_ref comp_unit_die;
3609 /* A list of DIEs with a NULL parent waiting to be relocated. */
3610 static GTY(()) limbo_die_node *limbo_die_list;
3612 /* Filenames referenced by this compilation unit. */
3613 static GTY(()) varray_type file_table;
3614 static GTY(()) varray_type file_table_emitted;
3615 static GTY(()) size_t file_table_last_lookup_index;
3617 /* A hash table of references to DIE's that describe declarations.
3618 The key is a DECL_UID() which is a unique number identifying each decl. */
3619 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
3621 /* Node of the variable location list. */
3622 struct var_loc_node GTY ((chain_next ("%h.next")))
3624 rtx GTY (()) var_loc_note;
3625 const char * GTY (()) label;
3626 struct var_loc_node * GTY (()) next;
3629 /* Variable location list. */
3630 struct var_loc_list_def GTY (())
3632 struct var_loc_node * GTY (()) first;
3634 /* Do not mark the last element of the chained list because
3635 it is marked through the chain. */
3636 struct var_loc_node * GTY ((skip ("%h"))) last;
3638 /* DECL_UID of the variable decl. */
3639 unsigned int decl_id;
3641 typedef struct var_loc_list_def var_loc_list;
3644 /* Table of decl location linked lists. */
3645 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
3647 /* A pointer to the base of a list of references to DIE's that
3648 are uniquely identified by their tag, presence/absence of
3649 children DIE's, and list of attribute/value pairs. */
3650 static GTY((length ("abbrev_die_table_allocated")))
3651 dw_die_ref *abbrev_die_table;
3653 /* Number of elements currently allocated for abbrev_die_table. */
3654 static GTY(()) unsigned abbrev_die_table_allocated;
3656 /* Number of elements in type_die_table currently in use. */
3657 static GTY(()) unsigned abbrev_die_table_in_use;
3659 /* Size (in elements) of increments by which we may expand the
3660 abbrev_die_table. */
3661 #define ABBREV_DIE_TABLE_INCREMENT 256
3663 /* A pointer to the base of a table that contains line information
3664 for each source code line in .text in the compilation unit. */
3665 static GTY((length ("line_info_table_allocated")))
3666 dw_line_info_ref line_info_table;
3668 /* Number of elements currently allocated for line_info_table. */
3669 static GTY(()) unsigned line_info_table_allocated;
3671 /* Number of elements in line_info_table currently in use. */
3672 static GTY(()) unsigned line_info_table_in_use;
3674 /* A pointer to the base of a table that contains line information
3675 for each source code line outside of .text in the compilation unit. */
3676 static GTY ((length ("separate_line_info_table_allocated")))
3677 dw_separate_line_info_ref separate_line_info_table;
3679 /* Number of elements currently allocated for separate_line_info_table. */
3680 static GTY(()) unsigned separate_line_info_table_allocated;
3682 /* Number of elements in separate_line_info_table currently in use. */
3683 static GTY(()) unsigned separate_line_info_table_in_use;
3685 /* Size (in elements) of increments by which we may expand the
3686 line_info_table. */
3687 #define LINE_INFO_TABLE_INCREMENT 1024
3689 /* A pointer to the base of a table that contains a list of publicly
3690 accessible names. */
3691 static GTY ((length ("pubname_table_allocated"))) pubname_ref pubname_table;
3693 /* Number of elements currently allocated for pubname_table. */
3694 static GTY(()) unsigned pubname_table_allocated;
3696 /* Number of elements in pubname_table currently in use. */
3697 static GTY(()) unsigned pubname_table_in_use;
3699 /* Size (in elements) of increments by which we may expand the
3700 pubname_table. */
3701 #define PUBNAME_TABLE_INCREMENT 64
3703 /* Array of dies for which we should generate .debug_arange info. */
3704 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
3706 /* Number of elements currently allocated for arange_table. */
3707 static GTY(()) unsigned arange_table_allocated;
3709 /* Number of elements in arange_table currently in use. */
3710 static GTY(()) unsigned arange_table_in_use;
3712 /* Size (in elements) of increments by which we may expand the
3713 arange_table. */
3714 #define ARANGE_TABLE_INCREMENT 64
3716 /* Array of dies for which we should generate .debug_ranges info. */
3717 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
3719 /* Number of elements currently allocated for ranges_table. */
3720 static GTY(()) unsigned ranges_table_allocated;
3722 /* Number of elements in ranges_table currently in use. */
3723 static GTY(()) unsigned ranges_table_in_use;
3725 /* Size (in elements) of increments by which we may expand the
3726 ranges_table. */
3727 #define RANGES_TABLE_INCREMENT 64
3729 /* Whether we have location lists that need outputting */
3730 static GTY(()) unsigned have_location_lists;
3732 /* Unique label counter. */
3733 static GTY(()) unsigned int loclabel_num;
3735 #ifdef DWARF2_DEBUGGING_INFO
3736 /* Record whether the function being analyzed contains inlined functions. */
3737 static int current_function_has_inlines;
3738 #endif
3739 #if 0 && defined (MIPS_DEBUGGING_INFO)
3740 static int comp_unit_has_inlines;
3741 #endif
3743 /* Number of file tables emitted in maybe_emit_file(). */
3744 static GTY(()) int emitcount = 0;
3746 /* Number of internal labels generated by gen_internal_sym(). */
3747 static GTY(()) int label_num;
3749 #ifdef DWARF2_DEBUGGING_INFO
3751 /* Forward declarations for functions defined in this file. */
3753 static int is_pseudo_reg (rtx);
3754 static tree type_main_variant (tree);
3755 static int is_tagged_type (tree);
3756 static const char *dwarf_tag_name (unsigned);
3757 static const char *dwarf_attr_name (unsigned);
3758 static const char *dwarf_form_name (unsigned);
3759 #if 0
3760 static const char *dwarf_type_encoding_name (unsigned);
3761 #endif
3762 static tree decl_ultimate_origin (tree);
3763 static tree block_ultimate_origin (tree);
3764 static tree decl_class_context (tree);
3765 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
3766 static inline enum dw_val_class AT_class (dw_attr_ref);
3767 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3768 static inline unsigned AT_flag (dw_attr_ref);
3769 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3770 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3771 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3772 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3773 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
3774 unsigned long);
3775 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3776 unsigned int, unsigned char *);
3777 static hashval_t debug_str_do_hash (const void *);
3778 static int debug_str_eq (const void *, const void *);
3779 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3780 static inline const char *AT_string (dw_attr_ref);
3781 static int AT_string_form (dw_attr_ref);
3782 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3783 static void add_AT_specification (dw_die_ref, dw_die_ref);
3784 static inline dw_die_ref AT_ref (dw_attr_ref);
3785 static inline int AT_ref_external (dw_attr_ref);
3786 static inline void set_AT_ref_external (dw_attr_ref, int);
3787 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3788 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3789 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3790 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3791 dw_loc_list_ref);
3792 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3793 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
3794 static inline rtx AT_addr (dw_attr_ref);
3795 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3796 static void add_AT_lbl_offset (dw_die_ref, enum dwarf_attribute, const char *);
3797 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3798 unsigned HOST_WIDE_INT);
3799 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3800 unsigned long);
3801 static inline const char *AT_lbl (dw_attr_ref);
3802 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3803 static const char *get_AT_low_pc (dw_die_ref);
3804 static const char *get_AT_hi_pc (dw_die_ref);
3805 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3806 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3807 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3808 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3809 static bool is_c_family (void);
3810 static bool is_cxx (void);
3811 static bool is_java (void);
3812 static bool is_fortran (void);
3813 static bool is_ada (void);
3814 static void remove_AT (dw_die_ref, enum dwarf_attribute);
3815 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3816 static inline void free_die (dw_die_ref);
3817 static void remove_children (dw_die_ref);
3818 static void add_child_die (dw_die_ref, dw_die_ref);
3819 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3820 static dw_die_ref lookup_type_die (tree);
3821 static void equate_type_number_to_die (tree, dw_die_ref);
3822 static hashval_t decl_die_table_hash (const void *);
3823 static int decl_die_table_eq (const void *, const void *);
3824 static dw_die_ref lookup_decl_die (tree);
3825 static hashval_t decl_loc_table_hash (const void *);
3826 static int decl_loc_table_eq (const void *, const void *);
3827 static var_loc_list *lookup_decl_loc (tree);
3828 static void equate_decl_number_to_die (tree, dw_die_ref);
3829 static void add_var_loc_to_decl (tree, struct var_loc_node *);
3830 static void print_spaces (FILE *);
3831 static void print_die (dw_die_ref, FILE *);
3832 static void print_dwarf_line_table (FILE *);
3833 static void reverse_die_lists (dw_die_ref);
3834 static void reverse_all_dies (dw_die_ref);
3835 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3836 static dw_die_ref pop_compile_unit (dw_die_ref);
3837 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3838 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3839 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3840 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3841 static int same_dw_val_p (dw_val_node *, dw_val_node *, int *);
3842 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3843 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3844 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3845 static void compute_section_prefix (dw_die_ref);
3846 static int is_type_die (dw_die_ref);
3847 static int is_comdat_die (dw_die_ref);
3848 static int is_symbol_die (dw_die_ref);
3849 static void assign_symbol_names (dw_die_ref);
3850 static void break_out_includes (dw_die_ref);
3851 static hashval_t htab_cu_hash (const void *);
3852 static int htab_cu_eq (const void *, const void *);
3853 static void htab_cu_del (void *);
3854 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
3855 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
3856 static void add_sibling_attributes (dw_die_ref);
3857 static void build_abbrev_table (dw_die_ref);
3858 static void output_location_lists (dw_die_ref);
3859 static int constant_size (long unsigned);
3860 static unsigned long size_of_die (dw_die_ref);
3861 static void calc_die_sizes (dw_die_ref);
3862 static void mark_dies (dw_die_ref);
3863 static void unmark_dies (dw_die_ref);
3864 static void unmark_all_dies (dw_die_ref);
3865 static unsigned long size_of_pubnames (void);
3866 static unsigned long size_of_aranges (void);
3867 static enum dwarf_form value_format (dw_attr_ref);
3868 static void output_value_format (dw_attr_ref);
3869 static void output_abbrev_section (void);
3870 static void output_die_symbol (dw_die_ref);
3871 static void output_die (dw_die_ref);
3872 static void output_compilation_unit_header (void);
3873 static void output_comp_unit (dw_die_ref, int);
3874 static const char *dwarf2_name (tree, int);
3875 static void add_pubname (tree, dw_die_ref);
3876 static void output_pubnames (void);
3877 static void add_arange (tree, dw_die_ref);
3878 static void output_aranges (void);
3879 static unsigned int add_ranges (tree);
3880 static void output_ranges (void);
3881 static void output_line_info (void);
3882 static void output_file_names (void);
3883 static dw_die_ref base_type_die (tree);
3884 static tree root_type (tree);
3885 static int is_base_type (tree);
3886 static bool is_subrange_type (tree);
3887 static dw_die_ref subrange_type_die (tree, dw_die_ref);
3888 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
3889 static int type_is_enum (tree);
3890 static unsigned int dbx_reg_number (rtx);
3891 static dw_loc_descr_ref reg_loc_descriptor (rtx);
3892 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int);
3893 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx);
3894 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
3895 static dw_loc_descr_ref based_loc_descr (unsigned, HOST_WIDE_INT, bool);
3896 static int is_based_loc (rtx);
3897 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode, bool);
3898 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx);
3899 static dw_loc_descr_ref loc_descriptor (rtx, bool);
3900 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
3901 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
3902 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3903 static tree field_type (tree);
3904 static unsigned int simple_type_align_in_bits (tree);
3905 static unsigned int simple_decl_align_in_bits (tree);
3906 static unsigned HOST_WIDE_INT simple_type_size_in_bits (tree);
3907 static HOST_WIDE_INT field_byte_offset (tree);
3908 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3909 dw_loc_descr_ref);
3910 static void add_data_member_location_attribute (dw_die_ref, tree);
3911 static void add_const_value_attribute (dw_die_ref, rtx);
3912 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3913 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
3914 static void insert_float (rtx, unsigned char *);
3915 static rtx rtl_for_decl_location (tree);
3916 static void add_location_or_const_value_attribute (dw_die_ref, tree,
3917 enum dwarf_attribute);
3918 static void tree_add_const_value_attribute (dw_die_ref, tree);
3919 static void add_name_attribute (dw_die_ref, const char *);
3920 static void add_comp_dir_attribute (dw_die_ref);
3921 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3922 static void add_subscript_info (dw_die_ref, tree);
3923 static void add_byte_size_attribute (dw_die_ref, tree);
3924 static void add_bit_offset_attribute (dw_die_ref, tree);
3925 static void add_bit_size_attribute (dw_die_ref, tree);
3926 static void add_prototyped_attribute (dw_die_ref, tree);
3927 static void add_abstract_origin_attribute (dw_die_ref, tree);
3928 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3929 static void add_src_coords_attributes (dw_die_ref, tree);
3930 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3931 static void push_decl_scope (tree);
3932 static void pop_decl_scope (void);
3933 static dw_die_ref scope_die_for (tree, dw_die_ref);
3934 static inline int local_scope_p (dw_die_ref);
3935 static inline int class_or_namespace_scope_p (dw_die_ref);
3936 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
3937 static void add_calling_convention_attribute (dw_die_ref, tree);
3938 static const char *type_tag (tree);
3939 static tree member_declared_type (tree);
3940 #if 0
3941 static const char *decl_start_label (tree);
3942 #endif
3943 static void gen_array_type_die (tree, dw_die_ref);
3944 static void gen_set_type_die (tree, dw_die_ref);
3945 #if 0
3946 static void gen_entry_point_die (tree, dw_die_ref);
3947 #endif
3948 static void gen_inlined_enumeration_type_die (tree, dw_die_ref);
3949 static void gen_inlined_structure_type_die (tree, dw_die_ref);
3950 static void gen_inlined_union_type_die (tree, dw_die_ref);
3951 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3952 static dw_die_ref gen_formal_parameter_die (tree, dw_die_ref);
3953 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3954 static void gen_formal_types_die (tree, dw_die_ref);
3955 static void gen_subprogram_die (tree, dw_die_ref);
3956 static void gen_variable_die (tree, dw_die_ref);
3957 static void gen_label_die (tree, dw_die_ref);
3958 static void gen_lexical_block_die (tree, dw_die_ref, int);
3959 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3960 static void gen_field_die (tree, dw_die_ref);
3961 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3962 static dw_die_ref gen_compile_unit_die (const char *);
3963 static void gen_string_type_die (tree, dw_die_ref);
3964 static void gen_inheritance_die (tree, tree, dw_die_ref);
3965 static void gen_member_die (tree, dw_die_ref);
3966 static void gen_struct_or_union_type_die (tree, dw_die_ref);
3967 static void gen_subroutine_type_die (tree, dw_die_ref);
3968 static void gen_typedef_die (tree, dw_die_ref);
3969 static void gen_type_die (tree, dw_die_ref);
3970 static void gen_tagged_type_instantiation_die (tree, dw_die_ref);
3971 static void gen_block_die (tree, dw_die_ref, int);
3972 static void decls_for_scope (tree, dw_die_ref, int);
3973 static int is_redundant_typedef (tree);
3974 static void gen_namespace_die (tree);
3975 static void gen_decl_die (tree, dw_die_ref);
3976 static dw_die_ref force_decl_die (tree);
3977 static dw_die_ref force_type_die (tree);
3978 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3979 static void declare_in_namespace (tree, dw_die_ref);
3980 static unsigned lookup_filename (const char *);
3981 static void init_file_table (void);
3982 static void retry_incomplete_types (void);
3983 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3984 static void splice_child_die (dw_die_ref, dw_die_ref);
3985 static int file_info_cmp (const void *, const void *);
3986 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3987 const char *, const char *, unsigned);
3988 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
3989 const char *, const char *,
3990 const char *);
3991 static void output_loc_list (dw_loc_list_ref);
3992 static char *gen_internal_sym (const char *);
3994 static void prune_unmark_dies (dw_die_ref);
3995 static void prune_unused_types_mark (dw_die_ref, int);
3996 static void prune_unused_types_walk (dw_die_ref);
3997 static void prune_unused_types_walk_attribs (dw_die_ref);
3998 static void prune_unused_types_prune (dw_die_ref);
3999 static void prune_unused_types (void);
4000 static int maybe_emit_file (int);
4002 /* Section names used to hold DWARF debugging information. */
4003 #ifndef DEBUG_INFO_SECTION
4004 #define DEBUG_INFO_SECTION ".debug_info"
4005 #endif
4006 #ifndef DEBUG_ABBREV_SECTION
4007 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
4008 #endif
4009 #ifndef DEBUG_ARANGES_SECTION
4010 #define DEBUG_ARANGES_SECTION ".debug_aranges"
4011 #endif
4012 #ifndef DEBUG_MACINFO_SECTION
4013 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
4014 #endif
4015 #ifndef DEBUG_LINE_SECTION
4016 #define DEBUG_LINE_SECTION ".debug_line"
4017 #endif
4018 #ifndef DEBUG_LOC_SECTION
4019 #define DEBUG_LOC_SECTION ".debug_loc"
4020 #endif
4021 #ifndef DEBUG_PUBNAMES_SECTION
4022 #define DEBUG_PUBNAMES_SECTION ".debug_pubnames"
4023 #endif
4024 #ifndef DEBUG_STR_SECTION
4025 #define DEBUG_STR_SECTION ".debug_str"
4026 #endif
4027 #ifndef DEBUG_RANGES_SECTION
4028 #define DEBUG_RANGES_SECTION ".debug_ranges"
4029 #endif
4031 /* Standard ELF section names for compiled code and data. */
4032 #ifndef TEXT_SECTION_NAME
4033 #define TEXT_SECTION_NAME ".text"
4034 #endif
4036 /* Section flags for .debug_str section. */
4037 #define DEBUG_STR_SECTION_FLAGS \
4038 (HAVE_GAS_SHF_MERGE && flag_merge_constants \
4039 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
4040 : SECTION_DEBUG)
4042 /* Labels we insert at beginning sections we can reference instead of
4043 the section names themselves. */
4045 #ifndef TEXT_SECTION_LABEL
4046 #define TEXT_SECTION_LABEL "Ltext"
4047 #endif
4048 #ifndef DEBUG_LINE_SECTION_LABEL
4049 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
4050 #endif
4051 #ifndef DEBUG_INFO_SECTION_LABEL
4052 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
4053 #endif
4054 #ifndef DEBUG_ABBREV_SECTION_LABEL
4055 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
4056 #endif
4057 #ifndef DEBUG_LOC_SECTION_LABEL
4058 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
4059 #endif
4060 #ifndef DEBUG_RANGES_SECTION_LABEL
4061 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
4062 #endif
4063 #ifndef DEBUG_MACINFO_SECTION_LABEL
4064 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
4065 #endif
4067 /* Definitions of defaults for formats and names of various special
4068 (artificial) labels which may be generated within this file (when the -g
4069 options is used and DWARF2_DEBUGGING_INFO is in effect.
4070 If necessary, these may be overridden from within the tm.h file, but
4071 typically, overriding these defaults is unnecessary. */
4073 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4074 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4075 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4076 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4077 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4078 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4079 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4080 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4082 #ifndef TEXT_END_LABEL
4083 #define TEXT_END_LABEL "Letext"
4084 #endif
4085 #ifndef BLOCK_BEGIN_LABEL
4086 #define BLOCK_BEGIN_LABEL "LBB"
4087 #endif
4088 #ifndef BLOCK_END_LABEL
4089 #define BLOCK_END_LABEL "LBE"
4090 #endif
4091 #ifndef LINE_CODE_LABEL
4092 #define LINE_CODE_LABEL "LM"
4093 #endif
4094 #ifndef SEPARATE_LINE_CODE_LABEL
4095 #define SEPARATE_LINE_CODE_LABEL "LSM"
4096 #endif
4098 /* We allow a language front-end to designate a function that is to be
4099 called to "demangle" any name before it it put into a DIE. */
4101 static const char *(*demangle_name_func) (const char *);
4103 void
4104 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4106 demangle_name_func = func;
4109 /* Test if rtl node points to a pseudo register. */
4111 static inline int
4112 is_pseudo_reg (rtx rtl)
4114 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4115 || (GET_CODE (rtl) == SUBREG
4116 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4119 /* Return a reference to a type, with its const and volatile qualifiers
4120 removed. */
4122 static inline tree
4123 type_main_variant (tree type)
4125 type = TYPE_MAIN_VARIANT (type);
4127 /* ??? There really should be only one main variant among any group of
4128 variants of a given type (and all of the MAIN_VARIANT values for all
4129 members of the group should point to that one type) but sometimes the C
4130 front-end messes this up for array types, so we work around that bug
4131 here. */
4132 if (TREE_CODE (type) == ARRAY_TYPE)
4133 while (type != TYPE_MAIN_VARIANT (type))
4134 type = TYPE_MAIN_VARIANT (type);
4136 return type;
4139 /* Return nonzero if the given type node represents a tagged type. */
4141 static inline int
4142 is_tagged_type (tree type)
4144 enum tree_code code = TREE_CODE (type);
4146 return (code == RECORD_TYPE || code == UNION_TYPE
4147 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4150 /* Convert a DIE tag into its string name. */
4152 static const char *
4153 dwarf_tag_name (unsigned int tag)
4155 switch (tag)
4157 case DW_TAG_padding:
4158 return "DW_TAG_padding";
4159 case DW_TAG_array_type:
4160 return "DW_TAG_array_type";
4161 case DW_TAG_class_type:
4162 return "DW_TAG_class_type";
4163 case DW_TAG_entry_point:
4164 return "DW_TAG_entry_point";
4165 case DW_TAG_enumeration_type:
4166 return "DW_TAG_enumeration_type";
4167 case DW_TAG_formal_parameter:
4168 return "DW_TAG_formal_parameter";
4169 case DW_TAG_imported_declaration:
4170 return "DW_TAG_imported_declaration";
4171 case DW_TAG_label:
4172 return "DW_TAG_label";
4173 case DW_TAG_lexical_block:
4174 return "DW_TAG_lexical_block";
4175 case DW_TAG_member:
4176 return "DW_TAG_member";
4177 case DW_TAG_pointer_type:
4178 return "DW_TAG_pointer_type";
4179 case DW_TAG_reference_type:
4180 return "DW_TAG_reference_type";
4181 case DW_TAG_compile_unit:
4182 return "DW_TAG_compile_unit";
4183 case DW_TAG_string_type:
4184 return "DW_TAG_string_type";
4185 case DW_TAG_structure_type:
4186 return "DW_TAG_structure_type";
4187 case DW_TAG_subroutine_type:
4188 return "DW_TAG_subroutine_type";
4189 case DW_TAG_typedef:
4190 return "DW_TAG_typedef";
4191 case DW_TAG_union_type:
4192 return "DW_TAG_union_type";
4193 case DW_TAG_unspecified_parameters:
4194 return "DW_TAG_unspecified_parameters";
4195 case DW_TAG_variant:
4196 return "DW_TAG_variant";
4197 case DW_TAG_common_block:
4198 return "DW_TAG_common_block";
4199 case DW_TAG_common_inclusion:
4200 return "DW_TAG_common_inclusion";
4201 case DW_TAG_inheritance:
4202 return "DW_TAG_inheritance";
4203 case DW_TAG_inlined_subroutine:
4204 return "DW_TAG_inlined_subroutine";
4205 case DW_TAG_module:
4206 return "DW_TAG_module";
4207 case DW_TAG_ptr_to_member_type:
4208 return "DW_TAG_ptr_to_member_type";
4209 case DW_TAG_set_type:
4210 return "DW_TAG_set_type";
4211 case DW_TAG_subrange_type:
4212 return "DW_TAG_subrange_type";
4213 case DW_TAG_with_stmt:
4214 return "DW_TAG_with_stmt";
4215 case DW_TAG_access_declaration:
4216 return "DW_TAG_access_declaration";
4217 case DW_TAG_base_type:
4218 return "DW_TAG_base_type";
4219 case DW_TAG_catch_block:
4220 return "DW_TAG_catch_block";
4221 case DW_TAG_const_type:
4222 return "DW_TAG_const_type";
4223 case DW_TAG_constant:
4224 return "DW_TAG_constant";
4225 case DW_TAG_enumerator:
4226 return "DW_TAG_enumerator";
4227 case DW_TAG_file_type:
4228 return "DW_TAG_file_type";
4229 case DW_TAG_friend:
4230 return "DW_TAG_friend";
4231 case DW_TAG_namelist:
4232 return "DW_TAG_namelist";
4233 case DW_TAG_namelist_item:
4234 return "DW_TAG_namelist_item";
4235 case DW_TAG_namespace:
4236 return "DW_TAG_namespace";
4237 case DW_TAG_packed_type:
4238 return "DW_TAG_packed_type";
4239 case DW_TAG_subprogram:
4240 return "DW_TAG_subprogram";
4241 case DW_TAG_template_type_param:
4242 return "DW_TAG_template_type_param";
4243 case DW_TAG_template_value_param:
4244 return "DW_TAG_template_value_param";
4245 case DW_TAG_thrown_type:
4246 return "DW_TAG_thrown_type";
4247 case DW_TAG_try_block:
4248 return "DW_TAG_try_block";
4249 case DW_TAG_variant_part:
4250 return "DW_TAG_variant_part";
4251 case DW_TAG_variable:
4252 return "DW_TAG_variable";
4253 case DW_TAG_volatile_type:
4254 return "DW_TAG_volatile_type";
4255 case DW_TAG_imported_module:
4256 return "DW_TAG_imported_module";
4257 case DW_TAG_MIPS_loop:
4258 return "DW_TAG_MIPS_loop";
4259 case DW_TAG_format_label:
4260 return "DW_TAG_format_label";
4261 case DW_TAG_function_template:
4262 return "DW_TAG_function_template";
4263 case DW_TAG_class_template:
4264 return "DW_TAG_class_template";
4265 case DW_TAG_GNU_BINCL:
4266 return "DW_TAG_GNU_BINCL";
4267 case DW_TAG_GNU_EINCL:
4268 return "DW_TAG_GNU_EINCL";
4269 default:
4270 return "DW_TAG_<unknown>";
4274 /* Convert a DWARF attribute code into its string name. */
4276 static const char *
4277 dwarf_attr_name (unsigned int attr)
4279 switch (attr)
4281 case DW_AT_sibling:
4282 return "DW_AT_sibling";
4283 case DW_AT_location:
4284 return "DW_AT_location";
4285 case DW_AT_name:
4286 return "DW_AT_name";
4287 case DW_AT_ordering:
4288 return "DW_AT_ordering";
4289 case DW_AT_subscr_data:
4290 return "DW_AT_subscr_data";
4291 case DW_AT_byte_size:
4292 return "DW_AT_byte_size";
4293 case DW_AT_bit_offset:
4294 return "DW_AT_bit_offset";
4295 case DW_AT_bit_size:
4296 return "DW_AT_bit_size";
4297 case DW_AT_element_list:
4298 return "DW_AT_element_list";
4299 case DW_AT_stmt_list:
4300 return "DW_AT_stmt_list";
4301 case DW_AT_low_pc:
4302 return "DW_AT_low_pc";
4303 case DW_AT_high_pc:
4304 return "DW_AT_high_pc";
4305 case DW_AT_language:
4306 return "DW_AT_language";
4307 case DW_AT_member:
4308 return "DW_AT_member";
4309 case DW_AT_discr:
4310 return "DW_AT_discr";
4311 case DW_AT_discr_value:
4312 return "DW_AT_discr_value";
4313 case DW_AT_visibility:
4314 return "DW_AT_visibility";
4315 case DW_AT_import:
4316 return "DW_AT_import";
4317 case DW_AT_string_length:
4318 return "DW_AT_string_length";
4319 case DW_AT_common_reference:
4320 return "DW_AT_common_reference";
4321 case DW_AT_comp_dir:
4322 return "DW_AT_comp_dir";
4323 case DW_AT_const_value:
4324 return "DW_AT_const_value";
4325 case DW_AT_containing_type:
4326 return "DW_AT_containing_type";
4327 case DW_AT_default_value:
4328 return "DW_AT_default_value";
4329 case DW_AT_inline:
4330 return "DW_AT_inline";
4331 case DW_AT_is_optional:
4332 return "DW_AT_is_optional";
4333 case DW_AT_lower_bound:
4334 return "DW_AT_lower_bound";
4335 case DW_AT_producer:
4336 return "DW_AT_producer";
4337 case DW_AT_prototyped:
4338 return "DW_AT_prototyped";
4339 case DW_AT_return_addr:
4340 return "DW_AT_return_addr";
4341 case DW_AT_start_scope:
4342 return "DW_AT_start_scope";
4343 case DW_AT_stride_size:
4344 return "DW_AT_stride_size";
4345 case DW_AT_upper_bound:
4346 return "DW_AT_upper_bound";
4347 case DW_AT_abstract_origin:
4348 return "DW_AT_abstract_origin";
4349 case DW_AT_accessibility:
4350 return "DW_AT_accessibility";
4351 case DW_AT_address_class:
4352 return "DW_AT_address_class";
4353 case DW_AT_artificial:
4354 return "DW_AT_artificial";
4355 case DW_AT_base_types:
4356 return "DW_AT_base_types";
4357 case DW_AT_calling_convention:
4358 return "DW_AT_calling_convention";
4359 case DW_AT_count:
4360 return "DW_AT_count";
4361 case DW_AT_data_member_location:
4362 return "DW_AT_data_member_location";
4363 case DW_AT_decl_column:
4364 return "DW_AT_decl_column";
4365 case DW_AT_decl_file:
4366 return "DW_AT_decl_file";
4367 case DW_AT_decl_line:
4368 return "DW_AT_decl_line";
4369 case DW_AT_declaration:
4370 return "DW_AT_declaration";
4371 case DW_AT_discr_list:
4372 return "DW_AT_discr_list";
4373 case DW_AT_encoding:
4374 return "DW_AT_encoding";
4375 case DW_AT_external:
4376 return "DW_AT_external";
4377 case DW_AT_frame_base:
4378 return "DW_AT_frame_base";
4379 case DW_AT_friend:
4380 return "DW_AT_friend";
4381 case DW_AT_identifier_case:
4382 return "DW_AT_identifier_case";
4383 case DW_AT_macro_info:
4384 return "DW_AT_macro_info";
4385 case DW_AT_namelist_items:
4386 return "DW_AT_namelist_items";
4387 case DW_AT_priority:
4388 return "DW_AT_priority";
4389 case DW_AT_segment:
4390 return "DW_AT_segment";
4391 case DW_AT_specification:
4392 return "DW_AT_specification";
4393 case DW_AT_static_link:
4394 return "DW_AT_static_link";
4395 case DW_AT_type:
4396 return "DW_AT_type";
4397 case DW_AT_use_location:
4398 return "DW_AT_use_location";
4399 case DW_AT_variable_parameter:
4400 return "DW_AT_variable_parameter";
4401 case DW_AT_virtuality:
4402 return "DW_AT_virtuality";
4403 case DW_AT_vtable_elem_location:
4404 return "DW_AT_vtable_elem_location";
4406 case DW_AT_allocated:
4407 return "DW_AT_allocated";
4408 case DW_AT_associated:
4409 return "DW_AT_associated";
4410 case DW_AT_data_location:
4411 return "DW_AT_data_location";
4412 case DW_AT_stride:
4413 return "DW_AT_stride";
4414 case DW_AT_entry_pc:
4415 return "DW_AT_entry_pc";
4416 case DW_AT_use_UTF8:
4417 return "DW_AT_use_UTF8";
4418 case DW_AT_extension:
4419 return "DW_AT_extension";
4420 case DW_AT_ranges:
4421 return "DW_AT_ranges";
4422 case DW_AT_trampoline:
4423 return "DW_AT_trampoline";
4424 case DW_AT_call_column:
4425 return "DW_AT_call_column";
4426 case DW_AT_call_file:
4427 return "DW_AT_call_file";
4428 case DW_AT_call_line:
4429 return "DW_AT_call_line";
4431 case DW_AT_MIPS_fde:
4432 return "DW_AT_MIPS_fde";
4433 case DW_AT_MIPS_loop_begin:
4434 return "DW_AT_MIPS_loop_begin";
4435 case DW_AT_MIPS_tail_loop_begin:
4436 return "DW_AT_MIPS_tail_loop_begin";
4437 case DW_AT_MIPS_epilog_begin:
4438 return "DW_AT_MIPS_epilog_begin";
4439 case DW_AT_MIPS_loop_unroll_factor:
4440 return "DW_AT_MIPS_loop_unroll_factor";
4441 case DW_AT_MIPS_software_pipeline_depth:
4442 return "DW_AT_MIPS_software_pipeline_depth";
4443 case DW_AT_MIPS_linkage_name:
4444 return "DW_AT_MIPS_linkage_name";
4445 case DW_AT_MIPS_stride:
4446 return "DW_AT_MIPS_stride";
4447 case DW_AT_MIPS_abstract_name:
4448 return "DW_AT_MIPS_abstract_name";
4449 case DW_AT_MIPS_clone_origin:
4450 return "DW_AT_MIPS_clone_origin";
4451 case DW_AT_MIPS_has_inlines:
4452 return "DW_AT_MIPS_has_inlines";
4454 case DW_AT_sf_names:
4455 return "DW_AT_sf_names";
4456 case DW_AT_src_info:
4457 return "DW_AT_src_info";
4458 case DW_AT_mac_info:
4459 return "DW_AT_mac_info";
4460 case DW_AT_src_coords:
4461 return "DW_AT_src_coords";
4462 case DW_AT_body_begin:
4463 return "DW_AT_body_begin";
4464 case DW_AT_body_end:
4465 return "DW_AT_body_end";
4466 case DW_AT_GNU_vector:
4467 return "DW_AT_GNU_vector";
4469 case DW_AT_VMS_rtnbeg_pd_address:
4470 return "DW_AT_VMS_rtnbeg_pd_address";
4472 default:
4473 return "DW_AT_<unknown>";
4477 /* Convert a DWARF value form code into its string name. */
4479 static const char *
4480 dwarf_form_name (unsigned int form)
4482 switch (form)
4484 case DW_FORM_addr:
4485 return "DW_FORM_addr";
4486 case DW_FORM_block2:
4487 return "DW_FORM_block2";
4488 case DW_FORM_block4:
4489 return "DW_FORM_block4";
4490 case DW_FORM_data2:
4491 return "DW_FORM_data2";
4492 case DW_FORM_data4:
4493 return "DW_FORM_data4";
4494 case DW_FORM_data8:
4495 return "DW_FORM_data8";
4496 case DW_FORM_string:
4497 return "DW_FORM_string";
4498 case DW_FORM_block:
4499 return "DW_FORM_block";
4500 case DW_FORM_block1:
4501 return "DW_FORM_block1";
4502 case DW_FORM_data1:
4503 return "DW_FORM_data1";
4504 case DW_FORM_flag:
4505 return "DW_FORM_flag";
4506 case DW_FORM_sdata:
4507 return "DW_FORM_sdata";
4508 case DW_FORM_strp:
4509 return "DW_FORM_strp";
4510 case DW_FORM_udata:
4511 return "DW_FORM_udata";
4512 case DW_FORM_ref_addr:
4513 return "DW_FORM_ref_addr";
4514 case DW_FORM_ref1:
4515 return "DW_FORM_ref1";
4516 case DW_FORM_ref2:
4517 return "DW_FORM_ref2";
4518 case DW_FORM_ref4:
4519 return "DW_FORM_ref4";
4520 case DW_FORM_ref8:
4521 return "DW_FORM_ref8";
4522 case DW_FORM_ref_udata:
4523 return "DW_FORM_ref_udata";
4524 case DW_FORM_indirect:
4525 return "DW_FORM_indirect";
4526 default:
4527 return "DW_FORM_<unknown>";
4531 /* Convert a DWARF type code into its string name. */
4533 #if 0
4534 static const char *
4535 dwarf_type_encoding_name (unsigned enc)
4537 switch (enc)
4539 case DW_ATE_address:
4540 return "DW_ATE_address";
4541 case DW_ATE_boolean:
4542 return "DW_ATE_boolean";
4543 case DW_ATE_complex_float:
4544 return "DW_ATE_complex_float";
4545 case DW_ATE_float:
4546 return "DW_ATE_float";
4547 case DW_ATE_signed:
4548 return "DW_ATE_signed";
4549 case DW_ATE_signed_char:
4550 return "DW_ATE_signed_char";
4551 case DW_ATE_unsigned:
4552 return "DW_ATE_unsigned";
4553 case DW_ATE_unsigned_char:
4554 return "DW_ATE_unsigned_char";
4555 default:
4556 return "DW_ATE_<unknown>";
4559 #endif
4561 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4562 instance of an inlined instance of a decl which is local to an inline
4563 function, so we have to trace all of the way back through the origin chain
4564 to find out what sort of node actually served as the original seed for the
4565 given block. */
4567 static tree
4568 decl_ultimate_origin (tree decl)
4570 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4571 nodes in the function to point to themselves; ignore that if
4572 we're trying to output the abstract instance of this function. */
4573 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4574 return NULL_TREE;
4576 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4577 most distant ancestor, this should never happen. */
4578 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4580 return DECL_ABSTRACT_ORIGIN (decl);
4583 /* Determine the "ultimate origin" of a block. The block may be an inlined
4584 instance of an inlined instance of a block which is local to an inline
4585 function, so we have to trace all of the way back through the origin chain
4586 to find out what sort of node actually served as the original seed for the
4587 given block. */
4589 static tree
4590 block_ultimate_origin (tree block)
4592 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
4594 /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
4595 nodes in the function to point to themselves; ignore that if
4596 we're trying to output the abstract instance of this function. */
4597 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
4598 return NULL_TREE;
4600 if (immediate_origin == NULL_TREE)
4601 return NULL_TREE;
4602 else
4604 tree ret_val;
4605 tree lookahead = immediate_origin;
4609 ret_val = lookahead;
4610 lookahead = (TREE_CODE (ret_val) == BLOCK
4611 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
4613 while (lookahead != NULL && lookahead != ret_val);
4615 return ret_val;
4619 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4620 of a virtual function may refer to a base class, so we check the 'this'
4621 parameter. */
4623 static tree
4624 decl_class_context (tree decl)
4626 tree context = NULL_TREE;
4628 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4629 context = DECL_CONTEXT (decl);
4630 else
4631 context = TYPE_MAIN_VARIANT
4632 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4634 if (context && !TYPE_P (context))
4635 context = NULL_TREE;
4637 return context;
4640 /* Add an attribute/value pair to a DIE. We build the lists up in reverse
4641 addition order, and correct that in reverse_all_dies. */
4643 static inline void
4644 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
4646 if (die != NULL && attr != NULL)
4648 attr->dw_attr_next = die->die_attr;
4649 die->die_attr = attr;
4653 static inline enum dw_val_class
4654 AT_class (dw_attr_ref a)
4656 return a->dw_attr_val.val_class;
4659 /* Add a flag value attribute to a DIE. */
4661 static inline void
4662 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4664 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4666 attr->dw_attr_next = NULL;
4667 attr->dw_attr = attr_kind;
4668 attr->dw_attr_val.val_class = dw_val_class_flag;
4669 attr->dw_attr_val.v.val_flag = flag;
4670 add_dwarf_attr (die, attr);
4673 static inline unsigned
4674 AT_flag (dw_attr_ref a)
4676 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4677 return a->dw_attr_val.v.val_flag;
4680 /* Add a signed integer attribute value to a DIE. */
4682 static inline void
4683 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4685 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4687 attr->dw_attr_next = NULL;
4688 attr->dw_attr = attr_kind;
4689 attr->dw_attr_val.val_class = dw_val_class_const;
4690 attr->dw_attr_val.v.val_int = int_val;
4691 add_dwarf_attr (die, attr);
4694 static inline HOST_WIDE_INT
4695 AT_int (dw_attr_ref a)
4697 gcc_assert (a && AT_class (a) == dw_val_class_const);
4698 return a->dw_attr_val.v.val_int;
4701 /* Add an unsigned integer attribute value to a DIE. */
4703 static inline void
4704 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4705 unsigned HOST_WIDE_INT unsigned_val)
4707 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4709 attr->dw_attr_next = NULL;
4710 attr->dw_attr = attr_kind;
4711 attr->dw_attr_val.val_class = dw_val_class_unsigned_const;
4712 attr->dw_attr_val.v.val_unsigned = unsigned_val;
4713 add_dwarf_attr (die, attr);
4716 static inline unsigned HOST_WIDE_INT
4717 AT_unsigned (dw_attr_ref a)
4719 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
4720 return a->dw_attr_val.v.val_unsigned;
4723 /* Add an unsigned double integer attribute value to a DIE. */
4725 static inline void
4726 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
4727 long unsigned int val_hi, long unsigned int val_low)
4729 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4731 attr->dw_attr_next = NULL;
4732 attr->dw_attr = attr_kind;
4733 attr->dw_attr_val.val_class = dw_val_class_long_long;
4734 attr->dw_attr_val.v.val_long_long.hi = val_hi;
4735 attr->dw_attr_val.v.val_long_long.low = val_low;
4736 add_dwarf_attr (die, attr);
4739 /* Add a floating point attribute value to a DIE and return it. */
4741 static inline void
4742 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4743 unsigned int length, unsigned int elt_size, unsigned char *array)
4745 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4747 attr->dw_attr_next = NULL;
4748 attr->dw_attr = attr_kind;
4749 attr->dw_attr_val.val_class = dw_val_class_vec;
4750 attr->dw_attr_val.v.val_vec.length = length;
4751 attr->dw_attr_val.v.val_vec.elt_size = elt_size;
4752 attr->dw_attr_val.v.val_vec.array = array;
4753 add_dwarf_attr (die, attr);
4756 /* Hash and equality functions for debug_str_hash. */
4758 static hashval_t
4759 debug_str_do_hash (const void *x)
4761 return htab_hash_string (((const struct indirect_string_node *)x)->str);
4764 static int
4765 debug_str_eq (const void *x1, const void *x2)
4767 return strcmp ((((const struct indirect_string_node *)x1)->str),
4768 (const char *)x2) == 0;
4771 /* Add a string attribute value to a DIE. */
4773 static inline void
4774 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4776 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4777 struct indirect_string_node *node;
4778 void **slot;
4780 if (! debug_str_hash)
4781 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
4782 debug_str_eq, NULL);
4784 slot = htab_find_slot_with_hash (debug_str_hash, str,
4785 htab_hash_string (str), INSERT);
4786 if (*slot == NULL)
4787 *slot = ggc_alloc_cleared (sizeof (struct indirect_string_node));
4788 node = (struct indirect_string_node *) *slot;
4789 node->str = ggc_strdup (str);
4790 node->refcount++;
4792 attr->dw_attr_next = NULL;
4793 attr->dw_attr = attr_kind;
4794 attr->dw_attr_val.val_class = dw_val_class_str;
4795 attr->dw_attr_val.v.val_str = node;
4796 add_dwarf_attr (die, attr);
4799 static inline const char *
4800 AT_string (dw_attr_ref a)
4802 gcc_assert (a && AT_class (a) == dw_val_class_str);
4803 return a->dw_attr_val.v.val_str->str;
4806 /* Find out whether a string should be output inline in DIE
4807 or out-of-line in .debug_str section. */
4809 static int
4810 AT_string_form (dw_attr_ref a)
4812 struct indirect_string_node *node;
4813 unsigned int len;
4814 char label[32];
4816 gcc_assert (a && AT_class (a) == dw_val_class_str);
4818 node = a->dw_attr_val.v.val_str;
4819 if (node->form)
4820 return node->form;
4822 len = strlen (node->str) + 1;
4824 /* If the string is shorter or equal to the size of the reference, it is
4825 always better to put it inline. */
4826 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4827 return node->form = DW_FORM_string;
4829 /* If we cannot expect the linker to merge strings in .debug_str
4830 section, only put it into .debug_str if it is worth even in this
4831 single module. */
4832 if ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) == 0
4833 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
4834 return node->form = DW_FORM_string;
4836 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4837 ++dw2_string_counter;
4838 node->label = xstrdup (label);
4840 return node->form = DW_FORM_strp;
4843 /* Add a DIE reference attribute value to a DIE. */
4845 static inline void
4846 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4848 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4850 attr->dw_attr_next = NULL;
4851 attr->dw_attr = attr_kind;
4852 attr->dw_attr_val.val_class = dw_val_class_die_ref;
4853 attr->dw_attr_val.v.val_die_ref.die = targ_die;
4854 attr->dw_attr_val.v.val_die_ref.external = 0;
4855 add_dwarf_attr (die, attr);
4858 /* Add an AT_specification attribute to a DIE, and also make the back
4859 pointer from the specification to the definition. */
4861 static inline void
4862 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4864 add_AT_die_ref (die, DW_AT_specification, targ_die);
4865 gcc_assert (!targ_die->die_definition);
4866 targ_die->die_definition = die;
4869 static inline dw_die_ref
4870 AT_ref (dw_attr_ref a)
4872 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4873 return a->dw_attr_val.v.val_die_ref.die;
4876 static inline int
4877 AT_ref_external (dw_attr_ref a)
4879 if (a && AT_class (a) == dw_val_class_die_ref)
4880 return a->dw_attr_val.v.val_die_ref.external;
4882 return 0;
4885 static inline void
4886 set_AT_ref_external (dw_attr_ref a, int i)
4888 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4889 a->dw_attr_val.v.val_die_ref.external = i;
4892 /* Add an FDE reference attribute value to a DIE. */
4894 static inline void
4895 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4897 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4899 attr->dw_attr_next = NULL;
4900 attr->dw_attr = attr_kind;
4901 attr->dw_attr_val.val_class = dw_val_class_fde_ref;
4902 attr->dw_attr_val.v.val_fde_index = targ_fde;
4903 add_dwarf_attr (die, attr);
4906 /* Add a location description attribute value to a DIE. */
4908 static inline void
4909 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4911 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4913 attr->dw_attr_next = NULL;
4914 attr->dw_attr = attr_kind;
4915 attr->dw_attr_val.val_class = dw_val_class_loc;
4916 attr->dw_attr_val.v.val_loc = loc;
4917 add_dwarf_attr (die, attr);
4920 static inline dw_loc_descr_ref
4921 AT_loc (dw_attr_ref a)
4923 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4924 return a->dw_attr_val.v.val_loc;
4927 static inline void
4928 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4930 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4932 attr->dw_attr_next = NULL;
4933 attr->dw_attr = attr_kind;
4934 attr->dw_attr_val.val_class = dw_val_class_loc_list;
4935 attr->dw_attr_val.v.val_loc_list = loc_list;
4936 add_dwarf_attr (die, attr);
4937 have_location_lists = 1;
4940 static inline dw_loc_list_ref
4941 AT_loc_list (dw_attr_ref a)
4943 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4944 return a->dw_attr_val.v.val_loc_list;
4947 /* Add an address constant attribute value to a DIE. */
4949 static inline void
4950 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
4952 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4954 attr->dw_attr_next = NULL;
4955 attr->dw_attr = attr_kind;
4956 attr->dw_attr_val.val_class = dw_val_class_addr;
4957 attr->dw_attr_val.v.val_addr = addr;
4958 add_dwarf_attr (die, attr);
4961 static inline rtx
4962 AT_addr (dw_attr_ref a)
4964 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4965 return a->dw_attr_val.v.val_addr;
4968 /* Add a label identifier attribute value to a DIE. */
4970 static inline void
4971 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
4973 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4975 attr->dw_attr_next = NULL;
4976 attr->dw_attr = attr_kind;
4977 attr->dw_attr_val.val_class = dw_val_class_lbl_id;
4978 attr->dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4979 add_dwarf_attr (die, attr);
4982 /* Add a section offset attribute value to a DIE. */
4984 static inline void
4985 add_AT_lbl_offset (dw_die_ref die, enum dwarf_attribute attr_kind, const char *label)
4987 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4989 attr->dw_attr_next = NULL;
4990 attr->dw_attr = attr_kind;
4991 attr->dw_attr_val.val_class = dw_val_class_lbl_offset;
4992 attr->dw_attr_val.v.val_lbl_id = xstrdup (label);
4993 add_dwarf_attr (die, attr);
4996 /* Add an offset attribute value to a DIE. */
4998 static inline void
4999 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
5000 unsigned HOST_WIDE_INT offset)
5002 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5004 attr->dw_attr_next = NULL;
5005 attr->dw_attr = attr_kind;
5006 attr->dw_attr_val.val_class = dw_val_class_offset;
5007 attr->dw_attr_val.v.val_offset = offset;
5008 add_dwarf_attr (die, attr);
5011 /* Add an range_list attribute value to a DIE. */
5013 static void
5014 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5015 long unsigned int offset)
5017 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5019 attr->dw_attr_next = NULL;
5020 attr->dw_attr = attr_kind;
5021 attr->dw_attr_val.val_class = dw_val_class_range_list;
5022 attr->dw_attr_val.v.val_offset = offset;
5023 add_dwarf_attr (die, attr);
5026 static inline const char *
5027 AT_lbl (dw_attr_ref a)
5029 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5030 || AT_class (a) == dw_val_class_lbl_offset));
5031 return a->dw_attr_val.v.val_lbl_id;
5034 /* Get the attribute of type attr_kind. */
5036 static dw_attr_ref
5037 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5039 dw_attr_ref a;
5040 dw_die_ref spec = NULL;
5042 if (die != NULL)
5044 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
5045 if (a->dw_attr == attr_kind)
5046 return a;
5047 else if (a->dw_attr == DW_AT_specification
5048 || a->dw_attr == DW_AT_abstract_origin)
5049 spec = AT_ref (a);
5051 if (spec)
5052 return get_AT (spec, attr_kind);
5055 return NULL;
5058 /* Return the "low pc" attribute value, typically associated with a subprogram
5059 DIE. Return null if the "low pc" attribute is either not present, or if it
5060 cannot be represented as an assembler label identifier. */
5062 static inline const char *
5063 get_AT_low_pc (dw_die_ref die)
5065 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
5067 return a ? AT_lbl (a) : NULL;
5070 /* Return the "high pc" attribute value, typically associated with a subprogram
5071 DIE. Return null if the "high pc" attribute is either not present, or if it
5072 cannot be represented as an assembler label identifier. */
5074 static inline const char *
5075 get_AT_hi_pc (dw_die_ref die)
5077 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
5079 return a ? AT_lbl (a) : NULL;
5082 /* Return the value of the string attribute designated by ATTR_KIND, or
5083 NULL if it is not present. */
5085 static inline const char *
5086 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5088 dw_attr_ref a = get_AT (die, attr_kind);
5090 return a ? AT_string (a) : NULL;
5093 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5094 if it is not present. */
5096 static inline int
5097 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5099 dw_attr_ref a = get_AT (die, attr_kind);
5101 return a ? AT_flag (a) : 0;
5104 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5105 if it is not present. */
5107 static inline unsigned
5108 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5110 dw_attr_ref a = get_AT (die, attr_kind);
5112 return a ? AT_unsigned (a) : 0;
5115 static inline dw_die_ref
5116 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5118 dw_attr_ref a = get_AT (die, attr_kind);
5120 return a ? AT_ref (a) : NULL;
5123 /* Return TRUE if the language is C or C++. */
5125 static inline bool
5126 is_c_family (void)
5128 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5130 return (lang == DW_LANG_C || lang == DW_LANG_C89
5131 || lang == DW_LANG_C_plus_plus);
5134 /* Return TRUE if the language is C++. */
5136 static inline bool
5137 is_cxx (void)
5139 return (get_AT_unsigned (comp_unit_die, DW_AT_language)
5140 == DW_LANG_C_plus_plus);
5143 /* Return TRUE if the language is Fortran. */
5145 static inline bool
5146 is_fortran (void)
5148 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5150 return (lang == DW_LANG_Fortran77
5151 || lang == DW_LANG_Fortran90
5152 || lang == DW_LANG_Fortran95);
5155 /* Return TRUE if the language is Java. */
5157 static inline bool
5158 is_java (void)
5160 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5162 return lang == DW_LANG_Java;
5165 /* Return TRUE if the language is Ada. */
5167 static inline bool
5168 is_ada (void)
5170 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5172 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5175 /* Free up the memory used by A. */
5177 static inline void free_AT (dw_attr_ref);
5178 static inline void
5179 free_AT (dw_attr_ref a)
5181 if (AT_class (a) == dw_val_class_str)
5182 if (a->dw_attr_val.v.val_str->refcount)
5183 a->dw_attr_val.v.val_str->refcount--;
5186 /* Remove the specified attribute if present. */
5188 static void
5189 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5191 dw_attr_ref *p;
5192 dw_attr_ref removed = NULL;
5194 if (die != NULL)
5196 for (p = &(die->die_attr); *p; p = &((*p)->dw_attr_next))
5197 if ((*p)->dw_attr == attr_kind)
5199 removed = *p;
5200 *p = (*p)->dw_attr_next;
5201 break;
5204 if (removed != 0)
5205 free_AT (removed);
5209 /* Remove child die whose die_tag is specified tag. */
5211 static void
5212 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5214 dw_die_ref current, prev, next;
5215 current = die->die_child;
5216 prev = NULL;
5217 while (current != NULL)
5219 if (current->die_tag == tag)
5221 next = current->die_sib;
5222 if (prev == NULL)
5223 die->die_child = next;
5224 else
5225 prev->die_sib = next;
5226 free_die (current);
5227 current = next;
5229 else
5231 prev = current;
5232 current = current->die_sib;
5237 /* Free up the memory used by DIE. */
5239 static inline void
5240 free_die (dw_die_ref die)
5242 remove_children (die);
5245 /* Discard the children of this DIE. */
5247 static void
5248 remove_children (dw_die_ref die)
5250 dw_die_ref child_die = die->die_child;
5252 die->die_child = NULL;
5254 while (child_die != NULL)
5256 dw_die_ref tmp_die = child_die;
5257 dw_attr_ref a;
5259 child_die = child_die->die_sib;
5261 for (a = tmp_die->die_attr; a != NULL;)
5263 dw_attr_ref tmp_a = a;
5265 a = a->dw_attr_next;
5266 free_AT (tmp_a);
5269 free_die (tmp_die);
5273 /* Add a child DIE below its parent. We build the lists up in reverse
5274 addition order, and correct that in reverse_all_dies. */
5276 static inline void
5277 add_child_die (dw_die_ref die, dw_die_ref child_die)
5279 if (die != NULL && child_die != NULL)
5281 gcc_assert (die != child_die);
5283 child_die->die_parent = die;
5284 child_die->die_sib = die->die_child;
5285 die->die_child = child_die;
5289 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5290 is the specification, to the front of PARENT's list of children. */
5292 static void
5293 splice_child_die (dw_die_ref parent, dw_die_ref child)
5295 dw_die_ref *p;
5297 /* We want the declaration DIE from inside the class, not the
5298 specification DIE at toplevel. */
5299 if (child->die_parent != parent)
5301 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5303 if (tmp)
5304 child = tmp;
5307 gcc_assert (child->die_parent == parent
5308 || (child->die_parent
5309 == get_AT_ref (parent, DW_AT_specification)));
5311 for (p = &(child->die_parent->die_child); *p; p = &((*p)->die_sib))
5312 if (*p == child)
5314 *p = child->die_sib;
5315 break;
5318 child->die_parent = parent;
5319 child->die_sib = parent->die_child;
5320 parent->die_child = child;
5323 /* Return a pointer to a newly created DIE node. */
5325 static inline dw_die_ref
5326 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5328 dw_die_ref die = ggc_alloc_cleared (sizeof (die_node));
5330 die->die_tag = tag_value;
5332 if (parent_die != NULL)
5333 add_child_die (parent_die, die);
5334 else
5336 limbo_die_node *limbo_node;
5338 limbo_node = ggc_alloc_cleared (sizeof (limbo_die_node));
5339 limbo_node->die = die;
5340 limbo_node->created_for = t;
5341 limbo_node->next = limbo_die_list;
5342 limbo_die_list = limbo_node;
5345 return die;
5348 /* Return the DIE associated with the given type specifier. */
5350 static inline dw_die_ref
5351 lookup_type_die (tree type)
5353 return TYPE_SYMTAB_DIE (type);
5356 /* Equate a DIE to a given type specifier. */
5358 static inline void
5359 equate_type_number_to_die (tree type, dw_die_ref type_die)
5361 TYPE_SYMTAB_DIE (type) = type_die;
5364 /* Returns a hash value for X (which really is a die_struct). */
5366 static hashval_t
5367 decl_die_table_hash (const void *x)
5369 return (hashval_t) ((const dw_die_ref) x)->decl_id;
5372 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5374 static int
5375 decl_die_table_eq (const void *x, const void *y)
5377 return (((const dw_die_ref) x)->decl_id == DECL_UID ((const tree) y));
5380 /* Return the DIE associated with a given declaration. */
5382 static inline dw_die_ref
5383 lookup_decl_die (tree decl)
5385 return htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
5388 /* Returns a hash value for X (which really is a var_loc_list). */
5390 static hashval_t
5391 decl_loc_table_hash (const void *x)
5393 return (hashval_t) ((const var_loc_list *) x)->decl_id;
5396 /* Return nonzero if decl_id of var_loc_list X is the same as
5397 UID of decl *Y. */
5399 static int
5400 decl_loc_table_eq (const void *x, const void *y)
5402 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const tree) y));
5405 /* Return the var_loc list associated with a given declaration. */
5407 static inline var_loc_list *
5408 lookup_decl_loc (tree decl)
5410 return htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5413 /* Equate a DIE to a particular declaration. */
5415 static void
5416 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5418 unsigned int decl_id = DECL_UID (decl);
5419 void **slot;
5421 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5422 *slot = decl_die;
5423 decl_die->decl_id = decl_id;
5426 /* Add a variable location node to the linked list for DECL. */
5428 static void
5429 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
5431 unsigned int decl_id = DECL_UID (decl);
5432 var_loc_list *temp;
5433 void **slot;
5435 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5436 if (*slot == NULL)
5438 temp = ggc_alloc_cleared (sizeof (var_loc_list));
5439 temp->decl_id = decl_id;
5440 *slot = temp;
5442 else
5443 temp = *slot;
5445 if (temp->last)
5447 /* If the current location is the same as the end of the list,
5448 we have nothing to do. */
5449 if (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
5450 NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
5452 /* Add LOC to the end of list and update LAST. */
5453 temp->last->next = loc;
5454 temp->last = loc;
5457 /* Do not add empty location to the beginning of the list. */
5458 else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
5460 temp->first = loc;
5461 temp->last = loc;
5465 /* Keep track of the number of spaces used to indent the
5466 output of the debugging routines that print the structure of
5467 the DIE internal representation. */
5468 static int print_indent;
5470 /* Indent the line the number of spaces given by print_indent. */
5472 static inline void
5473 print_spaces (FILE *outfile)
5475 fprintf (outfile, "%*s", print_indent, "");
5478 /* Print the information associated with a given DIE, and its children.
5479 This routine is a debugging aid only. */
5481 static void
5482 print_die (dw_die_ref die, FILE *outfile)
5484 dw_attr_ref a;
5485 dw_die_ref c;
5487 print_spaces (outfile);
5488 fprintf (outfile, "DIE %4lu: %s\n",
5489 die->die_offset, dwarf_tag_name (die->die_tag));
5490 print_spaces (outfile);
5491 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5492 fprintf (outfile, " offset: %lu\n", die->die_offset);
5494 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
5496 print_spaces (outfile);
5497 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5499 switch (AT_class (a))
5501 case dw_val_class_addr:
5502 fprintf (outfile, "address");
5503 break;
5504 case dw_val_class_offset:
5505 fprintf (outfile, "offset");
5506 break;
5507 case dw_val_class_loc:
5508 fprintf (outfile, "location descriptor");
5509 break;
5510 case dw_val_class_loc_list:
5511 fprintf (outfile, "location list -> label:%s",
5512 AT_loc_list (a)->ll_symbol);
5513 break;
5514 case dw_val_class_range_list:
5515 fprintf (outfile, "range list");
5516 break;
5517 case dw_val_class_const:
5518 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5519 break;
5520 case dw_val_class_unsigned_const:
5521 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5522 break;
5523 case dw_val_class_long_long:
5524 fprintf (outfile, "constant (%lu,%lu)",
5525 a->dw_attr_val.v.val_long_long.hi,
5526 a->dw_attr_val.v.val_long_long.low);
5527 break;
5528 case dw_val_class_vec:
5529 fprintf (outfile, "floating-point or vector constant");
5530 break;
5531 case dw_val_class_flag:
5532 fprintf (outfile, "%u", AT_flag (a));
5533 break;
5534 case dw_val_class_die_ref:
5535 if (AT_ref (a) != NULL)
5537 if (AT_ref (a)->die_symbol)
5538 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
5539 else
5540 fprintf (outfile, "die -> %lu", AT_ref (a)->die_offset);
5542 else
5543 fprintf (outfile, "die -> <null>");
5544 break;
5545 case dw_val_class_lbl_id:
5546 case dw_val_class_lbl_offset:
5547 fprintf (outfile, "label: %s", AT_lbl (a));
5548 break;
5549 case dw_val_class_str:
5550 if (AT_string (a) != NULL)
5551 fprintf (outfile, "\"%s\"", AT_string (a));
5552 else
5553 fprintf (outfile, "<null>");
5554 break;
5555 default:
5556 break;
5559 fprintf (outfile, "\n");
5562 if (die->die_child != NULL)
5564 print_indent += 4;
5565 for (c = die->die_child; c != NULL; c = c->die_sib)
5566 print_die (c, outfile);
5568 print_indent -= 4;
5570 if (print_indent == 0)
5571 fprintf (outfile, "\n");
5574 /* Print the contents of the source code line number correspondence table.
5575 This routine is a debugging aid only. */
5577 static void
5578 print_dwarf_line_table (FILE *outfile)
5580 unsigned i;
5581 dw_line_info_ref line_info;
5583 fprintf (outfile, "\n\nDWARF source line information\n");
5584 for (i = 1; i < line_info_table_in_use; i++)
5586 line_info = &line_info_table[i];
5587 fprintf (outfile, "%5d: ", i);
5588 fprintf (outfile, "%-20s",
5589 VARRAY_CHAR_PTR (file_table, line_info->dw_file_num));
5590 fprintf (outfile, "%6ld", line_info->dw_line_num);
5591 fprintf (outfile, "\n");
5594 fprintf (outfile, "\n\n");
5597 /* Print the information collected for a given DIE. */
5599 void
5600 debug_dwarf_die (dw_die_ref die)
5602 print_die (die, stderr);
5605 /* Print all DWARF information collected for the compilation unit.
5606 This routine is a debugging aid only. */
5608 void
5609 debug_dwarf (void)
5611 print_indent = 0;
5612 print_die (comp_unit_die, stderr);
5613 if (! DWARF2_ASM_LINE_DEBUG_INFO)
5614 print_dwarf_line_table (stderr);
5617 /* We build up the lists of children and attributes by pushing new ones
5618 onto the beginning of the list. Reverse the lists for DIE so that
5619 they are in order of addition. */
5621 static void
5622 reverse_die_lists (dw_die_ref die)
5624 dw_die_ref c, cp, cn;
5625 dw_attr_ref a, ap, an;
5627 for (a = die->die_attr, ap = 0; a; a = an)
5629 an = a->dw_attr_next;
5630 a->dw_attr_next = ap;
5631 ap = a;
5634 die->die_attr = ap;
5636 for (c = die->die_child, cp = 0; c; c = cn)
5638 cn = c->die_sib;
5639 c->die_sib = cp;
5640 cp = c;
5643 die->die_child = cp;
5646 /* reverse_die_lists only reverses the single die you pass it. Since we used to
5647 reverse all dies in add_sibling_attributes, which runs through all the dies,
5648 it would reverse all the dies. Now, however, since we don't call
5649 reverse_die_lists in add_sibling_attributes, we need a routine to
5650 recursively reverse all the dies. This is that routine. */
5652 static void
5653 reverse_all_dies (dw_die_ref die)
5655 dw_die_ref c;
5657 reverse_die_lists (die);
5659 for (c = die->die_child; c; c = c->die_sib)
5660 reverse_all_dies (c);
5663 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5664 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5665 DIE that marks the start of the DIEs for this include file. */
5667 static dw_die_ref
5668 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5670 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5671 dw_die_ref new_unit = gen_compile_unit_die (filename);
5673 new_unit->die_sib = old_unit;
5674 return new_unit;
5677 /* Close an include-file CU and reopen the enclosing one. */
5679 static dw_die_ref
5680 pop_compile_unit (dw_die_ref old_unit)
5682 dw_die_ref new_unit = old_unit->die_sib;
5684 old_unit->die_sib = NULL;
5685 return new_unit;
5688 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5689 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5691 /* Calculate the checksum of a location expression. */
5693 static inline void
5694 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5696 CHECKSUM (loc->dw_loc_opc);
5697 CHECKSUM (loc->dw_loc_oprnd1);
5698 CHECKSUM (loc->dw_loc_oprnd2);
5701 /* Calculate the checksum of an attribute. */
5703 static void
5704 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5706 dw_loc_descr_ref loc;
5707 rtx r;
5709 CHECKSUM (at->dw_attr);
5711 /* We don't care about differences in file numbering. */
5712 if (at->dw_attr == DW_AT_decl_file
5713 /* Or that this was compiled with a different compiler snapshot; if
5714 the output is the same, that's what matters. */
5715 || at->dw_attr == DW_AT_producer)
5716 return;
5718 switch (AT_class (at))
5720 case dw_val_class_const:
5721 CHECKSUM (at->dw_attr_val.v.val_int);
5722 break;
5723 case dw_val_class_unsigned_const:
5724 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5725 break;
5726 case dw_val_class_long_long:
5727 CHECKSUM (at->dw_attr_val.v.val_long_long);
5728 break;
5729 case dw_val_class_vec:
5730 CHECKSUM (at->dw_attr_val.v.val_vec);
5731 break;
5732 case dw_val_class_flag:
5733 CHECKSUM (at->dw_attr_val.v.val_flag);
5734 break;
5735 case dw_val_class_str:
5736 CHECKSUM_STRING (AT_string (at));
5737 break;
5739 case dw_val_class_addr:
5740 r = AT_addr (at);
5741 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5742 CHECKSUM_STRING (XSTR (r, 0));
5743 break;
5745 case dw_val_class_offset:
5746 CHECKSUM (at->dw_attr_val.v.val_offset);
5747 break;
5749 case dw_val_class_loc:
5750 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5751 loc_checksum (loc, ctx);
5752 break;
5754 case dw_val_class_die_ref:
5755 die_checksum (AT_ref (at), ctx, mark);
5756 break;
5758 case dw_val_class_fde_ref:
5759 case dw_val_class_lbl_id:
5760 case dw_val_class_lbl_offset:
5761 break;
5763 default:
5764 break;
5768 /* Calculate the checksum of a DIE. */
5770 static void
5771 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5773 dw_die_ref c;
5774 dw_attr_ref a;
5776 /* To avoid infinite recursion. */
5777 if (die->die_mark)
5779 CHECKSUM (die->die_mark);
5780 return;
5782 die->die_mark = ++(*mark);
5784 CHECKSUM (die->die_tag);
5786 for (a = die->die_attr; a; a = a->dw_attr_next)
5787 attr_checksum (a, ctx, mark);
5789 for (c = die->die_child; c; c = c->die_sib)
5790 die_checksum (c, ctx, mark);
5793 #undef CHECKSUM
5794 #undef CHECKSUM_STRING
5796 /* Do the location expressions look same? */
5797 static inline int
5798 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
5800 return loc1->dw_loc_opc == loc2->dw_loc_opc
5801 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
5802 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
5805 /* Do the values look the same? */
5806 static int
5807 same_dw_val_p (dw_val_node *v1, dw_val_node *v2, int *mark)
5809 dw_loc_descr_ref loc1, loc2;
5810 rtx r1, r2;
5812 if (v1->val_class != v2->val_class)
5813 return 0;
5815 switch (v1->val_class)
5817 case dw_val_class_const:
5818 return v1->v.val_int == v2->v.val_int;
5819 case dw_val_class_unsigned_const:
5820 return v1->v.val_unsigned == v2->v.val_unsigned;
5821 case dw_val_class_long_long:
5822 return v1->v.val_long_long.hi == v2->v.val_long_long.hi
5823 && v1->v.val_long_long.low == v2->v.val_long_long.low;
5824 case dw_val_class_vec:
5825 if (v1->v.val_vec.length != v2->v.val_vec.length
5826 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
5827 return 0;
5828 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
5829 v1->v.val_vec.length * v1->v.val_vec.elt_size))
5830 return 0;
5831 return 1;
5832 case dw_val_class_flag:
5833 return v1->v.val_flag == v2->v.val_flag;
5834 case dw_val_class_str:
5835 return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
5837 case dw_val_class_addr:
5838 r1 = v1->v.val_addr;
5839 r2 = v2->v.val_addr;
5840 if (GET_CODE (r1) != GET_CODE (r2))
5841 return 0;
5842 gcc_assert (GET_CODE (r1) == SYMBOL_REF);
5843 return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
5845 case dw_val_class_offset:
5846 return v1->v.val_offset == v2->v.val_offset;
5848 case dw_val_class_loc:
5849 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
5850 loc1 && loc2;
5851 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
5852 if (!same_loc_p (loc1, loc2, mark))
5853 return 0;
5854 return !loc1 && !loc2;
5856 case dw_val_class_die_ref:
5857 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
5859 case dw_val_class_fde_ref:
5860 case dw_val_class_lbl_id:
5861 case dw_val_class_lbl_offset:
5862 return 1;
5864 default:
5865 return 1;
5869 /* Do the attributes look the same? */
5871 static int
5872 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
5874 if (at1->dw_attr != at2->dw_attr)
5875 return 0;
5877 /* We don't care about differences in file numbering. */
5878 if (at1->dw_attr == DW_AT_decl_file
5879 /* Or that this was compiled with a different compiler snapshot; if
5880 the output is the same, that's what matters. */
5881 || at1->dw_attr == DW_AT_producer)
5882 return 1;
5884 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
5887 /* Do the dies look the same? */
5889 static int
5890 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
5892 dw_die_ref c1, c2;
5893 dw_attr_ref a1, a2;
5895 /* To avoid infinite recursion. */
5896 if (die1->die_mark)
5897 return die1->die_mark == die2->die_mark;
5898 die1->die_mark = die2->die_mark = ++(*mark);
5900 if (die1->die_tag != die2->die_tag)
5901 return 0;
5903 for (a1 = die1->die_attr, a2 = die2->die_attr;
5904 a1 && a2;
5905 a1 = a1->dw_attr_next, a2 = a2->dw_attr_next)
5906 if (!same_attr_p (a1, a2, mark))
5907 return 0;
5908 if (a1 || a2)
5909 return 0;
5911 for (c1 = die1->die_child, c2 = die2->die_child;
5912 c1 && c2;
5913 c1 = c1->die_sib, c2 = c2->die_sib)
5914 if (!same_die_p (c1, c2, mark))
5915 return 0;
5916 if (c1 || c2)
5917 return 0;
5919 return 1;
5922 /* Do the dies look the same? Wrapper around same_die_p. */
5924 static int
5925 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
5927 int mark = 0;
5928 int ret = same_die_p (die1, die2, &mark);
5930 unmark_all_dies (die1);
5931 unmark_all_dies (die2);
5933 return ret;
5936 /* The prefix to attach to symbols on DIEs in the current comdat debug
5937 info section. */
5938 static char *comdat_symbol_id;
5940 /* The index of the current symbol within the current comdat CU. */
5941 static unsigned int comdat_symbol_number;
5943 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
5944 children, and set comdat_symbol_id accordingly. */
5946 static void
5947 compute_section_prefix (dw_die_ref unit_die)
5949 const char *die_name = get_AT_string (unit_die, DW_AT_name);
5950 const char *base = die_name ? lbasename (die_name) : "anonymous";
5951 char *name = alloca (strlen (base) + 64);
5952 char *p;
5953 int i, mark;
5954 unsigned char checksum[16];
5955 struct md5_ctx ctx;
5957 /* Compute the checksum of the DIE, then append part of it as hex digits to
5958 the name filename of the unit. */
5960 md5_init_ctx (&ctx);
5961 mark = 0;
5962 die_checksum (unit_die, &ctx, &mark);
5963 unmark_all_dies (unit_die);
5964 md5_finish_ctx (&ctx, checksum);
5966 sprintf (name, "%s.", base);
5967 clean_symbol_name (name);
5969 p = name + strlen (name);
5970 for (i = 0; i < 4; i++)
5972 sprintf (p, "%.2x", checksum[i]);
5973 p += 2;
5976 comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
5977 comdat_symbol_number = 0;
5980 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
5982 static int
5983 is_type_die (dw_die_ref die)
5985 switch (die->die_tag)
5987 case DW_TAG_array_type:
5988 case DW_TAG_class_type:
5989 case DW_TAG_enumeration_type:
5990 case DW_TAG_pointer_type:
5991 case DW_TAG_reference_type:
5992 case DW_TAG_string_type:
5993 case DW_TAG_structure_type:
5994 case DW_TAG_subroutine_type:
5995 case DW_TAG_union_type:
5996 case DW_TAG_ptr_to_member_type:
5997 case DW_TAG_set_type:
5998 case DW_TAG_subrange_type:
5999 case DW_TAG_base_type:
6000 case DW_TAG_const_type:
6001 case DW_TAG_file_type:
6002 case DW_TAG_packed_type:
6003 case DW_TAG_volatile_type:
6004 case DW_TAG_typedef:
6005 return 1;
6006 default:
6007 return 0;
6011 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6012 Basically, we want to choose the bits that are likely to be shared between
6013 compilations (types) and leave out the bits that are specific to individual
6014 compilations (functions). */
6016 static int
6017 is_comdat_die (dw_die_ref c)
6019 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6020 we do for stabs. The advantage is a greater likelihood of sharing between
6021 objects that don't include headers in the same order (and therefore would
6022 put the base types in a different comdat). jason 8/28/00 */
6024 if (c->die_tag == DW_TAG_base_type)
6025 return 0;
6027 if (c->die_tag == DW_TAG_pointer_type
6028 || c->die_tag == DW_TAG_reference_type
6029 || c->die_tag == DW_TAG_const_type
6030 || c->die_tag == DW_TAG_volatile_type)
6032 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6034 return t ? is_comdat_die (t) : 0;
6037 return is_type_die (c);
6040 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6041 compilation unit. */
6043 static int
6044 is_symbol_die (dw_die_ref c)
6046 return (is_type_die (c)
6047 || (get_AT (c, DW_AT_declaration)
6048 && !get_AT (c, DW_AT_specification)));
6051 static char *
6052 gen_internal_sym (const char *prefix)
6054 char buf[256];
6056 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6057 return xstrdup (buf);
6060 /* Assign symbols to all worthy DIEs under DIE. */
6062 static void
6063 assign_symbol_names (dw_die_ref die)
6065 dw_die_ref c;
6067 if (is_symbol_die (die))
6069 if (comdat_symbol_id)
6071 char *p = alloca (strlen (comdat_symbol_id) + 64);
6073 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6074 comdat_symbol_id, comdat_symbol_number++);
6075 die->die_symbol = xstrdup (p);
6077 else
6078 die->die_symbol = gen_internal_sym ("LDIE");
6081 for (c = die->die_child; c != NULL; c = c->die_sib)
6082 assign_symbol_names (c);
6085 struct cu_hash_table_entry
6087 dw_die_ref cu;
6088 unsigned min_comdat_num, max_comdat_num;
6089 struct cu_hash_table_entry *next;
6092 /* Routines to manipulate hash table of CUs. */
6093 static hashval_t
6094 htab_cu_hash (const void *of)
6096 const struct cu_hash_table_entry *entry = of;
6098 return htab_hash_string (entry->cu->die_symbol);
6101 static int
6102 htab_cu_eq (const void *of1, const void *of2)
6104 const struct cu_hash_table_entry *entry1 = of1;
6105 const struct die_struct *entry2 = of2;
6107 return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
6110 static void
6111 htab_cu_del (void *what)
6113 struct cu_hash_table_entry *next, *entry = what;
6115 while (entry)
6117 next = entry->next;
6118 free (entry);
6119 entry = next;
6123 /* Check whether we have already seen this CU and set up SYM_NUM
6124 accordingly. */
6125 static int
6126 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6128 struct cu_hash_table_entry dummy;
6129 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6131 dummy.max_comdat_num = 0;
6133 slot = (struct cu_hash_table_entry **)
6134 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6135 INSERT);
6136 entry = *slot;
6138 for (; entry; last = entry, entry = entry->next)
6140 if (same_die_p_wrap (cu, entry->cu))
6141 break;
6144 if (entry)
6146 *sym_num = entry->min_comdat_num;
6147 return 1;
6150 entry = xcalloc (1, sizeof (struct cu_hash_table_entry));
6151 entry->cu = cu;
6152 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6153 entry->next = *slot;
6154 *slot = entry;
6156 return 0;
6159 /* Record SYM_NUM to record of CU in HTABLE. */
6160 static void
6161 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6163 struct cu_hash_table_entry **slot, *entry;
6165 slot = (struct cu_hash_table_entry **)
6166 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6167 NO_INSERT);
6168 entry = *slot;
6170 entry->max_comdat_num = sym_num;
6173 /* Traverse the DIE (which is always comp_unit_die), and set up
6174 additional compilation units for each of the include files we see
6175 bracketed by BINCL/EINCL. */
6177 static void
6178 break_out_includes (dw_die_ref die)
6180 dw_die_ref *ptr;
6181 dw_die_ref unit = NULL;
6182 limbo_die_node *node, **pnode;
6183 htab_t cu_hash_table;
6185 for (ptr = &(die->die_child); *ptr;)
6187 dw_die_ref c = *ptr;
6189 if (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6190 || (unit && is_comdat_die (c)))
6192 /* This DIE is for a secondary CU; remove it from the main one. */
6193 *ptr = c->die_sib;
6195 if (c->die_tag == DW_TAG_GNU_BINCL)
6197 unit = push_new_compile_unit (unit, c);
6198 free_die (c);
6200 else if (c->die_tag == DW_TAG_GNU_EINCL)
6202 unit = pop_compile_unit (unit);
6203 free_die (c);
6205 else
6206 add_child_die (unit, c);
6208 else
6210 /* Leave this DIE in the main CU. */
6211 ptr = &(c->die_sib);
6212 continue;
6216 #if 0
6217 /* We can only use this in debugging, since the frontend doesn't check
6218 to make sure that we leave every include file we enter. */
6219 gcc_assert (!unit);
6220 #endif
6222 assign_symbol_names (die);
6223 cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6224 for (node = limbo_die_list, pnode = &limbo_die_list;
6225 node;
6226 node = node->next)
6228 int is_dupl;
6230 compute_section_prefix (node->die);
6231 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6232 &comdat_symbol_number);
6233 assign_symbol_names (node->die);
6234 if (is_dupl)
6235 *pnode = node->next;
6236 else
6238 pnode = &node->next;
6239 record_comdat_symbol_number (node->die, cu_hash_table,
6240 comdat_symbol_number);
6243 htab_delete (cu_hash_table);
6246 /* Traverse the DIE and add a sibling attribute if it may have the
6247 effect of speeding up access to siblings. To save some space,
6248 avoid generating sibling attributes for DIE's without children. */
6250 static void
6251 add_sibling_attributes (dw_die_ref die)
6253 dw_die_ref c;
6255 if (die->die_tag != DW_TAG_compile_unit
6256 && die->die_sib && die->die_child != NULL)
6257 /* Add the sibling link to the front of the attribute list. */
6258 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
6260 for (c = die->die_child; c != NULL; c = c->die_sib)
6261 add_sibling_attributes (c);
6264 /* Output all location lists for the DIE and its children. */
6266 static void
6267 output_location_lists (dw_die_ref die)
6269 dw_die_ref c;
6270 dw_attr_ref d_attr;
6272 for (d_attr = die->die_attr; d_attr; d_attr = d_attr->dw_attr_next)
6273 if (AT_class (d_attr) == dw_val_class_loc_list)
6274 output_loc_list (AT_loc_list (d_attr));
6276 for (c = die->die_child; c != NULL; c = c->die_sib)
6277 output_location_lists (c);
6281 /* The format of each DIE (and its attribute value pairs) is encoded in an
6282 abbreviation table. This routine builds the abbreviation table and assigns
6283 a unique abbreviation id for each abbreviation entry. The children of each
6284 die are visited recursively. */
6286 static void
6287 build_abbrev_table (dw_die_ref die)
6289 unsigned long abbrev_id;
6290 unsigned int n_alloc;
6291 dw_die_ref c;
6292 dw_attr_ref d_attr, a_attr;
6294 /* Scan the DIE references, and mark as external any that refer to
6295 DIEs from other CUs (i.e. those which are not marked). */
6296 for (d_attr = die->die_attr; d_attr; d_attr = d_attr->dw_attr_next)
6297 if (AT_class (d_attr) == dw_val_class_die_ref
6298 && AT_ref (d_attr)->die_mark == 0)
6300 gcc_assert (AT_ref (d_attr)->die_symbol);
6302 set_AT_ref_external (d_attr, 1);
6305 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6307 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6309 if (abbrev->die_tag == die->die_tag)
6311 if ((abbrev->die_child != NULL) == (die->die_child != NULL))
6313 a_attr = abbrev->die_attr;
6314 d_attr = die->die_attr;
6316 while (a_attr != NULL && d_attr != NULL)
6318 if ((a_attr->dw_attr != d_attr->dw_attr)
6319 || (value_format (a_attr) != value_format (d_attr)))
6320 break;
6322 a_attr = a_attr->dw_attr_next;
6323 d_attr = d_attr->dw_attr_next;
6326 if (a_attr == NULL && d_attr == NULL)
6327 break;
6332 if (abbrev_id >= abbrev_die_table_in_use)
6334 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
6336 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
6337 abbrev_die_table = ggc_realloc (abbrev_die_table,
6338 sizeof (dw_die_ref) * n_alloc);
6340 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
6341 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
6342 abbrev_die_table_allocated = n_alloc;
6345 ++abbrev_die_table_in_use;
6346 abbrev_die_table[abbrev_id] = die;
6349 die->die_abbrev = abbrev_id;
6350 for (c = die->die_child; c != NULL; c = c->die_sib)
6351 build_abbrev_table (c);
6354 /* Return the power-of-two number of bytes necessary to represent VALUE. */
6356 static int
6357 constant_size (long unsigned int value)
6359 int log;
6361 if (value == 0)
6362 log = 0;
6363 else
6364 log = floor_log2 (value);
6366 log = log / 8;
6367 log = 1 << (floor_log2 (log) + 1);
6369 return log;
6372 /* Return the size of a DIE as it is represented in the
6373 .debug_info section. */
6375 static unsigned long
6376 size_of_die (dw_die_ref die)
6378 unsigned long size = 0;
6379 dw_attr_ref a;
6381 size += size_of_uleb128 (die->die_abbrev);
6382 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
6384 switch (AT_class (a))
6386 case dw_val_class_addr:
6387 size += DWARF2_ADDR_SIZE;
6388 break;
6389 case dw_val_class_offset:
6390 size += DWARF_OFFSET_SIZE;
6391 break;
6392 case dw_val_class_loc:
6394 unsigned long lsize = size_of_locs (AT_loc (a));
6396 /* Block length. */
6397 size += constant_size (lsize);
6398 size += lsize;
6400 break;
6401 case dw_val_class_loc_list:
6402 size += DWARF_OFFSET_SIZE;
6403 break;
6404 case dw_val_class_range_list:
6405 size += DWARF_OFFSET_SIZE;
6406 break;
6407 case dw_val_class_const:
6408 size += size_of_sleb128 (AT_int (a));
6409 break;
6410 case dw_val_class_unsigned_const:
6411 size += constant_size (AT_unsigned (a));
6412 break;
6413 case dw_val_class_long_long:
6414 size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
6415 break;
6416 case dw_val_class_vec:
6417 size += 1 + (a->dw_attr_val.v.val_vec.length
6418 * a->dw_attr_val.v.val_vec.elt_size); /* block */
6419 break;
6420 case dw_val_class_flag:
6421 size += 1;
6422 break;
6423 case dw_val_class_die_ref:
6424 if (AT_ref_external (a))
6425 size += DWARF2_ADDR_SIZE;
6426 else
6427 size += DWARF_OFFSET_SIZE;
6428 break;
6429 case dw_val_class_fde_ref:
6430 size += DWARF_OFFSET_SIZE;
6431 break;
6432 case dw_val_class_lbl_id:
6433 size += DWARF2_ADDR_SIZE;
6434 break;
6435 case dw_val_class_lbl_offset:
6436 size += DWARF_OFFSET_SIZE;
6437 break;
6438 case dw_val_class_str:
6439 if (AT_string_form (a) == DW_FORM_strp)
6440 size += DWARF_OFFSET_SIZE;
6441 else
6442 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
6443 break;
6444 default:
6445 gcc_unreachable ();
6449 return size;
6452 /* Size the debugging information associated with a given DIE. Visits the
6453 DIE's children recursively. Updates the global variable next_die_offset, on
6454 each time through. Uses the current value of next_die_offset to update the
6455 die_offset field in each DIE. */
6457 static void
6458 calc_die_sizes (dw_die_ref die)
6460 dw_die_ref c;
6462 die->die_offset = next_die_offset;
6463 next_die_offset += size_of_die (die);
6465 for (c = die->die_child; c != NULL; c = c->die_sib)
6466 calc_die_sizes (c);
6468 if (die->die_child != NULL)
6469 /* Count the null byte used to terminate sibling lists. */
6470 next_die_offset += 1;
6473 /* Set the marks for a die and its children. We do this so
6474 that we know whether or not a reference needs to use FORM_ref_addr; only
6475 DIEs in the same CU will be marked. We used to clear out the offset
6476 and use that as the flag, but ran into ordering problems. */
6478 static void
6479 mark_dies (dw_die_ref die)
6481 dw_die_ref c;
6483 gcc_assert (!die->die_mark);
6485 die->die_mark = 1;
6486 for (c = die->die_child; c; c = c->die_sib)
6487 mark_dies (c);
6490 /* Clear the marks for a die and its children. */
6492 static void
6493 unmark_dies (dw_die_ref die)
6495 dw_die_ref c;
6497 gcc_assert (die->die_mark);
6499 die->die_mark = 0;
6500 for (c = die->die_child; c; c = c->die_sib)
6501 unmark_dies (c);
6504 /* Clear the marks for a die, its children and referred dies. */
6506 static void
6507 unmark_all_dies (dw_die_ref die)
6509 dw_die_ref c;
6510 dw_attr_ref a;
6512 if (!die->die_mark)
6513 return;
6514 die->die_mark = 0;
6516 for (c = die->die_child; c; c = c->die_sib)
6517 unmark_all_dies (c);
6519 for (a = die->die_attr; a; a = a->dw_attr_next)
6520 if (AT_class (a) == dw_val_class_die_ref)
6521 unmark_all_dies (AT_ref (a));
6524 /* Return the size of the .debug_pubnames table generated for the
6525 compilation unit. */
6527 static unsigned long
6528 size_of_pubnames (void)
6530 unsigned long size;
6531 unsigned i;
6533 size = DWARF_PUBNAMES_HEADER_SIZE;
6534 for (i = 0; i < pubname_table_in_use; i++)
6536 pubname_ref p = &pubname_table[i];
6537 size += DWARF_OFFSET_SIZE + strlen (p->name) + 1;
6540 size += DWARF_OFFSET_SIZE;
6541 return size;
6544 /* Return the size of the information in the .debug_aranges section. */
6546 static unsigned long
6547 size_of_aranges (void)
6549 unsigned long size;
6551 size = DWARF_ARANGES_HEADER_SIZE;
6553 /* Count the address/length pair for this compilation unit. */
6554 size += 2 * DWARF2_ADDR_SIZE;
6555 size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
6557 /* Count the two zero words used to terminated the address range table. */
6558 size += 2 * DWARF2_ADDR_SIZE;
6559 return size;
6562 /* Select the encoding of an attribute value. */
6564 static enum dwarf_form
6565 value_format (dw_attr_ref a)
6567 switch (a->dw_attr_val.val_class)
6569 case dw_val_class_addr:
6570 return DW_FORM_addr;
6571 case dw_val_class_range_list:
6572 case dw_val_class_offset:
6573 switch (DWARF_OFFSET_SIZE)
6575 case 4:
6576 return DW_FORM_data4;
6577 case 8:
6578 return DW_FORM_data8;
6579 default:
6580 gcc_unreachable ();
6582 case dw_val_class_loc_list:
6583 /* FIXME: Could be DW_FORM_data8, with a > 32 bit size
6584 .debug_loc section */
6585 return DW_FORM_data4;
6586 case dw_val_class_loc:
6587 switch (constant_size (size_of_locs (AT_loc (a))))
6589 case 1:
6590 return DW_FORM_block1;
6591 case 2:
6592 return DW_FORM_block2;
6593 default:
6594 gcc_unreachable ();
6596 case dw_val_class_const:
6597 return DW_FORM_sdata;
6598 case dw_val_class_unsigned_const:
6599 switch (constant_size (AT_unsigned (a)))
6601 case 1:
6602 return DW_FORM_data1;
6603 case 2:
6604 return DW_FORM_data2;
6605 case 4:
6606 return DW_FORM_data4;
6607 case 8:
6608 return DW_FORM_data8;
6609 default:
6610 gcc_unreachable ();
6612 case dw_val_class_long_long:
6613 return DW_FORM_block1;
6614 case dw_val_class_vec:
6615 return DW_FORM_block1;
6616 case dw_val_class_flag:
6617 return DW_FORM_flag;
6618 case dw_val_class_die_ref:
6619 if (AT_ref_external (a))
6620 return DW_FORM_ref_addr;
6621 else
6622 return DW_FORM_ref;
6623 case dw_val_class_fde_ref:
6624 return DW_FORM_data;
6625 case dw_val_class_lbl_id:
6626 return DW_FORM_addr;
6627 case dw_val_class_lbl_offset:
6628 return DW_FORM_data;
6629 case dw_val_class_str:
6630 return AT_string_form (a);
6632 default:
6633 gcc_unreachable ();
6637 /* Output the encoding of an attribute value. */
6639 static void
6640 output_value_format (dw_attr_ref a)
6642 enum dwarf_form form = value_format (a);
6644 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
6647 /* Output the .debug_abbrev section which defines the DIE abbreviation
6648 table. */
6650 static void
6651 output_abbrev_section (void)
6653 unsigned long abbrev_id;
6655 dw_attr_ref a_attr;
6657 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6659 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6661 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
6662 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
6663 dwarf_tag_name (abbrev->die_tag));
6665 if (abbrev->die_child != NULL)
6666 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
6667 else
6668 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
6670 for (a_attr = abbrev->die_attr; a_attr != NULL;
6671 a_attr = a_attr->dw_attr_next)
6673 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
6674 dwarf_attr_name (a_attr->dw_attr));
6675 output_value_format (a_attr);
6678 dw2_asm_output_data (1, 0, NULL);
6679 dw2_asm_output_data (1, 0, NULL);
6682 /* Terminate the table. */
6683 dw2_asm_output_data (1, 0, NULL);
6686 /* Output a symbol we can use to refer to this DIE from another CU. */
6688 static inline void
6689 output_die_symbol (dw_die_ref die)
6691 char *sym = die->die_symbol;
6693 if (sym == 0)
6694 return;
6696 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
6697 /* We make these global, not weak; if the target doesn't support
6698 .linkonce, it doesn't support combining the sections, so debugging
6699 will break. */
6700 targetm.asm_out.globalize_label (asm_out_file, sym);
6702 ASM_OUTPUT_LABEL (asm_out_file, sym);
6705 /* Return a new location list, given the begin and end range, and the
6706 expression. gensym tells us whether to generate a new internal symbol for
6707 this location list node, which is done for the head of the list only. */
6709 static inline dw_loc_list_ref
6710 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
6711 const char *section, unsigned int gensym)
6713 dw_loc_list_ref retlist = ggc_alloc_cleared (sizeof (dw_loc_list_node));
6715 retlist->begin = begin;
6716 retlist->end = end;
6717 retlist->expr = expr;
6718 retlist->section = section;
6719 if (gensym)
6720 retlist->ll_symbol = gen_internal_sym ("LLST");
6722 return retlist;
6725 /* Add a location description expression to a location list. */
6727 static inline void
6728 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
6729 const char *begin, const char *end,
6730 const char *section)
6732 dw_loc_list_ref *d;
6734 /* Find the end of the chain. */
6735 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
6738 /* Add a new location list node to the list. */
6739 *d = new_loc_list (descr, begin, end, section, 0);
6742 /* Output the location list given to us. */
6744 static void
6745 output_loc_list (dw_loc_list_ref list_head)
6747 dw_loc_list_ref curr = list_head;
6749 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
6751 /* Walk the location list, and output each range + expression. */
6752 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
6754 unsigned long size;
6755 if (separate_line_info_table_in_use == 0)
6757 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
6758 "Location list begin address (%s)",
6759 list_head->ll_symbol);
6760 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
6761 "Location list end address (%s)",
6762 list_head->ll_symbol);
6764 else
6766 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
6767 "Location list begin address (%s)",
6768 list_head->ll_symbol);
6769 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
6770 "Location list end address (%s)",
6771 list_head->ll_symbol);
6773 size = size_of_locs (curr->expr);
6775 /* Output the block length for this list of location operations. */
6776 gcc_assert (size <= 0xffff);
6777 dw2_asm_output_data (2, size, "%s", "Location expression size");
6779 output_loc_sequence (curr->expr);
6782 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
6783 "Location list terminator begin (%s)",
6784 list_head->ll_symbol);
6785 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
6786 "Location list terminator end (%s)",
6787 list_head->ll_symbol);
6790 /* Output the DIE and its attributes. Called recursively to generate
6791 the definitions of each child DIE. */
6793 static void
6794 output_die (dw_die_ref die)
6796 dw_attr_ref a;
6797 dw_die_ref c;
6798 unsigned long size;
6800 /* If someone in another CU might refer to us, set up a symbol for
6801 them to point to. */
6802 if (die->die_symbol)
6803 output_die_symbol (die);
6805 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
6806 die->die_offset, dwarf_tag_name (die->die_tag));
6808 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
6810 const char *name = dwarf_attr_name (a->dw_attr);
6812 switch (AT_class (a))
6814 case dw_val_class_addr:
6815 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
6816 break;
6818 case dw_val_class_offset:
6819 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
6820 "%s", name);
6821 break;
6823 case dw_val_class_range_list:
6825 char *p = strchr (ranges_section_label, '\0');
6827 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
6828 a->dw_attr_val.v.val_offset);
6829 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
6830 "%s", name);
6831 *p = '\0';
6833 break;
6835 case dw_val_class_loc:
6836 size = size_of_locs (AT_loc (a));
6838 /* Output the block length for this list of location operations. */
6839 dw2_asm_output_data (constant_size (size), size, "%s", name);
6841 output_loc_sequence (AT_loc (a));
6842 break;
6844 case dw_val_class_const:
6845 /* ??? It would be slightly more efficient to use a scheme like is
6846 used for unsigned constants below, but gdb 4.x does not sign
6847 extend. Gdb 5.x does sign extend. */
6848 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
6849 break;
6851 case dw_val_class_unsigned_const:
6852 dw2_asm_output_data (constant_size (AT_unsigned (a)),
6853 AT_unsigned (a), "%s", name);
6854 break;
6856 case dw_val_class_long_long:
6858 unsigned HOST_WIDE_INT first, second;
6860 dw2_asm_output_data (1,
6861 2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
6862 "%s", name);
6864 if (WORDS_BIG_ENDIAN)
6866 first = a->dw_attr_val.v.val_long_long.hi;
6867 second = a->dw_attr_val.v.val_long_long.low;
6869 else
6871 first = a->dw_attr_val.v.val_long_long.low;
6872 second = a->dw_attr_val.v.val_long_long.hi;
6875 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
6876 first, "long long constant");
6877 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
6878 second, NULL);
6880 break;
6882 case dw_val_class_vec:
6884 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
6885 unsigned int len = a->dw_attr_val.v.val_vec.length;
6886 unsigned int i;
6887 unsigned char *p;
6889 dw2_asm_output_data (1, len * elt_size, "%s", name);
6890 if (elt_size > sizeof (HOST_WIDE_INT))
6892 elt_size /= 2;
6893 len *= 2;
6895 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
6896 i < len;
6897 i++, p += elt_size)
6898 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
6899 "fp or vector constant word %u", i);
6900 break;
6903 case dw_val_class_flag:
6904 dw2_asm_output_data (1, AT_flag (a), "%s", name);
6905 break;
6907 case dw_val_class_loc_list:
6909 char *sym = AT_loc_list (a)->ll_symbol;
6911 gcc_assert (sym);
6912 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, "%s", name);
6914 break;
6916 case dw_val_class_die_ref:
6917 if (AT_ref_external (a))
6919 char *sym = AT_ref (a)->die_symbol;
6921 gcc_assert (sym);
6922 dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, "%s", name);
6924 else
6926 gcc_assert (AT_ref (a)->die_offset);
6927 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
6928 "%s", name);
6930 break;
6932 case dw_val_class_fde_ref:
6934 char l1[20];
6936 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
6937 a->dw_attr_val.v.val_fde_index * 2);
6938 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, "%s", name);
6940 break;
6942 case dw_val_class_lbl_id:
6943 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
6944 break;
6946 case dw_val_class_lbl_offset:
6947 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a), "%s", name);
6948 break;
6950 case dw_val_class_str:
6951 if (AT_string_form (a) == DW_FORM_strp)
6952 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
6953 a->dw_attr_val.v.val_str->label,
6954 "%s: \"%s\"", name, AT_string (a));
6955 else
6956 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
6957 break;
6959 default:
6960 gcc_unreachable ();
6964 for (c = die->die_child; c != NULL; c = c->die_sib)
6965 output_die (c);
6967 /* Add null byte to terminate sibling list. */
6968 if (die->die_child != NULL)
6969 dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
6970 die->die_offset);
6973 /* Output the compilation unit that appears at the beginning of the
6974 .debug_info section, and precedes the DIE descriptions. */
6976 static void
6977 output_compilation_unit_header (void)
6979 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
6980 dw2_asm_output_data (4, 0xffffffff,
6981 "Initial length escape value indicating 64-bit DWARF extension");
6982 dw2_asm_output_data (DWARF_OFFSET_SIZE,
6983 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
6984 "Length of Compilation Unit Info");
6985 dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
6986 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
6987 "Offset Into Abbrev. Section");
6988 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
6991 /* Output the compilation unit DIE and its children. */
6993 static void
6994 output_comp_unit (dw_die_ref die, int output_if_empty)
6996 const char *secname;
6997 char *oldsym, *tmp;
6999 /* Unless we are outputting main CU, we may throw away empty ones. */
7000 if (!output_if_empty && die->die_child == NULL)
7001 return;
7003 /* Even if there are no children of this DIE, we must output the information
7004 about the compilation unit. Otherwise, on an empty translation unit, we
7005 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
7006 will then complain when examining the file. First mark all the DIEs in
7007 this CU so we know which get local refs. */
7008 mark_dies (die);
7010 build_abbrev_table (die);
7012 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
7013 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7014 calc_die_sizes (die);
7016 oldsym = die->die_symbol;
7017 if (oldsym)
7019 tmp = alloca (strlen (oldsym) + 24);
7021 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
7022 secname = tmp;
7023 die->die_symbol = NULL;
7025 else
7026 secname = (const char *) DEBUG_INFO_SECTION;
7028 /* Output debugging information. */
7029 named_section_flags (secname, SECTION_DEBUG);
7030 output_compilation_unit_header ();
7031 output_die (die);
7033 /* Leave the marks on the main CU, so we can check them in
7034 output_pubnames. */
7035 if (oldsym)
7037 unmark_dies (die);
7038 die->die_symbol = oldsym;
7042 /* The DWARF2 pubname for a nested thingy looks like "A::f". The
7043 output of lang_hooks.decl_printable_name for C++ looks like
7044 "A::f(int)". Let's drop the argument list, and maybe the scope. */
7046 static const char *
7047 dwarf2_name (tree decl, int scope)
7049 return lang_hooks.decl_printable_name (decl, scope ? 1 : 0);
7052 /* Add a new entry to .debug_pubnames if appropriate. */
7054 static void
7055 add_pubname (tree decl, dw_die_ref die)
7057 pubname_ref p;
7059 if (! TREE_PUBLIC (decl))
7060 return;
7062 if (pubname_table_in_use == pubname_table_allocated)
7064 pubname_table_allocated += PUBNAME_TABLE_INCREMENT;
7065 pubname_table
7066 = ggc_realloc (pubname_table,
7067 (pubname_table_allocated * sizeof (pubname_entry)));
7068 memset (pubname_table + pubname_table_in_use, 0,
7069 PUBNAME_TABLE_INCREMENT * sizeof (pubname_entry));
7072 p = &pubname_table[pubname_table_in_use++];
7073 p->die = die;
7074 p->name = xstrdup (dwarf2_name (decl, 1));
7077 /* Output the public names table used to speed up access to externally
7078 visible names. For now, only generate entries for externally
7079 visible procedures. */
7081 static void
7082 output_pubnames (void)
7084 unsigned i;
7085 unsigned long pubnames_length = size_of_pubnames ();
7087 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7088 dw2_asm_output_data (4, 0xffffffff,
7089 "Initial length escape value indicating 64-bit DWARF extension");
7090 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7091 "Length of Public Names Info");
7092 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7093 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7094 "Offset of Compilation Unit Info");
7095 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
7096 "Compilation Unit Length");
7098 for (i = 0; i < pubname_table_in_use; i++)
7100 pubname_ref pub = &pubname_table[i];
7102 /* We shouldn't see pubnames for DIEs outside of the main CU. */
7103 gcc_assert (pub->die->die_mark);
7105 dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
7106 "DIE offset");
7108 dw2_asm_output_nstring (pub->name, -1, "external name");
7111 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
7114 /* Add a new entry to .debug_aranges if appropriate. */
7116 static void
7117 add_arange (tree decl, dw_die_ref die)
7119 if (! DECL_SECTION_NAME (decl))
7120 return;
7122 if (arange_table_in_use == arange_table_allocated)
7124 arange_table_allocated += ARANGE_TABLE_INCREMENT;
7125 arange_table = ggc_realloc (arange_table,
7126 (arange_table_allocated
7127 * sizeof (dw_die_ref)));
7128 memset (arange_table + arange_table_in_use, 0,
7129 ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
7132 arange_table[arange_table_in_use++] = die;
7135 /* Output the information that goes into the .debug_aranges table.
7136 Namely, define the beginning and ending address range of the
7137 text section generated for this compilation unit. */
7139 static void
7140 output_aranges (void)
7142 unsigned i;
7143 unsigned long aranges_length = size_of_aranges ();
7145 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7146 dw2_asm_output_data (4, 0xffffffff,
7147 "Initial length escape value indicating 64-bit DWARF extension");
7148 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
7149 "Length of Address Ranges Info");
7150 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7151 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7152 "Offset of Compilation Unit Info");
7153 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
7154 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
7156 /* We need to align to twice the pointer size here. */
7157 if (DWARF_ARANGES_PAD_SIZE)
7159 /* Pad using a 2 byte words so that padding is correct for any
7160 pointer size. */
7161 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
7162 2 * DWARF2_ADDR_SIZE);
7163 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
7164 dw2_asm_output_data (2, 0, NULL);
7167 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
7168 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
7169 text_section_label, "Length");
7171 for (i = 0; i < arange_table_in_use; i++)
7173 dw_die_ref die = arange_table[i];
7175 /* We shouldn't see aranges for DIEs outside of the main CU. */
7176 gcc_assert (die->die_mark);
7178 if (die->die_tag == DW_TAG_subprogram)
7180 dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
7181 "Address");
7182 dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
7183 get_AT_low_pc (die), "Length");
7185 else
7187 /* A static variable; extract the symbol from DW_AT_location.
7188 Note that this code isn't currently hit, as we only emit
7189 aranges for functions (jason 9/23/99). */
7190 dw_attr_ref a = get_AT (die, DW_AT_location);
7191 dw_loc_descr_ref loc;
7193 gcc_assert (a && AT_class (a) == dw_val_class_loc);
7195 loc = AT_loc (a);
7196 gcc_assert (loc->dw_loc_opc == DW_OP_addr);
7198 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
7199 loc->dw_loc_oprnd1.v.val_addr, "Address");
7200 dw2_asm_output_data (DWARF2_ADDR_SIZE,
7201 get_AT_unsigned (die, DW_AT_byte_size),
7202 "Length");
7206 /* Output the terminator words. */
7207 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7208 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7211 /* Add a new entry to .debug_ranges. Return the offset at which it
7212 was placed. */
7214 static unsigned int
7215 add_ranges (tree block)
7217 unsigned int in_use = ranges_table_in_use;
7219 if (in_use == ranges_table_allocated)
7221 ranges_table_allocated += RANGES_TABLE_INCREMENT;
7222 ranges_table
7223 = ggc_realloc (ranges_table, (ranges_table_allocated
7224 * sizeof (struct dw_ranges_struct)));
7225 memset (ranges_table + ranges_table_in_use, 0,
7226 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
7229 ranges_table[in_use].block_num = (block ? BLOCK_NUMBER (block) : 0);
7230 ranges_table_in_use = in_use + 1;
7232 return in_use * 2 * DWARF2_ADDR_SIZE;
7235 static void
7236 output_ranges (void)
7238 unsigned i;
7239 static const char *const start_fmt = "Offset 0x%x";
7240 const char *fmt = start_fmt;
7242 for (i = 0; i < ranges_table_in_use; i++)
7244 int block_num = ranges_table[i].block_num;
7246 if (block_num)
7248 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
7249 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
7251 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
7252 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
7254 /* If all code is in the text section, then the compilation
7255 unit base address defaults to DW_AT_low_pc, which is the
7256 base of the text section. */
7257 if (separate_line_info_table_in_use == 0)
7259 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
7260 text_section_label,
7261 fmt, i * 2 * DWARF2_ADDR_SIZE);
7262 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
7263 text_section_label, NULL);
7266 /* Otherwise, we add a DW_AT_entry_pc attribute to force the
7267 compilation unit base address to zero, which allows us to
7268 use absolute addresses, and not worry about whether the
7269 target supports cross-section arithmetic. */
7270 else
7272 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
7273 fmt, i * 2 * DWARF2_ADDR_SIZE);
7274 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
7277 fmt = NULL;
7279 else
7281 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7282 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7283 fmt = start_fmt;
7288 /* Data structure containing information about input files. */
7289 struct file_info
7291 char *path; /* Complete file name. */
7292 char *fname; /* File name part. */
7293 int length; /* Length of entire string. */
7294 int file_idx; /* Index in input file table. */
7295 int dir_idx; /* Index in directory table. */
7298 /* Data structure containing information about directories with source
7299 files. */
7300 struct dir_info
7302 char *path; /* Path including directory name. */
7303 int length; /* Path length. */
7304 int prefix; /* Index of directory entry which is a prefix. */
7305 int count; /* Number of files in this directory. */
7306 int dir_idx; /* Index of directory used as base. */
7307 int used; /* Used in the end? */
7310 /* Callback function for file_info comparison. We sort by looking at
7311 the directories in the path. */
7313 static int
7314 file_info_cmp (const void *p1, const void *p2)
7316 const struct file_info *s1 = p1;
7317 const struct file_info *s2 = p2;
7318 unsigned char *cp1;
7319 unsigned char *cp2;
7321 /* Take care of file names without directories. We need to make sure that
7322 we return consistent values to qsort since some will get confused if
7323 we return the same value when identical operands are passed in opposite
7324 orders. So if neither has a directory, return 0 and otherwise return
7325 1 or -1 depending on which one has the directory. */
7326 if ((s1->path == s1->fname || s2->path == s2->fname))
7327 return (s2->path == s2->fname) - (s1->path == s1->fname);
7329 cp1 = (unsigned char *) s1->path;
7330 cp2 = (unsigned char *) s2->path;
7332 while (1)
7334 ++cp1;
7335 ++cp2;
7336 /* Reached the end of the first path? If so, handle like above. */
7337 if ((cp1 == (unsigned char *) s1->fname)
7338 || (cp2 == (unsigned char *) s2->fname))
7339 return ((cp2 == (unsigned char *) s2->fname)
7340 - (cp1 == (unsigned char *) s1->fname));
7342 /* Character of current path component the same? */
7343 else if (*cp1 != *cp2)
7344 return *cp1 - *cp2;
7348 /* Output the directory table and the file name table. We try to minimize
7349 the total amount of memory needed. A heuristic is used to avoid large
7350 slowdowns with many input files. */
7352 static void
7353 output_file_names (void)
7355 struct file_info *files;
7356 struct dir_info *dirs;
7357 int *saved;
7358 int *savehere;
7359 int *backmap;
7360 size_t ndirs;
7361 int idx_offset;
7362 size_t i;
7363 int idx;
7365 /* Handle the case where file_table is empty. */
7366 if (VARRAY_ACTIVE_SIZE (file_table) <= 1)
7368 dw2_asm_output_data (1, 0, "End directory table");
7369 dw2_asm_output_data (1, 0, "End file name table");
7370 return;
7373 /* Allocate the various arrays we need. */
7374 files = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (struct file_info));
7375 dirs = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (struct dir_info));
7377 /* Sort the file names. */
7378 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7380 char *f;
7382 /* Skip all leading "./". */
7383 f = VARRAY_CHAR_PTR (file_table, i);
7384 while (f[0] == '.' && f[1] == '/')
7385 f += 2;
7387 /* Create a new array entry. */
7388 files[i].path = f;
7389 files[i].length = strlen (f);
7390 files[i].file_idx = i;
7392 /* Search for the file name part. */
7393 f = strrchr (f, '/');
7394 files[i].fname = f == NULL ? files[i].path : f + 1;
7397 qsort (files + 1, VARRAY_ACTIVE_SIZE (file_table) - 1,
7398 sizeof (files[0]), file_info_cmp);
7400 /* Find all the different directories used. */
7401 dirs[0].path = files[1].path;
7402 dirs[0].length = files[1].fname - files[1].path;
7403 dirs[0].prefix = -1;
7404 dirs[0].count = 1;
7405 dirs[0].dir_idx = 0;
7406 dirs[0].used = 0;
7407 files[1].dir_idx = 0;
7408 ndirs = 1;
7410 for (i = 2; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7411 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
7412 && memcmp (dirs[ndirs - 1].path, files[i].path,
7413 dirs[ndirs - 1].length) == 0)
7415 /* Same directory as last entry. */
7416 files[i].dir_idx = ndirs - 1;
7417 ++dirs[ndirs - 1].count;
7419 else
7421 size_t j;
7423 /* This is a new directory. */
7424 dirs[ndirs].path = files[i].path;
7425 dirs[ndirs].length = files[i].fname - files[i].path;
7426 dirs[ndirs].count = 1;
7427 dirs[ndirs].dir_idx = ndirs;
7428 dirs[ndirs].used = 0;
7429 files[i].dir_idx = ndirs;
7431 /* Search for a prefix. */
7432 dirs[ndirs].prefix = -1;
7433 for (j = 0; j < ndirs; j++)
7434 if (dirs[j].length < dirs[ndirs].length
7435 && dirs[j].length > 1
7436 && (dirs[ndirs].prefix == -1
7437 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
7438 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
7439 dirs[ndirs].prefix = j;
7441 ++ndirs;
7444 /* Now to the actual work. We have to find a subset of the directories which
7445 allow expressing the file name using references to the directory table
7446 with the least amount of characters. We do not do an exhaustive search
7447 where we would have to check out every combination of every single
7448 possible prefix. Instead we use a heuristic which provides nearly optimal
7449 results in most cases and never is much off. */
7450 saved = alloca (ndirs * sizeof (int));
7451 savehere = alloca (ndirs * sizeof (int));
7453 memset (saved, '\0', ndirs * sizeof (saved[0]));
7454 for (i = 0; i < ndirs; i++)
7456 size_t j;
7457 int total;
7459 /* We can always save some space for the current directory. But this
7460 does not mean it will be enough to justify adding the directory. */
7461 savehere[i] = dirs[i].length;
7462 total = (savehere[i] - saved[i]) * dirs[i].count;
7464 for (j = i + 1; j < ndirs; j++)
7466 savehere[j] = 0;
7467 if (saved[j] < dirs[i].length)
7469 /* Determine whether the dirs[i] path is a prefix of the
7470 dirs[j] path. */
7471 int k;
7473 k = dirs[j].prefix;
7474 while (k != -1 && k != (int) i)
7475 k = dirs[k].prefix;
7477 if (k == (int) i)
7479 /* Yes it is. We can possibly safe some memory but
7480 writing the filenames in dirs[j] relative to
7481 dirs[i]. */
7482 savehere[j] = dirs[i].length;
7483 total += (savehere[j] - saved[j]) * dirs[j].count;
7488 /* Check whether we can safe enough to justify adding the dirs[i]
7489 directory. */
7490 if (total > dirs[i].length + 1)
7492 /* It's worthwhile adding. */
7493 for (j = i; j < ndirs; j++)
7494 if (savehere[j] > 0)
7496 /* Remember how much we saved for this directory so far. */
7497 saved[j] = savehere[j];
7499 /* Remember the prefix directory. */
7500 dirs[j].dir_idx = i;
7505 /* We have to emit them in the order they appear in the file_table array
7506 since the index is used in the debug info generation. To do this
7507 efficiently we generate a back-mapping of the indices first. */
7508 backmap = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (int));
7509 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7511 backmap[files[i].file_idx] = i;
7513 /* Mark this directory as used. */
7514 dirs[dirs[files[i].dir_idx].dir_idx].used = 1;
7517 /* That was it. We are ready to emit the information. First emit the
7518 directory name table. We have to make sure the first actually emitted
7519 directory name has index one; zero is reserved for the current working
7520 directory. Make sure we do not confuse these indices with the one for the
7521 constructed table (even though most of the time they are identical). */
7522 idx = 1;
7523 idx_offset = dirs[0].length > 0 ? 1 : 0;
7524 for (i = 1 - idx_offset; i < ndirs; i++)
7525 if (dirs[i].used != 0)
7527 dirs[i].used = idx++;
7528 dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
7529 "Directory Entry: 0x%x", dirs[i].used);
7532 dw2_asm_output_data (1, 0, "End directory table");
7534 /* Correct the index for the current working directory entry if it
7535 exists. */
7536 if (idx_offset == 0)
7537 dirs[0].used = 0;
7539 /* Now write all the file names. */
7540 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7542 int file_idx = backmap[i];
7543 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
7545 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
7546 "File Entry: 0x%lx", (unsigned long) i);
7548 /* Include directory index. */
7549 dw2_asm_output_data_uleb128 (dirs[dir_idx].used, NULL);
7551 /* Modification time. */
7552 dw2_asm_output_data_uleb128 (0, NULL);
7554 /* File length in bytes. */
7555 dw2_asm_output_data_uleb128 (0, NULL);
7558 dw2_asm_output_data (1, 0, "End file name table");
7562 /* Output the source line number correspondence information. This
7563 information goes into the .debug_line section. */
7565 static void
7566 output_line_info (void)
7568 char l1[20], l2[20], p1[20], p2[20];
7569 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
7570 char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
7571 unsigned opc;
7572 unsigned n_op_args;
7573 unsigned long lt_index;
7574 unsigned long current_line;
7575 long line_offset;
7576 long line_delta;
7577 unsigned long current_file;
7578 unsigned long function;
7580 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
7581 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
7582 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
7583 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
7585 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7586 dw2_asm_output_data (4, 0xffffffff,
7587 "Initial length escape value indicating 64-bit DWARF extension");
7588 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
7589 "Length of Source Line Info");
7590 ASM_OUTPUT_LABEL (asm_out_file, l1);
7592 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7593 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
7594 ASM_OUTPUT_LABEL (asm_out_file, p1);
7596 /* Define the architecture-dependent minimum instruction length (in
7597 bytes). In this implementation of DWARF, this field is used for
7598 information purposes only. Since GCC generates assembly language,
7599 we have no a priori knowledge of how many instruction bytes are
7600 generated for each source line, and therefore can use only the
7601 DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
7602 commands. Accordingly, we fix this as `1', which is "correct
7603 enough" for all architectures, and don't let the target override. */
7604 dw2_asm_output_data (1, 1,
7605 "Minimum Instruction Length");
7607 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
7608 "Default is_stmt_start flag");
7609 dw2_asm_output_data (1, DWARF_LINE_BASE,
7610 "Line Base Value (Special Opcodes)");
7611 dw2_asm_output_data (1, DWARF_LINE_RANGE,
7612 "Line Range Value (Special Opcodes)");
7613 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
7614 "Special Opcode Base");
7616 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
7618 switch (opc)
7620 case DW_LNS_advance_pc:
7621 case DW_LNS_advance_line:
7622 case DW_LNS_set_file:
7623 case DW_LNS_set_column:
7624 case DW_LNS_fixed_advance_pc:
7625 n_op_args = 1;
7626 break;
7627 default:
7628 n_op_args = 0;
7629 break;
7632 dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
7633 opc, n_op_args);
7636 /* Write out the information about the files we use. */
7637 output_file_names ();
7638 ASM_OUTPUT_LABEL (asm_out_file, p2);
7640 /* We used to set the address register to the first location in the text
7641 section here, but that didn't accomplish anything since we already
7642 have a line note for the opening brace of the first function. */
7644 /* Generate the line number to PC correspondence table, encoded as
7645 a series of state machine operations. */
7646 current_file = 1;
7647 current_line = 1;
7648 strcpy (prev_line_label, text_section_label);
7649 for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
7651 dw_line_info_ref line_info = &line_info_table[lt_index];
7653 #if 0
7654 /* Disable this optimization for now; GDB wants to see two line notes
7655 at the beginning of a function so it can find the end of the
7656 prologue. */
7658 /* Don't emit anything for redundant notes. Just updating the
7659 address doesn't accomplish anything, because we already assume
7660 that anything after the last address is this line. */
7661 if (line_info->dw_line_num == current_line
7662 && line_info->dw_file_num == current_file)
7663 continue;
7664 #endif
7666 /* Emit debug info for the address of the current line.
7668 Unfortunately, we have little choice here currently, and must always
7669 use the most general form. GCC does not know the address delta
7670 itself, so we can't use DW_LNS_advance_pc. Many ports do have length
7671 attributes which will give an upper bound on the address range. We
7672 could perhaps use length attributes to determine when it is safe to
7673 use DW_LNS_fixed_advance_pc. */
7675 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
7676 if (0)
7678 /* This can handle deltas up to 0xffff. This takes 3 bytes. */
7679 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7680 "DW_LNS_fixed_advance_pc");
7681 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
7683 else
7685 /* This can handle any delta. This takes
7686 4+DWARF2_ADDR_SIZE bytes. */
7687 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7688 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7689 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7690 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7693 strcpy (prev_line_label, line_label);
7695 /* Emit debug info for the source file of the current line, if
7696 different from the previous line. */
7697 if (line_info->dw_file_num != current_file)
7699 current_file = line_info->dw_file_num;
7700 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
7701 dw2_asm_output_data_uleb128 (current_file, "(\"%s\")",
7702 VARRAY_CHAR_PTR (file_table,
7703 current_file));
7706 /* Emit debug info for the current line number, choosing the encoding
7707 that uses the least amount of space. */
7708 if (line_info->dw_line_num != current_line)
7710 line_offset = line_info->dw_line_num - current_line;
7711 line_delta = line_offset - DWARF_LINE_BASE;
7712 current_line = line_info->dw_line_num;
7713 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
7714 /* This can handle deltas from -10 to 234, using the current
7715 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE. This
7716 takes 1 byte. */
7717 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
7718 "line %lu", current_line);
7719 else
7721 /* This can handle any delta. This takes at least 4 bytes,
7722 depending on the value being encoded. */
7723 dw2_asm_output_data (1, DW_LNS_advance_line,
7724 "advance to line %lu", current_line);
7725 dw2_asm_output_data_sleb128 (line_offset, NULL);
7726 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7729 else
7730 /* We still need to start a new row, so output a copy insn. */
7731 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7734 /* Emit debug info for the address of the end of the function. */
7735 if (0)
7737 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7738 "DW_LNS_fixed_advance_pc");
7739 dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
7741 else
7743 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7744 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7745 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7746 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
7749 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
7750 dw2_asm_output_data_uleb128 (1, NULL);
7751 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
7753 function = 0;
7754 current_file = 1;
7755 current_line = 1;
7756 for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
7758 dw_separate_line_info_ref line_info
7759 = &separate_line_info_table[lt_index];
7761 #if 0
7762 /* Don't emit anything for redundant notes. */
7763 if (line_info->dw_line_num == current_line
7764 && line_info->dw_file_num == current_file
7765 && line_info->function == function)
7766 goto cont;
7767 #endif
7769 /* Emit debug info for the address of the current line. If this is
7770 a new function, or the first line of a function, then we need
7771 to handle it differently. */
7772 ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
7773 lt_index);
7774 if (function != line_info->function)
7776 function = line_info->function;
7778 /* Set the address register to the first line in the function. */
7779 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7780 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7781 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7782 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7784 else
7786 /* ??? See the DW_LNS_advance_pc comment above. */
7787 if (0)
7789 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7790 "DW_LNS_fixed_advance_pc");
7791 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
7793 else
7795 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7796 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7797 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7798 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7802 strcpy (prev_line_label, line_label);
7804 /* Emit debug info for the source file of the current line, if
7805 different from the previous line. */
7806 if (line_info->dw_file_num != current_file)
7808 current_file = line_info->dw_file_num;
7809 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
7810 dw2_asm_output_data_uleb128 (current_file, "(\"%s\")",
7811 VARRAY_CHAR_PTR (file_table,
7812 current_file));
7815 /* Emit debug info for the current line number, choosing the encoding
7816 that uses the least amount of space. */
7817 if (line_info->dw_line_num != current_line)
7819 line_offset = line_info->dw_line_num - current_line;
7820 line_delta = line_offset - DWARF_LINE_BASE;
7821 current_line = line_info->dw_line_num;
7822 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
7823 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
7824 "line %lu", current_line);
7825 else
7827 dw2_asm_output_data (1, DW_LNS_advance_line,
7828 "advance to line %lu", current_line);
7829 dw2_asm_output_data_sleb128 (line_offset, NULL);
7830 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7833 else
7834 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7836 #if 0
7837 cont:
7838 #endif
7840 lt_index++;
7842 /* If we're done with a function, end its sequence. */
7843 if (lt_index == separate_line_info_table_in_use
7844 || separate_line_info_table[lt_index].function != function)
7846 current_file = 1;
7847 current_line = 1;
7849 /* Emit debug info for the address of the end of the function. */
7850 ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
7851 if (0)
7853 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7854 "DW_LNS_fixed_advance_pc");
7855 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
7857 else
7859 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7860 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7861 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7862 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7865 /* Output the marker for the end of this sequence. */
7866 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
7867 dw2_asm_output_data_uleb128 (1, NULL);
7868 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
7872 /* Output the marker for the end of the line number info. */
7873 ASM_OUTPUT_LABEL (asm_out_file, l2);
7876 /* Given a pointer to a tree node for some base type, return a pointer to
7877 a DIE that describes the given type.
7879 This routine must only be called for GCC type nodes that correspond to
7880 Dwarf base (fundamental) types. */
7882 static dw_die_ref
7883 base_type_die (tree type)
7885 dw_die_ref base_type_result;
7886 const char *type_name;
7887 enum dwarf_type encoding;
7888 tree name = TYPE_NAME (type);
7890 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
7891 return 0;
7893 if (name)
7895 if (TREE_CODE (name) == TYPE_DECL)
7896 name = DECL_NAME (name);
7898 type_name = IDENTIFIER_POINTER (name);
7900 else
7901 type_name = "__unknown__";
7903 switch (TREE_CODE (type))
7905 case INTEGER_TYPE:
7906 /* Carefully distinguish the C character types, without messing
7907 up if the language is not C. Note that we check only for the names
7908 that contain spaces; other names might occur by coincidence in other
7909 languages. */
7910 if (! (TYPE_PRECISION (type) == CHAR_TYPE_SIZE
7911 && (type == char_type_node
7912 || ! strcmp (type_name, "signed char")
7913 || ! strcmp (type_name, "unsigned char"))))
7915 if (TYPE_UNSIGNED (type))
7916 encoding = DW_ATE_unsigned;
7917 else
7918 encoding = DW_ATE_signed;
7919 break;
7921 /* else fall through. */
7923 case CHAR_TYPE:
7924 /* GNU Pascal/Ada CHAR type. Not used in C. */
7925 if (TYPE_UNSIGNED (type))
7926 encoding = DW_ATE_unsigned_char;
7927 else
7928 encoding = DW_ATE_signed_char;
7929 break;
7931 case REAL_TYPE:
7932 encoding = DW_ATE_float;
7933 break;
7935 /* Dwarf2 doesn't know anything about complex ints, so use
7936 a user defined type for it. */
7937 case COMPLEX_TYPE:
7938 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
7939 encoding = DW_ATE_complex_float;
7940 else
7941 encoding = DW_ATE_lo_user;
7942 break;
7944 case BOOLEAN_TYPE:
7945 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
7946 encoding = DW_ATE_boolean;
7947 break;
7949 default:
7950 /* No other TREE_CODEs are Dwarf fundamental types. */
7951 gcc_unreachable ();
7954 base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
7955 if (demangle_name_func)
7956 type_name = (*demangle_name_func) (type_name);
7958 add_AT_string (base_type_result, DW_AT_name, type_name);
7959 add_AT_unsigned (base_type_result, DW_AT_byte_size,
7960 int_size_in_bytes (type));
7961 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
7963 return base_type_result;
7966 /* Given a pointer to an arbitrary ..._TYPE tree node, return a pointer to
7967 the Dwarf "root" type for the given input type. The Dwarf "root" type of
7968 a given type is generally the same as the given type, except that if the
7969 given type is a pointer or reference type, then the root type of the given
7970 type is the root type of the "basis" type for the pointer or reference
7971 type. (This definition of the "root" type is recursive.) Also, the root
7972 type of a `const' qualified type or a `volatile' qualified type is the
7973 root type of the given type without the qualifiers. */
7975 static tree
7976 root_type (tree type)
7978 if (TREE_CODE (type) == ERROR_MARK)
7979 return error_mark_node;
7981 switch (TREE_CODE (type))
7983 case ERROR_MARK:
7984 return error_mark_node;
7986 case POINTER_TYPE:
7987 case REFERENCE_TYPE:
7988 return type_main_variant (root_type (TREE_TYPE (type)));
7990 default:
7991 return type_main_variant (type);
7995 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
7996 given input type is a Dwarf "fundamental" type. Otherwise return null. */
7998 static inline int
7999 is_base_type (tree type)
8001 switch (TREE_CODE (type))
8003 case ERROR_MARK:
8004 case VOID_TYPE:
8005 case INTEGER_TYPE:
8006 case REAL_TYPE:
8007 case COMPLEX_TYPE:
8008 case BOOLEAN_TYPE:
8009 case CHAR_TYPE:
8010 return 1;
8012 case SET_TYPE:
8013 case ARRAY_TYPE:
8014 case RECORD_TYPE:
8015 case UNION_TYPE:
8016 case QUAL_UNION_TYPE:
8017 case ENUMERAL_TYPE:
8018 case FUNCTION_TYPE:
8019 case METHOD_TYPE:
8020 case POINTER_TYPE:
8021 case REFERENCE_TYPE:
8022 case FILE_TYPE:
8023 case OFFSET_TYPE:
8024 case LANG_TYPE:
8025 case VECTOR_TYPE:
8026 return 0;
8028 default:
8029 gcc_unreachable ();
8032 return 0;
8035 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
8036 node, return the size in bits for the type if it is a constant, or else
8037 return the alignment for the type if the type's size is not constant, or
8038 else return BITS_PER_WORD if the type actually turns out to be an
8039 ERROR_MARK node. */
8041 static inline unsigned HOST_WIDE_INT
8042 simple_type_size_in_bits (tree type)
8044 if (TREE_CODE (type) == ERROR_MARK)
8045 return BITS_PER_WORD;
8046 else if (TYPE_SIZE (type) == NULL_TREE)
8047 return 0;
8048 else if (host_integerp (TYPE_SIZE (type), 1))
8049 return tree_low_cst (TYPE_SIZE (type), 1);
8050 else
8051 return TYPE_ALIGN (type);
8054 /* Return true if the debug information for the given type should be
8055 emitted as a subrange type. */
8057 static inline bool
8058 is_subrange_type (tree type)
8060 tree subtype = TREE_TYPE (type);
8062 /* Subrange types are identified by the fact that they are integer
8063 types, and that they have a subtype which is either an integer type
8064 or an enumeral type. */
8066 if (TREE_CODE (type) != INTEGER_TYPE
8067 || subtype == NULL_TREE)
8068 return false;
8070 if (TREE_CODE (subtype) != INTEGER_TYPE
8071 && TREE_CODE (subtype) != ENUMERAL_TYPE)
8072 return false;
8074 if (TREE_CODE (type) == TREE_CODE (subtype)
8075 && int_size_in_bytes (type) == int_size_in_bytes (subtype)
8076 && TYPE_MIN_VALUE (type) != NULL
8077 && TYPE_MIN_VALUE (subtype) != NULL
8078 && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
8079 && TYPE_MAX_VALUE (type) != NULL
8080 && TYPE_MAX_VALUE (subtype) != NULL
8081 && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
8083 /* The type and its subtype have the same representation. If in
8084 addition the two types also have the same name, then the given
8085 type is not a subrange type, but rather a plain base type. */
8086 /* FIXME: brobecker/2004-03-22:
8087 Sizetype INTEGER_CSTs nodes are canonicalized. It should
8088 therefore be sufficient to check the TYPE_SIZE node pointers
8089 rather than checking the actual size. Unfortunately, we have
8090 found some cases, such as in the Ada "integer" type, where
8091 this is not the case. Until this problem is solved, we need to
8092 keep checking the actual size. */
8093 tree type_name = TYPE_NAME (type);
8094 tree subtype_name = TYPE_NAME (subtype);
8096 if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
8097 type_name = DECL_NAME (type_name);
8099 if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
8100 subtype_name = DECL_NAME (subtype_name);
8102 if (type_name == subtype_name)
8103 return false;
8106 return true;
8109 /* Given a pointer to a tree node for a subrange type, return a pointer
8110 to a DIE that describes the given type. */
8112 static dw_die_ref
8113 subrange_type_die (tree type, dw_die_ref context_die)
8115 dw_die_ref subtype_die;
8116 dw_die_ref subrange_die;
8117 tree name = TYPE_NAME (type);
8118 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
8119 tree subtype = TREE_TYPE (type);
8121 if (context_die == NULL)
8122 context_die = comp_unit_die;
8124 if (TREE_CODE (subtype) == ENUMERAL_TYPE)
8125 subtype_die = gen_enumeration_type_die (subtype, context_die);
8126 else
8127 subtype_die = base_type_die (subtype);
8129 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
8131 if (name != NULL)
8133 if (TREE_CODE (name) == TYPE_DECL)
8134 name = DECL_NAME (name);
8135 add_name_attribute (subrange_die, IDENTIFIER_POINTER (name));
8138 if (int_size_in_bytes (subtype) != size_in_bytes)
8140 /* The size of the subrange type and its base type do not match,
8141 so we need to generate a size attribute for the subrange type. */
8142 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
8145 if (TYPE_MIN_VALUE (type) != NULL)
8146 add_bound_info (subrange_die, DW_AT_lower_bound,
8147 TYPE_MIN_VALUE (type));
8148 if (TYPE_MAX_VALUE (type) != NULL)
8149 add_bound_info (subrange_die, DW_AT_upper_bound,
8150 TYPE_MAX_VALUE (type));
8151 add_AT_die_ref (subrange_die, DW_AT_type, subtype_die);
8153 return subrange_die;
8156 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
8157 entry that chains various modifiers in front of the given type. */
8159 static dw_die_ref
8160 modified_type_die (tree type, int is_const_type, int is_volatile_type,
8161 dw_die_ref context_die)
8163 enum tree_code code = TREE_CODE (type);
8164 dw_die_ref mod_type_die = NULL;
8165 dw_die_ref sub_die = NULL;
8166 tree item_type = NULL;
8168 if (code != ERROR_MARK)
8170 tree qualified_type;
8172 /* See if we already have the appropriately qualified variant of
8173 this type. */
8174 qualified_type
8175 = get_qualified_type (type,
8176 ((is_const_type ? TYPE_QUAL_CONST : 0)
8177 | (is_volatile_type
8178 ? TYPE_QUAL_VOLATILE : 0)));
8180 /* If we do, then we can just use its DIE, if it exists. */
8181 if (qualified_type)
8183 mod_type_die = lookup_type_die (qualified_type);
8184 if (mod_type_die)
8185 return mod_type_die;
8188 /* Handle C typedef types. */
8189 if (qualified_type && TYPE_NAME (qualified_type)
8190 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL
8191 && DECL_ORIGINAL_TYPE (TYPE_NAME (qualified_type)))
8193 tree type_name = TYPE_NAME (qualified_type);
8194 tree dtype = TREE_TYPE (type_name);
8196 if (qualified_type == dtype)
8198 /* For a named type, use the typedef. */
8199 gen_type_die (qualified_type, context_die);
8200 mod_type_die = lookup_type_die (qualified_type);
8202 else if (is_const_type < TYPE_READONLY (dtype)
8203 || is_volatile_type < TYPE_VOLATILE (dtype))
8204 /* cv-unqualified version of named type. Just use the unnamed
8205 type to which it refers. */
8206 mod_type_die
8207 = modified_type_die (DECL_ORIGINAL_TYPE (type_name),
8208 is_const_type, is_volatile_type,
8209 context_die);
8211 /* Else cv-qualified version of named type; fall through. */
8214 if (mod_type_die)
8215 /* OK. */
8217 else if (is_const_type)
8219 mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
8220 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
8222 else if (is_volatile_type)
8224 mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
8225 sub_die = modified_type_die (type, 0, 0, context_die);
8227 else if (code == POINTER_TYPE)
8229 mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
8230 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8231 simple_type_size_in_bits (type) / BITS_PER_UNIT);
8232 #if 0
8233 add_AT_unsigned (mod_type_die, DW_AT_address_class, 0);
8234 #endif
8235 item_type = TREE_TYPE (type);
8237 else if (code == REFERENCE_TYPE)
8239 mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
8240 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8241 simple_type_size_in_bits (type) / BITS_PER_UNIT);
8242 #if 0
8243 add_AT_unsigned (mod_type_die, DW_AT_address_class, 0);
8244 #endif
8245 item_type = TREE_TYPE (type);
8247 else if (is_subrange_type (type))
8248 mod_type_die = subrange_type_die (type, context_die);
8249 else if (is_base_type (type))
8250 mod_type_die = base_type_die (type);
8251 else
8253 gen_type_die (type, context_die);
8255 /* We have to get the type_main_variant here (and pass that to the
8256 `lookup_type_die' routine) because the ..._TYPE node we have
8257 might simply be a *copy* of some original type node (where the
8258 copy was created to help us keep track of typedef names) and
8259 that copy might have a different TYPE_UID from the original
8260 ..._TYPE node. */
8261 if (TREE_CODE (type) != VECTOR_TYPE)
8262 mod_type_die = lookup_type_die (type_main_variant (type));
8263 else
8264 /* Vectors have the debugging information in the type,
8265 not the main variant. */
8266 mod_type_die = lookup_type_die (type);
8267 gcc_assert (mod_type_die);
8270 /* We want to equate the qualified type to the die below. */
8271 type = qualified_type;
8274 if (type)
8275 equate_type_number_to_die (type, mod_type_die);
8276 if (item_type)
8277 /* We must do this after the equate_type_number_to_die call, in case
8278 this is a recursive type. This ensures that the modified_type_die
8279 recursion will terminate even if the type is recursive. Recursive
8280 types are possible in Ada. */
8281 sub_die = modified_type_die (item_type,
8282 TYPE_READONLY (item_type),
8283 TYPE_VOLATILE (item_type),
8284 context_die);
8286 if (sub_die != NULL)
8287 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
8289 return mod_type_die;
8292 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
8293 an enumerated type. */
8295 static inline int
8296 type_is_enum (tree type)
8298 return TREE_CODE (type) == ENUMERAL_TYPE;
8301 /* Return the DBX register number described by a given RTL node. */
8303 static unsigned int
8304 dbx_reg_number (rtx rtl)
8306 unsigned regno = REGNO (rtl);
8308 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
8310 return DBX_REGISTER_NUMBER (regno);
8313 /* Return a location descriptor that designates a machine register or
8314 zero if there is none. */
8316 static dw_loc_descr_ref
8317 reg_loc_descriptor (rtx rtl)
8319 unsigned reg;
8320 rtx regs;
8322 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
8323 return 0;
8325 reg = dbx_reg_number (rtl);
8326 regs = targetm.dwarf_register_span (rtl);
8328 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1
8329 || regs)
8330 return multiple_reg_loc_descriptor (rtl, regs);
8331 else
8332 return one_reg_loc_descriptor (reg);
8335 /* Return a location descriptor that designates a machine register for
8336 a given hard register number. */
8338 static dw_loc_descr_ref
8339 one_reg_loc_descriptor (unsigned int regno)
8341 if (regno <= 31)
8342 return new_loc_descr (DW_OP_reg0 + regno, 0, 0);
8343 else
8344 return new_loc_descr (DW_OP_regx, regno, 0);
8347 /* Given an RTL of a register, return a location descriptor that
8348 designates a value that spans more than one register. */
8350 static dw_loc_descr_ref
8351 multiple_reg_loc_descriptor (rtx rtl, rtx regs)
8353 int nregs, size, i;
8354 unsigned reg;
8355 dw_loc_descr_ref loc_result = NULL;
8357 reg = dbx_reg_number (rtl);
8358 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
8360 /* Simple, contiguous registers. */
8361 if (regs == NULL_RTX)
8363 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
8365 loc_result = NULL;
8366 while (nregs--)
8368 dw_loc_descr_ref t;
8370 t = one_reg_loc_descriptor (reg);
8371 add_loc_descr (&loc_result, t);
8372 add_loc_descr (&loc_result, new_loc_descr (DW_OP_piece, size, 0));
8373 ++reg;
8375 return loc_result;
8378 /* Now onto stupid register sets in non contiguous locations. */
8380 gcc_assert (GET_CODE (regs) == PARALLEL);
8382 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8383 loc_result = NULL;
8385 for (i = 0; i < XVECLEN (regs, 0); ++i)
8387 dw_loc_descr_ref t;
8389 t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)));
8390 add_loc_descr (&loc_result, t);
8391 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8392 add_loc_descr (&loc_result, new_loc_descr (DW_OP_piece, size, 0));
8394 return loc_result;
8397 /* Return a location descriptor that designates a constant. */
8399 static dw_loc_descr_ref
8400 int_loc_descriptor (HOST_WIDE_INT i)
8402 enum dwarf_location_atom op;
8404 /* Pick the smallest representation of a constant, rather than just
8405 defaulting to the LEB encoding. */
8406 if (i >= 0)
8408 if (i <= 31)
8409 op = DW_OP_lit0 + i;
8410 else if (i <= 0xff)
8411 op = DW_OP_const1u;
8412 else if (i <= 0xffff)
8413 op = DW_OP_const2u;
8414 else if (HOST_BITS_PER_WIDE_INT == 32
8415 || i <= 0xffffffff)
8416 op = DW_OP_const4u;
8417 else
8418 op = DW_OP_constu;
8420 else
8422 if (i >= -0x80)
8423 op = DW_OP_const1s;
8424 else if (i >= -0x8000)
8425 op = DW_OP_const2s;
8426 else if (HOST_BITS_PER_WIDE_INT == 32
8427 || i >= -0x80000000)
8428 op = DW_OP_const4s;
8429 else
8430 op = DW_OP_consts;
8433 return new_loc_descr (op, i, 0);
8436 /* Return a location descriptor that designates a base+offset location. */
8438 static dw_loc_descr_ref
8439 based_loc_descr (unsigned int reg, HOST_WIDE_INT offset, bool can_use_fbreg)
8441 dw_loc_descr_ref loc_result;
8442 /* For the "frame base", we use the frame pointer or stack pointer
8443 registers, since the RTL for local variables is relative to one of
8444 them. */
8445 unsigned fp_reg = DBX_REGISTER_NUMBER (frame_pointer_needed
8446 ? HARD_FRAME_POINTER_REGNUM
8447 : STACK_POINTER_REGNUM);
8449 if (reg == fp_reg && can_use_fbreg)
8450 loc_result = new_loc_descr (DW_OP_fbreg, offset, 0);
8451 else if (reg <= 31)
8452 loc_result = new_loc_descr (DW_OP_breg0 + reg, offset, 0);
8453 else
8454 loc_result = new_loc_descr (DW_OP_bregx, reg, offset);
8456 return loc_result;
8459 /* Return true if this RTL expression describes a base+offset calculation. */
8461 static inline int
8462 is_based_loc (rtx rtl)
8464 return (GET_CODE (rtl) == PLUS
8465 && ((REG_P (XEXP (rtl, 0))
8466 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
8467 && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
8470 /* The following routine converts the RTL for a variable or parameter
8471 (resident in memory) into an equivalent Dwarf representation of a
8472 mechanism for getting the address of that same variable onto the top of a
8473 hypothetical "address evaluation" stack.
8475 When creating memory location descriptors, we are effectively transforming
8476 the RTL for a memory-resident object into its Dwarf postfix expression
8477 equivalent. This routine recursively descends an RTL tree, turning
8478 it into Dwarf postfix code as it goes.
8480 MODE is the mode of the memory reference, needed to handle some
8481 autoincrement addressing modes.
8483 CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the location
8484 list for RTL. We can't use it when we are emitting location list for
8485 virtual variable frame_base_decl (i.e. a location list for DW_AT_frame_base)
8486 which describes how frame base changes when !frame_pointer_needed.
8488 Return 0 if we can't represent the location. */
8490 static dw_loc_descr_ref
8491 mem_loc_descriptor (rtx rtl, enum machine_mode mode, bool can_use_fbreg)
8493 dw_loc_descr_ref mem_loc_result = NULL;
8494 enum dwarf_location_atom op;
8496 /* Note that for a dynamically sized array, the location we will generate a
8497 description of here will be the lowest numbered location which is
8498 actually within the array. That's *not* necessarily the same as the
8499 zeroth element of the array. */
8501 rtl = targetm.delegitimize_address (rtl);
8503 switch (GET_CODE (rtl))
8505 case POST_INC:
8506 case POST_DEC:
8507 case POST_MODIFY:
8508 /* POST_INC and POST_DEC can be handled just like a SUBREG. So we
8509 just fall into the SUBREG code. */
8511 /* ... fall through ... */
8513 case SUBREG:
8514 /* The case of a subreg may arise when we have a local (register)
8515 variable or a formal (register) parameter which doesn't quite fill
8516 up an entire register. For now, just assume that it is
8517 legitimate to make the Dwarf info refer to the whole register which
8518 contains the given subreg. */
8519 rtl = SUBREG_REG (rtl);
8521 /* ... fall through ... */
8523 case REG:
8524 /* Whenever a register number forms a part of the description of the
8525 method for calculating the (dynamic) address of a memory resident
8526 object, DWARF rules require the register number be referred to as
8527 a "base register". This distinction is not based in any way upon
8528 what category of register the hardware believes the given register
8529 belongs to. This is strictly DWARF terminology we're dealing with
8530 here. Note that in cases where the location of a memory-resident
8531 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
8532 OP_CONST (0)) the actual DWARF location descriptor that we generate
8533 may just be OP_BASEREG (basereg). This may look deceptively like
8534 the object in question was allocated to a register (rather than in
8535 memory) so DWARF consumers need to be aware of the subtle
8536 distinction between OP_REG and OP_BASEREG. */
8537 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
8538 mem_loc_result = based_loc_descr (dbx_reg_number (rtl), 0,
8539 can_use_fbreg);
8540 break;
8542 case MEM:
8543 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
8544 can_use_fbreg);
8545 if (mem_loc_result != 0)
8546 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
8547 break;
8549 case LO_SUM:
8550 rtl = XEXP (rtl, 1);
8552 /* ... fall through ... */
8554 case LABEL_REF:
8555 /* Some ports can transform a symbol ref into a label ref, because
8556 the symbol ref is too far away and has to be dumped into a constant
8557 pool. */
8558 case CONST:
8559 case SYMBOL_REF:
8560 /* Alternatively, the symbol in the constant pool might be referenced
8561 by a different symbol. */
8562 if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
8564 bool marked;
8565 rtx tmp = get_pool_constant_mark (rtl, &marked);
8567 if (GET_CODE (tmp) == SYMBOL_REF)
8569 rtl = tmp;
8570 if (CONSTANT_POOL_ADDRESS_P (tmp))
8571 get_pool_constant_mark (tmp, &marked);
8572 else
8573 marked = true;
8576 /* If all references to this pool constant were optimized away,
8577 it was not output and thus we can't represent it.
8578 FIXME: might try to use DW_OP_const_value here, though
8579 DW_OP_piece complicates it. */
8580 if (!marked)
8581 return 0;
8584 mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
8585 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
8586 mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
8587 VARRAY_PUSH_RTX (used_rtx_varray, rtl);
8588 break;
8590 case PRE_MODIFY:
8591 /* Extract the PLUS expression nested inside and fall into
8592 PLUS code below. */
8593 rtl = XEXP (rtl, 1);
8594 goto plus;
8596 case PRE_INC:
8597 case PRE_DEC:
8598 /* Turn these into a PLUS expression and fall into the PLUS code
8599 below. */
8600 rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
8601 GEN_INT (GET_CODE (rtl) == PRE_INC
8602 ? GET_MODE_UNIT_SIZE (mode)
8603 : -GET_MODE_UNIT_SIZE (mode)));
8605 /* ... fall through ... */
8607 case PLUS:
8608 plus:
8609 if (is_based_loc (rtl))
8610 mem_loc_result = based_loc_descr (dbx_reg_number (XEXP (rtl, 0)),
8611 INTVAL (XEXP (rtl, 1)),
8612 can_use_fbreg);
8613 else
8615 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
8616 can_use_fbreg);
8617 if (mem_loc_result == 0)
8618 break;
8620 if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
8621 && INTVAL (XEXP (rtl, 1)) >= 0)
8622 add_loc_descr (&mem_loc_result,
8623 new_loc_descr (DW_OP_plus_uconst,
8624 INTVAL (XEXP (rtl, 1)), 0));
8625 else
8627 add_loc_descr (&mem_loc_result,
8628 mem_loc_descriptor (XEXP (rtl, 1), mode,
8629 can_use_fbreg));
8630 add_loc_descr (&mem_loc_result,
8631 new_loc_descr (DW_OP_plus, 0, 0));
8634 break;
8636 /* If a pseudo-reg is optimized away, it is possible for it to
8637 be replaced with a MEM containing a multiply or shift. */
8638 case MULT:
8639 op = DW_OP_mul;
8640 goto do_binop;
8642 case ASHIFT:
8643 op = DW_OP_shl;
8644 goto do_binop;
8646 case ASHIFTRT:
8647 op = DW_OP_shra;
8648 goto do_binop;
8650 case LSHIFTRT:
8651 op = DW_OP_shr;
8652 goto do_binop;
8654 do_binop:
8656 dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
8657 can_use_fbreg);
8658 dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
8659 can_use_fbreg);
8661 if (op0 == 0 || op1 == 0)
8662 break;
8664 mem_loc_result = op0;
8665 add_loc_descr (&mem_loc_result, op1);
8666 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
8667 break;
8670 case CONST_INT:
8671 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
8672 break;
8674 default:
8675 gcc_unreachable ();
8678 return mem_loc_result;
8681 /* Return a descriptor that describes the concatenation of two locations.
8682 This is typically a complex variable. */
8684 static dw_loc_descr_ref
8685 concat_loc_descriptor (rtx x0, rtx x1)
8687 dw_loc_descr_ref cc_loc_result = NULL;
8688 dw_loc_descr_ref x0_ref = loc_descriptor (x0, true);
8689 dw_loc_descr_ref x1_ref = loc_descriptor (x1, true);
8691 if (x0_ref == 0 || x1_ref == 0)
8692 return 0;
8694 cc_loc_result = x0_ref;
8695 add_loc_descr (&cc_loc_result,
8696 new_loc_descr (DW_OP_piece,
8697 GET_MODE_SIZE (GET_MODE (x0)), 0));
8699 add_loc_descr (&cc_loc_result, x1_ref);
8700 add_loc_descr (&cc_loc_result,
8701 new_loc_descr (DW_OP_piece,
8702 GET_MODE_SIZE (GET_MODE (x1)), 0));
8704 return cc_loc_result;
8707 /* Output a proper Dwarf location descriptor for a variable or parameter
8708 which is either allocated in a register or in a memory location. For a
8709 register, we just generate an OP_REG and the register number. For a
8710 memory location we provide a Dwarf postfix expression describing how to
8711 generate the (dynamic) address of the object onto the address stack.
8713 If we don't know how to describe it, return 0. */
8715 static dw_loc_descr_ref
8716 loc_descriptor (rtx rtl, bool can_use_fbreg)
8718 dw_loc_descr_ref loc_result = NULL;
8720 switch (GET_CODE (rtl))
8722 case SUBREG:
8723 /* The case of a subreg may arise when we have a local (register)
8724 variable or a formal (register) parameter which doesn't quite fill
8725 up an entire register. For now, just assume that it is
8726 legitimate to make the Dwarf info refer to the whole register which
8727 contains the given subreg. */
8728 rtl = SUBREG_REG (rtl);
8730 /* ... fall through ... */
8732 case REG:
8733 loc_result = reg_loc_descriptor (rtl);
8734 break;
8736 case MEM:
8737 loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
8738 can_use_fbreg);
8739 break;
8741 case CONCAT:
8742 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1));
8743 break;
8745 case VAR_LOCATION:
8746 /* Single part. */
8747 if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
8749 loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), can_use_fbreg);
8750 break;
8753 rtl = XEXP (rtl, 1);
8754 /* FALLTHRU */
8756 case PARALLEL:
8758 rtvec par_elems = XVEC (rtl, 0);
8759 int num_elem = GET_NUM_ELEM (par_elems);
8760 enum machine_mode mode;
8761 int i;
8763 /* Create the first one, so we have something to add to. */
8764 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
8765 can_use_fbreg);
8766 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
8767 add_loc_descr (&loc_result,
8768 new_loc_descr (DW_OP_piece, GET_MODE_SIZE (mode), 0));
8769 for (i = 1; i < num_elem; i++)
8771 dw_loc_descr_ref temp;
8773 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
8774 can_use_fbreg);
8775 add_loc_descr (&loc_result, temp);
8776 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
8777 add_loc_descr (&loc_result,
8778 new_loc_descr (DW_OP_piece,
8779 GET_MODE_SIZE (mode), 0));
8782 break;
8784 default:
8785 gcc_unreachable ();
8788 return loc_result;
8791 /* Similar, but generate the descriptor from trees instead of rtl. This comes
8792 up particularly with variable length arrays. WANT_ADDRESS is 2 if this is
8793 a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
8794 top-level invocation, and we require the address of LOC; is 0 if we require
8795 the value of LOC. */
8797 static dw_loc_descr_ref
8798 loc_descriptor_from_tree_1 (tree loc, int want_address)
8800 dw_loc_descr_ref ret, ret1;
8801 int have_address = 0;
8802 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
8803 enum dwarf_location_atom op;
8805 /* ??? Most of the time we do not take proper care for sign/zero
8806 extending the values properly. Hopefully this won't be a real
8807 problem... */
8809 switch (TREE_CODE (loc))
8811 case ERROR_MARK:
8812 return 0;
8814 case PLACEHOLDER_EXPR:
8815 /* This case involves extracting fields from an object to determine the
8816 position of other fields. We don't try to encode this here. The
8817 only user of this is Ada, which encodes the needed information using
8818 the names of types. */
8819 return 0;
8821 case CALL_EXPR:
8822 return 0;
8824 case PREINCREMENT_EXPR:
8825 case PREDECREMENT_EXPR:
8826 case POSTINCREMENT_EXPR:
8827 case POSTDECREMENT_EXPR:
8828 /* There are no opcodes for these operations. */
8829 return 0;
8831 case ADDR_EXPR:
8832 /* If we already want an address, there's nothing we can do. */
8833 if (want_address)
8834 return 0;
8836 /* Otherwise, process the argument and look for the address. */
8837 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
8839 case VAR_DECL:
8840 if (DECL_THREAD_LOCAL (loc))
8842 rtx rtl;
8844 #ifndef ASM_OUTPUT_DWARF_DTPREL
8845 /* If this is not defined, we have no way to emit the data. */
8846 return 0;
8847 #endif
8849 /* The way DW_OP_GNU_push_tls_address is specified, we can only
8850 look up addresses of objects in the current module. */
8851 if (DECL_EXTERNAL (loc))
8852 return 0;
8854 rtl = rtl_for_decl_location (loc);
8855 if (rtl == NULL_RTX)
8856 return 0;
8858 if (!MEM_P (rtl))
8859 return 0;
8860 rtl = XEXP (rtl, 0);
8861 if (! CONSTANT_P (rtl))
8862 return 0;
8864 ret = new_loc_descr (INTERNAL_DW_OP_tls_addr, 0, 0);
8865 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
8866 ret->dw_loc_oprnd1.v.val_addr = rtl;
8868 ret1 = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
8869 add_loc_descr (&ret, ret1);
8871 have_address = 1;
8872 break;
8874 /* FALLTHRU */
8876 case PARM_DECL:
8877 if (DECL_VALUE_EXPR (loc))
8878 return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc), want_address);
8879 /* FALLTHRU */
8881 case RESULT_DECL:
8883 rtx rtl = rtl_for_decl_location (loc);
8885 if (rtl == NULL_RTX)
8886 return 0;
8887 else if (GET_CODE (rtl) == CONST_INT)
8889 HOST_WIDE_INT val = INTVAL (rtl);
8890 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
8891 val &= GET_MODE_MASK (DECL_MODE (loc));
8892 ret = int_loc_descriptor (val);
8894 else if (GET_CODE (rtl) == CONST_STRING)
8895 return 0;
8896 else if (CONSTANT_P (rtl))
8898 ret = new_loc_descr (DW_OP_addr, 0, 0);
8899 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
8900 ret->dw_loc_oprnd1.v.val_addr = rtl;
8902 else
8904 enum machine_mode mode;
8906 /* Certain constructs can only be represented at top-level. */
8907 if (want_address == 2)
8908 return loc_descriptor (rtl, true);
8910 mode = GET_MODE (rtl);
8911 if (MEM_P (rtl))
8913 rtl = XEXP (rtl, 0);
8914 have_address = 1;
8916 ret = mem_loc_descriptor (rtl, mode, true);
8919 break;
8921 case INDIRECT_REF:
8922 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
8923 have_address = 1;
8924 break;
8926 case COMPOUND_EXPR:
8927 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
8929 case NOP_EXPR:
8930 case CONVERT_EXPR:
8931 case NON_LVALUE_EXPR:
8932 case VIEW_CONVERT_EXPR:
8933 case SAVE_EXPR:
8934 case MODIFY_EXPR:
8935 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), want_address);
8937 case COMPONENT_REF:
8938 case BIT_FIELD_REF:
8939 case ARRAY_REF:
8940 case ARRAY_RANGE_REF:
8942 tree obj, offset;
8943 HOST_WIDE_INT bitsize, bitpos, bytepos;
8944 enum machine_mode mode;
8945 int volatilep;
8947 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
8948 &unsignedp, &volatilep);
8950 if (obj == loc)
8951 return 0;
8953 ret = loc_descriptor_from_tree_1 (obj, 1);
8954 if (ret == 0
8955 || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
8956 return 0;
8958 if (offset != NULL_TREE)
8960 /* Variable offset. */
8961 add_loc_descr (&ret, loc_descriptor_from_tree_1 (offset, 0));
8962 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
8965 bytepos = bitpos / BITS_PER_UNIT;
8966 if (bytepos > 0)
8967 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
8968 else if (bytepos < 0)
8970 add_loc_descr (&ret, int_loc_descriptor (bytepos));
8971 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
8974 have_address = 1;
8975 break;
8978 case INTEGER_CST:
8979 if (host_integerp (loc, 0))
8980 ret = int_loc_descriptor (tree_low_cst (loc, 0));
8981 else
8982 return 0;
8983 break;
8985 case CONSTRUCTOR:
8987 /* Get an RTL for this, if something has been emitted. */
8988 rtx rtl = lookup_constant_def (loc);
8989 enum machine_mode mode;
8991 if (!rtl || !MEM_P (rtl))
8992 return 0;
8993 mode = GET_MODE (rtl);
8994 rtl = XEXP (rtl, 0);
8995 ret = mem_loc_descriptor (rtl, mode, true);
8996 have_address = 1;
8997 break;
9000 case TRUTH_AND_EXPR:
9001 case TRUTH_ANDIF_EXPR:
9002 case BIT_AND_EXPR:
9003 op = DW_OP_and;
9004 goto do_binop;
9006 case TRUTH_XOR_EXPR:
9007 case BIT_XOR_EXPR:
9008 op = DW_OP_xor;
9009 goto do_binop;
9011 case TRUTH_OR_EXPR:
9012 case TRUTH_ORIF_EXPR:
9013 case BIT_IOR_EXPR:
9014 op = DW_OP_or;
9015 goto do_binop;
9017 case FLOOR_DIV_EXPR:
9018 case CEIL_DIV_EXPR:
9019 case ROUND_DIV_EXPR:
9020 case TRUNC_DIV_EXPR:
9021 op = DW_OP_div;
9022 goto do_binop;
9024 case MINUS_EXPR:
9025 op = DW_OP_minus;
9026 goto do_binop;
9028 case FLOOR_MOD_EXPR:
9029 case CEIL_MOD_EXPR:
9030 case ROUND_MOD_EXPR:
9031 case TRUNC_MOD_EXPR:
9032 op = DW_OP_mod;
9033 goto do_binop;
9035 case MULT_EXPR:
9036 op = DW_OP_mul;
9037 goto do_binop;
9039 case LSHIFT_EXPR:
9040 op = DW_OP_shl;
9041 goto do_binop;
9043 case RSHIFT_EXPR:
9044 op = (unsignedp ? DW_OP_shr : DW_OP_shra);
9045 goto do_binop;
9047 case PLUS_EXPR:
9048 if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
9049 && host_integerp (TREE_OPERAND (loc, 1), 0))
9051 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9052 if (ret == 0)
9053 return 0;
9055 add_loc_descr (&ret,
9056 new_loc_descr (DW_OP_plus_uconst,
9057 tree_low_cst (TREE_OPERAND (loc, 1),
9059 0));
9060 break;
9063 op = DW_OP_plus;
9064 goto do_binop;
9066 case LE_EXPR:
9067 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9068 return 0;
9070 op = DW_OP_le;
9071 goto do_binop;
9073 case GE_EXPR:
9074 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9075 return 0;
9077 op = DW_OP_ge;
9078 goto do_binop;
9080 case LT_EXPR:
9081 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9082 return 0;
9084 op = DW_OP_lt;
9085 goto do_binop;
9087 case GT_EXPR:
9088 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9089 return 0;
9091 op = DW_OP_gt;
9092 goto do_binop;
9094 case EQ_EXPR:
9095 op = DW_OP_eq;
9096 goto do_binop;
9098 case NE_EXPR:
9099 op = DW_OP_ne;
9100 goto do_binop;
9102 do_binop:
9103 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9104 ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9105 if (ret == 0 || ret1 == 0)
9106 return 0;
9108 add_loc_descr (&ret, ret1);
9109 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9110 break;
9112 case TRUTH_NOT_EXPR:
9113 case BIT_NOT_EXPR:
9114 op = DW_OP_not;
9115 goto do_unop;
9117 case ABS_EXPR:
9118 op = DW_OP_abs;
9119 goto do_unop;
9121 case NEGATE_EXPR:
9122 op = DW_OP_neg;
9123 goto do_unop;
9125 do_unop:
9126 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9127 if (ret == 0)
9128 return 0;
9130 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9131 break;
9133 case MIN_EXPR:
9134 case MAX_EXPR:
9136 const enum tree_code code =
9137 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
9139 loc = build3 (COND_EXPR, TREE_TYPE (loc),
9140 build2 (code, integer_type_node,
9141 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
9142 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
9145 /* ... fall through ... */
9147 case COND_EXPR:
9149 dw_loc_descr_ref lhs
9150 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9151 dw_loc_descr_ref rhs
9152 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
9153 dw_loc_descr_ref bra_node, jump_node, tmp;
9155 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9156 if (ret == 0 || lhs == 0 || rhs == 0)
9157 return 0;
9159 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
9160 add_loc_descr (&ret, bra_node);
9162 add_loc_descr (&ret, rhs);
9163 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
9164 add_loc_descr (&ret, jump_node);
9166 add_loc_descr (&ret, lhs);
9167 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9168 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
9170 /* ??? Need a node to point the skip at. Use a nop. */
9171 tmp = new_loc_descr (DW_OP_nop, 0, 0);
9172 add_loc_descr (&ret, tmp);
9173 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9174 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
9176 break;
9178 case FIX_TRUNC_EXPR:
9179 case FIX_CEIL_EXPR:
9180 case FIX_FLOOR_EXPR:
9181 case FIX_ROUND_EXPR:
9182 return 0;
9184 default:
9185 /* Leave front-end specific codes as simply unknown. This comes
9186 up, for instance, with the C STMT_EXPR. */
9187 if ((unsigned int) TREE_CODE (loc)
9188 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
9189 return 0;
9191 #ifdef ENABLE_CHECKING
9192 /* Otherwise this is a generic code; we should just lists all of
9193 these explicitly. Aborting means we forgot one. */
9194 gcc_unreachable ();
9195 #else
9196 /* In a release build, we want to degrade gracefully: better to
9197 generate incomplete debugging information than to crash. */
9198 return NULL;
9199 #endif
9202 /* Show if we can't fill the request for an address. */
9203 if (want_address && !have_address)
9204 return 0;
9206 /* If we've got an address and don't want one, dereference. */
9207 if (!want_address && have_address)
9209 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
9211 if (size > DWARF2_ADDR_SIZE || size == -1)
9212 return 0;
9213 else if (size == DWARF2_ADDR_SIZE)
9214 op = DW_OP_deref;
9215 else
9216 op = DW_OP_deref_size;
9218 add_loc_descr (&ret, new_loc_descr (op, size, 0));
9221 return ret;
9224 static inline dw_loc_descr_ref
9225 loc_descriptor_from_tree (tree loc)
9227 return loc_descriptor_from_tree_1 (loc, 2);
9230 /* Given a value, round it up to the lowest multiple of `boundary'
9231 which is not less than the value itself. */
9233 static inline HOST_WIDE_INT
9234 ceiling (HOST_WIDE_INT value, unsigned int boundary)
9236 return (((value + boundary - 1) / boundary) * boundary);
9239 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
9240 pointer to the declared type for the relevant field variable, or return
9241 `integer_type_node' if the given node turns out to be an
9242 ERROR_MARK node. */
9244 static inline tree
9245 field_type (tree decl)
9247 tree type;
9249 if (TREE_CODE (decl) == ERROR_MARK)
9250 return integer_type_node;
9252 type = DECL_BIT_FIELD_TYPE (decl);
9253 if (type == NULL_TREE)
9254 type = TREE_TYPE (decl);
9256 return type;
9259 /* Given a pointer to a tree node, return the alignment in bits for
9260 it, or else return BITS_PER_WORD if the node actually turns out to
9261 be an ERROR_MARK node. */
9263 static inline unsigned
9264 simple_type_align_in_bits (tree type)
9266 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
9269 static inline unsigned
9270 simple_decl_align_in_bits (tree decl)
9272 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
9275 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
9276 lowest addressed byte of the "containing object" for the given FIELD_DECL,
9277 or return 0 if we are unable to determine what that offset is, either
9278 because the argument turns out to be a pointer to an ERROR_MARK node, or
9279 because the offset is actually variable. (We can't handle the latter case
9280 just yet). */
9282 static HOST_WIDE_INT
9283 field_byte_offset (tree decl)
9285 unsigned int type_align_in_bits;
9286 unsigned int decl_align_in_bits;
9287 unsigned HOST_WIDE_INT type_size_in_bits;
9288 HOST_WIDE_INT object_offset_in_bits;
9289 tree type;
9290 tree field_size_tree;
9291 HOST_WIDE_INT bitpos_int;
9292 HOST_WIDE_INT deepest_bitpos;
9293 unsigned HOST_WIDE_INT field_size_in_bits;
9295 if (TREE_CODE (decl) == ERROR_MARK)
9296 return 0;
9298 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
9300 type = field_type (decl);
9301 field_size_tree = DECL_SIZE (decl);
9303 /* The size could be unspecified if there was an error, or for
9304 a flexible array member. */
9305 if (! field_size_tree)
9306 field_size_tree = bitsize_zero_node;
9308 /* We cannot yet cope with fields whose positions are variable, so
9309 for now, when we see such things, we simply return 0. Someday, we may
9310 be able to handle such cases, but it will be damn difficult. */
9311 if (! host_integerp (bit_position (decl), 0))
9312 return 0;
9314 bitpos_int = int_bit_position (decl);
9316 /* If we don't know the size of the field, pretend it's a full word. */
9317 if (host_integerp (field_size_tree, 1))
9318 field_size_in_bits = tree_low_cst (field_size_tree, 1);
9319 else
9320 field_size_in_bits = BITS_PER_WORD;
9322 type_size_in_bits = simple_type_size_in_bits (type);
9323 type_align_in_bits = simple_type_align_in_bits (type);
9324 decl_align_in_bits = simple_decl_align_in_bits (decl);
9326 /* The GCC front-end doesn't make any attempt to keep track of the starting
9327 bit offset (relative to the start of the containing structure type) of the
9328 hypothetical "containing object" for a bit-field. Thus, when computing
9329 the byte offset value for the start of the "containing object" of a
9330 bit-field, we must deduce this information on our own. This can be rather
9331 tricky to do in some cases. For example, handling the following structure
9332 type definition when compiling for an i386/i486 target (which only aligns
9333 long long's to 32-bit boundaries) can be very tricky:
9335 struct S { int field1; long long field2:31; };
9337 Fortunately, there is a simple rule-of-thumb which can be used in such
9338 cases. When compiling for an i386/i486, GCC will allocate 8 bytes for the
9339 structure shown above. It decides to do this based upon one simple rule
9340 for bit-field allocation. GCC allocates each "containing object" for each
9341 bit-field at the first (i.e. lowest addressed) legitimate alignment
9342 boundary (based upon the required minimum alignment for the declared type
9343 of the field) which it can possibly use, subject to the condition that
9344 there is still enough available space remaining in the containing object
9345 (when allocated at the selected point) to fully accommodate all of the
9346 bits of the bit-field itself.
9348 This simple rule makes it obvious why GCC allocates 8 bytes for each
9349 object of the structure type shown above. When looking for a place to
9350 allocate the "containing object" for `field2', the compiler simply tries
9351 to allocate a 64-bit "containing object" at each successive 32-bit
9352 boundary (starting at zero) until it finds a place to allocate that 64-
9353 bit field such that at least 31 contiguous (and previously unallocated)
9354 bits remain within that selected 64 bit field. (As it turns out, for the
9355 example above, the compiler finds it is OK to allocate the "containing
9356 object" 64-bit field at bit-offset zero within the structure type.)
9358 Here we attempt to work backwards from the limited set of facts we're
9359 given, and we try to deduce from those facts, where GCC must have believed
9360 that the containing object started (within the structure type). The value
9361 we deduce is then used (by the callers of this routine) to generate
9362 DW_AT_location and DW_AT_bit_offset attributes for fields (both bit-fields
9363 and, in the case of DW_AT_location, regular fields as well). */
9365 /* Figure out the bit-distance from the start of the structure to the
9366 "deepest" bit of the bit-field. */
9367 deepest_bitpos = bitpos_int + field_size_in_bits;
9369 /* This is the tricky part. Use some fancy footwork to deduce where the
9370 lowest addressed bit of the containing object must be. */
9371 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9373 /* Round up to type_align by default. This works best for bitfields. */
9374 object_offset_in_bits += type_align_in_bits - 1;
9375 object_offset_in_bits /= type_align_in_bits;
9376 object_offset_in_bits *= type_align_in_bits;
9378 if (object_offset_in_bits > bitpos_int)
9380 /* Sigh, the decl must be packed. */
9381 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9383 /* Round up to decl_align instead. */
9384 object_offset_in_bits += decl_align_in_bits - 1;
9385 object_offset_in_bits /= decl_align_in_bits;
9386 object_offset_in_bits *= decl_align_in_bits;
9389 return object_offset_in_bits / BITS_PER_UNIT;
9392 /* The following routines define various Dwarf attributes and any data
9393 associated with them. */
9395 /* Add a location description attribute value to a DIE.
9397 This emits location attributes suitable for whole variables and
9398 whole parameters. Note that the location attributes for struct fields are
9399 generated by the routine `data_member_location_attribute' below. */
9401 static inline void
9402 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
9403 dw_loc_descr_ref descr)
9405 if (descr != 0)
9406 add_AT_loc (die, attr_kind, descr);
9409 /* Attach the specialized form of location attribute used for data members of
9410 struct and union types. In the special case of a FIELD_DECL node which
9411 represents a bit-field, the "offset" part of this special location
9412 descriptor must indicate the distance in bytes from the lowest-addressed
9413 byte of the containing struct or union type to the lowest-addressed byte of
9414 the "containing object" for the bit-field. (See the `field_byte_offset'
9415 function above).
9417 For any given bit-field, the "containing object" is a hypothetical object
9418 (of some integral or enum type) within which the given bit-field lives. The
9419 type of this hypothetical "containing object" is always the same as the
9420 declared type of the individual bit-field itself (for GCC anyway... the
9421 DWARF spec doesn't actually mandate this). Note that it is the size (in
9422 bytes) of the hypothetical "containing object" which will be given in the
9423 DW_AT_byte_size attribute for this bit-field. (See the
9424 `byte_size_attribute' function below.) It is also used when calculating the
9425 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
9426 function below.) */
9428 static void
9429 add_data_member_location_attribute (dw_die_ref die, tree decl)
9431 HOST_WIDE_INT offset;
9432 dw_loc_descr_ref loc_descr = 0;
9434 if (TREE_CODE (decl) == TREE_BINFO)
9436 /* We're working on the TAG_inheritance for a base class. */
9437 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
9439 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
9440 aren't at a fixed offset from all (sub)objects of the same
9441 type. We need to extract the appropriate offset from our
9442 vtable. The following dwarf expression means
9444 BaseAddr = ObAddr + *((*ObAddr) - Offset)
9446 This is specific to the V3 ABI, of course. */
9448 dw_loc_descr_ref tmp;
9450 /* Make a copy of the object address. */
9451 tmp = new_loc_descr (DW_OP_dup, 0, 0);
9452 add_loc_descr (&loc_descr, tmp);
9454 /* Extract the vtable address. */
9455 tmp = new_loc_descr (DW_OP_deref, 0, 0);
9456 add_loc_descr (&loc_descr, tmp);
9458 /* Calculate the address of the offset. */
9459 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
9460 gcc_assert (offset < 0);
9462 tmp = int_loc_descriptor (-offset);
9463 add_loc_descr (&loc_descr, tmp);
9464 tmp = new_loc_descr (DW_OP_minus, 0, 0);
9465 add_loc_descr (&loc_descr, tmp);
9467 /* Extract the offset. */
9468 tmp = new_loc_descr (DW_OP_deref, 0, 0);
9469 add_loc_descr (&loc_descr, tmp);
9471 /* Add it to the object address. */
9472 tmp = new_loc_descr (DW_OP_plus, 0, 0);
9473 add_loc_descr (&loc_descr, tmp);
9475 else
9476 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
9478 else
9479 offset = field_byte_offset (decl);
9481 if (! loc_descr)
9483 enum dwarf_location_atom op;
9485 /* The DWARF2 standard says that we should assume that the structure
9486 address is already on the stack, so we can specify a structure field
9487 address by using DW_OP_plus_uconst. */
9489 #ifdef MIPS_DEBUGGING_INFO
9490 /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
9491 operator correctly. It works only if we leave the offset on the
9492 stack. */
9493 op = DW_OP_constu;
9494 #else
9495 op = DW_OP_plus_uconst;
9496 #endif
9498 loc_descr = new_loc_descr (op, offset, 0);
9501 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
9504 /* Writes integer values to dw_vec_const array. */
9506 static void
9507 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
9509 while (size != 0)
9511 *dest++ = val & 0xff;
9512 val >>= 8;
9513 --size;
9517 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
9519 static HOST_WIDE_INT
9520 extract_int (const unsigned char *src, unsigned int size)
9522 HOST_WIDE_INT val = 0;
9524 src += size;
9525 while (size != 0)
9527 val <<= 8;
9528 val |= *--src & 0xff;
9529 --size;
9531 return val;
9534 /* Writes floating point values to dw_vec_const array. */
9536 static void
9537 insert_float (rtx rtl, unsigned char *array)
9539 REAL_VALUE_TYPE rv;
9540 long val[4];
9541 int i;
9543 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
9544 real_to_target (val, &rv, GET_MODE (rtl));
9546 /* real_to_target puts 32-bit pieces in each long. Pack them. */
9547 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
9549 insert_int (val[i], 4, array);
9550 array += 4;
9554 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
9555 does not have a "location" either in memory or in a register. These
9556 things can arise in GNU C when a constant is passed as an actual parameter
9557 to an inlined function. They can also arise in C++ where declared
9558 constants do not necessarily get memory "homes". */
9560 static void
9561 add_const_value_attribute (dw_die_ref die, rtx rtl)
9563 switch (GET_CODE (rtl))
9565 case CONST_INT:
9567 HOST_WIDE_INT val = INTVAL (rtl);
9569 if (val < 0)
9570 add_AT_int (die, DW_AT_const_value, val);
9571 else
9572 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
9574 break;
9576 case CONST_DOUBLE:
9577 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
9578 floating-point constant. A CONST_DOUBLE is used whenever the
9579 constant requires more than one word in order to be adequately
9580 represented. We output CONST_DOUBLEs as blocks. */
9582 enum machine_mode mode = GET_MODE (rtl);
9584 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
9586 unsigned int length = GET_MODE_SIZE (mode);
9587 unsigned char *array = ggc_alloc (length);
9589 insert_float (rtl, array);
9590 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
9592 else
9594 /* ??? We really should be using HOST_WIDE_INT throughout. */
9595 gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
9597 add_AT_long_long (die, DW_AT_const_value,
9598 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
9601 break;
9603 case CONST_VECTOR:
9605 enum machine_mode mode = GET_MODE (rtl);
9606 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
9607 unsigned int length = CONST_VECTOR_NUNITS (rtl);
9608 unsigned char *array = ggc_alloc (length * elt_size);
9609 unsigned int i;
9610 unsigned char *p;
9612 switch (GET_MODE_CLASS (mode))
9614 case MODE_VECTOR_INT:
9615 for (i = 0, p = array; i < length; i++, p += elt_size)
9617 rtx elt = CONST_VECTOR_ELT (rtl, i);
9618 HOST_WIDE_INT lo, hi;
9620 switch (GET_CODE (elt))
9622 case CONST_INT:
9623 lo = INTVAL (elt);
9624 hi = -(lo < 0);
9625 break;
9627 case CONST_DOUBLE:
9628 lo = CONST_DOUBLE_LOW (elt);
9629 hi = CONST_DOUBLE_HIGH (elt);
9630 break;
9632 default:
9633 gcc_unreachable ();
9636 if (elt_size <= sizeof (HOST_WIDE_INT))
9637 insert_int (lo, elt_size, p);
9638 else
9640 unsigned char *p0 = p;
9641 unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
9643 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
9644 if (WORDS_BIG_ENDIAN)
9646 p0 = p1;
9647 p1 = p;
9649 insert_int (lo, sizeof (HOST_WIDE_INT), p0);
9650 insert_int (hi, sizeof (HOST_WIDE_INT), p1);
9653 break;
9655 case MODE_VECTOR_FLOAT:
9656 for (i = 0, p = array; i < length; i++, p += elt_size)
9658 rtx elt = CONST_VECTOR_ELT (rtl, i);
9659 insert_float (elt, p);
9661 break;
9663 default:
9664 gcc_unreachable ();
9667 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
9669 break;
9671 case CONST_STRING:
9672 add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
9673 break;
9675 case SYMBOL_REF:
9676 case LABEL_REF:
9677 case CONST:
9678 add_AT_addr (die, DW_AT_const_value, rtl);
9679 VARRAY_PUSH_RTX (used_rtx_varray, rtl);
9680 break;
9682 case PLUS:
9683 /* In cases where an inlined instance of an inline function is passed
9684 the address of an `auto' variable (which is local to the caller) we
9685 can get a situation where the DECL_RTL of the artificial local
9686 variable (for the inlining) which acts as a stand-in for the
9687 corresponding formal parameter (of the inline function) will look
9688 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
9689 exactly a compile-time constant expression, but it isn't the address
9690 of the (artificial) local variable either. Rather, it represents the
9691 *value* which the artificial local variable always has during its
9692 lifetime. We currently have no way to represent such quasi-constant
9693 values in Dwarf, so for now we just punt and generate nothing. */
9694 break;
9696 default:
9697 /* No other kinds of rtx should be possible here. */
9698 gcc_unreachable ();
9703 static rtx
9704 rtl_for_decl_location (tree decl)
9706 rtx rtl;
9708 /* Here we have to decide where we are going to say the parameter "lives"
9709 (as far as the debugger is concerned). We only have a couple of
9710 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
9712 DECL_RTL normally indicates where the parameter lives during most of the
9713 activation of the function. If optimization is enabled however, this
9714 could be either NULL or else a pseudo-reg. Both of those cases indicate
9715 that the parameter doesn't really live anywhere (as far as the code
9716 generation parts of GCC are concerned) during most of the function's
9717 activation. That will happen (for example) if the parameter is never
9718 referenced within the function.
9720 We could just generate a location descriptor here for all non-NULL
9721 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
9722 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
9723 where DECL_RTL is NULL or is a pseudo-reg.
9725 Note however that we can only get away with using DECL_INCOMING_RTL as
9726 a backup substitute for DECL_RTL in certain limited cases. In cases
9727 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
9728 we can be sure that the parameter was passed using the same type as it is
9729 declared to have within the function, and that its DECL_INCOMING_RTL
9730 points us to a place where a value of that type is passed.
9732 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
9733 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
9734 because in these cases DECL_INCOMING_RTL points us to a value of some
9735 type which is *different* from the type of the parameter itself. Thus,
9736 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
9737 such cases, the debugger would end up (for example) trying to fetch a
9738 `float' from a place which actually contains the first part of a
9739 `double'. That would lead to really incorrect and confusing
9740 output at debug-time.
9742 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
9743 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
9744 are a couple of exceptions however. On little-endian machines we can
9745 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
9746 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
9747 an integral type that is smaller than TREE_TYPE (decl). These cases arise
9748 when (on a little-endian machine) a non-prototyped function has a
9749 parameter declared to be of type `short' or `char'. In such cases,
9750 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
9751 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
9752 passed `int' value. If the debugger then uses that address to fetch
9753 a `short' or a `char' (on a little-endian machine) the result will be
9754 the correct data, so we allow for such exceptional cases below.
9756 Note that our goal here is to describe the place where the given formal
9757 parameter lives during most of the function's activation (i.e. between the
9758 end of the prologue and the start of the epilogue). We'll do that as best
9759 as we can. Note however that if the given formal parameter is modified
9760 sometime during the execution of the function, then a stack backtrace (at
9761 debug-time) will show the function as having been called with the *new*
9762 value rather than the value which was originally passed in. This happens
9763 rarely enough that it is not a major problem, but it *is* a problem, and
9764 I'd like to fix it.
9766 A future version of dwarf2out.c may generate two additional attributes for
9767 any given DW_TAG_formal_parameter DIE which will describe the "passed
9768 type" and the "passed location" for the given formal parameter in addition
9769 to the attributes we now generate to indicate the "declared type" and the
9770 "active location" for each parameter. This additional set of attributes
9771 could be used by debuggers for stack backtraces. Separately, note that
9772 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
9773 This happens (for example) for inlined-instances of inline function formal
9774 parameters which are never referenced. This really shouldn't be
9775 happening. All PARM_DECL nodes should get valid non-NULL
9776 DECL_INCOMING_RTL values. FIXME. */
9778 /* Use DECL_RTL as the "location" unless we find something better. */
9779 rtl = DECL_RTL_IF_SET (decl);
9781 /* When generating abstract instances, ignore everything except
9782 constants, symbols living in memory, and symbols living in
9783 fixed registers. */
9784 if (! reload_completed)
9786 if (rtl
9787 && (CONSTANT_P (rtl)
9788 || (MEM_P (rtl)
9789 && CONSTANT_P (XEXP (rtl, 0)))
9790 || (REG_P (rtl)
9791 && TREE_CODE (decl) == VAR_DECL
9792 && TREE_STATIC (decl))))
9794 rtl = targetm.delegitimize_address (rtl);
9795 return rtl;
9797 rtl = NULL_RTX;
9799 else if (TREE_CODE (decl) == PARM_DECL)
9801 if (rtl == NULL_RTX || is_pseudo_reg (rtl))
9803 tree declared_type = TREE_TYPE (decl);
9804 tree passed_type = DECL_ARG_TYPE (decl);
9805 enum machine_mode dmode = TYPE_MODE (declared_type);
9806 enum machine_mode pmode = TYPE_MODE (passed_type);
9808 /* This decl represents a formal parameter which was optimized out.
9809 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
9810 all cases where (rtl == NULL_RTX) just below. */
9811 if (dmode == pmode)
9812 rtl = DECL_INCOMING_RTL (decl);
9813 else if (SCALAR_INT_MODE_P (dmode)
9814 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
9815 && DECL_INCOMING_RTL (decl))
9817 rtx inc = DECL_INCOMING_RTL (decl);
9818 if (REG_P (inc))
9819 rtl = inc;
9820 else if (MEM_P (inc))
9822 if (BYTES_BIG_ENDIAN)
9823 rtl = adjust_address_nv (inc, dmode,
9824 GET_MODE_SIZE (pmode)
9825 - GET_MODE_SIZE (dmode));
9826 else
9827 rtl = inc;
9832 /* If the parm was passed in registers, but lives on the stack, then
9833 make a big endian correction if the mode of the type of the
9834 parameter is not the same as the mode of the rtl. */
9835 /* ??? This is the same series of checks that are made in dbxout.c before
9836 we reach the big endian correction code there. It isn't clear if all
9837 of these checks are necessary here, but keeping them all is the safe
9838 thing to do. */
9839 else if (MEM_P (rtl)
9840 && XEXP (rtl, 0) != const0_rtx
9841 && ! CONSTANT_P (XEXP (rtl, 0))
9842 /* Not passed in memory. */
9843 && !MEM_P (DECL_INCOMING_RTL (decl))
9844 /* Not passed by invisible reference. */
9845 && (!REG_P (XEXP (rtl, 0))
9846 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
9847 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
9848 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
9849 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
9850 #endif
9852 /* Big endian correction check. */
9853 && BYTES_BIG_ENDIAN
9854 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
9855 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
9856 < UNITS_PER_WORD))
9858 int offset = (UNITS_PER_WORD
9859 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
9861 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
9862 plus_constant (XEXP (rtl, 0), offset));
9865 else if (TREE_CODE (decl) == VAR_DECL
9866 && rtl
9867 && MEM_P (rtl)
9868 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
9869 && BYTES_BIG_ENDIAN)
9871 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
9872 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
9874 /* If a variable is declared "register" yet is smaller than
9875 a register, then if we store the variable to memory, it
9876 looks like we're storing a register-sized value, when in
9877 fact we are not. We need to adjust the offset of the
9878 storage location to reflect the actual value's bytes,
9879 else gdb will not be able to display it. */
9880 if (rsize > dsize)
9881 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
9882 plus_constant (XEXP (rtl, 0), rsize-dsize));
9885 if (rtl != NULL_RTX)
9887 rtl = eliminate_regs (rtl, 0, NULL_RTX);
9888 #ifdef LEAF_REG_REMAP
9889 if (current_function_uses_only_leaf_regs)
9890 leaf_renumber_regs_insn (rtl);
9891 #endif
9894 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
9895 and will have been substituted directly into all expressions that use it.
9896 C does not have such a concept, but C++ and other languages do. */
9897 else if (TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
9899 /* If a variable is initialized with a string constant without embedded
9900 zeros, build CONST_STRING. */
9901 if (TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
9902 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
9904 tree arrtype = TREE_TYPE (decl);
9905 tree enttype = TREE_TYPE (arrtype);
9906 tree domain = TYPE_DOMAIN (arrtype);
9907 tree init = DECL_INITIAL (decl);
9908 enum machine_mode mode = TYPE_MODE (enttype);
9910 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
9911 && domain
9912 && integer_zerop (TYPE_MIN_VALUE (domain))
9913 && compare_tree_int (TYPE_MAX_VALUE (domain),
9914 TREE_STRING_LENGTH (init) - 1) == 0
9915 && ((size_t) TREE_STRING_LENGTH (init)
9916 == strlen (TREE_STRING_POINTER (init)) + 1))
9917 rtl = gen_rtx_CONST_STRING (VOIDmode,
9918 ggc_strdup (TREE_STRING_POINTER (init)));
9920 /* If the initializer is something that we know will expand into an
9921 immediate RTL constant, expand it now. Expanding anything else
9922 tends to produce unresolved symbols; see debug/5770 and c++/6381. */
9923 else if (TREE_CODE (DECL_INITIAL (decl)) == INTEGER_CST
9924 || TREE_CODE (DECL_INITIAL (decl)) == REAL_CST)
9926 rtl = expand_expr (DECL_INITIAL (decl), NULL_RTX, VOIDmode,
9927 EXPAND_INITIALIZER);
9928 /* If expand_expr returns a MEM, it wasn't immediate. */
9929 gcc_assert (!rtl || !MEM_P (rtl));
9933 if (rtl)
9934 rtl = targetm.delegitimize_address (rtl);
9936 /* If we don't look past the constant pool, we risk emitting a
9937 reference to a constant pool entry that isn't referenced from
9938 code, and thus is not emitted. */
9939 if (rtl)
9940 rtl = avoid_constant_pool_reference (rtl);
9942 return rtl;
9945 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
9946 data attribute for a variable or a parameter. We generate the
9947 DW_AT_const_value attribute only in those cases where the given variable
9948 or parameter does not have a true "location" either in memory or in a
9949 register. This can happen (for example) when a constant is passed as an
9950 actual argument in a call to an inline function. (It's possible that
9951 these things can crop up in other ways also.) Note that one type of
9952 constant value which can be passed into an inlined function is a constant
9953 pointer. This can happen for example if an actual argument in an inlined
9954 function call evaluates to a compile-time constant address. */
9956 static void
9957 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
9958 enum dwarf_attribute attr)
9960 rtx rtl;
9961 dw_loc_descr_ref descr;
9962 var_loc_list *loc_list;
9964 if (TREE_CODE (decl) == ERROR_MARK)
9965 return;
9967 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
9968 || TREE_CODE (decl) == RESULT_DECL);
9970 /* See if we possibly have multiple locations for this variable. */
9971 loc_list = lookup_decl_loc (decl);
9973 /* If it truly has multiple locations, the first and last node will
9974 differ. */
9975 if (loc_list && loc_list->first != loc_list->last)
9977 const char *secname;
9978 const char *endname;
9979 dw_loc_list_ref list;
9980 rtx varloc;
9981 struct var_loc_node *node;
9983 /* We need to figure out what section we should use as the base
9984 for the address ranges where a given location is valid.
9985 1. If this particular DECL has a section associated with it,
9986 use that.
9987 2. If this function has a section associated with it, use
9988 that.
9989 3. Otherwise, use the text section.
9990 XXX: If you split a variable across multiple sections, this
9991 won't notice. */
9993 if (DECL_SECTION_NAME (decl))
9995 tree sectree = DECL_SECTION_NAME (decl);
9996 secname = TREE_STRING_POINTER (sectree);
9998 else if (current_function_decl
9999 && DECL_SECTION_NAME (current_function_decl))
10001 tree sectree = DECL_SECTION_NAME (current_function_decl);
10002 secname = TREE_STRING_POINTER (sectree);
10004 else
10005 secname = text_section_label;
10007 /* Now that we know what section we are using for a base,
10008 actually construct the list of locations.
10009 The first location information is what is passed to the
10010 function that creates the location list, and the remaining
10011 locations just get added on to that list.
10012 Note that we only know the start address for a location
10013 (IE location changes), so to build the range, we use
10014 the range [current location start, next location start].
10015 This means we have to special case the last node, and generate
10016 a range of [last location start, end of function label]. */
10018 node = loc_list->first;
10019 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10020 list = new_loc_list (loc_descriptor (varloc, attr != DW_AT_frame_base),
10021 node->label, node->next->label, secname, 1);
10022 node = node->next;
10024 for (; node->next; node = node->next)
10025 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10027 /* The variable has a location between NODE->LABEL and
10028 NODE->NEXT->LABEL. */
10029 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10030 add_loc_descr_to_loc_list (&list,
10031 loc_descriptor (varloc,
10032 attr != DW_AT_frame_base),
10033 node->label, node->next->label, secname);
10036 /* If the variable has a location at the last label
10037 it keeps its location until the end of function. */
10038 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10040 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
10042 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10043 if (!current_function_decl)
10044 endname = text_end_label;
10045 else
10047 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
10048 current_function_funcdef_no);
10049 endname = ggc_strdup (label_id);
10051 add_loc_descr_to_loc_list (&list,
10052 loc_descriptor (varloc,
10053 attr != DW_AT_frame_base),
10054 node->label, endname, secname);
10057 /* Finally, add the location list to the DIE, and we are done. */
10058 add_AT_loc_list (die, attr, list);
10059 return;
10062 rtl = rtl_for_decl_location (decl);
10063 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
10065 add_const_value_attribute (die, rtl);
10066 return;
10069 descr = loc_descriptor_from_tree (decl);
10070 if (descr)
10071 add_AT_location_description (die, attr, descr);
10074 /* If we don't have a copy of this variable in memory for some reason (such
10075 as a C++ member constant that doesn't have an out-of-line definition),
10076 we should tell the debugger about the constant value. */
10078 static void
10079 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
10081 tree init = DECL_INITIAL (decl);
10082 tree type = TREE_TYPE (decl);
10084 if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init
10085 && initializer_constant_valid_p (init, type) == null_pointer_node)
10086 /* OK */;
10087 else
10088 return;
10090 switch (TREE_CODE (type))
10092 case INTEGER_TYPE:
10093 if (host_integerp (init, 0))
10094 add_AT_unsigned (var_die, DW_AT_const_value,
10095 tree_low_cst (init, 0));
10096 else
10097 add_AT_long_long (var_die, DW_AT_const_value,
10098 TREE_INT_CST_HIGH (init),
10099 TREE_INT_CST_LOW (init));
10100 break;
10102 default:;
10106 /* Generate a DW_AT_name attribute given some string value to be included as
10107 the value of the attribute. */
10109 static void
10110 add_name_attribute (dw_die_ref die, const char *name_string)
10112 if (name_string != NULL && *name_string != 0)
10114 if (demangle_name_func)
10115 name_string = (*demangle_name_func) (name_string);
10117 add_AT_string (die, DW_AT_name, name_string);
10121 /* Generate a DW_AT_comp_dir attribute for DIE. */
10123 static void
10124 add_comp_dir_attribute (dw_die_ref die)
10126 const char *wd = get_src_pwd ();
10127 if (wd != NULL)
10128 add_AT_string (die, DW_AT_comp_dir, wd);
10131 /* Given a tree node describing an array bound (either lower or upper) output
10132 a representation for that bound. */
10134 static void
10135 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
10137 switch (TREE_CODE (bound))
10139 case ERROR_MARK:
10140 return;
10142 /* All fixed-bounds are represented by INTEGER_CST nodes. */
10143 case INTEGER_CST:
10144 if (! host_integerp (bound, 0)
10145 || (bound_attr == DW_AT_lower_bound
10146 && (((is_c_family () || is_java ()) && integer_zerop (bound))
10147 || (is_fortran () && integer_onep (bound)))))
10148 /* Use the default. */
10150 else
10151 add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
10152 break;
10154 case CONVERT_EXPR:
10155 case NOP_EXPR:
10156 case NON_LVALUE_EXPR:
10157 case VIEW_CONVERT_EXPR:
10158 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
10159 break;
10161 case SAVE_EXPR:
10162 break;
10164 case VAR_DECL:
10165 case PARM_DECL:
10166 case RESULT_DECL:
10168 dw_die_ref decl_die = lookup_decl_die (bound);
10170 /* ??? Can this happen, or should the variable have been bound
10171 first? Probably it can, since I imagine that we try to create
10172 the types of parameters in the order in which they exist in
10173 the list, and won't have created a forward reference to a
10174 later parameter. */
10175 if (decl_die != NULL)
10176 add_AT_die_ref (subrange_die, bound_attr, decl_die);
10177 break;
10180 default:
10182 /* Otherwise try to create a stack operation procedure to
10183 evaluate the value of the array bound. */
10185 dw_die_ref ctx, decl_die;
10186 dw_loc_descr_ref loc;
10188 loc = loc_descriptor_from_tree (bound);
10189 if (loc == NULL)
10190 break;
10192 if (current_function_decl == 0)
10193 ctx = comp_unit_die;
10194 else
10195 ctx = lookup_decl_die (current_function_decl);
10197 decl_die = new_die (DW_TAG_variable, ctx, bound);
10198 add_AT_flag (decl_die, DW_AT_artificial, 1);
10199 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
10200 add_AT_loc (decl_die, DW_AT_location, loc);
10202 add_AT_die_ref (subrange_die, bound_attr, decl_die);
10203 break;
10208 /* Note that the block of subscript information for an array type also
10209 includes information about the element type of type given array type. */
10211 static void
10212 add_subscript_info (dw_die_ref type_die, tree type)
10214 #ifndef MIPS_DEBUGGING_INFO
10215 unsigned dimension_number;
10216 #endif
10217 tree lower, upper;
10218 dw_die_ref subrange_die;
10220 /* The GNU compilers represent multidimensional array types as sequences of
10221 one dimensional array types whose element types are themselves array
10222 types. Here we squish that down, so that each multidimensional array
10223 type gets only one array_type DIE in the Dwarf debugging info. The draft
10224 Dwarf specification say that we are allowed to do this kind of
10225 compression in C (because there is no difference between an array or
10226 arrays and a multidimensional array in C) but for other source languages
10227 (e.g. Ada) we probably shouldn't do this. */
10229 /* ??? The SGI dwarf reader fails for multidimensional arrays with a
10230 const enum type. E.g. const enum machine_mode insn_operand_mode[2][10].
10231 We work around this by disabling this feature. See also
10232 gen_array_type_die. */
10233 #ifndef MIPS_DEBUGGING_INFO
10234 for (dimension_number = 0;
10235 TREE_CODE (type) == ARRAY_TYPE;
10236 type = TREE_TYPE (type), dimension_number++)
10237 #endif
10239 tree domain = TYPE_DOMAIN (type);
10241 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
10242 and (in GNU C only) variable bounds. Handle all three forms
10243 here. */
10244 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
10245 if (domain)
10247 /* We have an array type with specified bounds. */
10248 lower = TYPE_MIN_VALUE (domain);
10249 upper = TYPE_MAX_VALUE (domain);
10251 /* Define the index type. */
10252 if (TREE_TYPE (domain))
10254 /* ??? This is probably an Ada unnamed subrange type. Ignore the
10255 TREE_TYPE field. We can't emit debug info for this
10256 because it is an unnamed integral type. */
10257 if (TREE_CODE (domain) == INTEGER_TYPE
10258 && TYPE_NAME (domain) == NULL_TREE
10259 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
10260 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
10262 else
10263 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
10264 type_die);
10267 /* ??? If upper is NULL, the array has unspecified length,
10268 but it does have a lower bound. This happens with Fortran
10269 dimension arr(N:*)
10270 Since the debugger is definitely going to need to know N
10271 to produce useful results, go ahead and output the lower
10272 bound solo, and hope the debugger can cope. */
10274 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
10275 if (upper)
10276 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
10279 /* Otherwise we have an array type with an unspecified length. The
10280 DWARF-2 spec does not say how to handle this; let's just leave out the
10281 bounds. */
10285 static void
10286 add_byte_size_attribute (dw_die_ref die, tree tree_node)
10288 unsigned size;
10290 switch (TREE_CODE (tree_node))
10292 case ERROR_MARK:
10293 size = 0;
10294 break;
10295 case ENUMERAL_TYPE:
10296 case RECORD_TYPE:
10297 case UNION_TYPE:
10298 case QUAL_UNION_TYPE:
10299 size = int_size_in_bytes (tree_node);
10300 break;
10301 case FIELD_DECL:
10302 /* For a data member of a struct or union, the DW_AT_byte_size is
10303 generally given as the number of bytes normally allocated for an
10304 object of the *declared* type of the member itself. This is true
10305 even for bit-fields. */
10306 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
10307 break;
10308 default:
10309 gcc_unreachable ();
10312 /* Note that `size' might be -1 when we get to this point. If it is, that
10313 indicates that the byte size of the entity in question is variable. We
10314 have no good way of expressing this fact in Dwarf at the present time,
10315 so just let the -1 pass on through. */
10316 add_AT_unsigned (die, DW_AT_byte_size, size);
10319 /* For a FIELD_DECL node which represents a bit-field, output an attribute
10320 which specifies the distance in bits from the highest order bit of the
10321 "containing object" for the bit-field to the highest order bit of the
10322 bit-field itself.
10324 For any given bit-field, the "containing object" is a hypothetical object
10325 (of some integral or enum type) within which the given bit-field lives. The
10326 type of this hypothetical "containing object" is always the same as the
10327 declared type of the individual bit-field itself. The determination of the
10328 exact location of the "containing object" for a bit-field is rather
10329 complicated. It's handled by the `field_byte_offset' function (above).
10331 Note that it is the size (in bytes) of the hypothetical "containing object"
10332 which will be given in the DW_AT_byte_size attribute for this bit-field.
10333 (See `byte_size_attribute' above). */
10335 static inline void
10336 add_bit_offset_attribute (dw_die_ref die, tree decl)
10338 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
10339 tree type = DECL_BIT_FIELD_TYPE (decl);
10340 HOST_WIDE_INT bitpos_int;
10341 HOST_WIDE_INT highest_order_object_bit_offset;
10342 HOST_WIDE_INT highest_order_field_bit_offset;
10343 HOST_WIDE_INT unsigned bit_offset;
10345 /* Must be a field and a bit field. */
10346 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
10348 /* We can't yet handle bit-fields whose offsets are variable, so if we
10349 encounter such things, just return without generating any attribute
10350 whatsoever. Likewise for variable or too large size. */
10351 if (! host_integerp (bit_position (decl), 0)
10352 || ! host_integerp (DECL_SIZE (decl), 1))
10353 return;
10355 bitpos_int = int_bit_position (decl);
10357 /* Note that the bit offset is always the distance (in bits) from the
10358 highest-order bit of the "containing object" to the highest-order bit of
10359 the bit-field itself. Since the "high-order end" of any object or field
10360 is different on big-endian and little-endian machines, the computation
10361 below must take account of these differences. */
10362 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
10363 highest_order_field_bit_offset = bitpos_int;
10365 if (! BYTES_BIG_ENDIAN)
10367 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
10368 highest_order_object_bit_offset += simple_type_size_in_bits (type);
10371 bit_offset
10372 = (! BYTES_BIG_ENDIAN
10373 ? highest_order_object_bit_offset - highest_order_field_bit_offset
10374 : highest_order_field_bit_offset - highest_order_object_bit_offset);
10376 add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
10379 /* For a FIELD_DECL node which represents a bit field, output an attribute
10380 which specifies the length in bits of the given field. */
10382 static inline void
10383 add_bit_size_attribute (dw_die_ref die, tree decl)
10385 /* Must be a field and a bit field. */
10386 gcc_assert (TREE_CODE (decl) == FIELD_DECL
10387 && DECL_BIT_FIELD_TYPE (decl));
10389 if (host_integerp (DECL_SIZE (decl), 1))
10390 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
10393 /* If the compiled language is ANSI C, then add a 'prototyped'
10394 attribute, if arg types are given for the parameters of a function. */
10396 static inline void
10397 add_prototyped_attribute (dw_die_ref die, tree func_type)
10399 if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
10400 && TYPE_ARG_TYPES (func_type) != NULL)
10401 add_AT_flag (die, DW_AT_prototyped, 1);
10404 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
10405 by looking in either the type declaration or object declaration
10406 equate table. */
10408 static inline void
10409 add_abstract_origin_attribute (dw_die_ref die, tree origin)
10411 dw_die_ref origin_die = NULL;
10413 if (TREE_CODE (origin) != FUNCTION_DECL)
10415 /* We may have gotten separated from the block for the inlined
10416 function, if we're in an exception handler or some such; make
10417 sure that the abstract function has been written out.
10419 Doing this for nested functions is wrong, however; functions are
10420 distinct units, and our context might not even be inline. */
10421 tree fn = origin;
10423 if (TYPE_P (fn))
10424 fn = TYPE_STUB_DECL (fn);
10426 fn = decl_function_context (fn);
10427 if (fn)
10428 dwarf2out_abstract_function (fn);
10431 if (DECL_P (origin))
10432 origin_die = lookup_decl_die (origin);
10433 else if (TYPE_P (origin))
10434 origin_die = lookup_type_die (origin);
10436 gcc_assert (origin_die);
10438 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
10441 /* We do not currently support the pure_virtual attribute. */
10443 static inline void
10444 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
10446 if (DECL_VINDEX (func_decl))
10448 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
10450 if (host_integerp (DECL_VINDEX (func_decl), 0))
10451 add_AT_loc (die, DW_AT_vtable_elem_location,
10452 new_loc_descr (DW_OP_constu,
10453 tree_low_cst (DECL_VINDEX (func_decl), 0),
10454 0));
10456 /* GNU extension: Record what type this method came from originally. */
10457 if (debug_info_level > DINFO_LEVEL_TERSE)
10458 add_AT_die_ref (die, DW_AT_containing_type,
10459 lookup_type_die (DECL_CONTEXT (func_decl)));
10463 /* Add source coordinate attributes for the given decl. */
10465 static void
10466 add_src_coords_attributes (dw_die_ref die, tree decl)
10468 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
10469 unsigned file_index = lookup_filename (s.file);
10471 add_AT_unsigned (die, DW_AT_decl_file, file_index);
10472 add_AT_unsigned (die, DW_AT_decl_line, s.line);
10475 /* Add a DW_AT_name attribute and source coordinate attribute for the
10476 given decl, but only if it actually has a name. */
10478 static void
10479 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
10481 tree decl_name;
10483 decl_name = DECL_NAME (decl);
10484 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
10486 add_name_attribute (die, dwarf2_name (decl, 0));
10487 if (! DECL_ARTIFICIAL (decl))
10488 add_src_coords_attributes (die, decl);
10490 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
10491 && TREE_PUBLIC (decl)
10492 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
10493 && !DECL_ABSTRACT (decl))
10494 add_AT_string (die, DW_AT_MIPS_linkage_name,
10495 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
10498 #ifdef VMS_DEBUGGING_INFO
10499 /* Get the function's name, as described by its RTL. This may be different
10500 from the DECL_NAME name used in the source file. */
10501 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
10503 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
10504 XEXP (DECL_RTL (decl), 0));
10505 VARRAY_PUSH_RTX (used_rtx_varray, XEXP (DECL_RTL (decl), 0));
10507 #endif
10510 /* Push a new declaration scope. */
10512 static void
10513 push_decl_scope (tree scope)
10515 VARRAY_PUSH_TREE (decl_scope_table, scope);
10518 /* Pop a declaration scope. */
10520 static inline void
10521 pop_decl_scope (void)
10523 gcc_assert (VARRAY_ACTIVE_SIZE (decl_scope_table) > 0);
10525 VARRAY_POP (decl_scope_table);
10528 /* Return the DIE for the scope that immediately contains this type.
10529 Non-named types get global scope. Named types nested in other
10530 types get their containing scope if it's open, or global scope
10531 otherwise. All other types (i.e. function-local named types) get
10532 the current active scope. */
10534 static dw_die_ref
10535 scope_die_for (tree t, dw_die_ref context_die)
10537 dw_die_ref scope_die = NULL;
10538 tree containing_scope;
10539 int i;
10541 /* Non-types always go in the current scope. */
10542 gcc_assert (TYPE_P (t));
10544 containing_scope = TYPE_CONTEXT (t);
10546 /* Use the containing namespace if it was passed in (for a declaration). */
10547 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
10549 if (context_die == lookup_decl_die (containing_scope))
10550 /* OK */;
10551 else
10552 containing_scope = NULL_TREE;
10555 /* Ignore function type "scopes" from the C frontend. They mean that
10556 a tagged type is local to a parmlist of a function declarator, but
10557 that isn't useful to DWARF. */
10558 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
10559 containing_scope = NULL_TREE;
10561 if (containing_scope == NULL_TREE)
10562 scope_die = comp_unit_die;
10563 else if (TYPE_P (containing_scope))
10565 /* For types, we can just look up the appropriate DIE. But
10566 first we check to see if we're in the middle of emitting it
10567 so we know where the new DIE should go. */
10568 for (i = VARRAY_ACTIVE_SIZE (decl_scope_table) - 1; i >= 0; --i)
10569 if (VARRAY_TREE (decl_scope_table, i) == containing_scope)
10570 break;
10572 if (i < 0)
10574 gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
10575 || TREE_ASM_WRITTEN (containing_scope));
10577 /* If none of the current dies are suitable, we get file scope. */
10578 scope_die = comp_unit_die;
10580 else
10581 scope_die = lookup_type_die (containing_scope);
10583 else
10584 scope_die = context_die;
10586 return scope_die;
10589 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
10591 static inline int
10592 local_scope_p (dw_die_ref context_die)
10594 for (; context_die; context_die = context_die->die_parent)
10595 if (context_die->die_tag == DW_TAG_inlined_subroutine
10596 || context_die->die_tag == DW_TAG_subprogram)
10597 return 1;
10599 return 0;
10602 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
10603 whether or not to treat a DIE in this context as a declaration. */
10605 static inline int
10606 class_or_namespace_scope_p (dw_die_ref context_die)
10608 return (context_die
10609 && (context_die->die_tag == DW_TAG_structure_type
10610 || context_die->die_tag == DW_TAG_union_type
10611 || context_die->die_tag == DW_TAG_namespace));
10614 /* Many forms of DIEs require a "type description" attribute. This
10615 routine locates the proper "type descriptor" die for the type given
10616 by 'type', and adds a DW_AT_type attribute below the given die. */
10618 static void
10619 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
10620 int decl_volatile, dw_die_ref context_die)
10622 enum tree_code code = TREE_CODE (type);
10623 dw_die_ref type_die = NULL;
10625 /* ??? If this type is an unnamed subrange type of an integral or
10626 floating-point type, use the inner type. This is because we have no
10627 support for unnamed types in base_type_die. This can happen if this is
10628 an Ada subrange type. Correct solution is emit a subrange type die. */
10629 if ((code == INTEGER_TYPE || code == REAL_TYPE)
10630 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
10631 type = TREE_TYPE (type), code = TREE_CODE (type);
10633 if (code == ERROR_MARK
10634 /* Handle a special case. For functions whose return type is void, we
10635 generate *no* type attribute. (Note that no object may have type
10636 `void', so this only applies to function return types). */
10637 || code == VOID_TYPE)
10638 return;
10640 type_die = modified_type_die (type,
10641 decl_const || TYPE_READONLY (type),
10642 decl_volatile || TYPE_VOLATILE (type),
10643 context_die);
10645 if (type_die != NULL)
10646 add_AT_die_ref (object_die, DW_AT_type, type_die);
10649 /* Given an object die, add the calling convention attribute for the
10650 function call type. */
10651 static void
10652 add_calling_convention_attribute (dw_die_ref subr_die, tree type)
10654 enum dwarf_calling_convention value = DW_CC_normal;
10656 value = targetm.dwarf_calling_convention (type);
10658 /* Only add the attribute if the backend requests it, and
10659 is not DW_CC_normal. */
10660 if (value && (value != DW_CC_normal))
10661 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
10664 /* Given a tree pointer to a struct, class, union, or enum type node, return
10665 a pointer to the (string) tag name for the given type, or zero if the type
10666 was declared without a tag. */
10668 static const char *
10669 type_tag (tree type)
10671 const char *name = 0;
10673 if (TYPE_NAME (type) != 0)
10675 tree t = 0;
10677 /* Find the IDENTIFIER_NODE for the type name. */
10678 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
10679 t = TYPE_NAME (type);
10681 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
10682 a TYPE_DECL node, regardless of whether or not a `typedef' was
10683 involved. */
10684 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10685 && ! DECL_IGNORED_P (TYPE_NAME (type)))
10686 t = DECL_NAME (TYPE_NAME (type));
10688 /* Now get the name as a string, or invent one. */
10689 if (t != 0)
10690 name = IDENTIFIER_POINTER (t);
10693 return (name == 0 || *name == '\0') ? 0 : name;
10696 /* Return the type associated with a data member, make a special check
10697 for bit field types. */
10699 static inline tree
10700 member_declared_type (tree member)
10702 return (DECL_BIT_FIELD_TYPE (member)
10703 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
10706 /* Get the decl's label, as described by its RTL. This may be different
10707 from the DECL_NAME name used in the source file. */
10709 #if 0
10710 static const char *
10711 decl_start_label (tree decl)
10713 rtx x;
10714 const char *fnname;
10716 x = DECL_RTL (decl);
10717 gcc_assert (MEM_P (x));
10719 x = XEXP (x, 0);
10720 gcc_assert (GET_CODE (x) == SYMBOL_REF);
10722 fnname = XSTR (x, 0);
10723 return fnname;
10725 #endif
10727 /* These routines generate the internal representation of the DIE's for
10728 the compilation unit. Debugging information is collected by walking
10729 the declaration trees passed in from dwarf2out_decl(). */
10731 static void
10732 gen_array_type_die (tree type, dw_die_ref context_die)
10734 dw_die_ref scope_die = scope_die_for (type, context_die);
10735 dw_die_ref array_die;
10736 tree element_type;
10738 /* ??? The SGI dwarf reader fails for array of array of enum types unless
10739 the inner array type comes before the outer array type. Thus we must
10740 call gen_type_die before we call new_die. See below also. */
10741 #ifdef MIPS_DEBUGGING_INFO
10742 gen_type_die (TREE_TYPE (type), context_die);
10743 #endif
10745 array_die = new_die (DW_TAG_array_type, scope_die, type);
10746 add_name_attribute (array_die, type_tag (type));
10747 equate_type_number_to_die (type, array_die);
10749 if (TREE_CODE (type) == VECTOR_TYPE)
10751 /* The frontend feeds us a representation for the vector as a struct
10752 containing an array. Pull out the array type. */
10753 type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
10754 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
10757 #if 0
10758 /* We default the array ordering. SDB will probably do
10759 the right things even if DW_AT_ordering is not present. It's not even
10760 an issue until we start to get into multidimensional arrays anyway. If
10761 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
10762 then we'll have to put the DW_AT_ordering attribute back in. (But if
10763 and when we find out that we need to put these in, we will only do so
10764 for multidimensional arrays. */
10765 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
10766 #endif
10768 #ifdef MIPS_DEBUGGING_INFO
10769 /* The SGI compilers handle arrays of unknown bound by setting
10770 AT_declaration and not emitting any subrange DIEs. */
10771 if (! TYPE_DOMAIN (type))
10772 add_AT_flag (array_die, DW_AT_declaration, 1);
10773 else
10774 #endif
10775 add_subscript_info (array_die, type);
10777 /* Add representation of the type of the elements of this array type. */
10778 element_type = TREE_TYPE (type);
10780 /* ??? The SGI dwarf reader fails for multidimensional arrays with a
10781 const enum type. E.g. const enum machine_mode insn_operand_mode[2][10].
10782 We work around this by disabling this feature. See also
10783 add_subscript_info. */
10784 #ifndef MIPS_DEBUGGING_INFO
10785 while (TREE_CODE (element_type) == ARRAY_TYPE)
10786 element_type = TREE_TYPE (element_type);
10788 gen_type_die (element_type, context_die);
10789 #endif
10791 add_type_attribute (array_die, element_type, 0, 0, context_die);
10794 static void
10795 gen_set_type_die (tree type, dw_die_ref context_die)
10797 dw_die_ref type_die
10798 = new_die (DW_TAG_set_type, scope_die_for (type, context_die), type);
10800 equate_type_number_to_die (type, type_die);
10801 add_type_attribute (type_die, TREE_TYPE (type), 0, 0, context_die);
10804 #if 0
10805 static void
10806 gen_entry_point_die (tree decl, dw_die_ref context_die)
10808 tree origin = decl_ultimate_origin (decl);
10809 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
10811 if (origin != NULL)
10812 add_abstract_origin_attribute (decl_die, origin);
10813 else
10815 add_name_and_src_coords_attributes (decl_die, decl);
10816 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
10817 0, 0, context_die);
10820 if (DECL_ABSTRACT (decl))
10821 equate_decl_number_to_die (decl, decl_die);
10822 else
10823 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
10825 #endif
10827 /* Walk through the list of incomplete types again, trying once more to
10828 emit full debugging info for them. */
10830 static void
10831 retry_incomplete_types (void)
10833 int i;
10835 for (i = VARRAY_ACTIVE_SIZE (incomplete_types) - 1; i >= 0; i--)
10836 gen_type_die (VARRAY_TREE (incomplete_types, i), comp_unit_die);
10839 /* Generate a DIE to represent an inlined instance of an enumeration type. */
10841 static void
10842 gen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
10844 dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
10846 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
10847 be incomplete and such types are not marked. */
10848 add_abstract_origin_attribute (type_die, type);
10851 /* Generate a DIE to represent an inlined instance of a structure type. */
10853 static void
10854 gen_inlined_structure_type_die (tree type, dw_die_ref context_die)
10856 dw_die_ref type_die = new_die (DW_TAG_structure_type, context_die, type);
10858 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
10859 be incomplete and such types are not marked. */
10860 add_abstract_origin_attribute (type_die, type);
10863 /* Generate a DIE to represent an inlined instance of a union type. */
10865 static void
10866 gen_inlined_union_type_die (tree type, dw_die_ref context_die)
10868 dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
10870 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
10871 be incomplete and such types are not marked. */
10872 add_abstract_origin_attribute (type_die, type);
10875 /* Generate a DIE to represent an enumeration type. Note that these DIEs
10876 include all of the information about the enumeration values also. Each
10877 enumerated type name/value is listed as a child of the enumerated type
10878 DIE. */
10880 static dw_die_ref
10881 gen_enumeration_type_die (tree type, dw_die_ref context_die)
10883 dw_die_ref type_die = lookup_type_die (type);
10885 if (type_die == NULL)
10887 type_die = new_die (DW_TAG_enumeration_type,
10888 scope_die_for (type, context_die), type);
10889 equate_type_number_to_die (type, type_die);
10890 add_name_attribute (type_die, type_tag (type));
10892 else if (! TYPE_SIZE (type))
10893 return type_die;
10894 else
10895 remove_AT (type_die, DW_AT_declaration);
10897 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
10898 given enum type is incomplete, do not generate the DW_AT_byte_size
10899 attribute or the DW_AT_element_list attribute. */
10900 if (TYPE_SIZE (type))
10902 tree link;
10904 TREE_ASM_WRITTEN (type) = 1;
10905 add_byte_size_attribute (type_die, type);
10906 if (TYPE_STUB_DECL (type) != NULL_TREE)
10907 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
10909 /* If the first reference to this type was as the return type of an
10910 inline function, then it may not have a parent. Fix this now. */
10911 if (type_die->die_parent == NULL)
10912 add_child_die (scope_die_for (type, context_die), type_die);
10914 for (link = TYPE_VALUES (type);
10915 link != NULL; link = TREE_CHAIN (link))
10917 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
10918 tree value = TREE_VALUE (link);
10920 add_name_attribute (enum_die,
10921 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
10923 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
10924 /* DWARF2 does not provide a way of indicating whether or
10925 not enumeration constants are signed or unsigned. GDB
10926 always assumes the values are signed, so we output all
10927 values as if they were signed. That means that
10928 enumeration constants with very large unsigned values
10929 will appear to have negative values in the debugger. */
10930 add_AT_int (enum_die, DW_AT_const_value,
10931 tree_low_cst (value, tree_int_cst_sgn (value) > 0));
10934 else
10935 add_AT_flag (type_die, DW_AT_declaration, 1);
10937 return type_die;
10940 /* Generate a DIE to represent either a real live formal parameter decl or to
10941 represent just the type of some formal parameter position in some function
10942 type.
10944 Note that this routine is a bit unusual because its argument may be a
10945 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
10946 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
10947 node. If it's the former then this function is being called to output a
10948 DIE to represent a formal parameter object (or some inlining thereof). If
10949 it's the latter, then this function is only being called to output a
10950 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
10951 argument type of some subprogram type. */
10953 static dw_die_ref
10954 gen_formal_parameter_die (tree node, dw_die_ref context_die)
10956 dw_die_ref parm_die
10957 = new_die (DW_TAG_formal_parameter, context_die, node);
10958 tree origin;
10960 switch (TREE_CODE_CLASS (TREE_CODE (node)))
10962 case tcc_declaration:
10963 origin = decl_ultimate_origin (node);
10964 if (origin != NULL)
10965 add_abstract_origin_attribute (parm_die, origin);
10966 else
10968 add_name_and_src_coords_attributes (parm_die, node);
10969 add_type_attribute (parm_die, TREE_TYPE (node),
10970 TREE_READONLY (node),
10971 TREE_THIS_VOLATILE (node),
10972 context_die);
10973 if (DECL_ARTIFICIAL (node))
10974 add_AT_flag (parm_die, DW_AT_artificial, 1);
10977 equate_decl_number_to_die (node, parm_die);
10978 if (! DECL_ABSTRACT (node))
10979 add_location_or_const_value_attribute (parm_die, node, DW_AT_location);
10981 break;
10983 case tcc_type:
10984 /* We were called with some kind of a ..._TYPE node. */
10985 add_type_attribute (parm_die, node, 0, 0, context_die);
10986 break;
10988 default:
10989 gcc_unreachable ();
10992 return parm_die;
10995 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
10996 at the end of an (ANSI prototyped) formal parameters list. */
10998 static void
10999 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
11001 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
11004 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
11005 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
11006 parameters as specified in some function type specification (except for
11007 those which appear as part of a function *definition*). */
11009 static void
11010 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
11012 tree link;
11013 tree formal_type = NULL;
11014 tree first_parm_type;
11015 tree arg;
11017 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
11019 arg = DECL_ARGUMENTS (function_or_method_type);
11020 function_or_method_type = TREE_TYPE (function_or_method_type);
11022 else
11023 arg = NULL_TREE;
11025 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
11027 /* Make our first pass over the list of formal parameter types and output a
11028 DW_TAG_formal_parameter DIE for each one. */
11029 for (link = first_parm_type; link; )
11031 dw_die_ref parm_die;
11033 formal_type = TREE_VALUE (link);
11034 if (formal_type == void_type_node)
11035 break;
11037 /* Output a (nameless) DIE to represent the formal parameter itself. */
11038 parm_die = gen_formal_parameter_die (formal_type, context_die);
11039 if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
11040 && link == first_parm_type)
11041 || (arg && DECL_ARTIFICIAL (arg)))
11042 add_AT_flag (parm_die, DW_AT_artificial, 1);
11044 link = TREE_CHAIN (link);
11045 if (arg)
11046 arg = TREE_CHAIN (arg);
11049 /* If this function type has an ellipsis, add a
11050 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
11051 if (formal_type != void_type_node)
11052 gen_unspecified_parameters_die (function_or_method_type, context_die);
11054 /* Make our second (and final) pass over the list of formal parameter types
11055 and output DIEs to represent those types (as necessary). */
11056 for (link = TYPE_ARG_TYPES (function_or_method_type);
11057 link && TREE_VALUE (link);
11058 link = TREE_CHAIN (link))
11059 gen_type_die (TREE_VALUE (link), context_die);
11062 /* We want to generate the DIE for TYPE so that we can generate the
11063 die for MEMBER, which has been defined; we will need to refer back
11064 to the member declaration nested within TYPE. If we're trying to
11065 generate minimal debug info for TYPE, processing TYPE won't do the
11066 trick; we need to attach the member declaration by hand. */
11068 static void
11069 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
11071 gen_type_die (type, context_die);
11073 /* If we're trying to avoid duplicate debug info, we may not have
11074 emitted the member decl for this function. Emit it now. */
11075 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
11076 && ! lookup_decl_die (member))
11078 gcc_assert (!decl_ultimate_origin (member));
11080 push_decl_scope (type);
11081 if (TREE_CODE (member) == FUNCTION_DECL)
11082 gen_subprogram_die (member, lookup_type_die (type));
11083 else
11084 gen_variable_die (member, lookup_type_die (type));
11086 pop_decl_scope ();
11090 /* Generate the DWARF2 info for the "abstract" instance of a function which we
11091 may later generate inlined and/or out-of-line instances of. */
11093 static void
11094 dwarf2out_abstract_function (tree decl)
11096 dw_die_ref old_die;
11097 tree save_fn;
11098 tree context;
11099 int was_abstract = DECL_ABSTRACT (decl);
11101 /* Make sure we have the actual abstract inline, not a clone. */
11102 decl = DECL_ORIGIN (decl);
11104 old_die = lookup_decl_die (decl);
11105 if (old_die && get_AT (old_die, DW_AT_inline))
11106 /* We've already generated the abstract instance. */
11107 return;
11109 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
11110 we don't get confused by DECL_ABSTRACT. */
11111 if (debug_info_level > DINFO_LEVEL_TERSE)
11113 context = decl_class_context (decl);
11114 if (context)
11115 gen_type_die_for_member
11116 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
11119 /* Pretend we've just finished compiling this function. */
11120 save_fn = current_function_decl;
11121 current_function_decl = decl;
11123 set_decl_abstract_flags (decl, 1);
11124 dwarf2out_decl (decl);
11125 if (! was_abstract)
11126 set_decl_abstract_flags (decl, 0);
11128 current_function_decl = save_fn;
11131 /* Generate a DIE to represent a declared function (either file-scope or
11132 block-local). */
11134 static void
11135 gen_subprogram_die (tree decl, dw_die_ref context_die)
11137 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
11138 tree origin = decl_ultimate_origin (decl);
11139 dw_die_ref subr_die;
11140 rtx fp_reg;
11141 tree fn_arg_types;
11142 tree outer_scope;
11143 dw_die_ref old_die = lookup_decl_die (decl);
11144 int declaration = (current_function_decl != decl
11145 || class_or_namespace_scope_p (context_die));
11147 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
11148 started to generate the abstract instance of an inline, decided to output
11149 its containing class, and proceeded to emit the declaration of the inline
11150 from the member list for the class. If so, DECLARATION takes priority;
11151 we'll get back to the abstract instance when done with the class. */
11153 /* The class-scope declaration DIE must be the primary DIE. */
11154 if (origin && declaration && class_or_namespace_scope_p (context_die))
11156 origin = NULL;
11157 gcc_assert (!old_die);
11160 if (origin != NULL)
11162 gcc_assert (!declaration || local_scope_p (context_die));
11164 /* Fixup die_parent for the abstract instance of a nested
11165 inline function. */
11166 if (old_die && old_die->die_parent == NULL)
11167 add_child_die (context_die, old_die);
11169 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11170 add_abstract_origin_attribute (subr_die, origin);
11172 else if (old_die)
11174 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11175 unsigned file_index = lookup_filename (s.file);
11177 if (!get_AT_flag (old_die, DW_AT_declaration)
11178 /* We can have a normal definition following an inline one in the
11179 case of redefinition of GNU C extern inlines.
11180 It seems reasonable to use AT_specification in this case. */
11181 && !get_AT (old_die, DW_AT_inline))
11183 /* ??? This can happen if there is a bug in the program, for
11184 instance, if it has duplicate function definitions. Ideally,
11185 we should detect this case and ignore it. For now, if we have
11186 already reported an error, any error at all, then assume that
11187 we got here because of an input error, not a dwarf2 bug. */
11188 gcc_assert (errorcount);
11189 return;
11192 /* If the definition comes from the same place as the declaration,
11193 maybe use the old DIE. We always want the DIE for this function
11194 that has the *_pc attributes to be under comp_unit_die so the
11195 debugger can find it. We also need to do this for abstract
11196 instances of inlines, since the spec requires the out-of-line copy
11197 to have the same parent. For local class methods, this doesn't
11198 apply; we just use the old DIE. */
11199 if ((old_die->die_parent == comp_unit_die || context_die == NULL)
11200 && (DECL_ARTIFICIAL (decl)
11201 || (get_AT_unsigned (old_die, DW_AT_decl_file) == file_index
11202 && (get_AT_unsigned (old_die, DW_AT_decl_line)
11203 == (unsigned) s.line))))
11205 subr_die = old_die;
11207 /* Clear out the declaration attribute and the formal parameters.
11208 Do not remove all children, because it is possible that this
11209 declaration die was forced using force_decl_die(). In such
11210 cases die that forced declaration die (e.g. TAG_imported_module)
11211 is one of the children that we do not want to remove. */
11212 remove_AT (subr_die, DW_AT_declaration);
11213 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
11215 else
11217 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11218 add_AT_specification (subr_die, old_die);
11219 if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
11220 add_AT_unsigned (subr_die, DW_AT_decl_file, file_index);
11221 if (get_AT_unsigned (old_die, DW_AT_decl_line)
11222 != (unsigned) s.line)
11223 add_AT_unsigned
11224 (subr_die, DW_AT_decl_line, s.line);
11227 else
11229 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11231 if (TREE_PUBLIC (decl))
11232 add_AT_flag (subr_die, DW_AT_external, 1);
11234 add_name_and_src_coords_attributes (subr_die, decl);
11235 if (debug_info_level > DINFO_LEVEL_TERSE)
11237 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
11238 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
11239 0, 0, context_die);
11242 add_pure_or_virtual_attribute (subr_die, decl);
11243 if (DECL_ARTIFICIAL (decl))
11244 add_AT_flag (subr_die, DW_AT_artificial, 1);
11246 if (TREE_PROTECTED (decl))
11247 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
11248 else if (TREE_PRIVATE (decl))
11249 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
11252 if (declaration)
11254 if (!old_die || !get_AT (old_die, DW_AT_inline))
11256 add_AT_flag (subr_die, DW_AT_declaration, 1);
11258 /* The first time we see a member function, it is in the context of
11259 the class to which it belongs. We make sure of this by emitting
11260 the class first. The next time is the definition, which is
11261 handled above. The two may come from the same source text.
11263 Note that force_decl_die() forces function declaration die. It is
11264 later reused to represent definition. */
11265 equate_decl_number_to_die (decl, subr_die);
11268 else if (DECL_ABSTRACT (decl))
11270 if (DECL_DECLARED_INLINE_P (decl))
11272 if (cgraph_function_possibly_inlined_p (decl))
11273 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
11274 else
11275 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
11277 else
11279 if (cgraph_function_possibly_inlined_p (decl))
11280 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
11281 else
11282 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
11285 equate_decl_number_to_die (decl, subr_die);
11287 else if (!DECL_EXTERNAL (decl))
11289 if (!old_die || !get_AT (old_die, DW_AT_inline))
11290 equate_decl_number_to_die (decl, subr_die);
11292 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
11293 current_function_funcdef_no);
11294 add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
11295 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
11296 current_function_funcdef_no);
11297 add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
11299 add_pubname (decl, subr_die);
11300 add_arange (decl, subr_die);
11302 #ifdef MIPS_DEBUGGING_INFO
11303 /* Add a reference to the FDE for this routine. */
11304 add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
11305 #endif
11307 /* Define the "frame base" location for this routine. We use the
11308 frame pointer or stack pointer registers, since the RTL for local
11309 variables is relative to one of them. */
11310 if (frame_base_decl && lookup_decl_loc (frame_base_decl) != NULL)
11312 add_location_or_const_value_attribute (subr_die, frame_base_decl,
11313 DW_AT_frame_base);
11315 else
11317 fp_reg
11318 = frame_pointer_needed ? hard_frame_pointer_rtx : stack_pointer_rtx;
11319 add_AT_loc (subr_die, DW_AT_frame_base, reg_loc_descriptor (fp_reg));
11322 if (cfun->static_chain_decl)
11323 add_AT_location_description (subr_die, DW_AT_static_link,
11324 loc_descriptor_from_tree (cfun->static_chain_decl));
11327 /* Now output descriptions of the arguments for this function. This gets
11328 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
11329 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
11330 `...' at the end of the formal parameter list. In order to find out if
11331 there was a trailing ellipsis or not, we must instead look at the type
11332 associated with the FUNCTION_DECL. This will be a node of type
11333 FUNCTION_TYPE. If the chain of type nodes hanging off of this
11334 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
11335 an ellipsis at the end. */
11337 /* In the case where we are describing a mere function declaration, all we
11338 need to do here (and all we *can* do here) is to describe the *types* of
11339 its formal parameters. */
11340 if (debug_info_level <= DINFO_LEVEL_TERSE)
11342 else if (declaration)
11343 gen_formal_types_die (decl, subr_die);
11344 else
11346 /* Generate DIEs to represent all known formal parameters. */
11347 tree arg_decls = DECL_ARGUMENTS (decl);
11348 tree parm;
11350 /* When generating DIEs, generate the unspecified_parameters DIE
11351 instead if we come across the arg "__builtin_va_alist" */
11352 for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
11353 if (TREE_CODE (parm) == PARM_DECL)
11355 if (DECL_NAME (parm)
11356 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
11357 "__builtin_va_alist"))
11358 gen_unspecified_parameters_die (parm, subr_die);
11359 else
11360 gen_decl_die (parm, subr_die);
11363 /* Decide whether we need an unspecified_parameters DIE at the end.
11364 There are 2 more cases to do this for: 1) the ansi ... declaration -
11365 this is detectable when the end of the arg list is not a
11366 void_type_node 2) an unprototyped function declaration (not a
11367 definition). This just means that we have no info about the
11368 parameters at all. */
11369 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
11370 if (fn_arg_types != NULL)
11372 /* This is the prototyped case, check for.... */
11373 if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
11374 gen_unspecified_parameters_die (decl, subr_die);
11376 else if (DECL_INITIAL (decl) == NULL_TREE)
11377 gen_unspecified_parameters_die (decl, subr_die);
11380 /* Output Dwarf info for all of the stuff within the body of the function
11381 (if it has one - it may be just a declaration). */
11382 outer_scope = DECL_INITIAL (decl);
11384 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
11385 a function. This BLOCK actually represents the outermost binding contour
11386 for the function, i.e. the contour in which the function's formal
11387 parameters and labels get declared. Curiously, it appears that the front
11388 end doesn't actually put the PARM_DECL nodes for the current function onto
11389 the BLOCK_VARS list for this outer scope, but are strung off of the
11390 DECL_ARGUMENTS list for the function instead.
11392 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
11393 the LABEL_DECL nodes for the function however, and we output DWARF info
11394 for those in decls_for_scope. Just within the `outer_scope' there will be
11395 a BLOCK node representing the function's outermost pair of curly braces,
11396 and any blocks used for the base and member initializers of a C++
11397 constructor function. */
11398 if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
11400 /* Emit a DW_TAG_variable DIE for a named return value. */
11401 if (DECL_NAME (DECL_RESULT (decl)))
11402 gen_decl_die (DECL_RESULT (decl), subr_die);
11404 current_function_has_inlines = 0;
11405 decls_for_scope (outer_scope, subr_die, 0);
11407 #if 0 && defined (MIPS_DEBUGGING_INFO)
11408 if (current_function_has_inlines)
11410 add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
11411 if (! comp_unit_has_inlines)
11413 add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
11414 comp_unit_has_inlines = 1;
11417 #endif
11419 /* Add the calling convention attribute if requested. */
11420 add_calling_convention_attribute (subr_die, TREE_TYPE (decl));
11424 /* Generate a DIE to represent a declared data object. */
11426 static void
11427 gen_variable_die (tree decl, dw_die_ref context_die)
11429 tree origin = decl_ultimate_origin (decl);
11430 dw_die_ref var_die = new_die (DW_TAG_variable, context_die, decl);
11432 dw_die_ref old_die = lookup_decl_die (decl);
11433 int declaration = (DECL_EXTERNAL (decl)
11434 || class_or_namespace_scope_p (context_die));
11436 if (origin != NULL)
11437 add_abstract_origin_attribute (var_die, origin);
11439 /* Loop unrolling can create multiple blocks that refer to the same
11440 static variable, so we must test for the DW_AT_declaration flag.
11442 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
11443 copy decls and set the DECL_ABSTRACT flag on them instead of
11444 sharing them.
11446 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
11447 else if (old_die && TREE_STATIC (decl)
11448 && get_AT_flag (old_die, DW_AT_declaration) == 1)
11450 /* This is a definition of a C++ class level static. */
11451 add_AT_specification (var_die, old_die);
11452 if (DECL_NAME (decl))
11454 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11455 unsigned file_index = lookup_filename (s.file);
11457 if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
11458 add_AT_unsigned (var_die, DW_AT_decl_file, file_index);
11460 if (get_AT_unsigned (old_die, DW_AT_decl_line)
11461 != (unsigned) s.line)
11463 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
11466 else
11468 add_name_and_src_coords_attributes (var_die, decl);
11469 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
11470 TREE_THIS_VOLATILE (decl), context_die);
11472 if (TREE_PUBLIC (decl))
11473 add_AT_flag (var_die, DW_AT_external, 1);
11475 if (DECL_ARTIFICIAL (decl))
11476 add_AT_flag (var_die, DW_AT_artificial, 1);
11478 if (TREE_PROTECTED (decl))
11479 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
11480 else if (TREE_PRIVATE (decl))
11481 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
11484 if (declaration)
11485 add_AT_flag (var_die, DW_AT_declaration, 1);
11487 if (DECL_ABSTRACT (decl) || declaration)
11488 equate_decl_number_to_die (decl, var_die);
11490 if (! declaration && ! DECL_ABSTRACT (decl))
11492 add_location_or_const_value_attribute (var_die, decl, DW_AT_location);
11493 add_pubname (decl, var_die);
11495 else
11496 tree_add_const_value_attribute (var_die, decl);
11499 /* Generate a DIE to represent a label identifier. */
11501 static void
11502 gen_label_die (tree decl, dw_die_ref context_die)
11504 tree origin = decl_ultimate_origin (decl);
11505 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
11506 rtx insn;
11507 char label[MAX_ARTIFICIAL_LABEL_BYTES];
11509 if (origin != NULL)
11510 add_abstract_origin_attribute (lbl_die, origin);
11511 else
11512 add_name_and_src_coords_attributes (lbl_die, decl);
11514 if (DECL_ABSTRACT (decl))
11515 equate_decl_number_to_die (decl, lbl_die);
11516 else
11518 insn = DECL_RTL_IF_SET (decl);
11520 /* Deleted labels are programmer specified labels which have been
11521 eliminated because of various optimizations. We still emit them
11522 here so that it is possible to put breakpoints on them. */
11523 if (insn
11524 && (LABEL_P (insn)
11525 || ((NOTE_P (insn)
11526 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED_LABEL))))
11528 /* When optimization is enabled (via -O) some parts of the compiler
11529 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
11530 represent source-level labels which were explicitly declared by
11531 the user. This really shouldn't be happening though, so catch
11532 it if it ever does happen. */
11533 gcc_assert (!INSN_DELETED_P (insn));
11535 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
11536 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
11541 /* Generate a DIE for a lexical block. */
11543 static void
11544 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
11546 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
11547 char label[MAX_ARTIFICIAL_LABEL_BYTES];
11549 if (! BLOCK_ABSTRACT (stmt))
11551 if (BLOCK_FRAGMENT_CHAIN (stmt))
11553 tree chain;
11555 add_AT_range_list (stmt_die, DW_AT_ranges, add_ranges (stmt));
11557 chain = BLOCK_FRAGMENT_CHAIN (stmt);
11560 add_ranges (chain);
11561 chain = BLOCK_FRAGMENT_CHAIN (chain);
11563 while (chain);
11564 add_ranges (NULL);
11566 else
11568 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
11569 BLOCK_NUMBER (stmt));
11570 add_AT_lbl_id (stmt_die, DW_AT_low_pc, label);
11571 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
11572 BLOCK_NUMBER (stmt));
11573 add_AT_lbl_id (stmt_die, DW_AT_high_pc, label);
11577 decls_for_scope (stmt, stmt_die, depth);
11580 /* Generate a DIE for an inlined subprogram. */
11582 static void
11583 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
11585 tree decl = block_ultimate_origin (stmt);
11587 /* Emit info for the abstract instance first, if we haven't yet. We
11588 must emit this even if the block is abstract, otherwise when we
11589 emit the block below (or elsewhere), we may end up trying to emit
11590 a die whose origin die hasn't been emitted, and crashing. */
11591 dwarf2out_abstract_function (decl);
11593 if (! BLOCK_ABSTRACT (stmt))
11595 dw_die_ref subr_die
11596 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
11597 char label[MAX_ARTIFICIAL_LABEL_BYTES];
11599 add_abstract_origin_attribute (subr_die, decl);
11600 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
11601 BLOCK_NUMBER (stmt));
11602 add_AT_lbl_id (subr_die, DW_AT_low_pc, label);
11603 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
11604 BLOCK_NUMBER (stmt));
11605 add_AT_lbl_id (subr_die, DW_AT_high_pc, label);
11606 decls_for_scope (stmt, subr_die, depth);
11607 current_function_has_inlines = 1;
11609 else
11610 /* We may get here if we're the outer block of function A that was
11611 inlined into function B that was inlined into function C. When
11612 generating debugging info for C, dwarf2out_abstract_function(B)
11613 would mark all inlined blocks as abstract, including this one.
11614 So, we wouldn't (and shouldn't) expect labels to be generated
11615 for this one. Instead, just emit debugging info for
11616 declarations within the block. This is particularly important
11617 in the case of initializers of arguments passed from B to us:
11618 if they're statement expressions containing declarations, we
11619 wouldn't generate dies for their abstract variables, and then,
11620 when generating dies for the real variables, we'd die (pun
11621 intended :-) */
11622 gen_lexical_block_die (stmt, context_die, depth);
11625 /* Generate a DIE for a field in a record, or structure. */
11627 static void
11628 gen_field_die (tree decl, dw_die_ref context_die)
11630 dw_die_ref decl_die;
11632 if (TREE_TYPE (decl) == error_mark_node)
11633 return;
11635 decl_die = new_die (DW_TAG_member, context_die, decl);
11636 add_name_and_src_coords_attributes (decl_die, decl);
11637 add_type_attribute (decl_die, member_declared_type (decl),
11638 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
11639 context_die);
11641 if (DECL_BIT_FIELD_TYPE (decl))
11643 add_byte_size_attribute (decl_die, decl);
11644 add_bit_size_attribute (decl_die, decl);
11645 add_bit_offset_attribute (decl_die, decl);
11648 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
11649 add_data_member_location_attribute (decl_die, decl);
11651 if (DECL_ARTIFICIAL (decl))
11652 add_AT_flag (decl_die, DW_AT_artificial, 1);
11654 if (TREE_PROTECTED (decl))
11655 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
11656 else if (TREE_PRIVATE (decl))
11657 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
11659 /* Equate decl number to die, so that we can look up this decl later on. */
11660 equate_decl_number_to_die (decl, decl_die);
11663 #if 0
11664 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
11665 Use modified_type_die instead.
11666 We keep this code here just in case these types of DIEs may be needed to
11667 represent certain things in other languages (e.g. Pascal) someday. */
11669 static void
11670 gen_pointer_type_die (tree type, dw_die_ref context_die)
11672 dw_die_ref ptr_die
11673 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
11675 equate_type_number_to_die (type, ptr_die);
11676 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
11677 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
11680 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
11681 Use modified_type_die instead.
11682 We keep this code here just in case these types of DIEs may be needed to
11683 represent certain things in other languages (e.g. Pascal) someday. */
11685 static void
11686 gen_reference_type_die (tree type, dw_die_ref context_die)
11688 dw_die_ref ref_die
11689 = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
11691 equate_type_number_to_die (type, ref_die);
11692 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
11693 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
11695 #endif
11697 /* Generate a DIE for a pointer to a member type. */
11699 static void
11700 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
11702 dw_die_ref ptr_die
11703 = new_die (DW_TAG_ptr_to_member_type,
11704 scope_die_for (type, context_die), type);
11706 equate_type_number_to_die (type, ptr_die);
11707 add_AT_die_ref (ptr_die, DW_AT_containing_type,
11708 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
11709 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
11712 /* Generate the DIE for the compilation unit. */
11714 static dw_die_ref
11715 gen_compile_unit_die (const char *filename)
11717 dw_die_ref die;
11718 char producer[250];
11719 const char *language_string = lang_hooks.name;
11720 int language;
11722 die = new_die (DW_TAG_compile_unit, NULL, NULL);
11724 if (filename)
11726 add_name_attribute (die, filename);
11727 /* Don't add cwd for <built-in>. */
11728 if (filename[0] != DIR_SEPARATOR && filename[0] != '<')
11729 add_comp_dir_attribute (die);
11732 sprintf (producer, "%s %s", language_string, version_string);
11734 #ifdef MIPS_DEBUGGING_INFO
11735 /* The MIPS/SGI compilers place the 'cc' command line options in the producer
11736 string. The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
11737 not appear in the producer string, the debugger reaches the conclusion
11738 that the object file is stripped and has no debugging information.
11739 To get the MIPS/SGI debugger to believe that there is debugging
11740 information in the object file, we add a -g to the producer string. */
11741 if (debug_info_level > DINFO_LEVEL_TERSE)
11742 strcat (producer, " -g");
11743 #endif
11745 add_AT_string (die, DW_AT_producer, producer);
11747 if (strcmp (language_string, "GNU C++") == 0)
11748 language = DW_LANG_C_plus_plus;
11749 else if (strcmp (language_string, "GNU Ada") == 0)
11750 language = DW_LANG_Ada95;
11751 else if (strcmp (language_string, "GNU F77") == 0)
11752 language = DW_LANG_Fortran77;
11753 else if (strcmp (language_string, "GNU F95") == 0)
11754 language = DW_LANG_Fortran95;
11755 else if (strcmp (language_string, "GNU Pascal") == 0)
11756 language = DW_LANG_Pascal83;
11757 else if (strcmp (language_string, "GNU Java") == 0)
11758 language = DW_LANG_Java;
11759 else
11760 language = DW_LANG_C89;
11762 add_AT_unsigned (die, DW_AT_language, language);
11763 return die;
11766 /* Generate a DIE for a string type. */
11768 static void
11769 gen_string_type_die (tree type, dw_die_ref context_die)
11771 dw_die_ref type_die
11772 = new_die (DW_TAG_string_type, scope_die_for (type, context_die), type);
11774 equate_type_number_to_die (type, type_die);
11776 /* ??? Fudge the string length attribute for now.
11777 TODO: add string length info. */
11778 #if 0
11779 string_length_attribute (TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
11780 bound_representation (upper_bound, 0, 'u');
11781 #endif
11784 /* Generate the DIE for a base class. */
11786 static void
11787 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
11789 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
11791 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
11792 add_data_member_location_attribute (die, binfo);
11794 if (BINFO_VIRTUAL_P (binfo))
11795 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
11797 if (access == access_public_node)
11798 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
11799 else if (access == access_protected_node)
11800 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
11803 /* Generate a DIE for a class member. */
11805 static void
11806 gen_member_die (tree type, dw_die_ref context_die)
11808 tree member;
11809 tree binfo = TYPE_BINFO (type);
11810 dw_die_ref child;
11812 /* If this is not an incomplete type, output descriptions of each of its
11813 members. Note that as we output the DIEs necessary to represent the
11814 members of this record or union type, we will also be trying to output
11815 DIEs to represent the *types* of those members. However the `type'
11816 function (above) will specifically avoid generating type DIEs for member
11817 types *within* the list of member DIEs for this (containing) type except
11818 for those types (of members) which are explicitly marked as also being
11819 members of this (containing) type themselves. The g++ front- end can
11820 force any given type to be treated as a member of some other (containing)
11821 type by setting the TYPE_CONTEXT of the given (member) type to point to
11822 the TREE node representing the appropriate (containing) type. */
11824 /* First output info about the base classes. */
11825 if (binfo)
11827 VEC (tree) *accesses = BINFO_BASE_ACCESSES (binfo);
11828 int i;
11829 tree base;
11831 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
11832 gen_inheritance_die (base,
11833 (accesses ? VEC_index (tree, accesses, i)
11834 : access_public_node), context_die);
11837 /* Now output info about the data members and type members. */
11838 for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
11840 /* If we thought we were generating minimal debug info for TYPE
11841 and then changed our minds, some of the member declarations
11842 may have already been defined. Don't define them again, but
11843 do put them in the right order. */
11845 child = lookup_decl_die (member);
11846 if (child)
11847 splice_child_die (context_die, child);
11848 else
11849 gen_decl_die (member, context_die);
11852 /* Now output info about the function members (if any). */
11853 for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
11855 /* Don't include clones in the member list. */
11856 if (DECL_ABSTRACT_ORIGIN (member))
11857 continue;
11859 child = lookup_decl_die (member);
11860 if (child)
11861 splice_child_die (context_die, child);
11862 else
11863 gen_decl_die (member, context_die);
11867 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
11868 is set, we pretend that the type was never defined, so we only get the
11869 member DIEs needed by later specification DIEs. */
11871 static void
11872 gen_struct_or_union_type_die (tree type, dw_die_ref context_die)
11874 dw_die_ref type_die = lookup_type_die (type);
11875 dw_die_ref scope_die = 0;
11876 int nested = 0;
11877 int complete = (TYPE_SIZE (type)
11878 && (! TYPE_STUB_DECL (type)
11879 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
11880 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
11882 if (type_die && ! complete)
11883 return;
11885 if (TYPE_CONTEXT (type) != NULL_TREE
11886 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
11887 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
11888 nested = 1;
11890 scope_die = scope_die_for (type, context_die);
11892 if (! type_die || (nested && scope_die == comp_unit_die))
11893 /* First occurrence of type or toplevel definition of nested class. */
11895 dw_die_ref old_die = type_die;
11897 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
11898 ? DW_TAG_structure_type : DW_TAG_union_type,
11899 scope_die, type);
11900 equate_type_number_to_die (type, type_die);
11901 if (old_die)
11902 add_AT_specification (type_die, old_die);
11903 else
11904 add_name_attribute (type_die, type_tag (type));
11906 else
11907 remove_AT (type_die, DW_AT_declaration);
11909 /* If this type has been completed, then give it a byte_size attribute and
11910 then give a list of members. */
11911 if (complete && !ns_decl)
11913 /* Prevent infinite recursion in cases where the type of some member of
11914 this type is expressed in terms of this type itself. */
11915 TREE_ASM_WRITTEN (type) = 1;
11916 add_byte_size_attribute (type_die, type);
11917 if (TYPE_STUB_DECL (type) != NULL_TREE)
11918 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
11920 /* If the first reference to this type was as the return type of an
11921 inline function, then it may not have a parent. Fix this now. */
11922 if (type_die->die_parent == NULL)
11923 add_child_die (scope_die, type_die);
11925 push_decl_scope (type);
11926 gen_member_die (type, type_die);
11927 pop_decl_scope ();
11929 /* GNU extension: Record what type our vtable lives in. */
11930 if (TYPE_VFIELD (type))
11932 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
11934 gen_type_die (vtype, context_die);
11935 add_AT_die_ref (type_die, DW_AT_containing_type,
11936 lookup_type_die (vtype));
11939 else
11941 add_AT_flag (type_die, DW_AT_declaration, 1);
11943 /* We don't need to do this for function-local types. */
11944 if (TYPE_STUB_DECL (type)
11945 && ! decl_function_context (TYPE_STUB_DECL (type)))
11946 VARRAY_PUSH_TREE (incomplete_types, type);
11950 /* Generate a DIE for a subroutine _type_. */
11952 static void
11953 gen_subroutine_type_die (tree type, dw_die_ref context_die)
11955 tree return_type = TREE_TYPE (type);
11956 dw_die_ref subr_die
11957 = new_die (DW_TAG_subroutine_type,
11958 scope_die_for (type, context_die), type);
11960 equate_type_number_to_die (type, subr_die);
11961 add_prototyped_attribute (subr_die, type);
11962 add_type_attribute (subr_die, return_type, 0, 0, context_die);
11963 gen_formal_types_die (type, subr_die);
11966 /* Generate a DIE for a type definition. */
11968 static void
11969 gen_typedef_die (tree decl, dw_die_ref context_die)
11971 dw_die_ref type_die;
11972 tree origin;
11974 if (TREE_ASM_WRITTEN (decl))
11975 return;
11977 TREE_ASM_WRITTEN (decl) = 1;
11978 type_die = new_die (DW_TAG_typedef, context_die, decl);
11979 origin = decl_ultimate_origin (decl);
11980 if (origin != NULL)
11981 add_abstract_origin_attribute (type_die, origin);
11982 else
11984 tree type;
11986 add_name_and_src_coords_attributes (type_die, decl);
11987 if (DECL_ORIGINAL_TYPE (decl))
11989 type = DECL_ORIGINAL_TYPE (decl);
11991 gcc_assert (type != TREE_TYPE (decl));
11992 equate_type_number_to_die (TREE_TYPE (decl), type_die);
11994 else
11995 type = TREE_TYPE (decl);
11997 add_type_attribute (type_die, type, TREE_READONLY (decl),
11998 TREE_THIS_VOLATILE (decl), context_die);
12001 if (DECL_ABSTRACT (decl))
12002 equate_decl_number_to_die (decl, type_die);
12005 /* Generate a type description DIE. */
12007 static void
12008 gen_type_die (tree type, dw_die_ref context_die)
12010 int need_pop;
12012 if (type == NULL_TREE || type == error_mark_node)
12013 return;
12015 if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12016 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
12018 if (TREE_ASM_WRITTEN (type))
12019 return;
12021 /* Prevent broken recursion; we can't hand off to the same type. */
12022 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
12024 TREE_ASM_WRITTEN (type) = 1;
12025 gen_decl_die (TYPE_NAME (type), context_die);
12026 return;
12029 /* We are going to output a DIE to represent the unqualified version
12030 of this type (i.e. without any const or volatile qualifiers) so
12031 get the main variant (i.e. the unqualified version) of this type
12032 now. (Vectors are special because the debugging info is in the
12033 cloned type itself). */
12034 if (TREE_CODE (type) != VECTOR_TYPE)
12035 type = type_main_variant (type);
12037 if (TREE_ASM_WRITTEN (type))
12038 return;
12040 switch (TREE_CODE (type))
12042 case ERROR_MARK:
12043 break;
12045 case POINTER_TYPE:
12046 case REFERENCE_TYPE:
12047 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
12048 ensures that the gen_type_die recursion will terminate even if the
12049 type is recursive. Recursive types are possible in Ada. */
12050 /* ??? We could perhaps do this for all types before the switch
12051 statement. */
12052 TREE_ASM_WRITTEN (type) = 1;
12054 /* For these types, all that is required is that we output a DIE (or a
12055 set of DIEs) to represent the "basis" type. */
12056 gen_type_die (TREE_TYPE (type), context_die);
12057 break;
12059 case OFFSET_TYPE:
12060 /* This code is used for C++ pointer-to-data-member types.
12061 Output a description of the relevant class type. */
12062 gen_type_die (TYPE_OFFSET_BASETYPE (type), context_die);
12064 /* Output a description of the type of the object pointed to. */
12065 gen_type_die (TREE_TYPE (type), context_die);
12067 /* Now output a DIE to represent this pointer-to-data-member type
12068 itself. */
12069 gen_ptr_to_mbr_type_die (type, context_die);
12070 break;
12072 case SET_TYPE:
12073 gen_type_die (TYPE_DOMAIN (type), context_die);
12074 gen_set_type_die (type, context_die);
12075 break;
12077 case FILE_TYPE:
12078 gen_type_die (TREE_TYPE (type), context_die);
12079 /* No way to represent these in Dwarf yet! */
12080 gcc_unreachable ();
12081 break;
12083 case FUNCTION_TYPE:
12084 /* Force out return type (in case it wasn't forced out already). */
12085 gen_type_die (TREE_TYPE (type), context_die);
12086 gen_subroutine_type_die (type, context_die);
12087 break;
12089 case METHOD_TYPE:
12090 /* Force out return type (in case it wasn't forced out already). */
12091 gen_type_die (TREE_TYPE (type), context_die);
12092 gen_subroutine_type_die (type, context_die);
12093 break;
12095 case ARRAY_TYPE:
12096 if (TYPE_STRING_FLAG (type) && TREE_CODE (TREE_TYPE (type)) == CHAR_TYPE)
12098 gen_type_die (TREE_TYPE (type), context_die);
12099 gen_string_type_die (type, context_die);
12101 else
12102 gen_array_type_die (type, context_die);
12103 break;
12105 case VECTOR_TYPE:
12106 gen_array_type_die (type, context_die);
12107 break;
12109 case ENUMERAL_TYPE:
12110 case RECORD_TYPE:
12111 case UNION_TYPE:
12112 case QUAL_UNION_TYPE:
12113 /* If this is a nested type whose containing class hasn't been written
12114 out yet, writing it out will cover this one, too. This does not apply
12115 to instantiations of member class templates; they need to be added to
12116 the containing class as they are generated. FIXME: This hurts the
12117 idea of combining type decls from multiple TUs, since we can't predict
12118 what set of template instantiations we'll get. */
12119 if (TYPE_CONTEXT (type)
12120 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
12121 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
12123 gen_type_die (TYPE_CONTEXT (type), context_die);
12125 if (TREE_ASM_WRITTEN (type))
12126 return;
12128 /* If that failed, attach ourselves to the stub. */
12129 push_decl_scope (TYPE_CONTEXT (type));
12130 context_die = lookup_type_die (TYPE_CONTEXT (type));
12131 need_pop = 1;
12133 else
12135 declare_in_namespace (type, context_die);
12136 need_pop = 0;
12139 if (TREE_CODE (type) == ENUMERAL_TYPE)
12140 gen_enumeration_type_die (type, context_die);
12141 else
12142 gen_struct_or_union_type_die (type, context_die);
12144 if (need_pop)
12145 pop_decl_scope ();
12147 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
12148 it up if it is ever completed. gen_*_type_die will set it for us
12149 when appropriate. */
12150 return;
12152 case VOID_TYPE:
12153 case INTEGER_TYPE:
12154 case REAL_TYPE:
12155 case COMPLEX_TYPE:
12156 case BOOLEAN_TYPE:
12157 case CHAR_TYPE:
12158 /* No DIEs needed for fundamental types. */
12159 break;
12161 case LANG_TYPE:
12162 /* No Dwarf representation currently defined. */
12163 break;
12165 default:
12166 gcc_unreachable ();
12169 TREE_ASM_WRITTEN (type) = 1;
12172 /* Generate a DIE for a tagged type instantiation. */
12174 static void
12175 gen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
12177 if (type == NULL_TREE || type == error_mark_node)
12178 return;
12180 /* We are going to output a DIE to represent the unqualified version of
12181 this type (i.e. without any const or volatile qualifiers) so make sure
12182 that we have the main variant (i.e. the unqualified version) of this
12183 type now. */
12184 gcc_assert (type == type_main_variant (type));
12186 /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
12187 an instance of an unresolved type. */
12189 switch (TREE_CODE (type))
12191 case ERROR_MARK:
12192 break;
12194 case ENUMERAL_TYPE:
12195 gen_inlined_enumeration_type_die (type, context_die);
12196 break;
12198 case RECORD_TYPE:
12199 gen_inlined_structure_type_die (type, context_die);
12200 break;
12202 case UNION_TYPE:
12203 case QUAL_UNION_TYPE:
12204 gen_inlined_union_type_die (type, context_die);
12205 break;
12207 default:
12208 gcc_unreachable ();
12212 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
12213 things which are local to the given block. */
12215 static void
12216 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
12218 int must_output_die = 0;
12219 tree origin;
12220 tree decl;
12221 enum tree_code origin_code;
12223 /* Ignore blocks never really used to make RTL. */
12224 if (stmt == NULL_TREE || !TREE_USED (stmt)
12225 || (!TREE_ASM_WRITTEN (stmt) && !BLOCK_ABSTRACT (stmt)))
12226 return;
12228 /* If the block is one fragment of a non-contiguous block, do not
12229 process the variables, since they will have been done by the
12230 origin block. Do process subblocks. */
12231 if (BLOCK_FRAGMENT_ORIGIN (stmt))
12233 tree sub;
12235 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
12236 gen_block_die (sub, context_die, depth + 1);
12238 return;
12241 /* Determine the "ultimate origin" of this block. This block may be an
12242 inlined instance of an inlined instance of inline function, so we have
12243 to trace all of the way back through the origin chain to find out what
12244 sort of node actually served as the original seed for the creation of
12245 the current block. */
12246 origin = block_ultimate_origin (stmt);
12247 origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
12249 /* Determine if we need to output any Dwarf DIEs at all to represent this
12250 block. */
12251 if (origin_code == FUNCTION_DECL)
12252 /* The outer scopes for inlinings *must* always be represented. We
12253 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
12254 must_output_die = 1;
12255 else
12257 /* In the case where the current block represents an inlining of the
12258 "body block" of an inline function, we must *NOT* output any DIE for
12259 this block because we have already output a DIE to represent the whole
12260 inlined function scope and the "body block" of any function doesn't
12261 really represent a different scope according to ANSI C rules. So we
12262 check here to make sure that this block does not represent a "body
12263 block inlining" before trying to set the MUST_OUTPUT_DIE flag. */
12264 if (! is_body_block (origin ? origin : stmt))
12266 /* Determine if this block directly contains any "significant"
12267 local declarations which we will need to output DIEs for. */
12268 if (debug_info_level > DINFO_LEVEL_TERSE)
12269 /* We are not in terse mode so *any* local declaration counts
12270 as being a "significant" one. */
12271 must_output_die = (BLOCK_VARS (stmt) != NULL);
12272 else
12273 /* We are in terse mode, so only local (nested) function
12274 definitions count as "significant" local declarations. */
12275 for (decl = BLOCK_VARS (stmt);
12276 decl != NULL; decl = TREE_CHAIN (decl))
12277 if (TREE_CODE (decl) == FUNCTION_DECL
12278 && DECL_INITIAL (decl))
12280 must_output_die = 1;
12281 break;
12286 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
12287 DIE for any block which contains no significant local declarations at
12288 all. Rather, in such cases we just call `decls_for_scope' so that any
12289 needed Dwarf info for any sub-blocks will get properly generated. Note
12290 that in terse mode, our definition of what constitutes a "significant"
12291 local declaration gets restricted to include only inlined function
12292 instances and local (nested) function definitions. */
12293 if (must_output_die)
12295 if (origin_code == FUNCTION_DECL)
12296 gen_inlined_subroutine_die (stmt, context_die, depth);
12297 else
12298 gen_lexical_block_die (stmt, context_die, depth);
12300 else
12301 decls_for_scope (stmt, context_die, depth);
12304 /* Generate all of the decls declared within a given scope and (recursively)
12305 all of its sub-blocks. */
12307 static void
12308 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
12310 tree decl;
12311 tree subblocks;
12313 /* Ignore blocks never really used to make RTL. */
12314 if (stmt == NULL_TREE || ! TREE_USED (stmt))
12315 return;
12317 /* Output the DIEs to represent all of the data objects and typedefs
12318 declared directly within this block but not within any nested
12319 sub-blocks. Also, nested function and tag DIEs have been
12320 generated with a parent of NULL; fix that up now. */
12321 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
12323 dw_die_ref die;
12325 if (TREE_CODE (decl) == FUNCTION_DECL)
12326 die = lookup_decl_die (decl);
12327 else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
12328 die = lookup_type_die (TREE_TYPE (decl));
12329 else
12330 die = NULL;
12332 if (die != NULL && die->die_parent == NULL)
12333 add_child_die (context_die, die);
12334 else
12335 gen_decl_die (decl, context_die);
12338 /* If we're at -g1, we're not interested in subblocks. */
12339 if (debug_info_level <= DINFO_LEVEL_TERSE)
12340 return;
12342 /* Output the DIEs to represent all sub-blocks (and the items declared
12343 therein) of this block. */
12344 for (subblocks = BLOCK_SUBBLOCKS (stmt);
12345 subblocks != NULL;
12346 subblocks = BLOCK_CHAIN (subblocks))
12347 gen_block_die (subblocks, context_die, depth + 1);
12350 /* Is this a typedef we can avoid emitting? */
12352 static inline int
12353 is_redundant_typedef (tree decl)
12355 if (TYPE_DECL_IS_STUB (decl))
12356 return 1;
12358 if (DECL_ARTIFICIAL (decl)
12359 && DECL_CONTEXT (decl)
12360 && is_tagged_type (DECL_CONTEXT (decl))
12361 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
12362 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
12363 /* Also ignore the artificial member typedef for the class name. */
12364 return 1;
12366 return 0;
12369 /* Returns the DIE for decl or aborts. */
12371 static dw_die_ref
12372 force_decl_die (tree decl)
12374 dw_die_ref decl_die;
12375 unsigned saved_external_flag;
12376 tree save_fn = NULL_TREE;
12377 decl_die = lookup_decl_die (decl);
12378 if (!decl_die)
12380 dw_die_ref context_die;
12381 tree decl_context = DECL_CONTEXT (decl);
12382 if (decl_context)
12384 /* Find die that represents this context. */
12385 if (TYPE_P (decl_context))
12386 context_die = force_type_die (decl_context);
12387 else
12388 context_die = force_decl_die (decl_context);
12390 else
12391 context_die = comp_unit_die;
12393 switch (TREE_CODE (decl))
12395 case FUNCTION_DECL:
12396 /* Clear current_function_decl, so that gen_subprogram_die thinks
12397 that this is a declaration. At this point, we just want to force
12398 declaration die. */
12399 save_fn = current_function_decl;
12400 current_function_decl = NULL_TREE;
12401 gen_subprogram_die (decl, context_die);
12402 current_function_decl = save_fn;
12403 break;
12405 case VAR_DECL:
12406 /* Set external flag to force declaration die. Restore it after
12407 gen_decl_die() call. */
12408 saved_external_flag = DECL_EXTERNAL (decl);
12409 DECL_EXTERNAL (decl) = 1;
12410 gen_decl_die (decl, context_die);
12411 DECL_EXTERNAL (decl) = saved_external_flag;
12412 break;
12414 case NAMESPACE_DECL:
12415 dwarf2out_decl (decl);
12416 break;
12418 default:
12419 gcc_unreachable ();
12422 /* See if we can find the die for this deci now.
12423 If not then abort. */
12424 if (!decl_die)
12425 decl_die = lookup_decl_die (decl);
12426 gcc_assert (decl_die);
12429 return decl_die;
12432 /* Returns the DIE for decl or aborts. */
12434 static dw_die_ref
12435 force_type_die (tree type)
12437 dw_die_ref type_die;
12439 type_die = lookup_type_die (type);
12440 if (!type_die)
12442 dw_die_ref context_die;
12443 if (TYPE_CONTEXT (type))
12444 if (TYPE_P (TYPE_CONTEXT (type)))
12445 context_die = force_type_die (TYPE_CONTEXT (type));
12446 else
12447 context_die = force_decl_die (TYPE_CONTEXT (type));
12448 else
12449 context_die = comp_unit_die;
12451 gen_type_die (type, context_die);
12452 type_die = lookup_type_die (type);
12453 gcc_assert (type_die);
12455 return type_die;
12458 /* Force out any required namespaces to be able to output DECL,
12459 and return the new context_die for it, if it's changed. */
12461 static dw_die_ref
12462 setup_namespace_context (tree thing, dw_die_ref context_die)
12464 tree context = (DECL_P (thing)
12465 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
12466 if (context && TREE_CODE (context) == NAMESPACE_DECL)
12467 /* Force out the namespace. */
12468 context_die = force_decl_die (context);
12470 return context_die;
12473 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
12474 type) within its namespace, if appropriate.
12476 For compatibility with older debuggers, namespace DIEs only contain
12477 declarations; all definitions are emitted at CU scope. */
12479 static void
12480 declare_in_namespace (tree thing, dw_die_ref context_die)
12482 dw_die_ref ns_context;
12484 if (debug_info_level <= DINFO_LEVEL_TERSE)
12485 return;
12487 ns_context = setup_namespace_context (thing, context_die);
12489 if (ns_context != context_die)
12491 if (DECL_P (thing))
12492 gen_decl_die (thing, ns_context);
12493 else
12494 gen_type_die (thing, ns_context);
12498 /* Generate a DIE for a namespace or namespace alias. */
12500 static void
12501 gen_namespace_die (tree decl)
12503 dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
12505 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
12506 they are an alias of. */
12507 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
12509 /* Output a real namespace. */
12510 dw_die_ref namespace_die
12511 = new_die (DW_TAG_namespace, context_die, decl);
12512 add_name_and_src_coords_attributes (namespace_die, decl);
12513 equate_decl_number_to_die (decl, namespace_die);
12515 else
12517 /* Output a namespace alias. */
12519 /* Force out the namespace we are an alias of, if necessary. */
12520 dw_die_ref origin_die
12521 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
12523 /* Now create the namespace alias DIE. */
12524 dw_die_ref namespace_die
12525 = new_die (DW_TAG_imported_declaration, context_die, decl);
12526 add_name_and_src_coords_attributes (namespace_die, decl);
12527 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
12528 equate_decl_number_to_die (decl, namespace_die);
12532 /* Generate Dwarf debug information for a decl described by DECL. */
12534 static void
12535 gen_decl_die (tree decl, dw_die_ref context_die)
12537 tree origin;
12539 if (DECL_P (decl) && DECL_IGNORED_P (decl))
12540 return;
12542 switch (TREE_CODE (decl))
12544 case ERROR_MARK:
12545 break;
12547 case CONST_DECL:
12548 /* The individual enumerators of an enum type get output when we output
12549 the Dwarf representation of the relevant enum type itself. */
12550 break;
12552 case FUNCTION_DECL:
12553 /* Don't output any DIEs to represent mere function declarations,
12554 unless they are class members or explicit block externs. */
12555 if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
12556 && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
12557 break;
12559 #if 0
12560 /* FIXME */
12561 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
12562 on local redeclarations of global functions. That seems broken. */
12563 if (current_function_decl != decl)
12564 /* This is only a declaration. */;
12565 #endif
12567 /* If we're emitting a clone, emit info for the abstract instance. */
12568 if (DECL_ORIGIN (decl) != decl)
12569 dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
12571 /* If we're emitting an out-of-line copy of an inline function,
12572 emit info for the abstract instance and set up to refer to it. */
12573 else if (cgraph_function_possibly_inlined_p (decl)
12574 && ! DECL_ABSTRACT (decl)
12575 && ! class_or_namespace_scope_p (context_die)
12576 /* dwarf2out_abstract_function won't emit a die if this is just
12577 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
12578 that case, because that works only if we have a die. */
12579 && DECL_INITIAL (decl) != NULL_TREE)
12581 dwarf2out_abstract_function (decl);
12582 set_decl_origin_self (decl);
12585 /* Otherwise we're emitting the primary DIE for this decl. */
12586 else if (debug_info_level > DINFO_LEVEL_TERSE)
12588 /* Before we describe the FUNCTION_DECL itself, make sure that we
12589 have described its return type. */
12590 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
12592 /* And its virtual context. */
12593 if (DECL_VINDEX (decl) != NULL_TREE)
12594 gen_type_die (DECL_CONTEXT (decl), context_die);
12596 /* And its containing type. */
12597 origin = decl_class_context (decl);
12598 if (origin != NULL_TREE)
12599 gen_type_die_for_member (origin, decl, context_die);
12601 /* And its containing namespace. */
12602 declare_in_namespace (decl, context_die);
12605 /* Now output a DIE to represent the function itself. */
12606 gen_subprogram_die (decl, context_die);
12607 break;
12609 case TYPE_DECL:
12610 /* If we are in terse mode, don't generate any DIEs to represent any
12611 actual typedefs. */
12612 if (debug_info_level <= DINFO_LEVEL_TERSE)
12613 break;
12615 /* In the special case of a TYPE_DECL node representing the declaration
12616 of some type tag, if the given TYPE_DECL is marked as having been
12617 instantiated from some other (original) TYPE_DECL node (e.g. one which
12618 was generated within the original definition of an inline function) we
12619 have to generate a special (abbreviated) DW_TAG_structure_type,
12620 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. */
12621 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
12623 gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
12624 break;
12627 if (is_redundant_typedef (decl))
12628 gen_type_die (TREE_TYPE (decl), context_die);
12629 else
12630 /* Output a DIE to represent the typedef itself. */
12631 gen_typedef_die (decl, context_die);
12632 break;
12634 case LABEL_DECL:
12635 if (debug_info_level >= DINFO_LEVEL_NORMAL)
12636 gen_label_die (decl, context_die);
12637 break;
12639 case VAR_DECL:
12640 case RESULT_DECL:
12641 /* If we are in terse mode, don't generate any DIEs to represent any
12642 variable declarations or definitions. */
12643 if (debug_info_level <= DINFO_LEVEL_TERSE)
12644 break;
12646 /* Output any DIEs that are needed to specify the type of this data
12647 object. */
12648 gen_type_die (TREE_TYPE (decl), context_die);
12650 /* And its containing type. */
12651 origin = decl_class_context (decl);
12652 if (origin != NULL_TREE)
12653 gen_type_die_for_member (origin, decl, context_die);
12655 /* And its containing namespace. */
12656 declare_in_namespace (decl, context_die);
12658 /* Now output the DIE to represent the data object itself. This gets
12659 complicated because of the possibility that the VAR_DECL really
12660 represents an inlined instance of a formal parameter for an inline
12661 function. */
12662 origin = decl_ultimate_origin (decl);
12663 if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
12664 gen_formal_parameter_die (decl, context_die);
12665 else
12666 gen_variable_die (decl, context_die);
12667 break;
12669 case FIELD_DECL:
12670 /* Ignore the nameless fields that are used to skip bits but handle C++
12671 anonymous unions and structs. */
12672 if (DECL_NAME (decl) != NULL_TREE
12673 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
12674 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
12676 gen_type_die (member_declared_type (decl), context_die);
12677 gen_field_die (decl, context_die);
12679 break;
12681 case PARM_DECL:
12682 gen_type_die (TREE_TYPE (decl), context_die);
12683 gen_formal_parameter_die (decl, context_die);
12684 break;
12686 case NAMESPACE_DECL:
12687 gen_namespace_die (decl);
12688 break;
12690 default:
12691 /* Probably some frontend-internal decl. Assume we don't care. */
12692 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
12693 break;
12697 /* Add Ada "use" clause information for SGI Workshop debugger. */
12699 void
12700 dwarf2out_add_library_unit_info (const char *filename, const char *context_list)
12702 unsigned int file_index;
12704 if (filename != NULL)
12706 dw_die_ref unit_die = new_die (DW_TAG_module, comp_unit_die, NULL);
12707 tree context_list_decl
12708 = build_decl (LABEL_DECL, get_identifier (context_list),
12709 void_type_node);
12711 TREE_PUBLIC (context_list_decl) = TRUE;
12712 add_name_attribute (unit_die, context_list);
12713 file_index = lookup_filename (filename);
12714 add_AT_unsigned (unit_die, DW_AT_decl_file, file_index);
12715 add_pubname (context_list_decl, unit_die);
12719 /* Output debug information for global decl DECL. Called from toplev.c after
12720 compilation proper has finished. */
12722 static void
12723 dwarf2out_global_decl (tree decl)
12725 /* Output DWARF2 information for file-scope tentative data object
12726 declarations, file-scope (extern) function declarations (which had no
12727 corresponding body) and file-scope tagged type declarations and
12728 definitions which have not yet been forced out. */
12729 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
12730 dwarf2out_decl (decl);
12733 /* Output debug information for type decl DECL. Called from toplev.c
12734 and from language front ends (to record built-in types). */
12735 static void
12736 dwarf2out_type_decl (tree decl, int local)
12738 if (!local)
12739 dwarf2out_decl (decl);
12742 /* Output debug information for imported module or decl. */
12744 static void
12745 dwarf2out_imported_module_or_decl (tree decl, tree context)
12747 dw_die_ref imported_die, at_import_die;
12748 dw_die_ref scope_die;
12749 unsigned file_index;
12750 expanded_location xloc;
12752 if (debug_info_level <= DINFO_LEVEL_TERSE)
12753 return;
12755 gcc_assert (decl);
12757 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
12758 We need decl DIE for reference and scope die. First, get DIE for the decl
12759 itself. */
12761 /* Get the scope die for decl context. Use comp_unit_die for global module
12762 or decl. If die is not found for non globals, force new die. */
12763 if (!context)
12764 scope_die = comp_unit_die;
12765 else if (TYPE_P (context))
12766 scope_die = force_type_die (context);
12767 else
12768 scope_die = force_decl_die (context);
12770 /* For TYPE_DECL or CONST_DECL, lookup TREE_TYPE. */
12771 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
12772 at_import_die = force_type_die (TREE_TYPE (decl));
12773 else
12774 at_import_die = force_decl_die (decl);
12776 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
12777 if (TREE_CODE (decl) == NAMESPACE_DECL)
12778 imported_die = new_die (DW_TAG_imported_module, scope_die, context);
12779 else
12780 imported_die = new_die (DW_TAG_imported_declaration, scope_die, context);
12782 xloc = expand_location (input_location);
12783 file_index = lookup_filename (xloc.file);
12784 add_AT_unsigned (imported_die, DW_AT_decl_file, file_index);
12785 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
12786 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
12789 /* Write the debugging output for DECL. */
12791 void
12792 dwarf2out_decl (tree decl)
12794 dw_die_ref context_die = comp_unit_die;
12796 switch (TREE_CODE (decl))
12798 case ERROR_MARK:
12799 return;
12801 case FUNCTION_DECL:
12802 /* What we would really like to do here is to filter out all mere
12803 file-scope declarations of file-scope functions which are never
12804 referenced later within this translation unit (and keep all of ones
12805 that *are* referenced later on) but we aren't clairvoyant, so we have
12806 no idea which functions will be referenced in the future (i.e. later
12807 on within the current translation unit). So here we just ignore all
12808 file-scope function declarations which are not also definitions. If
12809 and when the debugger needs to know something about these functions,
12810 it will have to hunt around and find the DWARF information associated
12811 with the definition of the function.
12813 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
12814 nodes represent definitions and which ones represent mere
12815 declarations. We have to check DECL_INITIAL instead. That's because
12816 the C front-end supports some weird semantics for "extern inline"
12817 function definitions. These can get inlined within the current
12818 translation unit (an thus, we need to generate Dwarf info for their
12819 abstract instances so that the Dwarf info for the concrete inlined
12820 instances can have something to refer to) but the compiler never
12821 generates any out-of-lines instances of such things (despite the fact
12822 that they *are* definitions).
12824 The important point is that the C front-end marks these "extern
12825 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
12826 them anyway. Note that the C++ front-end also plays some similar games
12827 for inline function definitions appearing within include files which
12828 also contain `#pragma interface' pragmas. */
12829 if (DECL_INITIAL (decl) == NULL_TREE)
12830 return;
12832 /* If we're a nested function, initially use a parent of NULL; if we're
12833 a plain function, this will be fixed up in decls_for_scope. If
12834 we're a method, it will be ignored, since we already have a DIE. */
12835 if (decl_function_context (decl)
12836 /* But if we're in terse mode, we don't care about scope. */
12837 && debug_info_level > DINFO_LEVEL_TERSE)
12838 context_die = NULL;
12839 break;
12841 case VAR_DECL:
12842 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
12843 declaration and if the declaration was never even referenced from
12844 within this entire compilation unit. We suppress these DIEs in
12845 order to save space in the .debug section (by eliminating entries
12846 which are probably useless). Note that we must not suppress
12847 block-local extern declarations (whether used or not) because that
12848 would screw-up the debugger's name lookup mechanism and cause it to
12849 miss things which really ought to be in scope at a given point. */
12850 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
12851 return;
12853 /* If we are in terse mode, don't generate any DIEs to represent any
12854 variable declarations or definitions. */
12855 if (debug_info_level <= DINFO_LEVEL_TERSE)
12856 return;
12857 break;
12859 case NAMESPACE_DECL:
12860 if (debug_info_level <= DINFO_LEVEL_TERSE)
12861 return;
12862 if (lookup_decl_die (decl) != NULL)
12863 return;
12864 break;
12866 case TYPE_DECL:
12867 /* Don't emit stubs for types unless they are needed by other DIEs. */
12868 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
12869 return;
12871 /* Don't bother trying to generate any DIEs to represent any of the
12872 normal built-in types for the language we are compiling. */
12873 if (DECL_IS_BUILTIN (decl))
12875 /* OK, we need to generate one for `bool' so GDB knows what type
12876 comparisons have. */
12877 if ((get_AT_unsigned (comp_unit_die, DW_AT_language)
12878 == DW_LANG_C_plus_plus)
12879 && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
12880 && ! DECL_IGNORED_P (decl))
12881 modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
12883 return;
12886 /* If we are in terse mode, don't generate any DIEs for types. */
12887 if (debug_info_level <= DINFO_LEVEL_TERSE)
12888 return;
12890 /* If we're a function-scope tag, initially use a parent of NULL;
12891 this will be fixed up in decls_for_scope. */
12892 if (decl_function_context (decl))
12893 context_die = NULL;
12895 break;
12897 default:
12898 return;
12901 gen_decl_die (decl, context_die);
12904 /* Output a marker (i.e. a label) for the beginning of the generated code for
12905 a lexical block. */
12907 static void
12908 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
12909 unsigned int blocknum)
12911 function_section (current_function_decl);
12912 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
12915 /* Output a marker (i.e. a label) for the end of the generated code for a
12916 lexical block. */
12918 static void
12919 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
12921 function_section (current_function_decl);
12922 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
12925 /* Returns nonzero if it is appropriate not to emit any debugging
12926 information for BLOCK, because it doesn't contain any instructions.
12928 Don't allow this for blocks with nested functions or local classes
12929 as we would end up with orphans, and in the presence of scheduling
12930 we may end up calling them anyway. */
12932 static bool
12933 dwarf2out_ignore_block (tree block)
12935 tree decl;
12937 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
12938 if (TREE_CODE (decl) == FUNCTION_DECL
12939 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
12940 return 0;
12942 return 1;
12945 /* Lookup FILE_NAME (in the list of filenames that we know about here in
12946 dwarf2out.c) and return its "index". The index of each (known) filename is
12947 just a unique number which is associated with only that one filename. We
12948 need such numbers for the sake of generating labels (in the .debug_sfnames
12949 section) and references to those files numbers (in the .debug_srcinfo
12950 and.debug_macinfo sections). If the filename given as an argument is not
12951 found in our current list, add it to the list and assign it the next
12952 available unique index number. In order to speed up searches, we remember
12953 the index of the filename was looked up last. This handles the majority of
12954 all searches. */
12956 static unsigned
12957 lookup_filename (const char *file_name)
12959 size_t i, n;
12960 char *save_file_name;
12962 /* Check to see if the file name that was searched on the previous
12963 call matches this file name. If so, return the index. */
12964 if (file_table_last_lookup_index != 0)
12966 const char *last
12967 = VARRAY_CHAR_PTR (file_table, file_table_last_lookup_index);
12968 if (strcmp (file_name, last) == 0)
12969 return file_table_last_lookup_index;
12972 /* Didn't match the previous lookup, search the table. */
12973 n = VARRAY_ACTIVE_SIZE (file_table);
12974 for (i = 1; i < n; i++)
12975 if (strcmp (file_name, VARRAY_CHAR_PTR (file_table, i)) == 0)
12977 file_table_last_lookup_index = i;
12978 return i;
12981 /* Add the new entry to the end of the filename table. */
12982 file_table_last_lookup_index = n;
12983 save_file_name = (char *) ggc_strdup (file_name);
12984 VARRAY_PUSH_CHAR_PTR (file_table, save_file_name);
12985 VARRAY_PUSH_UINT (file_table_emitted, 0);
12987 return i;
12990 static int
12991 maybe_emit_file (int fileno)
12993 if (DWARF2_ASM_LINE_DEBUG_INFO && fileno > 0)
12995 if (!VARRAY_UINT (file_table_emitted, fileno))
12997 VARRAY_UINT (file_table_emitted, fileno) = ++emitcount;
12998 fprintf (asm_out_file, "\t.file %u ",
12999 VARRAY_UINT (file_table_emitted, fileno));
13000 output_quoted_string (asm_out_file,
13001 VARRAY_CHAR_PTR (file_table, fileno));
13002 fputc ('\n', asm_out_file);
13004 return VARRAY_UINT (file_table_emitted, fileno);
13006 else
13007 return fileno;
13010 static void
13011 init_file_table (void)
13013 /* Allocate the initial hunk of the file_table. */
13014 VARRAY_CHAR_PTR_INIT (file_table, 64, "file_table");
13015 VARRAY_UINT_INIT (file_table_emitted, 64, "file_table_emitted");
13017 /* Skip the first entry - file numbers begin at 1. */
13018 VARRAY_PUSH_CHAR_PTR (file_table, NULL);
13019 VARRAY_PUSH_UINT (file_table_emitted, 0);
13020 file_table_last_lookup_index = 0;
13023 /* Called by the final INSN scan whenever we see a var location. We
13024 use it to drop labels in the right places, and throw the location in
13025 our lookup table. */
13027 static void
13028 dwarf2out_var_location (rtx loc_note)
13030 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
13031 struct var_loc_node *newloc;
13032 rtx prev_insn;
13033 static rtx last_insn;
13034 static const char *last_label;
13036 if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
13037 return;
13038 prev_insn = PREV_INSN (loc_note);
13040 newloc = ggc_alloc_cleared (sizeof (struct var_loc_node));
13041 /* If the insn we processed last time is the previous insn
13042 and it is also a var location note, use the label we emitted
13043 last time. */
13044 if (last_insn != NULL_RTX
13045 && last_insn == prev_insn
13046 && NOTE_P (prev_insn)
13047 && NOTE_LINE_NUMBER (prev_insn) == NOTE_INSN_VAR_LOCATION)
13049 newloc->label = last_label;
13051 else
13053 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
13054 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
13055 loclabel_num++;
13056 newloc->label = ggc_strdup (loclabel);
13058 newloc->var_loc_note = loc_note;
13059 newloc->next = NULL;
13061 last_insn = loc_note;
13062 last_label = newloc->label;
13064 add_var_loc_to_decl (NOTE_VAR_LOCATION_DECL (loc_note), newloc);
13067 /* We need to reset the locations at the beginning of each
13068 function. We can't do this in the end_function hook, because the
13069 declarations that use the locations won't have been outputted when
13070 that hook is called. */
13072 static void
13073 dwarf2out_begin_function (tree unused ATTRIBUTE_UNUSED)
13075 htab_empty (decl_loc_table);
13078 /* Output a label to mark the beginning of a source code line entry
13079 and record information relating to this source line, in
13080 'line_info_table' for later output of the .debug_line section. */
13082 static void
13083 dwarf2out_source_line (unsigned int line, const char *filename)
13085 if (debug_info_level >= DINFO_LEVEL_NORMAL
13086 && line != 0)
13088 function_section (current_function_decl);
13090 /* If requested, emit something human-readable. */
13091 if (flag_debug_asm)
13092 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
13093 filename, line);
13095 if (DWARF2_ASM_LINE_DEBUG_INFO)
13097 unsigned file_num = lookup_filename (filename);
13099 file_num = maybe_emit_file (file_num);
13101 /* Emit the .loc directive understood by GNU as. */
13102 fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
13104 /* Indicate that line number info exists. */
13105 line_info_table_in_use++;
13107 /* Indicate that multiple line number tables exist. */
13108 if (DECL_SECTION_NAME (current_function_decl))
13109 separate_line_info_table_in_use++;
13111 else if (DECL_SECTION_NAME (current_function_decl))
13113 dw_separate_line_info_ref line_info;
13114 targetm.asm_out.internal_label (asm_out_file, SEPARATE_LINE_CODE_LABEL,
13115 separate_line_info_table_in_use);
13117 /* Expand the line info table if necessary. */
13118 if (separate_line_info_table_in_use
13119 == separate_line_info_table_allocated)
13121 separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
13122 separate_line_info_table
13123 = ggc_realloc (separate_line_info_table,
13124 separate_line_info_table_allocated
13125 * sizeof (dw_separate_line_info_entry));
13126 memset (separate_line_info_table
13127 + separate_line_info_table_in_use,
13129 (LINE_INFO_TABLE_INCREMENT
13130 * sizeof (dw_separate_line_info_entry)));
13133 /* Add the new entry at the end of the line_info_table. */
13134 line_info
13135 = &separate_line_info_table[separate_line_info_table_in_use++];
13136 line_info->dw_file_num = lookup_filename (filename);
13137 line_info->dw_line_num = line;
13138 line_info->function = current_function_funcdef_no;
13140 else
13142 dw_line_info_ref line_info;
13144 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
13145 line_info_table_in_use);
13147 /* Expand the line info table if necessary. */
13148 if (line_info_table_in_use == line_info_table_allocated)
13150 line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
13151 line_info_table
13152 = ggc_realloc (line_info_table,
13153 (line_info_table_allocated
13154 * sizeof (dw_line_info_entry)));
13155 memset (line_info_table + line_info_table_in_use, 0,
13156 LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
13159 /* Add the new entry at the end of the line_info_table. */
13160 line_info = &line_info_table[line_info_table_in_use++];
13161 line_info->dw_file_num = lookup_filename (filename);
13162 line_info->dw_line_num = line;
13167 /* Record the beginning of a new source file. */
13169 static void
13170 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
13172 if (flag_eliminate_dwarf2_dups)
13174 /* Record the beginning of the file for break_out_includes. */
13175 dw_die_ref bincl_die;
13177 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
13178 add_AT_string (bincl_die, DW_AT_name, filename);
13181 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13183 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13184 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
13185 dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
13186 lineno);
13187 maybe_emit_file (lookup_filename (filename));
13188 dw2_asm_output_data_uleb128 (lookup_filename (filename),
13189 "Filename we just started");
13193 /* Record the end of a source file. */
13195 static void
13196 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
13198 if (flag_eliminate_dwarf2_dups)
13199 /* Record the end of the file for break_out_includes. */
13200 new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
13202 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13204 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13205 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
13209 /* Called from debug_define in toplev.c. The `buffer' parameter contains
13210 the tail part of the directive line, i.e. the part which is past the
13211 initial whitespace, #, whitespace, directive-name, whitespace part. */
13213 static void
13214 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
13215 const char *buffer ATTRIBUTE_UNUSED)
13217 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13219 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13220 dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
13221 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
13222 dw2_asm_output_nstring (buffer, -1, "The macro");
13226 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
13227 the tail part of the directive line, i.e. the part which is past the
13228 initial whitespace, #, whitespace, directive-name, whitespace part. */
13230 static void
13231 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
13232 const char *buffer ATTRIBUTE_UNUSED)
13234 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13236 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13237 dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
13238 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
13239 dw2_asm_output_nstring (buffer, -1, "The macro");
13243 /* Set up for Dwarf output at the start of compilation. */
13245 static void
13246 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
13248 init_file_table ();
13250 /* Allocate the decl_die_table. */
13251 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
13252 decl_die_table_eq, NULL);
13254 /* Allocate the decl_loc_table. */
13255 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
13256 decl_loc_table_eq, NULL);
13258 /* Allocate the initial hunk of the decl_scope_table. */
13259 VARRAY_TREE_INIT (decl_scope_table, 256, "decl_scope_table");
13261 /* Allocate the initial hunk of the abbrev_die_table. */
13262 abbrev_die_table = ggc_alloc_cleared (ABBREV_DIE_TABLE_INCREMENT
13263 * sizeof (dw_die_ref));
13264 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
13265 /* Zero-th entry is allocated, but unused. */
13266 abbrev_die_table_in_use = 1;
13268 /* Allocate the initial hunk of the line_info_table. */
13269 line_info_table = ggc_alloc_cleared (LINE_INFO_TABLE_INCREMENT
13270 * sizeof (dw_line_info_entry));
13271 line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
13273 /* Zero-th entry is allocated, but unused. */
13274 line_info_table_in_use = 1;
13276 /* Generate the initial DIE for the .debug section. Note that the (string)
13277 value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
13278 will (typically) be a relative pathname and that this pathname should be
13279 taken as being relative to the directory from which the compiler was
13280 invoked when the given (base) source file was compiled. We will fill
13281 in this value in dwarf2out_finish. */
13282 comp_unit_die = gen_compile_unit_die (NULL);
13284 VARRAY_TREE_INIT (incomplete_types, 64, "incomplete_types");
13286 VARRAY_RTX_INIT (used_rtx_varray, 32, "used_rtx_varray");
13288 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
13289 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
13290 DEBUG_ABBREV_SECTION_LABEL, 0);
13291 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
13293 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
13294 DEBUG_INFO_SECTION_LABEL, 0);
13295 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
13296 DEBUG_LINE_SECTION_LABEL, 0);
13297 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
13298 DEBUG_RANGES_SECTION_LABEL, 0);
13299 named_section_flags (DEBUG_ABBREV_SECTION, SECTION_DEBUG);
13300 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
13301 named_section_flags (DEBUG_INFO_SECTION, SECTION_DEBUG);
13302 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
13303 named_section_flags (DEBUG_LINE_SECTION, SECTION_DEBUG);
13304 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
13306 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13308 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13309 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
13310 DEBUG_MACINFO_SECTION_LABEL, 0);
13311 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
13314 text_section ();
13315 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
13318 /* A helper function for dwarf2out_finish called through
13319 ht_forall. Emit one queued .debug_str string. */
13321 static int
13322 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
13324 struct indirect_string_node *node = (struct indirect_string_node *) *h;
13326 if (node->form == DW_FORM_strp)
13328 named_section_flags (DEBUG_STR_SECTION, DEBUG_STR_SECTION_FLAGS);
13329 ASM_OUTPUT_LABEL (asm_out_file, node->label);
13330 assemble_string (node->str, strlen (node->str) + 1);
13333 return 1;
13338 /* Clear the marks for a die and its children.
13339 Be cool if the mark isn't set. */
13341 static void
13342 prune_unmark_dies (dw_die_ref die)
13344 dw_die_ref c;
13345 die->die_mark = 0;
13346 for (c = die->die_child; c; c = c->die_sib)
13347 prune_unmark_dies (c);
13351 /* Given DIE that we're marking as used, find any other dies
13352 it references as attributes and mark them as used. */
13354 static void
13355 prune_unused_types_walk_attribs (dw_die_ref die)
13357 dw_attr_ref a;
13359 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
13361 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
13363 /* A reference to another DIE.
13364 Make sure that it will get emitted. */
13365 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
13367 else if (a->dw_attr == DW_AT_decl_file)
13369 /* A reference to a file. Make sure the file name is emitted. */
13370 a->dw_attr_val.v.val_unsigned =
13371 maybe_emit_file (a->dw_attr_val.v.val_unsigned);
13377 /* Mark DIE as being used. If DOKIDS is true, then walk down
13378 to DIE's children. */
13380 static void
13381 prune_unused_types_mark (dw_die_ref die, int dokids)
13383 dw_die_ref c;
13385 if (die->die_mark == 0)
13387 /* We haven't done this node yet. Mark it as used. */
13388 die->die_mark = 1;
13390 /* We also have to mark its parents as used.
13391 (But we don't want to mark our parents' kids due to this.) */
13392 if (die->die_parent)
13393 prune_unused_types_mark (die->die_parent, 0);
13395 /* Mark any referenced nodes. */
13396 prune_unused_types_walk_attribs (die);
13398 /* If this node is a specification,
13399 also mark the definition, if it exists. */
13400 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
13401 prune_unused_types_mark (die->die_definition, 1);
13404 if (dokids && die->die_mark != 2)
13406 /* We need to walk the children, but haven't done so yet.
13407 Remember that we've walked the kids. */
13408 die->die_mark = 2;
13410 /* Walk them. */
13411 for (c = die->die_child; c; c = c->die_sib)
13413 /* If this is an array type, we need to make sure our
13414 kids get marked, even if they're types. */
13415 if (die->die_tag == DW_TAG_array_type)
13416 prune_unused_types_mark (c, 1);
13417 else
13418 prune_unused_types_walk (c);
13424 /* Walk the tree DIE and mark types that we actually use. */
13426 static void
13427 prune_unused_types_walk (dw_die_ref die)
13429 dw_die_ref c;
13431 /* Don't do anything if this node is already marked. */
13432 if (die->die_mark)
13433 return;
13435 switch (die->die_tag) {
13436 case DW_TAG_const_type:
13437 case DW_TAG_packed_type:
13438 case DW_TAG_pointer_type:
13439 case DW_TAG_reference_type:
13440 case DW_TAG_volatile_type:
13441 case DW_TAG_typedef:
13442 case DW_TAG_array_type:
13443 case DW_TAG_structure_type:
13444 case DW_TAG_union_type:
13445 case DW_TAG_class_type:
13446 case DW_TAG_friend:
13447 case DW_TAG_variant_part:
13448 case DW_TAG_enumeration_type:
13449 case DW_TAG_subroutine_type:
13450 case DW_TAG_string_type:
13451 case DW_TAG_set_type:
13452 case DW_TAG_subrange_type:
13453 case DW_TAG_ptr_to_member_type:
13454 case DW_TAG_file_type:
13455 /* It's a type node --- don't mark it. */
13456 return;
13458 default:
13459 /* Mark everything else. */
13460 break;
13463 die->die_mark = 1;
13465 /* Now, mark any dies referenced from here. */
13466 prune_unused_types_walk_attribs (die);
13468 /* Mark children. */
13469 for (c = die->die_child; c; c = c->die_sib)
13470 prune_unused_types_walk (c);
13474 /* Remove from the tree DIE any dies that aren't marked. */
13476 static void
13477 prune_unused_types_prune (dw_die_ref die)
13479 dw_die_ref c, p, n;
13481 gcc_assert (die->die_mark);
13483 p = NULL;
13484 for (c = die->die_child; c; c = n)
13486 n = c->die_sib;
13487 if (c->die_mark)
13489 prune_unused_types_prune (c);
13490 p = c;
13492 else
13494 if (p)
13495 p->die_sib = n;
13496 else
13497 die->die_child = n;
13498 free_die (c);
13504 /* Remove dies representing declarations that we never use. */
13506 static void
13507 prune_unused_types (void)
13509 unsigned int i;
13510 limbo_die_node *node;
13512 /* Clear all the marks. */
13513 prune_unmark_dies (comp_unit_die);
13514 for (node = limbo_die_list; node; node = node->next)
13515 prune_unmark_dies (node->die);
13517 /* Set the mark on nodes that are actually used. */
13518 prune_unused_types_walk (comp_unit_die);
13519 for (node = limbo_die_list; node; node = node->next)
13520 prune_unused_types_walk (node->die);
13522 /* Also set the mark on nodes referenced from the
13523 pubname_table or arange_table. */
13524 for (i = 0; i < pubname_table_in_use; i++)
13525 prune_unused_types_mark (pubname_table[i].die, 1);
13526 for (i = 0; i < arange_table_in_use; i++)
13527 prune_unused_types_mark (arange_table[i], 1);
13529 /* Get rid of nodes that aren't marked. */
13530 prune_unused_types_prune (comp_unit_die);
13531 for (node = limbo_die_list; node; node = node->next)
13532 prune_unused_types_prune (node->die);
13534 /* Leave the marks clear. */
13535 prune_unmark_dies (comp_unit_die);
13536 for (node = limbo_die_list; node; node = node->next)
13537 prune_unmark_dies (node->die);
13540 /* Output stuff that dwarf requires at the end of every file,
13541 and generate the DWARF-2 debugging info. */
13543 static void
13544 dwarf2out_finish (const char *filename)
13546 limbo_die_node *node, *next_node;
13547 dw_die_ref die = 0;
13549 /* Add the name for the main input file now. We delayed this from
13550 dwarf2out_init to avoid complications with PCH. */
13551 add_name_attribute (comp_unit_die, filename);
13552 if (filename[0] != DIR_SEPARATOR)
13553 add_comp_dir_attribute (comp_unit_die);
13554 else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
13556 size_t i;
13557 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
13558 if (VARRAY_CHAR_PTR (file_table, i)[0] != DIR_SEPARATOR
13559 /* Don't add cwd for <built-in>. */
13560 && VARRAY_CHAR_PTR (file_table, i)[0] != '<')
13562 add_comp_dir_attribute (comp_unit_die);
13563 break;
13567 /* Traverse the limbo die list, and add parent/child links. The only
13568 dies without parents that should be here are concrete instances of
13569 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
13570 For concrete instances, we can get the parent die from the abstract
13571 instance. */
13572 for (node = limbo_die_list; node; node = next_node)
13574 next_node = node->next;
13575 die = node->die;
13577 if (die->die_parent == NULL)
13579 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
13581 if (origin)
13582 add_child_die (origin->die_parent, die);
13583 else if (die == comp_unit_die)
13585 else if (errorcount > 0 || sorrycount > 0)
13586 /* It's OK to be confused by errors in the input. */
13587 add_child_die (comp_unit_die, die);
13588 else
13590 /* In certain situations, the lexical block containing a
13591 nested function can be optimized away, which results
13592 in the nested function die being orphaned. Likewise
13593 with the return type of that nested function. Force
13594 this to be a child of the containing function.
13596 It may happen that even the containing function got fully
13597 inlined and optimized out. In that case we are lost and
13598 assign the empty child. This should not be big issue as
13599 the function is likely unreachable too. */
13600 tree context = NULL_TREE;
13602 gcc_assert (node->created_for);
13604 if (DECL_P (node->created_for))
13605 context = DECL_CONTEXT (node->created_for);
13606 else if (TYPE_P (node->created_for))
13607 context = TYPE_CONTEXT (node->created_for);
13609 gcc_assert (context && TREE_CODE (context) == FUNCTION_DECL);
13611 origin = lookup_decl_die (context);
13612 if (origin)
13613 add_child_die (origin, die);
13614 else
13615 add_child_die (comp_unit_die, die);
13620 limbo_die_list = NULL;
13622 /* Walk through the list of incomplete types again, trying once more to
13623 emit full debugging info for them. */
13624 retry_incomplete_types ();
13626 /* We need to reverse all the dies before break_out_includes, or
13627 we'll see the end of an include file before the beginning. */
13628 reverse_all_dies (comp_unit_die);
13630 if (flag_eliminate_unused_debug_types)
13631 prune_unused_types ();
13633 /* Generate separate CUs for each of the include files we've seen.
13634 They will go into limbo_die_list. */
13635 if (flag_eliminate_dwarf2_dups)
13636 break_out_includes (comp_unit_die);
13638 /* Traverse the DIE's and add add sibling attributes to those DIE's
13639 that have children. */
13640 add_sibling_attributes (comp_unit_die);
13641 for (node = limbo_die_list; node; node = node->next)
13642 add_sibling_attributes (node->die);
13644 /* Output a terminator label for the .text section. */
13645 text_section ();
13646 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
13648 /* Output the source line correspondence table. We must do this
13649 even if there is no line information. Otherwise, on an empty
13650 translation unit, we will generate a present, but empty,
13651 .debug_info section. IRIX 6.5 `nm' will then complain when
13652 examining the file. */
13653 if (! DWARF2_ASM_LINE_DEBUG_INFO)
13655 named_section_flags (DEBUG_LINE_SECTION, SECTION_DEBUG);
13656 output_line_info ();
13659 /* Output location list section if necessary. */
13660 if (have_location_lists)
13662 /* Output the location lists info. */
13663 named_section_flags (DEBUG_LOC_SECTION, SECTION_DEBUG);
13664 ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
13665 DEBUG_LOC_SECTION_LABEL, 0);
13666 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
13667 output_location_lists (die);
13668 have_location_lists = 0;
13671 /* We can only use the low/high_pc attributes if all of the code was
13672 in .text. */
13673 if (separate_line_info_table_in_use == 0)
13675 add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
13676 add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
13679 /* If it wasn't, we need to give .debug_loc and .debug_ranges an appropriate
13680 "base address". Use zero so that these addresses become absolute. */
13681 else if (have_location_lists || ranges_table_in_use)
13682 add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
13684 if (debug_info_level >= DINFO_LEVEL_NORMAL)
13685 add_AT_lbl_offset (comp_unit_die, DW_AT_stmt_list,
13686 debug_line_section_label);
13688 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13689 add_AT_lbl_offset (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
13691 /* Output all of the compilation units. We put the main one last so that
13692 the offsets are available to output_pubnames. */
13693 for (node = limbo_die_list; node; node = node->next)
13694 output_comp_unit (node->die, 0);
13696 output_comp_unit (comp_unit_die, 0);
13698 /* Output the abbreviation table. */
13699 named_section_flags (DEBUG_ABBREV_SECTION, SECTION_DEBUG);
13700 output_abbrev_section ();
13702 /* Output public names table if necessary. */
13703 if (pubname_table_in_use)
13705 named_section_flags (DEBUG_PUBNAMES_SECTION, SECTION_DEBUG);
13706 output_pubnames ();
13709 /* Output the address range information. We only put functions in the arange
13710 table, so don't write it out if we don't have any. */
13711 if (fde_table_in_use)
13713 named_section_flags (DEBUG_ARANGES_SECTION, SECTION_DEBUG);
13714 output_aranges ();
13717 /* Output ranges section if necessary. */
13718 if (ranges_table_in_use)
13720 named_section_flags (DEBUG_RANGES_SECTION, SECTION_DEBUG);
13721 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
13722 output_ranges ();
13725 /* Have to end the primary source file. */
13726 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13728 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13729 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
13730 dw2_asm_output_data (1, 0, "End compilation unit");
13733 /* If we emitted any DW_FORM_strp form attribute, output the string
13734 table too. */
13735 if (debug_str_hash)
13736 htab_traverse (debug_str_hash, output_indirect_string, NULL);
13738 #else
13740 /* This should never be used, but its address is needed for comparisons. */
13741 const struct gcc_debug_hooks dwarf2_debug_hooks;
13743 #endif /* DWARF2_DEBUGGING_INFO */
13745 #include "gt-dwarf2out.h"